[dpdk-dev] [PATCH v4 02/18] telemetry: move code to metrics for later reuse
Stephen Hemminger
stephen at networkplumber.org
Fri Apr 24 17:29:53 CEST 2020
On Fri, 24 Apr 2020 13:41:43 +0100
Ciara Power <ciara.power at intel.com> wrote:
> This commit moves some of the telemetry library code to a new file in
> the metrics library. No modifications are made to the moved code,
> except what is needed to allow it to compile and run. The additional
> code in metrics is built only when the Jansson library is present.
> Telemetry functions as normal, using the functions from the
> metrics_telemetry file. This move will enable code be reused by the new
> version of telemetry in a later commit, to support backward
> compatibility with the existing telemetry usage.
>
> Signed-off-by: Ciara Power <ciara.power at intel.com>
Minor comments, none of these are show stoppers.
> diff --git a/lib/librte_metrics/rte_metrics.c b/lib/librte_metrics/rte_metrics.c
> index df5e32c59f..9b38d7787c 100644
> --- a/lib/librte_metrics/rte_metrics.c
> +++ b/lib/librte_metrics/rte_metrics.c
> @@ -13,7 +13,6 @@
> #include <rte_memzone.h>
> #include <rte_spinlock.h>
>
> -#define RTE_METRICS_MAX_METRICS 256
> #define RTE_METRICS_MEMZONE_NAME "RTE_METRICS"
>
> /**
> diff --git a/lib/librte_metrics/rte_metrics.h b/lib/librte_metrics/rte_metrics.h
> index 77bffe08e4..466ca98c31 100644
> --- a/lib/librte_metrics/rte_metrics.h
> +++ b/lib/librte_metrics/rte_metrics.h
> @@ -32,6 +32,7 @@ extern "C" {
>
> /** Maximum length of metric name (including null-terminator) */
> #define RTE_METRICS_MAX_NAME_LEN 64
> +#define RTE_METRICS_MAX_METRICS 256
Exposing max metrics to API/ABI does limit you in the future.
> diff --git a/lib/librte_metrics/rte_metrics_telemetry.c b/lib/librte_metrics/rte_metrics_telemetry.c
> new file mode 100644
> index 0000000000..a6b2616714
> --- /dev/null
> +++ b/lib/librte_metrics/rte_metrics_telemetry.c
> @@ -0,0 +1,719 @@
...
> +static int32_t
> +rte_metrics_tel_is_port_active(int port_id)
portid should be uint16_t like rte_ethdev
return bool rather than int
> +{
> + int ret;
> +
> + ret = rte_eth_find_next(port_id);
> + if (ret == port_id)
> + return 1;
> +
> + METRICS_LOG_ERR("port_id: %d is invalid, not active",
> + port_id);
> +
> + return 0;
> +}
> +
> +static int32_t
> +rte_metrics_tel_reg_port_ethdev_to_metrics(uint16_t port_id)
> +{
> + int ret, num_xstats, ret_val, i;
> + struct rte_eth_xstat *eth_xstats = NULL;
> + struct rte_eth_xstat_name *eth_xstats_names = NULL;
You don't need to initialize all variables, and it is actually
bad practice with modern compilers since it breaks checking
for using values before set.
> +
> + if (!rte_eth_dev_is_valid_port(port_id)) {
> + METRICS_LOG_ERR("port_id: %d is invalid", port_id);
> + return -EINVAL;
> + }
Validity of port_id is already checked by rte_eth_xstats_get
> + num_xstats = rte_eth_xstats_get(port_id, NULL, 0);
> + if (num_xstats < 0) {
> + METRICS_LOG_ERR("rte_eth_xstats_get(%u) failed: %d",
> + port_id, num_xstats);
> + return -EPERM;
> + }
> +
> + eth_xstats = malloc(sizeof(struct rte_eth_xstat) * num_xstats);
> + if (eth_xstats == NULL) {
> + METRICS_LOG_ERR("Failed to malloc memory for xstats");
> + return -ENOMEM;
> + }
> +
> + ret = rte_eth_xstats_get(port_id, eth_xstats, num_xstats);
> + const char *xstats_names[num_xstats];
Please don't put declarations in the middle of the code.
You should malloc the names as well
> + eth_xstats_names = malloc(sizeof(struct rte_eth_xstat_name)
> + * num_xstats);
> + if (ret < 0 || ret > num_xstats) {
> + METRICS_LOG_ERR("rte_eth_xstats_get(%u) len%i failed: %d",
> + port_id, num_xstats, ret);
> + ret_val = -EPERM;
> + goto free_xstats;
> + }
> +
> + if (eth_xstats_names == NULL) {
> + METRICS_LOG_ERR("Failed to malloc memory for xstats_names");
> + ret_val = -ENOMEM;
> + goto free_xstats;
> + }
> +
> + ret = rte_eth_xstats_get_names(port_id, eth_xstats_names, num_xstats);
> + if (ret < 0 || ret > num_xstats) {
> + METRICS_LOG_ERR("rte_eth_xstats_get_names(%u) len%i failed: %d",
> + port_id, num_xstats, ret);
> + ret_val = -EPERM;
> + goto free_xstats;
> + }
> +
> + for (i = 0; i < num_xstats; i++)
> + xstats_names[i] = eth_xstats_names[eth_xstats[i].id].name;
> +
> + ret_val = rte_metrics_reg_names(xstats_names, num_xstats);
> + if (ret_val < 0) {
> + METRICS_LOG_ERR("rte_metrics_reg_names failed - metrics may already be registered");
> + ret_val = -1;
> + goto free_xstats;
> + }
> +
> + goto free_xstats;
> +
> +free_xstats:
> + free(eth_xstats);
> + free(eth_xstats_names);
> + return ret_val;
> +}
> +
> +int32_t
> +rte_metrics_tel_reg_all_ethdev(int *metrics_register_done, int *reg_index_list)
> +{
> + struct driver_index {
> + const void *dev_ops;
> + int reg_index;
> + } drv_idx[RTE_MAX_ETHPORTS] = { {0} };
> + int nb_drv_idx = 0;
> + uint16_t pid;
> + int ret;
> +
> + RTE_ETH_FOREACH_DEV(pid) {
> + int i;
> + /* Different device types have different numbers of stats, so
> + * first check if the stats for this type of device have
> + * already been registered
> + */
> + for (i = 0; i < nb_drv_idx; i++) {
> + if (rte_eth_devices[pid].dev_ops ==
> + drv_idx[i].dev_ops) {
> + reg_index_list[pid] = drv_idx[i].reg_index;
> + break;
> + }
> + }
> + if (i < nb_drv_idx)
> + continue; /* we found a match, go to next port */
> +
> + /* No match, register a new set of xstats for this port */
> + ret = rte_metrics_tel_reg_port_ethdev_to_metrics(pid);
> + if (ret < 0) {
> + METRICS_LOG_ERR("Failed to register ethdev metrics");
> + return -1;
> + }
> + reg_index_list[pid] = ret;
> + drv_idx[nb_drv_idx].dev_ops = rte_eth_devices[pid].dev_ops;
> + drv_idx[nb_drv_idx].reg_index = ret;
> + nb_drv_idx++;
> + }
> +
> + *metrics_register_done = 1;
> + return 0;
> +}
> +
> +static int32_t
> +rte_metrics_tel_update_metrics_ethdev(uint16_t port_id, int reg_start_index)
> +{
> + int ret, num_xstats, i;
> + struct rte_eth_xstat *eth_xstats;
> +
> + if (!rte_eth_dev_is_valid_port(port_id)) {
> + METRICS_LOG_ERR("port_id: %d is invalid", port_id);
> + return -EINVAL;
> + }
> +
> + ret = rte_metrics_tel_is_port_active(port_id);
> + if (ret < 1)
> + return -EINVAL;
> +
> + num_xstats = rte_eth_xstats_get(port_id, NULL, 0);
> + if (num_xstats < 0) {
> + METRICS_LOG_ERR("rte_eth_xstats_get(%u) failed: %d", port_id,
> + num_xstats);
> + return -EPERM;
> + }
The number of metrics on a port should not change (as long as it has
not been hot plugged). So you could optimize by knowing number of
stats from last query.
> + eth_xstats = malloc(sizeof(struct rte_eth_xstat) * num_xstats);
> + if (eth_xstats == NULL) {
> + METRICS_LOG_ERR("Failed to malloc memory for xstats");
> + return -ENOMEM;
> + }
> +
> + ret = rte_eth_xstats_get(port_id, eth_xstats, num_xstats);
> + if (ret < 0 || ret > num_xstats) {
> + free(eth_xstats);
> + METRICS_LOG_ERR("rte_eth_xstats_get(%u) len%i failed: %d",
> + port_id, num_xstats, ret);
> + return -EPERM;
> + }
> +
> + uint64_t xstats_values[num_xstats];
> + for (i = 0; i < num_xstats; i++)
> + xstats_values[i] = eth_xstats[i].value;
> +
> + ret = rte_metrics_update_values(port_id, reg_start_index, xstats_values,
> + num_xstats);
> + if (ret < 0) {
> + METRICS_LOG_ERR("Could not update metrics values");
> + free(eth_xstats);
> + return -EPERM;
> + }
> +
> + free(eth_xstats);
> + return 0;
> +}
> +
> +static int
> +rte_metrics_tel_get_metrics(uint32_t port_id, struct rte_metric_value
> + *metrics, struct rte_metric_name *names, int num_metrics)
Don't break awkwardly in the function declaration.
> +{
> + int ret, num_values;
> +
> + if (num_metrics < 0) {
if you don't want negative, make it unsigned.
> + METRICS_LOG_ERR("Invalid metrics count");
> + return -EINVAL;
> + } else if (num_metrics == 0) {
else after return non needed.
> + METRICS_LOG_ERR("No metrics to display (none have been registered)");
> + return -EPERM;
> + }
> +
> + if (metrics == NULL) {
> + METRICS_LOG_ERR("Metrics must be initialised.");
> + return -EINVAL;
> + }
> +
> + if (names == NULL) {
> + METRICS_LOG_ERR("Names must be initialised.");
> + return -EINVAL;
> + }
> +
> + ret = rte_metrics_get_names(names, num_metrics);
> + if (ret < 0 || ret > num_metrics) {
> + METRICS_LOG_ERR("Cannot get metrics names");
> + return -EPERM;
> + }
> +
> + num_values = rte_metrics_get_values(port_id, NULL, 0);
> + ret = rte_metrics_get_values(port_id, metrics, num_values);
> + if (ret < 0 || ret > num_values) {
> + METRICS_LOG_ERR("Cannot get metrics values");
> + return -EPERM;
> + }
> +
> + return 0;
> +}
More information about the dev
mailing list