04ab83769cce163eacb860e858f9317731594749
[vpp.git] / src / vlib / counter.h
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /*
16  * counter.h: simple and packet/byte counters
17  *
18  * Copyright (c) 2008 Eliot Dresselhaus
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining
21  * a copy of this software and associated documentation files (the
22  * "Software"), to deal in the Software without restriction, including
23  * without limitation the rights to use, copy, modify, merge, publish,
24  * distribute, sublicense, and/or sell copies of the Software, and to
25  * permit persons to whom the Software is furnished to do so, subject to
26  * the following conditions:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
31  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  */
39
40 #ifndef included_vlib_counter_h
41 #define included_vlib_counter_h
42
43 #include <vlib/counter_types.h>
44
45 /** \file
46
47     Optimized thread-safe counters.
48
49     Each vlib_[simple|combined]_counter_main_t consists of a per-thread
50     vector of per-object counters.
51
52     The idea is to drastically eliminate atomic operations.
53 */
54
55 /** A collection of simple counters */
56
57 typedef struct
58 {
59   counter_t **counters;  /**< Per-thread u64 non-atomic counters */
60   counter_t *value_at_last_serialize;   /**< Values as of last serialize. */
61   u32 last_incremental_serialize_index; /**< Last counter index
62                                            serialized incrementally. */
63
64   char *name;                   /**< The counter collection's name. */
65   char *stat_segment_name;    /**< Name in stat segment directory */
66 } vlib_simple_counter_main_t;
67
68 /** The number of counters (not the number of per-thread counters) */
69 u32 vlib_simple_counter_n_counters (const vlib_simple_counter_main_t * cm);
70
71 /** Increment a simple counter
72     @param cm - (vlib_simple_counter_main_t *) simple counter main pointer
73     @param thread_index - (u32) the current cpu index
74     @param index - (u32) index of the counter to increment
75     @param increment - (u64) quantitiy to add to the counter
76 */
77 always_inline void
78 vlib_increment_simple_counter (vlib_simple_counter_main_t * cm,
79                                u32 thread_index, u32 index, u64 increment)
80 {
81   counter_t *my_counters;
82
83   my_counters = cm->counters[thread_index];
84   my_counters[index] += increment;
85 }
86
87 /** Decrement a simple counter
88     @param cm - (vlib_simple_counter_main_t *) simple counter main pointer
89     @param thread_index - (u32) the current cpu index
90     @param index - (u32) index of the counter to increment
91     @param increment - (u64) quantitiy remove from the counter value
92 */
93 always_inline void
94 vlib_decrement_simple_counter (vlib_simple_counter_main_t * cm,
95                                u32 thread_index, u32 index, u64 decrement)
96 {
97   counter_t *my_counters;
98
99   my_counters = cm->counters[thread_index];
100
101   ASSERT (my_counters[index] >= decrement);
102
103   my_counters[index] -= decrement;
104 }
105
106 /** Set a simple counter
107     @param cm - (vlib_simple_counter_main_t *) simple counter main pointer
108     @param thread_index - (u32) the current cpu index
109     @param index - (u32) index of the counter to increment
110     @param value - (u64) quantitiy to set to the counter
111 */
112 always_inline void
113 vlib_set_simple_counter (vlib_simple_counter_main_t * cm,
114                          u32 thread_index, u32 index, u64 value)
115 {
116   counter_t *my_counters;
117
118   my_counters = cm->counters[thread_index];
119   my_counters[index] = value;
120 }
121
122 /** Get the value of a simple counter
123     Scrapes the entire set of per-thread counters. Innacurate unless
124     worker threads which might increment the counter are
125     barrier-synchronized
126
127     @param cm - (vlib_simple_counter_main_t *) simple counter main pointer
128     @param index - (u32) index of the counter to fetch
129     @returns - (u64) current counter value
130 */
131 always_inline counter_t
132 vlib_get_simple_counter (vlib_simple_counter_main_t * cm, u32 index)
133 {
134   counter_t *my_counters;
135   counter_t v;
136   int i;
137
138   ASSERT (index < vlib_simple_counter_n_counters (cm));
139
140   v = 0;
141
142   for (i = 0; i < vec_len (cm->counters); i++)
143     {
144       my_counters = cm->counters[i];
145       v += my_counters[index];
146     }
147
148   return v;
149 }
150
151 /** Clear a simple counter
152     Clears the set of per-thread u16 counters, and the u64 counter
153
154     @param cm - (vlib_simple_counter_main_t *) simple counter main pointer
155     @param index - (u32) index of the counter to clear
156 */
157 always_inline void
158 vlib_zero_simple_counter (vlib_simple_counter_main_t * cm, u32 index)
159 {
160   counter_t *my_counters;
161   int i;
162
163   ASSERT (index < vlib_simple_counter_n_counters (cm));
164
165   for (i = 0; i < vec_len (cm->counters); i++)
166     {
167       my_counters = cm->counters[i];
168       my_counters[index] = 0;
169     }
170 }
171
172 /** Add two combined counters, results in the first counter
173     @param [in,out] a - (vlib_counter_t *) dst counter
174     @param b - (vlib_counter_t *) src counter
175 */
176
177 always_inline void
178 vlib_counter_add (vlib_counter_t * a, vlib_counter_t * b)
179 {
180   a->packets += b->packets;
181   a->bytes += b->bytes;
182 }
183
184 /** Subtract combined counters, results in the first counter
185     @param [in,out] a - (vlib_counter_t *) dst counter
186     @param b - (vlib_counter_t *) src counter
187 */
188 always_inline void
189 vlib_counter_sub (vlib_counter_t * a, vlib_counter_t * b)
190 {
191   ASSERT (a->packets >= b->packets);
192   ASSERT (a->bytes >= b->bytes);
193   a->packets -= b->packets;
194   a->bytes -= b->bytes;
195 }
196
197 /** Clear a combined counter
198     @param a - (vlib_counter_t *) counter to clear
199 */
200 always_inline void
201 vlib_counter_zero (vlib_counter_t * a)
202 {
203   a->packets = a->bytes = 0;
204 }
205
206 /** A collection of combined counters */
207 typedef struct
208 {
209   vlib_counter_t **counters;    /**< Per-thread u64 non-atomic counter pairs */
210   vlib_counter_t *value_at_last_serialize; /**< Counter values as of last serialize. */
211   u32 last_incremental_serialize_index; /**< Last counter index serialized incrementally. */
212   char *name; /**< The counter collection's name. */
213   char *stat_segment_name;      /**< Name in stat segment directory */
214 } vlib_combined_counter_main_t;
215
216 /** The number of counters (not the number of per-thread counters) */
217 u32 vlib_combined_counter_n_counters (const vlib_combined_counter_main_t *
218                                       cm);
219
220 /** Clear a collection of simple counters
221     @param cm - (vlib_simple_counter_main_t *) collection to clear
222 */
223 void vlib_clear_simple_counters (vlib_simple_counter_main_t * cm);
224
225 /** Clear a collection of combined counters
226     @param cm - (vlib_combined_counter_main_t *) collection to clear
227 */
228 void vlib_clear_combined_counters (vlib_combined_counter_main_t * cm);
229
230 /** Increment a combined counter
231     @param cm - (vlib_combined_counter_main_t *) comined counter main pointer
232     @param thread_index - (u32) the current cpu index
233     @param index - (u32) index of the counter to increment
234     @param packet_increment - (u64) number of packets to add to the counter
235     @param byte_increment - (u64) number of bytes to add to the counter
236 */
237
238 always_inline void
239 vlib_increment_combined_counter (vlib_combined_counter_main_t * cm,
240                                  u32 thread_index,
241                                  u32 index, u64 n_packets, u64 n_bytes)
242 {
243   vlib_counter_t *my_counters;
244
245   /* Use this CPU's counter array */
246   my_counters = cm->counters[thread_index];
247
248   my_counters[index].packets += n_packets;
249   my_counters[index].bytes += n_bytes;
250 }
251
252 /** Pre-fetch a per-thread combined counter for the given object index */
253 always_inline void
254 vlib_prefetch_combined_counter (const vlib_combined_counter_main_t * cm,
255                                 u32 thread_index, u32 index)
256 {
257   vlib_counter_t *cpu_counters;
258
259   /*
260    * This CPU's index is assumed to already be in cache
261    */
262   cpu_counters = cm->counters[thread_index];
263   CLIB_PREFETCH (cpu_counters + index, CLIB_CACHE_LINE_BYTES, STORE);
264 }
265
266
267 /** Get the value of a combined counter, never called in the speed path
268     Scrapes the entire set of per-thread counters. Innacurate unless
269     worker threads which might increment the counter are
270     barrier-synchronized
271
272     @param cm - (vlib_combined_counter_main_t *) combined counter main pointer
273     @param index - (u32) index of the combined counter to fetch
274     @param result [out] - (vlib_counter_t *) result stored here
275 */
276
277 static inline void
278 vlib_get_combined_counter (const vlib_combined_counter_main_t * cm,
279                            u32 index, vlib_counter_t * result)
280 {
281   vlib_counter_t *my_counters, *counter;
282   int i;
283
284   result->packets = 0;
285   result->bytes = 0;
286
287   for (i = 0; i < vec_len (cm->counters); i++)
288     {
289       my_counters = cm->counters[i];
290
291       counter = vec_elt_at_index (my_counters, index);
292       result->packets += counter->packets;
293       result->bytes += counter->bytes;
294     }
295 }
296
297 /** Clear a combined counter
298     Clears the set of per-thread counters.
299
300     @param cm - (vlib_combined_counter_main_t *) combined counter main pointer
301     @param index - (u32) index of the counter to clear
302 */
303 always_inline void
304 vlib_zero_combined_counter (vlib_combined_counter_main_t * cm, u32 index)
305 {
306   vlib_counter_t *my_counters, *counter;
307   int i;
308
309   for (i = 0; i < vec_len (cm->counters); i++)
310     {
311       my_counters = cm->counters[i];
312
313       counter = vec_elt_at_index (my_counters, index);
314       counter->packets = 0;
315       counter->bytes = 0;
316     }
317 }
318
319 /** validate a simple counter
320     @param cm - (vlib_simple_counter_main_t *) pointer to the counter collection
321     @param index - (u32) index of the counter to validate
322 */
323
324 void vlib_validate_simple_counter (vlib_simple_counter_main_t * cm,
325                                    u32 index);
326 void vlib_free_simple_counter (vlib_simple_counter_main_t * cm);
327
328 /** validate a combined counter
329     @param cm - (vlib_combined_counter_main_t *) pointer to the counter
330     collection
331     @param index - (u32) index of the counter to validate
332 */
333
334 void vlib_validate_combined_counter (vlib_combined_counter_main_t * cm,
335                                      u32 index);
336 int vlib_validate_combined_counter_will_expand
337   (vlib_combined_counter_main_t * cm, u32 index);
338
339 void vlib_free_combined_counter (vlib_combined_counter_main_t * cm);
340
341 /** Obtain the number of simple or combined counters allocated.
342     A macro which reduces to to vec_len(cm->maxi), the answer in either
343     case.
344
345     @param cm - (vlib_simple_counter_main_t) or
346     (vlib_combined_counter_main_t) the counter collection to interrogate
347     @returns vec_len(cm->maxi)
348 */
349 #define vlib_counter_len(cm) vec_len((cm)->maxi)
350
351 #endif /* included_vlib_counter_h */
352
353 /*
354  * fd.io coding-style-patch-verification: ON
355  *
356  * Local Variables:
357  * eval: (c-set-style "gnu")
358  * End:
359  */