[dpdk-dev] [PATCH v2 3/3] keepalive: add rte_keepalive_xstats_get() and example

Harry van Haaren harry.van.haaren at intel.com
Thu Jan 21 12:05:27 CET 2016


This patch adds a function that exposes keepalive statistics
using a rte_keepalive_xstats struct. The function provides
the client API the opportunity to read last-seen and status of
each core.

Signed-off-by: Harry van Haaren <harry.van.haaren at intel.com>
---
 doc/guides/rel_notes/release_2_3.rst            |  6 ++++
 doc/guides/sample_app_ug/keep_alive.rst         | 11 ++++++
 examples/l2fwd-keepalive/main.c                 | 23 ++++++++++--
 lib/librte_eal/bsdapp/eal/rte_eal_version.map   |  7 ++++
 lib/librte_eal/common/include/rte_keepalive.h   | 28 ++++++++++++++-
 lib/librte_eal/common/rte_keepalive.c           | 47 ++++++++++++++++++++++++-
 lib/librte_eal/linuxapp/eal/rte_eal_version.map |  7 ++++
 7 files changed, 124 insertions(+), 5 deletions(-)

diff --git a/doc/guides/rel_notes/release_2_3.rst b/doc/guides/rel_notes/release_2_3.rst
index 99de186..c06e28c 100644
--- a/doc/guides/rel_notes/release_2_3.rst
+++ b/doc/guides/rel_notes/release_2_3.rst
@@ -4,6 +4,12 @@ DPDK Release 2.3
 New Features
 ------------
 
+* **Keep Alive xstats**
+
+  A function ``rte_keepalive_xstats_get()`` has been added to the
+  keepalive header, allowing the retrieval of keepalive statistics
+  such as last-alive-time and the status of each core registered
+  for monitoring. The API reflects that of the existing xstats API.
 
 Resolved Issues
 ---------------
diff --git a/doc/guides/sample_app_ug/keep_alive.rst b/doc/guides/sample_app_ug/keep_alive.rst
index 1478faf..7c2d2a4 100644
--- a/doc/guides/sample_app_ug/keep_alive.rst
+++ b/doc/guides/sample_app_ug/keep_alive.rst
@@ -190,3 +190,14 @@ The rte_keepalive_mark_alive function simply sets the core state to alive.
     {
         keepcfg->state_flags[rte_lcore_id()] = ALIVE;
     }
+
+Keepalive exposes its statistics using an API very similar to the xstats API.
+This allows client code to call the function and retrieve the current status
+of keepalive, providing information like last-alive time and status per-core.
+
+.. code-block:: c
+
+    nstats = rte_keepalive_xstats_get(rte_global_keepalive_info, xstats,
+                                      nstats);
+    for (i = 0; i < nstats; i++)
+        printf("%s : %lu\n", xstats[i].name, xstats[i].value);
diff --git a/examples/l2fwd-keepalive/main.c b/examples/l2fwd-keepalive/main.c
index f4d52f2..34c6d62 100644
--- a/examples/l2fwd-keepalive/main.c
+++ b/examples/l2fwd-keepalive/main.c
@@ -1,7 +1,7 @@
 /*-
  *   BSD LICENSE
  *
- *   Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
+ *   Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
  *   All rights reserved.
  *
  *   Redistribution and use in source and binary forms, with or without
@@ -66,6 +66,7 @@
 #include <rte_ether.h>
 #include <rte_ethdev.h>
 #include <rte_ring.h>
+#include <rte_malloc.h>
 #include <rte_mempool.h>
 #include <rte_mbuf.h>
 #include <rte_timer.h>
@@ -139,7 +140,7 @@ struct l2fwd_port_statistics port_statistics[RTE_MAX_ETHPORTS];
 /* A tsc-based timer responsible for triggering statistics printout */
 #define TIMER_MILLISECOND 1
 #define MAX_TIMER_PERIOD 86400 /* 1 day max */
-static int64_t timer_period = 10 * TIMER_MILLISECOND * 1000; /* 10 seconds */
+static int64_t timer_period = 1 * TIMER_MILLISECOND * 1000; /* 1 second */
 static int64_t check_period = 5; /* default check cycle is 5ms */
 
 /* Keepalive structure */
@@ -189,7 +190,23 @@ print_stats(__attribute__((unused)) struct rte_timer *ptr_timer,
 		   total_packets_tx,
 		   total_packets_rx,
 		   total_packets_dropped);
-	printf("\n====================================================\n");
+	printf("\nKeep Alive xstats ==================================\n");
+
+	/* Keepalive Xstats */
+	unsigned nstats = rte_keepalive_xstats_get(rte_global_keepalive_info,
+						   0, 0);
+	struct rte_keepalive_xstats *xstats =
+		rte_zmalloc("RTE_KEEPALIVE_XSTATS",
+			    sizeof(struct rte_keepalive_xstats) * nstats,
+			    RTE_CACHE_LINE_SIZE);
+
+	nstats = rte_keepalive_xstats_get(rte_global_keepalive_info, xstats,
+				      nstats);
+	unsigned i;
+	for (i = 0; i < nstats; i++)
+		printf("%s\t%lu\n", xstats[i].name, xstats[i].value);
+	printf("====================================================\n");
+	rte_free(xstats);
 }
 
 /* Send the burst of packets on an output interface */
diff --git a/lib/librte_eal/bsdapp/eal/rte_eal_version.map b/lib/librte_eal/bsdapp/eal/rte_eal_version.map
index 9d7adf1..ff1cbc7 100644
--- a/lib/librte_eal/bsdapp/eal/rte_eal_version.map
+++ b/lib/librte_eal/bsdapp/eal/rte_eal_version.map
@@ -135,3 +135,10 @@ DPDK_2.2 {
 	rte_xen_dom0_supported;
 
 } DPDK_2.1;
