From: Florian Forster Date: Sat, 17 Jan 2009 09:37:50 +0000 (+0100) Subject: src/common.[ch]: Add `counter_diff', a function to calculate the difference of two... X-Git-Tag: collectd-4.6.0~105 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=10b343057733ca75925de786f61cb7dfd58bacd5;p=collectd.git src/common.[ch]: Add `counter_diff', a function to calculate the difference of two counters. It's not just a simple (old - new), because wrap-around is handled, too. --- diff --git a/src/common.c b/src/common.c index ae89c44f..aeea28d3 100644 --- a/src/common.c +++ b/src/common.c @@ -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 */ diff --git a/src/common.h b/src/common.h index f463b77e..aefc2cc6 100644 --- a/src/common.h +++ b/src/common.h @@ -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 */