Caffe2 - C++ API
A deep learning, cross platform ML framework
static_tracepoint_elfx86.h
1 
17 /*
18  * Copyright 2017 Facebook, Inc.
19  *
20  * Licensed under the Apache License, Version 2.0 (the "License");
21  * you may not use this file except in compliance with the License.
22  * You may obtain a copy of the License at
23  *
24  * http://www.apache.org/licenses/LICENSE-2.0
25  *
26  * Unless required by applicable law or agreed to in writing, software
27  * distributed under the License is distributed on an "AS IS" BASIS,
28  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29  * See the License for the specific language governing permissions and
30  * limitations under the License.
31  */
32 
33 #pragma once
34 
35 // Default constraint for the probe arguments as operands.
36 #ifndef CAFFE_SDT_ARG_CONSTRAINT
37 #define CAFFE_SDT_ARG_CONSTRAINT "nor"
38 #endif
39 
40 // Instruction to emit for the probe.
41 #define CAFFE_SDT_NOP nop
42 
43 // Note section properties.
44 #define CAFFE_SDT_NOTE_NAME "stapsdt"
45 #define CAFFE_SDT_NOTE_TYPE 3
46 
47 // Size of address depending on platform.
48 #ifdef __LP64__
49 #define CAFFE_SDT_ASM_ADDR .8byte
50 #else
51 #define CAFFE_SDT_ASM_ADDR .4byte
52 #endif
53 
54 // Assembler helper Macros.
55 #define CAFFE_SDT_S(x) #x
56 #define CAFFE_SDT_ASM_1(x) CAFFE_SDT_S(x) "\n"
57 #define CAFFE_SDT_ASM_2(a, b) CAFFE_SDT_S(a) "," CAFFE_SDT_S(b) "\n"
58 #define CAFFE_SDT_ASM_3(a, b, c) CAFFE_SDT_S(a) "," CAFFE_SDT_S(b) "," \
59  CAFFE_SDT_S(c) "\n"
60 #define CAFFE_SDT_ASM_STRING(x) CAFFE_SDT_ASM_1(.asciz CAFFE_SDT_S(x))
61 
62 // Helper to determine the size of an argument.
63 #define CAFFE_SDT_ISARRAY(x) (__builtin_classify_type(x) == 14)
64 #define CAFFE_SDT_ARGSIZE(x) (CAFFE_SDT_ISARRAY(x) ? sizeof(void*) : sizeof(x))
65 
66 // Format of each probe arguments as operand.
67 // Size of the arugment tagged with CAFFE_SDT_Sn, with "n" constraint.
68 // Value of the argument tagged with CAFFE_SDT_An, with configured constraint.
69 #define CAFFE_SDT_ARG(n, x) \
70  [CAFFE_SDT_S##n] "n" ((size_t)CAFFE_SDT_ARGSIZE(x)), \
71  [CAFFE_SDT_A##n] CAFFE_SDT_ARG_CONSTRAINT (x)
72 
73 // Templates to append arguments as operands.
74 #define CAFFE_SDT_OPERANDS_0() [__sdt_dummy] "g" (0)
75 #define CAFFE_SDT_OPERANDS_1(_1) CAFFE_SDT_ARG(1, _1)
76 #define CAFFE_SDT_OPERANDS_2(_1, _2) \
77  CAFFE_SDT_OPERANDS_1(_1), CAFFE_SDT_ARG(2, _2)
78 #define CAFFE_SDT_OPERANDS_3(_1, _2, _3) \
79  CAFFE_SDT_OPERANDS_2(_1, _2), CAFFE_SDT_ARG(3, _3)
80 #define CAFFE_SDT_OPERANDS_4(_1, _2, _3, _4) \
81  CAFFE_SDT_OPERANDS_3(_1, _2, _3), CAFFE_SDT_ARG(4, _4)
82 #define CAFFE_SDT_OPERANDS_5(_1, _2, _3, _4, _5) \
83  CAFFE_SDT_OPERANDS_4(_1, _2, _3, _4), CAFFE_SDT_ARG(5, _5)
84 #define CAFFE_SDT_OPERANDS_6(_1, _2, _3, _4, _5, _6) \
85  CAFFE_SDT_OPERANDS_5(_1, _2, _3, _4, _5), CAFFE_SDT_ARG(6, _6)
86 #define CAFFE_SDT_OPERANDS_7(_1, _2, _3, _4, _5, _6, _7) \
87  CAFFE_SDT_OPERANDS_6(_1, _2, _3, _4, _5, _6), CAFFE_SDT_ARG(7, _7)
88 #define CAFFE_SDT_OPERANDS_8(_1, _2, _3, _4, _5, _6, _7, _8) \
89  CAFFE_SDT_OPERANDS_7(_1, _2, _3, _4, _5, _6, _7), CAFFE_SDT_ARG(8, _8)
90 
91 // Templates to reference the arguments from operands in note section.
92 #define CAFFE_SDT_ARGFMT(no) %n[CAFFE_SDT_S##no]@%[CAFFE_SDT_A##no]
93 #define CAFFE_SDT_ARG_TEMPLATE_0 /*No arguments*/
94 #define CAFFE_SDT_ARG_TEMPLATE_1 CAFFE_SDT_ARGFMT(1)
95 #define CAFFE_SDT_ARG_TEMPLATE_2 CAFFE_SDT_ARG_TEMPLATE_1 CAFFE_SDT_ARGFMT(2)
96 #define CAFFE_SDT_ARG_TEMPLATE_3 CAFFE_SDT_ARG_TEMPLATE_2 CAFFE_SDT_ARGFMT(3)
97 #define CAFFE_SDT_ARG_TEMPLATE_4 CAFFE_SDT_ARG_TEMPLATE_3 CAFFE_SDT_ARGFMT(4)
98 #define CAFFE_SDT_ARG_TEMPLATE_5 CAFFE_SDT_ARG_TEMPLATE_4 CAFFE_SDT_ARGFMT(5)
99 #define CAFFE_SDT_ARG_TEMPLATE_6 CAFFE_SDT_ARG_TEMPLATE_5 CAFFE_SDT_ARGFMT(6)
100 #define CAFFE_SDT_ARG_TEMPLATE_7 CAFFE_SDT_ARG_TEMPLATE_6 CAFFE_SDT_ARGFMT(7)
101 #define CAFFE_SDT_ARG_TEMPLATE_8 CAFFE_SDT_ARG_TEMPLATE_7 CAFFE_SDT_ARGFMT(8)
102 
103 // Structure of note section for the probe.
104 #define CAFFE_SDT_NOTE_CONTENT(provider, name, arg_template) \
105  CAFFE_SDT_ASM_1(990: CAFFE_SDT_NOP) \
106  CAFFE_SDT_ASM_3( .pushsection .note.stapsdt,"","note") \
107  CAFFE_SDT_ASM_1( .balign 4) \
108  CAFFE_SDT_ASM_3( .4byte 992f-991f, 994f-993f, CAFFE_SDT_NOTE_TYPE) \
109  CAFFE_SDT_ASM_1(991: .asciz CAFFE_SDT_NOTE_NAME) \
110  CAFFE_SDT_ASM_1(992: .balign 4) \
111  CAFFE_SDT_ASM_1(993: CAFFE_SDT_ASM_ADDR 990b) \
112  CAFFE_SDT_ASM_1( CAFFE_SDT_ASM_ADDR 0) /*Reserved for Semaphore address*/\
113  CAFFE_SDT_ASM_1( CAFFE_SDT_ASM_ADDR 0) /*Reserved for Semaphore name*/ \
114  CAFFE_SDT_ASM_STRING(provider) \
115  CAFFE_SDT_ASM_STRING(name) \
116  CAFFE_SDT_ASM_STRING(arg_template) \
117  CAFFE_SDT_ASM_1(994: .balign 4) \
118  CAFFE_SDT_ASM_1( .popsection)
119 
120 // Main probe Macro.
121 #define CAFFE_SDT_PROBE(provider, name, n, arglist) \
122  __asm__ __volatile__ ( \
123  CAFFE_SDT_NOTE_CONTENT(provider, name, CAFFE_SDT_ARG_TEMPLATE_##n) \
124  :: CAFFE_SDT_OPERANDS_##n arglist \
125  ) \
126 
127 // Helper Macros to handle variadic arguments.
128 #define CAFFE_SDT_NARG_(_0, _1, _2, _3, _4, _5, _6, _7, _8, N, ...) N
129 #define CAFFE_SDT_NARG(...) \
130  CAFFE_SDT_NARG_(__VA_ARGS__, 8, 7, 6, 5, 4, 3, 2, 1, 0)
131 #define CAFFE_SDT_PROBE_N(provider, name, N, ...) \
132  CAFFE_SDT_PROBE(provider, name, N, (__VA_ARGS__))