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