blob: 096730563d4482732d22b359c9bdb6dc01522fe3 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file stats.c */
Jacob Erlbeck95bf82802015-10-20 19:05:52 +02002/*
Harald Weltee08da972017-11-13 01:00:26 +09003 * (C) 2015 by sysmocom - s.f.m.c. GmbH
Jacob Erlbeck95bf82802015-10-20 19:05:52 +02004 * Author: Jacob Erlbeck <jerlbeck@sysmocom.de>
Jacob Erlbeck95bf82802015-10-20 19:05:52 +02005 * All Rights Reserved
6 *
Harald Weltee08da972017-11-13 01:00:26 +09007 * SPDX-License-Identifier: GPL-2.0+
8 *
Jacob Erlbeck95bf82802015-10-20 19:05:52 +02009 * 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 Welteeb5b6ce2017-10-15 20:03:24 +020025/*! \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 Welte67bdd802017-01-15 17:56:11 +010065#include "config.h"
66#if !defined(EMBEDDED)
67
Harald Welte95871da2017-05-15 12:11:36 +020068#include <osmocom/core/byteswap.h>
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020069#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 Freytherc3376932015-08-21 19:56:54 +000076#include <sys/types.h>
Alexander Chemerisf5bdef42020-05-09 01:57:51 +030077#include <inttypes.h>
Harald Welte67bdd802017-01-15 17:56:11 +010078
79#ifdef HAVE_SYS_SOCKET_H
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020080#include <sys/socket.h>
Holger Hans Peter Freytherc3376932015-08-21 19:56:54 +000081#include <netinet/in.h>
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020082#include <arpa/inet.h>
Harald Welte67bdd802017-01-15 17:56:11 +010083#endif
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020084
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 Chemerisf5bdef42020-05-09 01:57:51 +030089#include <osmocom/core/select.h>
Harald Welte216338c2017-10-15 19:46:19 +020090#include <osmocom/core/counter.h>
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +010091#include <osmocom/core/msgb.h>
Jacob Erlbeck95bf82802015-10-20 19:05:52 +020092
Harald Welte3217d512021-02-18 14:56:46 +010093#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 Hofmeyre90c7172021-09-14 14:37:38 +0200104#include <stat_item_internal.h>
105
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100106#define STATS_DEFAULT_INTERVAL 5 /* secs */
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100107#define STATS_DEFAULT_BUFLEN 256
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200108
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100109static LLIST_HEAD(osmo_stats_reporter_list);
110static void *osmo_stats_ctx = NULL;
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100111static int is_initialised = 0;
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100112
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100113static struct osmo_stats_config s_stats_config = {
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100114 .interval = STATS_DEFAULT_INTERVAL,
115};
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100116struct osmo_stats_config *osmo_stats_config = &s_stats_config;
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100117
Alexander Chemerisf5bdef42020-05-09 01:57:51 +0300118static struct osmo_fd osmo_stats_timer = { .fd = -1 };
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200119
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100120static int osmo_stats_reporter_log_send_counter(struct osmo_stats_reporter *srep,
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100121 const struct rate_ctr_group *ctrg,
122 const struct rate_ctr_desc *desc,
123 int64_t value, int64_t delta);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100124static int osmo_stats_reporter_log_send_item(struct osmo_stats_reporter *srep,
125 const struct osmo_stat_item_group *statg,
Harald Welte1554f802016-11-11 15:06:06 +0100126 const struct osmo_stat_item_desc *desc, int64_t value);
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100127
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100128static int update_srep_config(struct osmo_stats_reporter *srep)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200129{
130 int rc = 0;
131
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200132 if (srep->running) {
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100133 if (srep->close)
134 rc = srep->close(srep);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200135 srep->running = 0;
136 }
137
138 if (!srep->enabled)
139 return rc;
140
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100141 if (srep->open)
142 rc = srep->open(srep);
143 else
144 rc = 0;
145
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200146 if (rc < 0)
147 srep->enabled = 0;
148 else
149 srep->running = 1;
150
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100151 srep->force_single_flush = 1;
152
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200153 return rc;
154}
155
Alexander Chemerisf5bdef42020-05-09 01:57:51 +0300156static int osmo_stats_timer_cb(struct osmo_fd *ofd, unsigned int what)
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100157{
Alexander Chemerisf5bdef42020-05-09 01:57:51 +0300158 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 Erlbeckb1dbfb42015-10-26 11:58:38 +0100174
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100175 if (!llist_empty(&osmo_stats_reporter_list))
176 osmo_stats_report();
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100177
Alexander Chemerisf5bdef42020-05-09 01:57:51 +0300178 return 0;
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100179}
180
181static int start_timer()
182{
Alexander Chemerisf5bdef42020-05-09 01:57:51 +0300183 int rc;
184 int interval = osmo_stats_config->interval;
185
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100186 if (!is_initialised)
187 return -ESRCH;
188
Alexander Chemerisf5bdef42020-05-09 01:57:51 +0300189 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 Chemerisf5bdef42020-05-09 01:57:51 +0300196
Daniel Willmann1a1de332020-07-14 18:11:14 +0200197 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 Erlbeckb1dbfb42015-10-26 11:58:38 +0100211
212 return 0;
213}
214
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100215struct osmo_stats_reporter *osmo_stats_reporter_alloc(enum osmo_stats_reporter_type type,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200216 const char *name)
217{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100218 struct osmo_stats_reporter *srep;
219 srep = talloc_zero(osmo_stats_ctx, struct osmo_stats_reporter);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200220 OSMO_ASSERT(srep);
221 srep->type = type;
222 if (name)
223 srep->name = talloc_strdup(srep, name);
224 srep->fd = -1;
225
Vadim Yanitskiy4e924722021-11-09 03:58:35 +0300226 llist_add_tail(&srep->list, &osmo_stats_reporter_list);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200227
228 return srep;
229}
230
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200231/*! Destroy a given stats_reporter. Takes care of first disabling it.
232 * \param[in] srep stats_reporter that shall be disabled + destroyed */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100233void osmo_stats_reporter_free(struct osmo_stats_reporter *srep)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200234{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100235 osmo_stats_reporter_disable(srep);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200236 llist_del(&srep->list);
237 talloc_free(srep);
238}
239
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200240/*! Initialize the stats reporting module; call this once in your program.
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200241 * \param[in] ctx Talloc context from which stats related memory is allocated */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100242void osmo_stats_init(void *ctx)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200243{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100244 osmo_stats_ctx = ctx;
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100245 is_initialised = 1;
246 start_timer();
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200247}
248
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200249/*! 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 Erlbeckfc9533d2015-10-29 00:55:58 +0100253struct osmo_stats_reporter *osmo_stats_reporter_find(enum osmo_stats_reporter_type type,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200254 const char *name)
255{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100256 struct osmo_stats_reporter *srep;
257 llist_for_each_entry(srep, &osmo_stats_reporter_list, list) {
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200258 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 Welte67bdd802017-01-15 17:56:11 +0100270#ifdef HAVE_SYS_SOCKET_H
271
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200272/*! 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 Erlbeckfc9533d2015-10-29 00:55:58 +0100276int osmo_stats_reporter_set_remote_addr(struct osmo_stats_reporter *srep, const char *addr)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200277{
278 int rc;
279 struct sockaddr_in *sock_addr = (struct sockaddr_in *)&srep->dest_addr;
280 struct in_addr inaddr;
281
Jacob Erlbecked197fd2015-10-27 14:43:24 +0100282 if (!srep->have_net_config)
283 return -ENOTSUP;
284
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200285 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 Welteeb5b6ce2017-10-15 20:03:24 +0200301/*! 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 Erlbeckfc9533d2015-10-29 00:55:58 +0100305int osmo_stats_reporter_set_remote_port(struct osmo_stats_reporter *srep, int port)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200306{
307 struct sockaddr_in *sock_addr = (struct sockaddr_in *)&srep->dest_addr;
308
Jacob Erlbecked197fd2015-10-27 14:43:24 +0100309 if (!srep->have_net_config)
310 return -ENOTSUP;
311
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200312 srep->dest_port = port;
Harald Welte95871da2017-05-15 12:11:36 +0200313 sock_addr->sin_port = osmo_htons(port);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200314
315 return update_srep_config(srep);
316}
317
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200318/*! 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 Erlbeckfc9533d2015-10-29 00:55:58 +0100322int osmo_stats_reporter_set_local_addr(struct osmo_stats_reporter *srep, const char *addr)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200323{
324 int rc;
325 struct sockaddr_in *sock_addr = (struct sockaddr_in *)&srep->bind_addr;
326 struct in_addr inaddr;
327
Jacob Erlbecked197fd2015-10-27 14:43:24 +0100328 if (!srep->have_net_config)
329 return -ENOTSUP;
330
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200331 if (addr) {
332 rc = inet_pton(AF_INET, addr, &inaddr);
333 if (rc <= 0)
334 return -EINVAL;
335 } else {
Holger Hans Peter Freyther79219752015-11-02 15:50:32 +0100336 inaddr.s_addr = INADDR_ANY;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200337 }
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 Welteeb5b6ce2017-10-15 20:03:24 +0200349/*! 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 Erlbeckfc9533d2015-10-29 00:55:58 +0100353int osmo_stats_reporter_set_mtu(struct osmo_stats_reporter *srep, int mtu)
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100354{
Jacob Erlbecked197fd2015-10-27 14:43:24 +0100355 if (!srep->have_net_config)
356 return -ENOTSUP;
357
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100358 if (mtu < 0)
359 return -EINVAL;
360
361 srep->mtu = mtu;
362
363 return update_srep_config(srep);
364}
Harald Welte67bdd802017-01-15 17:56:11 +0100365#endif /* HAVE_SYS_SOCKETS_H */
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100366
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100367int 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 Chemerisf203ed32020-05-08 19:09:22 +0300378/*! Set the reporting interval (common for all reporters)
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200379 * \param[in] interval Reporting interval in seconds
380 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100381int osmo_stats_set_interval(int interval)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200382{
Daniel Willmann1a1de332020-07-14 18:11:14 +0200383 if (interval < 0)
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100384 return -EINVAL;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200385
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100386 osmo_stats_config->interval = interval;
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100387 if (is_initialised)
388 start_timer();
389
390 return 0;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200391}
392
Alexander Chemerisdfebf402020-05-08 19:10:40 +0300393/*! 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 */
401int 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 Welteeb5b6ce2017-10-15 20:03:24 +0200412/*! 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 Erlbeckfc9533d2015-10-29 00:55:58 +0100416int osmo_stats_reporter_set_name_prefix(struct osmo_stats_reporter *srep, const char *prefix)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200417{
418 talloc_free(srep->name_prefix);
Jacob Erlbeck916423e2015-11-09 10:52:19 +0100419 srep->name_prefix = prefix && strlen(prefix) > 0 ?
420 talloc_strdup(srep, prefix) : NULL;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200421
422 return update_srep_config(srep);
423}
424
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200425
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 Erlbeckfc9533d2015-10-29 00:55:58 +0100429int osmo_stats_reporter_enable(struct osmo_stats_reporter *srep)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200430{
431 srep->enabled = 1;
432
433 return update_srep_config(srep);
434}
435
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200436/*! 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 Erlbeckfc9533d2015-10-29 00:55:58 +0100439int osmo_stats_reporter_disable(struct osmo_stats_reporter *srep)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200440{
441 srep->enabled = 0;
442
443 return update_srep_config(srep);
444}
445
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100446/*** i/o helper functions ***/
447
Harald Welte67bdd802017-01-15 17:56:11 +0100448#ifdef HAVE_SYS_SOCKET_H
449
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200450/*! 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 Erlbeck2e8f9ed2015-11-09 15:48:25 +0100453int 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-Bellcc3694b2016-05-18 16:02:19 -0400466#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 Erlbeck2e8f9ed2015-11-09 15:48:25 +0100474 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
491failed:
492 rc = -errno;
493 close(sock);
494
495 return rc;
496}
497
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200498/*! 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 Erlbeck2e8f9ed2015-11-09 15:48:25 +0100501int 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 Welteeb5b6ce2017-10-15 20:03:24 +0200516/*! 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 Erlbeckb6e6bea2015-11-09 15:33:44 +0100521int osmo_stats_reporter_send(struct osmo_stats_reporter *srep, const char *data,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200522 int data_len)
523{
524 int rc;
525
Arran Cudbard-Bellcc3694b2016-05-18 16:02:19 -0400526 rc = sendto(srep->fd, data, data_len,
527#ifdef MSG_NOSIGNAL
528 MSG_NOSIGNAL |
529#endif
530 MSG_DONTWAIT,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200531 &srep->dest_addr, srep->dest_addr_len);
532
533 if (rc == -1)
534 rc = -errno;
535
536 return rc;
537}
538
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200539/*! 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 Erlbeckb6e6bea2015-11-09 15:33:44 +0100542int osmo_stats_reporter_send_buffer(struct osmo_stats_reporter *srep)
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100543{
544 int rc;
545
546 if (!srep->buffer || msgb_length(srep->buffer) == 0)
547 return 0;
548
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100549 rc = osmo_stats_reporter_send(srep,
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100550 (const char *)msgb_data(srep->buffer), msgb_length(srep->buffer));
551
552 msgb_trim(srep->buffer, 0);
553
554 return rc;
555}
Harald Welte67bdd802017-01-15 17:56:11 +0100556#endif /* HAVE_SYS_SOCKET_H */
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100557
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100558/*** log reporter ***/
559
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200560/*! 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 Erlbeckfc9533d2015-10-29 00:55:58 +0100567struct osmo_stats_reporter *osmo_stats_reporter_create_log(const char *name)
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100568{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100569 struct osmo_stats_reporter *srep;
570 srep = osmo_stats_reporter_alloc(OSMO_STATS_REPORTER_LOG, name);
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100571
572 srep->have_net_config = 0;
573
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100574 srep->send_counter = osmo_stats_reporter_log_send_counter;
575 srep->send_item = osmo_stats_reporter_log_send_item;
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100576
577 return srep;
578}
579
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100580static int osmo_stats_reporter_log_send(struct osmo_stats_reporter *srep,
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100581 const char *type,
Jacob Erlbeck16fe8da2015-11-02 11:30:01 +0100582 const char *name1, unsigned int index1, const char *name2, int value,
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100583 const char *unit)
584{
Jacob Erlbeck79125ec2015-11-02 15:17:50 +0100585 LOGP(DLSTATS, LOGL_INFO,
Jacob Erlbeck16fe8da2015-11-02 11:30:01 +0100586 "stats t=%s p=%s g=%s i=%u n=%s v=%d u=%s\n",
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100587 type, srep->name_prefix ? srep->name_prefix : "",
588 name1 ? name1 : "", index1,
589 name2, value, unit ? unit : "");
590
591 return 0;
592}
593
594
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100595static int osmo_stats_reporter_log_send_counter(struct osmo_stats_reporter *srep,
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100596 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 Erlbeckfc9533d2015-10-29 00:55:58 +0100601 return osmo_stats_reporter_log_send(srep, "c",
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100602 ctrg->desc->group_name_prefix,
603 ctrg->idx,
604 desc->name, value, NULL);
605 else
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100606 return osmo_stats_reporter_log_send(srep, "c",
Jacob Erlbeck16fe8da2015-11-02 11:30:01 +0100607 NULL, 0,
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100608 desc->name, value, NULL);
609}
610
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100611static int osmo_stats_reporter_log_send_item(struct osmo_stats_reporter *srep,
612 const struct osmo_stat_item_group *statg,
Harald Welte1554f802016-11-11 15:06:06 +0100613 const struct osmo_stat_item_desc *desc, int64_t value)
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100614{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100615 return osmo_stats_reporter_log_send(srep, "i",
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100616 statg->desc->group_name_prefix, statg->idx,
617 desc->name, value, desc->unit);
618}
619
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100620/*** helper for reporting ***/
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200621
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100622static int osmo_stats_reporter_check_config(struct osmo_stats_reporter *srep,
623 unsigned int index, int class_id)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200624{
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100625 if (class_id == OSMO_STATS_CLASS_UNKNOWN)
626 class_id = index != 0 ?
627 OSMO_STATS_CLASS_SUBSCRIBER : OSMO_STATS_CLASS_GLOBAL;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200628
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100629 return class_id <= srep->max_class;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200630}
631
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200632/*** generic rate counter support ***/
633
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100634static int osmo_stats_reporter_send_counter(struct osmo_stats_reporter *srep,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200635 const struct rate_ctr_group *ctrg,
636 const struct rate_ctr_desc *desc,
637 int64_t value, int64_t delta)
638{
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100639 if (!srep->send_counter)
640 return 0;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200641
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100642 return srep->send_counter(srep, ctrg, desc, value, delta);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200643}
644
645static 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 Erlbeckfc9533d2015-10-29 00:55:58 +0100649 struct osmo_stats_reporter *srep;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200650 int64_t delta = rate_ctr_difference(ctr);
651
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100652 llist_for_each_entry(srep, &osmo_stats_reporter_list, list) {
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200653 if (!srep->running)
654 continue;
655
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100656 if (delta == 0 && !srep->force_single_flush)
657 continue;
658
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100659 if (!osmo_stats_reporter_check_config(srep,
660 ctrg->idx, ctrg->desc->class_id))
Jacob Erlbeck8a97cb92015-11-09 11:39:42 +0100661 continue;
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100662
Holger Hans Peter Freyther837e9402015-11-02 15:44:26 +0100663 osmo_stats_reporter_send_counter(srep, ctrg, desc,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200664 ctr->current, delta);
665
Holger Hans Peter Freyther837e9402015-11-02 15:44:26 +0100666 /* TODO: handle result (log?, inc counter(!)?) or remove it */
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200667 }
668
669 return 0;
670}
671
672static 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 Erlbeckc27671c2015-10-26 12:32:07 +0100679/*** stat item support ***/
680
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100681static 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 Erlbeckc27671c2015-10-26 12:32:07 +0100684 int32_t value)
685{
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100686 if (!srep->send_item)
687 return 0;
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100688
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100689 return srep->send_item(srep, statg, desc, value);
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100690}
691
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100692static int osmo_stat_item_handler(
693 struct osmo_stat_item_group *statg, struct osmo_stat_item *item, void *sctx_)
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100694{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100695 struct osmo_stats_reporter *srep;
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200696 int32_t prev_reported_value = item->reported.max;
697 int32_t new_value = item->value.max;
Oliver Smith11da4a42021-08-19 11:58:09 +0200698
Daniel Willmann2aa527b2021-03-01 21:53:46 +0100699 llist_for_each_entry(srep, &osmo_stats_reporter_list, list) {
700 if (!srep->running)
701 continue;
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100702
Neels Hofmeyr6a594072021-09-14 21:49:00 +0200703 /* 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 Hofmeyre90c7172021-09-14 14:37:38 +0200706 */
Neels Hofmeyr6a594072021-09-14 21:49:00 +0200707 if (new_value == prev_reported_value && !srep->force_single_flush)
Daniel Willmann2aa527b2021-03-01 21:53:46 +0100708 continue;
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100709
Daniel Willmann2aa527b2021-03-01 21:53:46 +0100710 if (!osmo_stats_reporter_check_config(srep,
711 statg->idx, statg->desc->class_id))
712 continue;
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100713
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200714 osmo_stats_reporter_send_item(srep, statg, item->desc, new_value);
Daniel Willmann2aa527b2021-03-01 21:53:46 +0100715 }
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100716
Neels Hofmeyre90c7172021-09-14 14:37:38 +0200717 osmo_stat_item_flush(item);
718
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100719 return 0;
720}
721
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100722static int osmo_stat_item_group_handler(struct osmo_stat_item_group *statg, void *sctx_)
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100723{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100724 osmo_stat_item_for_each_item(statg, osmo_stat_item_handler, sctx_);
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100725
726 return 0;
727}
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200728
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100729/*** osmo counter support ***/
730
731static int handle_counter(struct osmo_counter *counter, void *sctx_)
732{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100733 struct osmo_stats_reporter *srep;
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100734 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 Erlbeckfc9533d2015-10-29 00:55:58 +0100741 llist_for_each_entry(srep, &osmo_stats_reporter_list, list) {
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100742 if (!srep->running)
743 continue;
744
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100745 if (delta == 0 && !srep->force_single_flush)
746 continue;
747
Holger Hans Peter Freyther837e9402015-11-02 15:44:26 +0100748 osmo_stats_reporter_send_counter(srep, NULL, &desc,
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100749 counter->value, delta);
750
Holger Hans Peter Freyther837e9402015-11-02 15:44:26 +0100751 /* TODO: handle result (log?, inc counter(!)?) */
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100752 }
753
754 return 0;
755}
756
757
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200758/*** main reporting function ***/
759
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100760static void flush_all_reporters()
761{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100762 struct osmo_stats_reporter *srep;
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100763
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100764 llist_for_each_entry(srep, &osmo_stats_reporter_list, list) {
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100765 if (!srep->running)
766 continue;
767
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100768 osmo_stats_reporter_send_buffer(srep);
Alexander Chemerisdfebf402020-05-08 19:10:40 +0300769
770 /* reset force_single_flush first */
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100771 srep->force_single_flush = 0;
Alexander Chemerisdfebf402020-05-08 19:10:40 +0300772 /* 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 Erlbeckd01acfc2015-10-26 16:22:45 +0100780 }
781}
782
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100783int osmo_stats_report()
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200784{
Jacob Erlbeck01e8c912015-11-09 14:13:23 +0100785 /* per group actions */
Harald Welte3217d512021-02-18 14:56:46 +0100786 TRACE(LIBOSMOCORE_STATS_START());
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100787 osmo_counters_for_each(handle_counter, NULL);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200788 rate_ctr_for_each_group(rate_ctr_group_handler, NULL);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100789 osmo_stat_item_for_each_group(osmo_stat_item_group_handler, NULL);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200790
Jacob Erlbeck01e8c912015-11-09 14:13:23 +0100791 /* global actions */
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100792 flush_all_reporters();
Harald Welte3217d512021-02-18 14:56:46 +0100793 TRACE(LIBOSMOCORE_STATS_DONE());
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100794
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200795 return 0;
796}
Harald Welte67bdd802017-01-15 17:56:11 +0100797
798#endif /* !EMBEDDED */
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200799
800/*! @} */