Profiler (profiler)
A hash table based profiling tool for measuring execution time and call count of code sections.
Struct
struct profiler_t {
uint64_t begin; /* Start timestamp (ns) */
uint64_t end; /* End timestamp (ns) */
uint64_t elapsed; /* Accumulated time (ns) */
uint64_t count; /* Call count */
};
API
| Function | Description |
|---|---|
profiler_begin(p) | Start timing, record current time into begin |
profiler_end(p) | Stop timing, accumulate elapsed time into elapsed, count++ |
profiler_search(name) | Find or create a profiler with the given name |
profiler_foreach(cb) | Iterate over all profilers, callback returns name, average time, call count |
profiler_clear() | Clear all profiler data |
Usage Example
#include <kernel/core/profiler.h>
struct profiler_t * p = profiler_search("my-function");
profiler_begin(p);
/* ... code to be measured ... */
profiler_end(p);
/* Print all profiling results */
profiler_foreach([](const char * name, uint64_t count, uint64_t time) {
shell_printf("%s: avg=%lluns, count=%llu\n", name, time, count);
});
Notes
- Profiler names are managed via a hash table; profilers with the same name share data
profiler_searchlooks up the hash table, auto-creates and initializes if not foundprofiler_begin/endare inline functions that usektime_get()to obtain nanosecond timestampsprofiler_foreachoutputs sorted by name,timein the callback is the average time (total / count)profiler_clearclears the hash table, resetting all data