import gsm0502_calc_paging_group() from openbsc
[osmocom-bb.git] / src / rate_ctr.c
index 9b2459e..6d771a4 100644 (file)
  */
 
 #include <stdint.h>
-#include <inttypes.h>
 #include <string.h>
 
-#include <osmocore/linuxlist.h>
-#include <osmocore/talloc.h>
-#include <osmocore/timer.h>
-#include <osmocore/rate_ctr.h>
+#include <osmocom/core/utils.h>
+#include <osmocom/core/linuxlist.h>
+#include <osmocom/core/talloc.h>
+#include <osmocom/core/timer.h>
+#include <osmocom/core/rate_ctr.h>
 
 static LLIST_HEAD(rate_ctr_groups);
 
@@ -51,9 +51,7 @@ struct rate_ctr_group *rate_ctr_group_alloc(void *ctx,
                return NULL;
 
        group->desc = desc;
-       /* Generate the Group prefix from the user-specified index */
-       group->name_prefix = talloc_size(group, strlen(desc->group_prefix_fmt) + 20);
-       sprintf(group->name_prefix, desc->group_prefix_fmt, idx);
+       group->idx = idx;
 
        llist_add(&group->list, &rate_ctr_groups);
 
@@ -77,9 +75,14 @@ static void interval_expired(struct rate_ctr *ctr, enum rate_ctr_intv intv)
        ctr->intv[intv].rate = ctr->current - ctr->intv[intv].last;
        /* save current counter for next interval */
        ctr->intv[intv].last = ctr->current;
+
+       /* update the rate of the next bigger interval.  This will
+        * be overwritten when that next larger interval expires */
+       if (intv + 1 < ARRAY_SIZE(ctr->intv))
+               ctr->intv[intv+1].rate += ctr->intv[intv].rate;
 }
 
-static struct timer_list rate_ctr_timer;
+static struct osmo_timer_list rate_ctr_timer;
 static uint64_t timer_ticks;
 
 /* The one-second interval has expired */
@@ -111,14 +114,48 @@ static void rate_ctr_timer_cb(void *data)
        llist_for_each_entry(ctrg, &rate_ctr_groups, list)
                rate_ctr_group_intv(ctrg);
 
-       bsc_schedule_timer(&rate_ctr_timer, 1, 0);
+       osmo_timer_schedule(&rate_ctr_timer, 1, 0);
 }
 
 int rate_ctr_init(void *tall_ctx)
 {
        tall_rate_ctr_ctx = tall_ctx;
        rate_ctr_timer.cb = rate_ctr_timer_cb;
-       bsc_schedule_timer(&rate_ctr_timer, 1, 0);
+       osmo_timer_schedule(&rate_ctr_timer, 1, 0);
 
        return 0;
 }
+
+struct rate_ctr_group *rate_ctr_get_group_by_name_idx(const char *name, const unsigned int idx)
+{
+       struct rate_ctr_group *ctrg;
+
+       llist_for_each_entry(ctrg, &rate_ctr_groups, list) {
+               if (!ctrg->desc)
+                       continue;
+
+               if (!strcmp(ctrg->desc->group_name_prefix, name) &&
+                               ctrg->idx == idx) {
+                       return ctrg;
+               }
+       }
+       return NULL;
+}
+
+const struct rate_ctr *rate_ctr_get_by_name(const struct rate_ctr_group *ctrg, const char *name)
+{
+       int i;
+       const struct rate_ctr_desc *ctr_desc;
+
+       if (!ctrg->desc)
+               return NULL;
+
+       for (i = 0; i < ctrg->desc->num_ctr; i++) {
+               ctr_desc = &ctrg->desc->ctr_desc[i];
+
+               if (!strcmp(ctr_desc->name, name)) {
+                       return &ctrg->ctr[i];
+               }
+       }
+       return NULL;
+}