Blame SOURCES/bz1791792-2-stats-Add-stats-for-scheduler-misses.patch

354091
From 48b6894ef41e9a06ccbb696d062d86ef60dc2c4b Mon Sep 17 00:00:00 2001
354091
From: Christine Caulfield <ccaulfie@redhat.com>
354091
Date: Fri, 17 Jan 2020 14:22:16 +0000
354091
Subject: [PATCH] stats: Add stats for scheduler misses
354091
354091
This patch add a stats.schedmiss.* set of entries that
354091
are a record of the last 10 times corosync was not scheduled
354091
in time.
354091
354091
These entries are keypt in reverse order (so stats.schedmiss.0.* is
354091
always the latest one kept) and the values, including the timestamp,
354091
are in milliseconds.
354091
354091
It's also possible to use a cmap tracker to follow these events, which
354091
might be useful.
354091
354091
Signed-off-by: Christine Caulfield <ccaulfie@redhat.com>
354091
Reviewed-by: Jan Friesse <jfriesse@redhat.com>
354091
---
354091
 exec/main.c              |   2 +
354091
 exec/stats.c             | 113 +++++++++++++++++++++++++++++++++++++++++++----
354091
 exec/stats.h             |   2 +
354091
 man/cmap_keys.7          |  26 ++++++++++-
354091
 tools/corosync-cmapctl.c |   5 ++-
354091
 5 files changed, 136 insertions(+), 12 deletions(-)
354091
354091
diff --git a/exec/main.c b/exec/main.c
354091
index 7a471a1..fb0486e 100644
354091
--- a/exec/main.c
354091
+++ b/exec/main.c
354091
@@ -835,6 +835,8 @@ static void timer_function_scheduler_timeout (void *data)
354091
 		log_printf (LOGSYS_LEVEL_WARNING, "Corosync main process was not scheduled for %0.4f ms "
354091
 		    "(threshold is %0.4f ms). Consider token timeout increase.",
354091
 		    (float)tv_diff / QB_TIME_NS_IN_MSEC, (float)timeout_data->max_tv_diff / QB_TIME_NS_IN_MSEC);
354091
+
354091
+		stats_add_schedmiss_event(tv_current / 1000, (float)tv_diff / QB_TIME_NS_IN_MSEC);
354091
 	}
354091
 
354091
 	/*
354091
diff --git a/exec/stats.c b/exec/stats.c
354091
index e89504e..d5c1cbc 100644
354091
--- a/exec/stats.c
354091
+++ b/exec/stats.c
354091
@@ -1,5 +1,5 @@
354091
 /*
354091
- * Copyright (c) 2017 Red Hat, Inc.
354091
+ * Copyright (c) 2017-2020 Red Hat, Inc.
354091
  *
354091
  * All rights reserved.
354091
  *
354091
@@ -60,9 +60,20 @@ LOGSYS_DECLARE_SUBSYS ("STATS");
354091
 
354091
 static qb_map_t *stats_map;
354091
 
354091
+/* Structure of an element in the schedmiss array */
354091
+struct schedmiss_entry {
354091
+	uint64_t timestamp;
354091
+	float delay;
354091
+};
354091
+#define MAX_SCHEDMISS_EVENTS 10
354091
+static struct schedmiss_entry schedmiss_event[MAX_SCHEDMISS_EVENTS];
354091
+static unsigned int highest_schedmiss_event;
354091
+
354091
+#define SCHEDMISS_PREFIX "stats.schedmiss"
354091
+
354091
 /* Convert iterator number to text and a stats pointer */
