Harald Welte | 216338c | 2017-10-15 19:46:19 +0200 | [diff] [blame] | 1 | /*! \file counter.c |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 2 | * utility routines for keeping some statistics. */ |
| 3 | /* |
| 4 | * (C) 2009 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
| 9 | * |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 10 | * 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 Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 20 | */ |
| 21 | |
Holger Hans Peter Freyther | a9f526a | 2011-04-18 16:45:45 +0200 | [diff] [blame] | 22 | #include <string.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 23 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 24 | #include <osmocom/core/linuxlist.h> |
| 25 | #include <osmocom/core/talloc.h> |
Harald Welte | 216338c | 2017-10-15 19:46:19 +0200 | [diff] [blame] | 26 | #include <osmocom/core/counter.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 27 | |
| 28 | static LLIST_HEAD(counters); |
| 29 | |
Harald Welte | 17bbaa3 | 2017-10-15 20:02:07 +0200 | [diff] [blame] | 30 | /*! Global talloc context for all osmo_counter allocations. */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 31 | void *tall_ctr_ctx; |
| 32 | |
Harald Welte | 17bbaa3 | 2017-10-15 20:02:07 +0200 | [diff] [blame] | 33 | /*! 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 Ayuso | 220abab | 2011-05-07 12:43:04 +0200 | [diff] [blame] | 36 | struct osmo_counter *osmo_counter_alloc(const char *name) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 37 | { |
Pablo Neira Ayuso | 220abab | 2011-05-07 12:43:04 +0200 | [diff] [blame] | 38 | struct osmo_counter *ctr = talloc_zero(tall_ctr_ctx, struct osmo_counter); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 39 | |
| 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 Welte | 17bbaa3 | 2017-10-15 20:02:07 +0200 | [diff] [blame] | 49 | /*! Release/Destroy a given counter |
| 50 | * \param[in] ctr Counter to be destroyed */ |
Pablo Neira Ayuso | 220abab | 2011-05-07 12:43:04 +0200 | [diff] [blame] | 51 | void osmo_counter_free(struct osmo_counter *ctr) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 52 | { |
| 53 | llist_del(&ctr->list); |
| 54 | talloc_free(ctr); |
| 55 | } |
| 56 | |
Harald Welte | 17bbaa3 | 2017-10-15 20:02:07 +0200 | [diff] [blame] | 57 | /*! 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 Ayuso | 220abab | 2011-05-07 12:43:04 +0200 | [diff] [blame] | 61 | int osmo_counters_for_each(int (*handle_counter)(struct osmo_counter *, void *), |
| 62 | void *data) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 63 | { |
Pablo Neira Ayuso | 220abab | 2011-05-07 12:43:04 +0200 | [diff] [blame] | 64 | struct osmo_counter *ctr; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 65 | 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 Couzens | 18ba26c | 2017-12-05 16:06:27 +0100 | [diff] [blame] | 76 | /*! Counts the registered counter |
| 77 | * \returns amount of counters */ |
Harald Welte | e61d459 | 2022-11-03 11:05:58 +0100 | [diff] [blame] | 78 | int osmo_counters_count(void) |
Alexander Couzens | 18ba26c | 2017-12-05 16:06:27 +0100 | [diff] [blame] | 79 | { |
| 80 | return llist_count(&counters); |
| 81 | } |
| 82 | |
Harald Welte | 17bbaa3 | 2017-10-15 20:02:07 +0200 | [diff] [blame] | 83 | /*! 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 Ayuso | 220abab | 2011-05-07 12:43:04 +0200 | [diff] [blame] | 86 | struct osmo_counter *osmo_counter_get_by_name(const char *name) |
Daniel Willmann | 334c8e1 | 2011-04-08 10:46:19 +0200 | [diff] [blame] | 87 | { |
Pablo Neira Ayuso | 220abab | 2011-05-07 12:43:04 +0200 | [diff] [blame] | 88 | struct osmo_counter *ctr; |
Daniel Willmann | 334c8e1 | 2011-04-08 10:46:19 +0200 | [diff] [blame] | 89 | |
| 90 | llist_for_each_entry(ctr, &counters, list) { |
| 91 | if (!strcmp(ctr->name, name)) |
| 92 | return ctr; |
| 93 | } |
| 94 | return NULL; |
| 95 | } |
Jacob Erlbeck | 80db4ec | 2015-10-26 14:39:08 +0100 | [diff] [blame] | 96 | |
Harald Welte | 17bbaa3 | 2017-10-15 20:02:07 +0200 | [diff] [blame] | 97 | /*! 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 Erlbeck | 80db4ec | 2015-10-26 14:39:08 +0100 | [diff] [blame] | 102 | int 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 | } |