blob: cbee7b9e2c010c6488e76591405d49b1cee7f32a [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 *
Harald Welteec8b4502010-02-20 20:34:29 +010020 */
21
Holger Hans Peter Freythera9f526a2011-04-18 16:45:45 +020022#include <string.h>
Harald Welteec8b4502010-02-20 20:34:29 +010023
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010024#include <osmocom/core/linuxlist.h>
25#include <osmocom/core/talloc.h>
Harald Welte216338c2017-10-15 19:46:19 +020026#include <osmocom/core/counter.h>
Harald Welteec8b4502010-02-20 20:34:29 +010027
28static LLIST_HEAD(counters);
29
Harald Welte17bbaa32017-10-15 20:02:07 +020030/*! Global talloc context for all osmo_counter allocations. */
Harald Welteec8b4502010-02-20 20:34:29 +010031void *tall_ctr_ctx;
32
Harald Welte17bbaa32017-10-15 20:02:07 +020033/*! Allocate a new counter with given name. Allocates from tall_ctr_ctx
34 * \param[in] name Human-readable string name for the counter
35 * \returns Allocated counter on success; NULL on error */
Pablo Neira Ayuso220abab2011-05-07 12:43:04 +020036struct osmo_counter *osmo_counter_alloc(const char *name)
Harald Welteec8b4502010-02-20 20:34:29 +010037{
Pablo Neira Ayuso220abab2011-05-07 12:43:04 +020038 struct osmo_counter *ctr = talloc_zero(tall_ctr_ctx, struct osmo_counter);
Harald Welteec8b4502010-02-20 20:34:29 +010039
40 if (!ctr)
41 return NULL;
42
43 ctr->name = name;
44 llist_add_tail(&ctr->list, &counters);
45
46 return ctr;
47}
48
Harald Welte17bbaa32017-10-15 20:02:07 +020049/*! Release/Destroy a given counter
50 * \param[in] ctr Counter to be destroyed */
Pablo Neira Ayuso220abab2011-05-07 12:43:04 +020051void osmo_counter_free(struct osmo_counter *ctr)
Harald Welteec8b4502010-02-20 20:34:29 +010052{
53 llist_del(&ctr->list);
54 talloc_free(ctr);
55}
56
Harald Welte17bbaa32017-10-15 20:02:07 +020057/*! Iterate over all counters; call \a handle_cunter call-back for each.
58 * \param[in] handle_counter Call-back to be called for each counter; aborts if rc < 0
59 * \param[in] data Opaque data passed through to \a handle_counter function
60 * \returns 0 if all \a handle_counter calls successfull; negative on error */
Pablo Neira Ayuso220abab2011-05-07 12:43:04 +020061int osmo_counters_for_each(int (*handle_counter)(struct osmo_counter *, void *),
62 void *data)
Harald Welteec8b4502010-02-20 20:34:29 +010063{
Pablo Neira Ayuso220abab2011-05-07 12:43:04 +020064 struct osmo_counter *ctr;
Harald Welteec8b4502010-02-20 20:34:29 +010065 int rc = 0;
66
67 llist_for_each_entry(ctr, &counters, list) {
68 rc = handle_counter(ctr, data);
69 if (rc < 0)
70 return rc;
71 }
72
73 return rc;
74}
75
Alexander Couzens18ba26c2017-12-05 16:06:27 +010076/*! Counts the registered counter
77 * \returns amount of counters */
78int osmo_counters_count()
79{
80 return llist_count(&counters);
81}
82
Harald Welte17bbaa32017-10-15 20:02:07 +020083/*! Find a counter by its name.
84 * \param[in] name Name used to look-up/search counter
85 * \returns Counter on success; NULL if not found */
Pablo Neira Ayuso220abab2011-05-07 12:43:04 +020086struct osmo_counter *osmo_counter_get_by_name(const char *name)
Daniel Willmann334c8e12011-04-08 10:46:19 +020087{
Pablo Neira Ayuso220abab2011-05-07 12:43:04 +020088 struct osmo_counter *ctr;
Daniel Willmann334c8e12011-04-08 10:46:19 +020089
90 llist_for_each_entry(ctr, &counters, list) {
91 if (!strcmp(ctr->name, name))
92 return ctr;
93 }
94 return NULL;
95}
Jacob Erlbeck80db4ec2015-10-26 14:39:08 +010096
Harald Welte17bbaa32017-10-15 20:02:07 +020097/*! Compute difference between current and previous counter value.
98 * \param[in] ctr Counter of which the difference is to be computed
99 * \returns Delta value between current counter and previous counter. Please
100 * note that the actual counter values are unsigned long, while the
101 * difference is computed as signed integer! */
Jacob Erlbeck80db4ec2015-10-26 14:39:08 +0100102int osmo_counter_difference(struct osmo_counter *ctr)
103{
104 int delta = ctr->value - ctr->previous;
105 ctr->previous = ctr->value;
106
107 return delta;
108}