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 | * |
| 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 Freyther | a9f526a | 2011-04-18 16:45:45 +0200 | [diff] [blame] | 24 | #include <string.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 25 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 26 | #include <osmocom/core/linuxlist.h> |
| 27 | #include <osmocom/core/talloc.h> |
Harald Welte | 216338c | 2017-10-15 19:46:19 +0200 | [diff] [blame] | 28 | #include <osmocom/core/counter.h> |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 29 | |
| 30 | static LLIST_HEAD(counters); |
| 31 | |
Harald Welte | 17bbaa3 | 2017-10-15 20:02:07 +0200 | [diff] [blame] | 32 | /*! Global talloc context for all osmo_counter allocations. */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 33 | void *tall_ctr_ctx; |
| 34 | |
Harald Welte | 17bbaa3 | 2017-10-15 20:02:07 +0200 | [diff] [blame] | 35 | /*! 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 Ayuso | 220abab | 2011-05-07 12:43:04 +0200 | [diff] [blame] | 38 | struct osmo_counter *osmo_counter_alloc(const char *name) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 39 | { |
Pablo Neira Ayuso | 220abab | 2011-05-07 12:43:04 +0200 | [diff] [blame] | 40 | struct osmo_counter *ctr = talloc_zero(tall_ctr_ctx, struct osmo_counter); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 41 | |
| 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 Welte | 17bbaa3 | 2017-10-15 20:02:07 +0200 | [diff] [blame] | 51 | /*! Release/Destroy a given counter |
| 52 | * \param[in] ctr Counter to be destroyed */ |
Pablo Neira Ayuso | 220abab | 2011-05-07 12:43:04 +0200 | [diff] [blame] | 53 | void osmo_counter_free(struct osmo_counter *ctr) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 54 | { |
| 55 | llist_del(&ctr->list); |
| 56 | talloc_free(ctr); |
| 57 | } |
| 58 | |
Harald Welte | 17bbaa3 | 2017-10-15 20:02:07 +0200 | [diff] [blame] | 59 | /*! 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 Ayuso | 220abab | 2011-05-07 12:43:04 +0200 | [diff] [blame] | 63 | int osmo_counters_for_each(int (*handle_counter)(struct osmo_counter *, void *), |
| 64 | void *data) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 65 | { |
Pablo Neira Ayuso | 220abab | 2011-05-07 12:43:04 +0200 | [diff] [blame] | 66 | struct osmo_counter *ctr; |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 67 | 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 Welte | 17bbaa3 | 2017-10-15 20:02:07 +0200 | [diff] [blame] | 78 | /*! 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 Ayuso | 220abab | 2011-05-07 12:43:04 +0200 | [diff] [blame] | 81 | struct osmo_counter *osmo_counter_get_by_name(const char *name) |
Daniel Willmann | 334c8e1 | 2011-04-08 10:46:19 +0200 | [diff] [blame] | 82 | { |
Pablo Neira Ayuso | 220abab | 2011-05-07 12:43:04 +0200 | [diff] [blame] | 83 | struct osmo_counter *ctr; |
Daniel Willmann | 334c8e1 | 2011-04-08 10:46:19 +0200 | [diff] [blame] | 84 | |
| 85 | llist_for_each_entry(ctr, &counters, list) { |
| 86 | if (!strcmp(ctr->name, name)) |
| 87 | return ctr; |
| 88 | } |
| 89 | return NULL; |
| 90 | } |
Jacob Erlbeck | 80db4ec | 2015-10-26 14:39:08 +0100 | [diff] [blame] | 91 | |
Harald Welte | 17bbaa3 | 2017-10-15 20:02:07 +0200 | [diff] [blame] | 92 | /*! 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 Erlbeck | 80db4ec | 2015-10-26 14:39:08 +0100 | [diff] [blame] | 97 | int 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 | } |