diff options
author | Holger Hans Peter Freyther <holger@moiji-mobile.com> | 2015-11-02 15:57:34 +0100 |
---|---|---|
committer | Holger Hans Peter Freyther <holger@moiji-mobile.com> | 2015-11-02 15:57:34 +0100 |
commit | d7b0577d7d30139491b5cfeffb467440f9e88818 (patch) | |
tree | 0666fa8f415a47e8f5645dd87ad8844251251c6a /include/osmocom/core | |
parent | c84851bccc2e5e60536afa474a5f13134a3b79c9 (diff) | |
parent | 8f0374f7521376bdb721e821047e8a6a4a727283 (diff) |
Merge branch 'jerlbeck/wip/stats'
* This adds a new counter type (to measure time or delay)
* A statsd reporting backend. This can be fed into graphite
or similar tools.
* A periodic log backend for performance values
Diffstat (limited to 'include/osmocom/core')
-rw-r--r-- | include/osmocom/core/logging.h | 3 | ||||
-rw-r--r-- | include/osmocom/core/rate_ctr.h | 21 | ||||
-rw-r--r-- | include/osmocom/core/stat_item.h | 130 | ||||
-rw-r--r-- | include/osmocom/core/statistics.h | 8 | ||||
-rw-r--r-- | include/osmocom/core/stats.h | 110 |
5 files changed, 269 insertions, 3 deletions
diff --git a/include/osmocom/core/logging.h b/include/osmocom/core/logging.h index ba41762f..1c159d0b 100644 --- a/include/osmocom/core/logging.h +++ b/include/osmocom/core/logging.h @@ -69,7 +69,8 @@ void logp(int subsys, const char *file, int line, int cont, const char *format, #define DLSMS -7 #define DLCTRL -8 #define DLGTP -9 -#define OSMO_NUM_DLIB 9 +#define DLSTATS -10 +#define OSMO_NUM_DLIB 10 struct log_category { uint8_t loglevel; diff --git a/include/osmocom/core/rate_ctr.h b/include/osmocom/core/rate_ctr.h index 821c7cfd..03b1bfbe 100644 --- a/include/osmocom/core/rate_ctr.h +++ b/include/osmocom/core/rate_ctr.h @@ -30,6 +30,7 @@ struct rate_ctr_per_intv { /*! \brief data we keep for each actual value */ struct rate_ctr { uint64_t current; /*!< \brief current value */ + uint64_t previous; /*!< \brief previous value, used for delta */ /*! \brief per-interval data */ struct rate_ctr_per_intv intv[RATE_CTR_INTV_NUM]; }; @@ -46,6 +47,8 @@ struct rate_ctr_group_desc { const char *group_name_prefix; /*! \brief The human-readable description of the group */ const char *group_description; + /*! \brief The class to which this group belongs */ + int class_id; /*! \brief The number of counters in this group */ const unsigned int num_ctr; /*! \brief Pointer to array of counter names */ @@ -78,9 +81,27 @@ static inline void rate_ctr_inc(struct rate_ctr *ctr) rate_ctr_add(ctr, 1); } +/*! \brief Return the counter difference since the last call to this function */ +int64_t rate_ctr_difference(struct rate_ctr *ctr); + int rate_ctr_init(void *tall_ctx); struct rate_ctr_group *rate_ctr_get_group_by_name_idx(const char *name, const unsigned int idx); const struct rate_ctr *rate_ctr_get_by_name(const struct rate_ctr_group *ctrg, const char *name); +typedef int (*rate_ctr_handler_t)( + struct rate_ctr_group *, struct rate_ctr *, + const struct rate_ctr_desc *, void *); +typedef int (*rate_ctr_group_handler_t)(struct rate_ctr_group *, void *); + + +/*! \brief Iterate over all counters + * \param[in] handle_item Call-back function, aborts if rc < 0 + * \param[in] data Private data handed through to \a handle_counter + */ +int rate_ctr_for_each_counter(struct rate_ctr_group *ctrg, + rate_ctr_handler_t handle_counter, void *data); + +int rate_ctr_for_each_group(rate_ctr_group_handler_t handle_group, void *data); + /*! @} */ diff --git a/include/osmocom/core/stat_item.h b/include/osmocom/core/stat_item.h new file mode 100644 index 00000000..c2ad8cfd --- /dev/null +++ b/include/osmocom/core/stat_item.h @@ -0,0 +1,130 @@ +#pragma once + +/*! \defgroup osmo_stat_item Statistics value item + * @{ + */ + +/*! \file stat_item.h */ + +#include <stdint.h> + +#include <osmocom/core/linuxlist.h> + +struct osmo_stat_item_desc; + +#define STAT_ITEM_NOVALUE_ID 0 + +struct osmo_stat_item_value { + int32_t id; + int32_t value; +}; + +/*! \brief data we keep for each actual value */ +struct osmo_stat_item { + const struct osmo_stat_item_desc *desc; + /*! \brief the index of the freshest value */ + int32_t last_value_index; + /*! \brief offset to the freshest value in the value fifo */ + int16_t last_offs; + /*! \brief value fifo */ + struct osmo_stat_item_value values[0]; +}; + +/*! \brief statistics value description */ +struct osmo_stat_item_desc { + const char *name; /*!< \brief name of the item */ + const char *description;/*!< \brief description of the item */ + const char *unit; /*!< \brief unit of a value */ + unsigned int num_values;/*!< \brief number of values to store */ + int32_t default_value; +}; + +/*! \brief description of a statistics value group */ +struct osmo_stat_item_group_desc { + /*! \brief The prefix to the name of all values in this group */ + const char *group_name_prefix; + /*! \brief The human-readable description of the group */ + const char *group_description; + /*! \brief The class to which this group belongs */ + int class_id; + /*! \brief The number of values in this group */ + const unsigned int num_items; + /*! \brief Pointer to array of value names */ + const struct osmo_stat_item_desc *item_desc; +}; + +/*! \brief One instance of a counter group class */ +struct osmo_stat_item_group { + /*! \brief Linked list of all value groups in the system */ + struct llist_head list; + /*! \brief Pointer to the counter group class */ + const struct osmo_stat_item_group_desc *desc; + /*! \brief The index of this value group within its class */ + unsigned int idx; + /*! \brief Actual counter structures below */ + struct osmo_stat_item *items[0]; +}; + +struct osmo_stat_item_group *osmo_stat_item_group_alloc( + void *ctx, + const struct osmo_stat_item_group_desc *desc, + unsigned int idx); + +void osmo_stat_item_group_free(struct osmo_stat_item_group *statg); + +void osmo_stat_item_set(struct osmo_stat_item *item, int32_t value); + +int osmo_stat_item_init(void *tall_ctx); + +struct osmo_stat_item_group *osmo_stat_item_get_group_by_name_idx( + const char *name, const unsigned int idx); + +const struct osmo_stat_item *osmo_stat_item_get_by_name( + const struct osmo_stat_item_group *statg, const char *name); + +/*! \brief Retrieve the next value from the osmo_stat_item object. + * If a new value has been set, it is returned. The idx is used to decide + * which value to return. + * On success, *idx is updated to refer to the next unread value. If + * values have been missed due to FIFO overflow, *idx is incremented by + * (1 + num_lost). + * This way, the osmo_stat_item object can be kept stateless from the reader's + * perspective and therefore be used by several backends simultaneously. + * + * \param val the osmo_stat_item object + * \param idx identifies the next value to be read + * \param value a pointer to store the value + * \returns the increment of the index (0: no value has been read, + * 1: one value has been taken, + * (1+n): n values have been skipped, one has been taken) + */ +int osmo_stat_item_get_next(const struct osmo_stat_item *item, int32_t *idx, int32_t *value); + +/*! \brief Get the last (freshest) value */ +static int32_t osmo_stat_item_get_last(const struct osmo_stat_item *item); + +/*! \brief Skip all values of the item and update idx accordingly */ +int osmo_stat_item_discard(const struct osmo_stat_item *item, int32_t *idx); + +/*! \brief Skip all values of all items and update idx accordingly */ +int osmo_stat_item_discard_all(int32_t *idx); + +typedef int (*osmo_stat_item_handler_t)( + struct osmo_stat_item_group *, struct osmo_stat_item *, void *); + +typedef int (*osmo_stat_item_group_handler_t)(struct osmo_stat_item_group *, void *); + +/*! \brief Iteate over all items + * \param[in] handle_item Call-back function, aborts if rc < 0 + * \param[in] data Private data handed through to \a handle_item + */ +int osmo_stat_item_for_each_item(struct osmo_stat_item_group *statg, + osmo_stat_item_handler_t handle_item, void *data); + +int osmo_stat_item_for_each_group(osmo_stat_item_group_handler_t handle_group, void *data); + +static inline int32_t osmo_stat_item_get_last(const struct osmo_stat_item *item) +{ + return item->values[item->last_offs].value; +} +/*! @} */ diff --git a/include/osmocom/core/statistics.h b/include/osmocom/core/statistics.h index de250bec..1e472ffd 100644 --- a/include/osmocom/core/statistics.h +++ b/include/osmocom/core/statistics.h @@ -9,6 +9,7 @@ struct osmo_counter { const char *name; /*!< \brief human-readable name */ const char *description; /*!< \brief humn-readable description */ unsigned long value; /*!< \brief current value */ + unsigned long previous; /*!< \brief previous value */ }; /*! \brief Increment counter */ @@ -37,8 +38,8 @@ struct osmo_counter *osmo_counter_alloc(const char *name); */ void osmo_counter_free(struct osmo_counter *ctr); -/*! \brief Iteate over all counters - * \param[in] handle_counter Call-back function +/*! \brief Iterate over all counters + * \param[in] handle_counter Call-back function, aborts if rc < 0 * \param[in] data Private dtata handed through to \a handle_counter */ int osmo_counters_for_each(int (*handle_counter)(struct osmo_counter *, void *), void *data); @@ -48,3 +49,6 @@ int osmo_counters_for_each(int (*handle_counter)(struct osmo_counter *, void *), * \returns pointer to counter (\ref osmo_counter) or NULL otherwise */ struct osmo_counter *osmo_counter_get_by_name(const char *name); + +/*! \brief Return the counter difference since the last call to this function */ +int osmo_counter_difference(struct osmo_counter *ctr); diff --git a/include/osmocom/core/stats.h b/include/osmocom/core/stats.h new file mode 100644 index 00000000..731fdb9b --- /dev/null +++ b/include/osmocom/core/stats.h @@ -0,0 +1,110 @@ +/* (C) 2015 by Sysmocom s.f.m.c. GmbH + * + * All Rights Reserved + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ +#pragma once + +#include <sys/socket.h> +#include <osmocom/core/linuxlist.h> + +struct msgb; +struct osmo_stat_item_group; +struct osmo_stat_item_desc; +struct rate_ctr_group; +struct rate_ctr_desc; + +enum osmo_stats_class { + OSMO_STATS_CLASS_UNKNOWN, + OSMO_STATS_CLASS_GLOBAL, + OSMO_STATS_CLASS_PEER, + OSMO_STATS_CLASS_SUBSCRIBER, +}; + +enum osmo_stats_reporter_type { + OSMO_STATS_REPORTER_STATSD, + OSMO_STATS_REPORTER_LOG, +}; + +struct osmo_stats_reporter { + enum osmo_stats_reporter_type type; + char *name; + + unsigned int have_net_config : 1; + + /* config */ + int enabled; + char *name_prefix; + char *dest_addr_str; + char *bind_addr_str; + int dest_port; + int mtu; + enum osmo_stats_class max_class; + + /* state */ + int running; + struct sockaddr dest_addr; + int dest_addr_len; + struct sockaddr bind_addr; + int bind_addr_len; + int fd; + struct msgb *buffer; + int agg_enabled; + + struct llist_head list; + int (*open)(struct osmo_stats_reporter *srep); + int (*close)(struct osmo_stats_reporter *srep); + int (*send_counter)(struct osmo_stats_reporter *srep, + const struct rate_ctr_group *ctrg, + const struct rate_ctr_desc *desc, + int64_t value, int64_t delta); + int (*send_item)(struct osmo_stats_reporter *srep, + const struct osmo_stat_item_group *statg, + const struct osmo_stat_item_desc *desc, + int32_t value); +}; + +struct osmo_stats_config { + int interval; +}; + +extern struct osmo_stats_config *osmo_stats_config; + +void osmo_stats_init(void *ctx); +int osmo_stats_report(); + +int osmo_stats_set_interval(int interval); + +struct osmo_stats_reporter *osmo_stats_reporter_alloc(enum osmo_stats_reporter_type type, + const char *name); +void osmo_stats_reporter_free(struct osmo_stats_reporter *srep); + +struct osmo_stats_reporter *osmo_stats_reporter_create_statsd(const char *name); +struct osmo_stats_reporter *osmo_stats_reporter_create_log(const char *name); + +struct osmo_stats_reporter *osmo_stats_reporter_find(enum osmo_stats_reporter_type type, + const char *name); + +int osmo_stats_reporter_set_remote_addr(struct osmo_stats_reporter *srep, const char *addr); +int osmo_stats_reporter_set_remote_port(struct osmo_stats_reporter *srep, int port); +int osmo_stats_reporter_set_local_addr(struct osmo_stats_reporter *srep, const char *addr); +int osmo_stats_reporter_set_mtu(struct osmo_stats_reporter *srep, int mtu); +int osmo_stats_reporter_set_max_class(struct osmo_stats_reporter *srep, + enum osmo_stats_class class_id); +int osmo_stats_reporter_set_name_prefix(struct osmo_stats_reporter *srep, const char *prefix); +int osmo_stats_reporter_enable(struct osmo_stats_reporter *srep); +int osmo_stats_reporter_disable(struct osmo_stats_reporter *srep); |