354091
 struct cs_stats_conv {
354091
-	enum {STAT_PG, STAT_SRP, STAT_KNET, STAT_KNET_HANDLE, STAT_IPCSC, STAT_IPCSG} type;
354091
+	enum {STAT_PG, STAT_SRP, STAT_KNET, STAT_KNET_HANDLE, STAT_IPCSC, STAT_IPCSG, STAT_SCHEDMISS} type;
354091
 	const char *name;
354091
 	const size_t offset;
354091
 	const icmap_value_types_t value_type;
354091
@@ -190,6 +201,10 @@ struct cs_stats_conv cs_ipcs_global_stats[] = {
354091
 	{ STAT_IPCSG, "global.active",        offsetof(struct ipcs_global_stats, active),           ICMAP_VALUETYPE_UINT64},
354091
 	{ STAT_IPCSG, "global.closed",        offsetof(struct ipcs_global_stats, closed),           ICMAP_VALUETYPE_UINT64},
354091
 };
354091
+struct cs_stats_conv cs_schedmiss_stats[] = {
354091
+	{ STAT_SCHEDMISS, "timestamp",    offsetof(struct schedmiss_entry, timestamp), ICMAP_VALUETYPE_UINT64},
354091
+	{ STAT_SCHEDMISS, "delay",        offsetof(struct schedmiss_entry, delay),     ICMAP_VALUETYPE_FLOAT},
354091
+};
354091
 
354091
 #define NUM_PG_STATS (sizeof(cs_pg_stats) / sizeof(struct cs_stats_conv))
354091
 #define NUM_SRP_STATS (sizeof(cs_srp_stats) / sizeof(struct cs_stats_conv))
354091
@@ -286,7 +301,7 @@ cs_error_t stats_map_init(const struct corosync_api_v1 *corosync_api)
354091
 		stats_add_entry(param, &cs_ipcs_global_stats[i]);
354091
 	}
354091
 
354091
-	/* KNET and IPCS stats are added when appropriate */
354091
+	/* KNET, IPCS & SCHEDMISS stats are added when appropriate */
354091
 	return CS_OK;
354091
 }
354091
 
354091
@@ -307,6 +322,8 @@ cs_error_t stats_map_get(const char *key_name,
354091
 	int link_no;
354091
 	int service_id;
354091
 	uint32_t pid;
354091
+	unsigned int sm_event;
354091
+	char *sm_type;
354091
 	void *conn_ptr;
354091
 
354091
 	item = qb_map_get(stats_map, key_name);
354091
@@ -363,17 +380,85 @@ cs_error_t stats_map_get(const char *key_name,
354091
 			cs_ipcs_get_global_stats(&ipcs_global_stats);
354091
 			stats_map_set_value(statinfo, &ipcs_global_stats, value, value_len, type);
354091
 			break;
354091
+		case STAT_SCHEDMISS:
354091
+			if (sscanf(key_name, SCHEDMISS_PREFIX ".%d", &sm_event) != 1) {
354091
+				return CS_ERR_NOT_EXIST;
354091
+			}
354091
+
354091
+			sm_type = strrchr(key_name, '.');
354091
+			if (sm_type == NULL) {
354091
+				return CS_ERR_NOT_EXIST;
354091
+			}
354091
+			sm_type++;
354091
+
354091
+			if (strcmp(sm_type, "timestamp") == 0) {
354091
+				memcpy(value, &schedmiss_event[sm_event].timestamp, sizeof(uint64_t));
354091
+				*value_len = sizeof(uint64_t);
354091
+				*type = ICMAP_VALUETYPE_UINT64;
354091
+			}
354091
+			if (strcmp(sm_type, "delay") == 0) {
354091
+				memcpy(value, &schedmiss_event[sm_event].delay, sizeof(float));
354091
+				*value_len = sizeof(float);
354091
+				*type = ICMAP_VALUETYPE_FLOAT;
354091
+			}
354091
+			break;
354091
 		default:
354091
 			return CS_ERR_LIBRARY;
354091
 	}
354091
 	return CS_OK;
354091
 }
354091
 
