blob: 0fa31661a1d4f4a8099ef8c8adb1926383a3ba4f [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 *
Harald Weltee08da972017-11-13 01:00:26 +09008 * SPDX-License-Identifier: GPL-2.0+
9 *
Harald Welteec8b4502010-02-20 20:34:29 +010010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 *
24 */
25
Holger Hans Peter Freythera9f526a2011-04-18 16:45:45 +020026#include <string.h>
Harald Welteec8b4502010-02-20 20:34:29 +010027
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010028#include <osmocom/core/linuxlist.h>
29#include <osmocom/core/talloc.h>
Harald Welte216338c2017-10-15 19:46:19 +020030#include <osmocom/core/counter.h>
Harald Welteec8b4502010-02-20 20:34:29 +010031
32static LLIST_HEAD(counters);
33
Harald Welte17bbaa32017-10-15 20:02:07 +020034/*! Global talloc context for all osmo_counter allocations. */
Harald Welteec8b4502010-02-20 20:34:29 +010035void *tall_ctr_ctx;
36
Harald Welte17bbaa32017-10-15 20:02:07 +020037/*! Allocate a new counter with given name. Allocates from tall_ctr_ctx
38 * \param[in] name Human-readable string name for the counter
39 * \returns Allocated counter on success; NULL on error */
Pablo Neira Ayuso220abab2011-05-07 12:43:04 +020040struct osmo_counter *osmo_counter_alloc(const char *name)
Harald Welteec8b4502010-02-20 20:34:29 +010041{
Pablo Neira Ayuso220abab2011-05-07 12:43:04 +020042 struct osmo_counter *ctr = talloc_zero(tall_ctr_ctx, struct osmo_counter);
Harald Welteec8b4502010-02-20 20:34:29 +010043
44 if (!ctr)
45 return NULL;
46
47 ctr->name = name;
48 llist_add_tail(&ctr->list, &counters);
49
50 return ctr;
51}
52
Harald Welte17bbaa32017-10-15 20:02:07 +020053/*! Release/Destroy a given counter
54 * \param[in] ctr Counter to be destroyed */
Pablo Neira Ayuso220abab2011-05-07 12:43:04 +020055void osmo_counter_free(struct osmo_counter *ctr)
Harald Welteec8b4502010-02-20 20:34:29 +010056{
57 llist_del(&ctr->list);
58 talloc_free(ctr);
59}
60
Harald Welte17bbaa32017-10-15 20:02:07 +020061/*! Iterate over all counters; call \a handle_cunter call-back for each.
62 * \param[in] handle_counter Call-back to be called for each counter; aborts if rc < 0
63 * \param[in] data Opaque data passed through to \a handle_counter function
64 * \returns 0 if all \a handle_counter calls successfull; negative on error */
Pablo Neira Ayuso220abab2011-05-07 12:43:04 +020065int osmo_counters_for_each(int (*handle_counter)(struct osmo_counter *, void *),
66 void *data)
Harald Welteec8b4502010-02-20 20:34:29 +010067{
Pablo Neira Ayuso220abab2011-05-07 12:43:04 +020068 struct osmo_counter *ctr;
Harald Welteec8b4502010-02-20 20:34:29 +010069 int rc = 0;
70
71 llist_for_each_entry(ctr, &counters, list) {
72 rc = handle_counter(ctr, data);
73 if (rc < 0)
74 return rc;
75 }
76
77 return rc;
78}
79
Alexander Couzens18ba26c2017-12-05 16:06:27 +010080/*! Counts the registered counter
81 * \returns amount of counters */
82int osmo_counters_count()
83{
84 return llist_count(&counters);
85}
86
Harald Welte17bbaa32017-10-15 20:02:07 +020087/*! Find a counter by its name.
88 * \param[in] name Name used to look-up/search counter
89 * \returns Counter on success; NULL if not found */
Pablo Neira Ayuso220abab2011-05-07 12:43:04 +020090struct osmo_counter *osmo_counter_get_by_name(const char *name)
Daniel Willmann334c8e12011-04-08 10:46:19 +020091{
Pablo Neira Ayuso220abab2011-05-07 12:43:04 +020092 struct osmo_counter *ctr;
Daniel Willmann334c8e12011-04-08 10:46:19 +020093
94 llist_for_each_entry(ctr, &counters, list) {
95 if (!strcmp(ctr->name, name))
96 return ctr;
97 }
98 return NULL;
99}
Jacob Erlbeck80db4ec2015-10-26 14:39:08 +0100100
Harald Welte17bbaa32017-10-15 20:02:07 +0200101/*! Compute difference between current and previous counter value.
102 * \param[in] ctr Counter of which the difference is to be computed
103 * \returns Delta value between current counter and previous counter. Please
104 * note that the actual counter values are unsigned long, while the
105 * difference is computed as signed integer! */
Jacob Erlbeck80db4ec2015-10-26 14:39:08 +0100106int osmo_counter_difference(struct osmo_counter *ctr)
107{
108 int delta = ctr->value - ctr->previous;
109 ctr->previous = ctr->value;
110
111 return delta;
112}