1 /* 2 * Copyright (C) 2021 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 DEVELOPTOOLS_INTERFACES_INNERKITS_BYTRACE_INCLUDE_BYTRACE_H 17 #define DEVELOPTOOLS_INTERFACES_INNERKITS_BYTRACE_INCLUDE_BYTRACE_H 18 19 #include <string> 20 21 #ifdef __cplusplus 22 extern "C" { 23 #endif 24 25 constexpr uint64_t BYTRACE_TAG_NEVER = 0; // This tag is never enabled. 26 constexpr uint64_t BYTRACE_TAG_ALWAYS = (1ULL << 0); // This tag is always enabled. 27 constexpr uint64_t BYTRACE_TAG_OHOS = (1ULL << 30); // OHOS generic tag. 28 constexpr uint64_t BYTRACE_TAG_ABILITY_MANAGER = (1ULL << 31); // Ability Manager tag. 29 constexpr uint64_t BYTRACE_TAG_ZCAMERA = (1ULL << 32); // Camera module tag. 30 constexpr uint64_t BYTRACE_TAG_ZMEDIA = (1ULL << 33); // Media module tag. 31 constexpr uint64_t BYTRACE_TAG_ZIMAGE = (1ULL << 34); // Image module tag. 32 constexpr uint64_t BYTRACE_TAG_ZAUDIO = (1ULL << 35); // Audio module tag. 33 constexpr uint64_t BYTRACE_TAG_DISTRIBUTEDDATA = (1ULL << 36); // Distributeddata manager module tag. 34 constexpr uint64_t BYTRACE_TAG_MDFS = (1ULL << 37); // Mobile distributed file system tag. 35 constexpr uint64_t BYTRACE_TAG_GRAPHIC_AGP = (1ULL << 38); // Graphic module tag. 36 constexpr uint64_t BYTRACE_TAG_ACE = (1ULL << 39); // ACE development framework tag. 37 constexpr uint64_t BYTRACE_TAG_NOTIFICATION = (1ULL << 40); // Notification module tag. 38 constexpr uint64_t BYTRACE_TAG_MISC = (1ULL << 41); // Notification module tag. 39 constexpr uint64_t BYTRACE_TAG_MULTIMODALINPUT = (1ULL << 42); // Multi modal module tag. 40 constexpr uint64_t BYTRACE_TAG_SENSORS = (1ULL << 43); // Sensors mudule tag. 41 constexpr uint64_t BYTRACE_TAG_MSDP = (1ULL << 44); // Multimodal Sensor Data Platform module tag. 42 constexpr uint64_t BYTRACE_TAG_DSOFTBUS = (1ULL << 45); // Distributed Softbus tag. 43 constexpr uint64_t BYTRACE_TAG_RPC = (1ULL << 46); // RPC and IPC tag. 44 constexpr uint64_t BYTRACE_TAG_ARK = (1ULL << 47); // ARK tag. 45 constexpr uint64_t BYTRACE_TAG_WINDOW_MANAGER = (1ULL << 48); // window manager tag. 46 constexpr uint64_t BYTRACE_TAG_APP = (1ULL << 62); // App tag. 47 48 constexpr uint64_t BYTRACE_TAG_LAST = BYTRACE_TAG_APP; 49 constexpr uint64_t BYTRACE_TAG_NOT_READY = (1ULL << 63); // Reserved for initialization. 50 constexpr uint64_t BYTRACE_TAG_VALID_MASK = ((BYTRACE_TAG_LAST - 1) | BYTRACE_TAG_LAST); 51 52 #ifndef BYTRACE_TAG 53 #define BYTRACE_TAG BYTRACE_TAG_NEVER 54 #elif BYTRACE_TAG > BYTRACE_TAG_VALID_MASK 55 #error BYTRACE_TAG must be defined to be one of the tags defined in bytrace.h 56 #elif BYTRACE_TAG < BYTRACE_TAG_OHOS 57 #error BYTRACE_TAG must be defined to be one of the tags defined in bytrace.h 58 #endif 59 60 #define RELEASE_LEVEL 0X01 61 #define DEBUG_LEVEL 0X02 62 63 #ifndef TRACE_LEVEL 64 #define TRACE_LEVEL RELEASE 65 #endif 66 67 #define TOKENPASTE(x, y) x ## y 68 #define TOKENPASTE2(x, y) TOKENPASTE(x, y) 69 #define BYTRACE_NAME(TAG, fmt, ...) ByTraceScoped TOKENPASTE2(tracer, __LINE__)(TAG, fmt, ##__VA_ARGS__) 70 #define BYTRACE(TAG) BYTRACE_NAME(TAG, __func__) 71 72 /** 73 * Update trace label when your process has started. 74 */ 75 void UpdateTraceLabel(); 76 77 /** 78 * Track the beginning of a context. 79 */ 80 void StartTrace(uint64_t label, const std::string& value, float limit = -1); 81 void StartTraceDebug(uint64_t label, const std::string& value, float limit = -1); 82 /** 83 * Track the end of a context. 84 */ 85 void FinishTrace(uint64_t label); 86 void FinishTraceDebug(uint64_t label); 87 /** 88 * Track the beginning of an asynchronous event. 89 */ 90 void StartAsyncTrace(uint64_t label, const std::string& value, int32_t taskId, float limit = -1); 91 void StartAsyncTraceDebug(uint64_t label, const std::string& value, int32_t taskId, float limit = -1); 92 93 /** 94 * Track the end of an asynchronous event. 95 */ 96 void FinishAsyncTrace(uint64_t label, const std::string& value, int32_t taskId); 97 void FinishAsyncTraceDebug(uint64_t label, const std::string& value, int32_t taskId); 98 99 /** 100 * Track the middle of a context. Match the previous function of StartTrace before it. 101 */ 102 void MiddleTrace(uint64_t label, const std::string& beforeValue, const std::string& afterValue); 103 void MiddleTraceDebug(uint64_t label, const std::string& beforeValue, const std::string& afterValue); 104 105 /** 106 * Track the 64-bit integer counter value. 107 */ 108 void CountTrace(uint64_t label, const std::string& name, int64_t count); 109 void CountTraceDebug(uint64_t label, const std::string& name, int64_t count); 110 111 class ByTraceScoped { 112 public: ByTraceScoped(uint64_t tag,const std::string & value)113 inline ByTraceScoped(uint64_t tag, const std::string &value) : mTag(tag) 114 { 115 StartTrace(mTag, value); 116 } 117 ~ByTraceScoped()118 inline ~ByTraceScoped() 119 { 120 FinishTrace(mTag); 121 } 122 private: 123 uint64_t mTag; 124 }; 125 #ifdef __cplusplus 126 } 127 #endif 128 #endif // DEVELOPTOOLS_INTERFACES_INNERKITS_BYTRACE_INCLUDE_BYTRACE_H 129