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