1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2023. All rights reserved. 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 * Description: FlowController define 16 */ 17 #ifndef FLOW_CONTROLLER_H 18 #define FLOW_CONTROLLER_H 19 #include "ftrace_data_reader.h" 20 #include "ftrace_namespace.h" 21 #include "ftrace_parser.h" 22 #include "kernel_symbols_parser.h" 23 #include "paged_mem_pool.h" 24 #include "plugin_module_api.h" 25 #include "result_transporter.h" 26 #include "trace_collector_client.h" 27 #include "trace_plugin_config.pb.h" 28 29 #include <atomic> 30 #include <cstdint> 31 #include <iostream> 32 #include <mutex> 33 #include <thread> 34 35 using WriterStructPtr = std::unique_ptr<WriterStruct>::pointer; 36 37 FTRACE_NS_BEGIN 38 class FlowController { 39 public: 40 FlowController(void); 41 ~FlowController(void); 42 43 int SetWriter(const WriterStructPtr& writer); 44 int LoadConfig(const uint8_t configData[], uint32_t size); 45 46 int StartCapture(void); 47 int StopCapture(void); 48 void SetReportBasicData(bool isReportBasicData); 49 bool ParseBasicData(void); IsDataReady()50 bool IsDataReady() 51 { 52 return dataReady_.load(); 53 } 54 55 private: 56 DISALLOW_COPY_AND_MOVE(FlowController); 57 bool CreateRawDataReaders(); 58 bool CreatePagedMemoryPool(); 59 bool CreateRawDataBuffers(); 60 bool CreateRawDataCaches(); 61 void SetupTraceBufferSize(uint32_t sizeKb); 62 void SetupTransporterFlushParams(uint32_t intervalMs, uint32_t thresholdKb); 63 void GenerateRawDataFileNames(const std::string& prefix); 64 void SetupTraceReadPeriod(uint32_t periodMs); 65 void CaptureWorkOnNomalModeInner(); 66 void HmCaptureWorkOnNomalModeInner(); 67 void CaptureWorkOnNomalMode(); 68 void CaptureWorkOnDelayMode(); 69 long ReadEventData(int cpuid); 70 long HmReadEventData(); 71 bool ParseEventDataOnNomalMode(int cpuid, long dataSize); 72 bool HmParseEventDataOnNomalMode(long dataSize); 73 bool ParseEventDataOnDelayMode(); 74 bool ParseEventData(int cpuid, uint8_t* page); 75 76 template <typename T, typename E> 77 bool HmParseEventData(T* tracePluginResult, uint8_t* &data, E* ftraceEvent); 78 79 bool AddPlatformEventsToParser(void); 80 void EnableTraceEvents(void); 81 void DisableTraceEvents(void); 82 void DisableAllCategories(void); 83 std::string GetCmdArgs(const TracePluginConfig& traceConfig); 84 85 template <typename T> bool ReportClockTimes(T& tracePluginResult); 86 87 template <typename T> bool ParseKernelSymbols(T& tracePluginResult); 88 89 template <typename T> bool ParsePerCpuStatus(T& tracePluginResult, int stage); 90 91 template <typename T, typename E> 92 bool ParseFtraceEvent(T* tracePluginResult, int cpuid, uint8_t page[], E* ftraceEvent); 93 94 std::string ReloadTraceArgs(); 95 96 // for UT SetTestInfo(int cpuNum,std::string path)97 void SetTestInfo(int cpuNum, std::string path) 98 { 99 platformCpuNum_ = cpuNum; 100 fakePath_ = path; 101 } 102 103 using EventTypeName = std::pair<std::string, std::string>; 104 std::vector<EventTypeName> supportedEvents_ = {}; 105 std::vector<EventTypeName> enabledEvents_ = {}; 106 107 std::unique_ptr<PagedMemPool> memPool_ = nullptr; 108 std::unique_ptr<KernelSymbolsParser> ksymsParser_ = nullptr; 109 std::unique_ptr<FtraceParser> ftraceParser_ = nullptr; 110 std::unique_ptr<ResultTransporter> tansporter_ = nullptr; 111 std::shared_ptr<OHOS::HiviewDFX::UCollectClient::TraceCollector> traceCollector_ = nullptr; 112 std::shared_ptr<FILE> rawDataFile_ = nullptr; 113 std::vector<std::unique_ptr<FtraceDataReader>> ftraceReaders_ = {}; 114 std::vector<std::shared_ptr<uint8_t>> ftraceBuffers_; 115 std::atomic<bool> keepRunning_ = false; 116 std::thread pollThread_ = {}; 117 std::atomic<bool> dataReady_ = false; 118 // for trace plugin config fields 119 std::vector<std::string> requestEvents_ = {}; // 1 120 std::vector<std::string> traceCategories_ = {}; // 2 121 std::vector<std::string> traceApps_ = {}; // 3 122 std::vector<std::string> rawDataDumpPath_ = {}; // 13 123 uint32_t tracePeriodMs_ = 0; // 10 124 uint32_t bufferSizeKb_ = 0; // 6 125 bool parseKsyms_ = false; // 7 126 TracePluginConfig_ParseMode parseMode_ = TracePluginConfig_ParseMode_NORMAL; 127 128 WriterStructPtr resultWriter_ = nullptr; 129 int platformCpuNum_ = 0; 130 bool getClockTimes_ = true; 131 132 bool ftraceSupported_ = false; 133 bool flushCacheData_ = false; 134 unsigned int hitraceTime_ = 0; 135 std::string traceClock_; 136 std::atomic<bool> isReportBasicData_ = false; 137 std::string fakePath_ = ""; 138 std::string osVersion_ = ""; 139 }; 140 FTRACE_NS_END 141 #endif // FLOW_CONTROLLER_H 142