summaryrefslogtreecommitdiff
path: root/lib/ultralib/src/debug/profile.c
blob: bdc47ae5d0d3c1641c14547708c060f9f6f0cd54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "PR/rdb.h"
#include "PR/ultratypes.h"
#include "PR/os.h"
#include "PR/ultraerror.h"
#include "PR/ultralog.h"
#include "PR/sptask.h"
#include "PRinternal/osint.h"
#include "PRinternal/macros.h"
#include "osint_debug.h"

#ifndef _FINALROM

OSTimer __osProfTimer;
OSMesg __osProfTimerMsg;

OSMesgQueue __osProfFlushMQ ALIGNED(0x8);
OSMesg __osProfFlushMesg;

OSMesgQueue __osProfAckMQ ALIGNED(0x8);
OSMesg __osProfAckMesg;

u32 __osProfTimerPeriod;

u32 __osProfNumSections;

static u32 __osProfileActive = FALSE;
static u32 __osProfileIOActive = FALSE;

STACK(__osProfileIOStack, 0x960) ALIGNED(0x10);

static OSThread __osProfileIOThread;

void osProfSendWord(u32 word);

void __osProfileIO(void* arg) {
    s32 totalBytes;
    u32 bytesThisBlock;
    u32 ct;
    u8* sendPtr;
    OSProf* t;

    while (TRUE) {
        osRecvMesg(&__osProfFlushMQ, NULL, OS_MESG_BLOCK);
        osProfSendWord(__osProfNumSections);
        osProfSendWord(__osProfTimerPeriod);
        osProfSendWord(__osProfileOverflowBin);

        for (t = __osProfileList; t < __osProfileListEnd; t++) {
            osProfSendWord(t->text_start);
            osProfSendWord(t->histo_size);
            osRecvMesg(&__osProfAckMQ, NULL, OS_MESG_BLOCK);

            totalBytes = t->histo_size * 2;
            sendPtr = t->histo_base;
            while (totalBytes > 0) {
                bytesThisBlock = (totalBytes < 0x800U) ? totalBytes : 0x800U;

                ct = 0;
                while (ct < bytesThisBlock) {
                    ct += __osRdbSend(sendPtr + ct, bytesThisBlock - ct, RDB_TYPE_GtoH_PROF_DATA);
                }

                sendPtr += bytesThisBlock;
                totalBytes -= bytesThisBlock;
                osRecvMesg(&__osProfAckMQ, NULL, OS_MESG_BLOCK);
            }
        }
    }
}

void osProfSendWord(u32 word) {
    u32 ct = 0;
    u8* sendPtr = &word;

    while (ct < sizeof(word)) {
        ct += __osRdbSend(sendPtr + ct, sizeof(word) - ct, RDB_TYPE_GtoH_PROF_DATA);
    }
}

void osProfileFlush(void) {
    osSendMesg(&__osProfFlushMQ, NULL, OS_MESG_BLOCK);
}

void osProfileInit(OSProf* profp, u32 profcnt) {
    u32 i;
    OSProf* t;

#if !defined(NDEBUG) && BUILD_VERSION >= VERSION_K
    if (__osProfileActive) {
        __osError(ERR_OSPROFILEINIT_STR, 0);
        return;
    }

    if (profcnt == 0) {
        __osError(ERR_OSPROFILEINIT_CNT, 1, profcnt);
        return;
    }
#endif

    for (t = profp; t < profp + profcnt; t++) {
#ifndef NDEBUG
        if ((u32)t->histo_base & 1) {
            __osError(ERR_OSPROFILEINIT_ALN, 1, t->histo_base);
            return;
        }

        if (t->text_start >= t->text_end) {
            __osError(ERR_OSPROFILEINIT_ORD, 2, t->text_start, t->text_end);
            return;
        }

        if (((u32)(t->text_end - t->text_start) / 4) > t->histo_size) {
            __osError(ERR_OSPROFILEINIT_SIZ, 1, t->histo_size);
            return;
        }
#endif

        for (i = 0; i < t->histo_size; i++) {
            t->histo_base[i] = 0;
        }
    }

    __osProfileActive = FALSE;
    __osProfileOverflowBin = 0;
    __osProfileList = profp;
    __osProfileListEnd = profp + profcnt;
    __osProfNumSections = profcnt;
    if (!__osProfileIOActive) {
        osCreateMesgQueue(&__osProfFlushMQ, &__osProfFlushMesg, 1);
        osSetEventMesg(OS_EVENT_RDB_FLUSH_PROF, &__osProfFlushMQ, 0);
        osCreateMesgQueue(&__osProfAckMQ, &__osProfAckMesg, 1);
        osSetEventMesg(OS_EVENT_RDB_ACK_PROF, &__osProfAckMQ, 0);
        osCreateThread(&__osProfileIOThread, 0, __osProfileIO, NULL, STACK_START(__osProfileIOStack), 0x81);
        osStartThread(&__osProfileIOThread);
        __osProfileIOActive = TRUE;
    }
}

void osProfileStart(u32 microseconds) {
#ifndef NDEBUG
    if (microseconds < 50) {
        __osError(ERR_OSPROFILESTART_TIME, 1, microseconds);
        return;
    }

    if (__osProfileActive) {
        __osError(ERR_OSPROFILESTART_FLAG, 0);
        return;
    }
#endif

    osCreateMesgQueue(&__osProfTimerQ, &__osProfTimerMsg, 1);
    osSetTimer(&__osProfTimer, 0, OS_USEC_TO_CYCLES(microseconds), &__osProfTimerQ, NULL);
    __osProfTimerPeriod = microseconds;
    __osProfileActive = TRUE;
}

void osProfileStop(void) {
#ifndef NDEBUG
    if (!__osProfileActive) {
        __osError(ERR_OSPROFILESTOP_FLAG, 0);
        return;
    }
#endif

    if (osStopTimer(&__osProfTimer) < 0) {
#ifndef NDEBUG
        __osError(ERR_OSPROFILESTOP_TIMER, 0);
        return;
#endif
    }

    __osProfileActive = FALSE;
}

#endif