FD.io VPP  v18.10-34-gcce845e
Vector Packet Processing
counter.h
Go to the documentation of this file.
1 /*
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 
43 #include <vlib/counter_types.h>
44 
45 /** \file
46 
47  Optimized thread-safe counters.
48 
49  Each vlib_[simple|combined]_counter_main_t consists of a per-thread
50  vector of per-object counters.
51 
52  The idea is to drastically eliminate atomic operations.
53 */
54 
55 /** A collection of simple counters */
56 
57 typedef struct
58 {
59  counter_t **counters; /**< Per-thread u64 non-atomic counters */
60  counter_t *value_at_last_serialize; /**< Values as of last serialize. */
61  u32 last_incremental_serialize_index; /**< Last counter index
62  serialized incrementally. */
63 
64  char *name; /**< The counter collection's name. */
65  char *stat_segment_name; /**< Name in stat segment directory */
67 
68 /** The number of counters (not the number of per-thread counters) */
70 
71 /** Increment a simple counter
72  @param cm - (vlib_simple_counter_main_t *) simple counter main pointer
73  @param thread_index - (u32) the current cpu index
74  @param index - (u32) index of the counter to increment
75  @param increment - (u64) quantitiy to add to the counter
76 */
77 always_inline void
79  u32 thread_index, u32 index, u64 increment)
80 {
81  counter_t *my_counters;
82 
83  my_counters = cm->counters[thread_index];
84  my_counters[index] += increment;
85 }
86 
87 /** Get the value of a simple counter
88  Scrapes the entire set of per-thread counters. Innacurate unless
89  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 */
98 {
99  counter_t *my_counters;
100  counter_t v;
101  int i;
102 
103  ASSERT (index < vlib_simple_counter_n_counters (cm));
104 
105  v = 0;
106 
107  for (i = 0; i < vec_len (cm->counters); i++)
108  {
109  my_counters = cm->counters[i];
110  v += my_counters[index];
111  }
112 
113  return v;
114 }
115 
116 /** 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 */
122 always_inline void
124 {
125  counter_t *my_counters;
126  int i;
127 
128  ASSERT (index < vlib_simple_counter_n_counters (cm));
129 
130  for (i = 0; i < vec_len (cm->counters); i++)
131  {
132  my_counters = cm->counters[i];
133  my_counters[index] = 0;
134  }
135 }
136 
137 /** Add two combined counters, results in the first counter
138  @param [in,out] a - (vlib_counter_t *) dst counter
139  @param b - (vlib_counter_t *) src counter
140 */
141 
142 always_inline void
144 {
145  a->packets += b->packets;
146  a->bytes += b->bytes;
147 }
148 
149 /** Subtract combined counters, results in the first counter
150  @param [in,out] a - (vlib_counter_t *) dst counter
151  @param b - (vlib_counter_t *) src counter
152 */
153 always_inline void
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 
162 /** Clear a combined counter
163  @param a - (vlib_counter_t *) counter to clear
164 */
165 always_inline void
167 {
168  a->packets = a->bytes = 0;
169 }
170 
171 /** A collection of combined counters */
172 typedef struct
173 {
174  vlib_counter_t **counters; /**< Per-thread u64 non-atomic counter pairs */
175  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. */
178  char *stat_segment_name; /**< Name in stat segment directory */
180 
181 /** The number of counters (not the number of per-thread counters) */
183  cm);
184 
185 /** Clear a collection of simple counters
186  @param cm - (vlib_simple_counter_main_t *) collection to clear
187 */
189 
190 /** Clear a collection of combined counters
191  @param cm - (vlib_combined_counter_main_t *) collection to clear
192 */
194 
195 /** Increment a combined counter
196  @param cm - (vlib_combined_counter_main_t *) comined counter main pointer
197  @param thread_index - (u32) the current cpu index
198  @param index - (u32) index of the counter to increment
199  @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
201 */
202 
203 always_inline void
205  u32 thread_index,
206  u32 index, u64 n_packets, u64 n_bytes)
207 {
208  vlib_counter_t *my_counters;
209 
210  /* Use this CPU's counter array */
211  my_counters = cm->counters[thread_index];
212 
213  my_counters[index].packets += n_packets;
214  my_counters[index].bytes += n_bytes;
215 }
216 
217 /** Pre-fetch a per-thread combined counter for the given object index */
218 always_inline void
220  u32 thread_index, u32 index)
221 {
222  vlib_counter_t *cpu_counters;
223 
224  /*
225  * This CPU's index is assumed to already be in cache
226  */
227  cpu_counters = cm->counters[thread_index];
228  CLIB_PREFETCH (cpu_counters + index, CLIB_CACHE_LINE_BYTES, STORE);
229 }
230 
231 
232 /** Get the value of a combined counter, never called in the speed path
233  Scrapes the entire set of per-thread counters. Innacurate unless
234  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 
242 static inline void
244  u32 index, vlib_counter_t * result)
245 {
246  vlib_counter_t *my_counters, *counter;
247  int i;
248 
249  result->packets = 0;
250  result->bytes = 0;
251 
252  for (i = 0; i < vec_len (cm->counters); i++)
253  {
254  my_counters = cm->counters[i];
255 
256  counter = vec_elt_at_index (my_counters, index);
257  result->packets += counter->packets;
258  result->bytes += counter->bytes;
259  }
260 }
261 
262 /** Clear a combined counter
263  Clears the set of per-thread counters.
264 
265  @param cm - (vlib_combined_counter_main_t *) combined counter main pointer
266  @param index - (u32) index of the counter to clear
267 */
268 always_inline void
270 {
271  vlib_counter_t *my_counters, *counter;
272  int i;
273 
274  for (i = 0; i < vec_len (cm->counters); i++)
275  {
276  my_counters = cm->counters[i];
277 
278  counter = vec_elt_at_index (my_counters, index);
279  counter->packets = 0;
280  counter->bytes = 0;
281  }
282 }
283 
284 /** 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 
290  u32 index);
291 /** 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 
298  u32 index);
299 
300 /** 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 */
308 #define vlib_counter_len(cm) vec_len((cm)->maxi)
309 
314 
315 #endif /* included_vlib_counter_h */
316 
317 /*
318  * fd.io coding-style-patch-verification: ON
319  *
320  * Local Variables:
321  * eval: (c-set-style "gnu")
322  * End:
323  */
void vlib_clear_combined_counters(vlib_combined_counter_main_t *cm)
Clear a collection of combined counters.
Definition: counter.c:60
a
Definition: bitmap.h:538
void vlib_validate_simple_counter(vlib_simple_counter_main_t *cm, u32 index)
validate a simple counter
Definition: counter.c:91
static void vlib_increment_combined_counter(vlib_combined_counter_main_t *cm, u32 thread_index, u32 index, u64 n_packets, u64 n_bytes)
Increment a combined counter.
Definition: counter.h:204
char * stat_segment_name
Name in stat segment directory.
Definition: counter.h:178
vlib_counter_t * value_at_last_serialize
Counter values as of last serialize.
Definition: counter.h:175
unsigned long u64
Definition: types.h:89
Combined counter to hold both packets and byte differences.
Definition: counter_types.h:26
int i
static void vlib_increment_simple_counter(vlib_simple_counter_main_t *cm, u32 thread_index, u32 index, u64 increment)
Increment a simple counter.
Definition: counter.h:78
static void vlib_counter_zero(vlib_counter_t *a)
Clear a combined counter.
Definition: counter.h:166
vlib_counter_t ** counters
Per-thread u64 non-atomic counter pairs.
Definition: counter.h:174
static counter_t vlib_get_simple_counter(vlib_simple_counter_main_t *cm, u32 index)
Get the value of a simple counter Scrapes the entire set of per-thread counters.
Definition: counter.h:97
#define always_inline
Definition: clib.h:94
uint64_t counter_t
64bit counters
Definition: counter_types.h:22
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
unsigned int u32
Definition: types.h:88
static void vlib_counter_sub(vlib_counter_t *a, vlib_counter_t *b)
Subtract combined counters, results in the first counter.
Definition: counter.h:154
A collection of simple counters.
Definition: counter.h:57
static void vlib_counter_add(vlib_counter_t *a, vlib_counter_t *b)
Add two combined counters, results in the first counter.
Definition: counter.h:143
char * name
The counter collection&#39;s name.
Definition: counter.h:64
static void vlib_zero_combined_counter(vlib_combined_counter_main_t *cm, u32 index)
Clear a combined counter Clears the set of per-thread counters.
Definition: counter.h:269
counter_t packets
packet counter
Definition: counter_types.h:28
serialize_function_t unserialize_vlib_simple_counter_main
Definition: counter.h:310
#define v
Definition: acl.c:496
static void vlib_prefetch_combined_counter(const vlib_combined_counter_main_t *cm, u32 thread_index, u32 index)
Pre-fetch a per-thread combined counter for the given object index.
Definition: counter.h:219
u32 last_incremental_serialize_index
Last counter index serialized incrementally.
Definition: counter.h:176
counter_t * value_at_last_serialize
Values as of last serialize.
Definition: counter.h:60
static void vlib_get_combined_counter(const vlib_combined_counter_main_t *cm, u32 index, vlib_counter_t *result)
Get the value of a combined counter, never called in the speed path Scrapes the entire set of per-thr...
Definition: counter.h:243
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:79
serialize_function_t serialize_vlib_combined_counter_main
Definition: counter.h:312
u32 last_incremental_serialize_index
Last counter index serialized incrementally.
Definition: counter.h:61
u32 vlib_simple_counter_n_counters(const vlib_simple_counter_main_t *cm)
The number of counters (not the number of per-thread counters)
Definition: counter.c:128
void vlib_validate_combined_counter(vlib_combined_counter_main_t *cm, u32 index)
validate a combined counter
Definition: counter.c:106
serialize_function_t serialize_vlib_simple_counter_main
Definition: counter.h:310
#define ASSERT(truth)
u32 vlib_combined_counter_n_counters(const vlib_combined_counter_main_t *cm)
The number of counters (not the number of per-thread counters)
Definition: counter.c:121
serialize_function_t unserialize_vlib_combined_counter_main
Definition: counter.h:312
static void vlib_zero_simple_counter(vlib_simple_counter_main_t *cm, u32 index)
Clear a simple counter Clears the set of per-thread u16 counters, and the u64 counter.
Definition: counter.h:123
counter_t bytes
byte counter
Definition: counter_types.h:29
char * stat_segment_name
Name in stat segment directory.
Definition: counter.h:65
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
void( serialize_function_t)(serialize_main_t *m, va_list *va)
Definition: serialize.h:168
char * name
The counter collection&#39;s name.
Definition: counter.h:177
A collection of combined counters.
Definition: counter.h:172
counter_t ** counters
Per-thread u64 non-atomic counters.
Definition: counter.h:59
void vlib_clear_simple_counters(vlib_simple_counter_main_t *cm)
Clear a collection of simple counters.
Definition: counter.c:43
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59