• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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_HITRACE_C_H
17 #define HIVIEWDFX_HITRACE_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 HiTraceBegin(const char* name, int flags);
105 void HiTraceEnd(const HiTraceIdStruct* pId);
106 HiTraceIdStruct HiTraceGetId();
107 void HiTraceSetId(const HiTraceIdStruct* pId);
108 void HiTraceClearId();
109 HiTraceIdStruct HiTraceCreateSpan();
110 void HiTraceTracepoint(HiTraceTracepointType type, const HiTraceIdStruct* pId, const char* fmt, ...)
111     __attribute__((__format__(os_log, 3, 4)));
112 void HiTraceTracepointWithArgs(HiTraceTracepointType type, const HiTraceIdStruct* pId, const char* fmt, va_list args);
113 void HiTraceTracepointEx(HiTraceCommunicationMode mode, HiTraceTracepointType type, const HiTraceIdStruct* pId,
114     const char* fmt, ...) __attribute__((__format__(os_log, 4, 5)));
115 void HiTraceTracepointExWithArgs(HiTraceCommunicationMode mode, HiTraceTracepointType type, const HiTraceIdStruct* pId,
116     const char* fmt, va_list args);
117 
HiTraceInitId(HiTraceIdStruct * pId)118 static inline void HiTraceInitId(HiTraceIdStruct* pId)
119 {
120     pId->valid = HITRACE_ID_INVALID;
121     pId->ver = 0;
122     pId->chainId = 0;
123     pId->flags = 0;
124     pId->spanId = 0;
125     pId->parentSpanId = 0;
126 }
127 
HiTraceIsValid(const HiTraceIdStruct * pId)128 static inline int HiTraceIsValid(const HiTraceIdStruct* pId)
129 {
130     return (pId) && (pId->valid == HITRACE_ID_VALID);
131 }
132 
HiTraceIsFlagEnabled(const HiTraceIdStruct * pId,HiTraceFlag flag)133 static inline int HiTraceIsFlagEnabled(const HiTraceIdStruct* pId, HiTraceFlag flag)
134 {
135     return HiTraceIsValid(pId) && ((pId->flags & (uint64_t)flag) != 0);
136 }
137 
HiTraceEnableFlag(HiTraceIdStruct * pId,HiTraceFlag flag)138 static inline void HiTraceEnableFlag(HiTraceIdStruct* pId, HiTraceFlag flag)
139 {
140     if (HiTraceIsValid(pId)) {
141         pId->flags |= (uint64_t)flag;
142     }
143     return;
144 }
145 
HiTraceGetFlags(const HiTraceIdStruct * pId)146 static inline int HiTraceGetFlags(const HiTraceIdStruct* pId)
147 {
148     if (!HiTraceIsValid(pId)) {
149         return 0;
150     }
151     return pId->flags;
152 }
153 
HiTraceSetFlags(HiTraceIdStruct * pId,int flags)154 static inline void HiTraceSetFlags(HiTraceIdStruct* pId, int flags)
155 {
156     if (HiTraceIsValid(pId) && (flags >= HITRACE_FLAG_MIN) && (flags < HITRACE_FLAG_MAX)) {
157         pId->flags = (uint64_t)flags;
158     }
159     return;
160 }
161 
HiTraceGetChainId(const HiTraceIdStruct * pId)162 static inline uint64_t HiTraceGetChainId(const HiTraceIdStruct* pId)
163 {
164     if (!HiTraceIsValid(pId)) {
165         return 0;
166     }
167     return pId->chainId;
168 }
169 
HiTraceSetChainId(HiTraceIdStruct * pId,uint64_t chainId)170 static inline void HiTraceSetChainId(HiTraceIdStruct* pId, uint64_t chainId)
171 {
172     if (!pId || chainId == 0) {
173         return;
174     }
175 
176     if (!HiTraceIsValid(pId)) {
177         pId->valid = HITRACE_ID_VALID;
178         pId->ver = HITRACE_VER_1;
179         pId->flags = pId->spanId = pId->parentSpanId = 0;
180     }
181     pId->chainId = chainId;
182 }
183 
HiTraceGetSpanId(const HiTraceIdStruct * pId)184 static inline uint64_t HiTraceGetSpanId(const HiTraceIdStruct* pId)
185 {
186     if (!HiTraceIsValid(pId)) {
187         return 0;
188     }
189     return pId->spanId;
190 }
191 
HiTraceSetSpanId(HiTraceIdStruct * pId,uint64_t spanId)192 static inline void HiTraceSetSpanId(HiTraceIdStruct* pId, uint64_t spanId)
193 {
194     if (HiTraceIsValid(pId)) {
195         pId->spanId = spanId;
196     }
197     return;
198 }
199 
HiTraceGetParentSpanId(const HiTraceIdStruct * pId)200 static inline uint64_t HiTraceGetParentSpanId(const HiTraceIdStruct* pId)
201 {
202     if (!HiTraceIsValid(pId)) {
203         return 0;
204     }
205     return pId->parentSpanId;
206 }
207 
HiTraceSetParentSpanId(HiTraceIdStruct * pId,uint64_t parentSpanId)208 static inline void HiTraceSetParentSpanId(HiTraceIdStruct* pId, uint64_t parentSpanId)
209 {
210     if (HiTraceIsValid(pId)) {
211         pId->parentSpanId = parentSpanId;
212     }
213     return;
214 }
215 
HiTraceIdToBytes(const HiTraceIdStruct * pId,uint8_t * pIdArray,int len)216 static inline int HiTraceIdToBytes(const HiTraceIdStruct* pId, uint8_t* pIdArray, int len)
217 {
218     if (!HiTraceIsValid(pId) || (len < (int)HITRACE_ID_LEN)) {
219         return 0;
220     }
221 
222     *((uint64_t*)pIdArray) = htobe64(*((uint64_t*)pId));
223     *((uint64_t*)pIdArray + 1) = htobe64(*((uint64_t*)pId + 1));
224     return sizeof(HiTraceIdStruct);
225 }
226 
HiTraceBytesToId(const uint8_t * pIdArray,int len)227 static inline HiTraceIdStruct HiTraceBytesToId(const uint8_t* pIdArray, int len)
228 {
229     HiTraceIdStruct id = {0, 0, 0, 0, 0, 0};
230     HiTraceInitId(&id);
231 
232     if ((!pIdArray) || (len != (int)HITRACE_ID_LEN)) {
233         return id;
234     }
235 
236     *((uint64_t*)(&id)) = be64toh(*((uint64_t*)pIdArray));
237     *((uint64_t*)(&id) + 1) = be64toh(*((uint64_t*)pIdArray + 1));
238     return id;
239 }
240 
241 #ifdef __cplusplus
242 }
243 #endif
244 
245 #endif // HIVIEWDFX_HITRACE_C_H
246