DPDK  23.07.0
rte_trace_point.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2020 Marvell International Ltd.
3  */
4 
5 #ifndef _RTE_TRACE_POINT_H_
6 #define _RTE_TRACE_POINT_H_
7 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 #include <stdbool.h>
24 #include <stdio.h>
25 
26 #include <rte_branch_prediction.h>
27 #include <rte_common.h>
28 #include <rte_compat.h>
29 #include <rte_cycles.h>
30 #include <rte_per_lcore.h>
31 #include <rte_string_fns.h>
32 #include <rte_uuid.h>
33 
35 typedef uint64_t rte_trace_point_t;
36 
42 #define RTE_TRACE_POINT_ARGS
43 
45 #define __RTE_TRACE_POINT(_mode, _tp, _args, ...) \
46 extern rte_trace_point_t __##_tp; \
47 static __rte_always_inline void \
48 _tp _args \
49 { \
50  __rte_trace_point_emit_header_##_mode(&__##_tp); \
51  __VA_ARGS__ \
52 }
53 
78 #define RTE_TRACE_POINT(tp, args, ...) \
79  __RTE_TRACE_POINT(generic, tp, args, __VA_ARGS__)
80 
99 #define RTE_TRACE_POINT_FP(tp, args, ...) \
100  __RTE_TRACE_POINT(fp, tp, args, __VA_ARGS__)
101 
102 #ifdef __DOXYGEN__
103 
115 #define RTE_TRACE_POINT_REGISTER(trace, name)
116 
118 #define rte_trace_point_emit_u64(val)
119 
120 #define rte_trace_point_emit_i64(val)
121 
122 #define rte_trace_point_emit_u32(val)
123 
124 #define rte_trace_point_emit_i32(val)
125 
126 #define rte_trace_point_emit_u16(val)
127 
128 #define rte_trace_point_emit_i16(val)
129 
130 #define rte_trace_point_emit_u8(val)
131 
132 #define rte_trace_point_emit_i8(val)
133 
134 #define rte_trace_point_emit_int(val)
135 
136 #define rte_trace_point_emit_long(val)
137 
138 #define rte_trace_point_emit_size_t(val)
139 
140 #define rte_trace_point_emit_float(val)
141 
142 #define rte_trace_point_emit_double(val)
143 
144 #define rte_trace_point_emit_ptr(val)
145 
146 #define rte_trace_point_emit_string(val)
147 
156 #define rte_trace_point_emit_blob(val, len)
157 
158 #endif /* __DOXYGEN__ */
159 
161 #define __RTE_TRACE_EMIT_STRING_LEN_MAX 32
162 
163 #define __RTE_TRACE_EVENT_HEADER_SZ sizeof(uint64_t)
164 
166 #define RTE_TRACE_BLOB_LEN_MAX 64
167 
177 __rte_experimental
178 int rte_trace_point_enable(rte_trace_point_t *tp);
179 
189 __rte_experimental
190 int rte_trace_point_disable(rte_trace_point_t *tp);
191 
200 __rte_experimental
201 bool rte_trace_point_is_enabled(rte_trace_point_t *tp);
202 
211 __rte_experimental
212 rte_trace_point_t *rte_trace_point_lookup(const char *name);
213 
222 __rte_experimental
223 static __rte_always_inline bool
224 __rte_trace_point_fp_is_enabled(void)
225 {
226 #ifdef RTE_ENABLE_TRACE_FP
227  return true;
228 #else
229  return false;
230 #endif
231 }
232 
238 __rte_experimental
239 void __rte_trace_mem_per_thread_alloc(void);
240 
256 __rte_experimental
257 void __rte_trace_point_emit_field(size_t sz, const char *field,
258  const char *type);
259 
276 __rte_experimental
277 int __rte_trace_point_register(rte_trace_point_t *trace, const char *name,
278  void (*register_fn)(void));
279 
280 #ifndef __DOXYGEN__
281 
282 #ifndef _RTE_TRACE_POINT_REGISTER_H_
283 #ifdef ALLOW_EXPERIMENTAL_API
284 
285 #define __RTE_TRACE_EVENT_HEADER_ID_SHIFT (48)
286 
287 #define __RTE_TRACE_FIELD_SIZE_SHIFT 0
288 #define __RTE_TRACE_FIELD_SIZE_MASK (0xffffULL << __RTE_TRACE_FIELD_SIZE_SHIFT)
289 #define __RTE_TRACE_FIELD_ID_SHIFT (16)
290 #define __RTE_TRACE_FIELD_ID_MASK (0xffffULL << __RTE_TRACE_FIELD_ID_SHIFT)
291 #define __RTE_TRACE_FIELD_ENABLE_MASK (1ULL << 63)
292 #define __RTE_TRACE_FIELD_ENABLE_DISCARD (1ULL << 62)
293 
294 struct __rte_trace_stream_header {
295  uint32_t magic;
296  rte_uuid_t uuid;
297  uint32_t lcore_id;
298  char thread_name[__RTE_TRACE_EMIT_STRING_LEN_MAX];
299 } __rte_packed;
300 
301 struct __rte_trace_header {
302  uint32_t offset;
303  uint32_t len;
304  struct __rte_trace_stream_header stream_header;
305  uint8_t mem[];
306 };
307 
308 RTE_DECLARE_PER_LCORE(void *, trace_mem);
309 
310 static __rte_always_inline void *
311 __rte_trace_mem_get(uint64_t in)
312 {
313  struct __rte_trace_header *trace =
314  (struct __rte_trace_header *)(RTE_PER_LCORE(trace_mem));
315  const uint16_t sz = in & __RTE_TRACE_FIELD_SIZE_MASK;
316 
317  /* Trace memory is not initialized for this thread */
318  if (unlikely(trace == NULL)) {
319  __rte_trace_mem_per_thread_alloc();
320  trace = (struct __rte_trace_header *)(RTE_PER_LCORE(trace_mem));
321  if (unlikely(trace == NULL))
322  return NULL;
323  }
324  /* Check the wrap around case */
325  uint32_t offset = trace->offset;
326  if (unlikely((offset + sz) >= trace->len)) {
327  /* Disable the trace event if it in DISCARD mode */
328  if (unlikely(in & __RTE_TRACE_FIELD_ENABLE_DISCARD))
329  return NULL;
330 
331  offset = 0;
332  }
333  /* Align to event header size */
334  offset = RTE_ALIGN_CEIL(offset, __RTE_TRACE_EVENT_HEADER_SZ);
335  void *mem = RTE_PTR_ADD(&trace->mem[0], offset);
336  offset += sz;
337  trace->offset = offset;
338 
339  return mem;
340 }
341 
342 static __rte_always_inline void *
343 __rte_trace_point_emit_ev_header(void *mem, uint64_t in)
344 {
345  uint64_t val;
346 
347  /* Event header [63:0] = id [63:48] | timestamp [47:0] */
348  val = rte_get_tsc_cycles() &
349  ~(0xffffULL << __RTE_TRACE_EVENT_HEADER_ID_SHIFT);
350  val |= ((in & __RTE_TRACE_FIELD_ID_MASK) <<
351  (__RTE_TRACE_EVENT_HEADER_ID_SHIFT -
352  __RTE_TRACE_FIELD_ID_SHIFT));
353 
354  *(uint64_t *)mem = val;
355  return RTE_PTR_ADD(mem, __RTE_TRACE_EVENT_HEADER_SZ);
356 }
357 
358 #define __rte_trace_point_emit_header_generic(t) \
359 void *mem; \
360 do { \
361  const uint64_t val = __atomic_load_n(t, __ATOMIC_ACQUIRE); \
362  if (likely(!(val & __RTE_TRACE_FIELD_ENABLE_MASK))) \
363  return; \
364  mem = __rte_trace_mem_get(val); \
365  if (unlikely(mem == NULL)) \
366  return; \
367  mem = __rte_trace_point_emit_ev_header(mem, val); \
368 } while (0)
369 
370 #define __rte_trace_point_emit_header_fp(t) \
371  if (!__rte_trace_point_fp_is_enabled()) \
372  return; \
373  __rte_trace_point_emit_header_generic(t)
374 
375 #define __rte_trace_point_emit(in, type) \
376 do { \
377  memcpy(mem, &(in), sizeof(in)); \
378  mem = RTE_PTR_ADD(mem, sizeof(in)); \
379 } while (0)
380 
381 #define rte_trace_point_emit_string(in) \
382 do { \
383  if (unlikely(in == NULL)) \
384  return; \
385  rte_strscpy((char *)mem, in, __RTE_TRACE_EMIT_STRING_LEN_MAX); \
386  mem = RTE_PTR_ADD(mem, __RTE_TRACE_EMIT_STRING_LEN_MAX); \
387 } while (0)
388 
389 #define rte_trace_point_emit_blob(in, len) \
390 do { \
391  if (unlikely(in == NULL)) \
392  return; \
393  if (len > RTE_TRACE_BLOB_LEN_MAX) \
394  len = RTE_TRACE_BLOB_LEN_MAX; \
395  __rte_trace_point_emit(len, uint8_t); \
396  memcpy(mem, in, len); \
397  memset(RTE_PTR_ADD(mem, len), 0, RTE_TRACE_BLOB_LEN_MAX - len); \
398  mem = RTE_PTR_ADD(mem, RTE_TRACE_BLOB_LEN_MAX); \
399 } while (0)
400 
401 #else
402 
403 #define __rte_trace_point_emit_header_generic(t) RTE_SET_USED(t)
404 #define __rte_trace_point_emit_header_fp(t) RTE_SET_USED(t)
405 #define __rte_trace_point_emit(in, type) RTE_SET_USED(in)
406 #define rte_trace_point_emit_string(in) RTE_SET_USED(in)
407 #define rte_trace_point_emit_blob(in, len) \
408 do { \
409  RTE_SET_USED(in); \
410  RTE_SET_USED(len); \
411 } while (0)
412 
413 
414 #endif /* ALLOW_EXPERIMENTAL_API */
415 #endif /* _RTE_TRACE_POINT_REGISTER_H_ */
416 
417 #define rte_trace_point_emit_u64(in) __rte_trace_point_emit(in, uint64_t)
418 #define rte_trace_point_emit_i64(in) __rte_trace_point_emit(in, int64_t)
419 #define rte_trace_point_emit_u32(in) __rte_trace_point_emit(in, uint32_t)
420 #define rte_trace_point_emit_i32(in) __rte_trace_point_emit(in, int32_t)
421 #define rte_trace_point_emit_u16(in) __rte_trace_point_emit(in, uint16_t)
422 #define rte_trace_point_emit_i16(in) __rte_trace_point_emit(in, int16_t)
423 #define rte_trace_point_emit_u8(in) __rte_trace_point_emit(in, uint8_t)
424 #define rte_trace_point_emit_i8(in) __rte_trace_point_emit(in, int8_t)
425 #define rte_trace_point_emit_int(in) __rte_trace_point_emit(in, int32_t)
426 #define rte_trace_point_emit_long(in) __rte_trace_point_emit(in, long)
427 #define rte_trace_point_emit_size_t(in) __rte_trace_point_emit(in, size_t)
428 #define rte_trace_point_emit_float(in) __rte_trace_point_emit(in, float)
429 #define rte_trace_point_emit_double(in) __rte_trace_point_emit(in, double)
430 #define rte_trace_point_emit_ptr(in) __rte_trace_point_emit(in, uintptr_t)
431 
432 #endif /* __DOXYGEN__ */
433 
434 #ifdef __cplusplus
435 }
436 #endif
437 
438 #endif /* _RTE_TRACE_POINT_H_ */
#define RTE_DECLARE_PER_LCORE(type, name)
Definition: rte_per_lcore.h:38
#define __rte_always_inline
Definition: rte_common.h:255
#define RTE_ALIGN_CEIL(val, align)
Definition: rte_common.h:346
__rte_experimental int rte_trace_point_disable(rte_trace_point_t *tp)
#define __rte_packed
Definition: rte_common.h:83
__rte_experimental rte_trace_point_t * rte_trace_point_lookup(const char *name)
#define RTE_PTR_ADD(ptr, x)
Definition: rte_common.h:290
#define unlikely(x)
unsigned char rte_uuid_t[16]
Definition: rte_uuid.h:24
static uint64_t rte_get_tsc_cycles(void)
__rte_experimental int rte_trace_point_enable(rte_trace_point_t *tp)
#define RTE_PER_LCORE(name)
Definition: rte_per_lcore.h:44
__rte_experimental bool rte_trace_point_is_enabled(rte_trace_point_t *tp)
uint64_t rte_trace_point_t