• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     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 CaptureWork();
61     long ReadEventData(int cpuid);
62     bool ParseEventData(int cpuid, long dataSize);
63 
64     bool AddPlatformEventsToParser(void);
65     void EnableTraceEvents(void);
66     void DisableTraceEvents(void);
67     void DisableAllCategories(void);
68 
69     bool ReportClockTimes(void);
70     bool ParseKernelSymbols(void);
71     bool ParsePerCpuStatus(int stage);
72     bool ParseFtraceEvent(int cpuid, uint8_t page[]);
73 
74     static std::unique_ptr<TraceOps> GetTraceOps();
75 
76     using EventTypeName = std::pair<std::string, std::string>;
77     std::vector<EventTypeName> supportedEvents_ = {};
78     std::vector<EventTypeName> enabledEvents_ = {};
79 
80     std::unique_ptr<PagedMemPool> memPool_ = nullptr;
81     std::unique_ptr<KernelSymbolsParser> ksymsParser_ = nullptr;
82     std::unique_ptr<FtraceParser> ftraceParser_ = nullptr;
83     std::unique_ptr<ResultTransporter> tansporter_ = nullptr;
84     std::unique_ptr<TraceOps> traceOps_ = nullptr;
85     std::vector<std::unique_ptr<FtraceDataReader>> ftraceReaders_ = {};
86     std::vector<std::shared_ptr<uint8_t>> ftraceBuffers_;
87     std::vector<std::shared_ptr<FILE>> rawDataDumpFile_;
88     std::atomic<bool> keepRunning_ = false;
89     std::thread pollThread_ = {};
90 
91     // for trace plugin config fields
92     std::vector<std::string> requestEvents_ = {};   // 1
93     std::vector<std::string> traceCategories_ = {}; // 2
94     std::vector<std::string> traceApps_ = {};       // 3
95     std::vector<std::string> rawDataDumpPath_ = {}; // 13
96     uint32_t tracePeriodMs_ = 0;                    // 10
97     uint32_t bufferSizeKb_ = 0;                     // 6
98     bool parseKsyms_ = false;                       // 7
99 
100     WriterStructPtr resultWriter_ = nullptr;
101     int platformCpuNum_ = 0;
102     bool getClockTimes_ = true;
103 
104     bool ftraceSupported_ = false;
105     bool flushCacheData_ = false;
106     unsigned int hitraceTime_ = 0;
107 };
108 FTRACE_NS_END
109 #endif // FLOW_CONTROLLER_H
110