add a value_to_rate function.
authorPierre-Yves Ritschard <pyr@spootnik.org>
Thu, 16 Jan 2014 18:23:52 +0000 (19:23 +0100)
committerPierre-Yves Ritschard <pyr@spootnik.org>
Thu, 16 Jan 2014 18:23:52 +0000 (19:23 +0100)
src/common.c
src/common.h

index 81142fd..18b5c43 100644 (file)
@@ -650,7 +650,7 @@ int get_kstat (kstat_t **ksp_ptr, char *module, int instance, char *name)
        char ident[128];
 
        *ksp_ptr = NULL;
-       
+
        if (kc == NULL)
                return (-1);
 
@@ -1236,7 +1236,7 @@ int walk_directory (const char *dir, dirwalk_callback_f callback,
        while ((ent = readdir (dh)) != NULL)
        {
                int status;
-               
+
                if (include_hidden)
                {
                        if ((strcmp (".", ent->d_name) == 0)
@@ -1399,6 +1399,69 @@ int rate_to_value (value_t *ret_value, gauge_t rate, /* {{{ */
        return (0);
 } /* }}} value_t rate_to_value */
 
+int value_to_rate (value_t *ret_rate, derive_t value, /* {{{ */
+               value_to_rate_state_t *state,
+               int ds_type, cdtime_t t)
+{
+       double interval;
+
+       /* Another invalid state: The time is not increasing. */
+       if (t <= state->last_time)
+       {
+               memset (state, 0, sizeof (*state));
+               return (EINVAL);
+       }
+
+       interval = CDTIME_T_TO_DOUBLE(t - state->last_time);
+
+       /* Previous value is invalid. */
+       if (state->last_time == 0) /* {{{ */
+       {
+               if (ds_type == DS_TYPE_DERIVE)
+               {
+                       state->last_value.derive = value;
+               }
+               else if (ds_type == DS_TYPE_COUNTER)
+               {
+                       state->last_value.counter = (counter_t) value;
+               }
+               else if (ds_type == DS_TYPE_ABSOLUTE)
+               {
+                       state->last_value.absolute = (absolute_t) value;
+               }
+               else
+               {
+                       assert (23 == 42);
+               }
+
+               state->last_time = t;
+               return (EAGAIN);
+       } /* }}} */
+
+       if (ds_type == DS_TYPE_DERIVE)
+       {
+               ret_rate->gauge = (value - state->last_value.derive) / interval;
+               state->last_value.derive = value;
+       }
+       else if (ds_type == DS_TYPE_COUNTER)
+       {
+               ret_rate->gauge = (((counter_t)value) - state->last_value.counter) / interval;
+               state->last_value.counter = (counter_t) value;
+       }
+       else if (ds_type == DS_TYPE_ABSOLUTE)
+       {
+               ret_rate->gauge = (((absolute_t)value) - state->last_value.absolute) / interval;
+               state->last_value.absolute = (absolute_t) value;
+       }
+       else
+       {
+               assert (23 == 42);
+       }
+
+        state->last_time = t;
+       return (0);
+} /* }}} value_t rate_to_value */
+
 int service_name_to_port_number (const char *service_name)
 {
        struct addrinfo *ai_list;
index 67f307c..29590ff 100644 (file)
@@ -55,6 +55,13 @@ struct rate_to_value_state_s
 };
 typedef struct rate_to_value_state_s rate_to_value_state_t;
 
+struct value_to_rate_state_s
+{
+  value_t last_value;
+  cdtime_t last_time;
+};
+typedef struct value_to_rate_state_s value_to_rate_state_t;
+
 char *sstrncpy (char *dest, const char *src, size_t n);
 
 __attribute__ ((format(printf,3,4)))
@@ -324,6 +331,9 @@ counter_t counter_diff (counter_t old_value, counter_t new_value);
 int rate_to_value (value_t *ret_value, gauge_t rate,
                rate_to_value_state_t *state, int ds_type, cdtime_t t);
 
+int value_to_rate (value_t *ret_rate, derive_t value,
+               value_to_rate_state_t *state, int ds_type, cdtime_t t);
+
 /* Converts a service name (a string) to a port number
  * (in the range [1-65535]). Returns less than zero on error. */
 int service_name_to_port_number (const char *service_name);