Change in libosmocore[master]: stats: Support regular stats flush

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

laforge gerrit-no-reply at lists.osmocom.org
Sat May 9 08:08:54 UTC 2020


laforge has submitted this change. ( https://gerrit.osmocom.org/c/libosmocore/+/18138 )

Change subject: stats: Support regular stats flush
......................................................................

stats: Support regular stats flush

Reliable monitoring requires regular flush of all stat values, even
if they have not changed. Otherwise (1) the monitoring app has to
maintain state and (2) can go out of sync if it's restarted while
the app is still running.

Change-Id: I04f1e7bdf0d6f20e4f15571e94191de61c47ddad
---
M include/osmocom/core/stats.h
M src/stats.c
M src/vty/stats_vty.c
3 files changed, 59 insertions(+), 1 deletion(-)

Approvals:
  laforge: Looks good to me, approved
  pespin: Looks good to me, but someone else must approve
  Jenkins Builder: Verified



diff --git a/include/osmocom/core/stats.h b/include/osmocom/core/stats.h
index e01016d..b9edac2 100644
--- a/include/osmocom/core/stats.h
+++ b/include/osmocom/core/stats.h
@@ -73,6 +73,7 @@
 	char *bind_addr_str;	/*!< local bind IP address */
 	int dest_port;		/*!< destination (UDP) port */
 	int mtu;		/*!< Maximum Transmission Unit */
+	unsigned int flush_period;	/*!< period between regular flushes */
 
 	/*! Maximum class/index to report. FIXME: More details! */
 	enum osmo_stats_class max_class;
@@ -87,7 +88,8 @@
 	int fd;				/*!< file descriptor of socket */
 	struct msgb *buffer;		/*!< message buffer for log output */
 	int agg_enabled;		/*!< is aggregation enabled? */
-	int force_single_flush;
+	int force_single_flush;		/*!< set to 1 to force a flush (send even unchanged stats values) */
+	unsigned int flush_period_counter;	/*!< count sends between forced flushes */
 
 	struct llist_head list;
 	int (*open)(struct osmo_stats_reporter *srep);
@@ -129,6 +131,7 @@
 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);
+int osmo_stats_reporter_set_flush_period(struct osmo_stats_reporter *srep, unsigned int period);
 
 /* reporter creation */
 struct osmo_stats_reporter *osmo_stats_reporter_create_log(const char *name);
diff --git a/src/stats.c b/src/stats.c
index 61b7df8..5954167 100644
--- a/src/stats.c
+++ b/src/stats.c
@@ -341,6 +341,25 @@
 	return 0;
 }
 
+/*! Set the regular flush period for a given stats_reporter
+ *
+ * Send all stats even if they have not changed (i.e. force the flush)
+ * every N-th reporting interval. Set to 0 to disable regular flush,
+ * set to 1 to flush every time, set to 2 to flush every 2nd time, etc.
+ *  \param[in] srep stats_reporter to set flush period for
+ *  \param[in] period Reporting interval in seconds
+ *  \returns 0 on success; negative on error */
+int osmo_stats_reporter_set_flush_period(struct osmo_stats_reporter *srep, unsigned int period)
+{
+	srep->flush_period = period;
+	srep->flush_period_counter = 0;
+	/* force the flush now if it's not disabled by period=0 */
+	if (period > 0)
+		srep->force_single_flush = 1;
+
+	return 0;
+}
+
 /*! Set the name prefix of a given stats_reporter.
  *  \param[in] srep stats_reporter whose name prefix is to be set
  *  \param[in] prefix NAme perfix to pre-pend for any reported value
@@ -706,7 +725,17 @@
 			continue;
 
 		osmo_stats_reporter_send_buffer(srep);
+
+		/* reset force_single_flush first */
 		srep->force_single_flush = 0;
+		/* and schedule a new flush if it's time for it */
+		if (srep->flush_period > 0) {
+			srep->flush_period_counter++;
+			if (srep->flush_period_counter >= srep->flush_period) {
+				srep->force_single_flush = 1;
+				srep->flush_period_counter = 0;
+			}
+		}
 	}
 }
 
diff --git a/src/vty/stats_vty.c b/src/vty/stats_vty.c
index a512703..4628281 100644
--- a/src/vty/stats_vty.c
+++ b/src/vty/stats_vty.c
@@ -245,6 +245,27 @@
 	return CMD_SUCCESS;
 }
 
+DEFUN(cfg_stats_reporter_flush_period, cfg_stats_reporter_flush_period_cmd,
+	"flush-period <0-65535>",
+	CFG_STATS_STR "Send all stats even if they have not changed (i.e. force the flush)"
+	              "every N-th reporting interval. Set to 0 to disable regular flush (default).\n"
+	"0 to disable regular flush (default), 1 to flush every time, 2 to flush every 2nd time, etc\n")
+{
+	int rc;
+	unsigned int period = atoi(argv[0]);
+	struct osmo_stats_reporter *srep = osmo_stats_vty2srep(vty);
+	OSMO_ASSERT(srep);
+
+	rc = osmo_stats_reporter_set_flush_period(srep, period);
+	if (rc < 0) {
+		vty_out(vty, "%% Unable to set force flush period: %s%s",
+			strerror(-rc), VTY_NEWLINE);
+		return CMD_WARNING;
+	}
+
+	return CMD_SUCCESS;
+}
+
 DEFUN(cfg_stats_reporter_statsd, cfg_stats_reporter_statsd_cmd,
 	"stats reporter statsd",
 	CFG_STATS_STR CFG_REPORTER_STR "Report to a STATSD server\n")
@@ -588,6 +609,10 @@
 	else
 		vty_out(vty, "  no prefix%s", VTY_NEWLINE);
 
+	if (srep->flush_period > 0)
+		vty_out(vty, "  flush-period %d%s",
+			srep->flush_period, VTY_NEWLINE);
+
 	if (srep->enabled)
 		vty_out(vty, "  enable%s", VTY_NEWLINE);
 
@@ -637,6 +662,7 @@
 	install_element(CFG_STATS_NODE, &cfg_stats_reporter_level_cmd);
 	install_element(CFG_STATS_NODE, &cfg_stats_reporter_enable_cmd);
 	install_element(CFG_STATS_NODE, &cfg_stats_reporter_disable_cmd);
+	install_element(CFG_STATS_NODE, &cfg_stats_reporter_flush_period_cmd);
 
 	install_element_ve(&show_stats_asciidoc_table_cmd);
 	install_element_ve(&show_rate_counters_cmd);

-- 
To view, visit https://gerrit.osmocom.org/c/libosmocore/+/18138
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: I04f1e7bdf0d6f20e4f15571e94191de61c47ddad
Gerrit-Change-Number: 18138
Gerrit-PatchSet: 3
Gerrit-Owner: ipse <Alexander.Chemeris at gmail.com>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: ipse <Alexander.Chemeris at gmail.com>
Gerrit-Reviewer: laforge <laforge at osmocom.org>
Gerrit-Reviewer: pespin <pespin at sysmocom.de>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20200509/71b105d8/attachment.htm>


More information about the gerrit-log mailing list