aboutsummaryrefslogtreecommitdiffstats
path: root/src/profiling.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/profiling.c')
-rw-r--r--src/profiling.c157
1 files changed, 157 insertions, 0 deletions
diff --git a/src/profiling.c b/src/profiling.c
new file mode 100644
index 0000000..2d230df
--- /dev/null
+++ b/src/profiling.c
@@ -0,0 +1,157 @@
1#ifndef PROF_ENABLE
2#define PROF_ENABLE 0
3#endif
4
5#if PROF_ENABLE > 0 && PROF_ENABLE < 3
6
7#ifndef PROF_N_FRAMES
8#define PROF_N_FRAMES 15
9#endif
10
11// Profile method 1: Average per N frames.
12#if PROF_ENABLE == 1
13#define TEXT_ENABLE 1
14#define PROF(F,VAR) \
15 do { \
16 u32 __tmp_prof = profile_measure();\
17 (F);\
18 (VAR) += profile_measure() - __tmp_prof;\
19 } while (0)
20
21// Profile method 2: Maximum in N frames.
22#elif PROF_ENABLE == 2
23#define TEXT_ENABLE 1
24#define PROF(F,VAR) \
25 do { \
26 u32 __tmp_prof = profile_measure();\
27 (F);\
28 (VAR) = MAX(profile_measure() - __tmp_prof, (VAR));\
29 } while (0)
30#endif
31
32#ifndef PROF_SHOW_X
33#define PROF_SHOW_X 0
34#endif
35#ifndef PROF_SHOW_Y
36#define PROF_SHOW_Y 0
37#endif
38
39#define PROF_SHOW() \
40 do { \
41 txt_position((PROF_SHOW_X), (PROF_SHOW_Y));\
42 txt_printf("INPUT %.8lu\n", avg_input_cycles);\
43 txt_printf("EVAL %.8lu\n", avg_eval_cycles);\
44 txt_printf("VIDEO\n");\
45 txt_printf(">PIX %.8lu\n", avg_ppu_pixel_cycles);\
46 txt_printf(">FILL %.8lu\n", avg_ppu_fill_cycles);\
47 txt_printf(">1BPP %.8lu\n", avg_ppu_icn_cycles);\
48 txt_printf(">2BPP %.8lu\n", avg_ppu_chr_cycles);\
49 txt_printf(">FLIP %.8lu\n", avg_flip_cycles);\
50 txt_printf("AUDIO %.8lu\n", avg_mix_cycles);\
51 txt_printf("TOTAL %.8lu\n", avg_frame_cycles);\
52 u32 frame_time =\
53 FP_DIV(\
54 FP_NUM(avg_frame_cycles + 1, 2),\
55 FP_NUM(2809, 2),\
56 2) * 166;\
57 u32 fps =\
58 FP_DIV(\
59 FP_NUM(280896 * 60, 2),\
60 FP_NUM(avg_frame_cycles + 1, 2),\
61 2);\
62 txt_printf("TIME %.8lu\n", frame_time >> 2);\
63 txt_printf("FPS %.8lu\n", (fps >> 2) + 1);\
64 screen_fill(BG_BACK, 0, 0, 8 * 16, 8 * 12, 2);\
65 } while (0)
66
67static u32 prof_frame_counter = 0;
68
69static u32 frame_cycles = 0;
70static u32 ppu_pixel_cycles = 0;
71static u32 ppu_fill_cycles = 0;
72static u32 ppu_chr_cycles = 0;
73static u32 ppu_icn_cycles = 0;
74static u32 flip_cycles = 0;
75static u32 eval_cycles = 0;
76static u32 input_cycles = 0;
77static u32 mix_cycles = 0;
78
79static u32 avg_ppu_pixel_cycles = 0;
80static u32 avg_ppu_fill_cycles = 0;
81static u32 avg_ppu_chr_cycles = 0;
82static u32 avg_ppu_icn_cycles = 0;
83static u32 avg_flip_cycles = 0;
84static u32 avg_eval_cycles = 0;
85static u32 avg_input_cycles = 0;
86static u32 avg_mix_cycles = 0;
87static u32 avg_frame_cycles = 0;
88
89#if PROF_ENABLE == 1
90#define FRAME_START()\
91 do { \
92 if (prof_frame_counter == PROF_N_FRAMES) {\
93 avg_ppu_pixel_cycles = ppu_pixel_cycles / prof_frame_counter;\
94 avg_ppu_fill_cycles = ppu_fill_cycles / prof_frame_counter;\
95 avg_ppu_chr_cycles = ppu_chr_cycles / prof_frame_counter;\
96 avg_ppu_icn_cycles = ppu_icn_cycles / prof_frame_counter;\
97 avg_flip_cycles = flip_cycles / prof_frame_counter;\
98 avg_eval_cycles = eval_cycles / prof_frame_counter;\
99 avg_input_cycles = input_cycles / prof_frame_counter;\
100 avg_mix_cycles = mix_cycles / prof_frame_counter;\
101 avg_frame_cycles = frame_cycles / prof_frame_counter;\
102 prof_frame_counter = 0;\
103 frame_cycles = 0;\
104 ppu_pixel_cycles = 0;\
105 ppu_fill_cycles = 0;\
106 ppu_chr_cycles = 0;\
107 ppu_icn_cycles = 0;\
108 flip_cycles = 0;\
109 eval_cycles = 0;\
110 input_cycles = 0;\
111 mix_cycles = 0;\
112 }\
113 profile_start();\
114 } while (0)
115#elif PROF_ENABLE == 2
116#define FRAME_START()\
117 do { \
118 if (prof_frame_counter == PROF_N_FRAMES) {\
119 avg_ppu_pixel_cycles = ppu_pixel_cycles;\
120 avg_ppu_fill_cycles = ppu_fill_cycles;\
121 avg_ppu_chr_cycles = ppu_chr_cycles;\
122 avg_ppu_icn_cycles = ppu_icn_cycles;\
123 avg_flip_cycles = flip_cycles;\
124 avg_eval_cycles = eval_cycles;\
125 avg_input_cycles = input_cycles;\
126 avg_mix_cycles = mix_cycles;\
127 avg_frame_cycles = frame_cycles / prof_frame_counter;\
128 prof_frame_counter = 0;\
129 frame_cycles = 0;\
130 ppu_pixel_cycles = 0;\
131 ppu_fill_cycles = 0;\
132 ppu_chr_cycles = 0;\
133 ppu_icn_cycles = 0;\
134 flip_cycles = 0;\
135 eval_cycles = 0;\
136 input_cycles = 0;\
137 mix_cycles = 0;\
138 }\
139 profile_start();\
140 } while (0)
141#endif
142
143#define FRAME_END() \
144 do { \
145 prof_frame_counter++;\
146 frame_cycles += profile_stop();\
147 } while (0)
148
149#else
150
151// No profiling.
152#define PROF(F,VAR) (F)
153#define PROF_SHOW()
154#define FRAME_START()
155#define FRAME_END()
156#endif
157