FD.io VPP  v18.07-rc0-415-g6c78436
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 /** \file
44 
45  Optimized thread-safe counters.
46 
47  Each vlib_[simple|combined]_counter_main_t consists of a per-thread
48  vector of per-object counters.
49 
50  The idea is to drastically eliminate atomic operations.
51 */
52 
53 /** 64bit counters */
54 typedef u64 counter_t;
55 
56 /** A collection of simple counters */
57 
58 typedef struct
59 {
60  counter_t **counters; /**< Per-thread u64 non-atomic counters */
61  counter_t *value_at_last_serialize; /**< Values as of last serialize. */
62  u32 last_incremental_serialize_index; /**< Last counter index
63  serialized incrementally. */
64 
65  char *name; /**< The counter collection's name. */
66  char *stat_segment_name; /**< Name in stat segment directory */
68 
69 /** The number of counters (not the number of per-thread counters) */
71 
72 /** Increment a simple counter
73  @param cm - (vlib_simple_counter_main_t *) simple counter main pointer
74  @param thread_index - (u32) the current cpu index
75  @param index - (u32) index of the counter to increment
76  @param increment - (u64) quantitiy to add to the counter
77 */
78 always_inline void
80  u32 thread_index, u32 index, u64 increment)
81 {
82  counter_t *my_counters;
83 
84  my_counters = cm->counters[thread_index];
85  my_counters[index] += increment;
86 }
87 
88 /** Get the value of a simple counter
89  Scrapes the entire set of per-thread counters. Innacurate unless
90  worker threads which might increment the counter are
91  barrier-synchronized
92 
93  @param cm - (vlib_simple_counter_main_t *) simple counter main pointer
94  @param index - (u32) index of the counter to fetch
95  @returns - (u64) current counter value
96 */
99 {
100  counter_t *my_counters;
101  counter_t v;
102  int i;
103 
104  ASSERT (index < vlib_simple_counter_n_counters (cm));
105 
106  v = 0;
107 
108  for (i = 0; i < vec_len (cm->counters); i++)
109  {
110  my_counters = cm->counters[i];
111  v += my_counters[index];
112  }
113 
114  return v;
115 }
116 
117 /** Clear a simple counter
118  Clears the set of per-thread u16 counters, and the u64 counter
119 
120  @param cm - (vlib_simple_counter_main_t *) simple counter main pointer
121  @param index - (u32) index of the counter to clear
122 */
123 always_inline void
125 {
126  counter_t *my_counters;
127  int i;
128 
129  ASSERT (index < vlib_simple_counter_n_counters (cm));
130 
131  for (i = 0; i < vec_len (cm->counters); i++)
132  {
133  my_counters = cm->counters[i];
134  my_counters[index] = 0;
135  }
136 }
137 
138 /** Combined counter to hold both packets and byte differences.
139  */
140 typedef struct
141 {
142  counter_t packets; /**< packet counter */
143  counter_t bytes; /**< byte counter */
145 
146 /** Add two combined counters, results in the first counter
147  @param [in,out] a - (vlib_counter_t *) dst counter
148  @param b - (vlib_counter_t *) src counter
149 */
150 
151 always_inline void
153 {
154  a->packets += b->packets;
155  a->bytes += b->bytes;
156 }
157 
158 /** Subtract combined counters, results in the first counter
159  @param [in,out] a - (vlib_counter_t *) dst counter
160  @param b - (vlib_counter_t *) src counter
161 */
162 always_inline void
164 {
165  ASSERT (a->packets >= b->packets);
166  ASSERT (a->bytes >= b->bytes);
167  a->packets -= b->packets;
168  a->bytes -= b->bytes;
169 }
170 
171 /** Clear a combined counter
172  @param a - (vlib_counter_t *) counter to clear
173 */
174 always_inline void
176 {
177  a->packets = a->bytes = 0;
178 }
179 
180 /** A collection of combined counters */
181 typedef struct
182 {
183  vlib_counter_t **counters; /**< Per-thread u64 non-atomic counter pairs */
184  vlib_counter_t *value_at_last_serialize; /**< Counter values as of last serialize. */
185  u32 last_incremental_serialize_index; /**< Last counter index serialized incrementally. */
186  char *name; /**< The counter collection's name. */
187  char *stat_segment_name; /**< Name in stat segment directory */
189 
190 /** The number of counters (not the number of per-thread counters) */
192  cm);
193 
194 /** Clear a collection of simple counters
195  @param cm - (vlib_simple_counter_main_t *) collection to clear
196 */
198 
199 /** Clear a collection of combined counters
200  @param cm - (vlib_combined_counter_main_t *) collection to clear
201 */
203 
204 /** Increment a combined counter
205  @param cm - (vlib_combined_counter_main_t *) comined counter main pointer
206  @param thread_index - (u32) the current cpu index
207  @param index - (u32) index of the counter to increment
208  @param packet_increment - (u64) number of packets to add to the counter
209  @param byte_increment - (u64) number of bytes to add to the counter
210 */
211 
212 always_inline void
214  u32 thread_index,
215  u32 index, u64 n_packets, u64 n_bytes)
216 {
217  vlib_counter_t *my_counters;
218 
219  /* Use this CPU's counter array */
220  my_counters = cm->counters[thread_index];
221 
222  my_counters[index].packets += n_packets;
223  my_counters[index].bytes += n_bytes;
224 }
225 
226 /** Pre-fetch a per-thread combined counter for the given object index */
227 always_inline void
229  u32 thread_index, u32 index)
230 {
231  vlib_counter_t *cpu_counters;
232 
233  /*
234  * This CPU's index is assumed to already be in cache
235  */
236  cpu_counters = cm->counters[thread_index];
237  CLIB_PREFETCH (cpu_counters + index, CLIB_CACHE_LINE_BYTES, STORE);
238 }
239 
240 
241 /** Get the value of a combined counter, never called in the speed path
242  Scrapes the entire set of per-thread counters. Innacurate unless
243  worker threads which might increment the counter are
244  barrier-synchronized
245 
246  @param cm - (vlib_combined_counter_main_t *) combined counter main pointer
247  @param index - (u32) index of the combined counter to fetch
248  @param result [out] - (vlib_counter_t *) result stored here
249 */
250 
251 static inline void
253  u32 index, vlib_counter_t * result)
254 {
255  vlib_counter_t *my_counters, *counter;
256  int i;
257 
258  result->packets = 0;
259  result->bytes = 0;
260 
261  for (i = 0; i < vec_len (cm->counters); i++)
262  {
263  my_counters = cm->counters[i];
264 
265  counter = vec_elt_at_index (my_counters, index);
266  result->packets += counter->packets;
267  result->bytes += counter->bytes;
268  }
269 }
270 
271 /** Clear a combined counter
272  Clears the set of per-thread counters.
273 
274  @param cm - (vlib_combined_counter_main_t *) combined counter main pointer
275  @param index - (u32) index of the counter to clear
276 */
277 always_inline void
279 {
280  vlib_counter_t *my_counters, *counter;
281  int i;
282 
283  for (i = 0; i < vec_len (cm->counters); i++)
284  {
285  my_counters = cm->counters[i];
286 
287  counter = vec_elt_at_index (my_counters, index);
288  counter->packets = 0;
289  counter->bytes = 0;
290  }
291 }
292 
293 /** validate a simple counter
294  @param cm - (vlib_simple_counter_main_t *) pointer to the counter collection
295  @param index - (u32) index of the counter to validate
296 */
297 
299  u32 index);
300 /** validate a combined counter
301  @param cm - (vlib_combined_counter_main_t *) pointer to the counter
302  collection
303  @param index - (u32) index of the counter to validate
304 */
305 
307  u32 index);
308 
309 /** Obtain the number of simple or combined counters allocated.
310  A macro which reduces to to vec_len(cm->maxi), the answer in either
311  case.
312 
313  @param cm - (vlib_simple_counter_main_t) or
314  (vlib_combined_counter_main_t) the counter collection to interrogate
315  @returns vec_len(cm->maxi)
316 */
317 #define vlib_counter_len(cm) vec_len((cm)->maxi)
318 
323 
324 #endif /* included_vlib_counter_h */
325 
326 /*
327  * fd.io coding-style-patch-verification: ON
328  *
329  * Local Variables:
330  * eval: (c-set-style "gnu")
331  * End:
332  */
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:537
void vlib_validate_simple_counter(vlib_simple_counter_main_t *cm, u32 index)
validate a simple counter
Definition: counter.c:92
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:213
char * stat_segment_name
Name in stat segment directory.
Definition: counter.h:187
vlib_counter_t * value_at_last_serialize
Counter values as of last serialize.
Definition: counter.h:184
unsigned long u64
Definition: types.h:89
Combined counter to hold both packets and byte differences.
Definition: counter.h:140
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:79
static void vlib_counter_zero(vlib_counter_t *a)
Clear a combined counter.
Definition: counter.h:175
vlib_counter_t ** counters
Per-thread u64 non-atomic counter pairs.
Definition: counter.h:183
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:98
#define always_inline
Definition: clib.h:92
#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:163
A collection of simple counters.
Definition: counter.h:58
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:152
char * name
The counter collection&#39;s name.
Definition: counter.h:65
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:278
counter_t packets
packet counter
Definition: counter.h:142
serialize_function_t unserialize_vlib_simple_counter_main
Definition: counter.h:319
#define v
Definition: acl.c:491
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:228
u32 last_incremental_serialize_index
Last counter index serialized incrementally.
Definition: counter.h:185
counter_t * value_at_last_serialize
Values as of last serialize.
Definition: counter.h:61
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:252
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:77
serialize_function_t serialize_vlib_combined_counter_main
Definition: counter.h:321
u32 last_incremental_serialize_index
Last counter index serialized incrementally.
Definition: counter.h:62
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:127
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:319
#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:120
serialize_function_t unserialize_vlib_combined_counter_main
Definition: counter.h:321
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:124
u64 counter_t
64bit counters
Definition: counter.h:54
counter_t bytes
byte counter
Definition: counter.h:143
char * stat_segment_name
Name in stat segment directory.
Definition: counter.h:66
#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:186
A collection of combined counters.
Definition: counter.h:181
counter_t ** counters
Per-thread u64 non-atomic counters.
Definition: counter.h:60
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:62