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 INTERFACES_INNERKITS_NATIVE_HITRACE_METER_H 17 #define INTERFACES_INNERKITS_NATIVE_HITRACE_METER_H 18 19 #include <string> 20 21 #ifdef __cplusplus 22 extern "C" { 23 #endif 24 25 constexpr uint64_t HITRACE_TAG_NEVER = 0; // This tag is never enabled. 26 constexpr uint64_t HITRACE_TAG_ALWAYS = (1ULL << 0); // This tag is always enabled. 27 constexpr uint64_t HITRACE_TAG_MUSL = (1ULL << 12); // musl module. 28 constexpr uint64_t HITRACE_TAG_FFRT = (1ULL << 13); // ffrt tasks. 29 constexpr uint64_t HITRACE_TAG_CLOUD = (1ULL << 14); // Cloud subsystem tag. 30 constexpr uint64_t HITRACE_TAG_DEV_AUTH = (1ULL << 15); // Device auth module tag. 31 constexpr uint64_t HITRACE_TAG_COMMONLIBRARY = (1ULL << 16); // Commonlibrary subsystem tag. 32 constexpr uint64_t HITRACE_TAG_HDCD = (1ULL << 17); // HDCD tag. 33 constexpr uint64_t HITRACE_TAG_HDF = (1ULL << 18); // HDF subsystem tag. 34 constexpr uint64_t HITRACE_TAG_USB = (1ULL << 19); // USB subsystem tag. 35 constexpr uint64_t HITRACE_TAG_INTERCONNECTION = (1ULL << 20); // Interconnection subsystem tag. 36 constexpr uint64_t HITRACE_TAG_DLP_CREDENTIAL = (1ULL << 21); // This tag is dlp credential service. 37 constexpr uint64_t HITRACE_TAG_ACCESS_CONTROL = (1ULL << 22); // This tag is access control tag. 38 constexpr uint64_t HITRACE_TAG_NET = (1ULL << 23); // Net tag. 39 constexpr uint64_t HITRACE_TAG_NWEB = (1ULL << 24); // NWeb tag. 40 constexpr uint64_t HITRACE_TAG_HUKS = (1ULL << 25); // This tag is huks. 41 constexpr uint64_t HITRACE_TAG_USERIAM = (1ULL << 26); // This tag is useriam. 42 constexpr uint64_t HITRACE_TAG_DISTRIBUTED_AUDIO = (1ULL << 27); // Distributed audio tag. 43 constexpr uint64_t HITRACE_TAG_DLSM = (1ULL << 28); // device security level tag. 44 constexpr uint64_t HITRACE_TAG_FILEMANAGEMENT = (1ULL << 29); // filemanagement tag. 45 constexpr uint64_t HITRACE_TAG_OHOS = (1ULL << 30); // OHOS generic tag. 46 constexpr uint64_t HITRACE_TAG_ABILITY_MANAGER = (1ULL << 31); // Ability Manager tag. 47 constexpr uint64_t HITRACE_TAG_ZCAMERA = (1ULL << 32); // Camera module tag. 48 constexpr uint64_t HITRACE_TAG_ZMEDIA = (1ULL << 33); // Media module tag. 49 constexpr uint64_t HITRACE_TAG_ZIMAGE = (1ULL << 34); // Image module tag. 50 constexpr uint64_t HITRACE_TAG_ZAUDIO = (1ULL << 35); // Audio module tag. 51 constexpr uint64_t HITRACE_TAG_DISTRIBUTEDDATA = (1ULL << 36); // Distributeddata manager module tag. 52 constexpr uint64_t HITRACE_TAG_MDFS = (1ULL << 37); // Mobile distributed file system tag. 53 constexpr uint64_t HITRACE_TAG_GRAPHIC_AGP = (1ULL << 38); // Graphic module tag. 54 constexpr uint64_t HITRACE_TAG_ACE = (1ULL << 39); // ACE development framework tag. 55 constexpr uint64_t HITRACE_TAG_NOTIFICATION = (1ULL << 40); // Notification module tag. 56 constexpr uint64_t HITRACE_TAG_MISC = (1ULL << 41); // Notification module tag. 57 constexpr uint64_t HITRACE_TAG_MULTIMODALINPUT = (1ULL << 42); // Multi modal module tag. 58 constexpr uint64_t HITRACE_TAG_SENSORS = (1ULL << 43); // Sensors mudule tag. 59 constexpr uint64_t HITRACE_TAG_MSDP = (1ULL << 44); // Multimodal Sensor Data Platform module tag. 60 constexpr uint64_t HITRACE_TAG_DSOFTBUS = (1ULL << 45); // Distributed Softbus tag. 61 constexpr uint64_t HITRACE_TAG_RPC = (1ULL << 46); // RPC and IPC tag. 62 constexpr uint64_t HITRACE_TAG_ARK = (1ULL << 47); // ARK tag. 63 constexpr uint64_t HITRACE_TAG_WINDOW_MANAGER = (1ULL << 48); // window manager tag. 64 constexpr uint64_t HITRACE_TAG_ACCOUNT_MANAGER = (1ULL << 49); // account manager tag. 65 constexpr uint64_t HITRACE_TAG_DISTRIBUTED_SCREEN = (1ULL << 50); // Distributed screen tag. 66 constexpr uint64_t HITRACE_TAG_DISTRIBUTED_CAMERA = (1ULL << 51); // Distributed camera tag. 67 constexpr uint64_t HITRACE_TAG_DISTRIBUTED_HARDWARE_FWK = (1ULL << 52); // Distributed hardware fwk tag. 68 constexpr uint64_t HITRACE_TAG_GLOBAL_RESMGR = (1ULL << 53); // Global resource manager tag. 69 constexpr uint64_t HITRACE_TAG_DEVICE_MANAGER = (1ULL << 54); // Distributed hardware devicemanager tag. 70 constexpr uint64_t HITRACE_TAG_SAMGR = (1ULL << 55); // SA tag. 71 constexpr uint64_t HITRACE_TAG_POWER = (1ULL << 56); // power manager tag. 72 constexpr uint64_t HITRACE_TAG_DISTRIBUTED_SCHEDULE = (1ULL << 57); // Distributed schedule tag. 73 constexpr uint64_t HITRACE_TAG_DEVICE_PROFILE = (1ULL << 58); // device profile tag. 74 constexpr uint64_t HITRACE_TAG_DISTRIBUTED_INPUT = (1ULL << 59); // Distributed input tag. 75 constexpr uint64_t HITRACE_TAG_BLUETOOTH = (1ULL << 60); // bluetooth tag. 76 constexpr uint64_t HITRACE_TAG_ACCESSIBILITY_MANAGER = (1ULL << 61); // accessibility manager tag. 77 constexpr uint64_t HITRACE_TAG_APP = (1ULL << 62); // App tag. 78 79 constexpr uint64_t HITRACE_TAG_LAST = HITRACE_TAG_APP; 80 constexpr uint64_t HITRACE_TAG_NOT_READY = (1ULL << 63); // Reserved for initialization. 81 constexpr uint64_t HITRACE_TAG_VALID_MASK = ((HITRACE_TAG_LAST - 1) | HITRACE_TAG_LAST); 82 83 #ifndef HITRACE_TAG 84 #define HITRACE_TAG HITRACE_TAG_NEVER 85 #elif HITRACE_TAG > HITRACE_TAG_VALID_MASK 86 #error HITRACE_TAG must be defined to be one of the tags defined in hitrace_meter.h 87 #elif HITRACE_TAG < HITRACE_TAG_OHOS 88 #error HITRACE_TAG must be defined to be one of the tags defined in hitrace_meter.h 89 #endif 90 91 #define TOKENPASTE(x, y) x ## y 92 #define TOKENPASTE2(x, y) TOKENPASTE(x, y) 93 #define HITRACE_METER_NAME(TAG, str) HitraceScoped TOKENPASTE2(tracer, __LINE__)(TAG, str) 94 #define HITRACE_METER(TAG) HITRACE_METER_NAME(TAG, __func__) 95 #define HITRACE_METER_FMT(TAG, fmt, ...) HitraceMeterFmtScoped TOKENPASTE2(tracer, __LINE__)(TAG, fmt, ##__VA_ARGS__) 96 97 /** 98 * Update trace label when your process has started. 99 */ 100 void UpdateTraceLabel(); 101 102 /** 103 * Disable trace outputs. 104 * You should know what you are doing when calling this function. 105 */ 106 void SetTraceDisabled(bool disable); 107 108 /** 109 * Track the beginning of a context. 110 */ 111 void StartTrace(uint64_t label, const std::string& value, float limit = -1); 112 void StartTraceDebug(bool isDebug, uint64_t label, const std::string& value, float limit = -1); 113 void StartTraceArgs(uint64_t label, const char *fmt, ...); 114 void StartTraceArgsDebug(bool isDebug, uint64_t label, const char *fmt, ...); 115 void StartTraceWrapper(uint64_t label, const char *value); 116 117 /** 118 * Track the end of a context. 119 */ 120 void FinishTrace(uint64_t label); 121 void FinishTraceDebug(bool isDebug, uint64_t label); 122 123 /** 124 * Track the beginning of an asynchronous event. 125 */ 126 void StartAsyncTrace(uint64_t label, const std::string& value, int32_t taskId, float limit = -1); 127 void StartAsyncTraceDebug(bool isDebug, uint64_t label, const std::string& value, int32_t taskId, float limit = -1); 128 void StartAsyncTraceArgs(uint64_t label, int32_t taskId, const char *fmt, ...); 129 void StartAsyncTraceArgsDebug(bool isDebug, uint64_t label, int32_t taskId, const char *fmt, ...); 130 void StartAsyncTraceWrapper(uint64_t label, const char *value, int32_t taskId); 131 132 /** 133 * Track the end of an asynchronous event. 134 */ 135 void FinishAsyncTrace(uint64_t label, const std::string& value, int32_t taskId); 136 void FinishAsyncTraceDebug(bool isDebug, uint64_t label, const std::string& value, int32_t taskId); 137 void FinishAsyncTraceArgs(uint64_t label, int32_t taskId, const char *fmt, ...); 138 void FinishAsyncTraceArgsDebug(bool isDebug, uint64_t label, int32_t taskId, const char *fmt, ...); 139 void FinishAsyncTraceWrapper(uint64_t label, const char *value, int32_t taskId); 140 141 /** 142 * Track the middle of a context. Match the previous function of StartTrace before it. 143 */ 144 void MiddleTrace(uint64_t label, const std::string& beforeValue, const std::string& afterValue); 145 void MiddleTraceDebug(bool isDebug, uint64_t label, const std::string& beforeValue, const std::string& afterValue); 146 147 /** 148 * Track the 64-bit integer counter value. 149 */ 150 void CountTrace(uint64_t label, const std::string& name, int64_t count); 151 void CountTraceDebug(bool isDebug, uint64_t label, const std::string& name, int64_t count); 152 void CountTraceWrapper(uint64_t label, const char *name, int64_t count); 153 154 class HitraceScoped { 155 public: HitraceScoped(uint64_t tag,const std::string & value)156 inline HitraceScoped(uint64_t tag, const std::string &value) : mTag(tag) 157 { 158 StartTrace(mTag, value); 159 } 160 ~HitraceScoped()161 inline ~HitraceScoped() 162 { 163 FinishTrace(mTag); 164 } 165 private: 166 uint64_t mTag; 167 }; 168 169 class HitraceMeterFmtScoped { 170 public: 171 HitraceMeterFmtScoped(uint64_t tag, const char *fmt, ...); 172 ~HitraceMeterFmtScoped()173 ~HitraceMeterFmtScoped() 174 { 175 FinishTrace(mTag); 176 } 177 private: 178 uint64_t mTag; 179 }; 180 #ifdef __cplusplus 181 } 182 #endif 183 #endif // INTERFACES_INNERKITS_NATIVE_HITRACE_METER_H 184