Caffe2 - C++ API
A deep learning, cross platform ML framework
static_tracepoint_elfx86.h
1 #pragma once
2 
3 // Default constraint for the probe arguments as operands.
4 #ifndef CAFFE_SDT_ARG_CONSTRAINT
5 #define CAFFE_SDT_ARG_CONSTRAINT "nor"
6 #endif
7 
8 // Instruction to emit for the probe.
9 #define CAFFE_SDT_NOP nop
10 
11 // Note section properties.
12 #define CAFFE_SDT_NOTE_NAME "stapsdt"
13 #define CAFFE_SDT_NOTE_TYPE 3
14 
15 // Size of address depending on platform.
16 #ifdef __LP64__
17 #define CAFFE_SDT_ASM_ADDR .8byte
18 #else
19 #define CAFFE_SDT_ASM_ADDR .4byte
20 #endif
21 
22 // Assembler helper Macros.
23 #define CAFFE_SDT_S(x) #x
24 #define CAFFE_SDT_ASM_1(x) CAFFE_SDT_S(x) "\n"
25 #define CAFFE_SDT_ASM_2(a, b) CAFFE_SDT_S(a) "," CAFFE_SDT_S(b) "\n"
26 #define CAFFE_SDT_ASM_3(a, b, c) CAFFE_SDT_S(a) "," CAFFE_SDT_S(b) "," \
27  CAFFE_SDT_S(c) "\n"
28 #define CAFFE_SDT_ASM_STRING(x) CAFFE_SDT_ASM_1(.asciz CAFFE_SDT_S(x))
29 
30 // Helper to determine the size of an argument.
31 #define CAFFE_SDT_ISARRAY(x) (__builtin_classify_type(x) == 14)
32 #define CAFFE_SDT_ARGSIZE(x) (CAFFE_SDT_ISARRAY(x) ? sizeof(void*) : sizeof(x))
33 
34 // Format of each probe arguments as operand.
35 // Size of the arugment tagged with CAFFE_SDT_Sn, with "n" constraint.
36 // Value of the argument tagged with CAFFE_SDT_An, with configured constraint.
37 #define CAFFE_SDT_ARG(n, x) \
38  [CAFFE_SDT_S##n] "n" ((size_t)CAFFE_SDT_ARGSIZE(x)), \
39  [CAFFE_SDT_A##n] CAFFE_SDT_ARG_CONSTRAINT (x)
40 
41 // Templates to append arguments as operands.
42 #define CAFFE_SDT_OPERANDS_0() [__sdt_dummy] "g" (0)
43 #define CAFFE_SDT_OPERANDS_1(_1) CAFFE_SDT_ARG(1, _1)
44 #define CAFFE_SDT_OPERANDS_2(_1, _2) \
45  CAFFE_SDT_OPERANDS_1(_1), CAFFE_SDT_ARG(2, _2)
46 #define CAFFE_SDT_OPERANDS_3(_1, _2, _3) \
47  CAFFE_SDT_OPERANDS_2(_1, _2), CAFFE_SDT_ARG(3, _3)
48 #define CAFFE_SDT_OPERANDS_4(_1, _2, _3, _4) \
49  CAFFE_SDT_OPERANDS_3(_1, _2, _3), CAFFE_SDT_ARG(4, _4)
50 #define CAFFE_SDT_OPERANDS_5(_1, _2, _3, _4, _5) \
51  CAFFE_SDT_OPERANDS_4(_1, _2, _3, _4), CAFFE_SDT_ARG(5, _5)
52 #define CAFFE_SDT_OPERANDS_6(_1, _2, _3, _4, _5, _6) \
53  CAFFE_SDT_OPERANDS_5(_1, _2, _3, _4, _5), CAFFE_SDT_ARG(6, _6)
54 #define CAFFE_SDT_OPERANDS_7(_1, _2, _3, _4, _5, _6, _7) \
55  CAFFE_SDT_OPERANDS_6(_1, _2, _3, _4, _5, _6), CAFFE_SDT_ARG(7, _7)
56 #define CAFFE_SDT_OPERANDS_8(_1, _2, _3, _4, _5, _6, _7, _8) \
57  CAFFE_SDT_OPERANDS_7(_1, _2, _3, _4, _5, _6, _7), CAFFE_SDT_ARG(8, _8)
58 
59 // Templates to reference the arguments from operands in note section.
60 #define CAFFE_SDT_ARGFMT(no) %n[CAFFE_SDT_S##no]@%[CAFFE_SDT_A##no]
61 #define CAFFE_SDT_ARG_TEMPLATE_0 /*No arguments*/
62 #define CAFFE_SDT_ARG_TEMPLATE_1 CAFFE_SDT_ARGFMT(1)
63 #define CAFFE_SDT_ARG_TEMPLATE_2 CAFFE_SDT_ARG_TEMPLATE_1 CAFFE_SDT_ARGFMT(2)
64 #define CAFFE_SDT_ARG_TEMPLATE_3 CAFFE_SDT_ARG_TEMPLATE_2 CAFFE_SDT_ARGFMT(3)
65 #define CAFFE_SDT_ARG_TEMPLATE_4 CAFFE_SDT_ARG_TEMPLATE_3 CAFFE_SDT_ARGFMT(4)
66 #define CAFFE_SDT_ARG_TEMPLATE_5 CAFFE_SDT_ARG_TEMPLATE_4 CAFFE_SDT_ARGFMT(5)
67 #define CAFFE_SDT_ARG_TEMPLATE_6 CAFFE_SDT_ARG_TEMPLATE_5 CAFFE_SDT_ARGFMT(6)
68 #define CAFFE_SDT_ARG_TEMPLATE_7 CAFFE_SDT_ARG_TEMPLATE_6 CAFFE_SDT_ARGFMT(7)
69 #define CAFFE_SDT_ARG_TEMPLATE_8 CAFFE_SDT_ARG_TEMPLATE_7 CAFFE_SDT_ARGFMT(8)
70 
71 // Structure of note section for the probe.
72 #define CAFFE_SDT_NOTE_CONTENT(provider, name, arg_template) \
73  CAFFE_SDT_ASM_1(990: CAFFE_SDT_NOP) \
74  CAFFE_SDT_ASM_3( .pushsection .note.stapsdt,"","note") \
75  CAFFE_SDT_ASM_1( .balign 4) \
76  CAFFE_SDT_ASM_3( .4byte 992f-991f, 994f-993f, CAFFE_SDT_NOTE_TYPE) \
77  CAFFE_SDT_ASM_1(991: .asciz CAFFE_SDT_NOTE_NAME) \
78  CAFFE_SDT_ASM_1(992: .balign 4) \
79  CAFFE_SDT_ASM_1(993: CAFFE_SDT_ASM_ADDR 990b) \
80  CAFFE_SDT_ASM_1( CAFFE_SDT_ASM_ADDR 0) /*Reserved for Semaphore address*/\
81  CAFFE_SDT_ASM_1( CAFFE_SDT_ASM_ADDR 0) /*Reserved for Semaphore name*/ \
82  CAFFE_SDT_ASM_STRING(provider) \
83  CAFFE_SDT_ASM_STRING(name) \
84  CAFFE_SDT_ASM_STRING(arg_template) \
85  CAFFE_SDT_ASM_1(994: .balign 4) \
86  CAFFE_SDT_ASM_1( .popsection)
87 
88 // Main probe Macro.
89 #define CAFFE_SDT_PROBE(provider, name, n, arglist) \
90  __asm__ __volatile__ ( \
91  CAFFE_SDT_NOTE_CONTENT(provider, name, CAFFE_SDT_ARG_TEMPLATE_##n) \
92  :: CAFFE_SDT_OPERANDS_##n arglist \
93  ) \
94 
95 // Helper Macros to handle variadic arguments.
96 #define CAFFE_SDT_NARG_(_0, _1, _2, _3, _4, _5, _6, _7, _8, N, ...) N
97 #define CAFFE_SDT_NARG(...) \
98  CAFFE_SDT_NARG_(__VA_ARGS__, 8, 7, 6, 5, 4, 3, 2, 1, 0)
99 #define CAFFE_SDT_PROBE_N(provider, name, N, ...) \
100  CAFFE_SDT_PROBE(provider, name, N, (__VA_ARGS__))