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 16 #ifndef FRAMEWORK_NATIVE_UNIFIED_COLLECTION_COLLECTOR_TRACE_STRATEGY_H 17 #define FRAMEWORK_NATIVE_UNIFIED_COLLECTION_COLLECTOR_TRACE_STRATEGY_H 18 #include <string> 19 #include <vector> 20 21 #include "trace_flow_controller.h" 22 #include "trace_handler.h" 23 24 namespace OHOS::HiviewDFX { 25 struct DumpEvent { 26 std::string caller; 27 int32_t errorCode = 0; 28 uint64_t ipcTime = 0; 29 uint64_t reqTime = 0; 30 int32_t reqDuration = 0; 31 uint64_t execTime = 0; 32 int32_t execDuration = 0; 33 int32_t coverDuration = 0; 34 int32_t coverRatio = 0; 35 std::vector<std::string> tags; 36 int64_t fileSize = 0; 37 int32_t sysMemTotal = 0; 38 int32_t sysMemFree = 0; 39 int32_t sysMemAvail = 0; 40 int32_t sysCpu = 0; 41 uint8_t traceMode = 0; 42 uint64_t startTime; 43 }; 44 45 struct StrategyParam { 46 uint32_t maxDuration = 0; 47 uint64_t happenTime = 0; 48 std::string caller; 49 std::string dbPath = FlowController::DEFAULT_DB_PATH; 50 std::string configPath = FlowController::DEFAULT_CONFIG_PATH; 51 }; 52 53 class TraceStrategy { 54 public: TraceStrategy(StrategyParam strategyParam,TraceScenario scenario,std::shared_ptr<TraceHandler> traceHandler)55 TraceStrategy(StrategyParam strategyParam, TraceScenario scenario, std::shared_ptr<TraceHandler> traceHandler) 56 : maxDuration_(strategyParam.maxDuration), 57 happenTime_(strategyParam.happenTime), 58 caller_(strategyParam.caller), 59 dbPath_(strategyParam.dbPath), 60 configPath_(strategyParam.configPath), 61 scenario_(scenario), 62 traceHandler_(traceHandler) {} 63 virtual ~TraceStrategy() = default; 64 virtual TraceRet DoDump(std::vector<std::string> &outputFiles, TraceRetInfo &traceRetInfo) = 0; 65 66 protected: 67 virtual TraceRet DumpTrace(DumpEvent &dumpEvent, TraceRetInfo &traceRetInfo) const; 68 69 protected: 70 uint32_t maxDuration_; 71 uint64_t happenTime_; 72 std::string caller_; 73 std::string dbPath_; 74 std::string configPath_; 75 TraceScenario scenario_; 76 std::shared_ptr<TraceHandler> traceHandler_; 77 std::shared_ptr<TraceFlowController> traceFlowController_; 78 }; 79 80 class TraceFlowControlStrategy : public TraceStrategy { 81 public: TraceFlowControlStrategy(StrategyParam strategyParam,TraceScenario scenario,std::shared_ptr<TraceHandler> traceHandler)82 TraceFlowControlStrategy(StrategyParam strategyParam, TraceScenario scenario, 83 std::shared_ptr<TraceHandler> traceHandler) 84 : TraceStrategy(strategyParam, scenario, traceHandler) 85 { 86 traceFlowController_ = std::make_shared<TraceFlowController>(caller_, dbPath_, configPath_); 87 } 88 89 TraceRet DoDump(std::vector<std::string> &outputFiles, TraceRetInfo &traceRetInfo) override; 90 }; 91 92 class TraceDevStrategy : public TraceStrategy { 93 public: TraceDevStrategy(StrategyParam strategyParam,TraceScenario scenario,std::shared_ptr<TraceHandler> traceHandler,std::shared_ptr<TraceZipHandler> zipHandler)94 TraceDevStrategy(StrategyParam strategyParam, TraceScenario scenario, std::shared_ptr<TraceHandler> traceHandler, 95 std::shared_ptr<TraceZipHandler> zipHandler) 96 : TraceStrategy(strategyParam, scenario, traceHandler), zipHandler_(zipHandler) 97 { 98 traceFlowController_ = std::make_shared<TraceFlowController>(caller_, dbPath_, configPath_); 99 } 100 TraceRet DoDump(std::vector<std::string> &outputFiles, TraceRetInfo &traceRetInfo) override; 101 102 private: 103 std::shared_ptr<TraceZipHandler> zipHandler_; 104 }; 105 106 /* 107 * TraceAsyncStrategy: dump trace asynchronously, no need to wait dump result of hitrace 108 * only Reliability adopts this strategy currently 109 */ 110 class TraceAsyncStrategy : public TraceStrategy, public std::enable_shared_from_this<TraceAsyncStrategy> { 111 public: TraceAsyncStrategy(StrategyParam strategyParam,TraceScenario scenario,std::shared_ptr<TraceHandler> traceHandler,std::shared_ptr<TraceZipHandler> zipHandler)112 TraceAsyncStrategy(StrategyParam strategyParam, TraceScenario scenario, std::shared_ptr<TraceHandler> traceHandler, 113 std::shared_ptr<TraceZipHandler> zipHandler) 114 : TraceStrategy(strategyParam, scenario, traceHandler), zipHandler_(zipHandler) 115 { 116 traceFlowController_ = std::make_shared<TraceFlowController>(caller_, dbPath_, configPath_); 117 } 118 119 TraceRet DoDump(std::vector<std::string> &outputFiles, TraceRetInfo &traceRetInfo) override; 120 121 protected: 122 TraceRet DumpTrace(DumpEvent &dumpEvent, TraceRetInfo &traceRetInfo) const override; 123 124 private: SetResultCopyFiles(std::vector<std::string> & outputFiles,const std::vector<std::string> & traceFiles)125 void SetResultCopyFiles(std::vector<std::string> &outputFiles, const std::vector<std::string>& traceFiles) const 126 { 127 if (traceHandler_ == nullptr) { 128 return; 129 } 130 outputFiles.clear(); 131 for (const auto &file : traceFiles) { 132 outputFiles.emplace_back(traceHandler_->GetTraceFinalPath(file, "")); 133 } 134 } 135 SetResultZipFiles(std::vector<std::string> & outputFiles,const std::vector<std::string> & traceFiles)136 void SetResultZipFiles(std::vector<std::string> &outputFiles, const std::vector<std::string>& traceFiles) const 137 { 138 if (zipHandler_ == nullptr) { 139 return; 140 } 141 outputFiles.clear(); 142 for (const auto &file : traceFiles) { 143 outputFiles.emplace_back(zipHandler_->GetTraceFinalPath(file, "")); 144 } 145 } 146 147 private: 148 std::shared_ptr<TraceZipHandler> zipHandler_; 149 }; 150 151 class TelemetryStrategy : public TraceStrategy, public std::enable_shared_from_this<TelemetryStrategy> { 152 public: TelemetryStrategy(StrategyParam strategyParam,std::shared_ptr<TraceHandler> traceHandler)153 TelemetryStrategy(StrategyParam strategyParam, std::shared_ptr<TraceHandler> traceHandler) 154 : TraceStrategy(strategyParam, TraceScenario::TRACE_TELEMETRY, traceHandler) 155 { 156 traceFlowController_ = std::make_shared<TraceFlowController>(BusinessName::TELEMETRY, dbPath_, configPath_); 157 } 158 159 TraceRet DoDump(std::vector<std::string> &outputFiles, TraceRetInfo &traceRetInfo) override; 160 }; 161 162 class TraceAppStrategy : public TraceStrategy { 163 public: 164 TraceAppStrategy(std::shared_ptr<AppCallerEvent> appCallerEvent, std::shared_ptr<TraceAppHandler> appHandler, 165 std::string dbPath = FlowController::DEFAULT_DB_PATH) TraceStrategy(StrategyParam{0, 0, ClientName::APP, dbPath},TraceScenario::TRACE_DYNAMIC,appHandler)166 : TraceStrategy(StrategyParam {0, 0, ClientName::APP, dbPath}, TraceScenario::TRACE_DYNAMIC, appHandler) 167 { 168 appCallerEvent_ = appCallerEvent; 169 traceFlowController_ = std::make_shared<TraceFlowController>(ClientName::APP, dbPath_, configPath_); 170 } 171 TraceRet DoDump(std::vector<std::string> &outputFiles, TraceRetInfo &traceRetInfo) override; 172 173 private: 174 void ShareAppEvent(std::shared_ptr<AppCallerEvent> appCallerEvent); 175 void ReportMainThreadJankForTrace(std::shared_ptr<AppCallerEvent> appCallerEvent); 176 void CleanOldAppTrace(); 177 178 private: 179 std::shared_ptr<AppCallerEvent> appCallerEvent_; 180 }; 181 } 182 #endif 183