blob: 01aefd8ebc081766fc6ad89a3abbe49d0b285e00 [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
Dave Barach63539202016-08-11 17:21:02 -040067/** Increment a simple counter
68 @param cm - (vlib_simple_counter_main_t *) simple counter main pointer
Damjan Marion586afd72017-04-05 19:18:20 +020069 @param thread_index - (u32) the current cpu index
Dave Barach63539202016-08-11 17:21:02 -040070 @param index - (u32) index of the counter to increment
Neale Ranns1bd01092017-03-15 15:41:17 -040071 @param increment - (u64) quantitiy to add to the counter
Dave Barach63539202016-08-11 17:21:02 -040072*/
Ed Warnickecb9cada2015-12-08 15:45:58 -070073always_inline void
74vlib_increment_simple_counter (vlib_simple_counter_main_t * cm,
Damjan Marion586afd72017-04-05 19:18:20 +020075 u32 thread_index, u32 index, u64 increment)
Ed Warnickecb9cada2015-12-08 15:45:58 -070076{
Neale Ranns1bd01092017-03-15 15:41:17 -040077 counter_t *my_counters;
Ed Warnickecb9cada2015-12-08 15:45:58 -070078
Damjan Marion586afd72017-04-05 19:18:20 +020079 my_counters = cm->counters[thread_index];
Neale Ranns1bd01092017-03-15 15:41:17 -040080 my_counters[index] += increment;
Ed Warnickecb9cada2015-12-08 15:45:58 -070081}
82
Sergey Matov34262d42020-09-15 13:40:55 +040083/** Decrement a simple counter
84 @param cm - (vlib_simple_counter_main_t *) simple counter main pointer
85 @param thread_index - (u32) the current cpu index
86 @param index - (u32) index of the counter to increment
87 @param increment - (u64) quantitiy remove from the counter value
88*/
89always_inline void
90vlib_decrement_simple_counter (vlib_simple_counter_main_t * cm,
91 u32 thread_index, u32 index, u64 decrement)
92{
93 counter_t *my_counters;
94
95 my_counters = cm->counters[thread_index];
96
97 ASSERT (my_counters[index] >= decrement);
98
99 my_counters[index] -= decrement;
100}
101
Matus Fabianb0055c82018-12-17 05:29:28 -0800102/** Set a simple counter
103 @param cm - (vlib_simple_counter_main_t *) simple counter main pointer
104 @param thread_index - (u32) the current cpu index
105 @param index - (u32) index of the counter to increment
106 @param value - (u64) quantitiy to set to the counter
107*/
108always_inline void
109vlib_set_simple_counter (vlib_simple_counter_main_t * cm,
110 u32 thread_index, u32 index, u64 value)
111{
112 counter_t *my_counters;
113
114 my_counters = cm->counters[thread_index];
115 my_counters[index] = value;
116}
117
Dave Barach63539202016-08-11 17:21:02 -0400118/** Get the value of a simple counter
Neale Ranns1bd01092017-03-15 15:41:17 -0400119 Scrapes the entire set of per-thread counters. Innacurate unless
Dave Barach63539202016-08-11 17:21:02 -0400120 worker threads which might increment the counter are
121 barrier-synchronized
122
123 @param cm - (vlib_simple_counter_main_t *) simple counter main pointer
124 @param index - (u32) index of the counter to fetch
125 @returns - (u64) current counter value
126*/
Neale Ranns1bd01092017-03-15 15:41:17 -0400127always_inline counter_t
Ed Warnickecb9cada2015-12-08 15:45:58 -0700128vlib_get_simple_counter (vlib_simple_counter_main_t * cm, u32 index)
129{
Neale Ranns1bd01092017-03-15 15:41:17 -0400130 counter_t *my_counters;
131 counter_t v;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700132 int i;
133
Neale Ranns1bd01092017-03-15 15:41:17 -0400134 ASSERT (index < vlib_simple_counter_n_counters (cm));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135
136 v = 0;
137
Neale Ranns1bd01092017-03-15 15:41:17 -0400138 for (i = 0; i < vec_len (cm->counters); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700139 {
Neale Ranns1bd01092017-03-15 15:41:17 -0400140 my_counters = cm->counters[i];
141 v += my_counters[index];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700142 }
143
144 return v;
145}
146
Dave Barach63539202016-08-11 17:21:02 -0400147/** Clear a simple counter
148 Clears the set of per-thread u16 counters, and the u64 counter
149
150 @param cm - (vlib_simple_counter_main_t *) simple counter main pointer
151 @param index - (u32) index of the counter to clear
152*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700153always_inline void
154vlib_zero_simple_counter (vlib_simple_counter_main_t * cm, u32 index)
155{
Neale Ranns1bd01092017-03-15 15:41:17 -0400156 counter_t *my_counters;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157 int i;
158
Neale Ranns1bd01092017-03-15 15:41:17 -0400159 ASSERT (index < vlib_simple_counter_n_counters (cm));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700160
Neale Ranns1bd01092017-03-15 15:41:17 -0400161 for (i = 0; i < vec_len (cm->counters); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162 {
Neale Ranns1bd01092017-03-15 15:41:17 -0400163 my_counters = cm->counters[i];
164 my_counters[index] = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166}
167
Dave Barach63539202016-08-11 17:21:02 -0400168/** Add two combined counters, results in the first counter
Chris Luked4024f52016-09-06 09:32:36 -0400169 @param [in,out] a - (vlib_counter_t *) dst counter
Dave Barach63539202016-08-11 17:21:02 -0400170 @param b - (vlib_counter_t *) src counter
171*/
172
Ed Warnickecb9cada2015-12-08 15:45:58 -0700173always_inline void
174vlib_counter_add (vlib_counter_t * a, vlib_counter_t * b)
175{
176 a->packets += b->packets;
177 a->bytes += b->bytes;
178}
179
Dave Barach63539202016-08-11 17:21:02 -0400180/** Subtract combined counters, results in the first counter
Chris Luked4024f52016-09-06 09:32:36 -0400181 @param [in,out] a - (vlib_counter_t *) dst counter
Dave Barach63539202016-08-11 17:21:02 -0400182 @param b - (vlib_counter_t *) src counter
183*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700184always_inline void
185vlib_counter_sub (vlib_counter_t * a, vlib_counter_t * b)
186{
187 ASSERT (a->packets >= b->packets);
188 ASSERT (a->bytes >= b->bytes);
189 a->packets -= b->packets;
190 a->bytes -= b->bytes;
191}
192
Dave Barach63539202016-08-11 17:21:02 -0400193/** Clear a combined counter
194 @param a - (vlib_counter_t *) counter to clear
195*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700196always_inline void
197vlib_counter_zero (vlib_counter_t * a)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400198{
199 a->packets = a->bytes = 0;
200}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700201
Dave Barach63539202016-08-11 17:21:02 -0400202/** A collection of combined counters */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400203typedef struct
204{
Neale Ranns1bd01092017-03-15 15:41:17 -0400205 vlib_counter_t **counters; /**< Per-thread u64 non-atomic counter pairs */
Dave Barach63539202016-08-11 17:21:02 -0400206 char *name; /**< The counter collection's name. */
Dave Barach048a4e52018-06-01 18:52:25 -0400207 char *stat_segment_name; /**< Name in stat segment directory */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700208} vlib_combined_counter_main_t;
209
Neale Ranns1bd01092017-03-15 15:41:17 -0400210/** The number of counters (not the number of per-thread counters) */
211u32 vlib_combined_counter_n_counters (const vlib_combined_counter_main_t *
212 cm);
213
Dave Barach63539202016-08-11 17:21:02 -0400214/** Clear a collection of simple counters
215 @param cm - (vlib_simple_counter_main_t *) collection to clear
216*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700217void vlib_clear_simple_counters (vlib_simple_counter_main_t * cm);
Dave Barach63539202016-08-11 17:21:02 -0400218
219/** Clear a collection of combined counters
220 @param cm - (vlib_combined_counter_main_t *) collection to clear
221*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700222void vlib_clear_combined_counters (vlib_combined_counter_main_t * cm);
223
Dave Barach63539202016-08-11 17:21:02 -0400224/** Increment a combined counter
225 @param cm - (vlib_combined_counter_main_t *) comined counter main pointer
Damjan Marion586afd72017-04-05 19:18:20 +0200226 @param thread_index - (u32) the current cpu index
Dave Barach63539202016-08-11 17:21:02 -0400227 @param index - (u32) index of the counter to increment
Neale Ranns1bd01092017-03-15 15:41:17 -0400228 @param packet_increment - (u64) number of packets to add to the counter
229 @param byte_increment - (u64) number of bytes to add to the counter
Dave Barach63539202016-08-11 17:21:02 -0400230*/
231
Ed Warnickecb9cada2015-12-08 15:45:58 -0700232always_inline void
233vlib_increment_combined_counter (vlib_combined_counter_main_t * cm,
Damjan Marion586afd72017-04-05 19:18:20 +0200234 u32 thread_index,
Neale Ranns1bd01092017-03-15 15:41:17 -0400235 u32 index, u64 n_packets, u64 n_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700236{
Neale Ranns1bd01092017-03-15 15:41:17 -0400237 vlib_counter_t *my_counters;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700238
Neale Ranns1bd01092017-03-15 15:41:17 -0400239 /* Use this CPU's counter array */
Damjan Marion586afd72017-04-05 19:18:20 +0200240 my_counters = cm->counters[thread_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241
Neale Ranns1bd01092017-03-15 15:41:17 -0400242 my_counters[index].packets += n_packets;
243 my_counters[index].bytes += n_bytes;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700244}
245
Neale Ranns1bd01092017-03-15 15:41:17 -0400246/** Pre-fetch a per-thread combined counter for the given object index */
247always_inline void
248vlib_prefetch_combined_counter (const vlib_combined_counter_main_t * cm,
Damjan Marion586afd72017-04-05 19:18:20 +0200249 u32 thread_index, u32 index)
Neale Ranns1bd01092017-03-15 15:41:17 -0400250{
251 vlib_counter_t *cpu_counters;
252
253 /*
254 * This CPU's index is assumed to already be in cache
255 */
Damjan Marion586afd72017-04-05 19:18:20 +0200256 cpu_counters = cm->counters[thread_index];
Neale Ranns1bd01092017-03-15 15:41:17 -0400257 CLIB_PREFETCH (cpu_counters + index, CLIB_CACHE_LINE_BYTES, STORE);
Neale Ranns044183f2017-01-24 01:34:25 -0800258}
259
260
Dave Barach63539202016-08-11 17:21:02 -0400261/** Get the value of a combined counter, never called in the speed path
Neale Ranns1bd01092017-03-15 15:41:17 -0400262 Scrapes the entire set of per-thread counters. Innacurate unless
Dave Barach63539202016-08-11 17:21:02 -0400263 worker threads which might increment the counter are
264 barrier-synchronized
265
266 @param cm - (vlib_combined_counter_main_t *) combined counter main pointer
267 @param index - (u32) index of the combined counter to fetch
268 @param result [out] - (vlib_counter_t *) result stored here
269*/
270
Ed Warnickecb9cada2015-12-08 15:45:58 -0700271static inline void
Neale Ranns1bd01092017-03-15 15:41:17 -0400272vlib_get_combined_counter (const vlib_combined_counter_main_t * cm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400273 u32 index, vlib_counter_t * result)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700274{
Neale Ranns1bd01092017-03-15 15:41:17 -0400275 vlib_counter_t *my_counters, *counter;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700276 int i;
277
278 result->packets = 0;
279 result->bytes = 0;
280
Neale Ranns1bd01092017-03-15 15:41:17 -0400281 for (i = 0; i < vec_len (cm->counters); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700282 {
Neale Ranns1bd01092017-03-15 15:41:17 -0400283 my_counters = cm->counters[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700284
Neale Ranns1bd01092017-03-15 15:41:17 -0400285 counter = vec_elt_at_index (my_counters, index);
286 result->packets += counter->packets;
287 result->bytes += counter->bytes;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700288 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700289}
290
Dave Barach63539202016-08-11 17:21:02 -0400291/** Clear a combined counter
Neale Ranns1bd01092017-03-15 15:41:17 -0400292 Clears the set of per-thread counters.
Dave Barach63539202016-08-11 17:21:02 -0400293
294 @param cm - (vlib_combined_counter_main_t *) combined counter main pointer
295 @param index - (u32) index of the counter to clear
296*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700297always_inline void
Dave Barach9b8ffd92016-07-08 08:13:45 -0400298vlib_zero_combined_counter (vlib_combined_counter_main_t * cm, u32 index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700299{
Neale Ranns1bd01092017-03-15 15:41:17 -0400300 vlib_counter_t *my_counters, *counter;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700301 int i;
302
Neale Ranns1bd01092017-03-15 15:41:17 -0400303 for (i = 0; i < vec_len (cm->counters); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700304 {
Neale Ranns1bd01092017-03-15 15:41:17 -0400305 my_counters = cm->counters[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700306
Neale Ranns1bd01092017-03-15 15:41:17 -0400307 counter = vec_elt_at_index (my_counters, index);
308 counter->packets = 0;
309 counter->bytes = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700310 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700311}
312
Dave Barach63539202016-08-11 17:21:02 -0400313/** validate a simple counter
314 @param cm - (vlib_simple_counter_main_t *) pointer to the counter collection
315 @param index - (u32) index of the counter to validate
316*/
317
Dave Barach9b8ffd92016-07-08 08:13:45 -0400318void vlib_validate_simple_counter (vlib_simple_counter_main_t * cm,
319 u32 index);
Ole Troana568a192020-04-21 17:54:41 +0200320void vlib_free_simple_counter (vlib_simple_counter_main_t * cm);
321
Dave Barach63539202016-08-11 17:21:02 -0400322/** validate a combined counter
323 @param cm - (vlib_combined_counter_main_t *) pointer to the counter
324 collection
325 @param index - (u32) index of the counter to validate
326*/
327
Dave Barach9b8ffd92016-07-08 08:13:45 -0400328void vlib_validate_combined_counter (vlib_combined_counter_main_t * cm,
329 u32 index);
Dave Barach8341f762020-06-03 08:05:15 -0400330int vlib_validate_combined_counter_will_expand
331 (vlib_combined_counter_main_t * cm, u32 index);
332
Ole Troana568a192020-04-21 17:54:41 +0200333void vlib_free_combined_counter (vlib_combined_counter_main_t * cm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700334
Dave Barach63539202016-08-11 17:21:02 -0400335/** Obtain the number of simple or combined counters allocated.
336 A macro which reduces to to vec_len(cm->maxi), the answer in either
337 case.
338
339 @param cm - (vlib_simple_counter_main_t) or
340 (vlib_combined_counter_main_t) the counter collection to interrogate
341 @returns vec_len(cm->maxi)
342*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700343#define vlib_counter_len(cm) vec_len((cm)->maxi)
344
Ed Warnickecb9cada2015-12-08 15:45:58 -0700345#endif /* included_vlib_counter_h */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400346
347/*
348 * fd.io coding-style-patch-verification: ON
349 *
350 * Local Variables:
351 * eval: (c-set-style "gnu")
352 * End:
353 */