blob: 359868dd0f038fcb84e23a226fe9d78ae74ad058 [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 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070056#include <vppinfra/mhash.h>
57
Dave Barachc3799992016-08-15 11:12:27 -040058typedef struct
59{
60 union
61 {
Dave Barach903fd512017-04-01 11:07:40 -040062 /** Absolute time stamp in CPU clock cycles. */
Ed Warnickecb9cada2015-12-08 15:45:58 -070063 u64 time_cycles;
64
Dave Barach903fd512017-04-01 11:07:40 -040065 /** Absolute time as floating point number in seconds. */
Ed Warnickecb9cada2015-12-08 15:45:58 -070066 f64 time;
67 };
68
Dave Barach903fd512017-04-01 11:07:40 -040069 /** Event type index. */
Ed Warnickecb9cada2015-12-08 15:45:58 -070070 u16 type;
71
Dave Barach903fd512017-04-01 11:07:40 -040072 /** Track for this event. Tracks allow events to be sorted and
Ed Warnickecb9cada2015-12-08 15:45:58 -070073 displayed by track. Think of 2 dimensional display with time and
Dave Barachc3799992016-08-15 11:12:27 -040074 track being the x and y axes. */
Ed Warnickecb9cada2015-12-08 15:45:58 -070075 u16 track;
76
Dave Barach903fd512017-04-01 11:07:40 -040077 /** 20-bytes of data follows, pads to 32 bytes. */
Ed Warnickecb9cada2015-12-08 15:45:58 -070078 u8 data[20];
79} elog_event_t;
80
Dave Barachc3799992016-08-15 11:12:27 -040081typedef struct
82{
Dave Barach903fd512017-04-01 11:07:40 -040083 /** Type index plus one assigned to this type.
Ed Warnickecb9cada2015-12-08 15:45:58 -070084 This is used to mark type as seen. */
85 u32 type_index_plus_one;
86
Dave Barach903fd512017-04-01 11:07:40 -040087 /** String table as a vector constructed when type is registered. */
Dave Barachc3799992016-08-15 11:12:27 -040088 char **enum_strings_vector;
Ed Warnickecb9cada2015-12-08 15:45:58 -070089
Dave Barach903fd512017-04-01 11:07:40 -040090 /** Format string. (example: "my-event (%d,%d)"). */
Dave Barachc3799992016-08-15 11:12:27 -040091 char *format;
Ed Warnickecb9cada2015-12-08 15:45:58 -070092
Dave Barach903fd512017-04-01 11:07:40 -040093 /** Specifies how arguments to format are parsed from event data.
Ed Warnickecb9cada2015-12-08 15:45:58 -070094 String of characters '0' '1' or '2' '3' to specify log2 size of data
95 (e.g. for u8, u16, u32 or u64),
96 's' means a null-terminated C string
97 't' means argument is an index into enum string table for this type.
98 'e' is a float,
99 'f' is a double. */
Dave Barachc3799992016-08-15 11:12:27 -0400100 char *format_args;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700101
Dave Barach903fd512017-04-01 11:07:40 -0400102 /** Function name generating event. */
Dave Barachc3799992016-08-15 11:12:27 -0400103 char *function;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700104
Dave Barach903fd512017-04-01 11:07:40 -0400105 /** Number of elements in string enum table. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106 u32 n_enum_strings;
107
Dave Barach903fd512017-04-01 11:07:40 -0400108 /** String table for enum/number to string formatting. */
Dave Barachc3799992016-08-15 11:12:27 -0400109 char *enum_strings[];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110} elog_event_type_t;
111
Dave Barachc3799992016-08-15 11:12:27 -0400112typedef struct
113{
Dave Barach903fd512017-04-01 11:07:40 -0400114 /** Track name vector. */
Dave Barachc3799992016-08-15 11:12:27 -0400115 char *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700116
Dave Barach903fd512017-04-01 11:07:40 -0400117 /** Set to one when track has been added to
Ed Warnickecb9cada2015-12-08 15:45:58 -0700118 main structure. */
119 u32 track_index_plus_one;
120} elog_track_t;
121
Dave Barachc3799992016-08-15 11:12:27 -0400122typedef struct
123{
Dave Barach903fd512017-04-01 11:07:40 -0400124 /** CPU cycle counter. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125 u64 cpu;
126
Dave Barach903fd512017-04-01 11:07:40 -0400127 /** OS timer in nano secs since epoch 3/30/2017, see elog_time_now() */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700128 u64 os_nsec;
129} elog_time_stamp_t;
130
Dave Barachc3799992016-08-15 11:12:27 -0400131typedef struct
132{
Dave Barach903fd512017-04-01 11:07:40 -0400133 /** Total number of events in buffer. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134 u32 n_total_events;
135
Dave Barach903fd512017-04-01 11:07:40 -0400136 /** When count reaches limit logging is disabled. This is
Ed Warnickecb9cada2015-12-08 15:45:58 -0700137 used for event triggers. */
138 u32 n_total_events_disable_limit;
139
Dave Barach903fd512017-04-01 11:07:40 -0400140 /** Dummy event to use when logger is disabled. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700141 elog_event_t dummy_event;
142
Dave Barach903fd512017-04-01 11:07:40 -0400143 /** Power of 2 number of elements in ring. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700144 uword event_ring_size;
145
Dave Barach903fd512017-04-01 11:07:40 -0400146 /** Vector of events (circular buffer). Power of 2 size.
147 Used when events are being collected. */
Dave Barachc3799992016-08-15 11:12:27 -0400148 elog_event_t *event_ring;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149
Dave Barach903fd512017-04-01 11:07:40 -0400150 /** Vector of event types. */
Dave Barachc3799992016-08-15 11:12:27 -0400151 elog_event_type_t *event_types;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700152
Dave Barach903fd512017-04-01 11:07:40 -0400153 /** Hash table mapping type format to type index. */
Dave Barachc3799992016-08-15 11:12:27 -0400154 uword *event_type_by_format;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700155
Dave Barach903fd512017-04-01 11:07:40 -0400156 /** Events may refer to strings in string table. */
Dave Barachc3799992016-08-15 11:12:27 -0400157 char *string_table;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158
Dave Barach903fd512017-04-01 11:07:40 -0400159 /** Vector of tracks. */
Dave Barachc3799992016-08-15 11:12:27 -0400160 elog_track_t *tracks;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161
Dave Barach903fd512017-04-01 11:07:40 -0400162 /** Default track. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700163 elog_track_t default_track;
164
Dave Barach903fd512017-04-01 11:07:40 -0400165 /** Place holder for CPU clock frequency. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166 clib_time_t cpu_timer;
167
Dave Barach903fd512017-04-01 11:07:40 -0400168 /** Timestamps */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700169 elog_time_stamp_t init_time, serialize_time;
170
Dave Barach903fd512017-04-01 11:07:40 -0400171 /** SMP lock, non-zero means locking required */
Dave Barachc3799992016-08-15 11:12:27 -0400172 uword *lock;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700173
Dave Barach903fd512017-04-01 11:07:40 -0400174 /** Use serialize_time and init_time to give estimate for
175 cpu clock frequency. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700176 f64 nsec_per_cpu_clock;
177
Dave Barach903fd512017-04-01 11:07:40 -0400178 /** Vector of events converted to generic form after collection. */
Dave Barachc3799992016-08-15 11:12:27 -0400179 elog_event_t *events;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700180} elog_main_t;
181
Dave Barach903fd512017-04-01 11:07:40 -0400182/** @brief Return number of events in the event-log buffer
183 @param em elog_main_t *
184 @return number of events in the buffer
185*/
186
Ed Warnickecb9cada2015-12-08 15:45:58 -0700187always_inline uword
188elog_n_events_in_buffer (elog_main_t * em)
Dave Barachc3799992016-08-15 11:12:27 -0400189{
190 return clib_min (em->n_total_events, em->event_ring_size);
191}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700192
Dave Barach903fd512017-04-01 11:07:40 -0400193/** @brief Return number of events which can fit in the event buffer
194 @param em elog_main_t *
195 @return number of events which can fit in the buffer
196*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700197always_inline uword
198elog_buffer_capacity (elog_main_t * em)
Dave Barachc3799992016-08-15 11:12:27 -0400199{
200 return em->event_ring_size;
201}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700202
Dave Barach903fd512017-04-01 11:07:40 -0400203/** @brief Reset the event buffer
204 @param em elog_main_t *
205*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700206always_inline void
207elog_reset_buffer (elog_main_t * em)
208{
209 em->n_total_events = 0;
210 em->n_total_events_disable_limit = ~0;
211}
212
Dave Barach903fd512017-04-01 11:07:40 -0400213/** @brief Enable or disable event logging
214 @param em elog_main_t *
215*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700216always_inline void
217elog_enable_disable (elog_main_t * em, int is_enabled)
218{
219 em->n_total_events = 0;
Damjan Marion2c29d752015-12-18 10:26:56 +0100220 em->n_total_events_disable_limit = is_enabled ? ~0 : 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700221}
222
Dave Barach903fd512017-04-01 11:07:40 -0400223/** @brief disable logging after specified number of ievents have been logged.
224
Ed Warnickecb9cada2015-12-08 15:45:58 -0700225 This is used as a "debug trigger" when a certain event has occurred.
226 Events will be logged both before and after the "event" but the
Dave Barach903fd512017-04-01 11:07:40 -0400227 event will not be lost as long as N < RING_SIZE.
228
229 @param em elog_main_t *
230 @param n uword number of events before disabling event logging
231*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700232always_inline void
233elog_disable_after_events (elog_main_t * em, uword n)
Dave Barachc3799992016-08-15 11:12:27 -0400234{
235 em->n_total_events_disable_limit = em->n_total_events + n;
236}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237
Dave Barach903fd512017-04-01 11:07:40 -0400238/* @brief mid-buffer logic-analyzer trigger
239
240 Currently, only midpoint triggering is supported, but it's pretty obvious
241 how to generalize the scheme.
242 @param em elog_main_t *
243*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700244always_inline void
245elog_disable_trigger (elog_main_t * em)
Dave Barachc3799992016-08-15 11:12:27 -0400246{
247 em->n_total_events_disable_limit =
248 em->n_total_events + vec_len (em->event_ring) / 2;
249}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700250
Dave Barach903fd512017-04-01 11:07:40 -0400251/** @brief register an event type
252 @param em elog_main_t *
253 @param t elog_event_type_t * event to register
254 @return type index
255 @warning Typically not called directly
256*/
257
Ed Warnickecb9cada2015-12-08 15:45:58 -0700258word elog_event_type_register (elog_main_t * em, elog_event_type_t * t);
Dave Barach903fd512017-04-01 11:07:40 -0400259
260/** @brief register an event track
261 @param em elog_main_t *
262 @param t elog_track_t * track to register
263 @return track index
264 @note this function is often called directly
265*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700266word elog_track_register (elog_main_t * em, elog_track_t * t);
267
Dave Barach903fd512017-04-01 11:07:40 -0400268/** @brief event logging enabled predicate
269 @param em elog_main_t *
270 @return 1 if enabled, 0 if not enabled
271*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700272always_inline uword
273elog_is_enabled (elog_main_t * em)
Dave Barachc3799992016-08-15 11:12:27 -0400274{
275 return em->n_total_events < em->n_total_events_disable_limit;
276}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700277
Dave Barach903fd512017-04-01 11:07:40 -0400278/** @brief Allocate an event to be filled in by the caller
279
280 Not normally called directly; this function underlies the
281 ELOG_DATA and ELOG_TRACK_DATA macros
282
283 @param em elog_main_t *
284 @param type elog_event_type_t * type
285 @param track elog_track_t * track
286 @param cpu_time u64 current cpu tick value
287 @returns event to be filled in
288*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700289always_inline void *
290elog_event_data_inline (elog_main_t * em,
291 elog_event_type_t * type,
Dave Barachc3799992016-08-15 11:12:27 -0400292 elog_track_t * track, u64 cpu_time)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700293{
Dave Barachc3799992016-08-15 11:12:27 -0400294 elog_event_t *e;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700295 uword ei;
296 word type_index, track_index;
297
298 /* Return the user dummy memory to scribble data into. */
Dave Barachc3799992016-08-15 11:12:27 -0400299 if (PREDICT_FALSE (!elog_is_enabled (em)))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700300 return em->dummy_event.data;
301
302 type_index = (word) type->type_index_plus_one - 1;
303 track_index = (word) track->track_index_plus_one - 1;
304 if (PREDICT_FALSE ((type_index | track_index) < 0))
305 {
306 if (type_index < 0)
307 type_index = elog_event_type_register (em, type);
308 if (track_index < 0)
309 track_index = elog_track_register (em, track);
310 }
311
312 ASSERT (type_index < vec_len (em->event_types));
313 ASSERT (track_index < vec_len (em->tracks));
314 ASSERT (is_pow2 (vec_len (em->event_ring)));
315
316 if (em->lock)
317 ei = clib_smp_atomic_add (&em->n_total_events, 1);
318 else
319 ei = em->n_total_events++;
320
321 ei &= em->event_ring_size - 1;
322 e = vec_elt_at_index (em->event_ring, ei);
323
324 e->time_cycles = cpu_time;
325 e->type = type_index;
326 e->track = track_index;
327
328 /* Return user data for caller to fill in. */
329 return e->data;
330}
331
332/* External version of inline. */
Dave Barachc3799992016-08-15 11:12:27 -0400333void *elog_event_data (elog_main_t * em,
334 elog_event_type_t * type,
335 elog_track_t * track, u64 cpu_time);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700336
Dave Barach903fd512017-04-01 11:07:40 -0400337/** @brief Allocate an event to be filled in by the caller, non-inline
338
339 Not normally called directly; this function underlies the
340 ELOG_DATA and ELOG_TRACK_DATA macros
341
342 @param em elog_main_t *
343 @param type elog_event_type_t * type
344 @param track elog_track_t * track
345 @param cpu_time u64 current cpu tick value
346 @returns event to be filled in
347*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700348always_inline void *
349elog_event_data_not_inline (elog_main_t * em,
350 elog_event_type_t * type,
Dave Barachc3799992016-08-15 11:12:27 -0400351 elog_track_t * track, u64 cpu_time)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700352{
353 /* Return the user dummy memory to scribble data into. */
Dave Barachc3799992016-08-15 11:12:27 -0400354 if (PREDICT_FALSE (!elog_is_enabled (em)))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700355 return em->dummy_event.data;
356 return elog_event_data (em, type, track, cpu_time);
357}
358
Dave Barach903fd512017-04-01 11:07:40 -0400359/** @brief Log a single-datum event
360 @param em elog_main_t *
361 @param type elog_event_type_t * type
362 @param data u32 single datum to capture
363*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700364always_inline void
365elog (elog_main_t * em, elog_event_type_t * type, u32 data)
366{
Dave Barachc3799992016-08-15 11:12:27 -0400367 u32 *d = elog_event_data_not_inline (em,
368 type,
369 &em->default_track,
370 clib_cpu_time_now ());
Ed Warnickecb9cada2015-12-08 15:45:58 -0700371 d[0] = data;
372}
373
Dave Barach903fd512017-04-01 11:07:40 -0400374/** @brief Log a single-datum event, inline version
375 @param em elog_main_t *
376 @param type elog_event_type_t * type
377 @param data u32 single datum to capture
378*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700379always_inline void
380elog_inline (elog_main_t * em, elog_event_type_t * type, u32 data)
381{
Dave Barachc3799992016-08-15 11:12:27 -0400382 u32 *d = elog_event_data_inline (em,
383 type,
384 &em->default_track,
385 clib_cpu_time_now ());
Ed Warnickecb9cada2015-12-08 15:45:58 -0700386 d[0] = data;
387}
388
Dave Barach903fd512017-04-01 11:07:40 -0400389/** @brief Log a single-datum event to a specific track, non-inline version
390 @param em elog_main_t *
391 @param type elog_event_type_t * type
392 @param type elog_event_track_t * track
393 @param data u32 single datum to capture
394*/
Dave Barachfb6e59d2016-03-26 18:45:42 -0400395always_inline void
Dave Barachc3799992016-08-15 11:12:27 -0400396elog_track (elog_main_t * em, elog_event_type_t * type, elog_track_t * track,
397 u32 data)
Dave Barachfb6e59d2016-03-26 18:45:42 -0400398{
Dave Barachc3799992016-08-15 11:12:27 -0400399 u32 *d = elog_event_data_not_inline (em,
400 type,
401 track,
402 clib_cpu_time_now ());
Dave Barachfb6e59d2016-03-26 18:45:42 -0400403 d[0] = data;
404}
405
Dave Barach903fd512017-04-01 11:07:40 -0400406/** @brief Log a single-datum event to a specific track
407 @param em elog_main_t *
408 @param type elog_event_type_t * type
409 @param type elog_event_track_t * track
410 @param data u32 single datum to capture
411*/
Dave Barachfb6e59d2016-03-26 18:45:42 -0400412always_inline void
Dave Barachc3799992016-08-15 11:12:27 -0400413elog_track_inline (elog_main_t * em, elog_event_type_t * type,
414 elog_track_t * track, u32 data)
Dave Barachfb6e59d2016-03-26 18:45:42 -0400415{
Dave Barachc3799992016-08-15 11:12:27 -0400416 u32 *d = elog_event_data_inline (em,
417 type,
418 track,
419 clib_cpu_time_now ());
Dave Barachfb6e59d2016-03-26 18:45:42 -0400420 d[0] = data;
421}
422
Ed Warnickecb9cada2015-12-08 15:45:58 -0700423always_inline void *
424elog_data (elog_main_t * em, elog_event_type_t * type, elog_track_t * track)
425{
Dave Barachc3799992016-08-15 11:12:27 -0400426 return elog_event_data_not_inline (em, type, track, clib_cpu_time_now ());
Ed Warnickecb9cada2015-12-08 15:45:58 -0700427}
428
429always_inline void *
Dave Barachc3799992016-08-15 11:12:27 -0400430elog_data_inline (elog_main_t * em, elog_event_type_t * type,
431 elog_track_t * track)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700432{
Dave Barachc3799992016-08-15 11:12:27 -0400433 return elog_event_data_inline (em, type, track, clib_cpu_time_now ());
Ed Warnickecb9cada2015-12-08 15:45:58 -0700434}
435
436/* Macro shorthands for generating/declaring events. */
437#define __ELOG_TYPE_VAR(f) f
438#define __ELOG_TRACK_VAR(f) f
439
440#define ELOG_TYPE_DECLARE(f) static elog_event_type_t __ELOG_TYPE_VAR(f)
441
442#define ELOG_TYPE_INIT_FORMAT_AND_FUNCTION(fmt,func) \
443 { .format = fmt, .function = func, }
444
445#define ELOG_TYPE_INIT(fmt) \
446 ELOG_TYPE_INIT_FORMAT_AND_FUNCTION(fmt,(char *) __FUNCTION__)
447
448#define ELOG_TYPE_DECLARE_HELPER(f,fmt,func) \
449 static elog_event_type_t __ELOG_TYPE_VAR(f) = \
450 ELOG_TYPE_INIT_FORMAT_AND_FUNCTION (fmt, func)
451
452#define ELOG_TYPE_DECLARE_FORMAT_AND_FUNCTION(f,fmt) \
453 ELOG_TYPE_DECLARE_HELPER (f, fmt, (char *) __FUNCTION__)
454
455#define ELOG_TYPE_DECLARE_FORMAT(f,fmt) \
456 ELOG_TYPE_DECLARE_HELPER (f, fmt, 0)
457
458/* Shorthands with and without __FUNCTION__.
459 D for decimal; X for hex. F for __FUNCTION__. */
460#define ELOG_TYPE(f,fmt) ELOG_TYPE_DECLARE_FORMAT_AND_FUNCTION(f,fmt)
461#define ELOG_TYPE_D(f) ELOG_TYPE_DECLARE_FORMAT (f, #f " %d")
462#define ELOG_TYPE_X(f) ELOG_TYPE_DECLARE_FORMAT (f, #f " 0x%x")
463#define ELOG_TYPE_DF(f) ELOG_TYPE_DECLARE_FORMAT_AND_FUNCTION (f, #f " %d")
464#define ELOG_TYPE_XF(f) ELOG_TYPE_DECLARE_FORMAT_AND_FUNCTION (f, #f " 0x%x")
465#define ELOG_TYPE_FD(f) ELOG_TYPE_DECLARE_FORMAT_AND_FUNCTION (f, #f " %d")
466#define ELOG_TYPE_FX(f) ELOG_TYPE_DECLARE_FORMAT_AND_FUNCTION (f, #f " 0x%x")
467
468#define ELOG_TRACK_DECLARE(f) static elog_track_t __ELOG_TRACK_VAR(f)
469#define ELOG_TRACK(f) ELOG_TRACK_DECLARE(f) = { .name = #f, }
470
471/* Log 32 bits of data. */
472#define ELOG(em,f,data) elog ((em), &__ELOG_TYPE_VAR(f), data)
473#define ELOG_INLINE(em,f,data) elog_inline ((em), &__ELOG_TYPE_VAR(f), data)
474
475/* Return data pointer to fill in. */
476#define ELOG_TRACK_DATA(em,f,track) \
477 elog_data ((em), &__ELOG_TYPE_VAR(f), &__ELOG_TRACK_VAR(track))
478#define ELOG_TRACK_DATA_INLINE(em,f,track) \
479 elog_data_inline ((em), &__ELOG_TYPE_VAR(f), &__ELOG_TRACK_VAR(track))
480
481/* Shorthand with default track. */
482#define ELOG_DATA(em,f) elog_data ((em), &__ELOG_TYPE_VAR (f), &(em)->default_track)
483#define ELOG_DATA_INLINE(em,f) elog_data_inline ((em), &__ELOG_TYPE_VAR (f), &(em)->default_track)
484
Dave Barach903fd512017-04-01 11:07:40 -0400485/** @brief add a string to the event-log string table
486
487 Often combined with hashing and the T4 elog format specifier to
488 display complex strings in offline tooling
489
490 @param em elog_main_t *
491 @param format char *
492 @param VARARGS
493 @return u32 index to add to event log
494*/
Dave Barachc3799992016-08-15 11:12:27 -0400495u32 elog_string (elog_main_t * em, char *format, ...);
Dave Barach903fd512017-04-01 11:07:40 -0400496
Ed Warnickecb9cada2015-12-08 15:45:58 -0700497void elog_time_now (elog_time_stamp_t * et);
498
Dave Barach903fd512017-04-01 11:07:40 -0400499/** @brief convert event ring events to events, and return them as a vector.
500 @param em elog_main_t *
501 @return event vector with timestamps in f64 seconds
502 @note sets em->events to resulting vector.
503*/
Dave Barachc3799992016-08-15 11:12:27 -0400504elog_event_t *elog_get_events (elog_main_t * em);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700505
Dave Barach903fd512017-04-01 11:07:40 -0400506/** @brief convert event ring events to events, and return them as a vector.
507 @param em elog_main_t *
508 @return event vector with timestamps in f64 seconds
509 @note no side effects
510*/
Dave Barachc3799992016-08-15 11:12:27 -0400511elog_event_t *elog_peek_events (elog_main_t * em);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700512
513/* Merge two logs, add supplied track tags. */
Dave Barachc3799992016-08-15 11:12:27 -0400514void elog_merge (elog_main_t * dst, u8 * dst_tag,
Dave Barach903fd512017-04-01 11:07:40 -0400515 elog_main_t * src, u8 * src_tag, f64 align_tweak);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700516
517/* 2 arguments elog_main_t and elog_event_t to format event or track name. */
Dave Barachc3799992016-08-15 11:12:27 -0400518u8 *format_elog_event (u8 * s, va_list * va);
519u8 *format_elog_track (u8 * s, va_list * va);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700520
521void serialize_elog_main (serialize_main_t * m, va_list * va);
522void unserialize_elog_main (serialize_main_t * m, va_list * va);
523
524void elog_init (elog_main_t * em, u32 n_events);
Dave Barache5389bb2016-03-28 17:12:19 -0400525void elog_alloc (elog_main_t * em, u32 n_events);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700526
527#ifdef CLIB_UNIX
528always_inline clib_error_t *
Dave Barach903fd512017-04-01 11:07:40 -0400529elog_write_file (elog_main_t * em, char *unix_file, int flush_ring)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700530{
531 serialize_main_t m;
Dave Barachc3799992016-08-15 11:12:27 -0400532 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700533
534 error = serialize_open_unix_file (&m, unix_file);
535 if (error)
536 return error;
Dave Barach903fd512017-04-01 11:07:40 -0400537 error = serialize (&m, serialize_elog_main, em, flush_ring);
Dave Barachc3799992016-08-15 11:12:27 -0400538 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700539 serialize_close (&m);
540 return error;
541}
542
543always_inline clib_error_t *
Dave Barachc3799992016-08-15 11:12:27 -0400544elog_read_file (elog_main_t * em, char *unix_file)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700545{
546 serialize_main_t m;
Dave Barachc3799992016-08-15 11:12:27 -0400547 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700548
549 error = unserialize_open_unix_file (&m, unix_file);
550 if (error)
551 return error;
552 error = unserialize (&m, unserialize_elog_main, em);
Dave Barachc3799992016-08-15 11:12:27 -0400553 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700554 unserialize_close (&m);
555 return error;
556}
557
558#endif /* CLIB_UNIX */
559
560#endif /* included_clib_elog_h */
Dave Barachc3799992016-08-15 11:12:27 -0400561
562/*
563 * fd.io coding-style-patch-verification: ON
564 *
565 * Local Variables:
566 * eval: (c-set-style "gnu")
567 * End:
568 */