src/common.[ch]: Add `counter_diff', a function to calculate the difference of two...
authorFlorian Forster <octo@leeloo.lan.home.verplant.org>
Sat, 17 Jan 2009 09:37:50 +0000 (10:37 +0100)
committerFlorian Forster <octo@leeloo.lan.home.verplant.org>
Sat, 17 Jan 2009 09:37:50 +0000 (10:37 +0100)
It's not just a simple (old - new), because wrap-around is handled, too.

src/common.c
src/common.h

index ae89c44..aeea28d 100644 (file)
@@ -925,4 +925,22 @@ int read_file_contents (const char *filename, char *buf, int bufsize)
        return n;
 }
 
+counter_t counter_diff (counter_t old_value, counter_t new_value)
+{
+       counter_t diff;
+
+       if (old_value > new_value)
+       {
+               if (old_value <= 4294967295U)
+                       diff = (4294967295U - old_value) + new_value;
+               else
+                       diff = (18446744073709551615ULL - old_value)
+                               + new_value;
+       }
+       else
+       {
+               diff = new_value - old_value;
+       }
 
+       return (diff);
+} /* counter_t counter_to_gauge */
index f463b77..aefc2cc 100644 (file)
@@ -209,4 +209,6 @@ int walk_directory (const char *dir, dirwalk_callback_f callback,
                void *user_data);
 int read_file_contents (const char *filename, char *buf, int bufsize);
 
+counter_t counter_diff (counter_t old_value, counter_t new_value);
+
 #endif /* COMMON_H */