blob: 04ab83769cce163eacb860e858f9317731594749 [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 */
60 counter_t *value_at_last_serialize; /**< Values as of last serialize. */
Dave Barach63539202016-08-11 17:21:02 -040061 u32 last_incremental_serialize_index; /**< Last counter index
62 serialized incrementally. */
Ed Warnickecb9cada2015-12-08 15:45:58 -070063
Dave Barach63539202016-08-11 17:21:02 -040064 char *name; /**< The counter collection's name. */
Dave Barach048a4e52018-06-01 18:52:25 -040065 char *stat_segment_name; /**< Name in stat segment directory */
Ed Warnickecb9cada2015-12-08 15:45:58 -070066} vlib_simple_counter_main_t;
67
Neale Ranns1bd01092017-03-15 15:41:17 -040068/** The number of counters (not the number of per-thread counters) */
69u32 vlib_simple_counter_n_counters (const vlib_simple_counter_main_t * cm);
70
Dave Barach63539202016-08-11 17:21:02 -040071/** Increment a simple counter
72 @param cm - (vlib_simple_counter_main_t *) simple counter main pointer
Damjan Marion586afd72017-04-05 19:18:20 +020073 @param thread_index - (u32) the current cpu index
Dave Barach63539202016-08-11 17:21:02 -040074 @param index - (u32) index of the counter to increment
Neale Ranns1bd01092017-03-15 15:41:17 -040075 @param increment - (u64) quantitiy to add to the counter
Dave Barach63539202016-08-11 17:21:02 -040076*/
Ed Warnickecb9cada2015-12-08 15:45:58 -070077always_inline void
78vlib_increment_simple_counter (vlib_simple_counter_main_t * cm,
Damjan Marion586afd72017-04-05 19:18:20 +020079 u32 thread_index, u32 index, u64 increment)
Ed Warnickecb9cada2015-12-08 15:45:58 -070080{
Neale Ranns1bd01092017-03-15 15:41:17 -040081 counter_t *my_counters;
Ed Warnickecb9cada2015-12-08 15:45:58 -070082
Damjan Marion586afd72017-04-05 19:18:20 +020083 my_counters = cm->counters[thread_index];
Neale Ranns1bd01092017-03-15 15:41:17 -040084 my_counters[index] += increment;
Ed Warnickecb9cada2015-12-08 15:45:58 -070085}
86
Sergey Matov34262d42020-09-15 13:40:55 +040087/** 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*/
93always_inline void
94vlib_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
Matus Fabianb0055c82018-12-17 05:29:28 -0800106/** 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*/
112always_inline void
113vlib_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
Dave Barach63539202016-08-11 17:21:02 -0400122/** Get the value of a simple counter
Neale Ranns1bd01092017-03-15 15:41:17 -0400123 Scrapes the entire set of per-thread counters. Innacurate unless
Dave Barach63539202016-08-11 17:21:02 -0400124 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*/
Neale Ranns1bd01092017-03-15 15:41:17 -0400131always_inline counter_t
Ed Warnickecb9cada2015-12-08 15:45:58 -0700132vlib_get_simple_counter (vlib_simple_counter_main_t * cm, u32 index)
133{
Neale Ranns1bd01092017-03-15 15:41:17 -0400134 counter_t *my_counters;
135 counter_t v;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700136 int i;
137
Neale Ranns1bd01092017-03-15 15:41:17 -0400138 ASSERT (index < vlib_simple_counter_n_counters (cm));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700139
140 v = 0;
141
Neale Ranns1bd01092017-03-15 15:41:17 -0400142 for (i = 0; i < vec_len (cm->counters); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143 {
Neale Ranns1bd01092017-03-15 15:41:17 -0400144 my_counters = cm->counters[i];
145 v += my_counters[index];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700146 }
147
148 return v;
149}
150
Dave Barach63539202016-08-11 17:21:02 -0400151/** 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*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157always_inline void
158vlib_zero_simple_counter (vlib_simple_counter_main_t * cm, u32 index)
159{
Neale Ranns1bd01092017-03-15 15:41:17 -0400160 counter_t *my_counters;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161 int i;
162
Neale Ranns1bd01092017-03-15 15:41:17 -0400163 ASSERT (index < vlib_simple_counter_n_counters (cm));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700164
Neale Ranns1bd01092017-03-15 15:41:17 -0400165 for (i = 0; i < vec_len (cm->counters); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166 {
Neale Ranns1bd01092017-03-15 15:41:17 -0400167 my_counters = cm->counters[i];
168 my_counters[index] = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700169 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700170}
171
Dave Barach63539202016-08-11 17:21:02 -0400172/** Add two combined counters, results in the first counter
Chris Luked4024f52016-09-06 09:32:36 -0400173 @param [in,out] a - (vlib_counter_t *) dst counter
Dave Barach63539202016-08-11 17:21:02 -0400174 @param b - (vlib_counter_t *) src counter
175*/
176
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177always_inline void
178vlib_counter_add (vlib_counter_t * a, vlib_counter_t * b)
179{
180 a->packets += b->packets;
181 a->bytes += b->bytes;
182}
183
Dave Barach63539202016-08-11 17:21:02 -0400184/** Subtract combined counters, results in the first counter
Chris Luked4024f52016-09-06 09:32:36 -0400185 @param [in,out] a - (vlib_counter_t *) dst counter
Dave Barach63539202016-08-11 17:21:02 -0400186 @param b - (vlib_counter_t *) src counter
187*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700188always_inline void
189vlib_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
Dave Barach63539202016-08-11 17:21:02 -0400197/** Clear a combined counter
198 @param a - (vlib_counter_t *) counter to clear
199*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700200always_inline void
201vlib_counter_zero (vlib_counter_t * a)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400202{
203 a->packets = a->bytes = 0;
204}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700205
Dave Barach63539202016-08-11 17:21:02 -0400206/** A collection of combined counters */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400207typedef struct
208{
Neale Ranns1bd01092017-03-15 15:41:17 -0400209 vlib_counter_t **counters; /**< Per-thread u64 non-atomic counter pairs */
Dave Barach63539202016-08-11 17:21:02 -0400210 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. */
Dave Barach048a4e52018-06-01 18:52:25 -0400213 char *stat_segment_name; /**< Name in stat segment directory */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700214} vlib_combined_counter_main_t;
215
Neale Ranns1bd01092017-03-15 15:41:17 -0400216/** The number of counters (not the number of per-thread counters) */
217u32 vlib_combined_counter_n_counters (const vlib_combined_counter_main_t *
218 cm);
219
Dave Barach63539202016-08-11 17:21:02 -0400220/** Clear a collection of simple counters
221 @param cm - (vlib_simple_counter_main_t *) collection to clear
222*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700223void vlib_clear_simple_counters (vlib_simple_counter_main_t * cm);
Dave Barach63539202016-08-11 17:21:02 -0400224
225/** Clear a collection of combined counters
226 @param cm - (vlib_combined_counter_main_t *) collection to clear
227*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700228void vlib_clear_combined_counters (vlib_combined_counter_main_t * cm);
229
Dave Barach63539202016-08-11 17:21:02 -0400230/** Increment a combined counter
231 @param cm - (vlib_combined_counter_main_t *) comined counter main pointer
Damjan Marion586afd72017-04-05 19:18:20 +0200232 @param thread_index - (u32) the current cpu index
Dave Barach63539202016-08-11 17:21:02 -0400233 @param index - (u32) index of the counter to increment
Neale Ranns1bd01092017-03-15 15:41:17 -0400234 @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
Dave Barach63539202016-08-11 17:21:02 -0400236*/
237
Ed Warnickecb9cada2015-12-08 15:45:58 -0700238always_inline void
239vlib_increment_combined_counter (vlib_combined_counter_main_t * cm,
Damjan Marion586afd72017-04-05 19:18:20 +0200240 u32 thread_index,
Neale Ranns1bd01092017-03-15 15:41:17 -0400241 u32 index, u64 n_packets, u64 n_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700242{
Neale Ranns1bd01092017-03-15 15:41:17 -0400243 vlib_counter_t *my_counters;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700244
Neale Ranns1bd01092017-03-15 15:41:17 -0400245 /* Use this CPU's counter array */
Damjan Marion586afd72017-04-05 19:18:20 +0200246 my_counters = cm->counters[thread_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247
Neale Ranns1bd01092017-03-15 15:41:17 -0400248 my_counters[index].packets += n_packets;
249 my_counters[index].bytes += n_bytes;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700250}
251
Neale Ranns1bd01092017-03-15 15:41:17 -0400252/** Pre-fetch a per-thread combined counter for the given object index */
253always_inline void
254vlib_prefetch_combined_counter (const vlib_combined_counter_main_t * cm,
Damjan Marion586afd72017-04-05 19:18:20 +0200255 u32 thread_index, u32 index)
Neale Ranns1bd01092017-03-15 15:41:17 -0400256{
257 vlib_counter_t *cpu_counters;
258
259 /*
260 * This CPU's index is assumed to already be in cache
261 */
Damjan Marion586afd72017-04-05 19:18:20 +0200262 cpu_counters = cm->counters[thread_index];
Neale Ranns1bd01092017-03-15 15:41:17 -0400263 CLIB_PREFETCH (cpu_counters + index, CLIB_CACHE_LINE_BYTES, STORE);
Neale Ranns044183f2017-01-24 01:34:25 -0800264}
265
266
Dave Barach63539202016-08-11 17:21:02 -0400267/** Get the value of a combined counter, never called in the speed path
Neale Ranns1bd01092017-03-15 15:41:17 -0400268 Scrapes the entire set of per-thread counters. Innacurate unless
Dave Barach63539202016-08-11 17:21:02 -0400269 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
Ed Warnickecb9cada2015-12-08 15:45:58 -0700277static inline void
Neale Ranns1bd01092017-03-15 15:41:17 -0400278vlib_get_combined_counter (const vlib_combined_counter_main_t * cm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400279 u32 index, vlib_counter_t * result)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700280{
Neale Ranns1bd01092017-03-15 15:41:17 -0400281 vlib_counter_t *my_counters, *counter;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700282 int i;
283
284 result->packets = 0;
285 result->bytes = 0;
286
Neale Ranns1bd01092017-03-15 15:41:17 -0400287 for (i = 0; i < vec_len (cm->counters); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700288 {
Neale Ranns1bd01092017-03-15 15:41:17 -0400289 my_counters = cm->counters[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700290
Neale Ranns1bd01092017-03-15 15:41:17 -0400291 counter = vec_elt_at_index (my_counters, index);
292 result->packets += counter->packets;
293 result->bytes += counter->bytes;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700294 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700295}
296
Dave Barach63539202016-08-11 17:21:02 -0400297/** Clear a combined counter
Neale Ranns1bd01092017-03-15 15:41:17 -0400298 Clears the set of per-thread counters.
Dave Barach63539202016-08-11 17:21:02 -0400299
300 @param cm - (vlib_combined_counter_main_t *) combined counter main pointer
301 @param index - (u32) index of the counter to clear
302*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700303always_inline void
Dave Barach9b8ffd92016-07-08 08:13:45 -0400304vlib_zero_combined_counter (vlib_combined_counter_main_t * cm, u32 index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700305{
Neale Ranns1bd01092017-03-15 15:41:17 -0400306 vlib_counter_t *my_counters, *counter;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700307 int i;
308
Neale Ranns1bd01092017-03-15 15:41:17 -0400309 for (i = 0; i < vec_len (cm->counters); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700310 {
Neale Ranns1bd01092017-03-15 15:41:17 -0400311 my_counters = cm->counters[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700312
Neale Ranns1bd01092017-03-15 15:41:17 -0400313 counter = vec_elt_at_index (my_counters, index);
314 counter->packets = 0;
315 counter->bytes = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700316 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700317}
318
Dave Barach63539202016-08-11 17:21:02 -0400319/** 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
Dave Barach9b8ffd92016-07-08 08:13:45 -0400324void vlib_validate_simple_counter (vlib_simple_counter_main_t * cm,
325 u32 index);
Ole Troana568a192020-04-21 17:54:41 +0200326void vlib_free_simple_counter (vlib_simple_counter_main_t * cm);
327
Dave Barach63539202016-08-11 17:21:02 -0400328/** 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
Dave Barach9b8ffd92016-07-08 08:13:45 -0400334void vlib_validate_combined_counter (vlib_combined_counter_main_t * cm,
335 u32 index);
Dave Barach8341f762020-06-03 08:05:15 -0400336int vlib_validate_combined_counter_will_expand
337 (vlib_combined_counter_main_t * cm, u32 index);
338
Ole Troana568a192020-04-21 17:54:41 +0200339void vlib_free_combined_counter (vlib_combined_counter_main_t * cm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700340
Dave Barach63539202016-08-11 17:21:02 -0400341/** 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*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700349#define vlib_counter_len(cm) vec_len((cm)->maxi)
350
Ed Warnickecb9cada2015-12-08 15:45:58 -0700351#endif /* included_vlib_counter_h */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400352
353/*
354 * fd.io coding-style-patch-verification: ON
355 *
356 * Local Variables:
357 * eval: (c-set-style "gnu")
358 * End:
359 */