|
| 1 | +/* |
| 2 | + * perf_counters.h - lightweight, opt-in instrumentation counters. |
| 3 | + * |
| 4 | + * Define BENCH_PROFILE at compile time to enable. Otherwise every macro |
| 5 | + * expands to (void)0 and there is no runtime, code-size, or symbol cost. |
| 6 | + * |
| 7 | + * Usage: |
| 8 | + * |
| 9 | + * #include "perf_counters.h" |
| 10 | + * |
| 11 | + * PERF_COUNTER(blitter_inner); |
| 12 | + * PERF_COUNTER(blitter_phrase_reads); |
| 13 | + * |
| 14 | + * void hot(void) { |
| 15 | + * PERF_INC(blitter_inner); |
| 16 | + * PERF_ADD(blitter_phrase_reads, 2); |
| 17 | + * } |
| 18 | + * |
| 19 | + * // Somewhere at shutdown (e.g., test harness atexit): |
| 20 | + * perf_counters_dump(stderr); |
| 21 | + * |
| 22 | + * Counters self-register via constructor functions, so PERF_COUNTER must |
| 23 | + * appear at file scope. Only one definition per name across the program. |
| 24 | + * |
| 25 | + * C89-clean. No designated initializers, no mid-block declarations. |
| 26 | + */ |
| 27 | +#ifndef VJ_PERF_COUNTERS_H |
| 28 | +#define VJ_PERF_COUNTERS_H |
| 29 | + |
| 30 | +#include <stdio.h> |
| 31 | + |
| 32 | +#ifdef __cplusplus |
| 33 | +extern "C" { |
| 34 | +#endif |
| 35 | + |
| 36 | +#ifdef BENCH_PROFILE |
| 37 | + |
| 38 | +typedef struct perf_counter_entry |
| 39 | +{ |
| 40 | + const char *name; |
| 41 | + unsigned long long *value; |
| 42 | + struct perf_counter_entry *next; |
| 43 | +} perf_counter_entry_t; |
| 44 | + |
| 45 | +void perf_counters_register(perf_counter_entry_t *entry); |
| 46 | +void perf_counters_dump(FILE *out); |
| 47 | +void perf_counters_reset(void); |
| 48 | + |
| 49 | +#define PERF_COUNTER(name) \ |
| 50 | + static unsigned long long perf_##name = 0; \ |
| 51 | + static perf_counter_entry_t perf_entry_##name = \ |
| 52 | + { #name, &perf_##name, (perf_counter_entry_t *)0 }; \ |
| 53 | + __attribute__((constructor)) \ |
| 54 | + static void perf_register_##name(void) { \ |
| 55 | + perf_counters_register(&perf_entry_##name); \ |
| 56 | + } \ |
| 57 | + typedef int perf_##name##_decl_semicolon_eater |
| 58 | + |
| 59 | +/* PERF_INC / PERF_ADD are expressions of integer type (not statements), |
| 60 | + * so they can be embedded in declaration initializers via the comma |
| 61 | + * operator without violating C89's no-decl-after-statement rule: |
| 62 | + * uint32_t cmd = (PERF_INC(my_event), real_value()); |
| 63 | + */ |
| 64 | +#define PERF_INC(name) (++perf_##name) |
| 65 | +#define PERF_ADD(name, n) (perf_##name += (unsigned long long)(n)) |
| 66 | + |
| 67 | +#else /* !BENCH_PROFILE */ |
| 68 | + |
| 69 | +#define PERF_COUNTER(name) typedef int perf_##name##_unused |
| 70 | +/* No-op forms remain expressions of integer type (not void) so callers |
| 71 | + * can use them inside comma operators without code changes. */ |
| 72 | +#define PERF_INC(name) (0) |
| 73 | +#define PERF_ADD(name, n) ((void)(n), 0) |
| 74 | + |
| 75 | +/* Stubs so callers don't need their own #ifdef around dump/reset. */ |
| 76 | +static __inline void perf_counters_dump(FILE *out) { (void)out; } |
| 77 | +static __inline void perf_counters_reset(void) { } |
| 78 | + |
| 79 | +#endif /* BENCH_PROFILE */ |
| 80 | + |
| 81 | +#ifdef __cplusplus |
| 82 | +} |
| 83 | +#endif |
| 84 | + |
| 85 | +#endif /* VJ_PERF_COUNTERS_H */ |
0 commit comments