blob: 56701e8b3917c7b7e6a784efb5c288ad1041b8ed [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 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070062} vlib_simple_counter_main_t;
63
Neale Ranns1bd01092017-03-15 15:41:17 -040064/** The number of counters (not the number of per-thread counters) */
65u32 vlib_simple_counter_n_counters (const vlib_simple_counter_main_t * cm);
66
Neale Rannse11203e2021-09-21 12:34:19 +000067/** Pre-fetch a per-thread simple counter for the given object index */
68always_inline void
69vlib_prefetch_simple_counter (const vlib_simple_counter_main_t *cm,
70 u32 thread_index, u32 index)
71{
72 counter_t *my_counters;
73
74 /*
75 * This CPU's index is assumed to already be in cache
76 */
77 my_counters = cm->counters[thread_index];
78 clib_prefetch_store (my_counters + index);
79}
80
Dave Barach63539202016-08-11 17:21:02 -040081/** Increment a simple counter
82 @param cm - (vlib_simple_counter_main_t *) simple counter main pointer
Damjan Marion586afd72017-04-05 19:18:20 +020083 @param thread_index - (u32) the current cpu index
Dave Barach63539202016-08-11 17:21:02 -040084 @param index - (u32) index of the counter to increment
Neale Ranns1bd01092017-03-15 15:41:17 -040085 @param increment - (u64) quantitiy to add to the counter
Dave Barach63539202016-08-11 17:21:02 -040086*/
Ed Warnickecb9cada2015-12-08 15:45:58 -070087always_inline void
88vlib_increment_simple_counter (vlib_simple_counter_main_t * cm,
Damjan Marion586afd72017-04-05 19:18:20 +020089 u32 thread_index, u32 index, u64 increment)
Ed Warnickecb9cada2015-12-08 15:45:58 -070090{
Neale Ranns1bd01092017-03-15 15:41:17 -040091 counter_t *my_counters;
Ed Warnickecb9cada2015-12-08 15:45:58 -070092
Damjan Marion586afd72017-04-05 19:18:20 +020093 my_counters = cm->counters[thread_index];
Neale Ranns1bd01092017-03-15 15:41:17 -040094 my_counters[index] += increment;
Ed Warnickecb9cada2015-12-08 15:45:58 -070095}
96
Sergey Matov34262d42020-09-15 13:40:55 +040097/** Decrement a simple counter
98 @param cm - (vlib_simple_counter_main_t *) simple counter main pointer
99 @param thread_index - (u32) the current cpu index
100 @param index - (u32) index of the counter to increment
101 @param increment - (u64) quantitiy remove from the counter value
102*/
103always_inline void
104vlib_decrement_simple_counter (vlib_simple_counter_main_t * cm,
105 u32 thread_index, u32 index, u64 decrement)
106{
107 counter_t *my_counters;
108
109 my_counters = cm->counters[thread_index];
110
111 ASSERT (my_counters[index] >= decrement);
112
113 my_counters[index] -= decrement;
114}
115
Matus Fabianb0055c82018-12-17 05:29:28 -0800116/** Set a simple counter
117 @param cm - (vlib_simple_counter_main_t *) simple counter main pointer
118 @param thread_index - (u32) the current cpu index
119 @param index - (u32) index of the counter to increment
120 @param value - (u64) quantitiy to set to the counter
121*/
122always_inline void
123vlib_set_simple_counter (vlib_simple_counter_main_t * cm,
124 u32 thread_index, u32 index, u64 value)
125{
126 counter_t *my_counters;
127
128 my_counters = cm->counters[thread_index];
129 my_counters[index] = value;
130}
131
Dave Barach63539202016-08-11 17:21:02 -0400132/** Get the value of a simple counter
Neale Ranns1bd01092017-03-15 15:41:17 -0400133 Scrapes the entire set of per-thread counters. Innacurate unless
Dave Barach63539202016-08-11 17:21:02 -0400134 worker threads which might increment the counter are
135 barrier-synchronized
136
137 @param cm - (vlib_simple_counter_main_t *) simple counter main pointer
138 @param index - (u32) index of the counter to fetch
139 @returns - (u64) current counter value
140*/
Neale Ranns1bd01092017-03-15 15:41:17 -0400141always_inline counter_t
Ed Warnickecb9cada2015-12-08 15:45:58 -0700142vlib_get_simple_counter (vlib_simple_counter_main_t * cm, u32 index)
143{
Neale Ranns1bd01092017-03-15 15:41:17 -0400144 counter_t *my_counters;
145 counter_t v;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700146 int i;
147
Neale Ranns1bd01092017-03-15 15:41:17 -0400148 ASSERT (index < vlib_simple_counter_n_counters (cm));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149
150 v = 0;
151
Neale Ranns1bd01092017-03-15 15:41:17 -0400152 for (i = 0; i < vec_len (cm->counters); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700153 {
Neale Ranns1bd01092017-03-15 15:41:17 -0400154 my_counters = cm->counters[i];
155 v += my_counters[index];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700156 }
157
158 return v;
159}
160
Dave Barach63539202016-08-11 17:21:02 -0400161/** Clear a simple counter
162 Clears the set of per-thread u16 counters, and the u64 counter
163
164 @param cm - (vlib_simple_counter_main_t *) simple counter main pointer
165 @param index - (u32) index of the counter to clear
166*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700167always_inline void
168vlib_zero_simple_counter (vlib_simple_counter_main_t * cm, u32 index)
169{
Neale Ranns1bd01092017-03-15 15:41:17 -0400170 counter_t *my_counters;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700171 int i;
172
Neale Ranns1bd01092017-03-15 15:41:17 -0400173 ASSERT (index < vlib_simple_counter_n_counters (cm));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700174
Neale Ranns1bd01092017-03-15 15:41:17 -0400175 for (i = 0; i < vec_len (cm->counters); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700176 {
Neale Ranns1bd01092017-03-15 15:41:17 -0400177 my_counters = cm->counters[i];
178 my_counters[index] = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700180}
181
Dave Barach63539202016-08-11 17:21:02 -0400182/** Add two combined counters, results in the first counter
Chris Luked4024f52016-09-06 09:32:36 -0400183 @param [in,out] a - (vlib_counter_t *) dst counter
Dave Barach63539202016-08-11 17:21:02 -0400184 @param b - (vlib_counter_t *) src counter
185*/
186
Ed Warnickecb9cada2015-12-08 15:45:58 -0700187always_inline void
188vlib_counter_add (vlib_counter_t * a, vlib_counter_t * b)
189{
190 a->packets += b->packets;
191 a->bytes += b->bytes;
192}
193
Dave Barach63539202016-08-11 17:21:02 -0400194/** Subtract combined counters, results in the first counter
Chris Luked4024f52016-09-06 09:32:36 -0400195 @param [in,out] a - (vlib_counter_t *) dst counter
Dave Barach63539202016-08-11 17:21:02 -0400196 @param b - (vlib_counter_t *) src counter
197*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700198always_inline void
199vlib_counter_sub (vlib_counter_t * a, vlib_counter_t * b)
200{
201 ASSERT (a->packets >= b->packets);
202 ASSERT (a->bytes >= b->bytes);
203 a->packets -= b->packets;
204 a->bytes -= b->bytes;
205}
206
Dave Barach63539202016-08-11 17:21:02 -0400207/** Clear a combined counter
208 @param a - (vlib_counter_t *) counter to clear
209*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700210always_inline void
211vlib_counter_zero (vlib_counter_t * a)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400212{
213 a->packets = a->bytes = 0;
214}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700215
Dave Barach63539202016-08-11 17:21:02 -0400216/** A collection of combined counters */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400217typedef struct
218{
Neale Ranns1bd01092017-03-15 15:41:17 -0400219 vlib_counter_t **counters; /**< Per-thread u64 non-atomic counter pairs */
Dave Barach63539202016-08-11 17:21:02 -0400220 char *name; /**< The counter collection's name. */
Dave Barach048a4e52018-06-01 18:52:25 -0400221 char *stat_segment_name; /**< Name in stat segment directory */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700222} vlib_combined_counter_main_t;
223
Neale Ranns1bd01092017-03-15 15:41:17 -0400224/** The number of counters (not the number of per-thread counters) */
225u32 vlib_combined_counter_n_counters (const vlib_combined_counter_main_t *
226 cm);
227
Dave Barach63539202016-08-11 17:21:02 -0400228/** Clear a collection of simple counters
229 @param cm - (vlib_simple_counter_main_t *) collection to clear
230*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700231void vlib_clear_simple_counters (vlib_simple_counter_main_t * cm);
Dave Barach63539202016-08-11 17:21:02 -0400232
233/** Clear a collection of combined counters
234 @param cm - (vlib_combined_counter_main_t *) collection to clear
235*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700236void vlib_clear_combined_counters (vlib_combined_counter_main_t * cm);
237
Dave Barach63539202016-08-11 17:21:02 -0400238/** Increment a combined counter
239 @param cm - (vlib_combined_counter_main_t *) comined counter main pointer
Damjan Marion586afd72017-04-05 19:18:20 +0200240 @param thread_index - (u32) the current cpu index
Dave Barach63539202016-08-11 17:21:02 -0400241 @param index - (u32) index of the counter to increment
Neale Ranns1bd01092017-03-15 15:41:17 -0400242 @param packet_increment - (u64) number of packets to add to the counter
243 @param byte_increment - (u64) number of bytes to add to the counter
Dave Barach63539202016-08-11 17:21:02 -0400244*/
245
Ed Warnickecb9cada2015-12-08 15:45:58 -0700246always_inline void
247vlib_increment_combined_counter (vlib_combined_counter_main_t * cm,
Damjan Marion586afd72017-04-05 19:18:20 +0200248 u32 thread_index,
Neale Ranns1bd01092017-03-15 15:41:17 -0400249 u32 index, u64 n_packets, u64 n_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700250{
Neale Ranns1bd01092017-03-15 15:41:17 -0400251 vlib_counter_t *my_counters;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700252
Neale Ranns1bd01092017-03-15 15:41:17 -0400253 /* Use this CPU's counter array */
Damjan Marion586afd72017-04-05 19:18:20 +0200254 my_counters = cm->counters[thread_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700255
Neale Ranns1bd01092017-03-15 15:41:17 -0400256 my_counters[index].packets += n_packets;
257 my_counters[index].bytes += n_bytes;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700258}
259
Neale Ranns1bd01092017-03-15 15:41:17 -0400260/** Pre-fetch a per-thread combined counter for the given object index */
261always_inline void
262vlib_prefetch_combined_counter (const vlib_combined_counter_main_t * cm,
Damjan Marion586afd72017-04-05 19:18:20 +0200263 u32 thread_index, u32 index)
Neale Ranns1bd01092017-03-15 15:41:17 -0400264{
265 vlib_counter_t *cpu_counters;
266
267 /*
268 * This CPU's index is assumed to already be in cache
269 */
Damjan Marion586afd72017-04-05 19:18:20 +0200270 cpu_counters = cm->counters[thread_index];
Damjan Marionaf7fb042021-07-15 11:54:41 +0200271 clib_prefetch_store (cpu_counters + index);
Neale Ranns044183f2017-01-24 01:34:25 -0800272}
273
274
Dave Barach63539202016-08-11 17:21:02 -0400275/** Get the value of a combined counter, never called in the speed path
Neale Ranns1bd01092017-03-15 15:41:17 -0400276 Scrapes the entire set of per-thread counters. Innacurate unless
Dave Barach63539202016-08-11 17:21:02 -0400277 worker threads which might increment the counter are
278 barrier-synchronized
279
280 @param cm - (vlib_combined_counter_main_t *) combined counter main pointer
281 @param index - (u32) index of the combined counter to fetch
282 @param result [out] - (vlib_counter_t *) result stored here
283*/
284
Ed Warnickecb9cada2015-12-08 15:45:58 -0700285static inline void
Neale Ranns1bd01092017-03-15 15:41:17 -0400286vlib_get_combined_counter (const vlib_combined_counter_main_t * cm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400287 u32 index, vlib_counter_t * result)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700288{
Neale Ranns1bd01092017-03-15 15:41:17 -0400289 vlib_counter_t *my_counters, *counter;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700290 int i;
291
292 result->packets = 0;
293 result->bytes = 0;
294
Neale Ranns1bd01092017-03-15 15:41:17 -0400295 for (i = 0; i < vec_len (cm->counters); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700296 {
Neale Ranns1bd01092017-03-15 15:41:17 -0400297 my_counters = cm->counters[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700298
Neale Ranns1bd01092017-03-15 15:41:17 -0400299 counter = vec_elt_at_index (my_counters, index);
300 result->packets += counter->packets;
301 result->bytes += counter->bytes;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700302 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700303}
304
Dave Barach63539202016-08-11 17:21:02 -0400305/** Clear a combined counter
Neale Ranns1bd01092017-03-15 15:41:17 -0400306 Clears the set of per-thread counters.
Dave Barach63539202016-08-11 17:21:02 -0400307
308 @param cm - (vlib_combined_counter_main_t *) combined counter main pointer
309 @param index - (u32) index of the counter to clear
310*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700311always_inline void
Dave Barach9b8ffd92016-07-08 08:13:45 -0400312vlib_zero_combined_counter (vlib_combined_counter_main_t * cm, u32 index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700313{
Neale Ranns1bd01092017-03-15 15:41:17 -0400314 vlib_counter_t *my_counters, *counter;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700315 int i;
316
Neale Ranns1bd01092017-03-15 15:41:17 -0400317 for (i = 0; i < vec_len (cm->counters); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700318 {
Neale Ranns1bd01092017-03-15 15:41:17 -0400319 my_counters = cm->counters[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700320
Neale Ranns1bd01092017-03-15 15:41:17 -0400321 counter = vec_elt_at_index (my_counters, index);
322 counter->packets = 0;
323 counter->bytes = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700324 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700325}
326
Dave Barach63539202016-08-11 17:21:02 -0400327/** validate a simple counter
328 @param cm - (vlib_simple_counter_main_t *) pointer to the counter collection
329 @param index - (u32) index of the counter to validate
330*/
331
Dave Barach9b8ffd92016-07-08 08:13:45 -0400332void vlib_validate_simple_counter (vlib_simple_counter_main_t * cm,
333 u32 index);
Ole Troana568a192020-04-21 17:54:41 +0200334void vlib_free_simple_counter (vlib_simple_counter_main_t * cm);
335
Dave Barach63539202016-08-11 17:21:02 -0400336/** validate a combined counter
337 @param cm - (vlib_combined_counter_main_t *) pointer to the counter
338 collection
339 @param index - (u32) index of the counter to validate
340*/
341
Dave Barach9b8ffd92016-07-08 08:13:45 -0400342void vlib_validate_combined_counter (vlib_combined_counter_main_t * cm,
343 u32 index);
Dave Barach8341f762020-06-03 08:05:15 -0400344int vlib_validate_combined_counter_will_expand
345 (vlib_combined_counter_main_t * cm, u32 index);
346
Ole Troana568a192020-04-21 17:54:41 +0200347void vlib_free_combined_counter (vlib_combined_counter_main_t * cm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700348
Dave Barach63539202016-08-11 17:21:02 -0400349/** Obtain the number of simple or combined counters allocated.
350 A macro which reduces to to vec_len(cm->maxi), the answer in either
351 case.
352
353 @param cm - (vlib_simple_counter_main_t) or
354 (vlib_combined_counter_main_t) the counter collection to interrogate
355 @returns vec_len(cm->maxi)
356*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700357#define vlib_counter_len(cm) vec_len((cm)->maxi)
358
Ed Warnickecb9cada2015-12-08 15:45:58 -0700359#endif /* included_vlib_counter_h */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400360
361/*
362 * fd.io coding-style-patch-verification: ON
363 *
364 * Local Variables:
365 * eval: (c-set-style "gnu")
366 * End:
367 */