From: Florian Forster Date: Sun, 28 Oct 2007 16:48:29 +0000 (+0100) Subject: src/utils_cache.c: Detect when a counter wraps around X-Git-Tag: collectd-4.3.0beta0~128 X-Git-Url: https://git.octo.it/?a=commitdiff_plain;h=64765bc4d41af1af14e1fb90c4ae2b81d9de39b5;p=collectd.git src/utils_cache.c: Detect when a counter wraps around and calculate the difference correctly. --- 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