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