blob: 3aacc9b375fd5ee90fb0b9f12d3eb8aa58956a76 [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
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/** Add two combined counters, results in the first counter
Chris Luked4024f52016-09-06 09:32:36 -0400138 @param [in,out] a - (vlib_counter_t *) dst counter
Dave Barach63539202016-08-11 17:21:02 -0400139 @param b - (vlib_counter_t *) src counter
140*/
141
Ed Warnickecb9cada2015-12-08 15:45:58 -0700142always_inline void
143vlib_counter_add (vlib_counter_t * a, vlib_counter_t * b)
144{
145 a->packets += b->packets;
146 a->bytes += b->bytes;
147}
148
Dave Barach63539202016-08-11 17:21:02 -0400149/** Subtract combined counters, results in the first counter
Chris Luked4024f52016-09-06 09:32:36 -0400150 @param [in,out] a - (vlib_counter_t *) dst counter
Dave Barach63539202016-08-11 17:21:02 -0400151 @param b - (vlib_counter_t *) src counter
152*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700153always_inline void
154vlib_counter_sub (vlib_counter_t * a, vlib_counter_t * b)
155{
156 ASSERT (a->packets >= b->packets);
157 ASSERT (a->bytes >= b->bytes);
158 a->packets -= b->packets;
159 a->bytes -= b->bytes;
160}
161
Dave Barach63539202016-08-11 17:21:02 -0400162/** Clear a combined counter
163 @param a - (vlib_counter_t *) counter to clear
164*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165always_inline void
166vlib_counter_zero (vlib_counter_t * a)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400167{
168 a->packets = a->bytes = 0;
169}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700170
Dave Barach63539202016-08-11 17:21:02 -0400171/** A collection of combined counters */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400172typedef struct
173{
Neale Ranns1bd01092017-03-15 15:41:17 -0400174 vlib_counter_t **counters; /**< Per-thread u64 non-atomic counter pairs */
Dave Barach63539202016-08-11 17:21:02 -0400175 vlib_counter_t *value_at_last_serialize; /**< Counter values as of last serialize. */
176 u32 last_incremental_serialize_index; /**< Last counter index serialized incrementally. */
177 char *name; /**< The counter collection's name. */
Dave Barach048a4e52018-06-01 18:52:25 -0400178 char *stat_segment_name; /**< Name in stat segment directory */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179} vlib_combined_counter_main_t;
180
Neale Ranns1bd01092017-03-15 15:41:17 -0400181/** The number of counters (not the number of per-thread counters) */
182u32 vlib_combined_counter_n_counters (const vlib_combined_counter_main_t *
183 cm);
184
Dave Barach63539202016-08-11 17:21:02 -0400185/** Clear a collection of simple counters
186 @param cm - (vlib_simple_counter_main_t *) collection to clear
187*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700188void vlib_clear_simple_counters (vlib_simple_counter_main_t * cm);
Dave Barach63539202016-08-11 17:21:02 -0400189
190/** Clear a collection of combined counters
191 @param cm - (vlib_combined_counter_main_t *) collection to clear
192*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700193void vlib_clear_combined_counters (vlib_combined_counter_main_t * cm);
194
Dave Barach63539202016-08-11 17:21:02 -0400195/** Increment a combined counter
196 @param cm - (vlib_combined_counter_main_t *) comined counter main pointer
Damjan Marion586afd72017-04-05 19:18:20 +0200197 @param thread_index - (u32) the current cpu index
Dave Barach63539202016-08-11 17:21:02 -0400198 @param index - (u32) index of the counter to increment
Neale Ranns1bd01092017-03-15 15:41:17 -0400199 @param packet_increment - (u64) number of packets to add to the counter
200 @param byte_increment - (u64) number of bytes to add to the counter
Dave Barach63539202016-08-11 17:21:02 -0400201*/
202
Ed Warnickecb9cada2015-12-08 15:45:58 -0700203always_inline void
204vlib_increment_combined_counter (vlib_combined_counter_main_t * cm,
Damjan Marion586afd72017-04-05 19:18:20 +0200205 u32 thread_index,
Neale Ranns1bd01092017-03-15 15:41:17 -0400206 u32 index, u64 n_packets, u64 n_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700207{
Neale Ranns1bd01092017-03-15 15:41:17 -0400208 vlib_counter_t *my_counters;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700209
Neale Ranns1bd01092017-03-15 15:41:17 -0400210 /* Use this CPU's counter array */
Damjan Marion586afd72017-04-05 19:18:20 +0200211 my_counters = cm->counters[thread_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700212
Neale Ranns1bd01092017-03-15 15:41:17 -0400213 my_counters[index].packets += n_packets;
214 my_counters[index].bytes += n_bytes;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700215}
216
Neale Ranns1bd01092017-03-15 15:41:17 -0400217/** Pre-fetch a per-thread combined counter for the given object index */
218always_inline void
219vlib_prefetch_combined_counter (const vlib_combined_counter_main_t * cm,
Damjan Marion586afd72017-04-05 19:18:20 +0200220 u32 thread_index, u32 index)
Neale Ranns1bd01092017-03-15 15:41:17 -0400221{
222 vlib_counter_t *cpu_counters;
223
224 /*
225 * This CPU's index is assumed to already be in cache
226 */
Damjan Marion586afd72017-04-05 19:18:20 +0200227 cpu_counters = cm->counters[thread_index];
Neale Ranns1bd01092017-03-15 15:41:17 -0400228 CLIB_PREFETCH (cpu_counters + index, CLIB_CACHE_LINE_BYTES, STORE);
Neale Ranns044183f2017-01-24 01:34:25 -0800229}
230
231
Dave Barach63539202016-08-11 17:21:02 -0400232/** Get the value of a combined counter, never called in the speed path
Neale Ranns1bd01092017-03-15 15:41:17 -0400233 Scrapes the entire set of per-thread counters. Innacurate unless
Dave Barach63539202016-08-11 17:21:02 -0400234 worker threads which might increment the counter are
235 barrier-synchronized
236
237 @param cm - (vlib_combined_counter_main_t *) combined counter main pointer
238 @param index - (u32) index of the combined counter to fetch
239 @param result [out] - (vlib_counter_t *) result stored here
240*/
241
Ed Warnickecb9cada2015-12-08 15:45:58 -0700242static inline void
Neale Ranns1bd01092017-03-15 15:41:17 -0400243vlib_get_combined_counter (const vlib_combined_counter_main_t * cm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400244 u32 index, vlib_counter_t * result)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700245{
Neale Ranns1bd01092017-03-15 15:41:17 -0400246 vlib_counter_t *my_counters, *counter;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247 int i;
248
249 result->packets = 0;
250 result->bytes = 0;
251
Neale Ranns1bd01092017-03-15 15:41:17 -0400252 for (i = 0; i < vec_len (cm->counters); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700253 {
Neale Ranns1bd01092017-03-15 15:41:17 -0400254 my_counters = cm->counters[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700255
Neale Ranns1bd01092017-03-15 15:41:17 -0400256 counter = vec_elt_at_index (my_counters, index);
257 result->packets += counter->packets;
258 result->bytes += counter->bytes;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700259 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700260}
261
Dave Barach63539202016-08-11 17:21:02 -0400262/** Clear a combined counter
Neale Ranns1bd01092017-03-15 15:41:17 -0400263 Clears the set of per-thread counters.
Dave Barach63539202016-08-11 17:21:02 -0400264
265 @param cm - (vlib_combined_counter_main_t *) combined counter main pointer
266 @param index - (u32) index of the counter to clear
267*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700268always_inline void
Dave Barach9b8ffd92016-07-08 08:13:45 -0400269vlib_zero_combined_counter (vlib_combined_counter_main_t * cm, u32 index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700270{
Neale Ranns1bd01092017-03-15 15:41:17 -0400271 vlib_counter_t *my_counters, *counter;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700272 int i;
273
Neale Ranns1bd01092017-03-15 15:41:17 -0400274 for (i = 0; i < vec_len (cm->counters); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700275 {
Neale Ranns1bd01092017-03-15 15:41:17 -0400276 my_counters = cm->counters[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700277
Neale Ranns1bd01092017-03-15 15:41:17 -0400278 counter = vec_elt_at_index (my_counters, index);
279 counter->packets = 0;
280 counter->bytes = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700281 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700282}
283
Dave Barach63539202016-08-11 17:21:02 -0400284/** validate a simple counter
285 @param cm - (vlib_simple_counter_main_t *) pointer to the counter collection
286 @param index - (u32) index of the counter to validate
287*/
288
Dave Barach9b8ffd92016-07-08 08:13:45 -0400289void vlib_validate_simple_counter (vlib_simple_counter_main_t * cm,
290 u32 index);
Dave Barach63539202016-08-11 17:21:02 -0400291/** validate a combined counter
292 @param cm - (vlib_combined_counter_main_t *) pointer to the counter
293 collection
294 @param index - (u32) index of the counter to validate
295*/
296
Dave Barach9b8ffd92016-07-08 08:13:45 -0400297void vlib_validate_combined_counter (vlib_combined_counter_main_t * cm,
298 u32 index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700299
Dave Barach63539202016-08-11 17:21:02 -0400300/** Obtain the number of simple or combined counters allocated.
301 A macro which reduces to to vec_len(cm->maxi), the answer in either
302 case.
303
304 @param cm - (vlib_simple_counter_main_t) or
305 (vlib_combined_counter_main_t) the counter collection to interrogate
306 @returns vec_len(cm->maxi)
307*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700308#define vlib_counter_len(cm) vec_len((cm)->maxi)
309
Dave Barach9b8ffd92016-07-08 08:13:45 -0400310serialize_function_t serialize_vlib_simple_counter_main,
311 unserialize_vlib_simple_counter_main;
312serialize_function_t serialize_vlib_combined_counter_main,
313 unserialize_vlib_combined_counter_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700314
315#endif /* included_vlib_counter_h */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400316
317/*
318 * fd.io coding-style-patch-verification: ON
319 *
320 * Local Variables:
321 * eval: (c-set-style "gnu")
322 * End:
323 */