+
+DPDK_2.3 {
+	global:
+
+	rte_keepalive_xstats_get;
+
+} DPDK_2.2;
diff --git a/lib/librte_eal/common/include/rte_keepalive.h b/lib/librte_eal/common/include/rte_keepalive.h
index 02472c0..f98b92c 100644
--- a/lib/librte_eal/common/include/rte_keepalive.h
+++ b/lib/librte_eal/common/include/rte_keepalive.h
@@ -1,7 +1,7 @@
 /*-
  *   BSD LICENSE
  *
- *   Copyright 2015 Intel Shannon Ltd. All rights reserved.
+ *   Copyright 2015-2016 Intel Shannon Ltd. All rights reserved.
  *
  *   Redistribution and use in source and binary forms, with or without
  *   modification, are permitted provided that the following conditions
@@ -48,6 +48,19 @@
 #define RTE_KEEPALIVE_MAXCORES RTE_MAX_LCORE
 #endif
 
+#define RTE_KEEPALIVE_XSTATS_NAME_LEN 64
+
+/**
+ * Keepalive extended statistic structure
+ *
+ * This structure is used by rte_keepalive_xstats_get() to provide
+ * statistics that are not provided in the generic rte_eth_stats
+ * structure.
+ */
+struct rte_keepalive_xstats {
+	char name[RTE_KEEPALIVE_XSTATS_NAME_LEN];
+	uint64_t value;
+};
 
 /**
  * Keepalive failure callback.
@@ -127,6 +140,19 @@ void rte_keepalive_dispatch_pings(void *ptr_timer, void *ptr_data);
 void rte_keepalive_register_core(struct rte_keepalive *keepcfg,
 	const int id_core);
 
+/**
+ * Get statistics of the keepalive state. If xstats NULL or n is zero, the
+ * function returns the number of xstats available. If xstats is a pointer to
+ * array of size n, n items will be filled in, and then returned.
+ * @param *keepcfg
+ *   Keepalive structure pointer
+ * @param *xstats
+ *   An array of rte_eth_xstats, or NULL.
+ * @param n
+ *   Size of the array of xstats being passed in
+ */
+int rte_keepalive_xstats_get(struct rte_keepalive *keepcfg,
+			     struct rte_keepalive_xstats *xstats, unsigned n);
 
 /**
  * Per-core keepalive check.
diff --git a/lib/librte_eal/common/rte_keepalive.c b/lib/librte_eal/common/rte_keepalive.c
index 5358322..ec0f986 100644
--- a/lib/librte_eal/common/rte_keepalive.c
+++ b/lib/librte_eal/common/rte_keepalive.c
@@ -1,7 +1,7 @@
 /*-
  *   BSD LICENSE
  *
- *   Copyright 2015 Intel Shannon Ltd. All rights reserved.
+ *   Copyright 2015-2016 Intel Shannon Ltd. All rights reserved.
  *
  *   Redistribution and use in source and binary forms, with or without
  *   modification, are permitted provided that the following conditions
@@ -40,6 +40,8 @@
 #include <rte_malloc.h>
 #include <rte_cycles.h>
 
+#define RTE_KEEPALIVE_NSTATS 2
+
 static void
 print_trace(const char *msg, struct rte_keepalive *keepcfg, int idx_core)
 {
@@ -114,3 +116,46 @@ rte_keepalive_register_core(struct rte_keepalive *keepcfg, const int id_core)
 		keepcfg->last_alive[id_core] = rte_rdtsc();
 	}
 }
+
+int
+rte_keepalive_xstats_get(struct rte_keepalive *ka,
+			 struct rte_keepalive_xstats *xstats, unsigned n)
+{
+	unsigned i, c, active = 0;
+	for (i = 0; i < RTE_KEEPALIVE_MAXCORES; i++) {
+		if (ka->active_cores[i])
+			active++;
+	}
+
+	const unsigned nstats = active * RTE_KEEPALIVE_NSTATS;
+
+	/* Indicate number of ka-xstats */
+	if (n < nstats)
+		return nstats;
+
+	if (xstats == NULL)
+		return nstats;
+
+	uint64_t tsc = rte_rdtsc();
+	i = 0;
+	for (c = 0; c < RTE_KEEPALIVE_MAXCORES; c++) {
+		if (ka->active_cores[c]) {
+			snprintf(xstats[i].name,
+				 RTE_KEEPALIVE_XSTATS_NAME_LEN,
+				 "%s%u%s", "keepalive_core",
+				 c, "_last_time");
+			xstats[i].value = ((tsc - ka->last_alive[c])*1000) /
+				rte_get_tsc_hz();
+			i++;
+
+			snprintf(xstats[i].name,
+				 RTE_KEEPALIVE_XSTATS_NAME_LEN,
+				 "%s%u%s\t", "keepalive_core",
+				 c, "_status");
+			xstats[i].value = (uint32_t)ka->state_flags[c];
+			i++;
+		}
+	}
+
+	return nstats;
+}
diff --git a/lib/librte_eal/linuxapp/eal/rte_eal_version.map b/lib/librte_eal/linuxapp/eal/rte_eal_version.map
index cbe175f..088fc18 100644
--- a/lib/librte_eal/linuxapp/eal/rte_eal_version.map
+++ b/lib/librte_eal/linuxapp/eal/rte_eal_version.map
@@ -138,3 +138,10 @@ DPDK_2.2 {
 	rte_xen_dom0_supported;
 
 } DPDK_2.1;
+
+DPDK_2.3 {
+	global:
+
+	rte_keepalive_xstats_get;
+
+} DPDK_2.2;
-- 
2.5.0



More information about the dev mailing list