core: Extend rate_ctr by helper functions

For global value reporting, some additional helper functions are
needed. The statsd protocol expects differential counter values,
which are currently not provided by rate_ctr (except for s/m/h/d
intervals).

This commit adds several helper functions to rate_ctr:

- rate_ctr_difference  returns the counter delta since the last
                       call to this function for a given counter
- rate_ctr_for_each_counter
                       iterates through each counter of a group
- rate_ctr_for_each_group
                       iterates through all globally registered
                       counter groups

Note that the rate_ctr_difference function can only be used by a
single backend, since it modifies the 'previous' field in the
rate_ctr obj.

Sponsored-by: On-Waves ehf
diff --git a/src/rate_ctr.c b/src/rate_ctr.c
index 8a232e8..50b3fe7 100644
--- a/src/rate_ctr.c
+++ b/src/rate_ctr.c
@@ -83,6 +83,15 @@
 	ctr->current += inc;
 }
 
+/*! \brief Return the counter difference since the last call to this function */
+int64_t rate_ctr_difference(struct rate_ctr *ctr)
+{
+	int64_t result = ctr->current - ctr->previous;
+	ctr->previous = ctr->current;
+
+	return result;
+}
+
 static void interval_expired(struct rate_ctr *ctr, enum rate_ctr_intv intv)
 {
 	/* calculate rate over last interval */
@@ -177,4 +186,36 @@
 	return NULL;
 }
 
+int rate_ctr_for_each_counter(struct rate_ctr_group *ctrg,
+	rate_ctr_handler_t handle_counter, void *data)
+{
+	int rc = 0;
+	int i;
+
+	for (i = 0; i < ctrg->desc->num_ctr; i++) {
+		struct rate_ctr *ctr = &ctrg->ctr[i];
+		rc = handle_counter(ctrg,
+			ctr, &ctrg->desc->ctr_desc[i], data);
+		if (rc < 0)
+			return rc;
+	}
+
+	return rc;
+}
+
+int rate_ctr_for_each_group(rate_ctr_group_handler_t handle_group, void *data)
+{
+	struct rate_ctr_group *statg;
+	int rc = 0;
+
+	llist_for_each_entry(statg, &rate_ctr_groups, list) {
+		rc = handle_group(statg, data);
+		if (rc < 0)
+			return rc;
+	}
+
+	return rc;
+}
+
+
 /*! @} */