0fa3104ec4d786eb0f222bd8a68bf06834664262
[deb_dpdk.git] / lib / librte_metrics / rte_metrics.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2017 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 /**
35  * @file
36  *
37  * DPDK Metrics module
38  *
39  * Metrics are statistics that are not generated by PMDs, and hence
40  * are better reported through a mechanism that is independent from
41  * the ethdev-based extended statistics. Providers will typically
42  * be other libraries and consumers will typically be applications.
43  *
44  * Metric information is populated using a push model, where producers
45  * update the values contained within the metric library by calling
46  * an update function on the relevant metrics. Consumers receive
47  * metric information by querying the central metric data, which is
48  * held in shared memory. Currently only bulk querying of metrics
49  * by consumers is supported.
50  */
51
52 #ifndef _RTE_METRICS_H_
53 #define _RTE_METRICS_H_
54
55 #include <stdint.h>
56
57 #ifdef __cplusplus
58 extern "C" {
59 #endif
60
61 /** Maximum length of metric name (including null-terminator) */
62 #define RTE_METRICS_MAX_NAME_LEN 64
63
64 /**
65  * Global metric special id.
66  *
67  * When used for the port_id parameter when calling
68  * rte_metrics_update_metric() or rte_metrics_update_metric(),
69  * the global metric, which are not associated with any specific
70  * port (i.e. device), are updated.
71  */
72 #define RTE_METRICS_GLOBAL -1
73
74
75 /**
76  * A name-key lookup for metrics.
77  *
78  * An array of this structure is returned by rte_metrics_get_names().
79  * The struct rte_metric_value references these names via their array index.
80  */
81 struct rte_metric_name {
82         /** String describing metric */
83         char name[RTE_METRICS_MAX_NAME_LEN];
84 };
85
86
87 /**
88  * Metric value structure.
89  *
90  * This structure is used by rte_metrics_get_values() to return metrics,
91  * which are statistics that are not generated by PMDs. It maps a name key,
92  * which corresponds to an index in the array returned by
93  * rte_metrics_get_names().
94  */
95 struct rte_metric_value {
96         /** Numeric identifier of metric. */
97         uint16_t key;
98         /** Value for metric */
99         uint64_t value;
100 };
101
102
103 /**
104  * Initializes metric module. This function must be called from
105  * a primary process before metrics are used.
106  *
107  * @param socket_id
108  *   Socket to use for shared memory allocation.
109  */
110 void rte_metrics_init(int socket_id);
111
112 /**
113  * Register a metric, making it available as a reporting parameter.
114  *
115  * Registering a metric is the way producers declare a parameter
116  * that they wish to be reported. Once registered, the associated
117  * numeric key can be obtained via rte_metrics_get_names(), which
118  * is required for updating said metric's value.
119  *
120  * @param name
121  *   Metric name
122  *
123  * @return
124  *  - Zero or positive: Success (index key of new metric)
125  *  - -EIO: Error, unable to access metrics shared memory
126  *    (rte_metrics_init() not called)
127  *  - -EINVAL: Error, invalid parameters
128  *  - -ENOMEM: Error, maximum metrics reached
129  */
130 int rte_metrics_reg_name(const char *name);
131
132 /**
133  * Register a set of metrics.
134  *
135  * This is a bulk version of rte_metrics_reg_names() and aside from
136  * handling multiple keys at once is functionally identical.
137  *
138  * @param names
139  *   List of metric names
140  *
141  * @param cnt_names
142  *   Number of metrics in set
143  *
144  * @return
145  *  - Zero or positive: Success (index key of start of set)
146  *  - -EIO: Error, unable to access metrics shared memory
147  *    (rte_metrics_init() not called)
148  *  - -EINVAL: Error, invalid parameters
149  *  - -ENOMEM: Error, maximum metrics reached
150  */
151 int rte_metrics_reg_names(const char * const *names, uint16_t cnt_names);
152
153 /**
154  * Get metric name-key lookup table.
155  *
156  * @param names
157  *   A struct rte_metric_name array of at least *capacity* in size to
158  *   receive key names. If this is NULL, function returns the required
159  *   number of elements for this array.
160  *
161  * @param capacity
162  *   Size (number of elements) of struct rte_metric_name array.
163  *   Disregarded if names is NULL.
164  *
165  * @return
166  *   - Positive value above capacity: error, *names* is too small.
167  *     Return value is required size.
168  *   - Positive value equal or less than capacity: Success. Return
169  *     value is number of elements filled in.
170  *   - Negative value: error.
171  */
172 int rte_metrics_get_names(
173         struct rte_metric_name *names,
174         uint16_t capacity);
175
176 /**
177  * Get metric value table.
178  *
179  * @param port_id
180  *   Port id to query
181  *
182  * @param values
183  *   A struct rte_metric_value array of at least *capacity* in size to
184  *   receive metric ids and values. If this is NULL, function returns
185  *   the required number of elements for this array.
186  *
187  * @param capacity
188  *   Size (number of elements) of struct rte_metric_value array.
189  *   Disregarded if names is NULL.
190  *
191  * @return
192  *   - Positive value above capacity: error, *values* is too small.
193  *     Return value is required size.
194  *   - Positive value equal or less than capacity: Success. Return
195  *     value is number of elements filled in.
196  *   - Negative value: error.
197  */
198 int rte_metrics_get_values(
199         int port_id,
200         struct rte_metric_value *values,
201         uint16_t capacity);
202
203 /**
204  * Updates a metric
205  *
206  * @param port_id
207  *   Port to update metrics for
208  * @param key
209  *   Id of metric to update
210  * @param value
211  *   New value
212  *
213  * @return
214  *   - -EIO if unable to access shared metrics memory
215  *   - Zero on success
216  */
217 int rte_metrics_update_value(
218         int port_id,
219         uint16_t key,
220         const uint64_t value);
221
222 /**
223  * Updates a metric set. Note that it is an error to try to
224  * update across a set boundary.
225  *
226  * @param port_id
227  *   Port to update metrics for
228  * @param key
229  *   Base id of metrics set to update
230  * @param values
231  *   Set of new values
232  * @param count
233  *   Number of new values
234  *
235  * @return
236  *   - -ERANGE if count exceeds metric set size
237  *   - -EIO if unable to access shared metrics memory
238  *   - Zero on success
239  */
240 int rte_metrics_update_values(
241         int port_id,
242         uint16_t key,
243         const uint64_t *values,
244         uint32_t count);
245
246 #ifdef __cplusplus
247 }
248 #endif
249
250 #endif