metrics: add function to deinitialise library
[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
36 /**
37  * Global metric special id.
38  *
39  * When used for the port_id parameter when calling
40  * rte_metrics_update_metric() or rte_metrics_update_metric(),
41  * the global metric, which are not associated with any specific
42  * port (i.e. device), are updated.
43  */
44 #define RTE_METRICS_GLOBAL -1
45
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 /**
76  * Initializes metric module. This function must be called from
77  * a primary process before metrics are used.
78  *
79  * @param socket_id
80  *   Socket to use for shared memory allocation.
81  */
82 void rte_metrics_init(int socket_id);
83
84 /**
85  * @warning
86  * @b EXPERIMENTAL: this API may change without prior notice
87  *
88  * Deinitialize metric module. This function must be called from
89  * a primary process after all the metrics usage is over, to
90  *  release the shared memory.
91  *
92  * @return
93  *  -EINVAL - invalid parameter.
94  *  -EIO: Error, unable to access metrics shared memory
95  *    (rte_metrics_init() not called)
96  *  0 - success
97  */
98 __rte_experimental
99 int rte_metrics_deinit(void);
100
101 /**
102  * Register a metric, making it available as a reporting parameter.
103  *
104  * Registering a metric is the way producers declare a parameter
105  * that they wish to be reported. Once registered, the associated
106  * numeric key can be obtained via rte_metrics_get_names(), which
107  * is required for updating said metric's value.
108  *
109  * @param name
110  *   Metric name. If this exceeds RTE_METRICS_MAX_NAME_LEN (including
111  *   the NULL terminator), it is truncated.
112  *
113  * @return
114  *  - Zero or positive: Success (index key of new metric)
115  *  - -EIO: Error, unable to access metrics shared memory
116  *    (rte_metrics_init() not called)
117  *  - -EINVAL: Error, invalid parameters
118  *  - -ENOMEM: Error, maximum metrics reached
119  */
120 int rte_metrics_reg_name(const char *name);
121
122 /**
123  * Register a set of metrics.
124  *
125  * This is a bulk version of rte_metrics_reg_names() and aside from
126  * handling multiple keys at once is functionally identical.
127  *
128  * @param names
129  *   List of metric names
130  *
131  * @param cnt_names
132  *   Number of metrics in set
133  *
134  * @return
135  *  - Zero or positive: Success (index key of start of set)
136  *  - -EIO: Error, unable to access metrics shared memory
137  *    (rte_metrics_init() not called)
138  *  - -EINVAL: Error, invalid parameters
139  *  - -ENOMEM: Error, maximum metrics reached
140  */
141 int rte_metrics_reg_names(const char * const *names, uint16_t cnt_names);
142
143 /**
144  * Get metric name-key lookup table.
145  *
146  * @param names
147  *   A struct rte_metric_name array of at least *capacity* in size to
148  *   receive key names. If this is NULL, function returns the required
149  *   number of elements for this array.
150  *
151  * @param capacity
152  *   Size (number of elements) of struct rte_metric_name array.
153  *   Disregarded if names is NULL.
154  *
155  * @return
156  *   - Positive value above capacity: error, *names* is too small.
157  *     Return value is required size.
158  *   - Positive value equal or less than capacity: Success. Return
159  *     value is number of elements filled in.
160  *   - Negative value: error.
161  */
162 int rte_metrics_get_names(
163         struct rte_metric_name *names,
164         uint16_t capacity);
165
166 /**
167  * Get metric value table.
168  *
169  * @param port_id
170  *   Port id to query
171  *
172  * @param values
173  *   A struct rte_metric_value array of at least *capacity* in size to
174  *   receive metric ids and values. If this is NULL, function returns
175  *   the required number of elements for this array.
176  *
177  * @param capacity
178  *   Size (number of elements) of struct rte_metric_value array.
179  *   Disregarded if names is NULL.
180  *
181  * @return
182  *   - Positive value above capacity: error, *values* is too small.
183  *     Return value is required size.
184  *   - Positive value equal or less than capacity: Success. Return
185  *     value is number of elements filled in.
186  *   - Negative value: error.
187  */
188 int rte_metrics_get_values(
189         int port_id,
190         struct rte_metric_value *values,
191         uint16_t capacity);
192
193 /**
194  * Updates a metric
195  *
196  * @param port_id
197  *   Port to update metrics for
198  * @param key
199  *   Id of metric to update
200  * @param value
201  *   New value
202  *
203  * @return
204  *   - -EIO if unable to access shared metrics memory
205  *   - Zero on success
206  */
207 int rte_metrics_update_value(
208         int port_id,
209         uint16_t key,
210         const uint64_t value);
211
212 /**
213  * Updates a metric set. Note that it is an error to try to
214  * update across a set boundary.
215  *
216  * @param port_id
217  *   Port to update metrics for
218  * @param key
219  *   Base id of metrics set to update
220  * @param values
221  *   Set of new values
222  * @param count
223  *   Number of new values
224  *
225  * @return
226  *   - -ERANGE if count exceeds metric set size
227  *   - -EIO if unable to access shared metrics memory
228  *   - Zero on success
229  */
230 int rte_metrics_update_values(
231         int port_id,
232         uint16_t key,
233         const uint64_t *values,
234         uint32_t count);
235
236 #ifdef __cplusplus
237 }
238 #endif
239
240 #endif