1 /* 2 * Copyright (c) 2025 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 #ifndef HIVIEWDFX_HIVIEW_TRACE_STATE_MACHINE_H 16 #define HIVIEWDFX_HIVIEW_TRACE_STATE_MACHINE_H 17 18 #include <cinttypes> 19 #include <memory> 20 #include <mutex> 21 #include <string> 22 #include <vector> 23 24 #include "singleton.h" 25 #include "trace_common.h" 26 #include "trace_base_state.h" 27 28 namespace OHOS::HiviewDFX { 29 class TraceStateMachine : public OHOS::DelayedRefSingleton<TraceStateMachine> { 30 public: 31 TraceStateMachine(); 32 TraceRet OpenTrace(TraceScenario scenario, const std::vector<std::string> &tagGroups); 33 TraceRet OpenTrace(TraceScenario scenario, const std::string &args); 34 TraceRet OpenTelemetryTrace(const std::string &args, TelemetryPolicy policy); 35 TraceRet OpenDynamicTrace(int32_t appid); 36 TraceRet DumpTrace(TraceScenario scenario, uint32_t maxDuration, uint64_t happenTime, TraceRetInfo &info); 37 TraceRet DumpTraceAsync(const DumpTraceArgs &args, int64_t fileSizeLimit, 38 TraceRetInfo &info, DumpTraceCallback callback); 39 TraceRet DumpTraceWithFilter(uint32_t maxDuration, uint64_t happenTime, TraceRetInfo &info); 40 TraceRet TraceDropOn(TraceScenario scenario); 41 TraceRet TraceDropOff(TraceScenario scenario, TraceRetInfo &info); 42 TraceRet CloseTrace(TraceScenario scenario); 43 TraceRet TraceCacheOn(); 44 TraceRet TraceCacheOff(); 45 TraceRet TraceTelemetryOn(); 46 TraceRet TraceTelemetryOff(); 47 TraceRet PostTelemetryOn(uint64_t time); 48 TraceRet PostTelemetryTimeOut(); 49 TraceRet PowerTelemetryOn(); 50 TraceRet PowerTelemetryOff(); 51 int32_t SetAppFilterInfo(const std::string &bundleName); 52 int32_t SetFilterPidInfo(pid_t pid); 53 void InitTelemetryStatus(bool isStatusOn); 54 void TransToCommonState(); 55 void TransToCommandState(); 56 void TransToCommandDropState(); 57 void TransToCommonDropState(); 58 void TransToTeleMetryState(TelemetryPolicy policy); 59 void TransToDynamicState(int32_t appid); 60 void TransToCloseState(); 61 bool RegisterTelemetryCallback(std::shared_ptr<TelemetryCallback> stateCallback); 62 SetCacheParams(int32_t totalFileSize,int32_t sliceMaxDuration)63 void SetCacheParams(int32_t totalFileSize, int32_t sliceMaxDuration) 64 { 65 cacheSizeLimit_ = totalFileSize; 66 cacheSliceSpan_ = sliceMaxDuration; 67 } 68 GetCurrentAppPid()69 int32_t GetCurrentAppPid() 70 { 71 return currentState_->GetAppPid(); 72 } 73 GetTaskBeginTime()74 uint64_t GetTaskBeginTime() 75 { 76 return currentState_->GetTaskBeginTime(); 77 } 78 SetTraceVersionBeta()79 void SetTraceVersionBeta() 80 { 81 std::lock_guard<std::mutex> lock(traceMutex_); 82 uint8_t beta = 1 << 3; 83 traceSwitchState_ = traceSwitchState_ | beta; 84 RecoverState(); 85 } 86 CloseVersionBeta()87 void CloseVersionBeta() 88 { 89 uint8_t beta = 1 << 3; 90 traceSwitchState_ = traceSwitchState_ & (~beta); 91 } 92 SetTraceSwitchUcOn()93 void SetTraceSwitchUcOn() 94 { 95 std::lock_guard<std::mutex> lock(traceMutex_); 96 uint8_t ucollection = 1 << 2; 97 traceSwitchState_ = traceSwitchState_ | ucollection; 98 RecoverState(); 99 } 100 SetTraceSwitchUcOff()101 void SetTraceSwitchUcOff() 102 { 103 std::lock_guard<std::mutex> lock(traceMutex_); 104 uint8_t ucollection = 1 << 2; 105 traceSwitchState_ = traceSwitchState_ & (~ucollection); 106 RecoverState(); 107 } 108 SetTraceSwitchFreezeOn()109 void SetTraceSwitchFreezeOn() 110 { 111 std::lock_guard<std::mutex> lock(traceMutex_); 112 uint8_t freeze = 1 << 1; 113 traceSwitchState_ = traceSwitchState_ | freeze; 114 RecoverState(); 115 } 116 SetTraceSwitchFreezeOff()117 void SetTraceSwitchFreezeOff() 118 { 119 std::lock_guard<std::mutex> lock(traceMutex_); 120 uint8_t freeze = 1 << 1; 121 traceSwitchState_ = traceSwitchState_ & (~freeze); 122 RecoverState(); 123 } 124 SetTraceSwitchDevOn()125 void SetTraceSwitchDevOn() 126 { 127 std::lock_guard<std::mutex> lock(traceMutex_); 128 uint8_t dev = 1; 129 traceSwitchState_ = traceSwitchState_ | dev; 130 RecoverState(); 131 } 132 SetTraceSwitchDevOff()133 void SetTraceSwitchDevOff() 134 { 135 std::lock_guard<std::mutex> lock(traceMutex_); 136 uint8_t dev = 1; 137 traceSwitchState_ = traceSwitchState_ & (~dev); 138 RecoverState(); 139 } 140 SetCommandState(bool isCommandState)141 void SetCommandState(bool isCommandState) 142 { 143 isCommandState_ = isCommandState; 144 } 145 GetCommandState()146 bool GetCommandState() 147 { 148 return isCommandState_; 149 } 150 151 private: 152 TraceRet RecoverState(); 153 TraceRet InitCommonDropState(); 154 TraceRet InitCommonState(); 155 156 private: 157 std::shared_ptr<TraceBaseState> currentState_; 158 int32_t cacheSizeLimit_ = 800; // 800MB; 159 int32_t cacheSliceSpan_ = 10; // 10 seconds 160 uint8_t traceSwitchState_ = 0; 161 bool isCommandState_ = false; 162 bool isCachSwitchOn_ = false; 163 std::mutex traceMutex_; 164 }; 165 } 166 167 #endif // HIVIEWDFX_HIVIEW_TRACE_STATE_MACHINE_H 168