Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 43 | /** \file |
| 44 | |
| 45 | Optimized thread-safe counters. |
| 46 | |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 47 | Each vlib_[simple|combined]_counter_main_t consists of a per-thread |
| 48 | vector of per-object counters. |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 49 | |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 50 | The idea is to drastically eliminate atomic operations. |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 51 | */ |
| 52 | |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 53 | /** 64bit counters */ |
| 54 | typedef u64 counter_t; |
| 55 | |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 56 | /** A collection of simple counters */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 57 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 58 | typedef struct |
| 59 | { |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 60 | counter_t **counters; /**< Per-thread u64 non-atomic counters */ |
| 61 | counter_t *value_at_last_serialize; /**< Values as of last serialize. */ |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 62 | u32 last_incremental_serialize_index; /**< Last counter index |
| 63 | serialized incrementally. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 64 | |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 65 | char *name; /**< The counter collection's name. */ |
Dave Barach | 048a4e5 | 2018-06-01 18:52:25 -0400 | [diff] [blame] | 66 | char *stat_segment_name; /**< Name in stat segment directory */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 67 | } vlib_simple_counter_main_t; |
| 68 | |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 69 | /** The number of counters (not the number of per-thread counters) */ |
| 70 | u32 vlib_simple_counter_n_counters (const vlib_simple_counter_main_t * cm); |
| 71 | |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 72 | /** Increment a simple counter |
| 73 | @param cm - (vlib_simple_counter_main_t *) simple counter main pointer |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 74 | @param thread_index - (u32) the current cpu index |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 75 | @param index - (u32) index of the counter to increment |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 76 | @param increment - (u64) quantitiy to add to the counter |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 77 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 78 | always_inline void |
| 79 | vlib_increment_simple_counter (vlib_simple_counter_main_t * cm, |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 80 | u32 thread_index, u32 index, u64 increment) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 81 | { |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 82 | counter_t *my_counters; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 83 | |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 84 | my_counters = cm->counters[thread_index]; |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 85 | my_counters[index] += increment; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 88 | /** Get the value of a simple counter |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 89 | Scrapes the entire set of per-thread counters. Innacurate unless |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 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 | */ |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 97 | always_inline counter_t |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 98 | vlib_get_simple_counter (vlib_simple_counter_main_t * cm, u32 index) |
| 99 | { |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 100 | counter_t *my_counters; |
| 101 | counter_t v; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 102 | int i; |
| 103 | |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 104 | ASSERT (index < vlib_simple_counter_n_counters (cm)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 105 | |
| 106 | v = 0; |
| 107 | |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 108 | for (i = 0; i < vec_len (cm->counters); i++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 109 | { |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 110 | my_counters = cm->counters[i]; |
| 111 | v += my_counters[index]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | return v; |
| 115 | } |
| 116 | |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 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 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 123 | always_inline void |
| 124 | vlib_zero_simple_counter (vlib_simple_counter_main_t * cm, u32 index) |
| 125 | { |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 126 | counter_t *my_counters; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 127 | int i; |
| 128 | |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 129 | ASSERT (index < vlib_simple_counter_n_counters (cm)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 130 | |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 131 | for (i = 0; i < vec_len (cm->counters); i++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 132 | { |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 133 | my_counters = cm->counters[i]; |
| 134 | my_counters[index] = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 135 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 138 | /** Combined counter to hold both packets and byte differences. |
| 139 | */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 140 | typedef struct |
| 141 | { |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 142 | counter_t packets; /**< packet counter */ |
| 143 | counter_t bytes; /**< byte counter */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 144 | } vlib_counter_t; |
| 145 | |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 146 | /** Add two combined counters, results in the first counter |
Chris Luke | d4024f5 | 2016-09-06 09:32:36 -0400 | [diff] [blame] | 147 | @param [in,out] a - (vlib_counter_t *) dst counter |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 148 | @param b - (vlib_counter_t *) src counter |
| 149 | */ |
| 150 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 151 | always_inline void |
| 152 | vlib_counter_add (vlib_counter_t * a, vlib_counter_t * b) |
| 153 | { |
| 154 | a->packets += b->packets; |
| 155 | a->bytes += b->bytes; |
| 156 | } |
| 157 | |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 158 | /** Subtract combined counters, results in the first counter |
Chris Luke | d4024f5 | 2016-09-06 09:32:36 -0400 | [diff] [blame] | 159 | @param [in,out] a - (vlib_counter_t *) dst counter |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 160 | @param b - (vlib_counter_t *) src counter |
| 161 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 162 | always_inline void |
| 163 | vlib_counter_sub (vlib_counter_t * a, vlib_counter_t * b) |
| 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 | |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 171 | /** Clear a combined counter |
| 172 | @param a - (vlib_counter_t *) counter to clear |
| 173 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 174 | always_inline void |
| 175 | vlib_counter_zero (vlib_counter_t * a) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 176 | { |
| 177 | a->packets = a->bytes = 0; |
| 178 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 179 | |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 180 | /** A collection of combined counters */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 181 | typedef struct |
| 182 | { |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 183 | vlib_counter_t **counters; /**< Per-thread u64 non-atomic counter pairs */ |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 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. */ |
Dave Barach | 048a4e5 | 2018-06-01 18:52:25 -0400 | [diff] [blame] | 187 | char *stat_segment_name; /**< Name in stat segment directory */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 188 | } vlib_combined_counter_main_t; |
| 189 | |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 190 | /** The number of counters (not the number of per-thread counters) */ |
| 191 | u32 vlib_combined_counter_n_counters (const vlib_combined_counter_main_t * |
| 192 | cm); |
| 193 | |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 194 | /** Clear a collection of simple counters |
| 195 | @param cm - (vlib_simple_counter_main_t *) collection to clear |
| 196 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 197 | void vlib_clear_simple_counters (vlib_simple_counter_main_t * cm); |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 198 | |
| 199 | /** Clear a collection of combined counters |
| 200 | @param cm - (vlib_combined_counter_main_t *) collection to clear |
| 201 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 202 | void vlib_clear_combined_counters (vlib_combined_counter_main_t * cm); |
| 203 | |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 204 | /** Increment a combined counter |
| 205 | @param cm - (vlib_combined_counter_main_t *) comined counter main pointer |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 206 | @param thread_index - (u32) the current cpu index |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 207 | @param index - (u32) index of the counter to increment |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 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 |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 210 | */ |
| 211 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 212 | always_inline void |
| 213 | vlib_increment_combined_counter (vlib_combined_counter_main_t * cm, |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 214 | u32 thread_index, |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 215 | u32 index, u64 n_packets, u64 n_bytes) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 216 | { |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 217 | vlib_counter_t *my_counters; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 218 | |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 219 | /* Use this CPU's counter array */ |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 220 | my_counters = cm->counters[thread_index]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 221 | |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 222 | my_counters[index].packets += n_packets; |
| 223 | my_counters[index].bytes += n_bytes; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 226 | /** Pre-fetch a per-thread combined counter for the given object index */ |
| 227 | always_inline void |
| 228 | vlib_prefetch_combined_counter (const vlib_combined_counter_main_t * cm, |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 229 | u32 thread_index, u32 index) |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 230 | { |
| 231 | vlib_counter_t *cpu_counters; |
| 232 | |
| 233 | /* |
| 234 | * This CPU's index is assumed to already be in cache |
| 235 | */ |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 236 | cpu_counters = cm->counters[thread_index]; |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 237 | CLIB_PREFETCH (cpu_counters + index, CLIB_CACHE_LINE_BYTES, STORE); |
Neale Ranns | 044183f | 2017-01-24 01:34:25 -0800 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 241 | /** Get the value of a combined counter, never called in the speed path |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 242 | Scrapes the entire set of per-thread counters. Innacurate unless |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 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 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 251 | static inline void |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 252 | vlib_get_combined_counter (const vlib_combined_counter_main_t * cm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 253 | u32 index, vlib_counter_t * result) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 254 | { |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 255 | vlib_counter_t *my_counters, *counter; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 256 | int i; |
| 257 | |
| 258 | result->packets = 0; |
| 259 | result->bytes = 0; |
| 260 | |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 261 | for (i = 0; i < vec_len (cm->counters); i++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 262 | { |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 263 | my_counters = cm->counters[i]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 264 | |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 265 | counter = vec_elt_at_index (my_counters, index); |
| 266 | result->packets += counter->packets; |
| 267 | result->bytes += counter->bytes; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 268 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 271 | /** Clear a combined counter |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 272 | Clears the set of per-thread counters. |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 273 | |
| 274 | @param cm - (vlib_combined_counter_main_t *) combined counter main pointer |
| 275 | @param index - (u32) index of the counter to clear |
| 276 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 277 | always_inline void |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 278 | vlib_zero_combined_counter (vlib_combined_counter_main_t * cm, u32 index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 279 | { |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 280 | vlib_counter_t *my_counters, *counter; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 281 | int i; |
| 282 | |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 283 | for (i = 0; i < vec_len (cm->counters); i++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 284 | { |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 285 | my_counters = cm->counters[i]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 286 | |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 287 | counter = vec_elt_at_index (my_counters, index); |
| 288 | counter->packets = 0; |
| 289 | counter->bytes = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 290 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 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 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 298 | void vlib_validate_simple_counter (vlib_simple_counter_main_t * cm, |
| 299 | u32 index); |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 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 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 306 | void vlib_validate_combined_counter (vlib_combined_counter_main_t * cm, |
| 307 | u32 index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 308 | |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 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 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 317 | #define vlib_counter_len(cm) vec_len((cm)->maxi) |
| 318 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 319 | serialize_function_t serialize_vlib_simple_counter_main, |
| 320 | unserialize_vlib_simple_counter_main; |
| 321 | serialize_function_t serialize_vlib_combined_counter_main, |
| 322 | unserialize_vlib_combined_counter_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 323 | |
| 324 | #endif /* included_vlib_counter_h */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 325 | |
| 326 | /* |
| 327 | * fd.io coding-style-patch-verification: ON |
| 328 | * |
| 329 | * Local Variables: |
| 330 | * eval: (c-set-style "gnu") |
| 331 | * End: |
| 332 | */ |