diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/modules/systemlib/perf_counter.c | 40 | ||||
-rw-r--r-- | src/modules/systemlib/perf_counter.h | 12 |
2 files changed, 52 insertions, 0 deletions
diff --git a/src/modules/systemlib/perf_counter.c b/src/modules/systemlib/perf_counter.c index 350c74d8a..4d62db0ee 100644 --- a/src/modules/systemlib/perf_counter.c +++ b/src/modules/systemlib/perf_counter.c @@ -273,6 +273,46 @@ perf_end(perf_counter_t handle) } void +perf_set(perf_counter_t handle, int64_t elapsed) +{ + if (handle == NULL) + return; + + switch (handle->type) { + case PC_ELAPSED: { + struct perf_ctr_elapsed *pce = (struct perf_ctr_elapsed *)handle; + + if (elapsed < 0) { + pce->event_overruns++; + } else { + + pce->event_count++; + pce->time_total += elapsed; + + if ((pce->time_least > (uint64_t)elapsed) || (pce->time_least == 0)) + pce->time_least = elapsed; + + if (pce->time_most < (uint64_t)elapsed) + pce->time_most = elapsed; + + // maintain mean and variance of the elapsed time in seconds + // Knuth/Welford recursive mean and variance of update intervals (via Wikipedia) + float dt = elapsed / 1e6f; + float delta_intvl = dt - pce->mean; + pce->mean += delta_intvl / pce->event_count; + pce->M2 += delta_intvl * (dt - pce->mean); + + pce->time_start = 0; + } + } + break; + + default: + break; + } +} + +void perf_cancel(perf_counter_t handle) { if (handle == NULL) diff --git a/src/modules/systemlib/perf_counter.h b/src/modules/systemlib/perf_counter.h index 92f064d04..0c1243de3 100644 --- a/src/modules/systemlib/perf_counter.h +++ b/src/modules/systemlib/perf_counter.h @@ -112,6 +112,18 @@ __EXPORT extern void perf_begin(perf_counter_t handle); __EXPORT extern void perf_end(perf_counter_t handle); /** + * Register a measurement + * + * This call applies to counters that operate over ranges of time; PC_ELAPSED etc. + * If a call is made without a corresponding perf_begin call. It sets the + * value provided as argument as a new measurement. + * + * @param handle The handle returned from perf_alloc. + * @param elapsed The time elapsed. Negative values lead to incrementing the overrun counter. + */ +__EXPORT extern void perf_set(perf_counter_t handle, int64_t elapsed); + +/** * Cancel a performance event. * * This call applies to counters that operate over ranges of time; PC_ELAPSED etc. |