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. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 66 | } vlib_simple_counter_main_t; |
| 67 | |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 68 | /** The number of counters (not the number of per-thread counters) */ |
| 69 | u32 vlib_simple_counter_n_counters (const vlib_simple_counter_main_t * cm); |
| 70 | |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 71 | /** Increment a simple counter |
| 72 | @param cm - (vlib_simple_counter_main_t *) simple counter main pointer |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 73 | @param thread_index - (u32) the current cpu index |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 74 | @param index - (u32) index of the counter to increment |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 75 | @param increment - (u64) quantitiy to add to the counter |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 76 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 77 | always_inline void |
| 78 | vlib_increment_simple_counter (vlib_simple_counter_main_t * cm, |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 79 | u32 thread_index, u32 index, u64 increment) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 80 | { |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 81 | counter_t *my_counters; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 82 | |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 83 | my_counters = cm->counters[thread_index]; |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 84 | my_counters[index] += increment; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 87 | /** Get the value of a simple counter |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 88 | Scrapes the entire set of per-thread counters. Innacurate unless |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 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 | */ |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 96 | always_inline counter_t |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 97 | vlib_get_simple_counter (vlib_simple_counter_main_t * cm, u32 index) |
| 98 | { |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 99 | counter_t *my_counters; |
| 100 | counter_t v; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 101 | int i; |
| 102 | |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 103 | ASSERT (index < vlib_simple_counter_n_counters (cm)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 104 | |
| 105 | v = 0; |
| 106 | |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 107 | for (i = 0; i < vec_len (cm->counters); i++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 108 | { |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 109 | my_counters = cm->counters[i]; |
| 110 | v += my_counters[index]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | return v; |
| 114 | } |
| 115 | |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 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 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 122 | always_inline void |
| 123 | vlib_zero_simple_counter (vlib_simple_counter_main_t * cm, u32 index) |
| 124 | { |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 125 | counter_t *my_counters; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 126 | int i; |
| 127 | |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 128 | ASSERT (index < vlib_simple_counter_n_counters (cm)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 129 | |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 130 | for (i = 0; i < vec_len (cm->counters); i++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 131 | { |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 132 | my_counters = cm->counters[i]; |
| 133 | my_counters[index] = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 134 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 137 | /** Combined counter to hold both packets and byte differences. |
| 138 | */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 139 | typedef struct |
| 140 | { |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 141 | counter_t packets; /**< packet counter */ |
| 142 | counter_t bytes; /**< byte counter */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 143 | } vlib_counter_t; |
| 144 | |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 145 | /** Add two combined counters, results in the first counter |
Chris Luke | d4024f5 | 2016-09-06 09:32:36 -0400 | [diff] [blame] | 146 | @param [in,out] a - (vlib_counter_t *) dst counter |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 147 | @param b - (vlib_counter_t *) src counter |
| 148 | */ |
| 149 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 150 | always_inline void |
| 151 | vlib_counter_add (vlib_counter_t * a, vlib_counter_t * b) |
| 152 | { |
| 153 | a->packets += b->packets; |
| 154 | a->bytes += b->bytes; |
| 155 | } |
| 156 | |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 157 | /** Subtract combined counters, results in the first counter |
Chris Luke | d4024f5 | 2016-09-06 09:32:36 -0400 | [diff] [blame] | 158 | @param [in,out] a - (vlib_counter_t *) dst counter |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 159 | @param b - (vlib_counter_t *) src counter |
| 160 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 161 | always_inline void |
| 162 | vlib_counter_sub (vlib_counter_t * a, vlib_counter_t * b) |
| 163 | { |
| 164 | ASSERT (a->packets >= b->packets); |
| 165 | ASSERT (a->bytes >= b->bytes); |
| 166 | a->packets -= b->packets; |
| 167 | a->bytes -= b->bytes; |
| 168 | } |
| 169 | |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 170 | /** Clear a combined counter |
| 171 | @param a - (vlib_counter_t *) counter to clear |
| 172 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 173 | always_inline void |
| 174 | vlib_counter_zero (vlib_counter_t * a) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 175 | { |
| 176 | a->packets = a->bytes = 0; |
| 177 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 178 | |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 179 | /** A collection of combined counters */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 180 | typedef struct |
| 181 | { |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 182 | vlib_counter_t **counters; /**< Per-thread u64 non-atomic counter pairs */ |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 183 | vlib_counter_t *value_at_last_serialize; /**< Counter values as of last serialize. */ |
| 184 | u32 last_incremental_serialize_index; /**< Last counter index serialized incrementally. */ |
| 185 | char *name; /**< The counter collection's name. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 186 | } vlib_combined_counter_main_t; |
| 187 | |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 188 | /** The number of counters (not the number of per-thread counters) */ |
| 189 | u32 vlib_combined_counter_n_counters (const vlib_combined_counter_main_t * |
| 190 | cm); |
| 191 | |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 192 | /** Clear a collection of simple counters |
| 193 | @param cm - (vlib_simple_counter_main_t *) collection to clear |
| 194 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 195 | void vlib_clear_simple_counters (vlib_simple_counter_main_t * cm); |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 196 | |
| 197 | /** Clear a collection of combined counters |
| 198 | @param cm - (vlib_combined_counter_main_t *) collection to clear |
| 199 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 200 | void vlib_clear_combined_counters (vlib_combined_counter_main_t * cm); |
| 201 | |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 202 | /** Increment a combined counter |
| 203 | @param cm - (vlib_combined_counter_main_t *) comined counter main pointer |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 204 | @param thread_index - (u32) the current cpu index |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 205 | @param index - (u32) index of the counter to increment |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 206 | @param packet_increment - (u64) number of packets to add to the counter |
| 207 | @param byte_increment - (u64) number of bytes to add to the counter |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 208 | */ |
| 209 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 210 | always_inline void |
| 211 | vlib_increment_combined_counter (vlib_combined_counter_main_t * cm, |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 212 | u32 thread_index, |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 213 | u32 index, u64 n_packets, u64 n_bytes) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 214 | { |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 215 | vlib_counter_t *my_counters; |
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 | /* Use this CPU's counter array */ |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 218 | my_counters = cm->counters[thread_index]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 219 | |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 220 | my_counters[index].packets += n_packets; |
| 221 | my_counters[index].bytes += n_bytes; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 224 | /** Pre-fetch a per-thread combined counter for the given object index */ |
| 225 | always_inline void |
| 226 | vlib_prefetch_combined_counter (const vlib_combined_counter_main_t * cm, |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 227 | u32 thread_index, u32 index) |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 228 | { |
| 229 | vlib_counter_t *cpu_counters; |
| 230 | |
| 231 | /* |
| 232 | * This CPU's index is assumed to already be in cache |
| 233 | */ |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 234 | cpu_counters = cm->counters[thread_index]; |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 235 | CLIB_PREFETCH (cpu_counters + index, CLIB_CACHE_LINE_BYTES, STORE); |
Neale Ranns | 044183f | 2017-01-24 01:34:25 -0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 239 | /** 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] | 240 | Scrapes the entire set of per-thread counters. Innacurate unless |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 241 | worker threads which might increment the counter are |
| 242 | barrier-synchronized |
| 243 | |
| 244 | @param cm - (vlib_combined_counter_main_t *) combined counter main pointer |
| 245 | @param index - (u32) index of the combined counter to fetch |
| 246 | @param result [out] - (vlib_counter_t *) result stored here |
| 247 | */ |
| 248 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 249 | static inline void |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 250 | vlib_get_combined_counter (const vlib_combined_counter_main_t * cm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 251 | u32 index, vlib_counter_t * result) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 252 | { |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 253 | vlib_counter_t *my_counters, *counter; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 254 | int i; |
| 255 | |
| 256 | result->packets = 0; |
| 257 | result->bytes = 0; |
| 258 | |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 259 | for (i = 0; i < vec_len (cm->counters); i++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 260 | { |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 261 | my_counters = 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 | counter = vec_elt_at_index (my_counters, index); |
| 264 | result->packets += counter->packets; |
| 265 | result->bytes += counter->bytes; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 266 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 269 | /** Clear a combined counter |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 270 | Clears the set of per-thread counters. |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 271 | |
| 272 | @param cm - (vlib_combined_counter_main_t *) combined counter main pointer |
| 273 | @param index - (u32) index of the counter to clear |
| 274 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 275 | always_inline void |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 276 | vlib_zero_combined_counter (vlib_combined_counter_main_t * cm, u32 index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 277 | { |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 278 | vlib_counter_t *my_counters, *counter; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 279 | int i; |
| 280 | |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 281 | for (i = 0; i < vec_len (cm->counters); i++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 282 | { |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 283 | my_counters = 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 | counter = vec_elt_at_index (my_counters, index); |
| 286 | counter->packets = 0; |
| 287 | counter->bytes = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 288 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 291 | /** validate a simple counter |
| 292 | @param cm - (vlib_simple_counter_main_t *) pointer to the counter collection |
| 293 | @param index - (u32) index of the counter to validate |
| 294 | */ |
| 295 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 296 | void vlib_validate_simple_counter (vlib_simple_counter_main_t * cm, |
| 297 | u32 index); |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 298 | /** validate a combined counter |
| 299 | @param cm - (vlib_combined_counter_main_t *) pointer to the counter |
| 300 | collection |
| 301 | @param index - (u32) index of the counter to validate |
| 302 | */ |
| 303 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 304 | void vlib_validate_combined_counter (vlib_combined_counter_main_t * cm, |
| 305 | u32 index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 306 | |
Dave Barach | 6353920 | 2016-08-11 17:21:02 -0400 | [diff] [blame] | 307 | /** Obtain the number of simple or combined counters allocated. |
| 308 | A macro which reduces to to vec_len(cm->maxi), the answer in either |
| 309 | case. |
| 310 | |
| 311 | @param cm - (vlib_simple_counter_main_t) or |
| 312 | (vlib_combined_counter_main_t) the counter collection to interrogate |
| 313 | @returns vec_len(cm->maxi) |
| 314 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 315 | #define vlib_counter_len(cm) vec_len((cm)->maxi) |
| 316 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 317 | serialize_function_t serialize_vlib_simple_counter_main, |
| 318 | unserialize_vlib_simple_counter_main; |
| 319 | serialize_function_t serialize_vlib_combined_counter_main, |
| 320 | unserialize_vlib_combined_counter_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 321 | |
| 322 | #endif /* included_vlib_counter_h */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 323 | |
| 324 | /* |
| 325 | * fd.io coding-style-patch-verification: ON |
| 326 | * |
| 327 | * Local Variables: |
| 328 | * eval: (c-set-style "gnu") |
| 329 | * End: |
| 330 | */ |