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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 * counter.h: simple and packet/byte counters
18 * Copyright (c) 2008 Eliot Dresselhaus
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:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
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.
40 #ifndef included_vlib_counter_h
41 #define included_vlib_counter_h
43 #include <vlib/counter_types.h>
47 Optimized thread-safe counters.
49 Each vlib_[simple|combined]_counter_main_t consists of a per-thread
50 vector of per-object counters.
52 The idea is to drastically eliminate atomic operations.
55 /** A collection of simple counters */
59 counter_t **counters; /**< Per-thread u64 non-atomic counters */
60 char *name; /**< The counter collection's name. */
61 char *stat_segment_name; /**< Name in stat segment directory */
62 } vlib_simple_counter_main_t;
64 /** The number of counters (not the number of per-thread counters) */
65 u32 vlib_simple_counter_n_counters (const vlib_simple_counter_main_t * cm);
67 /** Pre-fetch a per-thread simple counter for the given object index */
69 vlib_prefetch_simple_counter (const vlib_simple_counter_main_t *cm,
70 u32 thread_index, u32 index)
72 counter_t *my_counters;
75 * This CPU's index is assumed to already be in cache
77 my_counters = cm->counters[thread_index];
78 clib_prefetch_store (my_counters + index);
81 /** Increment a simple counter
82 @param cm - (vlib_simple_counter_main_t *) simple counter main pointer
83 @param thread_index - (u32) the current cpu index
84 @param index - (u32) index of the counter to increment
85 @param increment - (u64) quantitiy to add to the counter
88 vlib_increment_simple_counter (vlib_simple_counter_main_t * cm,
89 u32 thread_index, u32 index, u64 increment)
91 counter_t *my_counters;
93 my_counters = cm->counters[thread_index];
94 my_counters[index] += increment;
97 /** 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
104 vlib_decrement_simple_counter (vlib_simple_counter_main_t * cm,
105 u32 thread_index, u32 index, u64 decrement)
107 counter_t *my_counters;
109 my_counters = cm->counters[thread_index];
111 ASSERT (my_counters[index] >= decrement);
113 my_counters[index] -= decrement;
116 /** 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
123 vlib_set_simple_counter (vlib_simple_counter_main_t * cm,
124 u32 thread_index, u32 index, u64 value)
126 counter_t *my_counters;
128 my_counters = cm->counters[thread_index];
129 my_counters[index] = value;
132 /** Get the value of a simple counter
133 Scrapes the entire set of per-thread counters. Innacurate unless
134 worker threads which might increment the counter are
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
141 always_inline counter_t
142 vlib_get_simple_counter (vlib_simple_counter_main_t * cm, u32 index)
144 counter_t *my_counters;
148 ASSERT (index < vlib_simple_counter_n_counters (cm));
152 for (i = 0; i < vec_len (cm->counters); i++)
154 my_counters = cm->counters[i];
155 v += my_counters[index];
161 /** Clear a simple counter
162 Clears the set of per-thread u16 counters, and the u64 counter
164 @param cm - (vlib_simple_counter_main_t *) simple counter main pointer
165 @param index - (u32) index of the counter to clear
168 vlib_zero_simple_counter (vlib_simple_counter_main_t * cm, u32 index)
170 counter_t *my_counters;
173 ASSERT (index < vlib_simple_counter_n_counters (cm));
175 for (i = 0; i < vec_len (cm->counters); i++)
177 my_counters = cm->counters[i];
178 my_counters[index] = 0;
182 /** Add two combined counters, results in the first counter
183 @param [in,out] a - (vlib_counter_t *) dst counter
184 @param b - (vlib_counter_t *) src counter
188 vlib_counter_add (vlib_counter_t * a, vlib_counter_t * b)
190 a->packets += b->packets;
191 a->bytes += b->bytes;
194 /** Subtract combined counters, results in the first counter
195 @param [in,out] a - (vlib_counter_t *) dst counter
196 @param b - (vlib_counter_t *) src counter
199 vlib_counter_sub (vlib_counter_t * a, vlib_counter_t * b)
201 ASSERT (a->packets >= b->packets);
202 ASSERT (a->bytes >= b->bytes);
203 a->packets -= b->packets;
204 a->bytes -= b->bytes;
207 /** Clear a combined counter
208 @param a - (vlib_counter_t *) counter to clear
211 vlib_counter_zero (vlib_counter_t * a)
213 a->packets = a->bytes = 0;
216 /** A collection of combined counters */
219 vlib_counter_t **counters; /**< Per-thread u64 non-atomic counter pairs */
220 char *name; /**< The counter collection's name. */
221 char *stat_segment_name; /**< Name in stat segment directory */
222 } vlib_combined_counter_main_t;
224 /** The number of counters (not the number of per-thread counters) */
225 u32 vlib_combined_counter_n_counters (const vlib_combined_counter_main_t *
228 /** Clear a collection of simple counters
229 @param cm - (vlib_simple_counter_main_t *) collection to clear
231 void vlib_clear_simple_counters (vlib_simple_counter_main_t * cm);
233 /** Clear a collection of combined counters
234 @param cm - (vlib_combined_counter_main_t *) collection to clear
236 void vlib_clear_combined_counters (vlib_combined_counter_main_t * cm);
238 /** Increment a combined counter
239 @param cm - (vlib_combined_counter_main_t *) comined counter main pointer
240 @param thread_index - (u32) the current cpu index
241 @param index - (u32) index of the counter to increment
242 @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
247 vlib_increment_combined_counter (vlib_combined_counter_main_t * cm,
249 u32 index, u64 n_packets, u64 n_bytes)
251 vlib_counter_t *my_counters;
253 /* Use this CPU's counter array */
254 my_counters = cm->counters[thread_index];
256 my_counters[index].packets += n_packets;
257 my_counters[index].bytes += n_bytes;
260 /** Pre-fetch a per-thread combined counter for the given object index */
262 vlib_prefetch_combined_counter (const vlib_combined_counter_main_t * cm,
263 u32 thread_index, u32 index)
265 vlib_counter_t *cpu_counters;
268 * This CPU's index is assumed to already be in cache
270 cpu_counters = cm->counters[thread_index];
271 clib_prefetch_store (cpu_counters + index);
275 /** Get the value of a combined counter, never called in the speed path
276 Scrapes the entire set of per-thread counters. Innacurate unless
277 worker threads which might increment the counter are
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
286 vlib_get_combined_counter (const vlib_combined_counter_main_t * cm,
287 u32 index, vlib_counter_t * result)
289 vlib_counter_t *my_counters, *counter;
295 for (i = 0; i < vec_len (cm->counters); i++)
297 my_counters = cm->counters[i];
299 counter = vec_elt_at_index (my_counters, index);
300 result->packets += counter->packets;
301 result->bytes += counter->bytes;
305 /** Clear a combined counter
306 Clears the set of per-thread counters.
308 @param cm - (vlib_combined_counter_main_t *) combined counter main pointer
309 @param index - (u32) index of the counter to clear
312 vlib_zero_combined_counter (vlib_combined_counter_main_t * cm, u32 index)
314 vlib_counter_t *my_counters, *counter;
317 for (i = 0; i < vec_len (cm->counters); i++)
319 my_counters = cm->counters[i];
321 counter = vec_elt_at_index (my_counters, index);
322 counter->packets = 0;
327 /** 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
332 void vlib_validate_simple_counter (vlib_simple_counter_main_t * cm,
334 void vlib_free_simple_counter (vlib_simple_counter_main_t * cm);
336 /** validate a combined counter
337 @param cm - (vlib_combined_counter_main_t *) pointer to the counter
339 @param index - (u32) index of the counter to validate
342 void vlib_validate_combined_counter (vlib_combined_counter_main_t * cm,
344 int vlib_validate_combined_counter_will_expand
345 (vlib_combined_counter_main_t * cm, u32 index);
347 void vlib_free_combined_counter (vlib_combined_counter_main_t * cm);
349 /** Obtain the number of simple or combined counters allocated.
350 A macro which reduces to to vec_len(cm->maxi), the answer in either
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)
357 #define vlib_counter_len(cm) vec_len((cm)->maxi)
359 #endif /* included_vlib_counter_h */
362 * fd.io coding-style-patch-verification: ON
365 * eval: (c-set-style "gnu")