354091
-#define STATS_CLEAR       "stats.clear."
354091
-#define STATS_CLEAR_KNET  "stats.clear.knet"
354091
-#define STATS_CLEAR_IPC   "stats.clear.ipc"
354091
-#define STATS_CLEAR_TOTEM "stats.clear.totem"
354091
-#define STATS_CLEAR_ALL   "stats.clear.all"
354091
+static void schedmiss_clear_stats(void)
354091
+{
354091
+	int i;
354091
+	char param[ICMAP_KEYNAME_MAXLEN];
354091
+
354091
+	for (i=0; i
354091
+		if (i < highest_schedmiss_event) {
354091
+			sprintf(param, SCHEDMISS_PREFIX ".%i.timestamp", i);
354091
+			stats_rm_entry(param);
354091
+			sprintf(param, SCHEDMISS_PREFIX ".%i.delay", i);
354091
+			stats_rm_entry(param);
354091
+		}
354091
+		schedmiss_event[i].timestamp = (uint64_t)0LL;
354091
+		schedmiss_event[i].delay = 0.0f;
354091
+	}
354091
+	highest_schedmiss_event = 0;
354091
+}
354091
+
354091
+/* Called from main.c */
354091
+void stats_add_schedmiss_event(uint64_t timestamp, float delay)
354091
+{
354091
+	char param[ICMAP_KEYNAME_MAXLEN];
354091
+	int i;
354091
+
354091
+	/* Move 'em all up */
354091
+	for (i=MAX_SCHEDMISS_EVENTS-2; i>=0; i--) {
354091
+		schedmiss_event[i+1].timestamp = schedmiss_event[i].timestamp;
354091
+		schedmiss_event[i+1].delay = schedmiss_event[i].delay;
354091
+	}
354091
+
354091
+	/* New entries are always at the front */
354091
+	schedmiss_event[0].timestamp = timestamp;
354091
+	schedmiss_event[0].delay = delay;
354091
+
354091
+	/* If we've not run off the end then add an entry in the trie for the new 'end' one */
354091
+	if (highest_schedmiss_event < MAX_SCHEDMISS_EVENTS) {
354091
+		sprintf(param, SCHEDMISS_PREFIX ".%i.timestamp", highest_schedmiss_event);
354091
+		stats_add_entry(param, &cs_schedmiss_stats[0]);
354091
+		sprintf(param, SCHEDMISS_PREFIX ".%i.delay", highest_schedmiss_event);
354091
+		stats_add_entry(param, &cs_schedmiss_stats[1]);
354091
+		highest_schedmiss_event++;
354091
+	}
354091
+	/* Notifications get sent by the stats_updater */
354091
+}
354091
+
354091
+#define STATS_CLEAR           "stats.clear."
354091
+#define STATS_CLEAR_KNET      "stats.clear.knet"
354091
+#define STATS_CLEAR_IPC       "stats.clear.ipc"
354091
+#define STATS_CLEAR_TOTEM     "stats.clear.totem"
354091
+#define STATS_CLEAR_ALL       "stats.clear.all"
354091
+#define STATS_CLEAR_SCHEDMISS "stats.clear.schedmiss"
354091
 
