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 | Copyright (c) 2005,2009 Eliot Dresselhaus |
| 17 | |
| 18 | Permission is hereby granted, free of charge, to any person obtaining |
| 19 | a copy of this software and associated documentation files (the |
| 20 | "Software"), to deal in the Software without restriction, including |
| 21 | without limitation the rights to use, copy, modify, merge, publish, |
| 22 | distribute, sublicense, and/or sell copies of the Software, and to |
| 23 | permit persons to whom the Software is furnished to do so, subject to |
| 24 | the following conditions: |
| 25 | |
| 26 | The above copyright notice and this permission notice shall be |
| 27 | included in all copies or substantial portions of the Software. |
| 28 | |
| 29 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 30 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 31 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 32 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 33 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 34 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 35 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 36 | */ |
| 37 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 38 | /* High speed event logger */ |
| 39 | |
| 40 | /** \file |
| 41 | The fine-grained event logger allows lightweight, thread-safe |
| 42 | event logging at minimum cost. In typical operation, logging |
| 43 | a single event costs around 80ns on x86_64. It's appropriate |
| 44 | for at-least per-frame event-logging in vector packet processing. |
| 45 | |
| 46 | See https://wiki.fd.io/view/VPP/elog for more information. |
| 47 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 48 | |
| 49 | #ifndef included_clib_elog_h |
| 50 | #define included_clib_elog_h |
| 51 | |
| 52 | #include <vppinfra/cache.h> |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 53 | #include <vppinfra/error.h> /* for ASSERT */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 54 | #include <vppinfra/serialize.h> |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 55 | #include <vppinfra/time.h> /* for clib_cpu_time_now */ |
Dave Barach | 9c8ca8d | 2019-06-14 12:02:38 -0400 | [diff] [blame] | 56 | #include <vppinfra/hash.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 57 | #include <vppinfra/mhash.h> |
| 58 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 59 | typedef struct |
| 60 | { |
| 61 | union |
| 62 | { |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 63 | /** Absolute time stamp in CPU clock cycles. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 64 | u64 time_cycles; |
| 65 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 66 | /** Absolute time as floating point number in seconds. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 67 | f64 time; |
| 68 | }; |
| 69 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 70 | /** Event type index. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 71 | u16 type; |
| 72 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 73 | /** Track for this event. Tracks allow events to be sorted and |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 74 | displayed by track. Think of 2 dimensional display with time and |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 75 | track being the x and y axes. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 76 | u16 track; |
| 77 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 78 | /** 20-bytes of data follows, pads to 32 bytes. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 79 | u8 data[20]; |
| 80 | } elog_event_t; |
| 81 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 82 | typedef struct |
| 83 | { |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 84 | /** Type index plus one assigned to this type. |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 85 | This is used to mark type as seen. */ |
| 86 | u32 type_index_plus_one; |
| 87 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 88 | /** String table as a vector constructed when type is registered. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 89 | char **enum_strings_vector; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 90 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 91 | /** Format string. (example: "my-event (%d,%d)"). */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 92 | char *format; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 93 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 94 | /** Specifies how arguments to format are parsed from event data. |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 95 | String of characters '0' '1' or '2' '3' to specify log2 size of data |
| 96 | (e.g. for u8, u16, u32 or u64), |
| 97 | 's' means a null-terminated C string |
| 98 | 't' means argument is an index into enum string table for this type. |
| 99 | 'e' is a float, |
| 100 | 'f' is a double. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 101 | char *format_args; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 102 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 103 | /** Function name generating event. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 104 | char *function; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 105 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 106 | /** Number of elements in string enum table. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 107 | u32 n_enum_strings; |
| 108 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 109 | /** String table for enum/number to string formatting. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 110 | char *enum_strings[]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 111 | } elog_event_type_t; |
| 112 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 113 | typedef struct |
| 114 | { |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 115 | /** Track name vector. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 116 | char *name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 117 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 118 | /** Set to one when track has been added to |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 119 | main structure. */ |
| 120 | u32 track_index_plus_one; |
| 121 | } elog_track_t; |
| 122 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 123 | typedef struct |
| 124 | { |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 125 | /** CPU cycle counter. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 126 | u64 cpu; |
| 127 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 128 | /** OS timer in nano secs since epoch 3/30/2017, see elog_time_now() */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 129 | u64 os_nsec; |
| 130 | } elog_time_stamp_t; |
| 131 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 132 | typedef struct |
| 133 | { |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 134 | /** Total number of events in buffer. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 135 | u32 n_total_events; |
| 136 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 137 | /** When count reaches limit logging is disabled. This is |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 138 | used for event triggers. */ |
| 139 | u32 n_total_events_disable_limit; |
| 140 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 141 | /** Dummy event to use when logger is disabled. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 142 | elog_event_t dummy_event; |
| 143 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 144 | /** Power of 2 number of elements in ring. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 145 | uword event_ring_size; |
| 146 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 147 | /** Vector of events (circular buffer). Power of 2 size. |
| 148 | Used when events are being collected. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 149 | elog_event_t *event_ring; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 150 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 151 | /** Vector of event types. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 152 | elog_event_type_t *event_types; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 153 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 154 | /** Hash table mapping type format to type index. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 155 | uword *event_type_by_format; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 156 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 157 | /** Events may refer to strings in string table. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 158 | char *string_table; |
Dave Barach | 9c8ca8d | 2019-06-14 12:02:38 -0400 | [diff] [blame] | 159 | uword *string_table_hash; |
| 160 | u8 *string_table_tmp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 161 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 162 | /** Vector of tracks. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 163 | elog_track_t *tracks; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 164 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 165 | /** Default track. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 166 | elog_track_t default_track; |
| 167 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 168 | /** Place holder for CPU clock frequency. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 169 | clib_time_t cpu_timer; |
| 170 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 171 | /** Timestamps */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 172 | elog_time_stamp_t init_time, serialize_time; |
| 173 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 174 | /** SMP lock, non-zero means locking required */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 175 | uword *lock; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 176 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 177 | /** Use serialize_time and init_time to give estimate for |
| 178 | cpu clock frequency. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 179 | f64 nsec_per_cpu_clock; |
| 180 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 181 | /** Vector of events converted to generic form after collection. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 182 | elog_event_t *events; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 183 | } elog_main_t; |
| 184 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 185 | /** @brief Return number of events in the event-log buffer |
| 186 | @param em elog_main_t * |
| 187 | @return number of events in the buffer |
| 188 | */ |
| 189 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 190 | always_inline uword |
| 191 | elog_n_events_in_buffer (elog_main_t * em) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 192 | { |
| 193 | return clib_min (em->n_total_events, em->event_ring_size); |
| 194 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 195 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 196 | /** @brief Return number of events which can fit in the event buffer |
| 197 | @param em elog_main_t * |
| 198 | @return number of events which can fit in the buffer |
| 199 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 200 | always_inline uword |
| 201 | elog_buffer_capacity (elog_main_t * em) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 202 | { |
| 203 | return em->event_ring_size; |
| 204 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 205 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 206 | /** @brief Reset the event buffer |
| 207 | @param em elog_main_t * |
| 208 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 209 | always_inline void |
| 210 | elog_reset_buffer (elog_main_t * em) |
| 211 | { |
| 212 | em->n_total_events = 0; |
| 213 | em->n_total_events_disable_limit = ~0; |
| 214 | } |
| 215 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 216 | /** @brief Enable or disable event logging |
| 217 | @param em elog_main_t * |
| 218 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 219 | always_inline void |
| 220 | elog_enable_disable (elog_main_t * em, int is_enabled) |
| 221 | { |
| 222 | em->n_total_events = 0; |
Damjan Marion | 2c29d75 | 2015-12-18 10:26:56 +0100 | [diff] [blame] | 223 | em->n_total_events_disable_limit = is_enabled ? ~0 : 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 226 | /** @brief disable logging after specified number of ievents have been logged. |
| 227 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 228 | This is used as a "debug trigger" when a certain event has occurred. |
| 229 | Events will be logged both before and after the "event" but the |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 230 | event will not be lost as long as N < RING_SIZE. |
| 231 | |
| 232 | @param em elog_main_t * |
| 233 | @param n uword number of events before disabling event logging |
| 234 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 235 | always_inline void |
| 236 | elog_disable_after_events (elog_main_t * em, uword n) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 237 | { |
| 238 | em->n_total_events_disable_limit = em->n_total_events + n; |
| 239 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 240 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 241 | /* @brief mid-buffer logic-analyzer trigger |
| 242 | |
| 243 | Currently, only midpoint triggering is supported, but it's pretty obvious |
| 244 | how to generalize the scheme. |
| 245 | @param em elog_main_t * |
| 246 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 247 | always_inline void |
| 248 | elog_disable_trigger (elog_main_t * em) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 249 | { |
| 250 | em->n_total_events_disable_limit = |
| 251 | em->n_total_events + vec_len (em->event_ring) / 2; |
| 252 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 253 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 254 | /** @brief register an event type |
| 255 | @param em elog_main_t * |
| 256 | @param t elog_event_type_t * event to register |
| 257 | @return type index |
| 258 | @warning Typically not called directly |
| 259 | */ |
| 260 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 261 | word elog_event_type_register (elog_main_t * em, elog_event_type_t * t); |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 262 | |
| 263 | /** @brief register an event track |
| 264 | @param em elog_main_t * |
| 265 | @param t elog_track_t * track to register |
| 266 | @return track index |
| 267 | @note this function is often called directly |
| 268 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 269 | word elog_track_register (elog_main_t * em, elog_track_t * t); |
| 270 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 271 | /** @brief event logging enabled predicate |
| 272 | @param em elog_main_t * |
| 273 | @return 1 if enabled, 0 if not enabled |
| 274 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 275 | always_inline uword |
| 276 | elog_is_enabled (elog_main_t * em) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 277 | { |
| 278 | return em->n_total_events < em->n_total_events_disable_limit; |
| 279 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 280 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 281 | /** @brief Allocate an event to be filled in by the caller |
| 282 | |
| 283 | Not normally called directly; this function underlies the |
| 284 | ELOG_DATA and ELOG_TRACK_DATA macros |
| 285 | |
| 286 | @param em elog_main_t * |
| 287 | @param type elog_event_type_t * type |
| 288 | @param track elog_track_t * track |
| 289 | @param cpu_time u64 current cpu tick value |
| 290 | @returns event to be filled in |
| 291 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 292 | always_inline void * |
| 293 | elog_event_data_inline (elog_main_t * em, |
| 294 | elog_event_type_t * type, |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 295 | elog_track_t * track, u64 cpu_time) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 296 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 297 | elog_event_t *e; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 298 | uword ei; |
| 299 | word type_index, track_index; |
| 300 | |
| 301 | /* Return the user dummy memory to scribble data into. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 302 | if (PREDICT_FALSE (!elog_is_enabled (em))) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 303 | return em->dummy_event.data; |
| 304 | |
| 305 | type_index = (word) type->type_index_plus_one - 1; |
| 306 | track_index = (word) track->track_index_plus_one - 1; |
| 307 | if (PREDICT_FALSE ((type_index | track_index) < 0)) |
| 308 | { |
| 309 | if (type_index < 0) |
| 310 | type_index = elog_event_type_register (em, type); |
| 311 | if (track_index < 0) |
| 312 | track_index = elog_track_register (em, track); |
| 313 | } |
| 314 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 315 | ASSERT (track_index < vec_len (em->tracks)); |
| 316 | ASSERT (is_pow2 (vec_len (em->event_ring))); |
| 317 | |
| 318 | if (em->lock) |
Sirshak Das | 2f6d7bb | 2018-10-03 22:53:51 +0000 | [diff] [blame] | 319 | ei = clib_atomic_fetch_add (&em->n_total_events, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 320 | else |
| 321 | ei = em->n_total_events++; |
| 322 | |
| 323 | ei &= em->event_ring_size - 1; |
| 324 | e = vec_elt_at_index (em->event_ring, ei); |
| 325 | |
| 326 | e->time_cycles = cpu_time; |
| 327 | e->type = type_index; |
| 328 | e->track = track_index; |
| 329 | |
| 330 | /* Return user data for caller to fill in. */ |
| 331 | return e->data; |
| 332 | } |
| 333 | |
| 334 | /* External version of inline. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 335 | void *elog_event_data (elog_main_t * em, |
| 336 | elog_event_type_t * type, |
| 337 | elog_track_t * track, u64 cpu_time); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 338 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 339 | /** @brief Allocate an event to be filled in by the caller, non-inline |
| 340 | |
| 341 | Not normally called directly; this function underlies the |
| 342 | ELOG_DATA and ELOG_TRACK_DATA macros |
| 343 | |
| 344 | @param em elog_main_t * |
| 345 | @param type elog_event_type_t * type |
| 346 | @param track elog_track_t * track |
| 347 | @param cpu_time u64 current cpu tick value |
| 348 | @returns event to be filled in |
| 349 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 350 | always_inline void * |
| 351 | elog_event_data_not_inline (elog_main_t * em, |
| 352 | elog_event_type_t * type, |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 353 | elog_track_t * track, u64 cpu_time) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 354 | { |
| 355 | /* Return the user dummy memory to scribble data into. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 356 | if (PREDICT_FALSE (!elog_is_enabled (em))) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 357 | return em->dummy_event.data; |
| 358 | return elog_event_data (em, type, track, cpu_time); |
| 359 | } |
| 360 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 361 | /** @brief Log a single-datum event |
| 362 | @param em elog_main_t * |
| 363 | @param type elog_event_type_t * type |
| 364 | @param data u32 single datum to capture |
| 365 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 366 | always_inline void |
| 367 | elog (elog_main_t * em, elog_event_type_t * type, u32 data) |
| 368 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 369 | u32 *d = elog_event_data_not_inline (em, |
| 370 | type, |
| 371 | &em->default_track, |
| 372 | clib_cpu_time_now ()); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 373 | d[0] = data; |
| 374 | } |
| 375 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 376 | /** @brief Log a single-datum event, inline version |
| 377 | @param em elog_main_t * |
| 378 | @param type elog_event_type_t * type |
| 379 | @param data u32 single datum to capture |
| 380 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 381 | always_inline void |
| 382 | elog_inline (elog_main_t * em, elog_event_type_t * type, u32 data) |
| 383 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 384 | u32 *d = elog_event_data_inline (em, |
| 385 | type, |
| 386 | &em->default_track, |
| 387 | clib_cpu_time_now ()); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 388 | d[0] = data; |
| 389 | } |
| 390 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 391 | /** @brief Log a single-datum event to a specific track, non-inline version |
| 392 | @param em elog_main_t * |
| 393 | @param type elog_event_type_t * type |
| 394 | @param type elog_event_track_t * track |
| 395 | @param data u32 single datum to capture |
| 396 | */ |
Dave Barach | fb6e59d | 2016-03-26 18:45:42 -0400 | [diff] [blame] | 397 | always_inline void |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 398 | elog_track (elog_main_t * em, elog_event_type_t * type, elog_track_t * track, |
| 399 | u32 data) |
Dave Barach | fb6e59d | 2016-03-26 18:45:42 -0400 | [diff] [blame] | 400 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 401 | u32 *d = elog_event_data_not_inline (em, |
| 402 | type, |
| 403 | track, |
| 404 | clib_cpu_time_now ()); |
Dave Barach | fb6e59d | 2016-03-26 18:45:42 -0400 | [diff] [blame] | 405 | d[0] = data; |
| 406 | } |
| 407 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 408 | /** @brief Log a single-datum event to a specific track |
| 409 | @param em elog_main_t * |
| 410 | @param type elog_event_type_t * type |
| 411 | @param type elog_event_track_t * track |
| 412 | @param data u32 single datum to capture |
| 413 | */ |
Dave Barach | fb6e59d | 2016-03-26 18:45:42 -0400 | [diff] [blame] | 414 | always_inline void |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 415 | elog_track_inline (elog_main_t * em, elog_event_type_t * type, |
| 416 | elog_track_t * track, u32 data) |
Dave Barach | fb6e59d | 2016-03-26 18:45:42 -0400 | [diff] [blame] | 417 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 418 | u32 *d = elog_event_data_inline (em, |
| 419 | type, |
| 420 | track, |
| 421 | clib_cpu_time_now ()); |
Dave Barach | fb6e59d | 2016-03-26 18:45:42 -0400 | [diff] [blame] | 422 | d[0] = data; |
| 423 | } |
| 424 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 425 | always_inline void * |
| 426 | elog_data (elog_main_t * em, elog_event_type_t * type, elog_track_t * track) |
| 427 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 428 | return elog_event_data_not_inline (em, type, track, clib_cpu_time_now ()); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | always_inline void * |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 432 | elog_data_inline (elog_main_t * em, elog_event_type_t * type, |
| 433 | elog_track_t * track) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 434 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 435 | return elog_event_data_inline (em, type, track, clib_cpu_time_now ()); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | /* Macro shorthands for generating/declaring events. */ |
| 439 | #define __ELOG_TYPE_VAR(f) f |
| 440 | #define __ELOG_TRACK_VAR(f) f |
| 441 | |
| 442 | #define ELOG_TYPE_DECLARE(f) static elog_event_type_t __ELOG_TYPE_VAR(f) |
| 443 | |
| 444 | #define ELOG_TYPE_INIT_FORMAT_AND_FUNCTION(fmt,func) \ |
| 445 | { .format = fmt, .function = func, } |
| 446 | |
| 447 | #define ELOG_TYPE_INIT(fmt) \ |
| 448 | ELOG_TYPE_INIT_FORMAT_AND_FUNCTION(fmt,(char *) __FUNCTION__) |
| 449 | |
| 450 | #define ELOG_TYPE_DECLARE_HELPER(f,fmt,func) \ |
| 451 | static elog_event_type_t __ELOG_TYPE_VAR(f) = \ |
| 452 | ELOG_TYPE_INIT_FORMAT_AND_FUNCTION (fmt, func) |
| 453 | |
| 454 | #define ELOG_TYPE_DECLARE_FORMAT_AND_FUNCTION(f,fmt) \ |
| 455 | ELOG_TYPE_DECLARE_HELPER (f, fmt, (char *) __FUNCTION__) |
| 456 | |
| 457 | #define ELOG_TYPE_DECLARE_FORMAT(f,fmt) \ |
| 458 | ELOG_TYPE_DECLARE_HELPER (f, fmt, 0) |
| 459 | |
| 460 | /* Shorthands with and without __FUNCTION__. |
| 461 | D for decimal; X for hex. F for __FUNCTION__. */ |
| 462 | #define ELOG_TYPE(f,fmt) ELOG_TYPE_DECLARE_FORMAT_AND_FUNCTION(f,fmt) |
| 463 | #define ELOG_TYPE_D(f) ELOG_TYPE_DECLARE_FORMAT (f, #f " %d") |
| 464 | #define ELOG_TYPE_X(f) ELOG_TYPE_DECLARE_FORMAT (f, #f " 0x%x") |
| 465 | #define ELOG_TYPE_DF(f) ELOG_TYPE_DECLARE_FORMAT_AND_FUNCTION (f, #f " %d") |
| 466 | #define ELOG_TYPE_XF(f) ELOG_TYPE_DECLARE_FORMAT_AND_FUNCTION (f, #f " 0x%x") |
| 467 | #define ELOG_TYPE_FD(f) ELOG_TYPE_DECLARE_FORMAT_AND_FUNCTION (f, #f " %d") |
| 468 | #define ELOG_TYPE_FX(f) ELOG_TYPE_DECLARE_FORMAT_AND_FUNCTION (f, #f " 0x%x") |
| 469 | |
| 470 | #define ELOG_TRACK_DECLARE(f) static elog_track_t __ELOG_TRACK_VAR(f) |
| 471 | #define ELOG_TRACK(f) ELOG_TRACK_DECLARE(f) = { .name = #f, } |
| 472 | |
| 473 | /* Log 32 bits of data. */ |
| 474 | #define ELOG(em,f,data) elog ((em), &__ELOG_TYPE_VAR(f), data) |
| 475 | #define ELOG_INLINE(em,f,data) elog_inline ((em), &__ELOG_TYPE_VAR(f), data) |
| 476 | |
| 477 | /* Return data pointer to fill in. */ |
| 478 | #define ELOG_TRACK_DATA(em,f,track) \ |
| 479 | elog_data ((em), &__ELOG_TYPE_VAR(f), &__ELOG_TRACK_VAR(track)) |
| 480 | #define ELOG_TRACK_DATA_INLINE(em,f,track) \ |
| 481 | elog_data_inline ((em), &__ELOG_TYPE_VAR(f), &__ELOG_TRACK_VAR(track)) |
| 482 | |
| 483 | /* Shorthand with default track. */ |
| 484 | #define ELOG_DATA(em,f) elog_data ((em), &__ELOG_TYPE_VAR (f), &(em)->default_track) |
| 485 | #define ELOG_DATA_INLINE(em,f) elog_data_inline ((em), &__ELOG_TYPE_VAR (f), &(em)->default_track) |
| 486 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 487 | /** @brief add a string to the event-log string table |
| 488 | |
| 489 | Often combined with hashing and the T4 elog format specifier to |
| 490 | display complex strings in offline tooling |
| 491 | |
| 492 | @param em elog_main_t * |
| 493 | @param format char * |
| 494 | @param VARARGS |
| 495 | @return u32 index to add to event log |
| 496 | */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 497 | u32 elog_string (elog_main_t * em, char *format, ...); |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 498 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 499 | void elog_time_now (elog_time_stamp_t * et); |
| 500 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 501 | /** @brief convert event ring events to events, and return them as a vector. |
| 502 | @param em elog_main_t * |
| 503 | @return event vector with timestamps in f64 seconds |
| 504 | @note sets em->events to resulting vector. |
| 505 | */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 506 | elog_event_t *elog_get_events (elog_main_t * em); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 507 | |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 508 | /** @brief convert event ring events to events, and return them as a vector. |
| 509 | @param em elog_main_t * |
| 510 | @return event vector with timestamps in f64 seconds |
| 511 | @note no side effects |
| 512 | */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 513 | elog_event_t *elog_peek_events (elog_main_t * em); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 514 | |
| 515 | /* Merge two logs, add supplied track tags. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 516 | void elog_merge (elog_main_t * dst, u8 * dst_tag, |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 517 | elog_main_t * src, u8 * src_tag, f64 align_tweak); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 518 | |
| 519 | /* 2 arguments elog_main_t and elog_event_t to format event or track name. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 520 | u8 *format_elog_event (u8 * s, va_list * va); |
Florin Coras | aefbede | 2018-12-19 13:07:49 -0800 | [diff] [blame] | 521 | u8 *format_elog_track_name (u8 * s, va_list * va); |
| 522 | u8 *format_elog_track (u8 * s, va_list * args); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 523 | |
| 524 | void serialize_elog_main (serialize_main_t * m, va_list * va); |
| 525 | void unserialize_elog_main (serialize_main_t * m, va_list * va); |
| 526 | |
| 527 | void elog_init (elog_main_t * em, u32 n_events); |
Dave Barach | e5389bb | 2016-03-28 17:12:19 -0400 | [diff] [blame] | 528 | void elog_alloc (elog_main_t * em, u32 n_events); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 529 | |
| 530 | #ifdef CLIB_UNIX |
| 531 | always_inline clib_error_t * |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 532 | elog_write_file (elog_main_t * em, char *clib_file, int flush_ring) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 533 | { |
| 534 | serialize_main_t m; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 535 | clib_error_t *error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 536 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 537 | error = serialize_open_clib_file (&m, clib_file); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 538 | if (error) |
| 539 | return error; |
Dave Barach | 903fd51 | 2017-04-01 11:07:40 -0400 | [diff] [blame] | 540 | error = serialize (&m, serialize_elog_main, em, flush_ring); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 541 | if (!error) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 542 | serialize_close (&m); |
| 543 | return error; |
| 544 | } |
| 545 | |
| 546 | always_inline clib_error_t * |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 547 | elog_read_file (elog_main_t * em, char *clib_file) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 548 | { |
| 549 | serialize_main_t m; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 550 | clib_error_t *error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 551 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 552 | error = unserialize_open_clib_file (&m, clib_file); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 553 | if (error) |
| 554 | return error; |
| 555 | error = unserialize (&m, unserialize_elog_main, em); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 556 | if (!error) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 557 | unserialize_close (&m); |
| 558 | return error; |
| 559 | } |
| 560 | |
| 561 | #endif /* CLIB_UNIX */ |
| 562 | |
| 563 | #endif /* included_clib_elog_h */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 564 | |
| 565 | /* |
| 566 | * fd.io coding-style-patch-verification: ON |
| 567 | * |
| 568 | * Local Variables: |
| 569 | * eval: (c-set-style "gnu") |
| 570 | * End: |
| 571 | */ |