From 64765bc4d41af1af14e1fb90c4ae2b81d9de39b5 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Sun, 28 Oct 2007 17:48:29 +0100 Subject: [PATCH] src/utils_cache.c: Detect when a counter wraps around and calculate the difference correctly. --- src/utils_cache.c | 20 ++++++++++++++++++-- src/utils_cache.h | 2 +- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/utils_cache.c b/src/utils_cache.c index 5ecb83cf..79989a07 100644 --- a/src/utils_cache.c +++ b/src/utils_cache.c @@ -87,8 +87,24 @@ int uc_update (const data_set_t *ds, const value_list_t *vl) { if (ds->ds[i].type == DS_TYPE_COUNTER) { - ce->values_gauge[i] = ((double) (vl->values[i].counter - - ce->values_counter[i])) + counter_t diff; + + /* check if the counter has wrapped around */ + if (vl->values[i].counter < ce->values_counter[i]) + { + if (ce->values_counter[i] <= 4294967295U) + diff = (4294967295U - ce->values_counter[i]) + + vl->values[i].counter; + else + diff = (18446744073709551615ULL - ce->values_counter[i]) + + vl->values[i].counter; + } + else /* counter has NOT wrapped around */ + { + diff = vl->values[i].counter - ce->values_counter[i]; + } + + ce->values_gauge[i] = ((double) diff) / ((double) (vl->time - ce->last_update)); ce->values_counter[i] = vl->values[i].counter; } diff --git a/src/utils_cache.h b/src/utils_cache.h index 91454057..8d1c7549 100644 --- a/src/utils_cache.h +++ b/src/utils_cache.h @@ -1,5 +1,5 @@ /** - * collectd - src/utils_cache.c + * collectd - src/utils_cache.h * Copyright (C) 2007 Florian octo Forster * * This program is free software; you can redistribute it and/or modify it -- 2.11.0