aboutsummaryrefslogtreecommitdiffstats
path: root/src/basecmd.c
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2017-01-09 23:08:23 -0500
committerKevin O'Connor <kevin@koconnor.net>2017-01-09 23:08:23 -0500
commit3a7a77d49e620420ee33996eec0a70a7c186f6bb (patch)
treed540c997551dbbf7f334de15ffb6f5daacb95987 /src/basecmd.c
parentc87c090264fb9fd91bc87fbc63a05c5e53e3c484 (diff)
downloadkutter-3a7a77d49e620420ee33996eec0a70a7c186f6bb.tar.gz
kutter-3a7a77d49e620420ee33996eec0a70a7c186f6bb.tar.xz
kutter-3a7a77d49e620420ee33996eec0a70a7c186f6bb.zip
basecmd: Improve accuracy of stats "sumsq" variable
Use a base of 256 instead of 65536 when calculating the sum of the square of the clock differences in the stats. This makes the calculation more accurate. Export the new base via DECL_CONSTANT for the host to access. Use DIV_ROUND_UP() when adjusting for the base to ensure no lost ticks. Do the division after multiplication in the common case where the time between stats_task() invocations is less than 64K ticks. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Diffstat (limited to 'src/basecmd.c')
-rw-r--r--src/basecmd.c15
1 files changed, 13 insertions, 2 deletions
diff --git a/src/basecmd.c b/src/basecmd.c
index 2c9562b6..4902bd9d 100644
--- a/src/basecmd.c
+++ b/src/basecmd.c
@@ -191,15 +191,26 @@ command_get_status(uint32_t *args)
}
DECL_COMMAND_FLAGS(command_get_status, HF_IN_SHUTDOWN, "get_status");
+#define SUMSQ_BASE 256
+DECL_CONSTANT(STATS_SUMSQ_BASE, SUMSQ_BASE);
+
static void
stats_task(void)
{
static uint32_t last, count, sumsq;
uint32_t cur = sched_read_time();
- uint32_t diff = (cur - last) >> 8;
+ uint32_t diff = cur - last;
last = cur;
count++;
- uint32_t nextsumsq = sumsq + diff*diff;
+ // Calculate sum of diff^2 - be careful of integer overflow
+ uint32_t nextsumsq;
+ if (diff <= 0xffff) {
+ nextsumsq = sumsq + DIV_ROUND_UP(diff * diff, SUMSQ_BASE);
+ } else if (diff <= 0xfffff) {
+ nextsumsq = sumsq + DIV_ROUND_UP(diff, SUMSQ_BASE) * diff;
+ } else {
+ nextsumsq = 0xffffffff;
+ }
if (nextsumsq < sumsq)
nextsumsq = 0xffffffff;
sumsq = nextsumsq;