• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.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);
50 
51 private:
52     DISALLOW_COPY_AND_MOVE(FlowController);
53     bool CreateRawDataReaders();
54     bool CreatePagedMemoryPool();
55     bool CreateRawDataBuffers();
56     bool CreateRawDataCaches();
57     void SetupTraceBufferSize(uint32_t sizeKb);
58     void SetupTransporterFlushParams(uint32_t intervalMs, uint32_t thresholdKb);
59     void GenerateRawDataFileNames(const std::string& prefix);
60     void SetupTraceReadPeriod(uint32_t periodMs);
61     void CaptureWorkOnNomalModeInner();
62     void HmCaptureWorkOnNomalModeInner();
63     void CaptureWorkOnNomalMode();
64     void CaptureWorkOnDelayMode();
65     long ReadEventData(int cpuid);
66     long HmReadEventData();
67     bool ParseEventDataOnNomalMode(int cpuid, long dataSize);
68     bool HmParseEventDataOnNomalMode(long dataSize);
69     bool ParseEventDataOnDelayMode();
70     bool ParseEventData(int cpuid, uint8_t* page);
71 
72     template <typename T, typename E>
73     bool HmParseEventData(T* tracePluginResult, uint8_t* &data, E* ftraceEvent);
74 
75     bool AddPlatformEventsToParser(void);
76     void EnableTraceEvents(void);
77     void DisableTraceEvents(void);
78     void DisableAllCategories(void);
79 
80     template <typename T> bool ReportClockTimes(T& tracePluginResult);
81 
82     template <typename T> bool ParseKernelSymbols(T& tracePluginResult);
83 
84     template <typename T> bool ParsePerCpuStatus(T& tracePluginResult, int stage);
85 
86     template <typename T, typename E>
87     bool ParseFtraceEvent(T* tracePluginResult, int cpuid, uint8_t page[], E* ftraceEvent);
88 
89     std::string ReloadTraceArgs();
90 
91     // for UT
SetTestInfo(int cpuNum,std::string path)92     void SetTestInfo(int cpuNum, std::string path)
93     {
94         platformCpuNum_ = cpuNum;
95         fakePath_ = path;
96     }
97 
98     using EventTypeName = std::pair<std::string, std::string>;
99     std::vector<EventTypeName> supportedEvents_ = {};
100     std::vector<EventTypeName> enabledEvents_ = {};
101 
102     std::unique_ptr<PagedMemPool> memPool_ = nullptr;
103     std::unique_ptr<KernelSymbolsParser> ksymsParser_ = nullptr;
104     std::unique_ptr<FtraceParser> ftraceParser_ = nullptr;
105     std::unique_ptr<ResultTransporter> tansporter_ = nullptr;
106     std::shared_ptr<OHOS::HiviewDFX::UCollectClient::TraceCollector> traceCollector_ = nullptr;
107     std::shared_ptr<FILE> rawDataFile_ = nullptr;
108     std::vector<std::unique_ptr<FtraceDataReader>> ftraceReaders_ = {};
109     std::vector<std::shared_ptr<uint8_t>> ftraceBuffers_;
110     std::atomic<bool> keepRunning_ = false;
111     std::thread pollThread_ = {};
112 
113     // for trace plugin config fields
114     std::vector<std::string> requestEvents_ = {};   // 1
115     std::vector<std::string> traceCategories_ = {}; // 2
116     std::vector<std::string> traceApps_ = {};       // 3
117     std::vector<std::string> rawDataDumpPath_ = {}; // 13
118     uint32_t tracePeriodMs_ = 0;                    // 10
119     uint32_t bufferSizeKb_ = 0;                     // 6
120     bool parseKsyms_ = false;                       // 7
121     TracePluginConfig_ParseMode parseMode_ = TracePluginConfig_ParseMode_NORMAL;
122 
123     WriterStructPtr resultWriter_ = nullptr;
124     int platformCpuNum_ = 0;
125     bool getClockTimes_ = true;
126 
127     bool ftraceSupported_ = false;
128     bool flushCacheData_ = false;
129     unsigned int hitraceTime_ = 0;
130     std::string traceClock_;
131     std::atomic<bool> isReportBasicData_ = false;
132     std::string fakePath_ = "";
133 };
134 FTRACE_NS_END
135 #endif // FLOW_CONTROLLER_H
136