xref: /dpdk/lib/metrics/rte_metrics.h (revision 1094dd940ec0cc4e3ce2c5cd94807350855a17f9)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  * Copyright(c) 2017 Intel Corporation
399a2dd95SBruce Richardson  */
499a2dd95SBruce Richardson 
599a2dd95SBruce Richardson /**
699a2dd95SBruce Richardson  * @file
799a2dd95SBruce Richardson  *
899a2dd95SBruce Richardson  * DPDK Metrics module
999a2dd95SBruce Richardson  *
1099a2dd95SBruce Richardson  * Metrics are statistics that are not generated by PMDs, and hence
1199a2dd95SBruce Richardson  * are better reported through a mechanism that is independent from
1299a2dd95SBruce Richardson  * the ethdev-based extended statistics. Providers will typically
1399a2dd95SBruce Richardson  * be other libraries and consumers will typically be applications.
1499a2dd95SBruce Richardson  *
1599a2dd95SBruce Richardson  * Metric information is populated using a push model, where producers
1699a2dd95SBruce Richardson  * update the values contained within the metric library by calling
1799a2dd95SBruce Richardson  * an update function on the relevant metrics. Consumers receive
1899a2dd95SBruce Richardson  * metric information by querying the central metric data, which is
1999a2dd95SBruce Richardson  * held in shared memory. Currently only bulk querying of metrics
2099a2dd95SBruce Richardson  * by consumers is supported.
2199a2dd95SBruce Richardson  */
2299a2dd95SBruce Richardson 
2399a2dd95SBruce Richardson #ifndef _RTE_METRICS_H_
2499a2dd95SBruce Richardson #define _RTE_METRICS_H_
2599a2dd95SBruce Richardson 
2699a2dd95SBruce Richardson #include <stdint.h>
2799a2dd95SBruce Richardson 
2899a2dd95SBruce Richardson #ifdef __cplusplus
2999a2dd95SBruce Richardson extern "C" {
3099a2dd95SBruce Richardson #endif
3199a2dd95SBruce Richardson 
3299a2dd95SBruce Richardson extern int metrics_initialized;
3399a2dd95SBruce Richardson 
3499a2dd95SBruce Richardson /** Maximum length of metric name (including null-terminator) */
3599a2dd95SBruce Richardson #define RTE_METRICS_MAX_NAME_LEN 64
3699a2dd95SBruce Richardson #define RTE_METRICS_MAX_METRICS 256
3799a2dd95SBruce Richardson 
3899a2dd95SBruce Richardson /**
3999a2dd95SBruce Richardson  * Global metric special id.
4099a2dd95SBruce Richardson  *
4199a2dd95SBruce Richardson  * When used for the port_id parameter when calling
4299a2dd95SBruce Richardson  * rte_metrics_update_metric() or rte_metrics_update_metric(),
4399a2dd95SBruce Richardson  * the global metric, which are not associated with any specific
4499a2dd95SBruce Richardson  * port (i.e. device), are updated.
4599a2dd95SBruce Richardson  */
4699a2dd95SBruce Richardson #define RTE_METRICS_GLOBAL -1
4799a2dd95SBruce Richardson 
4899a2dd95SBruce Richardson /**
4999a2dd95SBruce Richardson  * A name-key lookup for metrics.
5099a2dd95SBruce Richardson  *
5199a2dd95SBruce Richardson  * An array of this structure is returned by rte_metrics_get_names().
5299a2dd95SBruce Richardson  * The struct rte_metric_value references these names via their array index.
5399a2dd95SBruce Richardson  */
5499a2dd95SBruce Richardson struct rte_metric_name {
5599a2dd95SBruce Richardson 	/** String describing metric */
5699a2dd95SBruce Richardson 	char name[RTE_METRICS_MAX_NAME_LEN];
5799a2dd95SBruce Richardson };
5899a2dd95SBruce Richardson 
5999a2dd95SBruce Richardson 
6099a2dd95SBruce Richardson /**
6199a2dd95SBruce Richardson  * Metric value structure.
6299a2dd95SBruce Richardson  *
6399a2dd95SBruce Richardson  * This structure is used by rte_metrics_get_values() to return metrics,
6499a2dd95SBruce Richardson  * which are statistics that are not generated by PMDs. It maps a name key,
6599a2dd95SBruce Richardson  * which corresponds to an index in the array returned by
6699a2dd95SBruce Richardson  * rte_metrics_get_names().
6799a2dd95SBruce Richardson  */
6899a2dd95SBruce Richardson struct rte_metric_value {
6999a2dd95SBruce Richardson 	/** Numeric identifier of metric. */
7099a2dd95SBruce Richardson 	uint16_t key;
7199a2dd95SBruce Richardson 	/** Value for metric */
7299a2dd95SBruce Richardson 	uint64_t value;
7399a2dd95SBruce Richardson };
7499a2dd95SBruce Richardson 
7599a2dd95SBruce Richardson /**
7699a2dd95SBruce Richardson  * Initializes metric module. This function must be called from
7799a2dd95SBruce Richardson  * a primary process before metrics are used.
7899a2dd95SBruce Richardson  *
7999a2dd95SBruce Richardson  * @param socket_id
8099a2dd95SBruce Richardson  *   Socket to use for shared memory allocation.
81*6d03ef60SBruce Richardson  * @return
82*6d03ef60SBruce Richardson  *   0 on success
83*6d03ef60SBruce Richardson  *   Negative error code (from rte_errno.h) on error:
84*6d03ef60SBruce Richardson  *     -EEXIST - a memzone for metrics already exists but metrics is not initialized
85*6d03ef60SBruce Richardson  *     -ENOMEM - cannot allocate metrics memzone
86*6d03ef60SBruce Richardson  *     -E_RTE_SECONDARY - function called from secondary process
8799a2dd95SBruce Richardson  */
88*6d03ef60SBruce Richardson int rte_metrics_init(int socket_id);
8999a2dd95SBruce Richardson 
9099a2dd95SBruce Richardson /**
9199a2dd95SBruce Richardson  * Deinitialize metric module. This function must be called from
9299a2dd95SBruce Richardson  * a primary process after all the metrics usage is over, to
9399a2dd95SBruce Richardson  *  release the shared memory.
9499a2dd95SBruce Richardson  *
9599a2dd95SBruce Richardson  * @return
9699a2dd95SBruce Richardson  *  -EINVAL - invalid parameter.
9799a2dd95SBruce Richardson  *  -EIO: Error, unable to access metrics shared memory
9899a2dd95SBruce Richardson  *    (rte_metrics_init() not called)
9999a2dd95SBruce Richardson  *  0 - success
10099a2dd95SBruce Richardson  */
10199a2dd95SBruce Richardson int rte_metrics_deinit(void);
10299a2dd95SBruce Richardson 
10399a2dd95SBruce Richardson /**
10499a2dd95SBruce Richardson  * Register a metric, making it available as a reporting parameter.
10599a2dd95SBruce Richardson  *
10699a2dd95SBruce Richardson  * Registering a metric is the way producers declare a parameter
10799a2dd95SBruce Richardson  * that they wish to be reported. Once registered, the associated
10899a2dd95SBruce Richardson  * numeric key can be obtained via rte_metrics_get_names(), which
10999a2dd95SBruce Richardson  * is required for updating said metric's value.
11099a2dd95SBruce Richardson  *
11199a2dd95SBruce Richardson  * @param name
11299a2dd95SBruce Richardson  *   Metric name. If this exceeds RTE_METRICS_MAX_NAME_LEN (including
11399a2dd95SBruce Richardson  *   the NULL terminator), it is truncated.
11499a2dd95SBruce Richardson  *
11599a2dd95SBruce Richardson  * @return
11699a2dd95SBruce Richardson  *  - Zero or positive: Success (index key of new metric)
11799a2dd95SBruce Richardson  *  - -EIO: Error, unable to access metrics shared memory
11899a2dd95SBruce Richardson  *    (rte_metrics_init() not called)
11999a2dd95SBruce Richardson  *  - -EINVAL: Error, invalid parameters
12099a2dd95SBruce Richardson  *  - -ENOMEM: Error, maximum metrics reached
12199a2dd95SBruce Richardson  */
12299a2dd95SBruce Richardson int rte_metrics_reg_name(const char *name);
12399a2dd95SBruce Richardson 
12499a2dd95SBruce Richardson /**
12599a2dd95SBruce Richardson  * Register a set of metrics.
12699a2dd95SBruce Richardson  *
12799a2dd95SBruce Richardson  * This is a bulk version of rte_metrics_reg_names() and aside from
12899a2dd95SBruce Richardson  * handling multiple keys at once is functionally identical.
12999a2dd95SBruce Richardson  *
13099a2dd95SBruce Richardson  * @param names
13199a2dd95SBruce Richardson  *   List of metric names
13299a2dd95SBruce Richardson  *
13399a2dd95SBruce Richardson  * @param cnt_names
13499a2dd95SBruce Richardson  *   Number of metrics in set
13599a2dd95SBruce Richardson  *
13699a2dd95SBruce Richardson  * @return
13799a2dd95SBruce Richardson  *  - Zero or positive: Success (index key of start of set)
13899a2dd95SBruce Richardson  *  - -EIO: Error, unable to access metrics shared memory
13999a2dd95SBruce Richardson  *    (rte_metrics_init() not called)
14099a2dd95SBruce Richardson  *  - -EINVAL: Error, invalid parameters
14199a2dd95SBruce Richardson  *  - -ENOMEM: Error, maximum metrics reached
14299a2dd95SBruce Richardson  */
14399a2dd95SBruce Richardson int rte_metrics_reg_names(const char * const *names, uint16_t cnt_names);
14499a2dd95SBruce Richardson 
14599a2dd95SBruce Richardson /**
14699a2dd95SBruce Richardson  * Get metric name-key lookup table.
14799a2dd95SBruce Richardson  *
14899a2dd95SBruce Richardson  * @param names
14999a2dd95SBruce Richardson  *   A struct rte_metric_name array of at least *capacity* in size to
15099a2dd95SBruce Richardson  *   receive key names. If this is NULL, function returns the required
15199a2dd95SBruce Richardson  *   number of elements for this array.
15299a2dd95SBruce Richardson  *
15399a2dd95SBruce Richardson  * @param capacity
15499a2dd95SBruce Richardson  *   Size (number of elements) of struct rte_metric_name array.
15599a2dd95SBruce Richardson  *   Disregarded if names is NULL.
15699a2dd95SBruce Richardson  *
15799a2dd95SBruce Richardson  * @return
15899a2dd95SBruce Richardson  *   - Positive value above capacity: error, *names* is too small.
15999a2dd95SBruce Richardson  *     Return value is required size.
16099a2dd95SBruce Richardson  *   - Positive value equal or less than capacity: Success. Return
16199a2dd95SBruce Richardson  *     value is number of elements filled in.
16299a2dd95SBruce Richardson  *   - Negative value: error.
16399a2dd95SBruce Richardson  */
16499a2dd95SBruce Richardson int rte_metrics_get_names(
16599a2dd95SBruce Richardson 	struct rte_metric_name *names,
16699a2dd95SBruce Richardson 	uint16_t capacity);
16799a2dd95SBruce Richardson 
16899a2dd95SBruce Richardson /**
16999a2dd95SBruce Richardson  * Get metric value table.
17099a2dd95SBruce Richardson  *
17199a2dd95SBruce Richardson  * @param port_id
17299a2dd95SBruce Richardson  *   Port id to query
17399a2dd95SBruce Richardson  *
17499a2dd95SBruce Richardson  * @param values
17599a2dd95SBruce Richardson  *   A struct rte_metric_value array of at least *capacity* in size to
17699a2dd95SBruce Richardson  *   receive metric ids and values. If this is NULL, function returns
17799a2dd95SBruce Richardson  *   the required number of elements for this array.
17899a2dd95SBruce Richardson  *
17999a2dd95SBruce Richardson  * @param capacity
18099a2dd95SBruce Richardson  *   Size (number of elements) of struct rte_metric_value array.
18199a2dd95SBruce Richardson  *   Disregarded if names is NULL.
18299a2dd95SBruce Richardson  *
18399a2dd95SBruce Richardson  * @return
18499a2dd95SBruce Richardson  *   - Positive value above capacity: error, *values* is too small.
18599a2dd95SBruce Richardson  *     Return value is required size.
18699a2dd95SBruce Richardson  *   - Positive value equal or less than capacity: Success. Return
18799a2dd95SBruce Richardson  *     value is number of elements filled in.
18899a2dd95SBruce Richardson  *   - Negative value: error.
18999a2dd95SBruce Richardson  */
19099a2dd95SBruce Richardson int rte_metrics_get_values(
19199a2dd95SBruce Richardson 	int port_id,
19299a2dd95SBruce Richardson 	struct rte_metric_value *values,
19399a2dd95SBruce Richardson 	uint16_t capacity);
19499a2dd95SBruce Richardson 
19599a2dd95SBruce Richardson /**
19699a2dd95SBruce Richardson  * Updates a metric
19799a2dd95SBruce Richardson  *
19899a2dd95SBruce Richardson  * @param port_id
19999a2dd95SBruce Richardson  *   Port to update metrics for
20099a2dd95SBruce Richardson  * @param key
20199a2dd95SBruce Richardson  *   Id of metric to update
20299a2dd95SBruce Richardson  * @param value
20399a2dd95SBruce Richardson  *   New value
20499a2dd95SBruce Richardson  *
20599a2dd95SBruce Richardson  * @return
20699a2dd95SBruce Richardson  *   - -EIO if unable to access shared metrics memory
20799a2dd95SBruce Richardson  *   - Zero on success
20899a2dd95SBruce Richardson  */
20999a2dd95SBruce Richardson int rte_metrics_update_value(
21099a2dd95SBruce Richardson 	int port_id,
21199a2dd95SBruce Richardson 	uint16_t key,
21299a2dd95SBruce Richardson 	const uint64_t value);
21399a2dd95SBruce Richardson 
21499a2dd95SBruce Richardson /**
21599a2dd95SBruce Richardson  * Updates a metric set. Note that it is an error to try to
21699a2dd95SBruce Richardson  * update across a set boundary.
21799a2dd95SBruce Richardson  *
21899a2dd95SBruce Richardson  * @param port_id
21999a2dd95SBruce Richardson  *   Port to update metrics for
22099a2dd95SBruce Richardson  * @param key
22199a2dd95SBruce Richardson  *   Base id of metrics set to update
22299a2dd95SBruce Richardson  * @param values
22399a2dd95SBruce Richardson  *   Set of new values
22499a2dd95SBruce Richardson  * @param count
22599a2dd95SBruce Richardson  *   Number of new values
22699a2dd95SBruce Richardson  *
22799a2dd95SBruce Richardson  * @return
22899a2dd95SBruce Richardson  *   - -ERANGE if count exceeds metric set size
22999a2dd95SBruce Richardson  *   - -EIO if unable to access shared metrics memory
23099a2dd95SBruce Richardson  *   - Zero on success
23199a2dd95SBruce Richardson  */
23299a2dd95SBruce Richardson int rte_metrics_update_values(
23399a2dd95SBruce Richardson 	int port_id,
23499a2dd95SBruce Richardson 	uint16_t key,
23599a2dd95SBruce Richardson 	const uint64_t *values,
23699a2dd95SBruce Richardson 	uint32_t count);
23799a2dd95SBruce Richardson 
23899a2dd95SBruce Richardson #ifdef __cplusplus
23999a2dd95SBruce Richardson }
24099a2dd95SBruce Richardson #endif
24199a2dd95SBruce Richardson 
24299a2dd95SBruce Richardson #endif
243