354091
 cs_error_t stats_map_set(const char *key_name,
354091
 			 const void *value,
354091
@@ -394,9 +479,14 @@ cs_error_t stats_map_set(const char *key_name,
354091
 		totempg_stats_clear(TOTEMPG_STATS_CLEAR_TOTEM);
354091
 		cleared = 1;
354091
 	}
354091
+	if (strncmp(key_name, STATS_CLEAR_SCHEDMISS, strlen(STATS_CLEAR_SCHEDMISS)) == 0) {
354091
+		schedmiss_clear_stats();
354091
+		cleared = 1;
354091
+	}
354091
 	if (strncmp(key_name, STATS_CLEAR_ALL, strlen(STATS_CLEAR_ALL)) == 0) {
354091
 		totempg_stats_clear(TOTEMPG_STATS_CLEAR_TRANSPORT | TOTEMPG_STATS_CLEAR_TOTEM);
354091
 		cs_ipcs_clear_stats();
354091
+		schedmiss_clear_stats();
354091
 		cleared = 1;
354091
 	}
354091
 	if (!cleared) {
354091
@@ -500,6 +590,11 @@ static void stats_map_notify_fn(uint32_t event, char *key, void *old_value, void
354091
 		return ;
354091
 	}
354091
 
354091
+	/* Ignore schedmiss trackers as the values are read from the circular buffer */
354091
+	if (strncmp(key, SCHEDMISS_PREFIX, strlen(SCHEDMISS_PREFIX)) == 0 ) {
354091
+		return ;
354091
+	}
354091
+
354091
 	new_val.data = new_value;
354091
 	if (stats_map_get(key,
354091
 			  &new_value,
354091
@@ -556,7 +651,7 @@ cs_error_t stats_map_track_add(const char *key_name,
354091
 		}
354091
 		/* Get initial value */
354091
 		if (stats_map_get(tracker->key_name,
354091
-				  &tracker->old_value, &value_len, &type) == CS_OK) {
354091
+				  &tracker->old_value, &value_len, &type) != CS_OK) {
354091
 			tracker->old_value = 0ULL;
354091
 		}
354091
 	} else {
354091
diff --git a/exec/stats.h b/exec/stats.h
354091
index 45891ae..eac9e7c 100644
354091
--- a/exec/stats.h
354091
+++ b/exec/stats.h
354091
@@ -69,3 +69,5 @@ void stats_trigger_trackers(void);
354091
 void stats_ipcs_add_connection(int service_id, uint32_t pid, void *ptr);
354091
 void stats_ipcs_del_connection(int service_id, uint32_t pid, void *ptr);
354091
 cs_error_t cs_ipcs_get_conn_stats(int service_id, uint32_t pid, void *conn_ptr, struct ipcs_conn_stats *ipcs_stats);
354091
+
354091
+void stats_add_schedmiss_event(uint64_t, float delay);
354091
diff --git a/man/cmap_keys.7 b/man/cmap_keys.7
354091
index 6bc04fe..da95c51 100644
354091
--- a/man/cmap_keys.7
354091
+++ b/man/cmap_keys.7
354091
@@ -1,5 +1,5 @@
354091
 .\"/*
354091
-.\" * Copyright (c) 2012-2018 Red Hat, Inc.
354091
+.\" * Copyright (c) 2012-2020 Red Hat, Inc.
354091
 .\" *
354091
 .\" * All rights reserved.
354091
 .\" *
354091
@@ -357,6 +357,27 @@ contains the total number of interrupted sends.
354091
 .B service_id
354091
 contains the ID of service which the IPC is connected to.
354091
 
354091
+
354091
+.TP
354091
+stats.schedmiss.<n>.*
354091
+If corosync is not scheduled after the required period of time it will
354091
+log this event and also write an entry to the stats cmap under this key.
354091
+There can be up to 10 entries (0..9) in here, when an 11th event happens
354091
+the earliest will be removed.
354091
+
354091
+These events will always be in reverse order, so stats.schedmiss.0.* will
354091
+always be the latest event kept and 9 the oldest. If you want to listen
354091
+for notifications then you are recommended to listen for changes
354091
+to stats.schedmiss.0.timestamp or stats.schedmiss.0.delay.
354091
+
354091
+.B timestamp
354091
+The time of the event in ms since the Epoch (ie time_t * 1000 but with
354091
+valid milliseconds).
354091
+
354091
+.B delay
354091
+The time that corosync was paused (in ms, float value).
354091
+
354091
+
354091
 .TP
354091
 stats.clear.*
354091
 These are write-only keys used to clear the stats for various subsystems
354091
@@ -370,6 +391,9 @@ Clears the knet stats
354091
 .B ipc
354091
 Clears the ipc stats
354091
 
354091
+.B schedmiss
354091
+Clears the schedmiss stats
354091
+
354091
 .B all
354091
 Clears all of the above stats
354091
 
354091
diff --git a/tools/corosync-cmapctl.c b/tools/corosync-cmapctl.c
354091
index a4b61bd..ffca7e1 100644
354091
--- a/tools/corosync-cmapctl.c
354091
+++ b/tools/corosync-cmapctl.c
354091
@@ -115,7 +115,7 @@ static int print_help(void)
354091
 	printf("    about the networking and IPC traffic in some detail.\n");
354091
 	printf("\n");
354091
 	printf("Clear stats:\n");
354091
-	printf("    corosync-cmapctl -C [knet|ipc|totem|all]\n");
354091
+	printf("    corosync-cmapctl -C [knet|ipc|totem|schedmiss|all]\n");
354091
 	printf("    The 'stats' map is implied\n");
354091
 	printf("\n");
354091
 	printf("Load settings from a file:\n");
354091
@@ -849,6 +849,7 @@ int main(int argc, char *argv[])
354091
 			if (strcmp(optarg, "knet") == 0 ||
354091
 			    strcmp(optarg, "totem") == 0 ||
354091
 			    strcmp(optarg, "ipc") == 0 ||
354091
+			    strcmp(optarg, "schedmiss") == 0 ||
354091
 			    strcmp(optarg, "all") == 0) {
354091
 				action = ACTION_CLEARSTATS;
354091
 				clear_opt = optarg;
354091
@@ -857,7 +858,7 @@ int main(int argc, char *argv[])
354091
 				map = CMAP_MAP_STATS;
354091
 			}
354091
 			else {
354091
-				fprintf(stderr, "argument to -C should be 'knet', 'totem', 'ipc' or 'all'\n");
354091
+				fprintf(stderr, "argument to -C should be 'knet', 'totem', 'ipc', 'schedmiss' or 'all'\n");
354091
 				return (EXIT_FAILURE);
354091
 			}
354091
 			break;
354091
-- 
354091
1.8.3.1
354091