blob: f9da576a5f20b32bb0dc9b9344151e8fe1c32ea3 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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
Ole Troan58492a82018-09-04 13:19:12 +020043#include <vlib/counter_types.h>
44
Dave Barach63539202016-08-11 17:21:02 -040045/** \file
46
47 Optimized thread-safe counters.
48
Neale Ranns1bd01092017-03-15 15:41:17 -040049 Each vlib_[simple|combined]_counter_main_t consists of a per-thread
50 vector of per-object counters.
Dave Barach63539202016-08-11 17:21:02 -040051
Neale Ranns1bd01092017-03-15 15:41:17 -040052 The idea is to drastically eliminate atomic operations.
Dave Barach63539202016-08-11 17:21:02 -040053*/
54
55/** A collection of simple counters */
Ed Warnickecb9cada2015-12-08 15:45:58 -070056
Dave Barach9b8ffd92016-07-08 08:13:45 -040057typedef struct
58{
Neale Ranns1bd01092017-03-15 15:41:17 -040059 counter_t **counters; /**< Per-thread u64 non-atomic counters */
Dave Barach63539202016-08-11 17:21:02 -040060 char *name; /**< The counter collection's name. */
Dave Barach048a4e52018-06-01 18:52:25 -040061 char *stat_segment_name; /**< Name in stat segment directory */
Damjan Marion58fd4812022-03-14 13:04:38 +010062 u32 stats_entry_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -070063} vlib_simple_counter_main_t;
64
Neale Ranns1bd01092017-03-15 15:41:17 -040065/** The number of counters (not the number of per-thread counters) */
66u32 vlib_simple_counter_n_counters (const vlib_simple_counter_main_t * cm);
67
Neale Rannse11203e2021-09-21 12:34:19 +000068/** Pre-fetch a per-thread simple counter for the given object index */
69always_inline void
70vlib_prefetch_simple_counter (const vlib_simple_counter_main_t *cm,
71 u32 thread_index, u32 index)
72{
73 counter_t *my_counters;
74
75 /*
76 * This CPU's index is assumed to already be in cache
77 */
78 my_counters = cm->counters[thread_index];
79 clib_prefetch_store (my_counters + index);
80}
81
Dave Barach63539202016-08-11 17:21:02 -040082/** Increment a simple counter
83 @param cm - (vlib_simple_counter_main_t *) simple counter main pointer
Damjan Marion586afd72017-04-05 19:18:20 +020084 @param thread_index - (u32) the current cpu index
Dave Barach63539202016-08-11 17:21:02 -040085 @param index - (u32) index of the counter to increment
Neale Ranns1bd01092017-03-15 15:41:17 -040086 @param increment - (u64) quantitiy to add to the counter
Dave Barach63539202016-08-11 17:21:02 -040087*/
Ed Warnickecb9cada2015-12-08 15:45:58 -070088always_inline void
89vlib_increment_simple_counter (vlib_simple_counter_main_t * cm,
Damjan Marion586afd72017-04-05 19:18:20 +020090 u32 thread_index, u32 index, u64 increment)
Ed Warnickecb9cada2015-12-08 15:45:58 -070091{
Neale Ranns1bd01092017-03-15 15:41:17 -040092 counter_t *my_counters;
Ed Warnickecb9cada2015-12-08 15:45:58 -070093
Damjan Marion586afd72017-04-05 19:18:20 +020094 my_counters = cm->counters[thread_index];
Neale Ranns1bd01092017-03-15 15:41:17 -040095 my_counters[index] += increment;
Ed Warnickecb9cada2015-12-08 15:45:58 -070096}
97
Sergey Matov34262d42020-09-15 13:40:55 +040098/** Decrement a simple counter
99 @param cm - (vlib_simple_counter_main_t *) simple counter main pointer
100 @param thread_index - (u32) the current cpu index
101 @param index - (u32) index of the counter to increment
102 @param increment - (u64) quantitiy remove from the counter value
103*/
104always_inline void
105vlib_decrement_simple_counter (vlib_simple_counter_main_t * cm,
106 u32 thread_index, u32 index, u64 decrement)
107{
108 counter_t *my_counters;
109
110 my_counters = cm->counters[thread_index];
111
112 ASSERT (my_counters[index] >= decrement);
113
114 my_counters[index] -= decrement;
115}
116
Matus Fabianb0055c82018-12-17 05:29:28 -0800117/** Set a simple counter
118 @param cm - (vlib_simple_counter_main_t *) simple counter main pointer
119 @param thread_index - (u32) the current cpu index
120 @param index - (u32) index of the counter to increment
121 @param value - (u64) quantitiy to set to the counter
122*/
123always_inline void
124vlib_set_simple_counter (vlib_simple_counter_main_t * cm,
125 u32 thread_index, u32 index, u64 value)
126{
127 counter_t *my_counters;
128
129 my_counters = cm->counters[thread_index];
130 my_counters[index] = value;
131}
132
Dave Barach63539202016-08-11 17:21:02 -0400133/** Get the value of a simple counter
Neale Ranns1bd01092017-03-15 15:41:17 -0400134 Scrapes the entire set of per-thread counters. Innacurate unless
Dave Barach63539202016-08-11 17:21:02 -0400135 worker threads which might increment the counter are
136 barrier-synchronized
137
138 @param cm - (vlib_simple_counter_main_t *) simple counter main pointer
139 @param index - (u32) index of the counter to fetch
140 @returns - (u64) current counter value
141*/
Neale Ranns1bd01092017-03-15 15:41:17 -0400142always_inline counter_t
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143vlib_get_simple_counter (vlib_simple_counter_main_t * cm, u32 index)
144{
Neale Ranns1bd01092017-03-15 15:41:17 -0400145 counter_t *my_counters;
146 counter_t v;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700147 int i;
148
Neale Ranns1bd01092017-03-15 15:41:17 -0400149 ASSERT (index < vlib_simple_counter_n_counters (cm));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150
151 v = 0;
152
Neale Ranns1bd01092017-03-15 15:41:17 -0400153 for (i = 0; i < vec_len (cm->counters); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700154 {
Neale Ranns1bd01092017-03-15 15:41:17 -0400155 my_counters = cm->counters[i];
156 v += my_counters[index];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157 }
158
159 return v;
160}
161
Dave Barach63539202016-08-11 17:21:02 -0400162/** Clear a simple counter
163 Clears the set of per-thread u16 counters, and the u64 counter
164
165 @param cm - (vlib_simple_counter_main_t *) simple counter main pointer
166 @param index - (u32) index of the counter to clear
167*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168always_inline void
169vlib_zero_simple_counter (vlib_simple_counter_main_t * cm, u32 index)
170{
Neale Ranns1bd01092017-03-15 15:41:17 -0400171 counter_t *my_counters;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172 int i;
173
Neale Ranns1bd01092017-03-15 15:41:17 -0400174 ASSERT (index < vlib_simple_counter_n_counters (cm));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175
Neale Ranns1bd01092017-03-15 15:41:17 -0400176 for (i = 0; i < vec_len (cm->counters); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177 {
Neale Ranns1bd01092017-03-15 15:41:17 -0400178 my_counters = cm->counters[i];
179 my_counters[index] = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700180 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181}
182
Dave Barach63539202016-08-11 17:21:02 -0400183/** Add two combined counters, results in the first counter
Chris Luked4024f52016-09-06 09:32:36 -0400184 @param [in,out] a - (vlib_counter_t *) dst counter
Dave Barach63539202016-08-11 17:21:02 -0400185 @param b - (vlib_counter_t *) src counter
186*/
187
Ed Warnickecb9cada2015-12-08 15:45:58 -0700188always_inline void
189vlib_counter_add (vlib_counter_t * a, vlib_counter_t * b)
190{
191 a->packets += b->packets;
192 a->bytes += b->bytes;
193}
194
Dave Barach63539202016-08-11 17:21:02 -0400195/** Subtract combined counters, results in the first counter
Chris Luked4024f52016-09-06 09:32:36 -0400196 @param [in,out] a - (vlib_counter_t *) dst counter
Dave Barach63539202016-08-11 17:21:02 -0400197 @param b - (vlib_counter_t *) src counter
198*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700199always_inline void
200vlib_counter_sub (vlib_counter_t * a, vlib_counter_t * b)
201{
202 ASSERT (a->packets >= b->packets);
203 ASSERT (a->bytes >= b->bytes);
204 a->packets -= b->packets;
205 a->bytes -= b->bytes;
206}
207
Dave Barach63539202016-08-11 17:21:02 -0400208/** Clear a combined counter
209 @param a - (vlib_counter_t *) counter to clear
210*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700211always_inline void
212vlib_counter_zero (vlib_counter_t * a)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400213{
214 a->packets = a->bytes = 0;
215}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700216
Dave Barach63539202016-08-11 17:21:02 -0400217/** A collection of combined counters */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400218typedef struct
219{
Neale Ranns1bd01092017-03-15 15:41:17 -0400220 vlib_counter_t **counters; /**< Per-thread u64 non-atomic counter pairs */
Dave Barach63539202016-08-11 17:21:02 -0400221 char *name; /**< The counter collection's name. */
Dave Barach048a4e52018-06-01 18:52:25 -0400222 char *stat_segment_name; /**< Name in stat segment directory */
Damjan Marion58fd4812022-03-14 13:04:38 +0100223 u32 stats_entry_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700224} vlib_combined_counter_main_t;
225
Neale Ranns1bd01092017-03-15 15:41:17 -0400226/** The number of counters (not the number of per-thread counters) */
227u32 vlib_combined_counter_n_counters (const vlib_combined_counter_main_t *
228 cm);
229
Dave Barach63539202016-08-11 17:21:02 -0400230/** Clear a collection of simple counters
231 @param cm - (vlib_simple_counter_main_t *) collection to clear
232*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700233void vlib_clear_simple_counters (vlib_simple_counter_main_t * cm);
Dave Barach63539202016-08-11 17:21:02 -0400234
235/** Clear a collection of combined counters
236 @param cm - (vlib_combined_counter_main_t *) collection to clear
237*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700238void vlib_clear_combined_counters (vlib_combined_counter_main_t * cm);
239
Dave Barach63539202016-08-11 17:21:02 -0400240/** Increment a combined counter
241 @param cm - (vlib_combined_counter_main_t *) comined counter main pointer
Damjan Marion586afd72017-04-05 19:18:20 +0200242 @param thread_index - (u32) the current cpu index
Dave Barach63539202016-08-11 17:21:02 -0400243 @param index - (u32) index of the counter to increment
Neale Ranns1bd01092017-03-15 15:41:17 -0400244 @param packet_increment - (u64) number of packets to add to the counter
245 @param byte_increment - (u64) number of bytes to add to the counter
Dave Barach63539202016-08-11 17:21:02 -0400246*/
247
Ed Warnickecb9cada2015-12-08 15:45:58 -0700248always_inline void
249vlib_increment_combined_counter (vlib_combined_counter_main_t * cm,
Damjan Marion586afd72017-04-05 19:18:20 +0200250 u32 thread_index,
Neale Ranns1bd01092017-03-15 15:41:17 -0400251 u32 index, u64 n_packets, u64 n_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700252{
Neale Ranns1bd01092017-03-15 15:41:17 -0400253 vlib_counter_t *my_counters;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700254
Neale Ranns1bd01092017-03-15 15:41:17 -0400255 /* Use this CPU's counter array */
Damjan Marion586afd72017-04-05 19:18:20 +0200256 my_counters = cm->counters[thread_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700257
Neale Ranns1bd01092017-03-15 15:41:17 -0400258 my_counters[index].packets += n_packets;
259 my_counters[index].bytes += n_bytes;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700260}
261
Neale Ranns1bd01092017-03-15 15:41:17 -0400262/** Pre-fetch a per-thread combined counter for the given object index */
263always_inline void
264vlib_prefetch_combined_counter (const vlib_combined_counter_main_t * cm,
Damjan Marion586afd72017-04-05 19:18:20 +0200265 u32 thread_index, u32 index)
Neale Ranns1bd01092017-03-15 15:41:17 -0400266{
267 vlib_counter_t *cpu_counters;
268
269 /*
270 * This CPU's index is assumed to already be in cache
271 */
Damjan Marion586afd72017-04-05 19:18:20 +0200272 cpu_counters = cm->counters[thread_index];
Damjan Marionaf7fb042021-07-15 11:54:41 +0200273 clib_prefetch_store (cpu_counters + index);
Neale Ranns044183f2017-01-24 01:34:25 -0800274}
275
276
Dave Barach63539202016-08-11 17:21:02 -0400277/** Get the value of a combined counter, never called in the speed path
Neale Ranns1bd01092017-03-15 15:41:17 -0400278 Scrapes the entire set of per-thread counters. Innacurate unless
Dave Barach63539202016-08-11 17:21:02 -0400279 worker threads which might increment the counter are
280 barrier-synchronized
281
282 @param cm - (vlib_combined_counter_main_t *) combined counter main pointer
283 @param index - (u32) index of the combined counter to fetch
284 @param result [out] - (vlib_counter_t *) result stored here
285*/
286
Ed Warnickecb9cada2015-12-08 15:45:58 -0700287static inline void
Neale Ranns1bd01092017-03-15 15:41:17 -0400288vlib_get_combined_counter (const vlib_combined_counter_main_t * cm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400289 u32 index, vlib_counter_t * result)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700290{
Neale Ranns1bd01092017-03-15 15:41:17 -0400291 vlib_counter_t *my_counters, *counter;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700292 int i;
293
294 result->packets = 0;
295 result->bytes = 0;
296
Neale Ranns1bd01092017-03-15 15:41:17 -0400297 for (i = 0; i < vec_len (cm->counters); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700298 {
Neale Ranns1bd01092017-03-15 15:41:17 -0400299 my_counters = cm->counters[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700300
Neale Ranns1bd01092017-03-15 15:41:17 -0400301 counter = vec_elt_at_index (my_counters, index);
302 result->packets += counter->packets;
303 result->bytes += counter->bytes;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700304 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700305}
306
Dave Barach63539202016-08-11 17:21:02 -0400307/** Clear a combined counter
Neale Ranns1bd01092017-03-15 15:41:17 -0400308 Clears the set of per-thread counters.
Dave Barach63539202016-08-11 17:21:02 -0400309
310 @param cm - (vlib_combined_counter_main_t *) combined counter main pointer
311 @param index - (u32) index of the counter to clear
312*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700313always_inline void
Dave Barach9b8ffd92016-07-08 08:13:45 -0400314vlib_zero_combined_counter (vlib_combined_counter_main_t * cm, u32 index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700315{
Neale Ranns1bd01092017-03-15 15:41:17 -0400316 vlib_counter_t *my_counters, *counter;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700317 int i;
318
Neale Ranns1bd01092017-03-15 15:41:17 -0400319 for (i = 0; i < vec_len (cm->counters); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700320 {
Neale Ranns1bd01092017-03-15 15:41:17 -0400321 my_counters = cm->counters[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700322
Neale Ranns1bd01092017-03-15 15:41:17 -0400323 counter = vec_elt_at_index (my_counters, index);
324 counter->packets = 0;
325 counter->bytes = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700326 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700327}
328
Dave Barach63539202016-08-11 17:21:02 -0400329/** validate a simple counter
330 @param cm - (vlib_simple_counter_main_t *) pointer to the counter collection
331 @param index - (u32) index of the counter to validate
332*/
333
Dave Barach9b8ffd92016-07-08 08:13:45 -0400334void vlib_validate_simple_counter (vlib_simple_counter_main_t * cm,
335 u32 index);
Ole Troana568a192020-04-21 17:54:41 +0200336void vlib_free_simple_counter (vlib_simple_counter_main_t * cm);
337
Dave Barach63539202016-08-11 17:21:02 -0400338/** validate a combined counter
339 @param cm - (vlib_combined_counter_main_t *) pointer to the counter
340 collection
341 @param index - (u32) index of the counter to validate
342*/
343
Dave Barach9b8ffd92016-07-08 08:13:45 -0400344void vlib_validate_combined_counter (vlib_combined_counter_main_t * cm,
345 u32 index);
Dave Barach8341f762020-06-03 08:05:15 -0400346int vlib_validate_combined_counter_will_expand
347 (vlib_combined_counter_main_t * cm, u32 index);
348
Ole Troana568a192020-04-21 17:54:41 +0200349void vlib_free_combined_counter (vlib_combined_counter_main_t * cm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700350
Dave Barach63539202016-08-11 17:21:02 -0400351/** Obtain the number of simple or combined counters allocated.
352 A macro which reduces to to vec_len(cm->maxi), the answer in either
353 case.
354
355 @param cm - (vlib_simple_counter_main_t) or
356 (vlib_combined_counter_main_t) the counter collection to interrogate
357 @returns vec_len(cm->maxi)
358*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700359#define vlib_counter_len(cm) vec_len((cm)->maxi)
360
Ed Warnickecb9cada2015-12-08 15:45:58 -0700361#endif /* included_vlib_counter_h */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400362
363/*
364 * fd.io coding-style-patch-verification: ON
365 *
366 * Local Variables:
367 * eval: (c-set-style "gnu")
368 * End:
369 */