1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
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 #ifndef HIVIEWDFX_HITRACECHAIN_C_H
17 #define HIVIEWDFX_HITRACECHAIN_C_H
18
19 #include <endian.h>
20 #include <stdarg.h>
21 #include <stdint.h>
22
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26
27 typedef enum HiTraceIdValid {
28 HITRACE_ID_INVALID = 0,
29 HITRACE_ID_VALID = 1,
30 } HiTraceIdValid;
31
32 typedef enum HiTraceVersion {
33 HITRACE_VER_1 = 0,
34 } HiTraceVersion;
35
36 typedef enum HiTraceFlag {
37 // MIN: valid.
38 HITRACE_FLAG_MIN = 0,
39 // DEFAULT: default value.
40 HITRACE_FLAG_DEFAULT = 0,
41 // trace sync and async call. default: trace sync call only.
42 HITRACE_FLAG_INCLUDE_ASYNC = 1 << 0,
43 // do not create child span. default: create child span.
44 HITRACE_FLAG_DONOT_CREATE_SPAN = 1 << 1,
45 // output tracepoint info in span. default: do not output tracepoint info.
46 HITRACE_FLAG_TP_INFO = 1 << 2,
47 // do not output begin and end info. default: output begin and end info.
48 HITRACE_FLAG_NO_BE_INFO = 1 << 3,
49 // do not add id to log. default: add id to log.
50 HITRACE_FLAG_DONOT_ENABLE_LOG = 1 << 4,
51 // the trace is triggered by fault.
52 HITRACE_FLAG_FAULT_TRIGGER = 1 << 5,
53 // output device-to-device tracepoint info in span only. default: do not output device-to-device tracepoint info.
54 HITRACE_FLAG_D2D_TP_INFO = 1 << 6,
55 // MAX: valid.
56 HITRACE_FLAG_MAX = (1 << 7) - 1,
57 } HiTraceFlag;
58
59 // HiTrace tracepoint type
60 typedef enum HiTraceTracepointType {
61 HITRACE_TP_MIN = 0, // MIN: valid
62 HITRACE_TP_CS = 0, // client send
63 HITRACE_TP_CR = 1, // client receive
64 HITRACE_TP_SS = 2, // server send
65 HITRACE_TP_SR = 3, // server receive
66 HITRACE_TP_GENERAL = 4, // general info
67 HITRACE_TP_MAX = 4, // MAX: valid
68 } HiTraceTracepointType;
69
70 // HiTrace communication mode
71 typedef enum HiTraceCommunicationMode {
72 HITRACE_CM_MIN = 0, // MIN: valid
73 HITRACE_CM_DEFAULT = 0, // unspecified communication mode
74 HITRACE_CM_THREAD = 1, // thread-to-thread communication mode
75 HITRACE_CM_PROCESS = 2, // process-to-process communication mode
76 HITRACE_CM_DEVICE = 3, // device-to-device communication mode
77 HITRACE_CM_MAX = 3, // MAX: valid
78 } HiTraceCommunicationMode;
79
80 typedef struct HiTraceIdStruct {
81 #if __BYTE_ORDER == __LITTLE_ENDIAN
82 uint64_t valid : 1;
83 uint64_t ver : 3;
84 uint64_t chainId : 60;
85
86 uint64_t flags : 12;
87 uint64_t spanId : 26;
88 uint64_t parentSpanId : 26;
89 #elif __BYTE_ORDER == __BIG_ENDIAN
90 uint64_t chainId : 60;
91 uint64_t ver : 3;
92 uint64_t valid : 1;
93
94 uint64_t parentSpanId : 26;
95 uint64_t spanId : 26;
96 uint64_t flags : 12;
97 #else
98 #error "ERROR: No BIG_LITTLE_ENDIAN defines."
99 #endif
100 } HiTraceIdStruct;
101
102 #define HITRACE_ID_LEN sizeof(HiTraceIdStruct)
103
104 HiTraceIdStruct HiTraceChainBegin(const char* name, int flags);
105 void HiTraceChainEnd(const HiTraceIdStruct* pId);
106 HiTraceIdStruct HiTraceChainGetId(void);
107 void HiTraceChainSetId(const HiTraceIdStruct* pId);
108 void HiTraceChainClearId(void);
109 HiTraceIdStruct HiTraceChainCreateSpan(void);
110 void HiTraceChainTracepoint(HiTraceTracepointType type, const HiTraceIdStruct* pId, const char* fmt, ...)
111 __attribute__((__format__(os_log, 3, 4)));
112 void HiTraceChainTracepointWithArgs(HiTraceTracepointType type, const HiTraceIdStruct* pId, const char* fmt,
113 va_list args);
114 void HiTraceChainTracepointEx(HiTraceCommunicationMode mode, HiTraceTracepointType type, const HiTraceIdStruct* pId,
115 const char* fmt, ...) __attribute__((__format__(os_log, 4, 5)));
116 void HiTraceChainTracepointExWithArgs(HiTraceCommunicationMode mode, HiTraceTracepointType type,
117 const HiTraceIdStruct* pId, const char* fmt, va_list args);
118
HiTraceChainInitId(HiTraceIdStruct * pId)119 static inline void HiTraceChainInitId(HiTraceIdStruct* pId)
120 {
121 pId->valid = HITRACE_ID_INVALID;
122 pId->ver = 0;
123 pId->chainId = 0;
124 pId->flags = 0;
125 pId->spanId = 0;
126 pId->parentSpanId = 0;
127 }
128
HiTraceChainIsValid(const HiTraceIdStruct * pId)129 static inline int HiTraceChainIsValid(const HiTraceIdStruct* pId)
130 {
131 return (pId) && (pId->valid == HITRACE_ID_VALID);
132 }
133
HiTraceChainIsFlagEnabled(const HiTraceIdStruct * pId,HiTraceFlag flag)134 static inline int HiTraceChainIsFlagEnabled(const HiTraceIdStruct* pId, HiTraceFlag flag)
135 {
136 return HiTraceChainIsValid(pId) && ((pId->flags & (uint64_t)flag) != 0);
137 }
138
HiTraceChainEnableFlag(HiTraceIdStruct * pId,HiTraceFlag flag)139 static inline void HiTraceChainEnableFlag(HiTraceIdStruct* pId, HiTraceFlag flag)
140 {
141 if (HiTraceChainIsValid(pId)) {
142 pId->flags |= (uint64_t)flag;
143 }
144 return;
145 }
146
HiTraceChainGetFlags(const HiTraceIdStruct * pId)147 static inline int HiTraceChainGetFlags(const HiTraceIdStruct* pId)
148 {
149 if (!HiTraceChainIsValid(pId)) {
150 return 0;
151 }
152 return pId->flags;
153 }
154
HiTraceChainSetFlags(HiTraceIdStruct * pId,int flags)155 static inline void HiTraceChainSetFlags(HiTraceIdStruct* pId, int flags)
156 {
157 if (HiTraceChainIsValid(pId) && (flags >= HITRACE_FLAG_MIN) && (flags < HITRACE_FLAG_MAX)) {
158 pId->flags = (uint64_t)flags;
159 }
160 return;
161 }
162
HiTraceChainGetChainId(const HiTraceIdStruct * pId)163 static inline uint64_t HiTraceChainGetChainId(const HiTraceIdStruct* pId)
164 {
165 if (!HiTraceChainIsValid(pId)) {
166 return 0;
167 }
168 return pId->chainId;
169 }
170
HiTraceChainSetChainId(HiTraceIdStruct * pId,uint64_t chainId)171 static inline void HiTraceChainSetChainId(HiTraceIdStruct* pId, uint64_t chainId)
172 {
173 if (!pId || chainId == 0) {
174 return;
175 }
176
177 if (!HiTraceChainIsValid(pId)) {
178 pId->valid = HITRACE_ID_VALID;
179 pId->ver = HITRACE_VER_1;
180 pId->flags = pId->spanId = pId->parentSpanId = 0;
181 }
182 pId->chainId = chainId;
183 }
184
HiTraceChainGetSpanId(const HiTraceIdStruct * pId)185 static inline uint64_t HiTraceChainGetSpanId(const HiTraceIdStruct* pId)
186 {
187 if (!HiTraceChainIsValid(pId)) {
188 return 0;
189 }
190 return pId->spanId;
191 }
192
HiTraceChainSetSpanId(HiTraceIdStruct * pId,uint64_t spanId)193 static inline void HiTraceChainSetSpanId(HiTraceIdStruct* pId, uint64_t spanId)
194 {
195 if (HiTraceChainIsValid(pId)) {
196 pId->spanId = spanId;
197 }
198 return;
199 }
200
HiTraceChainGetParentSpanId(const HiTraceIdStruct * pId)201 static inline uint64_t HiTraceChainGetParentSpanId(const HiTraceIdStruct* pId)
202 {
203 if (!HiTraceChainIsValid(pId)) {
204 return 0;
205 }
206 return pId->parentSpanId;
207 }
208
HiTraceChainSetParentSpanId(HiTraceIdStruct * pId,uint64_t parentSpanId)209 static inline void HiTraceChainSetParentSpanId(HiTraceIdStruct* pId, uint64_t parentSpanId)
210 {
211 if (HiTraceChainIsValid(pId)) {
212 pId->parentSpanId = parentSpanId;
213 }
214 return;
215 }
216
HiTraceChainIdToBytes(const HiTraceIdStruct * pId,uint8_t * pIdArray,int len)217 static inline int HiTraceChainIdToBytes(const HiTraceIdStruct* pId, uint8_t* pIdArray, int len)
218 {
219 if (!HiTraceChainIsValid(pId) || (len < (int)HITRACE_ID_LEN)) {
220 return 0;
221 }
222
223 *((uint64_t*)pIdArray) = htobe64(*((uint64_t*)pId));
224 *((uint64_t*)pIdArray + 1) = htobe64(*((uint64_t*)pId + 1));
225 return sizeof(HiTraceIdStruct);
226 }
227
HiTraceChainBytesToId(const uint8_t * pIdArray,int len)228 static inline HiTraceIdStruct HiTraceChainBytesToId(const uint8_t* pIdArray, int len)
229 {
230 HiTraceIdStruct id = {0, 0, 0, 0, 0, 0};
231 HiTraceChainInitId(&id);
232
233 if ((!pIdArray) || (len != (int)HITRACE_ID_LEN)) {
234 return id;
235 }
236
237 *((uint64_t*)(&id)) = be64toh(*((uint64_t*)pIdArray));
238 *((uint64_t*)(&id) + 1) = be64toh(*((uint64_t*)pIdArray + 1));
239 return id;
240 }
241
242 #ifdef __cplusplus
243 }
244 #endif
245
246 #endif // HIVIEWDFX_HITRACECHAIN_C_H
247