aboutsummaryrefslogtreecommitdiffstats
path: root/src/profiling.c
blob: 0255552ae76cbcf3b0fd114c5e28f24d51761643 (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
177
178
179
180
181
182
183
184
185
186
187
188
189
//
// Profiling macros.
//

#ifndef PROF_ENABLE
#define PROF_ENABLE 0
#endif

#if PROF_ENABLE > 0 && PROF_ENABLE < 3

#ifndef PROF_N_FRAMES
#define PROF_N_FRAMES 30
#endif

// Profile method 1: Average per N frames.
#if PROF_ENABLE == 1
#define TEXT_ENABLE 1
#define PROF(F,VAR) \
    do { \
        u32 __tmp_prof = profile_measure();\
        F;\
        (VAR) += profile_measure() - __tmp_prof;\
    } while (0)

// Profile method 2: Maximum in N frames.
#elif PROF_ENABLE == 2
#define TEXT_ENABLE 1
#define PROF(F,VAR) \
    do { \
        u32 __tmp_prof = profile_measure();\
        (F);\
        (VAR) = MAX(profile_measure() - __tmp_prof, (VAR));\
    } while (0)
#endif

#ifndef PROF_SHOW_X
#define PROF_SHOW_X 0
#endif
#ifndef PROF_SHOW_Y
#define PROF_SHOW_Y 0
#endif

static bool profile_show = true;

#define PROF_SHOW() \
    do { \
        if (key_tap(KEY_START)) {\
            profile_show ^= 1;\
        }\
        if (profile_show) {\
            txt_color(1);\
            txt_position((PROF_SHOW_X), (PROF_SHOW_Y));\
            draw_filled_rect((PROF_SHOW_X), (PROF_SHOW_X), 8 * 14, 8 * 10, 0);\
            txt_printf("VIDEO\n");\
            txt_printf(">CLEAR  %.8lu\n", avg_clear_cycles);\
            txt_printf(">FLIP   %.8lu\n", avg_flip_cycles);\
            txt_printf("SEQUENCER RENDER\n");\
            txt_printf(">TRIGS  %.8lu\n", avg_draw_trigs_cycles);\
            txt_printf(">BTNS   %.8lu\n", avg_draw_btns_cycles);\
            txt_printf(">PARAM  %.8lu\n", avg_draw_param_cycles);\
            txt_printf(">PIANO  %.8lu\n", avg_draw_piano_cycles);\
            txt_printf(">CURSOR %.8lu\n", avg_draw_cursor_cycles);\
            txt_printf("UPDATE  %.8lu\n", avg_update_cycles);\
            txt_printf("INPUT   %.8lu\n", avg_input_cycles);\
            txt_printf("RENDER  %.8lu\n", avg_render_cycles);\
            txt_printf("TOTAL   %.8lu\n", avg_frame_cycles);\
            txt_render();\
        }\
        u32 frame_time =\
            FP_DIV(\
                FP_NUM(avg_frame_cycles + 1, 2),\
                FP_NUM(2809, 2),\
                2) * 166;\
        u32 fps =\
            FP_DIV(\
                FP_NUM(280896 * 60, 2),\
                FP_NUM(avg_frame_cycles + 1, 2),\
                2);\
        draw_filled_rect(8 * 18, 0, 239, 16, 0);\
        txt_drawf("TIME:     %.6lu", 8 * 18, 0, 1, frame_time >> 2);\
        txt_drawf("MAX FPS:    %.4lu", 8 * 18, 8, 1, (fps >> 2) + 1);\
    } while (0)

static u32 prof_frame_counter = 0;

static u32 frame_cycles       = 0;
static u32 flip_cycles        = 0;
static u32 clear_cycles       = 0;
static u32 input_cycles       = 0;
static u32 draw_trigs_cycles  = 0;
static u32 draw_btn_cycles    = 0;
static u32 draw_piano_cycles  = 0;
static u32 draw_param_cycles  = 0;
static u32 draw_cursor_cycles = 0;
static u32 render_cycles      = 0;
static u32 update_cycles      = 0;

static u32 avg_frame_cycles       = 0;
static u32 avg_flip_cycles        = 0;
static u32 avg_clear_cycles       = 0;
static u32 avg_input_cycles       = 0;
static u32 avg_draw_trigs_cycles  = 0;
static u32 avg_draw_btns_cycles   = 0;
static u32 avg_draw_piano_cycles  = 0;
static u32 avg_draw_param_cycles  = 0;
static u32 avg_draw_cursor_cycles = 0;
static u32 avg_render_cycles      = 0;
static u32 avg_update_cycles      = 0;

#if PROF_ENABLE == 1
#define FRAME_START()\
    do { \
        if (prof_frame_counter == PROF_N_FRAMES) {\
            avg_frame_cycles       = frame_cycles       / prof_frame_counter;\
            avg_flip_cycles        = flip_cycles        / prof_frame_counter;\
            avg_clear_cycles       = clear_cycles       / prof_frame_counter;\
            avg_draw_trigs_cycles  = draw_trigs_cycles  / prof_frame_counter;\
            avg_draw_btns_cycles   = draw_btn_cycles    / prof_frame_counter;\
            avg_draw_piano_cycles  = draw_piano_cycles  / prof_frame_counter;\
            avg_draw_param_cycles  = draw_param_cycles  / prof_frame_counter;\
            avg_draw_cursor_cycles = draw_cursor_cycles / prof_frame_counter;\
            avg_input_cycles       = input_cycles       / prof_frame_counter;\
            avg_render_cycles      = render_cycles      / prof_frame_counter;\
            avg_update_cycles      = update_cycles      / prof_frame_counter;\
            frame_cycles           = 0;\
            flip_cycles            = 0;\
            clear_cycles           = 0;\
            input_cycles           = 0;\
            render_cycles          = 0;\
            update_cycles          = 0;\
            draw_trigs_cycles      = 0;\
            draw_param_cycles      = 0;\
            draw_cursor_cycles     = 0;\
            draw_btn_cycles        = 0;\
            draw_piano_cycles      = 0;\
            prof_frame_counter     = 0;\
        }\
        profile_start();\
    } while (0)
#elif PROF_ENABLE == 2
#define FRAME_START()\
    do { \
        if (prof_frame_counter == PROF_N_FRAMES) {\
            avg_frame_cycles      = frame_cycles;\
            avg_flip_cycles       = flip_cycles;\
            avg_clear_cycles      = clear_cycles;\
            avg_line_cycles       = line_cycles;\
            avg_rect_cycles       = rect_cycles;\
            avg_fill_rect_cycles  = fill_rect_cycles;\
            avg_chr_cycles        = chr_cycles;\
            avg_icn_cycles        = icn_cycles;\
            avg_txt_drawf_cycles  = txt_drawf_cycles;\
            avg_txt_printf_cycles = txt_printf_cycles;\
            avg_txt_render_cycles = txt_render_cycles;\
            avg_txt_clear_cycles  = txt_clear_cycles;\
            avg_input_cycles      = input_cycles;\
            frame_cycles          = 0;\
            flip_cycles           = 0;\
            clear_cycles          = 0;\
            line_cycles           = 0;\
            rect_cycles           = 0;\
            fill_rect_cycles      = 0;\
            chr_cycles            = 0;\
            icn_cycles            = 0;\
            txt_drawf_cycles      = 0;\
            txt_printf_cycles     = 0;\
            txt_render_cycles     = 0;\
            txt_clear_cycles      = 0;\
            input_cycles          = 0;\
            prof_frame_counter    = 0;\
        }\
        profile_start();\
    } while (0)
#endif

#define FRAME_END() \
    do { \
        prof_frame_counter++;\
        frame_cycles += profile_stop();\
    } while (0)

#else

// No profiling.
#define PROF(F,VAR) (F)
#define PROF_SHOW()
#define FRAME_START()
#define FRAME_END()
#endif