aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJacob Erlbeck <jerlbeck@sysmocom.de>2015-10-19 14:04:38 +0200
committerJacob Erlbeck <jerlbeck@sysmocom.de>2015-10-28 23:51:24 +0100
commitc6a7108828bf98ebcaf31d24bd8d789afdd4da94 (patch)
tree25135f35eea7431f4f6730d247e2100e6dc8e257
parent423c1e5a4fc7ad2cd5e95e852b778c7e2c892bc1 (diff)
stats: Add stat_item_for_each functions
This commit adds the following functions: stat_item_for_each_group Call a handler for each group stat_item_for_each_item Call a handler for each item of a group Sponsored-by: On-Waves ehf
-rw-r--r--include/osmocom/core/stat_item.h14
-rw-r--r--src/stat_item.c30
2 files changed, 44 insertions, 0 deletions
diff --git a/include/osmocom/core/stat_item.h b/include/osmocom/core/stat_item.h
index 003c9e0..0786a3b 100644
--- a/include/osmocom/core/stat_item.h
+++ b/include/osmocom/core/stat_item.h
@@ -107,6 +107,20 @@ int stat_item_discard(const struct stat_item *item, int32_t *idx);
/*! \brief Skip all values of all items and update idx accordingly */
int stat_item_discard_all(int32_t *idx);
+typedef int (*stat_item_handler_t)(
+ struct stat_item_group *, struct stat_item *, void *);
+
+typedef int (*stat_item_group_handler_t)(struct stat_item_group *, void *);
+
+/*! \brief Iteate over all items
+ * \param[in] handle_item Call-back function, aborts if rc < 0
+ * \param[in] data Private data handed through to \a handle_item
+ */
+int stat_item_for_each_item(struct stat_item_group *statg,
+ stat_item_handler_t handle_item, void *data);
+
+int stat_item_for_each_group(stat_item_group_handler_t handle_group, void *data);
+
static inline int32_t stat_item_get_last(const struct stat_item *item)
{
return item->values[item->last_offs].value;
diff --git a/src/stat_item.c b/src/stat_item.c
index 1e283d4..525a94b 100644
--- a/src/stat_item.c
+++ b/src/stat_item.c
@@ -235,4 +235,34 @@ const struct stat_item *stat_item_get_by_name(
return NULL;
}
+int stat_item_for_each_item(struct stat_item_group *statg,
+ stat_item_handler_t handle_item, void *data)
+{
+ int rc = 0;
+ int i;
+
+ for (i = 0; i < statg->desc->num_items; i++) {
+ struct stat_item *item = statg->items[i];
+ rc = handle_item(statg, item, data);
+ if (rc < 0)
+ return rc;
+ }
+
+ return rc;
+}
+
+int stat_item_for_each_group(stat_item_group_handler_t handle_group, void *data)
+{
+ struct stat_item_group *statg;
+ int rc = 0;
+
+ llist_for_each_entry(statg, &stat_item_groups, list) {
+ rc = handle_group(statg, data);
+ if (rc < 0)
+ return rc;
+ }
+
+ return rc;
+}
+
/*! @} */