• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022-2024 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 INTERFACES_INNERKITS_NATIVE_HITRACE_METER_H
17 #define INTERFACES_INNERKITS_NATIVE_HITRACE_METER_H
18 
19 #include <string>
20 #include <unistd.h>
21 
22 #include "hitrace_meter_c.h"
23 #ifdef HITRACE_UNITTEST
24 #include <hilog/log.h>
25 #include "param/sys_param.h"
26 #endif
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 constexpr uint64_t HITRACE_TAG_NEVER = 0; // This tag is never enabled.
33 constexpr uint64_t HITRACE_TAG_ALWAYS = (1ULL << 0); // This tag is always enabled.
34 constexpr uint64_t HITRACE_TAG_DRM = (1ULL << 6); // Media subsystem Digital Rights Management tag.
35 constexpr uint64_t HITRACE_TAG_SECURITY = (1ULL << 7); // Security subsystem tag.
36 constexpr uint64_t HITRACE_TAG_ANIMATION = (1ULL << 9); // Animation tag.
37 constexpr uint64_t HITRACE_TAG_PUSH = (1ULL << 10); // Push subsystem tag.
38 constexpr uint64_t HITRACE_TAG_VIRSE = (1ULL << 11); // virtualization service.
39 constexpr uint64_t HITRACE_TAG_MUSL = (1ULL << 12); // musl module.
40 constexpr uint64_t HITRACE_TAG_FFRT = (1ULL << 13); // ffrt tasks.
41 constexpr uint64_t HITRACE_TAG_CLOUD = (1ULL << 14); // Cloud subsystem tag.
42 constexpr uint64_t HITRACE_TAG_DEV_AUTH = (1ULL << 15); // Device auth module tag.
43 constexpr uint64_t HITRACE_TAG_COMMONLIBRARY = (1ULL << 16); // Commonlibrary subsystem tag.
44 constexpr uint64_t HITRACE_TAG_HDCD = (1ULL << 17); // HDCD tag.
45 constexpr uint64_t HITRACE_TAG_HDF = (1ULL << 18); // HDF subsystem tag.
46 constexpr uint64_t HITRACE_TAG_USB = (1ULL << 19); // USB subsystem tag.
47 constexpr uint64_t HITRACE_TAG_INTERCONNECTION = (1ULL << 20); // Interconnection subsystem tag.
48 constexpr uint64_t HITRACE_TAG_DLP_CREDENTIAL = (1ULL << 21); // This tag is dlp credential service.
49 constexpr uint64_t HITRACE_TAG_ACCESS_CONTROL = (1ULL << 22); // This tag is access control tag.
50 constexpr uint64_t HITRACE_TAG_NET = (1ULL << 23); // Net tag.
51 constexpr uint64_t HITRACE_TAG_NWEB = (1ULL << 24); // NWeb tag.
52 constexpr uint64_t HITRACE_TAG_HUKS = (1ULL << 25); // This tag is huks.
53 constexpr uint64_t HITRACE_TAG_USERIAM = (1ULL << 26); // This tag is useriam.
54 constexpr uint64_t HITRACE_TAG_DISTRIBUTED_AUDIO = (1ULL << 27); // Distributed audio tag.
55 constexpr uint64_t HITRACE_TAG_DLSM = (1ULL << 28); // device security level tag.
56 constexpr uint64_t HITRACE_TAG_FILEMANAGEMENT = (1ULL << 29); // filemanagement tag.
57 constexpr uint64_t HITRACE_TAG_OHOS = (1ULL << 30); // OHOS generic tag.
58 constexpr uint64_t HITRACE_TAG_ABILITY_MANAGER = (1ULL << 31); // Ability Manager tag.
59 constexpr uint64_t HITRACE_TAG_ZCAMERA = (1ULL << 32); // Camera module tag.
60 constexpr uint64_t HITRACE_TAG_ZMEDIA = (1ULL << 33); // Media module tag.
61 constexpr uint64_t HITRACE_TAG_ZIMAGE = (1ULL << 34); // Image module tag.
62 constexpr uint64_t HITRACE_TAG_ZAUDIO = (1ULL << 35); // Audio module tag.
63 constexpr uint64_t HITRACE_TAG_DISTRIBUTEDDATA = (1ULL << 36); // Distributeddata manager module tag.
64 constexpr uint64_t HITRACE_TAG_MDFS = (1ULL << 37); // Mobile distributed file system tag.
65 constexpr uint64_t HITRACE_TAG_GRAPHIC_AGP = (1ULL << 38); // Graphic module tag.
66 constexpr uint64_t HITRACE_TAG_ACE = (1ULL << 39); // ACE development framework tag.
67 constexpr uint64_t HITRACE_TAG_NOTIFICATION = (1ULL << 40); // Notification module tag.
68 constexpr uint64_t HITRACE_TAG_MISC = (1ULL << 41); // Notification module tag.
69 constexpr uint64_t HITRACE_TAG_MULTIMODALINPUT = (1ULL << 42); // Multi modal module tag.
70 constexpr uint64_t HITRACE_TAG_SENSORS = (1ULL << 43); // Sensors mudule tag.
71 constexpr uint64_t HITRACE_TAG_MSDP = (1ULL << 44); // Multimodal Sensor Data Platform module tag.
72 constexpr uint64_t HITRACE_TAG_DSOFTBUS = (1ULL << 45); // Distributed Softbus tag.
73 constexpr uint64_t HITRACE_TAG_RPC = (1ULL << 46); // RPC and IPC tag.
74 constexpr uint64_t HITRACE_TAG_ARK = (1ULL << 47); // ARK tag.
75 constexpr uint64_t HITRACE_TAG_WINDOW_MANAGER = (1ULL << 48); // window manager tag.
76 constexpr uint64_t HITRACE_TAG_ACCOUNT_MANAGER = (1ULL << 49); // account manager tag.
77 constexpr uint64_t HITRACE_TAG_DISTRIBUTED_SCREEN = (1ULL << 50); // Distributed screen tag.
78 constexpr uint64_t HITRACE_TAG_DISTRIBUTED_CAMERA = (1ULL << 51); // Distributed camera tag.
79 constexpr uint64_t HITRACE_TAG_DISTRIBUTED_HARDWARE_FWK = (1ULL << 52); // Distributed hardware fwk tag.
80 constexpr uint64_t HITRACE_TAG_GLOBAL_RESMGR = (1ULL << 53); // Global resource manager tag.
81 constexpr uint64_t HITRACE_TAG_DEVICE_MANAGER = (1ULL << 54); // Distributed hardware devicemanager tag.
82 constexpr uint64_t HITRACE_TAG_SAMGR = (1ULL << 55); // SA tag.
83 constexpr uint64_t HITRACE_TAG_POWER = (1ULL << 56); // power manager tag.
84 constexpr uint64_t HITRACE_TAG_DISTRIBUTED_SCHEDULE = (1ULL << 57); // Distributed schedule tag.
85 constexpr uint64_t HITRACE_TAG_DEVICE_PROFILE = (1ULL << 58); // device profile tag.
86 constexpr uint64_t HITRACE_TAG_DISTRIBUTED_INPUT = (1ULL << 59); // Distributed input tag.
87 constexpr uint64_t HITRACE_TAG_BLUETOOTH = (1ULL << 60); // bluetooth tag.
88 constexpr uint64_t HITRACE_TAG_ACCESSIBILITY_MANAGER = (1ULL << 61); // accessibility manager tag.
89 constexpr uint64_t HITRACE_TAG_APP = (1ULL << 62); // App tag.
90 
91 constexpr uint64_t HITRACE_TAG_LAST = HITRACE_TAG_APP;
92 constexpr uint64_t HITRACE_TAG_NOT_READY = (1ULL << 63); // Reserved for initialization.
93 constexpr uint64_t HITRACE_TAG_VALID_MASK = ((HITRACE_TAG_LAST - 1) | HITRACE_TAG_LAST);
94 constexpr uint64_t HITRACE_TAG_COMMERCIAL = (1ULL << 5); // Tag for commercial version.
95 
96 #ifndef HITRACE_TAG
97 #define HITRACE_TAG HITRACE_TAG_NEVER
98 #elif HITRACE_TAG > HITRACE_TAG_VALID_MASK
99 #error HITRACE_TAG must be defined to be one of the tags defined in hitrace_meter.h
100 #elif HITRACE_TAG < HITRACE_TAG_OHOS
101 #error HITRACE_TAG must be defined to be one of the tags defined in hitrace_meter.h
102 #endif
103 
104 #define TOKENPASTE(x, y) x ## y
105 #define TOKENPASTE2(x, y) TOKENPASTE(x, y)
106 #define HITRACE_METER_NAME(TAG, str) HitraceScoped TOKENPASTE2(tracer, __LINE__)(TAG, str)
107 #define HITRACE_METER(TAG) HITRACE_METER_NAME(TAG, __func__)
108 #define HITRACE_METER_FMT(TAG, fmt, ...) HitraceMeterFmtScoped TOKENPASTE2(tracer, __LINE__)(TAG, fmt, ##__VA_ARGS__)
109 
110 #define HITRACE_METER_NAME_EX(level, TAG, name, customArgs) \
111     HitraceScopedEx TOKENPASTE2(tracer, __LINE__)(level, TAG, name, customArgs)
112 #define HITRACE_METER_EX(level, TAG, customArgs) \
113     HITRACE_METER_NAME_EX(level, TAG, __func__, customArgs)
114 #define HITRACE_METER_FMT_EX(level, TAG, customArgs, fmt, ...) \
115     HitraceMeterFmtScopedEx TOKENPASTE2(tracer, __LINE__)(level, TAG, customArgs, fmt, ##__VA_ARGS__)
116 
117 /**
118  * Update trace label when your process has started.
119  */
120 void UpdateTraceLabel(void);
121 
122 /**
123  * Disable trace outputs.
124  * You should know what you are doing when calling this function.
125  */
126 void SetTraceDisabled(bool disable);
127 
128 /**
129  * Track the beginning of a context.
130  */
131 void StartTrace(uint64_t tag, const std::string& name, float limit = -1);
132 void StartTraceEx(HiTraceOutputLevel level, uint64_t tag, const char* name, const char* customArgs = "");
133 void StartTraceDebug(bool isDebug, uint64_t tag, const std::string& name, float limit = -1);
134 void StartTraceArgs(uint64_t tag, const char* fmt, ...);
135 void StartTraceArgsEx(HiTraceOutputLevel level, uint64_t tag, const char* customArgs, const char* fmt, ...);
136 void StartTraceArgsDebug(bool isDebug, uint64_t tag, const char* fmt, ...);
137 void StartTraceWrapper(uint64_t tag, const char* name);
138 
139 /**
140  * Track the end of a context.
141  */
142 void FinishTrace(uint64_t tag);
143 void FinishTraceEx(HiTraceOutputLevel level, uint64_t tag);
144 void FinishTraceDebug(bool isDebug, uint64_t tag);
145 
146 /**
147  * Track the beginning of an asynchronous event.
148  */
149 void StartAsyncTrace(uint64_t tag, const std::string& name, int32_t taskId, float limit = -1);
150 void StartAsyncTraceEx(HiTraceOutputLevel level, uint64_t tag, const char* name, int32_t taskId,
151     const char* customCategory, const char* customArgs = "");
152 void StartAsyncTraceDebug(bool isDebug, uint64_t tag, const std::string& name, int32_t taskId, float limit = -1);
153 void StartAsyncTraceArgs(uint64_t tag, int32_t taskId, const char* fmt, ...);
154 void StartAsyncTraceArgsEx(HiTraceOutputLevel level, uint64_t tag, int32_t taskId,
155     const char* customCategory, const char* customArgs, const char* fmt, ...);
156 void StartAsyncTraceArgsDebug(bool isDebug, uint64_t tag, int32_t taskId, const char* fmt, ...);
157 void StartAsyncTraceWrapper(uint64_t tag, const char* name, int32_t taskId);
158 
159 /**
160  * Track the beginning of an hitrace chain event.
161  */
162 struct HiTraceIdStruct;
163 void StartTraceChain(uint64_t tag, const struct HiTraceIdStruct* hiTraceId, const char* name);
164 
165 /**
166  * Track the end of an asynchronous event.
167  */
168 void FinishAsyncTrace(uint64_t tag, const std::string& name, int32_t taskId);
169 void FinishAsyncTraceEx(HiTraceOutputLevel level, uint64_t tag, const char* name, int32_t taskId);
170 void FinishAsyncTraceDebug(bool isDebug, uint64_t tag, const std::string& name, int32_t taskId);
171 void FinishAsyncTraceArgs(uint64_t tag, int32_t taskId, const char* fmt, ...);
172 void FinishAsyncTraceArgsEx(HiTraceOutputLevel level, uint64_t tag, int32_t taskId, const char* fmt, ...);
173 void FinishAsyncTraceArgsDebug(bool isDebug, uint64_t tag, int32_t taskId, const char* fmt, ...);
174 void FinishAsyncTraceWrapper(uint64_t tag, const char* name, int32_t taskId);
175 
176 /**
177  * Track the middle of a context. Match the previous function of StartTrace before it.
178  */
179 void MiddleTrace(uint64_t tag, const std::string& beforeValue, const std::string& afterValue);
180 void MiddleTraceDebug(bool isDebug, uint64_t tag, const std::string& beforeValue, const std::string& afterValue);
181 
182 /**
183  * Track the 64-bit integer counter value.
184  */
185 void CountTrace(uint64_t tag, const std::string& name, int64_t count);
186 void CountTraceEx(HiTraceOutputLevel level, uint64_t tag, const char* name, int64_t count);
187 void CountTraceDebug(bool isDebug, uint64_t tag, const std::string& name, int64_t count);
188 void CountTraceWrapper(uint64_t tag, const char* name, int64_t count);
189 
190 bool IsTagEnabled(uint64_t tag);
191 void ParseTagBits(const uint64_t tag, char* bitStr, const int bitStrSize);
192 
193 enum RetType {
194     RET_SUCC = 0, // Successful
195     RET_STARTED = 1, // The capture process has already started
196     RET_STOPPED = 2, // The capture process has stopped
197     RET_FAILD = 1000, // Other failures
198     RET_FAIL_INVALID_ARGS = 1001, // Invalid parameter
199     RET_FAIL_MKDIR = 1002, // Failed to create dir
200     RET_FAIL_SETACL = 1003, // Failed to set the acl permission
201     RET_FAIL_ENOENT = 1004, // The file does not exist
202     RET_FAIL_EACCES = 1005, // No permission to open file
203 };
204 
205 enum TraceFlag {
206     FLAG_MAIN_THREAD = 1,
207     FLAG_ALL_THREAD = 2
208 };
209 
210 #ifdef HITRACE_UNITTEST
211 void SetReloadPid(bool isReloadPid);
212 void SetpidHasReload(bool ispidHasReload);
213 void SetMarkerFd(int markerFd);
214 void SetCachedHandle(const char* name, CachedHandle cachedHandle);
215 void SetWriteOnceLog(LogLevel loglevel, const std::string& logStr, bool& isWrite);
216 #endif
217 
218 int StartCaptureAppTrace(TraceFlag flag, uint64_t tags, uint64_t limitSize, std::string& fileName);
219 int StopCaptureAppTrace(void);
220 
221 class HitraceScopedEx {
222 public:
223     inline HitraceScopedEx(HiTraceOutputLevel level, uint64_t tag, const char* name,
tag_(tag)224         const char* customArgs = "") : tag_(tag), level_(level)
225     {
226         StartTraceEx(level_, tag_, name, customArgs);
227     }
228 
~HitraceScopedEx()229     inline ~HitraceScopedEx()
230     {
231         FinishTraceEx(level_, tag_);
232     }
233 private:
234     uint64_t tag_;
235     HiTraceOutputLevel level_;
236 };
237 
238 class HitraceScoped {
239 public:
HitraceScoped(uint64_t tag,const std::string & name)240     inline HitraceScoped(uint64_t tag, const std::string& name) : mTag(tag)
241     {
242         StartTrace(mTag, name);
243     }
244 
~HitraceScoped()245     inline ~HitraceScoped()
246     {
247         FinishTrace(mTag);
248     }
249 private:
250     uint64_t mTag;
251 };
252 
253 class HitracePerfScoped {
254 public:
255     HitracePerfScoped(bool isDebug, uint64_t tag, const std::string& name);
256 
257     ~HitracePerfScoped();
258 
GetInsCount()259     inline long long GetInsCount()
260     {
261         if (fd1st_ == -1) {
262             return err_;
263         }
264         read(fd1st_, &countIns_, sizeof(long long));
265         return countIns_;
266     }
267 
GetCycleCount()268     inline long long GetCycleCount()
269     {
270         if (fd2nd_ == -1) {
271             return err_;
272         }
273         read(fd2nd_, &countCycles_, sizeof(long long));
274         return countCycles_;
275     }
276 private:
277     uint64_t mTag_;
278     std::string mName_;
279     int fd1st_ = -1;
280     int fd2nd_ = -1;
281     long long countIns_ = 0;
282     long long countCycles_ = 0;
283     int err_ = 0;
284 };
285 
286 class HitraceMeterFmtScoped {
287 public:
288     HitraceMeterFmtScoped(uint64_t tag, const char* fmt, ...);
289 
~HitraceMeterFmtScoped()290     ~HitraceMeterFmtScoped()
291     {
292         FinishTrace(mTag);
293     }
294 private:
295     uint64_t mTag;
296 };
297 
298 class HitraceMeterFmtScopedEx {
299 public:
300     HitraceMeterFmtScopedEx(HiTraceOutputLevel level, uint64_t tag, const char* customArgs, const char* fmt, ...);
301 
~HitraceMeterFmtScopedEx()302     ~HitraceMeterFmtScopedEx()
303     {
304         FinishTraceEx(level_, tag_);
305     }
306 private:
307     uint64_t tag_;
308     HiTraceOutputLevel level_;
309 };
310 #ifdef __cplusplus
311 }
312 #endif
313 #endif // INTERFACES_INNERKITS_NATIVE_HITRACE_METER_H
314