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 * 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_ops.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 49 private: 50 DISALLOW_COPY_AND_MOVE(FlowController); 51 bool CreateRawDataReaders(); 52 bool CreatePagedMemoryPool(); 53 bool CreateRawDataBuffers(); 54 bool CreateRawDataCaches(); 55 void SetupTraceBufferSize(uint32_t sizeKb); 56 void SetupTransporterFlushParams(uint32_t intervalMs, uint32_t thresholdKb); 57 void GenerateRawDataFileNames(const std::string& prefix); 58 void SetupTraceReadPeriod(uint32_t periodMs); 59 void CaptureWork(); 60 long ReadEventData(int cpuid); 61 bool ParseEventData(int cpuid, long dataSize); 62 63 bool AddPlatformEventsToParser(void); 64 void EnableTraceEvents(void); 65 void DisableTraceEvents(void); 66 67 bool ReportClockTimes(void); 68 bool ParseKernelSymbols(void); 69 bool ParsePerCpuStatus(int stage); 70 bool ParseFtraceEvent(int cpuid, uint8_t page[]); 71 72 static std::unique_ptr<TraceOps> GetTraceOps(); 73 74 using EventTypeName = std::pair<std::string, std::string>; 75 std::vector<EventTypeName> supportedEvents_ = {}; 76 std::vector<EventTypeName> enabledEvents_ = {}; 77 78 std::unique_ptr<PagedMemPool> memPool_ = nullptr; 79 std::unique_ptr<KernelSymbolsParser> ksymsParser_ = nullptr; 80 std::unique_ptr<FtraceParser> ftraceParser_ = nullptr; 81 std::unique_ptr<ResultTransporter> tansporter_ = nullptr; 82 std::unique_ptr<TraceOps> traceOps_ = nullptr; 83 std::vector<std::unique_ptr<FtraceDataReader>> ftraceReaders_ = {}; 84 std::vector<std::shared_ptr<uint8_t>> ftraceBuffers_; 85 std::vector<std::shared_ptr<FILE>> rawDataDumpFile_; 86 std::atomic<bool> keepRunning_ = false; 87 std::thread pollThread_ = {}; 88 89 // for trace plugin config fields 90 std::vector<std::string> requestEvents_ = {}; // 1 91 std::vector<std::string> traceCategories_ = {}; // 2 92 std::vector<std::string> traceApps_ = {}; // 3 93 std::vector<std::string> rawDataDumpPath_ = {}; // 13 94 uint32_t tracePeriodMs_ = 0; // 10 95 uint32_t bufferSizeKb_ = 0; // 6 96 bool parseKsyms_ = false; // 7 97 98 WriterStructPtr resultWriter_ = nullptr; 99 int platformCpuNum_ = 0; 100 bool getClockTimes_ = true; 101 102 bool ftraceSupported_ = false; 103 }; 104 FTRACE_NS_END 105 #endif // FLOW_CONTROLLER_H 106