[03/12] telemetry: invert dependency on metrics
diff mbox series

Message ID 20200319171907.60891-4-ciara.power@intel.com
State Superseded, archived
Delegated to: David Marchand
Headers show
Series
  • update and simplify telemetry library.
Related show

Checks

Context Check Description
ci/Intel-compilation fail Compilation issues
ci/checkpatch success coding style OK

Commit Message

Ciara Power March 19, 2020, 5:18 p.m. UTC
From: Bruce Richardson <bruce.richardson@intel.com>

Rather than having the telemetry library depend on the metrics
lib we invert the dependency so that metrics instead depends
on telemetry lib, and registers the needed functions with it
at init time. This prepares the way for a cleaner telemetry
architecture to be applied in later patches.

Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
---
 lib/Makefile                                  |  7 +++--
 lib/librte_metrics/Makefile                   |  1 +
 lib/librte_metrics/meson.build                |  2 +-
 lib/librte_metrics/rte_metrics.c              |  5 ++++
 lib/librte_metrics/rte_metrics.h              |  2 ++
 lib/librte_metrics/rte_metrics_telemetry.c    | 14 +++++++++
 lib/librte_telemetry/Makefile                 |  3 +-
 lib/librte_telemetry/meson.build              |  2 +-
 lib/librte_telemetry/rte_telemetry.c          | 19 ++++++++----
 lib/librte_telemetry/rte_telemetry_internal.h | 30 +++++++++++++++++++
 lib/librte_telemetry/rte_telemetry_parser.c   |  5 ++--
 .../rte_telemetry_version.map                 |  1 +
 lib/meson.build                               |  3 +-
 mk/rte.app.mk                                 |  6 +---
 14 files changed, 80 insertions(+), 20 deletions(-)

Patch
diff mbox series

diff --git a/lib/Makefile b/lib/Makefile
index 07b1ec0b6..31b943817 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -22,6 +22,8 @@  DIRS-$(CONFIG_RTE_LIBRTE_CFGFILE) += librte_cfgfile
 DEPDIRS-librte_cfgfile := librte_eal
 DIRS-$(CONFIG_RTE_LIBRTE_CMDLINE) += librte_cmdline
 DEPDIRS-librte_cmdline := librte_eal librte_net
+DIRS-$(CONFIG_RTE_LIBRTE_TELEMETRY) += librte_telemetry
+DEPDIRS-librte_telemetry := librte_eal
 DIRS-$(CONFIG_RTE_LIBRTE_ETHER) += librte_ethdev
 DEPDIRS-librte_ethdev := librte_net librte_eal librte_mempool librte_ring
 DEPDIRS-librte_ethdev += librte_mbuf
@@ -72,6 +74,9 @@  DIRS-$(CONFIG_RTE_LIBRTE_JOBSTATS) += librte_jobstats
 DEPDIRS-librte_jobstats := librte_eal
 DIRS-$(CONFIG_RTE_LIBRTE_METRICS) += librte_metrics
 DEPDIRS-librte_metrics := librte_eal librte_ethdev
+ifeq ($(CONFIG_RTE_LIBRTE_TELEMETRY),y)
+DEPDIRS-librte_metrics += librte_telemetry
+endif
 DIRS-$(CONFIG_RTE_LIBRTE_BITRATE) += librte_bitratestats
 DEPDIRS-librte_bitratestats := librte_eal librte_metrics librte_ethdev
 DIRS-$(CONFIG_RTE_LIBRTE_LATENCY_STATS) += librte_latencystats
@@ -114,8 +119,6 @@  DEPDIRS-librte_bpf := librte_eal librte_mempool librte_mbuf librte_ethdev
 DIRS-$(CONFIG_RTE_LIBRTE_IPSEC) += librte_ipsec
 DEPDIRS-librte_ipsec := librte_eal librte_mbuf librte_cryptodev librte_security \
 			librte_net
-DIRS-$(CONFIG_RTE_LIBRTE_TELEMETRY) += librte_telemetry
-DEPDIRS-librte_telemetry := librte_eal librte_metrics librte_ethdev
 DIRS-$(CONFIG_RTE_LIBRTE_RCU) += librte_rcu
 DEPDIRS-librte_rcu := librte_eal
 
