blob: 6fa87ba34a65b83e22955f31d637d699ddffa60d [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
32void *tall_ctr_ctx;
33
Pablo Neira Ayuso220abab2011-05-07 12:43:04 +020034struct osmo_counter *osmo_counter_alloc(const char *name)
Harald Welteec8b4502010-02-20 20:34:29 +010035{
Pablo Neira Ayuso220abab2011-05-07 12:43:04 +020036 struct osmo_counter *ctr = talloc_zero(tall_ctr_ctx, struct osmo_counter);
Harald Welteec8b4502010-02-20 20:34:29 +010037
38 if (!ctr)
39 return NULL;
40
41 ctr->name = name;
42 llist_add_tail(&ctr->list, &counters);
43
44 return ctr;
45}
46
Pablo Neira Ayuso220abab2011-05-07 12:43:04 +020047void osmo_counter_free(struct osmo_counter *ctr)
Harald Welteec8b4502010-02-20 20:34:29 +010048{
49 llist_del(&ctr->list);
50 talloc_free(ctr);
51}
52
Pablo Neira Ayuso220abab2011-05-07 12:43:04 +020053int osmo_counters_for_each(int (*handle_counter)(struct osmo_counter *, void *),
54 void *data)
Harald Welteec8b4502010-02-20 20:34:29 +010055{
Pablo Neira Ayuso220abab2011-05-07 12:43:04 +020056 struct osmo_counter *ctr;
Harald Welteec8b4502010-02-20 20:34:29 +010057 int rc = 0;
58
59 llist_for_each_entry(ctr, &counters, list) {
60 rc = handle_counter(ctr, data);
61 if (rc < 0)
62 return rc;
63 }
64
65 return rc;
66}
67
Pablo Neira Ayuso220abab2011-05-07 12:43:04 +020068struct osmo_counter *osmo_counter_get_by_name(const char *name)
Daniel Willmann334c8e12011-04-08 10:46:19 +020069{
Pablo Neira Ayuso220abab2011-05-07 12:43:04 +020070 struct osmo_counter *ctr;
Daniel Willmann334c8e12011-04-08 10:46:19 +020071
72 llist_for_each_entry(ctr, &counters, list) {
73 if (!strcmp(ctr->name, name))
74 return ctr;
75 }
76 return NULL;
77}
Jacob Erlbeck80db4ec2015-10-26 14:39:08 +010078
79int osmo_counter_difference(struct osmo_counter *ctr)
80{
81 int delta = ctr->value - ctr->previous;
82 ctr->previous = ctr->value;
83
84 return delta;
85}