Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file stats.c */ |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 2 | /* |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 3 | * (C) 2015 by sysmocom - s.f.m.c. GmbH |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 4 | * Author: Jacob Erlbeck <jerlbeck@sysmocom.de> |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 5 | * All Rights Reserved |
| 6 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 7 | * SPDX-License-Identifier: GPL-2.0+ |
| 8 | * |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 22 | * |
| 23 | */ |
| 24 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 25 | /*! \addtogroup stats |
| 26 | * @{ |
| 27 | * |
| 28 | * This module implements periodic reporting of statistics / counters. |
| 29 | * It supports the notion of multiple \ref osmo_stats_reporter objects |
| 30 | * which independently of each other can report statistics at different |
| 31 | * configurable intervals to different destinations. |
| 32 | * |
| 33 | * In order to use this facility, you have to call \ref |
| 34 | * osmo_stats_init() once at application start-up and then create one or |
| 35 | * more \ref osmo_stats_reporter, either using the direct API functions |
| 36 | * or by using the optional VTY bindings: |
| 37 | * |
| 38 | * - reporting to any of the libosmocore log targets |
| 39 | * \ref osmo_stats_reporter_create_log() creates a new stats_reporter |
| 40 | * which reports to the libosmcoore \ref logging subsystem. |
| 41 | * |
| 42 | * - reporting to statsd (a front-end proxy for the Graphite/Carbon |
| 43 | * metrics server |
| 44 | * \ref osmo_stats_reporter_create_statsd() creates a new stats_reporter |
| 45 | * which reports via UDP to statsd. |
| 46 | * |
| 47 | * You can either use the above API functions directly to create \ref |
| 48 | * osmo_stats_reporter instances, or you can use the VTY support |
| 49 | * contained in libosmovty. See the "stats" configuration node |
| 50 | * installed by osmo_stats_vty_Add_cmds(). |
| 51 | * |
| 52 | * An \ref osmo_stats_reporter reports statistics on all of the following |
| 53 | * libosmocore internal counter/statistics objects: |
| 54 | * - \ref osmo_counter |
| 55 | * - \ref rate_ctr |
| 56 | * - \ref osmo_stat_item |
| 57 | * |
| 58 | * You do not need to do anything in particular to expose a given |
| 59 | * counter or stat_item, they are all exported automatically via any |
| 60 | * \ref osmo_stats_reporter. If you have multiple \ref |
| 61 | * osmo_stats_reporter, they will each report all counters/stat_items. |
| 62 | * |
| 63 | * \file stats.c */ |
| 64 | |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 65 | #include "config.h" |
| 66 | #if !defined(EMBEDDED) |
| 67 | |
Harald Welte | 95871da | 2017-05-15 12:11:36 +0200 | [diff] [blame] | 68 | #include <osmocom/core/byteswap.h> |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 69 | #include <osmocom/core/stats.h> |
| 70 | |
| 71 | #include <unistd.h> |
| 72 | #include <string.h> |
| 73 | #include <stdint.h> |
| 74 | #include <errno.h> |
| 75 | #include <stdio.h> |
Holger Hans Peter Freyther | c337693 | 2015-08-21 19:56:54 +0000 | [diff] [blame] | 76 | #include <sys/types.h> |
Alexander Chemeris | f5bdef4 | 2020-05-09 01:57:51 +0300 | [diff] [blame] | 77 | #include <inttypes.h> |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 78 | |
| 79 | #ifdef HAVE_SYS_SOCKET_H |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 80 | #include <sys/socket.h> |
Holger Hans Peter Freyther | c337693 | 2015-08-21 19:56:54 +0000 | [diff] [blame] | 81 | #include <netinet/in.h> |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 82 | #include <arpa/inet.h> |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 83 | #endif |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 84 | |
| 85 | #include <osmocom/core/utils.h> |
| 86 | #include <osmocom/core/logging.h> |
| 87 | #include <osmocom/core/rate_ctr.h> |
| 88 | #include <osmocom/core/stat_item.h> |
Alexander Chemeris | f5bdef4 | 2020-05-09 01:57:51 +0300 | [diff] [blame] | 89 | #include <osmocom/core/select.h> |
Harald Welte | 216338c | 2017-10-15 19:46:19 +0200 | [diff] [blame] | 90 | #include <osmocom/core/counter.h> |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 91 | #include <osmocom/core/msgb.h> |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 92 | |
Harald Welte | 3217d51 | 2021-02-18 14:56:46 +0100 | [diff] [blame] | 93 | #ifdef HAVE_SYSTEMTAP |
| 94 | /* include the generated probes header and put markers in code */ |
| 95 | #include "probes.h" |
| 96 | #define TRACE(probe) probe |
| 97 | #define TRACE_ENABLED(probe) probe ## _ENABLED() |
| 98 | #else |
| 99 | /* Wrap the probe to allow it to be removed when no systemtap available */ |
| 100 | #define TRACE(probe) |
| 101 | #define TRACE_ENABLED(probe) (0) |
| 102 | #endif /* HAVE_SYSTEMTAP */ |
| 103 | |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 104 | #define STATS_DEFAULT_INTERVAL 5 /* secs */ |
Jacob Erlbeck | b6e6bea | 2015-11-09 15:33:44 +0100 | [diff] [blame] | 105 | #define STATS_DEFAULT_BUFLEN 256 |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 106 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 107 | static LLIST_HEAD(osmo_stats_reporter_list); |
| 108 | static void *osmo_stats_ctx = NULL; |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 109 | static int is_initialised = 0; |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 110 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 111 | static struct osmo_stats_config s_stats_config = { |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 112 | .interval = STATS_DEFAULT_INTERVAL, |
| 113 | }; |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 114 | struct osmo_stats_config *osmo_stats_config = &s_stats_config; |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 115 | |
Alexander Chemeris | f5bdef4 | 2020-05-09 01:57:51 +0300 | [diff] [blame] | 116 | static struct osmo_fd osmo_stats_timer = { .fd = -1 }; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 117 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 118 | static int osmo_stats_reporter_log_send_counter(struct osmo_stats_reporter *srep, |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 119 | const struct rate_ctr_group *ctrg, |
| 120 | const struct rate_ctr_desc *desc, |
| 121 | int64_t value, int64_t delta); |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 122 | static int osmo_stats_reporter_log_send_item(struct osmo_stats_reporter *srep, |
| 123 | const struct osmo_stat_item_group *statg, |
Harald Welte | 1554f80 | 2016-11-11 15:06:06 +0100 | [diff] [blame] | 124 | const struct osmo_stat_item_desc *desc, int64_t value); |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 125 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 126 | static int update_srep_config(struct osmo_stats_reporter *srep) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 127 | { |
| 128 | int rc = 0; |
| 129 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 130 | if (srep->running) { |
Jacob Erlbeck | 490b38f | 2015-10-27 15:10:28 +0100 | [diff] [blame] | 131 | if (srep->close) |
| 132 | rc = srep->close(srep); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 133 | srep->running = 0; |
| 134 | } |
| 135 | |
| 136 | if (!srep->enabled) |
| 137 | return rc; |
| 138 | |
Jacob Erlbeck | 490b38f | 2015-10-27 15:10:28 +0100 | [diff] [blame] | 139 | if (srep->open) |
| 140 | rc = srep->open(srep); |
| 141 | else |
| 142 | rc = 0; |
| 143 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 144 | if (rc < 0) |
| 145 | srep->enabled = 0; |
| 146 | else |
| 147 | srep->running = 1; |
| 148 | |
Jacob Erlbeck | aed7c12 | 2015-11-09 11:25:12 +0100 | [diff] [blame] | 149 | srep->force_single_flush = 1; |
| 150 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 151 | return rc; |
| 152 | } |
| 153 | |
Alexander Chemeris | f5bdef4 | 2020-05-09 01:57:51 +0300 | [diff] [blame] | 154 | static int osmo_stats_timer_cb(struct osmo_fd *ofd, unsigned int what) |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 155 | { |
Alexander Chemeris | f5bdef4 | 2020-05-09 01:57:51 +0300 | [diff] [blame] | 156 | uint64_t expire_count; |
| 157 | int rc; |
| 158 | |
| 159 | /* check that the timer has actually expired */ |
| 160 | if (!(what & OSMO_FD_READ)) |
| 161 | return 0; |
| 162 | |
| 163 | /* read from timerfd: number of expirations of periodic timer */ |
| 164 | rc = read(ofd->fd, (void *) &expire_count, sizeof(expire_count)); |
| 165 | if (rc < 0 && errno == EAGAIN) |
| 166 | return 0; |
| 167 | OSMO_ASSERT(rc == sizeof(expire_count)); |
| 168 | |
| 169 | if (expire_count > 1) |
| 170 | LOGP(DLSTATS, LOGL_NOTICE, "Stats timer expire_count=%" PRIu64 ": We missed %" PRIu64 " timers\n", |
| 171 | expire_count, expire_count-1); |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 172 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 173 | if (!llist_empty(&osmo_stats_reporter_list)) |
| 174 | osmo_stats_report(); |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 175 | |
Alexander Chemeris | f5bdef4 | 2020-05-09 01:57:51 +0300 | [diff] [blame] | 176 | return 0; |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | static int start_timer() |
| 180 | { |
Alexander Chemeris | f5bdef4 | 2020-05-09 01:57:51 +0300 | [diff] [blame] | 181 | int rc; |
| 182 | int interval = osmo_stats_config->interval; |
| 183 | |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 184 | if (!is_initialised) |
| 185 | return -ESRCH; |
| 186 | |
Alexander Chemeris | f5bdef4 | 2020-05-09 01:57:51 +0300 | [diff] [blame] | 187 | struct timespec ts_first = {.tv_sec=0, .tv_nsec=1000}; |
| 188 | struct timespec ts_interval = {.tv_sec=interval, .tv_nsec=0}; |
| 189 | |
| 190 | rc = osmo_timerfd_setup(&osmo_stats_timer, osmo_stats_timer_cb, NULL); |
| 191 | if (rc < 0) |
| 192 | LOGP(DLSTATS, LOGL_ERROR, "Failed to setup the timer with error code %d (fd=%d)\n", |
| 193 | rc, osmo_stats_timer.fd); |
Alexander Chemeris | f5bdef4 | 2020-05-09 01:57:51 +0300 | [diff] [blame] | 194 | |
Daniel Willmann | 1a1de33 | 2020-07-14 18:11:14 +0200 | [diff] [blame] | 195 | if (interval == 0) { |
| 196 | rc = osmo_timerfd_disable(&osmo_stats_timer); |
| 197 | if (rc < 0) |
| 198 | LOGP(DLSTATS, LOGL_ERROR, "Failed to disable the timer with error code %d (fd=%d)\n", |
| 199 | rc, osmo_stats_timer.fd); |
| 200 | } else { |
| 201 | |
| 202 | rc = osmo_timerfd_schedule(&osmo_stats_timer, &ts_first, &ts_interval); |
| 203 | if (rc < 0) |
| 204 | LOGP(DLSTATS, LOGL_ERROR, "Failed to schedule the timer with error code %d (fd=%d, interval %d sec)\n", |
| 205 | rc, osmo_stats_timer.fd, interval); |
| 206 | |
| 207 | LOGP(DLSTATS, LOGL_INFO, "Stats timer started with interval %d sec\n", interval); |
| 208 | } |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 209 | |
| 210 | return 0; |
| 211 | } |
| 212 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 213 | struct osmo_stats_reporter *osmo_stats_reporter_alloc(enum osmo_stats_reporter_type type, |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 214 | const char *name) |
| 215 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 216 | struct osmo_stats_reporter *srep; |
| 217 | srep = talloc_zero(osmo_stats_ctx, struct osmo_stats_reporter); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 218 | OSMO_ASSERT(srep); |
| 219 | srep->type = type; |
| 220 | if (name) |
| 221 | srep->name = talloc_strdup(srep, name); |
| 222 | srep->fd = -1; |
| 223 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 224 | llist_add(&srep->list, &osmo_stats_reporter_list); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 225 | |
| 226 | return srep; |
| 227 | } |
| 228 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 229 | /*! Destroy a given stats_reporter. Takes care of first disabling it. |
| 230 | * \param[in] srep stats_reporter that shall be disabled + destroyed */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 231 | void osmo_stats_reporter_free(struct osmo_stats_reporter *srep) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 232 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 233 | osmo_stats_reporter_disable(srep); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 234 | llist_del(&srep->list); |
| 235 | talloc_free(srep); |
| 236 | } |
| 237 | |
Oliver Smith | 6140194 | 2021-03-26 10:18:37 +0100 | [diff] [blame] | 238 | static int osmo_stats_discard_item(struct osmo_stat_item_group *statg, struct osmo_stat_item *item, void *sctx_) |
| 239 | { |
| 240 | return osmo_stat_item_discard(item, &item->stats_next_id); |
| 241 | } |
| 242 | |
| 243 | static int osmo_stats_discard_group(struct osmo_stat_item_group *statg, void *sctx_) |
| 244 | { |
| 245 | return osmo_stat_item_for_each_item(statg, &osmo_stats_discard_item, NULL); |
| 246 | } |
| 247 | |
| 248 | static int osmo_stats_discard_all() |
| 249 | { |
| 250 | return osmo_stat_item_for_each_group(&osmo_stats_discard_group, NULL); |
| 251 | } |
| 252 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 253 | /*! Initilize the stats reporting module; call this once in your program |
| 254 | * \param[in] ctx Talloc context from which stats related memory is allocated */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 255 | void osmo_stats_init(void *ctx) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 256 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 257 | osmo_stats_ctx = ctx; |
Oliver Smith | 6140194 | 2021-03-26 10:18:37 +0100 | [diff] [blame] | 258 | osmo_stats_discard_all(); |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 259 | |
| 260 | is_initialised = 1; |
| 261 | start_timer(); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 262 | } |
| 263 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 264 | /*! Find a stats_reporter of given \a type and \a name. |
| 265 | * \param[in] type Type of stats_reporter to find |
| 266 | * \param[in] name Name of stats_reporter to find |
| 267 | * \returns stats_reporter matching \a type and \a name; NULL otherwise */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 268 | struct osmo_stats_reporter *osmo_stats_reporter_find(enum osmo_stats_reporter_type type, |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 269 | const char *name) |
| 270 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 271 | struct osmo_stats_reporter *srep; |
| 272 | llist_for_each_entry(srep, &osmo_stats_reporter_list, list) { |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 273 | if (srep->type != type) |
| 274 | continue; |
| 275 | if (srep->name != name) { |
| 276 | if (name == NULL || srep->name == NULL || |
| 277 | strcmp(name, srep->name) != 0) |
| 278 | continue; |
| 279 | } |
| 280 | return srep; |
| 281 | } |
| 282 | return NULL; |
| 283 | } |
| 284 | |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 285 | #ifdef HAVE_SYS_SOCKET_H |
| 286 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 287 | /*! Set the remote (IP) address of a given stats_reporter. |
| 288 | * \param[in] srep stats_reporter whose remote address is to be set |
| 289 | * \param[in] addr String representation of remote IPv4 address |
| 290 | * \returns 0 on success; negative on error */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 291 | int osmo_stats_reporter_set_remote_addr(struct osmo_stats_reporter *srep, const char *addr) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 292 | { |
| 293 | int rc; |
| 294 | struct sockaddr_in *sock_addr = (struct sockaddr_in *)&srep->dest_addr; |
| 295 | struct in_addr inaddr; |
| 296 | |
Jacob Erlbeck | ed197fd | 2015-10-27 14:43:24 +0100 | [diff] [blame] | 297 | if (!srep->have_net_config) |
| 298 | return -ENOTSUP; |
| 299 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 300 | OSMO_ASSERT(addr != NULL); |
| 301 | |
| 302 | rc = inet_pton(AF_INET, addr, &inaddr); |
| 303 | if (rc <= 0) |
| 304 | return -EINVAL; |
| 305 | |
| 306 | sock_addr->sin_addr = inaddr; |
| 307 | sock_addr->sin_family = AF_INET; |
| 308 | srep->dest_addr_len = sizeof(*sock_addr); |
| 309 | |
| 310 | talloc_free(srep->dest_addr_str); |
| 311 | srep->dest_addr_str = talloc_strdup(srep, addr); |
| 312 | |
| 313 | return update_srep_config(srep); |
| 314 | } |
| 315 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 316 | /*! Set the remote (UDP) port of a given stats_reporter |
| 317 | * \param[in] srep stats_reporter whose remote address is to be set |
| 318 | * \param[in] port UDP port of remote statsd to which we report |
| 319 | * \returns 0 on success; negative on error */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 320 | int osmo_stats_reporter_set_remote_port(struct osmo_stats_reporter *srep, int port) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 321 | { |
| 322 | struct sockaddr_in *sock_addr = (struct sockaddr_in *)&srep->dest_addr; |
| 323 | |
Jacob Erlbeck | ed197fd | 2015-10-27 14:43:24 +0100 | [diff] [blame] | 324 | if (!srep->have_net_config) |
| 325 | return -ENOTSUP; |
| 326 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 327 | srep->dest_port = port; |
Harald Welte | 95871da | 2017-05-15 12:11:36 +0200 | [diff] [blame] | 328 | sock_addr->sin_port = osmo_htons(port); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 329 | |
| 330 | return update_srep_config(srep); |
| 331 | } |
| 332 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 333 | /*! Set the local (IP) address of a given stats_reporter. |
| 334 | * \param[in] srep stats_reporter whose remote address is to be set |
| 335 | * \param[in] addr String representation of local IP address |
| 336 | * \returns 0 on success; negative on error */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 337 | int osmo_stats_reporter_set_local_addr(struct osmo_stats_reporter *srep, const char *addr) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 338 | { |
| 339 | int rc; |
| 340 | struct sockaddr_in *sock_addr = (struct sockaddr_in *)&srep->bind_addr; |
| 341 | struct in_addr inaddr; |
| 342 | |
Jacob Erlbeck | ed197fd | 2015-10-27 14:43:24 +0100 | [diff] [blame] | 343 | if (!srep->have_net_config) |
| 344 | return -ENOTSUP; |
| 345 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 346 | if (addr) { |
| 347 | rc = inet_pton(AF_INET, addr, &inaddr); |
| 348 | if (rc <= 0) |
| 349 | return -EINVAL; |
| 350 | } else { |
Holger Hans Peter Freyther | 7921975 | 2015-11-02 15:50:32 +0100 | [diff] [blame] | 351 | inaddr.s_addr = INADDR_ANY; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | sock_addr->sin_addr = inaddr; |
| 355 | sock_addr->sin_family = AF_INET; |
| 356 | srep->bind_addr_len = addr ? sizeof(*sock_addr) : 0; |
| 357 | |
| 358 | talloc_free(srep->bind_addr_str); |
| 359 | srep->bind_addr_str = addr ? talloc_strdup(srep, addr) : NULL; |
| 360 | |
| 361 | return update_srep_config(srep); |
| 362 | } |
| 363 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 364 | /*! Set the maximum transmission unit of a given stats_reporter. |
| 365 | * \param[in] srep stats_reporter whose remote address is to be set |
| 366 | * \param[in] mtu Maximum Transmission Unit of \a srep |
| 367 | * \returns 0 on success; negative on error */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 368 | int osmo_stats_reporter_set_mtu(struct osmo_stats_reporter *srep, int mtu) |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 369 | { |
Jacob Erlbeck | ed197fd | 2015-10-27 14:43:24 +0100 | [diff] [blame] | 370 | if (!srep->have_net_config) |
| 371 | return -ENOTSUP; |
| 372 | |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 373 | if (mtu < 0) |
| 374 | return -EINVAL; |
| 375 | |
| 376 | srep->mtu = mtu; |
| 377 | |
| 378 | return update_srep_config(srep); |
| 379 | } |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 380 | #endif /* HAVE_SYS_SOCKETS_H */ |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 381 | |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 382 | int osmo_stats_reporter_set_max_class(struct osmo_stats_reporter *srep, |
| 383 | enum osmo_stats_class class_id) |
| 384 | { |
| 385 | if (class_id == OSMO_STATS_CLASS_UNKNOWN) |
| 386 | return -EINVAL; |
| 387 | |
| 388 | srep->max_class = class_id; |
| 389 | |
| 390 | return 0; |
| 391 | } |
| 392 | |
Alexander Chemeris | f203ed3 | 2020-05-08 19:09:22 +0300 | [diff] [blame] | 393 | /*! Set the reporting interval (common for all reporters) |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 394 | * \param[in] interval Reporting interval in seconds |
| 395 | * \returns 0 on success; negative on error */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 396 | int osmo_stats_set_interval(int interval) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 397 | { |
Daniel Willmann | 1a1de33 | 2020-07-14 18:11:14 +0200 | [diff] [blame] | 398 | if (interval < 0) |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 399 | return -EINVAL; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 400 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 401 | osmo_stats_config->interval = interval; |
Jacob Erlbeck | b1dbfb4 | 2015-10-26 11:58:38 +0100 | [diff] [blame] | 402 | if (is_initialised) |
| 403 | start_timer(); |
| 404 | |
| 405 | return 0; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 406 | } |
| 407 | |
Alexander Chemeris | dfebf40 | 2020-05-08 19:10:40 +0300 | [diff] [blame] | 408 | /*! Set the regular flush period for a given stats_reporter |
| 409 | * |
| 410 | * Send all stats even if they have not changed (i.e. force the flush) |
| 411 | * every N-th reporting interval. Set to 0 to disable regular flush, |
| 412 | * set to 1 to flush every time, set to 2 to flush every 2nd time, etc. |
| 413 | * \param[in] srep stats_reporter to set flush period for |
| 414 | * \param[in] period Reporting interval in seconds |
| 415 | * \returns 0 on success; negative on error */ |
| 416 | int osmo_stats_reporter_set_flush_period(struct osmo_stats_reporter *srep, unsigned int period) |
| 417 | { |
| 418 | srep->flush_period = period; |
| 419 | srep->flush_period_counter = 0; |
| 420 | /* force the flush now if it's not disabled by period=0 */ |
| 421 | if (period > 0) |
| 422 | srep->force_single_flush = 1; |
| 423 | |
| 424 | return 0; |
| 425 | } |
| 426 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 427 | /*! Set the name prefix of a given stats_reporter. |
| 428 | * \param[in] srep stats_reporter whose name prefix is to be set |
| 429 | * \param[in] prefix NAme perfix to pre-pend for any reported value |
| 430 | * \returns 0 on success; negative on error */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 431 | int osmo_stats_reporter_set_name_prefix(struct osmo_stats_reporter *srep, const char *prefix) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 432 | { |
| 433 | talloc_free(srep->name_prefix); |
Jacob Erlbeck | 916423e | 2015-11-09 10:52:19 +0100 | [diff] [blame] | 434 | srep->name_prefix = prefix && strlen(prefix) > 0 ? |
| 435 | talloc_strdup(srep, prefix) : NULL; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 436 | |
| 437 | return update_srep_config(srep); |
| 438 | } |
| 439 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 440 | |
| 441 | /*! Enable the given stats_reporter. |
| 442 | * \param[in] srep stats_reporter who is to be enabled |
| 443 | * \returns 0 on success; negative on error */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 444 | int osmo_stats_reporter_enable(struct osmo_stats_reporter *srep) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 445 | { |
| 446 | srep->enabled = 1; |
| 447 | |
| 448 | return update_srep_config(srep); |
| 449 | } |
| 450 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 451 | /*! Disable the given stats_reporter. |
| 452 | * \param[in] srep stats_reporter who is to be disabled |
| 453 | * \returns 0 on success; negative on error */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 454 | int osmo_stats_reporter_disable(struct osmo_stats_reporter *srep) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 455 | { |
| 456 | srep->enabled = 0; |
| 457 | |
| 458 | return update_srep_config(srep); |
| 459 | } |
| 460 | |
Jacob Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 461 | /*** i/o helper functions ***/ |
| 462 | |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 463 | #ifdef HAVE_SYS_SOCKET_H |
| 464 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 465 | /*! Open the UDP socket for given stats_reporter. |
| 466 | * \param[in] srep stats_reporter whose UDP socket is to be opened |
| 467 | * ]returns 0 on success; negative otherwise */ |
Jacob Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 468 | int osmo_stats_reporter_udp_open(struct osmo_stats_reporter *srep) |
| 469 | { |
| 470 | int sock; |
| 471 | int rc; |
| 472 | int buffer_size = STATS_DEFAULT_BUFLEN; |
| 473 | |
| 474 | if (srep->fd != -1 && srep->close) |
| 475 | srep->close(srep); |
| 476 | |
| 477 | sock = socket(AF_INET, SOCK_DGRAM, 0); |
| 478 | if (sock == -1) |
| 479 | return -errno; |
| 480 | |
Arran Cudbard-Bell | cc3694b | 2016-05-18 16:02:19 -0400 | [diff] [blame] | 481 | #if defined(__APPLE__) && !defined(MSG_NOSIGNAL) |
| 482 | { |
| 483 | static int val = 1; |
| 484 | |
| 485 | rc = setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, (void*)&val, sizeof(val)); |
| 486 | goto failed; |
| 487 | } |
| 488 | #endif |
Jacob Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 489 | if (srep->bind_addr_len > 0) { |
| 490 | rc = bind(sock, &srep->bind_addr, srep->bind_addr_len); |
| 491 | if (rc == -1) |
| 492 | goto failed; |
| 493 | } |
| 494 | |
| 495 | srep->fd = sock; |
| 496 | |
| 497 | if (srep->mtu > 0) { |
| 498 | buffer_size = srep->mtu - 20 /* IP */ - 8 /* UDP */; |
| 499 | srep->agg_enabled = 1; |
| 500 | } |
| 501 | |
| 502 | srep->buffer = msgb_alloc(buffer_size, "stats buffer"); |
| 503 | |
| 504 | return 0; |
| 505 | |
| 506 | failed: |
| 507 | rc = -errno; |
| 508 | close(sock); |
| 509 | |
| 510 | return rc; |
| 511 | } |
| 512 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 513 | /*! Closee the UDP socket for given stats_reporter. |
| 514 | * \param[in] srep stats_reporter whose UDP socket is to be closed |
| 515 | * ]returns 0 on success; negative otherwise */ |
Jacob Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 516 | int osmo_stats_reporter_udp_close(struct osmo_stats_reporter *srep) |
| 517 | { |
| 518 | int rc; |
| 519 | if (srep->fd == -1) |
| 520 | return -EBADF; |
| 521 | |
| 522 | osmo_stats_reporter_send_buffer(srep); |
| 523 | |
| 524 | rc = close(srep->fd); |
| 525 | srep->fd = -1; |
| 526 | msgb_free(srep->buffer); |
| 527 | srep->buffer = NULL; |
| 528 | return rc == -1 ? -errno : 0; |
| 529 | } |
| 530 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 531 | /*! Send given date to given stats_reporter. |
| 532 | * \param[in] srep stats_reporter whose UDP socket is to be opened |
| 533 | * \param[in] data string data to be sent |
| 534 | * \param[in] data_len Length of \a data in bytes |
| 535 | * \returns number of bytes on success; negative otherwise */ |
Jacob Erlbeck | b6e6bea | 2015-11-09 15:33:44 +0100 | [diff] [blame] | 536 | int osmo_stats_reporter_send(struct osmo_stats_reporter *srep, const char *data, |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 537 | int data_len) |
| 538 | { |
| 539 | int rc; |
| 540 | |
Arran Cudbard-Bell | cc3694b | 2016-05-18 16:02:19 -0400 | [diff] [blame] | 541 | rc = sendto(srep->fd, data, data_len, |
| 542 | #ifdef MSG_NOSIGNAL |
| 543 | MSG_NOSIGNAL | |
| 544 | #endif |
| 545 | MSG_DONTWAIT, |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 546 | &srep->dest_addr, srep->dest_addr_len); |
| 547 | |
| 548 | if (rc == -1) |
| 549 | rc = -errno; |
| 550 | |
| 551 | return rc; |
| 552 | } |
| 553 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 554 | /*! Send current accumulated buffer to given stats_reporter. |
| 555 | * \param[in] srep stats_reporter whose UDP socket is to be opened |
| 556 | * \returns number of bytes on success; negative otherwise */ |
Jacob Erlbeck | b6e6bea | 2015-11-09 15:33:44 +0100 | [diff] [blame] | 557 | int osmo_stats_reporter_send_buffer(struct osmo_stats_reporter *srep) |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 558 | { |
| 559 | int rc; |
| 560 | |
| 561 | if (!srep->buffer || msgb_length(srep->buffer) == 0) |
| 562 | return 0; |
| 563 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 564 | rc = osmo_stats_reporter_send(srep, |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 565 | (const char *)msgb_data(srep->buffer), msgb_length(srep->buffer)); |
| 566 | |
| 567 | msgb_trim(srep->buffer, 0); |
| 568 | |
| 569 | return rc; |
| 570 | } |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 571 | #endif /* HAVE_SYS_SOCKET_H */ |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 572 | |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 573 | /*** log reporter ***/ |
| 574 | |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 575 | /*! Create a stats_reporter that logs via libosmocore logging. |
| 576 | * A stats_reporter created via this function will simply print the statistics |
| 577 | * via the libosmocore logging framework, using DLSTATS subsystem and LOGL_INFO |
| 578 | * priority. The configuration of the libosmocore log targets define where this |
| 579 | * information will end up (ignored, text file, stderr, syslog, ...). |
| 580 | * \param[in] name Name of the to-be-created stats_reporter |
| 581 | * \returns stats_reporter on success; NULL on error */ |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 582 | struct osmo_stats_reporter *osmo_stats_reporter_create_log(const char *name) |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 583 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 584 | struct osmo_stats_reporter *srep; |
| 585 | srep = osmo_stats_reporter_alloc(OSMO_STATS_REPORTER_LOG, name); |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 586 | |
| 587 | srep->have_net_config = 0; |
| 588 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 589 | srep->send_counter = osmo_stats_reporter_log_send_counter; |
| 590 | srep->send_item = osmo_stats_reporter_log_send_item; |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 591 | |
| 592 | return srep; |
| 593 | } |
| 594 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 595 | static int osmo_stats_reporter_log_send(struct osmo_stats_reporter *srep, |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 596 | const char *type, |
Jacob Erlbeck | 16fe8da | 2015-11-02 11:30:01 +0100 | [diff] [blame] | 597 | const char *name1, unsigned int index1, const char *name2, int value, |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 598 | const char *unit) |
| 599 | { |
Jacob Erlbeck | 79125ec | 2015-11-02 15:17:50 +0100 | [diff] [blame] | 600 | LOGP(DLSTATS, LOGL_INFO, |
Jacob Erlbeck | 16fe8da | 2015-11-02 11:30:01 +0100 | [diff] [blame] | 601 | "stats t=%s p=%s g=%s i=%u n=%s v=%d u=%s\n", |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 602 | type, srep->name_prefix ? srep->name_prefix : "", |
| 603 | name1 ? name1 : "", index1, |
| 604 | name2, value, unit ? unit : ""); |
| 605 | |
| 606 | return 0; |
| 607 | } |
| 608 | |
| 609 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 610 | static int osmo_stats_reporter_log_send_counter(struct osmo_stats_reporter *srep, |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 611 | const struct rate_ctr_group *ctrg, |
| 612 | const struct rate_ctr_desc *desc, |
| 613 | int64_t value, int64_t delta) |
| 614 | { |
| 615 | if (ctrg) |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 616 | return osmo_stats_reporter_log_send(srep, "c", |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 617 | ctrg->desc->group_name_prefix, |
| 618 | ctrg->idx, |
| 619 | desc->name, value, NULL); |
| 620 | else |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 621 | return osmo_stats_reporter_log_send(srep, "c", |
Jacob Erlbeck | 16fe8da | 2015-11-02 11:30:01 +0100 | [diff] [blame] | 622 | NULL, 0, |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 623 | desc->name, value, NULL); |
| 624 | } |
| 625 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 626 | static int osmo_stats_reporter_log_send_item(struct osmo_stats_reporter *srep, |
| 627 | const struct osmo_stat_item_group *statg, |
Harald Welte | 1554f80 | 2016-11-11 15:06:06 +0100 | [diff] [blame] | 628 | const struct osmo_stat_item_desc *desc, int64_t value) |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 629 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 630 | return osmo_stats_reporter_log_send(srep, "i", |
Jacob Erlbeck | bc4f7ae | 2015-10-28 21:47:45 +0100 | [diff] [blame] | 631 | statg->desc->group_name_prefix, statg->idx, |
| 632 | desc->name, value, desc->unit); |
| 633 | } |
| 634 | |
Jacob Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 635 | /*** helper for reporting ***/ |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 636 | |
Jacob Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 637 | static int osmo_stats_reporter_check_config(struct osmo_stats_reporter *srep, |
| 638 | unsigned int index, int class_id) |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 639 | { |
Jacob Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 640 | if (class_id == OSMO_STATS_CLASS_UNKNOWN) |
| 641 | class_id = index != 0 ? |
| 642 | OSMO_STATS_CLASS_SUBSCRIBER : OSMO_STATS_CLASS_GLOBAL; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 643 | |
Jacob Erlbeck | 2e8f9ed | 2015-11-09 15:48:25 +0100 | [diff] [blame] | 644 | return class_id <= srep->max_class; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 645 | } |
| 646 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 647 | /*** generic rate counter support ***/ |
| 648 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 649 | static int osmo_stats_reporter_send_counter(struct osmo_stats_reporter *srep, |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 650 | const struct rate_ctr_group *ctrg, |
| 651 | const struct rate_ctr_desc *desc, |
| 652 | int64_t value, int64_t delta) |
| 653 | { |
Jacob Erlbeck | 490b38f | 2015-10-27 15:10:28 +0100 | [diff] [blame] | 654 | if (!srep->send_counter) |
| 655 | return 0; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 656 | |
Jacob Erlbeck | 490b38f | 2015-10-27 15:10:28 +0100 | [diff] [blame] | 657 | return srep->send_counter(srep, ctrg, desc, value, delta); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | static int rate_ctr_handler( |
| 661 | struct rate_ctr_group *ctrg, struct rate_ctr *ctr, |
| 662 | const struct rate_ctr_desc *desc, void *sctx_) |
| 663 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 664 | struct osmo_stats_reporter *srep; |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 665 | int64_t delta = rate_ctr_difference(ctr); |
| 666 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 667 | llist_for_each_entry(srep, &osmo_stats_reporter_list, list) { |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 668 | if (!srep->running) |
| 669 | continue; |
| 670 | |
Jacob Erlbeck | aed7c12 | 2015-11-09 11:25:12 +0100 | [diff] [blame] | 671 | if (delta == 0 && !srep->force_single_flush) |
| 672 | continue; |
| 673 | |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 674 | if (!osmo_stats_reporter_check_config(srep, |
| 675 | ctrg->idx, ctrg->desc->class_id)) |
Jacob Erlbeck | 8a97cb9 | 2015-11-09 11:39:42 +0100 | [diff] [blame] | 676 | continue; |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 677 | |
Holger Hans Peter Freyther | 837e940 | 2015-11-02 15:44:26 +0100 | [diff] [blame] | 678 | osmo_stats_reporter_send_counter(srep, ctrg, desc, |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 679 | ctr->current, delta); |
| 680 | |
Holger Hans Peter Freyther | 837e940 | 2015-11-02 15:44:26 +0100 | [diff] [blame] | 681 | /* TODO: handle result (log?, inc counter(!)?) or remove it */ |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | return 0; |
| 685 | } |
| 686 | |
| 687 | static int rate_ctr_group_handler(struct rate_ctr_group *ctrg, void *sctx_) |
| 688 | { |
| 689 | rate_ctr_for_each_counter(ctrg, rate_ctr_handler, sctx_); |
| 690 | |
| 691 | return 0; |
| 692 | } |
| 693 | |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 694 | /*** stat item support ***/ |
| 695 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 696 | static int osmo_stats_reporter_send_item(struct osmo_stats_reporter *srep, |
| 697 | const struct osmo_stat_item_group *statg, |
| 698 | const struct osmo_stat_item_desc *desc, |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 699 | int32_t value) |
| 700 | { |
Jacob Erlbeck | 490b38f | 2015-10-27 15:10:28 +0100 | [diff] [blame] | 701 | if (!srep->send_item) |
| 702 | return 0; |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 703 | |
Jacob Erlbeck | 490b38f | 2015-10-27 15:10:28 +0100 | [diff] [blame] | 704 | return srep->send_item(srep, statg, desc, value); |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 705 | } |
| 706 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 707 | static int osmo_stat_item_handler( |
| 708 | struct osmo_stat_item_group *statg, struct osmo_stat_item *item, void *sctx_) |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 709 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 710 | struct osmo_stats_reporter *srep; |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 711 | int32_t value; |
Oliver Smith | d439360 | 2021-04-06 11:53:42 +0200 | [diff] [blame] | 712 | bool have_value; |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 713 | |
Oliver Smith | 6140194 | 2021-03-26 10:18:37 +0100 | [diff] [blame] | 714 | have_value = osmo_stat_item_get_next(item, &item->stats_next_id, &value) > 0; |
Daniel Willmann | 2aa527b | 2021-03-01 21:53:46 +0100 | [diff] [blame] | 715 | if (!have_value) { |
Jacob Erlbeck | aed7c12 | 2015-11-09 11:25:12 +0100 | [diff] [blame] | 716 | /* Send the last value in case a flush is requested */ |
| 717 | value = osmo_stat_item_get_last(item); |
Oliver Smith | 11da4a4 | 2021-08-19 11:58:09 +0200 | [diff] [blame] | 718 | |
| 719 | /* Also send it in case a different max value was sent |
| 720 | * previously (OS#5215) */ |
| 721 | if (!item->stats_last_sent_was_max) |
| 722 | have_value = 1; |
Daniel Willmann | 2aa527b | 2021-03-01 21:53:46 +0100 | [diff] [blame] | 723 | } else { |
| 724 | int32_t next_val; |
| 725 | /* If we have multiple values only send the max */ |
Oliver Smith | 6140194 | 2021-03-26 10:18:37 +0100 | [diff] [blame] | 726 | while (osmo_stat_item_get_next(item, &item->stats_next_id, &next_val) > 0) |
Daniel Willmann | 2aa527b | 2021-03-01 21:53:46 +0100 | [diff] [blame] | 727 | value = OSMO_MAX(value, next_val); |
| 728 | } |
Jacob Erlbeck | aed7c12 | 2015-11-09 11:25:12 +0100 | [diff] [blame] | 729 | |
Oliver Smith | 11da4a4 | 2021-08-19 11:58:09 +0200 | [diff] [blame] | 730 | item->stats_last_sent_was_max = (osmo_stat_item_get_last(item) == value); |
| 731 | |
Daniel Willmann | 2aa527b | 2021-03-01 21:53:46 +0100 | [diff] [blame] | 732 | llist_for_each_entry(srep, &osmo_stats_reporter_list, list) { |
| 733 | if (!srep->running) |
| 734 | continue; |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 735 | |
Daniel Willmann | 2aa527b | 2021-03-01 21:53:46 +0100 | [diff] [blame] | 736 | if (!have_value && !srep->force_single_flush) |
| 737 | continue; |
Jacob Erlbeck | aed7c12 | 2015-11-09 11:25:12 +0100 | [diff] [blame] | 738 | |
Daniel Willmann | 2aa527b | 2021-03-01 21:53:46 +0100 | [diff] [blame] | 739 | if (!osmo_stats_reporter_check_config(srep, |
| 740 | statg->idx, statg->desc->class_id)) |
| 741 | continue; |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 742 | |
Daniel Willmann | 2aa527b | 2021-03-01 21:53:46 +0100 | [diff] [blame] | 743 | osmo_stats_reporter_send_item(srep, statg, |
| 744 | item->desc, value); |
Jacob Erlbeck | aed7c12 | 2015-11-09 11:25:12 +0100 | [diff] [blame] | 745 | |
Daniel Willmann | 2aa527b | 2021-03-01 21:53:46 +0100 | [diff] [blame] | 746 | } |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 747 | |
| 748 | return 0; |
| 749 | } |
| 750 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 751 | static int osmo_stat_item_group_handler(struct osmo_stat_item_group *statg, void *sctx_) |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 752 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 753 | osmo_stat_item_for_each_item(statg, osmo_stat_item_handler, sctx_); |
Jacob Erlbeck | c27671c | 2015-10-26 12:32:07 +0100 | [diff] [blame] | 754 | |
| 755 | return 0; |
| 756 | } |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 757 | |
Jacob Erlbeck | c8f47b6 | 2015-10-26 14:42:05 +0100 | [diff] [blame] | 758 | /*** osmo counter support ***/ |
| 759 | |
| 760 | static int handle_counter(struct osmo_counter *counter, void *sctx_) |
| 761 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 762 | struct osmo_stats_reporter *srep; |
Jacob Erlbeck | c8f47b6 | 2015-10-26 14:42:05 +0100 | [diff] [blame] | 763 | struct rate_ctr_desc desc = {0}; |
| 764 | /* Fake a rate counter description */ |
| 765 | desc.name = counter->name; |
| 766 | desc.description = counter->description; |
| 767 | |
| 768 | int delta = osmo_counter_difference(counter); |
| 769 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 770 | llist_for_each_entry(srep, &osmo_stats_reporter_list, list) { |
Jacob Erlbeck | c8f47b6 | 2015-10-26 14:42:05 +0100 | [diff] [blame] | 771 | if (!srep->running) |
| 772 | continue; |
| 773 | |
Jacob Erlbeck | aed7c12 | 2015-11-09 11:25:12 +0100 | [diff] [blame] | 774 | if (delta == 0 && !srep->force_single_flush) |
| 775 | continue; |
| 776 | |
Holger Hans Peter Freyther | 837e940 | 2015-11-02 15:44:26 +0100 | [diff] [blame] | 777 | osmo_stats_reporter_send_counter(srep, NULL, &desc, |
Jacob Erlbeck | c8f47b6 | 2015-10-26 14:42:05 +0100 | [diff] [blame] | 778 | counter->value, delta); |
| 779 | |
Holger Hans Peter Freyther | 837e940 | 2015-11-02 15:44:26 +0100 | [diff] [blame] | 780 | /* TODO: handle result (log?, inc counter(!)?) */ |
Jacob Erlbeck | c8f47b6 | 2015-10-26 14:42:05 +0100 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | return 0; |
| 784 | } |
| 785 | |
| 786 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 787 | /*** main reporting function ***/ |
| 788 | |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 789 | static void flush_all_reporters() |
| 790 | { |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 791 | struct osmo_stats_reporter *srep; |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 792 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 793 | llist_for_each_entry(srep, &osmo_stats_reporter_list, list) { |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 794 | if (!srep->running) |
| 795 | continue; |
| 796 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 797 | osmo_stats_reporter_send_buffer(srep); |
Alexander Chemeris | dfebf40 | 2020-05-08 19:10:40 +0300 | [diff] [blame] | 798 | |
| 799 | /* reset force_single_flush first */ |
Jacob Erlbeck | aed7c12 | 2015-11-09 11:25:12 +0100 | [diff] [blame] | 800 | srep->force_single_flush = 0; |
Alexander Chemeris | dfebf40 | 2020-05-08 19:10:40 +0300 | [diff] [blame] | 801 | /* and schedule a new flush if it's time for it */ |
| 802 | if (srep->flush_period > 0) { |
| 803 | srep->flush_period_counter++; |
| 804 | if (srep->flush_period_counter >= srep->flush_period) { |
| 805 | srep->force_single_flush = 1; |
| 806 | srep->flush_period_counter = 0; |
| 807 | } |
| 808 | } |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 809 | } |
| 810 | } |
| 811 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 812 | int osmo_stats_report() |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 813 | { |
Jacob Erlbeck | 01e8c91 | 2015-11-09 14:13:23 +0100 | [diff] [blame] | 814 | /* per group actions */ |
Harald Welte | 3217d51 | 2021-02-18 14:56:46 +0100 | [diff] [blame] | 815 | TRACE(LIBOSMOCORE_STATS_START()); |
Jacob Erlbeck | c8f47b6 | 2015-10-26 14:42:05 +0100 | [diff] [blame] | 816 | osmo_counters_for_each(handle_counter, NULL); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 817 | rate_ctr_for_each_group(rate_ctr_group_handler, NULL); |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 818 | osmo_stat_item_for_each_group(osmo_stat_item_group_handler, NULL); |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 819 | |
Jacob Erlbeck | 01e8c91 | 2015-11-09 14:13:23 +0100 | [diff] [blame] | 820 | /* global actions */ |
Oliver Smith | 6140194 | 2021-03-26 10:18:37 +0100 | [diff] [blame] | 821 | osmo_stats_discard_all(); |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 822 | flush_all_reporters(); |
Harald Welte | 3217d51 | 2021-02-18 14:56:46 +0100 | [diff] [blame] | 823 | TRACE(LIBOSMOCORE_STATS_DONE()); |
Jacob Erlbeck | d01acfc | 2015-10-26 16:22:45 +0100 | [diff] [blame] | 824 | |
Jacob Erlbeck | 95bf8280 | 2015-10-20 19:05:52 +0200 | [diff] [blame] | 825 | return 0; |
| 826 | } |
Harald Welte | 67bdd80 | 2017-01-15 17:56:11 +0100 | [diff] [blame] | 827 | |
| 828 | #endif /* !EMBEDDED */ |
Harald Welte | eb5b6ce | 2017-10-15 20:03:24 +0200 | [diff] [blame] | 829 | |
| 830 | /*! @} */ |