src/utils_cache.c: Detect when a counter wraps around
authorFlorian Forster <octo@leeloo.lan.home.verplant.org>
Sun, 28 Oct 2007 16:48:29 +0000 (17:48 +0100)
committerFlorian Forster <octo@leeloo.lan.home.verplant.org>
Sun, 28 Oct 2007 16:48:29 +0000 (17:48 +0100)
and calculate the difference correctly.

src/utils_cache.c
src/utils_cache.h

index 5ecb83c..79989a0 100644 (file)
@@ -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;
       }
index 9145405..8d1c754 100644 (file)
@@ -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