diff --git a/lib/librte_metrics/Makefile b/lib/librte_metrics/Makefile
index 455569948..fa762ba37 100644
--- a/lib/librte_metrics/Makefile
+++ b/lib/librte_metrics/Makefile
@@ -25,6 +25,7 @@  LDLIBS += -lrte_ethdev
 LDLIBS += $(JANSSON_LINK)
 
 CFLAGS += $(JANSSON_CFLAGS)
+CFLAGS += -I$(RTE_SDK)/lib/librte_telemetry/
 endif
 
 # Install header file
diff --git a/lib/librte_metrics/meson.build b/lib/librte_metrics/meson.build
index 0f9f2e0e6..d116857e2 100644
--- a/lib/librte_metrics/meson.build
+++ b/lib/librte_metrics/meson.build
@@ -10,5 +10,5 @@  if jansson.found()
 	ext_deps += jansson
 	sources += files('rte_metrics_telemetry.c')
 	headers = files('rte_metrics_telemetry.h')
-	deps += ['ethdev']
+	deps += ['ethdev', 'telemetry']
 endif
diff --git a/lib/librte_metrics/rte_metrics.c b/lib/librte_metrics/rte_metrics.c
index 9b38d7787..e07670219 100644
--- a/lib/librte_metrics/rte_metrics.c
+++ b/lib/librte_metrics/rte_metrics.c
@@ -13,6 +13,8 @@ 
 #include <rte_memzone.h>
 #include <rte_spinlock.h>
 
+int metrics_initialized;
+
 #define RTE_METRICS_MEMZONE_NAME "RTE_METRICS"
 
 /**
@@ -60,6 +62,8 @@  rte_metrics_init(int socket_id)
 	struct rte_metrics_data_s *stats;
 	const struct rte_memzone *memzone;
 
+	if (metrics_initialized)
+		return;
 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
 		return;
 
@@ -73,6 +77,7 @@  rte_metrics_init(int socket_id)
 	stats = memzone->addr;
 	memset(stats, 0, sizeof(struct rte_metrics_data_s));
 	rte_spinlock_init(&stats->lock);
+	metrics_initialized = 1;
 }
 
 int
diff --git a/lib/librte_metrics/rte_metrics.h b/lib/librte_metrics/rte_metrics.h
index 466ca98c3..fbe64ddf2 100644
--- a/lib/librte_metrics/rte_metrics.h
+++ b/lib/librte_metrics/rte_metrics.h
@@ -30,6 +30,8 @@ 
 extern "C" {
 #endif
 
+extern int metrics_initialized;
+
 /** Maximum length of metric name (including null-terminator) */
 #define RTE_METRICS_MAX_NAME_LEN 64
 #define RTE_METRICS_MAX_METRICS 256
diff --git a/lib/librte_metrics/rte_metrics_telemetry.c b/lib/librte_metrics/rte_metrics_telemetry.c
index 78c21663d..31d505551 100644
--- a/lib/librte_metrics/rte_metrics_telemetry.c
+++ b/lib/librte_metrics/rte_metrics_telemetry.c
@@ -6,6 +6,9 @@ 
 
 #include <rte_ethdev.h>
 #include <rte_string_fns.h>
+#ifdef RTE_LIBRTE_TELEMETRY
+#include <rte_telemetry_internal.h>
+#endif
 
 #include "rte_metrics.h"
 #include "rte_metrics_telemetry.h"
@@ -74,6 +77,7 @@  rte_metrics_tel_reg_all_ethdev(int *metrics_register_done, int *reg_index_list)
 	int ret, nb_drv_idx = 0;
 	uint16_t d;
 
