blob: a0834d293318f2d106095740b3479862138045ec [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
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100104#define STATS_DEFAULT_INTERVAL 5 /* secs */
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100105#define STATS_DEFAULT_BUFLEN 256
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200106
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100107static LLIST_HEAD(osmo_stats_reporter_list);
108static void *osmo_stats_ctx = NULL;
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100109static int is_initialised = 0;
110static int32_t current_stat_item_index = 0;
111
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100112static struct osmo_stats_config s_stats_config = {
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100113 .interval = STATS_DEFAULT_INTERVAL,
114};
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100115struct osmo_stats_config *osmo_stats_config = &s_stats_config;
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100116
Alexander Chemerisf5bdef42020-05-09 01:57:51 +0300117static struct osmo_fd osmo_stats_timer = { .fd = -1 };
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200118
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100119static int osmo_stats_reporter_log_send_counter(struct osmo_stats_reporter *srep,
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100120 const struct rate_ctr_group *ctrg,
121 const struct rate_ctr_desc *desc,
122 int64_t value, int64_t delta);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100123static int osmo_stats_reporter_log_send_item(struct osmo_stats_reporter *srep,
124 const struct osmo_stat_item_group *statg,
Harald Welte1554f802016-11-11 15:06:06 +0100125 const struct osmo_stat_item_desc *desc, int64_t value);
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100126
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100127static int update_srep_config(struct osmo_stats_reporter *srep)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200128{
129 int rc = 0;
130
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200131 if (srep->running) {
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100132 if (srep->close)
133 rc = srep->close(srep);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200134 srep->running = 0;
135 }
136
137 if (!srep->enabled)
138 return rc;
139
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100140 if (srep->open)
141 rc = srep->open(srep);
142 else
143 rc = 0;
144
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200145 if (rc < 0)
146 srep->enabled = 0;
147 else
148 srep->running = 1;
149
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100150 srep->force_single_flush = 1;
151
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200152 return rc;
153}
154
Alexander Chemerisf5bdef42020-05-09 01:57:51 +0300155static int osmo_stats_timer_cb(struct osmo_fd *ofd, unsigned int what)
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100156{
Alexander Chemerisf5bdef42020-05-09 01:57:51 +0300157 uint64_t expire_count;
158 int rc;
159
160 /* check that the timer has actually expired */
161 if (!(what & OSMO_FD_READ))
162 return 0;
163
164 /* read from timerfd: number of expirations of periodic timer */
165 rc = read(ofd->fd, (void *) &expire_count, sizeof(expire_count));
166 if (rc < 0 && errno == EAGAIN)
167 return 0;
168 OSMO_ASSERT(rc == sizeof(expire_count));
169
170 if (expire_count > 1)
171 LOGP(DLSTATS, LOGL_NOTICE, "Stats timer expire_count=%" PRIu64 ": We missed %" PRIu64 " timers\n",
172 expire_count, expire_count-1);
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100173
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100174 if (!llist_empty(&osmo_stats_reporter_list))
175 osmo_stats_report();
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100176
Alexander Chemerisf5bdef42020-05-09 01:57:51 +0300177 return 0;
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100178}
179
180static int start_timer()
181{
Alexander Chemerisf5bdef42020-05-09 01:57:51 +0300182 int rc;
183 int interval = osmo_stats_config->interval;
184
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100185 if (!is_initialised)
186 return -ESRCH;
187
Alexander Chemerisf5bdef42020-05-09 01:57:51 +0300188 struct timespec ts_first = {.tv_sec=0, .tv_nsec=1000};
189 struct timespec ts_interval = {.tv_sec=interval, .tv_nsec=0};
190
191 rc = osmo_timerfd_setup(&osmo_stats_timer, osmo_stats_timer_cb, NULL);
192 if (rc < 0)
193 LOGP(DLSTATS, LOGL_ERROR, "Failed to setup the timer with error code %d (fd=%d)\n",
194 rc, osmo_stats_timer.fd);
Alexander Chemerisf5bdef42020-05-09 01:57:51 +0300195
Daniel Willmann1a1de332020-07-14 18:11:14 +0200196 if (interval == 0) {
197 rc = osmo_timerfd_disable(&osmo_stats_timer);
198 if (rc < 0)
199 LOGP(DLSTATS, LOGL_ERROR, "Failed to disable the timer with error code %d (fd=%d)\n",
200 rc, osmo_stats_timer.fd);
201 } else {
202
203 rc = osmo_timerfd_schedule(&osmo_stats_timer, &ts_first, &ts_interval);
204 if (rc < 0)
205 LOGP(DLSTATS, LOGL_ERROR, "Failed to schedule the timer with error code %d (fd=%d, interval %d sec)\n",
206 rc, osmo_stats_timer.fd, interval);
207
208 LOGP(DLSTATS, LOGL_INFO, "Stats timer started with interval %d sec\n", interval);
209 }
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100210
211 return 0;
212}
213
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100214struct osmo_stats_reporter *osmo_stats_reporter_alloc(enum osmo_stats_reporter_type type,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200215 const char *name)
216{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100217 struct osmo_stats_reporter *srep;
218 srep = talloc_zero(osmo_stats_ctx, struct osmo_stats_reporter);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200219 OSMO_ASSERT(srep);
220 srep->type = type;
221 if (name)
222 srep->name = talloc_strdup(srep, name);
223 srep->fd = -1;
224
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100225 llist_add(&srep->list, &osmo_stats_reporter_list);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200226
227 return srep;
228}
229
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200230/*! Destroy a given stats_reporter. Takes care of first disabling it.
231 * \param[in] srep stats_reporter that shall be disabled + destroyed */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100232void osmo_stats_reporter_free(struct osmo_stats_reporter *srep)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200233{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100234 osmo_stats_reporter_disable(srep);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200235 llist_del(&srep->list);
236 talloc_free(srep);
237}
238
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200239/*! Initilize the stats reporting module; call this once in your program
240 * \param[in] ctx Talloc context from which stats related memory is allocated */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100241void osmo_stats_init(void *ctx)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200242{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100243 osmo_stats_ctx = ctx;
244 osmo_stat_item_discard_all(&current_stat_item_index);
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100245
246 is_initialised = 1;
247 start_timer();
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200248}
249
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200250/*! Find a stats_reporter of given \a type and \a name.
251 * \param[in] type Type of stats_reporter to find
252 * \param[in] name Name of stats_reporter to find
253 * \returns stats_reporter matching \a type and \a name; NULL otherwise */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100254struct osmo_stats_reporter *osmo_stats_reporter_find(enum osmo_stats_reporter_type type,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200255 const char *name)
256{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100257 struct osmo_stats_reporter *srep;
258 llist_for_each_entry(srep, &osmo_stats_reporter_list, list) {
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200259 if (srep->type != type)
260 continue;
261 if (srep->name != name) {
262 if (name == NULL || srep->name == NULL ||
263 strcmp(name, srep->name) != 0)
264 continue;
265 }
266 return srep;
267 }
268 return NULL;
269}
270
Harald Welte67bdd802017-01-15 17:56:11 +0100271#ifdef HAVE_SYS_SOCKET_H
272
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200273/*! Set the remote (IP) address of a given stats_reporter.
274 * \param[in] srep stats_reporter whose remote address is to be set
275 * \param[in] addr String representation of remote IPv4 address
276 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100277int osmo_stats_reporter_set_remote_addr(struct osmo_stats_reporter *srep, const char *addr)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200278{
279 int rc;
280 struct sockaddr_in *sock_addr = (struct sockaddr_in *)&srep->dest_addr;
281 struct in_addr inaddr;
282
Jacob Erlbecked197fd2015-10-27 14:43:24 +0100283 if (!srep->have_net_config)
284 return -ENOTSUP;
285
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200286 OSMO_ASSERT(addr != NULL);
287
288 rc = inet_pton(AF_INET, addr, &inaddr);
289 if (rc <= 0)
290 return -EINVAL;
291
292 sock_addr->sin_addr = inaddr;
293 sock_addr->sin_family = AF_INET;
294 srep->dest_addr_len = sizeof(*sock_addr);
295
296 talloc_free(srep->dest_addr_str);
297 srep->dest_addr_str = talloc_strdup(srep, addr);
298
299 return update_srep_config(srep);
300}
301
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200302/*! Set the remote (UDP) port of a given stats_reporter
303 * \param[in] srep stats_reporter whose remote address is to be set
304 * \param[in] port UDP port of remote statsd to which we report
305 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100306int osmo_stats_reporter_set_remote_port(struct osmo_stats_reporter *srep, int port)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200307{
308 struct sockaddr_in *sock_addr = (struct sockaddr_in *)&srep->dest_addr;
309
Jacob Erlbecked197fd2015-10-27 14:43:24 +0100310 if (!srep->have_net_config)
311 return -ENOTSUP;
312
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200313 srep->dest_port = port;
Harald Welte95871da2017-05-15 12:11:36 +0200314 sock_addr->sin_port = osmo_htons(port);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200315
316 return update_srep_config(srep);
317}
318
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200319/*! Set the local (IP) address of a given stats_reporter.
320 * \param[in] srep stats_reporter whose remote address is to be set
321 * \param[in] addr String representation of local IP address
322 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100323int osmo_stats_reporter_set_local_addr(struct osmo_stats_reporter *srep, const char *addr)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200324{
325 int rc;
326 struct sockaddr_in *sock_addr = (struct sockaddr_in *)&srep->bind_addr;
327 struct in_addr inaddr;
328
Jacob Erlbecked197fd2015-10-27 14:43:24 +0100329 if (!srep->have_net_config)
330 return -ENOTSUP;
331
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200332 if (addr) {
333 rc = inet_pton(AF_INET, addr, &inaddr);
334 if (rc <= 0)
335 return -EINVAL;
336 } else {
Holger Hans Peter Freyther79219752015-11-02 15:50:32 +0100337 inaddr.s_addr = INADDR_ANY;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200338 }
339
340 sock_addr->sin_addr = inaddr;
341 sock_addr->sin_family = AF_INET;
342 srep->bind_addr_len = addr ? sizeof(*sock_addr) : 0;
343
344 talloc_free(srep->bind_addr_str);
345 srep->bind_addr_str = addr ? talloc_strdup(srep, addr) : NULL;
346
347 return update_srep_config(srep);
348}
349
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200350/*! Set the maximum transmission unit of a given stats_reporter.
351 * \param[in] srep stats_reporter whose remote address is to be set
352 * \param[in] mtu Maximum Transmission Unit of \a srep
353 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100354int osmo_stats_reporter_set_mtu(struct osmo_stats_reporter *srep, int mtu)
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100355{
Jacob Erlbecked197fd2015-10-27 14:43:24 +0100356 if (!srep->have_net_config)
357 return -ENOTSUP;
358
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100359 if (mtu < 0)
360 return -EINVAL;
361
362 srep->mtu = mtu;
363
364 return update_srep_config(srep);
365}
Harald Welte67bdd802017-01-15 17:56:11 +0100366#endif /* HAVE_SYS_SOCKETS_H */
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100367
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100368int osmo_stats_reporter_set_max_class(struct osmo_stats_reporter *srep,
369 enum osmo_stats_class class_id)
370{
371 if (class_id == OSMO_STATS_CLASS_UNKNOWN)
372 return -EINVAL;
373
374 srep->max_class = class_id;
375
376 return 0;
377}
378
Alexander Chemerisf203ed32020-05-08 19:09:22 +0300379/*! Set the reporting interval (common for all reporters)
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200380 * \param[in] interval Reporting interval in seconds
381 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100382int osmo_stats_set_interval(int interval)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200383{
Daniel Willmann1a1de332020-07-14 18:11:14 +0200384 if (interval < 0)
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100385 return -EINVAL;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200386
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100387 osmo_stats_config->interval = interval;
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100388 if (is_initialised)
389 start_timer();
390
391 return 0;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200392}
393
Alexander Chemerisdfebf402020-05-08 19:10:40 +0300394/*! Set the regular flush period for a given stats_reporter
395 *
396 * Send all stats even if they have not changed (i.e. force the flush)
397 * every N-th reporting interval. Set to 0 to disable regular flush,
398 * set to 1 to flush every time, set to 2 to flush every 2nd time, etc.
399 * \param[in] srep stats_reporter to set flush period for
400 * \param[in] period Reporting interval in seconds
401 * \returns 0 on success; negative on error */
402int osmo_stats_reporter_set_flush_period(struct osmo_stats_reporter *srep, unsigned int period)
403{
404 srep->flush_period = period;
405 srep->flush_period_counter = 0;
406 /* force the flush now if it's not disabled by period=0 */
407 if (period > 0)
408 srep->force_single_flush = 1;
409
410 return 0;
411}
412
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200413/*! Set the name prefix of a given stats_reporter.
414 * \param[in] srep stats_reporter whose name prefix is to be set
415 * \param[in] prefix NAme perfix to pre-pend for any reported value
416 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100417int osmo_stats_reporter_set_name_prefix(struct osmo_stats_reporter *srep, const char *prefix)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200418{
419 talloc_free(srep->name_prefix);
Jacob Erlbeck916423e2015-11-09 10:52:19 +0100420 srep->name_prefix = prefix && strlen(prefix) > 0 ?
421 talloc_strdup(srep, prefix) : NULL;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200422
423 return update_srep_config(srep);
424}
425
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200426
427/*! Enable the given stats_reporter.
428 * \param[in] srep stats_reporter who is to be enabled
429 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100430int osmo_stats_reporter_enable(struct osmo_stats_reporter *srep)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200431{
432 srep->enabled = 1;
433
434 return update_srep_config(srep);
435}
436
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200437/*! Disable the given stats_reporter.
438 * \param[in] srep stats_reporter who is to be disabled
439 * \returns 0 on success; negative on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100440int osmo_stats_reporter_disable(struct osmo_stats_reporter *srep)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200441{
442 srep->enabled = 0;
443
444 return update_srep_config(srep);
445}
446
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100447/*** i/o helper functions ***/
448
Harald Welte67bdd802017-01-15 17:56:11 +0100449#ifdef HAVE_SYS_SOCKET_H
450
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200451/*! Open the UDP socket for given stats_reporter.
452 * \param[in] srep stats_reporter whose UDP socket is to be opened
453 * ]returns 0 on success; negative otherwise */
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100454int osmo_stats_reporter_udp_open(struct osmo_stats_reporter *srep)
455{
456 int sock;
457 int rc;
458 int buffer_size = STATS_DEFAULT_BUFLEN;
459
460 if (srep->fd != -1 && srep->close)
461 srep->close(srep);
462
463 sock = socket(AF_INET, SOCK_DGRAM, 0);
464 if (sock == -1)
465 return -errno;
466
Arran Cudbard-Bellcc3694b2016-05-18 16:02:19 -0400467#if defined(__APPLE__) && !defined(MSG_NOSIGNAL)
468 {
469 static int val = 1;
470
471 rc = setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, (void*)&val, sizeof(val));
472 goto failed;
473 }
474#endif
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100475 if (srep->bind_addr_len > 0) {
476 rc = bind(sock, &srep->bind_addr, srep->bind_addr_len);
477 if (rc == -1)
478 goto failed;
479 }
480
481 srep->fd = sock;
482
483 if (srep->mtu > 0) {
484 buffer_size = srep->mtu - 20 /* IP */ - 8 /* UDP */;
485 srep->agg_enabled = 1;
486 }
487
488 srep->buffer = msgb_alloc(buffer_size, "stats buffer");
489
490 return 0;
491
492failed:
493 rc = -errno;
494 close(sock);
495
496 return rc;
497}
498
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200499/*! Closee the UDP socket for given stats_reporter.
500 * \param[in] srep stats_reporter whose UDP socket is to be closed
501 * ]returns 0 on success; negative otherwise */
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100502int osmo_stats_reporter_udp_close(struct osmo_stats_reporter *srep)
503{
504 int rc;
505 if (srep->fd == -1)
506 return -EBADF;
507
508 osmo_stats_reporter_send_buffer(srep);
509
510 rc = close(srep->fd);
511 srep->fd = -1;
512 msgb_free(srep->buffer);
513 srep->buffer = NULL;
514 return rc == -1 ? -errno : 0;
515}
516
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200517/*! Send given date to given stats_reporter.
518 * \param[in] srep stats_reporter whose UDP socket is to be opened
519 * \param[in] data string data to be sent
520 * \param[in] data_len Length of \a data in bytes
521 * \returns number of bytes on success; negative otherwise */
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100522int osmo_stats_reporter_send(struct osmo_stats_reporter *srep, const char *data,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200523 int data_len)
524{
525 int rc;
526
Arran Cudbard-Bellcc3694b2016-05-18 16:02:19 -0400527 rc = sendto(srep->fd, data, data_len,
528#ifdef MSG_NOSIGNAL
529 MSG_NOSIGNAL |
530#endif
531 MSG_DONTWAIT,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200532 &srep->dest_addr, srep->dest_addr_len);
533
534 if (rc == -1)
535 rc = -errno;
536
537 return rc;
538}
539
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200540/*! Send current accumulated buffer to given stats_reporter.
541 * \param[in] srep stats_reporter whose UDP socket is to be opened
542 * \returns number of bytes on success; negative otherwise */
Jacob Erlbeckb6e6bea2015-11-09 15:33:44 +0100543int osmo_stats_reporter_send_buffer(struct osmo_stats_reporter *srep)
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100544{
545 int rc;
546
547 if (!srep->buffer || msgb_length(srep->buffer) == 0)
548 return 0;
549
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100550 rc = osmo_stats_reporter_send(srep,
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100551 (const char *)msgb_data(srep->buffer), msgb_length(srep->buffer));
552
553 msgb_trim(srep->buffer, 0);
554
555 return rc;
556}
Harald Welte67bdd802017-01-15 17:56:11 +0100557#endif /* HAVE_SYS_SOCKET_H */
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100558
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100559/*** log reporter ***/
560
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200561/*! Create a stats_reporter that logs via libosmocore logging.
562 * A stats_reporter created via this function will simply print the statistics
563 * via the libosmocore logging framework, using DLSTATS subsystem and LOGL_INFO
564 * priority. The configuration of the libosmocore log targets define where this
565 * information will end up (ignored, text file, stderr, syslog, ...).
566 * \param[in] name Name of the to-be-created stats_reporter
567 * \returns stats_reporter on success; NULL on error */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100568struct osmo_stats_reporter *osmo_stats_reporter_create_log(const char *name)
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100569{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100570 struct osmo_stats_reporter *srep;
571 srep = osmo_stats_reporter_alloc(OSMO_STATS_REPORTER_LOG, name);
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100572
573 srep->have_net_config = 0;
574
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100575 srep->send_counter = osmo_stats_reporter_log_send_counter;
576 srep->send_item = osmo_stats_reporter_log_send_item;
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100577
578 return srep;
579}
580
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100581static int osmo_stats_reporter_log_send(struct osmo_stats_reporter *srep,
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100582 const char *type,
Jacob Erlbeck16fe8da2015-11-02 11:30:01 +0100583 const char *name1, unsigned int index1, const char *name2, int value,
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100584 const char *unit)
585{
Jacob Erlbeck79125ec2015-11-02 15:17:50 +0100586 LOGP(DLSTATS, LOGL_INFO,
Jacob Erlbeck16fe8da2015-11-02 11:30:01 +0100587 "stats t=%s p=%s g=%s i=%u n=%s v=%d u=%s\n",
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100588 type, srep->name_prefix ? srep->name_prefix : "",
589 name1 ? name1 : "", index1,
590 name2, value, unit ? unit : "");
591
592 return 0;
593}
594
595
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100596static int osmo_stats_reporter_log_send_counter(struct osmo_stats_reporter *srep,
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100597 const struct rate_ctr_group *ctrg,
598 const struct rate_ctr_desc *desc,
599 int64_t value, int64_t delta)
600{
601 if (ctrg)
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100602 return osmo_stats_reporter_log_send(srep, "c",
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100603 ctrg->desc->group_name_prefix,
604 ctrg->idx,
605 desc->name, value, NULL);
606 else
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100607 return osmo_stats_reporter_log_send(srep, "c",
Jacob Erlbeck16fe8da2015-11-02 11:30:01 +0100608 NULL, 0,
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100609 desc->name, value, NULL);
610}
611
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100612static int osmo_stats_reporter_log_send_item(struct osmo_stats_reporter *srep,
613 const struct osmo_stat_item_group *statg,
Harald Welte1554f802016-11-11 15:06:06 +0100614 const struct osmo_stat_item_desc *desc, int64_t value)
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100615{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100616 return osmo_stats_reporter_log_send(srep, "i",
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100617 statg->desc->group_name_prefix, statg->idx,
618 desc->name, value, desc->unit);
619}
620
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100621/*** helper for reporting ***/
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200622
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100623static int osmo_stats_reporter_check_config(struct osmo_stats_reporter *srep,
624 unsigned int index, int class_id)
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200625{
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100626 if (class_id == OSMO_STATS_CLASS_UNKNOWN)
627 class_id = index != 0 ?
628 OSMO_STATS_CLASS_SUBSCRIBER : OSMO_STATS_CLASS_GLOBAL;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200629
Jacob Erlbeck2e8f9ed2015-11-09 15:48:25 +0100630 return class_id <= srep->max_class;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200631}
632
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200633/*** generic rate counter support ***/
634
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100635static int osmo_stats_reporter_send_counter(struct osmo_stats_reporter *srep,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200636 const struct rate_ctr_group *ctrg,
637 const struct rate_ctr_desc *desc,
638 int64_t value, int64_t delta)
639{
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100640 if (!srep->send_counter)
641 return 0;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200642
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100643 return srep->send_counter(srep, ctrg, desc, value, delta);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200644}
645
646static int rate_ctr_handler(
647 struct rate_ctr_group *ctrg, struct rate_ctr *ctr,
648 const struct rate_ctr_desc *desc, void *sctx_)
649{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100650 struct osmo_stats_reporter *srep;
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200651 int64_t delta = rate_ctr_difference(ctr);
652
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100653 llist_for_each_entry(srep, &osmo_stats_reporter_list, list) {
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200654 if (!srep->running)
655 continue;
656
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100657 if (delta == 0 && !srep->force_single_flush)
658 continue;
659
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100660 if (!osmo_stats_reporter_check_config(srep,
661 ctrg->idx, ctrg->desc->class_id))
Jacob Erlbeck8a97cb92015-11-09 11:39:42 +0100662 continue;
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100663
Holger Hans Peter Freyther837e9402015-11-02 15:44:26 +0100664 osmo_stats_reporter_send_counter(srep, ctrg, desc,
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200665 ctr->current, delta);
666
Holger Hans Peter Freyther837e9402015-11-02 15:44:26 +0100667 /* TODO: handle result (log?, inc counter(!)?) or remove it */
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200668 }
669
670 return 0;
671}
672
673static int rate_ctr_group_handler(struct rate_ctr_group *ctrg, void *sctx_)
674{
675 rate_ctr_for_each_counter(ctrg, rate_ctr_handler, sctx_);
676
677 return 0;
678}
679
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100680/*** stat item support ***/
681
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100682static int osmo_stats_reporter_send_item(struct osmo_stats_reporter *srep,
683 const struct osmo_stat_item_group *statg,
684 const struct osmo_stat_item_desc *desc,
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100685 int32_t value)
686{
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100687 if (!srep->send_item)
688 return 0;
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100689
Jacob Erlbeck490b38f2015-10-27 15:10:28 +0100690 return srep->send_item(srep, statg, desc, value);
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100691}
692
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100693static int osmo_stat_item_handler(
694 struct osmo_stat_item_group *statg, struct osmo_stat_item *item, void *sctx_)
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100695{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100696 struct osmo_stats_reporter *srep;
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100697 int32_t idx = current_stat_item_index;
698 int32_t value;
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100699 int have_value;
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100700
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100701 have_value = osmo_stat_item_get_next(item, &idx, &value) > 0;
702 if (!have_value)
703 /* Send the last value in case a flush is requested */
704 value = osmo_stat_item_get_last(item);
705
706 do {
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100707 llist_for_each_entry(srep, &osmo_stats_reporter_list, list) {
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100708 if (!srep->running)
709 continue;
710
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100711 if (!have_value && !srep->force_single_flush)
712 continue;
713
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100714 if (!osmo_stats_reporter_check_config(srep,
715 statg->idx, statg->desc->class_id))
Jacob Erlbeck8a97cb92015-11-09 11:39:42 +0100716 continue;
Jacob Erlbeckbc9d9ac2015-11-02 14:49:35 +0100717
Holger Hans Peter Freyther837e9402015-11-02 15:44:26 +0100718 osmo_stats_reporter_send_item(srep, statg,
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100719 item->desc, value);
720 }
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100721
722 if (!have_value)
723 break;
724
725 have_value = osmo_stat_item_get_next(item, &idx, &value) > 0;
726 } while (have_value);
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100727
728 return 0;
729}
730
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100731static int osmo_stat_item_group_handler(struct osmo_stat_item_group *statg, void *sctx_)
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100732{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100733 osmo_stat_item_for_each_item(statg, osmo_stat_item_handler, sctx_);
Jacob Erlbeckc27671c2015-10-26 12:32:07 +0100734
735 return 0;
736}
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200737
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100738/*** osmo counter support ***/
739
740static int handle_counter(struct osmo_counter *counter, void *sctx_)
741{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100742 struct osmo_stats_reporter *srep;
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100743 struct rate_ctr_desc desc = {0};
744 /* Fake a rate counter description */
745 desc.name = counter->name;
746 desc.description = counter->description;
747
748 int delta = osmo_counter_difference(counter);
749
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100750 llist_for_each_entry(srep, &osmo_stats_reporter_list, list) {
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100751 if (!srep->running)
752 continue;
753
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100754 if (delta == 0 && !srep->force_single_flush)
755 continue;
756
Holger Hans Peter Freyther837e9402015-11-02 15:44:26 +0100757 osmo_stats_reporter_send_counter(srep, NULL, &desc,
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100758 counter->value, delta);
759
Holger Hans Peter Freyther837e9402015-11-02 15:44:26 +0100760 /* TODO: handle result (log?, inc counter(!)?) */
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100761 }
762
763 return 0;
764}
765
766
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200767/*** main reporting function ***/
768
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100769static void flush_all_reporters()
770{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100771 struct osmo_stats_reporter *srep;
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100772
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100773 llist_for_each_entry(srep, &osmo_stats_reporter_list, list) {
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100774 if (!srep->running)
775 continue;
776
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100777 osmo_stats_reporter_send_buffer(srep);
Alexander Chemerisdfebf402020-05-08 19:10:40 +0300778
779 /* reset force_single_flush first */
Jacob Erlbeckaed7c122015-11-09 11:25:12 +0100780 srep->force_single_flush = 0;
Alexander Chemerisdfebf402020-05-08 19:10:40 +0300781 /* and schedule a new flush if it's time for it */
782 if (srep->flush_period > 0) {
783 srep->flush_period_counter++;
784 if (srep->flush_period_counter >= srep->flush_period) {
785 srep->force_single_flush = 1;
786 srep->flush_period_counter = 0;
787 }
788 }
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100789 }
790}
791
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100792int osmo_stats_report()
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200793{
Jacob Erlbeck01e8c912015-11-09 14:13:23 +0100794 /* per group actions */
Harald Welte3217d512021-02-18 14:56:46 +0100795 TRACE(LIBOSMOCORE_STATS_START());
Jacob Erlbeckc8f47b62015-10-26 14:42:05 +0100796 osmo_counters_for_each(handle_counter, NULL);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200797 rate_ctr_for_each_group(rate_ctr_group_handler, NULL);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100798 osmo_stat_item_for_each_group(osmo_stat_item_group_handler, NULL);
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200799
Jacob Erlbeck01e8c912015-11-09 14:13:23 +0100800 /* global actions */
801 osmo_stat_item_discard_all(&current_stat_item_index);
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100802 flush_all_reporters();
Harald Welte3217d512021-02-18 14:56:46 +0100803 TRACE(LIBOSMOCORE_STATS_DONE());
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100804
Jacob Erlbeck95bf82802015-10-20 19:05:52 +0200805 return 0;
806}
Harald Welte67bdd802017-01-15 17:56:11 +0100807
808#endif /* !EMBEDDED */
Harald Welteeb5b6ce2017-10-15 20:03:24 +0200809
810/*! @} */