aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLorenz Meier <lm@inf.ethz.ch>2015-01-07 18:19:23 +0100
committerLorenz Meier <lm@inf.ethz.ch>2015-01-11 11:03:56 +0100
commit172dbf37070e2dccadc8779d6e0926d3f8d60706 (patch)
tree3f95531b4bdc372c55c2e23d165a9bec8bb682e4
parent4712c75dea935b5709c944b31ae670dd6879f2e9 (diff)
downloadpx4-firmware-172dbf37070e2dccadc8779d6e0926d3f8d60706.tar.gz
px4-firmware-172dbf37070e2dccadc8779d6e0926d3f8d60706.tar.bz2
px4-firmware-172dbf37070e2dccadc8779d6e0926d3f8d60706.zip
Performance counters: Add option to set otherwise estimated time interval
-rw-r--r--src/modules/systemlib/perf_counter.c40
-rw-r--r--src/modules/systemlib/perf_counter.h12
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.