+	rte_metrics_init(rte_socket_id());
 	RTE_ETH_FOREACH_DEV(d) {
 		int i;
 		/* Different device types have different numbers of stats, so
@@ -415,6 +419,16 @@  rte_metrics_tel_extract_data(struct telemetry_encode_param *ep, json_t *data)
 
 RTE_INIT(metrics_ctor)
 {
+#ifdef RTE_LIBRTE_TELEMETRY
+	static const struct metrics_functions fns = {
+		.reg_all_ethdev = rte_metrics_tel_reg_all_ethdev,
+		.encode_json_format = rte_metrics_tel_encode_json_format,
+		.get_port_stats_ids = rte_metrics_tel_get_port_stats_ids,
+		.get_ports_stats_json = rte_metrics_tel_get_ports_stats_json,
+		.extract_data = rte_metrics_tel_extract_data
+	};
+	rte_telemetry_set_metrics_fns(&fns); /* assign them to telemetry lib */
+#endif
 	metrics_log_level = rte_log_register("lib.metrics");
 	if (metrics_log_level >= 0)
 		rte_log_set_level(metrics_log_level, RTE_LOG_ERR);
diff --git a/lib/librte_telemetry/Makefile b/lib/librte_telemetry/Makefile
index f36454855..17e651c41 100644
--- a/lib/librte_telemetry/Makefile
+++ b/lib/librte_telemetry/Makefile
@@ -9,8 +9,9 @@  LIB = librte_telemetry.a
 CFLAGS += -O3
 CFLAGS += $(WERROR_FLAGS) -I$(SRCDIR)
 CFLAGS += -DALLOW_EXPERIMENTAL_API
+CFLAGS += -I$(RTE_SDK)/lib/librte_metrics/
 
-LDLIBS += -lrte_eal -lrte_ethdev
+LDLIBS += -lrte_eal
 LDLIBS += -lrte_metrics
 LDLIBS += -lpthread
 LDLIBS += -ljansson
diff --git a/lib/librte_telemetry/meson.build b/lib/librte_telemetry/meson.build
index 26a331140..b34661ac1 100644
--- a/lib/librte_telemetry/meson.build
+++ b/lib/librte_telemetry/meson.build
@@ -3,8 +3,8 @@ 
 
 sources = files('rte_telemetry.c', 'rte_telemetry_parser.c', 'rte_telemetry_parser_test.c')
 headers = files('rte_telemetry.h', 'rte_telemetry_internal.h', 'rte_telemetry_parser.h')
-deps += ['metrics', 'ethdev']
 cflags += '-DALLOW_EXPERIMENTAL_API'
+includes += include_directories('../librte_metrics')
 
 jansson = dependency('jansson', required: false)
 if jansson.found()
diff --git a/lib/librte_telemetry/rte_telemetry.c b/lib/librte_telemetry/rte_telemetry.c
index 2022ce68e..2fb8ffe87 100644
--- a/lib/librte_telemetry/rte_telemetry.c
+++ b/lib/librte_telemetry/rte_telemetry.c
@@ -10,10 +10,10 @@ 
 #include <jansson.h>
 
 #include <rte_eal.h>
-#include <rte_ethdev.h>
 #include <rte_metrics.h>
 #include <rte_option.h>
 #include <rte_string_fns.h>
+#include <rte_lcore.h>
 
 #include "rte_telemetry.h"
 #include "rte_telemetry_internal.h"
@@ -43,6 +43,15 @@  struct json_data {
 	int stat_value;
 };
 
+/* global array of functions pointers to metrics library */
+const struct metrics_functions *metrics_fns;
+
+void
+rte_telemetry_set_metrics_fns(const struct metrics_functions *fns)
+{
+	metrics_fns = fns;
+}
+
 static void
 rte_telemetry_get_runtime_dir(char *socket_path, size_t size)
 {
@@ -145,7 +154,7 @@  rte_telemetry_send_global_stats_values(struct telemetry_encode_param *ep,
 		return -1;
 	}
 
-	ret = rte_metrics_tel_encode_json_format(ep, &json_buffer);
+	ret = metrics_fns->encode_json_format(ep, &json_buffer);
 	if (ret < 0) {
 		TELEMETRY_LOG_ERR("JSON encode function failed");
 		ret = rte_telemetry_send_error_response(telemetry, ret);
@@ -190,7 +199,7 @@  rte_telemetry_send_ports_stats_values(struct telemetry_encode_param *ep,
 		goto einval_fail;
 	}
 
-	ret = rte_metrics_tel_get_ports_stats_json(ep, telemetry->reg_index,
+	ret = metrics_fns->get_ports_stats_json(ep, telemetry->reg_index,
 			&json_buffer);
 	if (ret < 0) {
 		TELEMETRY_LOG_ERR("Function for get_ports_stats_json"
@@ -222,8 +231,7 @@  rte_telemetry_initial_accept(struct telemetry_impl *telemetry)
 	int ret;
 	int selftest = 0;
 
-	ret = rte_metrics_tel_reg_all_ethdev(
-			&telemetry->metrics_register_done,
+	ret = metrics_fns->reg_all_ethdev(&telemetry->metrics_register_done,
 			telemetry->reg_index);
 	if (ret < 0) {
 		TELEMETRY_LOG_ERR("Failed to register ethdev metrics");
@@ -467,7 +475,6 @@  rte_telemetry_init(void)
 	}
 
 	static_telemetry->socket_id = rte_socket_id();
-	rte_metrics_init(static_telemetry->socket_id);
 
 	ret = pthread_attr_init(&attr);
 	if (ret != 0) {
diff --git a/lib/librte_telemetry/rte_telemetry_internal.h b/lib/librte_telemetry/rte_telemetry_internal.h
index ca70e2ed7..2ef72b1ec 100644
--- a/lib/librte_telemetry/rte_telemetry_internal.h
+++ b/lib/librte_telemetry/rte_telemetry_internal.h
@@ -10,6 +10,36 @@ 
 #ifndef _RTE_TELEMETRY_INTERNAL_H_
 #define _RTE_TELEMETRY_INTERNAL_H_
 
+/* function types for the functions coming from metrics library */
+typedef int32_t (*metrics_tel_reg_all_ethdev_t)(int *metrics_register_done,
+		int *reg_index_list);
+
+typedef int32_t (*metrics_tel_encode_json_format_t)(
+		struct telemetry_encode_param *ep, char **json_buffer);
+
+typedef int32_t (*metrics_tel_get_port_stats_ids_t)(
+		struct telemetry_encode_param *ep);
+
+typedef int32_t (*metrics_tel_get_ports_stats_json_t)(
+		struct telemetry_encode_param *ep,
+		int *reg_index, char **json_buffer);
+
+typedef int32_t (*metrics_tel_extract_data_t)(struct telemetry_encode_param *ep,
+		json_t *data);
+
+struct metrics_functions {
+	metrics_tel_reg_all_ethdev_t reg_all_ethdev;
+	metrics_tel_encode_json_format_t encode_json_format;
+	metrics_tel_get_port_stats_ids_t get_port_stats_ids;
+	metrics_tel_get_ports_stats_json_t get_ports_stats_json;
+	metrics_tel_extract_data_t extract_data;
+};
+extern const struct metrics_functions *metrics_fns;
+
+/* API for use by metrics libraries to provide the functions to use */
+__rte_experimental
+void rte_telemetry_set_metrics_fns(const struct metrics_functions *fns);
+
 /* Logging Macros */
 extern int telemetry_log_level;
 
diff --git a/lib/librte_telemetry/rte_telemetry_parser.c b/lib/librte_telemetry/rte_telemetry_parser.c
index 4e236e1e6..7dc84c2cf 100644
--- a/lib/librte_telemetry/rte_telemetry_parser.c
+++ b/lib/librte_telemetry/rte_telemetry_parser.c
@@ -10,7 +10,6 @@ 
 
 #include <rte_metrics.h>
 #include <rte_common.h>
-#include <rte_ethdev.h>
 #include <rte_metrics_telemetry.h>
 
 #include "rte_telemetry_internal.h"
@@ -202,7 +201,7 @@  rte_telemetry_command_ports_all_stat_values(struct telemetry_impl *telemetry,
 		return -1;
 	}
 
-	ret = rte_metrics_tel_get_port_stats_ids(&ep);
+	ret = metrics_fns->get_port_stats_ids(&ep);
 	if (ret < 0) {
 		TELEMETRY_LOG_ERR("Could not get ports stat values");
 		ret = rte_telemetry_send_error_response(telemetry, ret);
@@ -276,7 +275,7 @@  rte_telemetry_command_ports_stats_values_by_name(struct telemetry_impl
 		return -1;
 	}
 
-	ret = rte_metrics_tel_extract_data(&ep, data);
+	ret = metrics_fns->extract_data(&ep, data);
 	if (ret < 0) {
 		TELEMETRY_LOG_ERR("Extracting JSON data failed");
 		ret = rte_telemetry_send_error_response(telemetry, ret);
diff --git a/lib/librte_telemetry/rte_telemetry_version.map b/lib/librte_telemetry/rte_telemetry_version.map
index fa62d7718..a80058c59 100644
--- a/lib/librte_telemetry/rte_telemetry_version.map
+++ b/lib/librte_telemetry/rte_telemetry_version.map
@@ -5,6 +5,7 @@  EXPERIMENTAL {
 	rte_telemetry_init;
 	rte_telemetry_parse;
 	rte_telemetry_selftest;
+	rte_telemetry_set_metrics_fns;
 
 	local: *;
 };
diff --git a/lib/meson.build b/lib/meson.build
index 0af3efab2..77bed670e 100644
--- a/lib/meson.build
+++ b/lib/meson.build
@@ -11,6 +11,7 @@ 
 libraries = [
 	'kvargs', # eal depends on kvargs
 	'eal', # everything depends on eal
+	'telemetry',
 	'ring', 'mempool', 'mbuf', 'net', 'meter', 'ethdev', 'pci', # core
 	'cmdline',
 	'metrics', # bitrate/latency stats depends on this
@@ -30,7 +31,7 @@  libraries = [
 	# add pkt framework libs which use other libs from above
 	'port', 'table', 'pipeline',
 	# flow_classify lib depends on pkt framework table lib
-	'flow_classify', 'bpf', 'telemetry']
+	'flow_classify', 'bpf']
 
 if is_windows
 	libraries = ['kvargs','eal'] # only supported libraries for windows
diff --git a/mk/rte.app.mk b/mk/rte.app.mk
index e68368614..fdaf3ec2c 100644
--- a/mk/rte.app.mk
+++ b/mk/rte.app.mk
@@ -49,11 +49,6 @@  _LDLIBS-$(CONFIG_RTE_LIBRTE_FIB)            += -lrte_fib
 _LDLIBS-$(CONFIG_RTE_LIBRTE_RIB)            += -lrte_rib
 _LDLIBS-$(CONFIG_RTE_LIBRTE_LPM)            += -lrte_lpm
 _LDLIBS-$(CONFIG_RTE_LIBRTE_ACL)            += -lrte_acl
-_LDLIBS-$(CONFIG_RTE_LIBRTE_TELEMETRY)      += --no-as-needed
-_LDLIBS-$(CONFIG_RTE_LIBRTE_TELEMETRY)      += --whole-archive
-_LDLIBS-$(CONFIG_RTE_LIBRTE_TELEMETRY)      += -lrte_telemetry -ljansson
-_LDLIBS-$(CONFIG_RTE_LIBRTE_TELEMETRY)      += --no-whole-archive
-_LDLIBS-$(CONFIG_RTE_LIBRTE_TELEMETRY)      += --as-needed
 _LDLIBS-$(CONFIG_RTE_LIBRTE_JOBSTATS)       += -lrte_jobstats
 _LDLIBS-$(CONFIG_RTE_LIBRTE_METRICS)        += --whole-archive
 _LDLIBS-$(CONFIG_RTE_LIBRTE_METRICS)        += -lrte_metrics
@@ -95,6 +90,7 @@  _LDLIBS-$(CONFIG_RTE_DRIVER_MEMPOOL_RING)   += -lrte_mempool_ring
 _LDLIBS-$(CONFIG_RTE_LIBRTE_OCTEONTX2_MEMPOOL) += -lrte_mempool_octeontx2
 _LDLIBS-$(CONFIG_RTE_LIBRTE_RING)           += -lrte_ring
 _LDLIBS-$(CONFIG_RTE_LIBRTE_PCI)            += -lrte_pci
+_LDLIBS-$(CONFIG_RTE_LIBRTE_TELEMETRY)      += -lrte_telemetry -ljansson
 _LDLIBS-$(CONFIG_RTE_LIBRTE_EAL)            += -lrte_eal
 _LDLIBS-$(CONFIG_RTE_LIBRTE_CMDLINE)        += -lrte_cmdline
 _LDLIBS-$(CONFIG_RTE_LIBRTE_REORDER)        += -lrte_reorder