blob: 60e2055d232a30d1b7513d3d38d8fe4943ce2a97 [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
Dave Barach63539202016-08-11 17:21:02 -040043/** \file
44
45 Optimized thread-safe counters.
46
Neale Ranns1bd01092017-03-15 15:41:17 -040047 Each vlib_[simple|combined]_counter_main_t consists of a per-thread
48 vector of per-object counters.
Dave Barach63539202016-08-11 17:21:02 -040049
Neale Ranns1bd01092017-03-15 15:41:17 -040050 The idea is to drastically eliminate atomic operations.
Dave Barach63539202016-08-11 17:21:02 -040051*/
52
Neale Ranns1bd01092017-03-15 15:41:17 -040053/** 64bit counters */
54typedef u64 counter_t;
55
Dave Barach63539202016-08-11 17:21:02 -040056/** A collection of simple counters */
Ed Warnickecb9cada2015-12-08 15:45:58 -070057
Dave Barach9b8ffd92016-07-08 08:13:45 -040058typedef struct
59{
Neale Ranns1bd01092017-03-15 15:41:17 -040060 counter_t **counters; /**< Per-thread u64 non-atomic counters */
61 counter_t *value_at_last_serialize; /**< Values as of last serialize. */
Dave Barach63539202016-08-11 17:21:02 -040062 u32 last_incremental_serialize_index; /**< Last counter index
63 serialized incrementally. */
Ed Warnickecb9cada2015-12-08 15:45:58 -070064
Dave Barach63539202016-08-11 17:21:02 -040065 char *name; /**< The counter collection's name. */
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
Dave Barach63539202016-08-11 17:21:02 -040087/** Get the value of a simple counter
Neale Ranns1bd01092017-03-15 15:41:17 -040088 Scrapes the entire set of per-thread counters. Innacurate unless
Dave Barach63539202016-08-11 17:21:02 -040089 worker threads which might increment the counter are
90 barrier-synchronized
91
92 @param cm - (vlib_simple_counter_main_t *) simple counter main pointer
93 @param index - (u32) index of the counter to fetch
94 @returns - (u64) current counter value
95*/
Neale Ranns1bd01092017-03-15 15:41:17 -040096always_inline counter_t
Ed Warnickecb9cada2015-12-08 15:45:58 -070097vlib_get_simple_counter (vlib_simple_counter_main_t * cm, u32 index)
98{
Neale Ranns1bd01092017-03-15 15:41:17 -040099 counter_t *my_counters;
100 counter_t v;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700101 int i;
102
Neale Ranns1bd01092017-03-15 15:41:17 -0400103 ASSERT (index < vlib_simple_counter_n_counters (cm));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700104
105 v = 0;
106
Neale Ranns1bd01092017-03-15 15:41:17 -0400107 for (i = 0; i < vec_len (cm->counters); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700108 {
Neale Ranns1bd01092017-03-15 15:41:17 -0400109 my_counters = cm->counters[i];
110 v += my_counters[index];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700111 }
112
113 return v;
114}
115
Dave Barach63539202016-08-11 17:21:02 -0400116/** Clear a simple counter
117 Clears the set of per-thread u16 counters, and the u64 counter
118
119 @param cm - (vlib_simple_counter_main_t *) simple counter main pointer
120 @param index - (u32) index of the counter to clear
121*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122always_inline void
123vlib_zero_simple_counter (vlib_simple_counter_main_t * cm, u32 index)
124{
Neale Ranns1bd01092017-03-15 15:41:17 -0400125 counter_t *my_counters;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700126 int i;
127
Neale Ranns1bd01092017-03-15 15:41:17 -0400128 ASSERT (index < vlib_simple_counter_n_counters (cm));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700129
Neale Ranns1bd01092017-03-15 15:41:17 -0400130 for (i = 0; i < vec_len (cm->counters); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700131 {
Neale Ranns1bd01092017-03-15 15:41:17 -0400132 my_counters = cm->counters[i];
133 my_counters[index] = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135}
136
Dave Barach63539202016-08-11 17:21:02 -0400137/** Combined counter to hold both packets and byte differences.
138 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400139typedef struct
140{
Neale Ranns1bd01092017-03-15 15:41:17 -0400141 counter_t packets; /**< packet counter */
142 counter_t bytes; /**< byte counter */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143} vlib_counter_t;
144
Dave Barach63539202016-08-11 17:21:02 -0400145/** Add two combined counters, results in the first counter
Chris Luked4024f52016-09-06 09:32:36 -0400146 @param [in,out] a - (vlib_counter_t *) dst counter
Dave Barach63539202016-08-11 17:21:02 -0400147 @param b - (vlib_counter_t *) src counter
148*/
149
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150always_inline void
151vlib_counter_add (vlib_counter_t * a, vlib_counter_t * b)
152{
153 a->packets += b->packets;
154 a->bytes += b->bytes;
155}
156
Dave Barach63539202016-08-11 17:21:02 -0400157/** Subtract combined counters, results in the first counter
Chris Luked4024f52016-09-06 09:32:36 -0400158 @param [in,out] a - (vlib_counter_t *) dst counter
Dave Barach63539202016-08-11 17:21:02 -0400159 @param b - (vlib_counter_t *) src counter
160*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161always_inline void
162vlib_counter_sub (vlib_counter_t * a, vlib_counter_t * b)
163{
164 ASSERT (a->packets >= b->packets);
165 ASSERT (a->bytes >= b->bytes);
166 a->packets -= b->packets;
167 a->bytes -= b->bytes;
168}
169
Dave Barach63539202016-08-11 17:21:02 -0400170/** Clear a combined counter
171 @param a - (vlib_counter_t *) counter to clear
172*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700173always_inline void
174vlib_counter_zero (vlib_counter_t * a)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400175{
176 a->packets = a->bytes = 0;
177}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178
Dave Barach63539202016-08-11 17:21:02 -0400179/** A collection of combined counters */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400180typedef struct
181{
Neale Ranns1bd01092017-03-15 15:41:17 -0400182 vlib_counter_t **counters; /**< Per-thread u64 non-atomic counter pairs */
Dave Barach63539202016-08-11 17:21:02 -0400183 vlib_counter_t *value_at_last_serialize; /**< Counter values as of last serialize. */
184 u32 last_incremental_serialize_index; /**< Last counter index serialized incrementally. */
185 char *name; /**< The counter collection's name. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700186} vlib_combined_counter_main_t;
187
Neale Ranns1bd01092017-03-15 15:41:17 -0400188/** The number of counters (not the number of per-thread counters) */
189u32 vlib_combined_counter_n_counters (const vlib_combined_counter_main_t *
190 cm);
191
Dave Barach63539202016-08-11 17:21:02 -0400192/** Clear a collection of simple counters
193 @param cm - (vlib_simple_counter_main_t *) collection to clear
194*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700195void vlib_clear_simple_counters (vlib_simple_counter_main_t * cm);
Dave Barach63539202016-08-11 17:21:02 -0400196
197/** Clear a collection of combined counters
198 @param cm - (vlib_combined_counter_main_t *) collection to clear
199*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700200void vlib_clear_combined_counters (vlib_combined_counter_main_t * cm);
201
Dave Barach63539202016-08-11 17:21:02 -0400202/** Increment a combined counter
203 @param cm - (vlib_combined_counter_main_t *) comined counter main pointer
Damjan Marion586afd72017-04-05 19:18:20 +0200204 @param thread_index - (u32) the current cpu index
Dave Barach63539202016-08-11 17:21:02 -0400205 @param index - (u32) index of the counter to increment
Neale Ranns1bd01092017-03-15 15:41:17 -0400206 @param packet_increment - (u64) number of packets to add to the counter
207 @param byte_increment - (u64) number of bytes to add to the counter
Dave Barach63539202016-08-11 17:21:02 -0400208*/
209
Ed Warnickecb9cada2015-12-08 15:45:58 -0700210always_inline void
211vlib_increment_combined_counter (vlib_combined_counter_main_t * cm,
Damjan Marion586afd72017-04-05 19:18:20 +0200212 u32 thread_index,
Neale Ranns1bd01092017-03-15 15:41:17 -0400213 u32 index, u64 n_packets, u64 n_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700214{
Neale Ranns1bd01092017-03-15 15:41:17 -0400215 vlib_counter_t *my_counters;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700216
Neale Ranns1bd01092017-03-15 15:41:17 -0400217 /* Use this CPU's counter array */
Damjan Marion586afd72017-04-05 19:18:20 +0200218 my_counters = cm->counters[thread_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700219
Neale Ranns1bd01092017-03-15 15:41:17 -0400220 my_counters[index].packets += n_packets;
221 my_counters[index].bytes += n_bytes;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700222}
223
Neale Ranns1bd01092017-03-15 15:41:17 -0400224/** Pre-fetch a per-thread combined counter for the given object index */
225always_inline void
226vlib_prefetch_combined_counter (const vlib_combined_counter_main_t * cm,
Damjan Marion586afd72017-04-05 19:18:20 +0200227 u32 thread_index, u32 index)
Neale Ranns1bd01092017-03-15 15:41:17 -0400228{
229 vlib_counter_t *cpu_counters;
230
231 /*
232 * This CPU's index is assumed to already be in cache
233 */
Damjan Marion586afd72017-04-05 19:18:20 +0200234 cpu_counters = cm->counters[thread_index];
Neale Ranns1bd01092017-03-15 15:41:17 -0400235 CLIB_PREFETCH (cpu_counters + index, CLIB_CACHE_LINE_BYTES, STORE);
Neale Ranns044183f2017-01-24 01:34:25 -0800236}
237
238
Dave Barach63539202016-08-11 17:21:02 -0400239/** Get the value of a combined counter, never called in the speed path
Neale Ranns1bd01092017-03-15 15:41:17 -0400240 Scrapes the entire set of per-thread counters. Innacurate unless
Dave Barach63539202016-08-11 17:21:02 -0400241 worker threads which might increment the counter are
242 barrier-synchronized
243
244 @param cm - (vlib_combined_counter_main_t *) combined counter main pointer
245 @param index - (u32) index of the combined counter to fetch
246 @param result [out] - (vlib_counter_t *) result stored here
247*/
248
Ed Warnickecb9cada2015-12-08 15:45:58 -0700249static inline void
Neale Ranns1bd01092017-03-15 15:41:17 -0400250vlib_get_combined_counter (const vlib_combined_counter_main_t * cm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400251 u32 index, vlib_counter_t * result)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700252{
Neale Ranns1bd01092017-03-15 15:41:17 -0400253 vlib_counter_t *my_counters, *counter;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700254 int i;
255
256 result->packets = 0;
257 result->bytes = 0;
258
Neale Ranns1bd01092017-03-15 15:41:17 -0400259 for (i = 0; i < vec_len (cm->counters); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700260 {
Neale Ranns1bd01092017-03-15 15:41:17 -0400261 my_counters = cm->counters[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700262
Neale Ranns1bd01092017-03-15 15:41:17 -0400263 counter = vec_elt_at_index (my_counters, index);
264 result->packets += counter->packets;
265 result->bytes += counter->bytes;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700266 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700267}
268
Dave Barach63539202016-08-11 17:21:02 -0400269/** Clear a combined counter
Neale Ranns1bd01092017-03-15 15:41:17 -0400270 Clears the set of per-thread counters.
Dave Barach63539202016-08-11 17:21:02 -0400271
272 @param cm - (vlib_combined_counter_main_t *) combined counter main pointer
273 @param index - (u32) index of the counter to clear
274*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700275always_inline void
Dave Barach9b8ffd92016-07-08 08:13:45 -0400276vlib_zero_combined_counter (vlib_combined_counter_main_t * cm, u32 index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700277{
Neale Ranns1bd01092017-03-15 15:41:17 -0400278 vlib_counter_t *my_counters, *counter;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700279 int i;
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 counter->packets = 0;
287 counter->bytes = 0;
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/** validate a simple counter
292 @param cm - (vlib_simple_counter_main_t *) pointer to the counter collection
293 @param index - (u32) index of the counter to validate
294*/
295
Dave Barach9b8ffd92016-07-08 08:13:45 -0400296void vlib_validate_simple_counter (vlib_simple_counter_main_t * cm,
297 u32 index);
Dave Barach63539202016-08-11 17:21:02 -0400298/** validate a combined counter
299 @param cm - (vlib_combined_counter_main_t *) pointer to the counter
300 collection
301 @param index - (u32) index of the counter to validate
302*/
303
Dave Barach9b8ffd92016-07-08 08:13:45 -0400304void vlib_validate_combined_counter (vlib_combined_counter_main_t * cm,
305 u32 index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700306
Dave Barach63539202016-08-11 17:21:02 -0400307/** Obtain the number of simple or combined counters allocated.
308 A macro which reduces to to vec_len(cm->maxi), the answer in either
309 case.
310
311 @param cm - (vlib_simple_counter_main_t) or
312 (vlib_combined_counter_main_t) the counter collection to interrogate
313 @returns vec_len(cm->maxi)
314*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700315#define vlib_counter_len(cm) vec_len((cm)->maxi)
316
Dave Barach9b8ffd92016-07-08 08:13:45 -0400317serialize_function_t serialize_vlib_simple_counter_main,
318 unserialize_vlib_simple_counter_main;
319serialize_function_t serialize_vlib_combined_counter_main,
320 unserialize_vlib_combined_counter_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700321
322#endif /* included_vlib_counter_h */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400323
324/*
325 * fd.io coding-style-patch-verification: ON
326 *
327 * Local Variables:
328 * eval: (c-set-style "gnu")
329 * End:
330 */