-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofiler.c
More file actions
301 lines (257 loc) · 7.28 KB
/
profiler.c
File metadata and controls
301 lines (257 loc) · 7.28 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/**
* C Profiler - Implementation
*
* Copyright (c) 2025 Max Base
* Licensed under MIT License
*/
#define _POSIX_C_SOURCE 199309L
#include "profiler.h"
#include <stdlib.h>
#include <string.h>
#include <limits.h>
/* Global profiler configuration */
static profiler_config_t g_profiler_config = {
.log_file = NULL,
.auto_log = false,
.enabled = true
};
/**
* Initialize the profiler
*/
void profiler_init(void) {
g_profiler_config.enabled = true;
g_profiler_config.auto_log = false;
g_profiler_config.log_file = NULL;
}
/**
* Shutdown the profiler and clean up resources
*/
void profiler_shutdown(void) {
profiler_close_log_file();
g_profiler_config.enabled = false;
}
/**
* Enable or disable profiling
*/
void profiler_enable(bool enable) {
g_profiler_config.enabled = enable;
}
/**
* Set log file for automatic logging
*/
void profiler_set_log_file(const char *filename) {
if (!filename) {
return;
}
/* Close existing log file if open */
profiler_close_log_file();
/* Open new log file */
g_profiler_config.log_file = fopen(filename, "a");
if (g_profiler_config.log_file) {
g_profiler_config.auto_log = true;
fprintf(g_profiler_config.log_file, "=== Profiler Log Started ===\n");
fflush(g_profiler_config.log_file);
}
}
/**
* Close the log file
*/
void profiler_close_log_file(void) {
if (g_profiler_config.log_file) {
fprintf(g_profiler_config.log_file, "=== Profiler Log Ended ===\n");
fclose(g_profiler_config.log_file);
g_profiler_config.log_file = NULL;
g_profiler_config.auto_log = false;
}
}
/**
* Get current time in nanoseconds
*/
uint64_t profiler_get_time_ns(void) {
struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
return 0;
}
return (uint64_t)ts.tv_sec * 1000000000ULL + (uint64_t)ts.tv_nsec;
}
/**
* Start a timer
*/
void profiler_timer_start(profiler_timer_t *timer, const char *name) {
if (!timer || !g_profiler_config.enabled) {
return;
}
timer->name = name;
timer->is_running = true;
timer->elapsed_ns = 0;
if (clock_gettime(CLOCK_MONOTONIC, &timer->start_time) != 0) {
timer->is_running = false;
}
}
/**
* Stop a timer and calculate elapsed time
*/
void profiler_timer_stop(profiler_timer_t *timer) {
struct timespec end_time;
if (!timer || !timer->is_running) {
return;
}
/* Allow stopping even when disabled, but don't update timing if disabled */
if (clock_gettime(CLOCK_MONOTONIC, &end_time) != 0) {
timer->is_running = false;
return;
}
timer->end_time = end_time;
timer->is_running = false;
/* Calculate elapsed time in nanoseconds */
uint64_t start_ns = (uint64_t)timer->start_time.tv_sec * 1000000000ULL +
(uint64_t)timer->start_time.tv_nsec;
uint64_t end_ns = (uint64_t)timer->end_time.tv_sec * 1000000000ULL +
(uint64_t)timer->end_time.tv_nsec;
timer->elapsed_ns = end_ns - start_ns;
/* Auto-log only if enabled and configured */
if (g_profiler_config.enabled && g_profiler_config.auto_log) {
profiler_log_timer(timer);
}
}
/**
* Get elapsed time in nanoseconds
*/
uint64_t profiler_timer_elapsed_ns(const profiler_timer_t *timer) {
if (!timer) {
return 0;
}
return timer->elapsed_ns;
}
/**
* Get elapsed time in microseconds
*/
double profiler_timer_elapsed_us(const profiler_timer_t *timer) {
if (!timer) {
return 0.0;
}
return (double)timer->elapsed_ns / 1000.0;
}
/**
* Get elapsed time in milliseconds
*/
double profiler_timer_elapsed_ms(const profiler_timer_t *timer) {
if (!timer) {
return 0.0;
}
return (double)timer->elapsed_ns / 1000000.0;
}
/**
* Get elapsed time in seconds
*/
double profiler_timer_elapsed_s(const profiler_timer_t *timer) {
if (!timer) {
return 0.0;
}
return (double)timer->elapsed_ns / 1000000000.0;
}
/**
* Initialize statistics structure
*/
void profiler_stats_init(profiler_stats_t *stats, const char *name) {
if (!stats) {
return;
}
stats->name = name;
stats->count = 0;
stats->total_ns = 0;
stats->min_ns = UINT64_MAX;
stats->max_ns = 0;
stats->avg_ns = 0.0;
}
/**
* Add a measurement to statistics
*/
void profiler_stats_add(profiler_stats_t *stats, uint64_t elapsed_ns) {
if (!stats || !g_profiler_config.enabled) {
return;
}
stats->count++;
stats->total_ns += elapsed_ns;
if (elapsed_ns < stats->min_ns) {
stats->min_ns = elapsed_ns;
}
if (elapsed_ns > stats->max_ns) {
stats->max_ns = elapsed_ns;
}
stats->avg_ns = (double)stats->total_ns / (double)stats->count;
}
/**
* Print statistics to stdout
*/
void profiler_stats_print(const profiler_stats_t *stats) {
if (!stats || stats->count == 0) {
return;
}
printf("[PROFILER STATS] %s:\n", stats->name);
printf(" Count: %lu\n", stats->count);
printf(" Total: %.3f ms\n", (double)stats->total_ns / 1000000.0);
printf(" Average: %.3f us\n", stats->avg_ns / 1000.0);
printf(" Min: %.3f us\n", (double)stats->min_ns / 1000.0);
printf(" Max: %.3f us\n", (double)stats->max_ns / 1000.0);
}
/**
* Log statistics to file
*/
void profiler_stats_log(const profiler_stats_t *stats, FILE *file) {
if (!stats || !file || stats->count == 0) {
return;
}
fprintf(file, "[PROFILER STATS] %s:\n", stats->name);
fprintf(file, " Count: %lu\n", stats->count);
fprintf(file, " Total: %.3f ms\n", (double)stats->total_ns / 1000000.0);
fprintf(file, " Average: %.3f us\n", stats->avg_ns / 1000.0);
fprintf(file, " Min: %.3f us\n", (double)stats->min_ns / 1000.0);
fprintf(file, " Max: %.3f us\n", (double)stats->max_ns / 1000.0);
fflush(file);
}
/**
* Reset statistics
*/
void profiler_stats_reset(profiler_stats_t *stats) {
if (!stats) {
return;
}
const char *name = stats->name;
profiler_stats_init(stats, name);
}
/**
* Print timer result to stdout
*/
void profiler_print_timer(const profiler_timer_t *timer) {
if (!timer || timer->is_running) {
return;
}
const char *name = timer->name ? timer->name : "unnamed";
double elapsed_ms = profiler_timer_elapsed_ms(timer);
if (elapsed_ms >= 1.0) {
printf("[PROFILER] %s: %.3f ms\n", name, elapsed_ms);
} else {
double elapsed_us = profiler_timer_elapsed_us(timer);
printf("[PROFILER] %s: %.3f us\n", name, elapsed_us);
}
}
/**
* Log timer result to file
*/
void profiler_log_timer(const profiler_timer_t *timer) {
if (!timer || timer->is_running || !g_profiler_config.log_file) {
return;
}
const char *name = timer->name ? timer->name : "unnamed";
double elapsed_ms = profiler_timer_elapsed_ms(timer);
if (elapsed_ms >= 1.0) {
fprintf(g_profiler_config.log_file, "[PROFILER] %s: %.3f ms\n",
name, elapsed_ms);
} else {
double elapsed_us = profiler_timer_elapsed_us(timer);
fprintf(g_profiler_config.log_file, "[PROFILER] %s: %.3f us\n",
name, elapsed_us);
}
fflush(g_profiler_config.log_file);
}