466ca98c313c8faf58b0c15507e453e08ce180ba
[dpdk.git] / lib / librte_metrics / rte_metrics.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Intel Corporation
3  */
4
5 /**
6  * @file
7  *
8  * DPDK Metrics module
9  *
10  * Metrics are statistics that are not generated by PMDs, and hence
11  * are better reported through a mechanism that is independent from
12  * the ethdev-based extended statistics. Providers will typically
13  * be other libraries and consumers will typically be applications.
14  *
15  * Metric information is populated using a push model, where producers
16  * update the values contained within the metric library by calling
17  * an update function on the relevant metrics. Consumers receive
18  * metric information by querying the central metric data, which is
19  * held in shared memory. Currently only bulk querying of metrics
20  * by consumers is supported.
21  */
22
23 #ifndef _RTE_METRICS_H_
24 #define _RTE_METRICS_H_
25
26 #include <stdint.h>
27 #include <rte_compat.h>
28
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32
33 /** Maximum length of metric name (including null-terminator) */
34 #define RTE_METRICS_MAX_NAME_LEN 64
35 #define RTE_METRICS_MAX_METRICS 256
36
37 /**
38  * Global metric special id.
39  *
40  * When used for the port_id parameter when calling
41  * rte_metrics_update_metric() or rte_metrics_update_metric(),
42  * the global metric, which are not associated with any specific
43  * port (i.e. device), are updated.
44  */
45 #define RTE_METRICS_GLOBAL -1
46
47 /**
48  * A name-key lookup for metrics.
49  *
50  * An array of this structure is returned by rte_metrics_get_names().
51  * The struct rte_metric_value references these names via their array index.
52  */
53 struct rte_metric_name {
54         /** String describing metric */
55         char name[RTE_METRICS_MAX_NAME_LEN];
56 };
57
58
59 /**
60  * Metric value structure.
61  *
62  * This structure is used by rte_metrics_get_values() to return metrics,
63  * which are statistics that are not generated by PMDs. It maps a name key,
64  * which corresponds to an index in the array returned by
65  * rte_metrics_get_names().
66  */
67 struct rte_metric_value {
68         /** Numeric identifier of metric. */
69         uint16_t key;
70         /** Value for metric */
71         uint64_t value;
72 };
73
74 /**
75  * Initializes metric module. This function must be called from
76  * a primary process before metrics are used.
77  *
78  * @param socket_id
79  *   Socket to use for shared memory allocation.
80  */
81 void rte_metrics_init(int socket_id);
82
83 /**
84  * @warning
85  * @b EXPERIMENTAL: this API may change without prior notice
86  *
87  * Deinitialize metric module. This function must be called from
88  * a primary process after all the metrics usage is over, to
89  *  release the shared memory.
90  *
91  * @return
92  *  -EINVAL - invalid parameter.
93  *  -EIO: Error, unable to access metrics shared memory
94  *    (rte_metrics_init() not called)
95  *  0 - success
96  */
97 __rte_experimental
98 int rte_metrics_deinit(void);
99
100 /**
101  * Register a metric, making it available as a reporting parameter.
102  *
103  * Registering a metric is the way producers declare a parameter
104  * that they wish to be reported. Once registered, the associated
105  * numeric key can be obtained via rte_metrics_get_names(), which
106  * is required for updating said metric's value.
107  *
108  * @param name
109  *   Metric name. If this exceeds RTE_METRICS_MAX_NAME_LEN (including
110  *   the NULL terminator), it is truncated.
111  *
112  * @return
113  *  - Zero or positive: Success (index key of new metric)
114  *  - -EIO: Error, unable to access metrics shared memory
115  *    (rte_metrics_init() not called)
116  *  - -EINVAL: Error, invalid parameters
117  *  - -ENOMEM: Error, maximum metrics reached
118  */
119 int rte_metrics_reg_name(const char *name);
120
121 /**
122  * Register a set of metrics.
123  *
124  * This is a bulk version of rte_metrics_reg_names() and aside from
125  * handling multiple keys at once is functionally identical.
126  *
127  * @param names
128  *   List of metric names
129  *
130  * @param cnt_names
131  *   Number of metrics in set
132  *
133  * @return
134  *  - Zero or positive: Success (index key of start of set)
135  *  - -EIO: Error, unable to access metrics shared memory
136  *    (rte_metrics_init() not called)
137  *  - -EINVAL: Error, invalid parameters
138  *  - -ENOMEM: Error, maximum metrics reached
139  */
140 int rte_metrics_reg_names(const char * const *names, uint16_t cnt_names);
141
142 /**
143  * Get metric name-key lookup table.
144  *
145  * @param names
146  *   A struct rte_metric_name array of at least *capacity* in size to
147  *   receive key names. If this is NULL, function returns the required
148  *   number of elements for this array.
149  *
150  * @param capacity
151  *   Size (number of elements) of struct rte_metric_name array.
152  *   Disregarded if names is NULL.
153  *
154  * @return
155  *   - Positive value above capacity: error, *names* is too small.
156  *     Return value is required size.
157  *   - Positive value equal or less than capacity: Success. Return
158  *     value is number of elements filled in.
159  *   - Negative value: error.
160  */
161 int rte_metrics_get_names(
162         struct rte_metric_name *names,
163         uint16_t capacity);
164
165 /**
166  * Get metric value table.
167  *
168  * @param port_id
169  *   Port id to query
170  *
171  * @param values
172  *   A struct rte_metric_value array of at least *capacity* in size to
173  *   receive metric ids and values. If this is NULL, function returns
174  *   the required number of elements for this array.
175  *
176  * @param capacity
177  *   Size (number of elements) of struct rte_metric_value array.
178  *   Disregarded if names is NULL.
179  *
180  * @return
181  *   - Positive value above capacity: error, *values* is too small.
182  *     Return value is required size.
183  *   - Positive value equal or less than capacity: Success. Return
184  *     value is number of elements filled in.
185  *   - Negative value: error.
186  */
187 int rte_metrics_get_values(
188         int port_id,
189         struct rte_metric_value *values,
190         uint16_t capacity);
191
192 /**
193  * Updates a metric
194  *
195  * @param port_id
196  *   Port to update metrics for
197  * @param key
198  *   Id of metric to update
199  * @param value
200  *   New value
201  *
202  * @return
203  *   - -EIO if unable to access shared metrics memory
204  *   - Zero on success
205  */
206 int rte_metrics_update_value(
207         int port_id,
208         uint16_t key,
209         const uint64_t value);
210
211 /**
212  * Updates a metric set. Note that it is an error to try to
213  * update across a set boundary.
214  *
215  * @param port_id
216  *   Port to update metrics for
217  * @param key
218  *   Base id of metrics set to update
219  * @param values
220  *   Set of new values
221  * @param count
222  *   Number of new values
223  *
224  * @return
225  *   - -ERANGE if count exceeds metric set size
226  *   - -EIO if unable to access shared metrics memory
227  *   - Zero on success
228  */
229 int rte_metrics_update_values(
230         int port_id,
231         uint16_t key,
232         const uint64_t *values,
233         uint32_t count);
234
235 #ifdef __cplusplus
236 }
237 #endif
238
239 #endif