blob: 9f957e336d01b53b5db9ed41519c9a89b1c68fba [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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 Barach903fd512017-04-01 11:07:40 -040038/* 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 Warnickecb9cada2015-12-08 15:45:58 -070048
49#ifndef included_clib_elog_h
50#define included_clib_elog_h
51
52#include <vppinfra/cache.h>
Dave Barachc3799992016-08-15 11:12:27 -040053#include <vppinfra/error.h> /* for ASSERT */
Ed Warnickecb9cada2015-12-08 15:45:58 -070054#include <vppinfra/serialize.h>
Dave Barachc3799992016-08-15 11:12:27 -040055#include <vppinfra/time.h> /* for clib_cpu_time_now */
Dave Barach9c8ca8d2019-06-14 12:02:38 -040056#include <vppinfra/hash.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070057#include <vppinfra/mhash.h>
58
Dave Barachc3799992016-08-15 11:12:27 -040059typedef struct
60{
61 union
62 {
Dave Barach903fd512017-04-01 11:07:40 -040063 /** Absolute time stamp in CPU clock cycles. */
Ed Warnickecb9cada2015-12-08 15:45:58 -070064 u64 time_cycles;
65
Dave Barach903fd512017-04-01 11:07:40 -040066 /** Absolute time as floating point number in seconds. */
Ed Warnickecb9cada2015-12-08 15:45:58 -070067 f64 time;
68 };
69
Dave Barach903fd512017-04-01 11:07:40 -040070 /** Event type index. */
Ed Warnickecb9cada2015-12-08 15:45:58 -070071 u16 type;
72
Dave Barach903fd512017-04-01 11:07:40 -040073 /** Track for this event. Tracks allow events to be sorted and
Ed Warnickecb9cada2015-12-08 15:45:58 -070074 displayed by track. Think of 2 dimensional display with time and
Dave Barachc3799992016-08-15 11:12:27 -040075 track being the x and y axes. */
Ed Warnickecb9cada2015-12-08 15:45:58 -070076 u16 track;
77
Dave Barach903fd512017-04-01 11:07:40 -040078 /** 20-bytes of data follows, pads to 32 bytes. */
Ed Warnickecb9cada2015-12-08 15:45:58 -070079 u8 data[20];
80} elog_event_t;
81
Dave Barachc3799992016-08-15 11:12:27 -040082typedef struct
83{
Dave Barach903fd512017-04-01 11:07:40 -040084 /** Type index plus one assigned to this type.
Ed Warnickecb9cada2015-12-08 15:45:58 -070085 This is used to mark type as seen. */
86 u32 type_index_plus_one;
87
Dave Barach903fd512017-04-01 11:07:40 -040088 /** String table as a vector constructed when type is registered. */
Dave Barachc3799992016-08-15 11:12:27 -040089 char **enum_strings_vector;
Ed Warnickecb9cada2015-12-08 15:45:58 -070090
Dave Barach903fd512017-04-01 11:07:40 -040091 /** Format string. (example: "my-event (%d,%d)"). */
Dave Barachc3799992016-08-15 11:12:27 -040092 char *format;
Ed Warnickecb9cada2015-12-08 15:45:58 -070093
Dave Barach903fd512017-04-01 11:07:40 -040094 /** Specifies how arguments to format are parsed from event data.
Ed Warnickecb9cada2015-12-08 15:45:58 -070095 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 Barachc3799992016-08-15 11:12:27 -0400101 char *format_args;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700102
Dave Barach903fd512017-04-01 11:07:40 -0400103 /** Function name generating event. */
Dave Barachc3799992016-08-15 11:12:27 -0400104 char *function;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700105
Dave Barach903fd512017-04-01 11:07:40 -0400106 /** Number of elements in string enum table. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700107 u32 n_enum_strings;
108
Dave Barach903fd512017-04-01 11:07:40 -0400109 /** String table for enum/number to string formatting. */
Dave Barachc3799992016-08-15 11:12:27 -0400110 char *enum_strings[];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700111} elog_event_type_t;
112
Dave Barachc3799992016-08-15 11:12:27 -0400113typedef struct
114{
Dave Barach903fd512017-04-01 11:07:40 -0400115 /** Track name vector. */
Dave Barachc3799992016-08-15 11:12:27 -0400116 char *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700117
Dave Barach903fd512017-04-01 11:07:40 -0400118 /** Set to one when track has been added to
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119 main structure. */
120 u32 track_index_plus_one;
121} elog_track_t;
122
Dave Barachc3799992016-08-15 11:12:27 -0400123typedef struct
124{
Dave Barach903fd512017-04-01 11:07:40 -0400125 /** CPU cycle counter. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700126 u64 cpu;
127
Dave Barach903fd512017-04-01 11:07:40 -0400128 /** OS timer in nano secs since epoch 3/30/2017, see elog_time_now() */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700129 u64 os_nsec;
130} elog_time_stamp_t;
131
Dave Barachc3799992016-08-15 11:12:27 -0400132typedef struct
133{
Dave Barach903fd512017-04-01 11:07:40 -0400134 /** Total number of events in buffer. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135 u32 n_total_events;
136
Dave Barach903fd512017-04-01 11:07:40 -0400137 /** When count reaches limit logging is disabled. This is
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138 used for event triggers. */
139 u32 n_total_events_disable_limit;
140
Dave Barach903fd512017-04-01 11:07:40 -0400141 /** Dummy event to use when logger is disabled. */
Dave Barach11fb09e2020-08-06 12:10:09 -0400142 elog_event_t placeholder_event;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143
Dave Barach903fd512017-04-01 11:07:40 -0400144 /** Power of 2 number of elements in ring. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700145 uword event_ring_size;
146
Dave Barach903fd512017-04-01 11:07:40 -0400147 /** Vector of events (circular buffer). Power of 2 size.
148 Used when events are being collected. */
Dave Barachc3799992016-08-15 11:12:27 -0400149 elog_event_t *event_ring;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150
Dave Barach903fd512017-04-01 11:07:40 -0400151 /** Vector of event types. */
Dave Barachc3799992016-08-15 11:12:27 -0400152 elog_event_type_t *event_types;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700153
Dave Barach903fd512017-04-01 11:07:40 -0400154 /** Hash table mapping type format to type index. */
Dave Barachc3799992016-08-15 11:12:27 -0400155 uword *event_type_by_format;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700156
Dave Barach903fd512017-04-01 11:07:40 -0400157 /** Events may refer to strings in string table. */
Dave Barachc3799992016-08-15 11:12:27 -0400158 char *string_table;
Dave Barach9c8ca8d2019-06-14 12:02:38 -0400159 uword *string_table_hash;
160 u8 *string_table_tmp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161
Dave Barach903fd512017-04-01 11:07:40 -0400162 /** Vector of tracks. */
Dave Barachc3799992016-08-15 11:12:27 -0400163 elog_track_t *tracks;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700164
Dave Barach903fd512017-04-01 11:07:40 -0400165 /** Default track. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166 elog_track_t default_track;
167
Dave Barach903fd512017-04-01 11:07:40 -0400168 /** Place holder for CPU clock frequency. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700169 clib_time_t cpu_timer;
170
Dave Barach903fd512017-04-01 11:07:40 -0400171 /** Timestamps */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172 elog_time_stamp_t init_time, serialize_time;
173
Dave Barach903fd512017-04-01 11:07:40 -0400174 /** SMP lock, non-zero means locking required */
Dave Barachc3799992016-08-15 11:12:27 -0400175 uword *lock;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700176
Dave Barach903fd512017-04-01 11:07:40 -0400177 /** Use serialize_time and init_time to give estimate for
178 cpu clock frequency. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179 f64 nsec_per_cpu_clock;
180
Dave Barach903fd512017-04-01 11:07:40 -0400181 /** Vector of events converted to generic form after collection. */
Dave Barachc3799992016-08-15 11:12:27 -0400182 elog_event_t *events;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700183} elog_main_t;
184
Dave Barach903fd512017-04-01 11:07:40 -0400185/** @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 Warnickecb9cada2015-12-08 15:45:58 -0700190always_inline uword
191elog_n_events_in_buffer (elog_main_t * em)
Dave Barachc3799992016-08-15 11:12:27 -0400192{
193 return clib_min (em->n_total_events, em->event_ring_size);
194}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700195
Dave Barach903fd512017-04-01 11:07:40 -0400196/** @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 Warnickecb9cada2015-12-08 15:45:58 -0700200always_inline uword
201elog_buffer_capacity (elog_main_t * em)
Dave Barachc3799992016-08-15 11:12:27 -0400202{
203 return em->event_ring_size;
204}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700205
Dave Barach903fd512017-04-01 11:07:40 -0400206/** @brief Reset the event buffer
207 @param em elog_main_t *
208*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700209always_inline void
210elog_reset_buffer (elog_main_t * em)
211{
212 em->n_total_events = 0;
213 em->n_total_events_disable_limit = ~0;
214}
215
Dave Barach903fd512017-04-01 11:07:40 -0400216/** @brief Enable or disable event logging
217 @param em elog_main_t *
218*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700219always_inline void
220elog_enable_disable (elog_main_t * em, int is_enabled)
221{
222 em->n_total_events = 0;
Damjan Marion2c29d752015-12-18 10:26:56 +0100223 em->n_total_events_disable_limit = is_enabled ? ~0 : 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700224}
225
Dave Barach903fd512017-04-01 11:07:40 -0400226/** @brief disable logging after specified number of ievents have been logged.
227
Ed Warnickecb9cada2015-12-08 15:45:58 -0700228 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 Barach903fd512017-04-01 11:07:40 -0400230 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 Warnickecb9cada2015-12-08 15:45:58 -0700235always_inline void
236elog_disable_after_events (elog_main_t * em, uword n)
Dave Barachc3799992016-08-15 11:12:27 -0400237{
238 em->n_total_events_disable_limit = em->n_total_events + n;
239}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700240
Dave Barach903fd512017-04-01 11:07:40 -0400241/* @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 Warnickecb9cada2015-12-08 15:45:58 -0700247always_inline void
248elog_disable_trigger (elog_main_t * em)
Dave Barachc3799992016-08-15 11:12:27 -0400249{
250 em->n_total_events_disable_limit =
251 em->n_total_events + vec_len (em->event_ring) / 2;
252}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700253
Dave Barach903fd512017-04-01 11:07:40 -0400254/** @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 Warnickecb9cada2015-12-08 15:45:58 -0700261word elog_event_type_register (elog_main_t * em, elog_event_type_t * t);
Dave Barach903fd512017-04-01 11:07:40 -0400262
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 Warnickecb9cada2015-12-08 15:45:58 -0700269word elog_track_register (elog_main_t * em, elog_track_t * t);
270
Dave Barach903fd512017-04-01 11:07:40 -0400271/** @brief event logging enabled predicate
272 @param em elog_main_t *
273 @return 1 if enabled, 0 if not enabled
274*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700275always_inline uword
276elog_is_enabled (elog_main_t * em)
Dave Barachc3799992016-08-15 11:12:27 -0400277{
278 return em->n_total_events < em->n_total_events_disable_limit;
279}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700280
Dave Barach903fd512017-04-01 11:07:40 -0400281/** @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 Warnickecb9cada2015-12-08 15:45:58 -0700292always_inline void *
293elog_event_data_inline (elog_main_t * em,
294 elog_event_type_t * type,
Dave Barachc3799992016-08-15 11:12:27 -0400295 elog_track_t * track, u64 cpu_time)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700296{
Dave Barachc3799992016-08-15 11:12:27 -0400297 elog_event_t *e;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700298 uword ei;
299 word type_index, track_index;
300
Dave Barach11fb09e2020-08-06 12:10:09 -0400301 /* Return the user placeholder memory to scribble data into. */
Dave Barachc3799992016-08-15 11:12:27 -0400302 if (PREDICT_FALSE (!elog_is_enabled (em)))
Dave Barach11fb09e2020-08-06 12:10:09 -0400303 return em->placeholder_event.data;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700304
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 Warnickecb9cada2015-12-08 15:45:58 -0700315 ASSERT (track_index < vec_len (em->tracks));
316 ASSERT (is_pow2 (vec_len (em->event_ring)));
317
318 if (em->lock)
Sirshak Das2f6d7bb2018-10-03 22:53:51 +0000319 ei = clib_atomic_fetch_add (&em->n_total_events, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700320 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 Barachc3799992016-08-15 11:12:27 -0400335void *elog_event_data (elog_main_t * em,
336 elog_event_type_t * type,
337 elog_track_t * track, u64 cpu_time);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700338
Dave Barach903fd512017-04-01 11:07:40 -0400339/** @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 Warnickecb9cada2015-12-08 15:45:58 -0700350always_inline void *
351elog_event_data_not_inline (elog_main_t * em,
352 elog_event_type_t * type,
Dave Barachc3799992016-08-15 11:12:27 -0400353 elog_track_t * track, u64 cpu_time)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700354{
Dave Barach11fb09e2020-08-06 12:10:09 -0400355 /* Return the user placeholder memory to scribble data into. */
Dave Barachc3799992016-08-15 11:12:27 -0400356 if (PREDICT_FALSE (!elog_is_enabled (em)))
Dave Barach11fb09e2020-08-06 12:10:09 -0400357 return em->placeholder_event.data;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700358 return elog_event_data (em, type, track, cpu_time);
359}
360
Dave Barach903fd512017-04-01 11:07:40 -0400361/** @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 Warnickecb9cada2015-12-08 15:45:58 -0700366always_inline void
367elog (elog_main_t * em, elog_event_type_t * type, u32 data)
368{
Dave Barachc3799992016-08-15 11:12:27 -0400369 u32 *d = elog_event_data_not_inline (em,
370 type,
371 &em->default_track,
372 clib_cpu_time_now ());
Ed Warnickecb9cada2015-12-08 15:45:58 -0700373 d[0] = data;
374}
375
Dave Barach903fd512017-04-01 11:07:40 -0400376/** @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 Warnickecb9cada2015-12-08 15:45:58 -0700381always_inline void
382elog_inline (elog_main_t * em, elog_event_type_t * type, u32 data)
383{
Dave Barachc3799992016-08-15 11:12:27 -0400384 u32 *d = elog_event_data_inline (em,
385 type,
386 &em->default_track,
387 clib_cpu_time_now ());
Ed Warnickecb9cada2015-12-08 15:45:58 -0700388 d[0] = data;
389}
390
Dave Barach903fd512017-04-01 11:07:40 -0400391/** @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 Barachfb6e59d2016-03-26 18:45:42 -0400397always_inline void
Dave Barachc3799992016-08-15 11:12:27 -0400398elog_track (elog_main_t * em, elog_event_type_t * type, elog_track_t * track,
399 u32 data)
Dave Barachfb6e59d2016-03-26 18:45:42 -0400400{
Dave Barachc3799992016-08-15 11:12:27 -0400401 u32 *d = elog_event_data_not_inline (em,
402 type,
403 track,
404 clib_cpu_time_now ());
Dave Barachfb6e59d2016-03-26 18:45:42 -0400405 d[0] = data;
406}
407
Dave Barach903fd512017-04-01 11:07:40 -0400408/** @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 Barachfb6e59d2016-03-26 18:45:42 -0400414always_inline void
Dave Barachc3799992016-08-15 11:12:27 -0400415elog_track_inline (elog_main_t * em, elog_event_type_t * type,
416 elog_track_t * track, u32 data)
Dave Barachfb6e59d2016-03-26 18:45:42 -0400417{
Dave Barachc3799992016-08-15 11:12:27 -0400418 u32 *d = elog_event_data_inline (em,
419 type,
420 track,
421 clib_cpu_time_now ());
Dave Barachfb6e59d2016-03-26 18:45:42 -0400422 d[0] = data;
423}
424
Ed Warnickecb9cada2015-12-08 15:45:58 -0700425always_inline void *
426elog_data (elog_main_t * em, elog_event_type_t * type, elog_track_t * track)
427{
Dave Barachc3799992016-08-15 11:12:27 -0400428 return elog_event_data_not_inline (em, type, track, clib_cpu_time_now ());
Ed Warnickecb9cada2015-12-08 15:45:58 -0700429}
430
431always_inline void *
Dave Barachc3799992016-08-15 11:12:27 -0400432elog_data_inline (elog_main_t * em, elog_event_type_t * type,
433 elog_track_t * track)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700434{
Dave Barachc3799992016-08-15 11:12:27 -0400435 return elog_event_data_inline (em, type, track, clib_cpu_time_now ());
Ed Warnickecb9cada2015-12-08 15:45:58 -0700436}
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 Barach903fd512017-04-01 11:07:40 -0400487/** @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 Barachc3799992016-08-15 11:12:27 -0400497u32 elog_string (elog_main_t * em, char *format, ...);
Dave Barach903fd512017-04-01 11:07:40 -0400498
Ed Warnickecb9cada2015-12-08 15:45:58 -0700499void elog_time_now (elog_time_stamp_t * et);
500
Dave Barach903fd512017-04-01 11:07:40 -0400501/** @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 Barachc3799992016-08-15 11:12:27 -0400506elog_event_t *elog_get_events (elog_main_t * em);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700507
Dave Barach903fd512017-04-01 11:07:40 -0400508/** @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 Barachc3799992016-08-15 11:12:27 -0400513elog_event_t *elog_peek_events (elog_main_t * em);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700514
515/* Merge two logs, add supplied track tags. */
Dave Barachc3799992016-08-15 11:12:27 -0400516void elog_merge (elog_main_t * dst, u8 * dst_tag,
Dave Barach903fd512017-04-01 11:07:40 -0400517 elog_main_t * src, u8 * src_tag, f64 align_tweak);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700518
519/* 2 arguments elog_main_t and elog_event_t to format event or track name. */
Dave Barachc3799992016-08-15 11:12:27 -0400520u8 *format_elog_event (u8 * s, va_list * va);
Florin Corasaefbede2018-12-19 13:07:49 -0800521u8 *format_elog_track_name (u8 * s, va_list * va);
522u8 *format_elog_track (u8 * s, va_list * args);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700523
524void serialize_elog_main (serialize_main_t * m, va_list * va);
525void unserialize_elog_main (serialize_main_t * m, va_list * va);
526
527void elog_init (elog_main_t * em, u32 n_events);
Dave Barache5389bb2016-03-28 17:12:19 -0400528void elog_alloc (elog_main_t * em, u32 n_events);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700529
530#ifdef CLIB_UNIX
531always_inline clib_error_t *
Dave Barach59b25652017-09-10 15:04:27 -0400532elog_write_file (elog_main_t * em, char *clib_file, int flush_ring)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700533{
534 serialize_main_t m;
Dave Barachc3799992016-08-15 11:12:27 -0400535 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700536
Dave Barach59b25652017-09-10 15:04:27 -0400537 error = serialize_open_clib_file (&m, clib_file);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700538 if (error)
539 return error;
Dave Barach903fd512017-04-01 11:07:40 -0400540 error = serialize (&m, serialize_elog_main, em, flush_ring);
Dave Barachc3799992016-08-15 11:12:27 -0400541 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700542 serialize_close (&m);
543 return error;
544}
545
546always_inline clib_error_t *
Dave Barach59b25652017-09-10 15:04:27 -0400547elog_read_file (elog_main_t * em, char *clib_file)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700548{
549 serialize_main_t m;
Dave Barachc3799992016-08-15 11:12:27 -0400550 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700551
Dave Barach59b25652017-09-10 15:04:27 -0400552 error = unserialize_open_clib_file (&m, clib_file);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700553 if (error)
554 return error;
555 error = unserialize (&m, unserialize_elog_main, em);
Dave Barachc3799992016-08-15 11:12:27 -0400556 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700557 unserialize_close (&m);
558 return error;
559}
560
561#endif /* CLIB_UNIX */
562
563#endif /* included_clib_elog_h */
Dave Barachc3799992016-08-15 11:12:27 -0400564
565/*
566 * fd.io coding-style-patch-verification: ON
567 *
568 * Local Variables:
569 * eval: (c-set-style "gnu")
570 * End:
571 */