blob: 296377799bc703b2d0820b07c395342d127191d0 [file] [log] [blame]
Harald Welte216338c2017-10-15 19:46:19 +02001/*! \file counter.c
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02002 * utility routines for keeping some statistics. */
3/*
4 * (C) 2009 by Harald Welte <laforge@gnumonks.org>
Harald Welteec8b4502010-02-20 20:34:29 +01005 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
Holger Hans Peter Freythera9f526a2011-04-18 16:45:45 +020024#include <string.h>
Harald Welteec8b4502010-02-20 20:34:29 +010025
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010026#include <osmocom/core/linuxlist.h>
27#include <osmocom/core/talloc.h>
Harald Welte216338c2017-10-15 19:46:19 +020028#include <osmocom/core/counter.h>
Harald Welteec8b4502010-02-20 20:34:29 +010029
30static LLIST_HEAD(counters);
31
Harald Welte17bbaa32017-10-15 20:02:07 +020032/*! Global talloc context for all osmo_counter allocations. */
Harald Welteec8b4502010-02-20 20:34:29 +010033void *tall_ctr_ctx;
34
Harald Welte17bbaa32017-10-15 20:02:07 +020035/*! Allocate a new counter with given name. Allocates from tall_ctr_ctx
36 * \param[in] name Human-readable string name for the counter
37 * \returns Allocated counter on success; NULL on error */
Pablo Neira Ayuso220abab2011-05-07 12:43:04 +020038struct osmo_counter *osmo_counter_alloc(const char *name)
Harald Welteec8b4502010-02-20 20:34:29 +010039{
Pablo Neira Ayuso220abab2011-05-07 12:43:04 +020040 struct osmo_counter *ctr = talloc_zero(tall_ctr_ctx, struct osmo_counter);
Harald Welteec8b4502010-02-20 20:34:29 +010041
42 if (!ctr)
43 return NULL;
44
45 ctr->name = name;
46 llist_add_tail(&ctr->list, &counters);
47
48 return ctr;
49}
50
Harald Welte17bbaa32017-10-15 20:02:07 +020051/*! Release/Destroy a given counter
52 * \param[in] ctr Counter to be destroyed */
Pablo Neira Ayuso220abab2011-05-07 12:43:04 +020053void osmo_counter_free(struct osmo_counter *ctr)
Harald Welteec8b4502010-02-20 20:34:29 +010054{
55 llist_del(&ctr->list);
56 talloc_free(ctr);
57}
58
Harald Welte17bbaa32017-10-15 20:02:07 +020059/*! Iterate over all counters; call \a handle_cunter call-back for each.
60 * \param[in] handle_counter Call-back to be called for each counter; aborts if rc < 0
61 * \param[in] data Opaque data passed through to \a handle_counter function
62 * \returns 0 if all \a handle_counter calls successfull; negative on error */
Pablo Neira Ayuso220abab2011-05-07 12:43:04 +020063int osmo_counters_for_each(int (*handle_counter)(struct osmo_counter *, void *),
64 void *data)
Harald Welteec8b4502010-02-20 20:34:29 +010065{
Pablo Neira Ayuso220abab2011-05-07 12:43:04 +020066 struct osmo_counter *ctr;
Harald Welteec8b4502010-02-20 20:34:29 +010067 int rc = 0;
68
69 llist_for_each_entry(ctr, &counters, list) {
70 rc = handle_counter(ctr, data);
71 if (rc < 0)
72 return rc;
73 }
74
75 return rc;
76}
77
Harald Welte17bbaa32017-10-15 20:02:07 +020078/*! Find a counter by its name.
79 * \param[in] name Name used to look-up/search counter
80 * \returns Counter on success; NULL if not found */
Pablo Neira Ayuso220abab2011-05-07 12:43:04 +020081struct osmo_counter *osmo_counter_get_by_name(const char *name)
Daniel Willmann334c8e12011-04-08 10:46:19 +020082{
Pablo Neira Ayuso220abab2011-05-07 12:43:04 +020083 struct osmo_counter *ctr;
Daniel Willmann334c8e12011-04-08 10:46:19 +020084
85 llist_for_each_entry(ctr, &counters, list) {
86 if (!strcmp(ctr->name, name))
87 return ctr;
88 }
89 return NULL;
90}
Jacob Erlbeck80db4ec2015-10-26 14:39:08 +010091
Harald Welte17bbaa32017-10-15 20:02:07 +020092/*! Compute difference between current and previous counter value.
93 * \param[in] ctr Counter of which the difference is to be computed
94 * \returns Delta value between current counter and previous counter. Please
95 * note that the actual counter values are unsigned long, while the
96 * difference is computed as signed integer! */
Jacob Erlbeck80db4ec2015-10-26 14:39:08 +010097int osmo_counter_difference(struct osmo_counter *ctr)
98{
99 int delta = ctr->value - ctr->previous;
100 ctr->previous = ctr->value;
101
102 return delta;
103}