• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 #ifndef SUBCOMMAND_RECORD_H
16 #define SUBCOMMAND_RECORD_H
17 
18 // some debug feaure
19 #define HIDEBUG_RECORD_NOT_PROCESS       0
20 #define HIDEBUG_RECORD_NOT_PROCESS_VM    0
21 #define HIDEBUG_RECORD_NOT_SAVE          0
22 #define HIDEBUG_SKIP_PROCESS_SYMBOLS     0
23 #define HIDEBUG_SKIP_MATCH_SYMBOLS       0
24 #define HIDEBUG_SKIP_LOAD_KERNEL_SYMBOLS 0
25 #define HIDEBUG_SKIP_SAVE_SYMBOLS        0
26 #define USE_COLLECT_SYMBOLIC             1
27 
28 #include <functional>
29 #include <thread>
30 #include <unordered_map>
31 #include <unordered_set>
32 #include <chrono>
33 #include "perf_event_record.h"
34 #include "perf_events.h"
35 #include "perf_file_writer.h"
36 #include "subcommand.h"
37 #include "virtual_runtime.h"
38 
39 namespace OHOS {
40 namespace Developtools {
41 namespace HiPerf {
42 class SubCommandRecord : public SubCommand {
43 public:
44     static constexpr int DEFAULT_CPU_PERCENT = 25;
45     static constexpr int MIN_CPU_PERCENT = 1;
46     static constexpr int MAX_CPU_PERCENT = 100;
47     static constexpr int MIN_SAMPLE_FREQUENCY = 1;
48     static constexpr int MAX_SAMPLE_FREQUENCY = 100000;
49     static constexpr int DEFAULT_MMAP_PAGES = 256;
50     static constexpr int MIN_PERF_MMAP_PAGE = 2;
51     static constexpr int MAX_PERF_MMAP_PAGE = 1024;
52     static constexpr int DEFAULT_CHECK_APP_MS = 10;
53     static constexpr int MIN_CHECK_APP_MS = 1;
54     static constexpr int MAX_CHECK_APP_MS = 200;
55     static constexpr float MIN_STOP_SECONDS = 0.100;
56     static constexpr float MAX_STOP_SECONDS = 10000.0;
57 
SubCommandRecord()58     SubCommandRecord()
59         // clang-format off
60         : SubCommand("record", "Collect performance sample information",
61         "Usage: hiperf record [options] [command [command-args]]\n"
62         "       Collect performance sampling information of running [command].\n"
63         "       The default options are: -c <all cpu> --cpu-limit 25 -d 10000.0 -e hw-cpu-cycles\n"
64         "       -f 4000 -m 1024 -o /data/local/tmp/perf.data.\n"
65         "   -a\n"
66         "         Collect system-wide information.\n"
67         "         for measures all processes/threads\n"
68         "         This requires CAP_PERFMON (since Linux 5.8) or CAP_SYS_ADMIN capability or a\n"
69         "         /proc/sys/kernel/perf_event_paranoid value of less than 1.\n"
70         "   --exclude-hiperf\n"
71         "         Don't record events issued by hiperf itself.\n"
72         "   -c <cpuid>[<,cpuid>]...\n"
73         "         cpuid should be 0,1,2...\n"
74         "         Limit the CPU that collects data.\n"
75         "         0 means cpu0, 1 means cpu1 ...\n"
76         "   --cpu-limit <percent>\n"
77         "         Set the max percent of cpu time used for recording.\n"
78         "         percent is in range [1-100], default is 25.\n"
79         "   -d <sec>\n"
80         "         stop in <sec> seconds. floating point number. seconds is in range [0.100-10000.0]\n"
81         "         default is 10000.0\n"
82         "   -f <freq>\n"
83         "         Set event sampling frequency. default is 4000 samples every second.\n"
84         "         check /proc/sys/kernel/perf_event_max_sample_rate for maximum allowed frequency\n"
85         "   --period <num>\n"
86         "         Set event sampling period for tracepoint events. recording one sample when <num> events happen.\n"
87         "         The default <num> is 1\n"
88         "   -e <event1[:<u|k>]>[,event1[:<u|k>]]...\n"
89         "         Customize the name of the event that needs to be sampled.\n"
90         "         The name can use the names listed in the list parameter.\n"
91         "         It can also be represented by the value of 0x<hex>.\n"
92         "           u - monitor user space events only\n"
93         "           k - monitor kernel space events only\n"
94         "   -g <event1[:<u|k>]>[,event1[:<u|k>]]...\n"
95         "         Put the events into a group, can set multiple groups by multiple -g\n"
96         "         PMU is required to report data in designated groups\n"
97         "         limited by HW capability, too many events cannot be reported in the same sampling)\n"
98         "   --no-inherit\n"
99         "         Don't trace child processes.\n"
100         "   -p <pid1>[,pid2]...\n"
101         "         Limit the process id of the collection target. Conflicts with the -a option.\n"
102         "   -t <tid1>[,tid2]...\n"
103         "         Limit the thread id of the collection target. Conflicts with the -a option.\n"
104         "   --exclude-tid <tid1>[,tid2]...\n"
105         "         Exclude threads of the collection target by thread ids. Conflicts with the -a option.\n"
106         "   --exclude-thread <tname1>[,tname2]...\n"
107         "         Exclude threads of the collection target by thread names. Conflicts with the -a option.\n"
108         "   --offcpu\n"
109         "         Trace when threads are scheduled off cpu.\n"
110         "   -j <branch_filter1>[,branch_filter2]...\n"
111         "         taken branch stack sampling, filter can be:\n"
112         "           any: any type of branch\n"
113         "           any_call: any function call or system call\n"
114         "           any_ret: any function return or system call return\n"
115         "           ind_call: any indirect branch\n"
116         "           call: direct calls, including far (to/from kernel) calls\n"
117         "           u: only when the branch target is at the user level\n"
118         "           k: only when the branch target is in the kernel\n"
119         "         requires at least one of any, any_call, any_ret, ind_call\n"
120         "   -s / --call-stack <fp|dwarf[,size]>\n"
121         "         Setup and enable call stack (stack chain/backtrace) recording, Default is 'fp'.\n"
122         "           the value can be:\n"
123         "             fp: frame pointer\n"
124         "             dwarf: DWARF's CFI - Call Frame Information\n"
125         "               'dwarf,size' set sample stack size, size should be in 8~65528 and 8 byte aligned. \n"
126         "           as the method to collect the information used to show the call stacks.\n"
127         "   --delay-unwind\n"
128         "         If '-s dwarf' used, stack will be unwind while recording, use this option to switch\n"
129         "         to unwind after recording.\n"
130         "   --disable-unwind\n"
131         "         If '-s dwarf' is used, stack will be unwind while recording by default\n"
132         "         use this option to disable unwinding.\n"
133         "   --disable-callstack-expand\n"
134         "         If '-s dwarf' is used, to break the 64k stack limit, callstack is merged by default\n"
135         "         to build more complete call stack. that may not be correct sometimes.\n"
136         "   --clockid <clock_id>\n"
137         "         Set the clock id to use for the various time fields in the perf_event_type records.\n"
138         "         monotonic and monotonic_raw are supported,\n"
139         "         some events might also allow boottime, realtime and clock_tai.\n"
140         "   --symbol-dir <dir>\n"
141         "         Set directory to look for symbol files, used for unwinding. \n"
142         "   -m <mmap_pages>\n"
143         "         Number of the mmap pages, used to receiving record data from kernel,\n"
144         "         must be a power of two, rang[2,1024], default is 1024.\n"
145         "   --app <package_name>\n"
146         "         Collect profile info for an OHOS app, the app must be debuggable.\n"
147         "         Record will exit if the process is not started within 10 seconds.\n"
148         "   --chkms <millisec>\n"
149         "         Set the interval of querying the <package_name>.\n"
150         "         <millisec> is in range [1-200], default is 10.\n"
151         "   --data-limit <SIZE[K|M|G]>\n"
152         "         Stop recording after SIZE bytes of records. Default is unlimited.\n"
153         "   -o <output_file_name>\n"
154         "         Set output file name, default is /data/local/tmp/perf.data.\n"
155         "   -z\n"
156         "         Compress record data.\n"
157         "   --restart\n"
158         "         Collect performance counter information of application startup.\n"
159         "         Record will exit if the process is not started within 30 seconds.\n"
160         "   --verbose\n"
161         "         Show more detailed reports.\n"
162         "   --control <command>\n"
163         "         Control sampling by <command>, the <command> can be:\n"
164         "           prepare: set arguments and prepare sampling\n"
165         "           start: start sampling\n"
166         "           pause: pause sampling\n"
167         "           resume: resume sampling\n"
168         "           stop: stop sampling\n"
169         )
170     // clang-format on
171     {
172     }
173 
174     ~SubCommandRecord();
175     bool OnSubCommand(std::vector<std::string> &args) override;
176     bool ParseOption(std::vector<std::string> &args) override;
177     void DumpOptions(void) const override;
178 
179     static bool RegisterSubCommandRecord(void);
180 
181 private:
182     PerfEvents perfEvents_;
183 
184     bool targetSystemWide_ = false;
185     bool compressData_ = false;
186     bool noInherit_ = false;
187     bool excludeHiperf_ = false;
188     bool offCPU_ = false;
189     bool delayUnwind_ = false;
190     bool disableUnwind_ = false;
191     bool disableCallstackExpend_ = false;
192     bool verboseReport_ = false;
193     float timeStopSec_ = PerfEvents::DEFAULT_TIMEOUT;
194     int frequency_ = 0;
195     int period_ = 0;
196     int cpuPercent_ = DEFAULT_CPU_PERCENT;
197     int mmapPages_ = MAX_PERF_MMAP_PAGE;
198     std::vector<std::string> symbolDir_ = {};
199     std::string outputFilename_ = "/data/local/tmp/perf.data";
200     std::string appPackage_ = {};
201     int checkAppMs_ = DEFAULT_CHECK_APP_MS;
202     std::string clockId_ = {};
203     std::string strLimit_ = {};
204     std::vector<pid_t> selectCpus_ = {};
205     std::vector<pid_t> selectPids_ = {};
206     std::vector<pid_t> selectTids_ = {};
207     std::vector<pid_t> excludeTids_ = {};
208     bool restart_ = false;
209     std::vector<std::string> selectEvents_ = {};
210     std::vector<std::vector<std::string>> selectGroups_ = {};
211     std::vector<std::string> callStackType_ = {};
212     std::vector<std::string> vecBranchFilters_ = {};
213     std::vector<std::string> trackedCommand_ = {};
214     std::vector<std::string> excludeThreadNames_ = {};
215 
216     bool GetOptions(std::vector<std::string> &args);
217     bool CheckOptions();
218     bool CheckDataLimitOption();
219     bool CheckSelectCpuPidOption();
220     bool GetOptionFrequencyAndPeriod(std::vector<std::string> &args);
221 
222     bool isCallStackDwarf_ = false;
223     bool isCallStackFp_ = false;
224     uint32_t callStackDwarfSize_ = MAX_SAMPLE_STACK_SIZE;
225     uint64_t branchSampleType_ = 0;
226     uint64_t dataSizeLimit_ = 0;
227     bool isDataSizeLimitStop_ = false;
228 
229     std::unique_ptr<PerfFileWriter> fileWriter_ = nullptr;
230 
231     // for client
232     int clientPipeInput_ = -1;
233     int clientPipeOutput_ = -1;
234     int nullFd_ = -1;
235     std::thread clientCommandHanle_;
236     bool clientExit_ = false;
237     void ClientCommandHandle();
238     bool ClientCommandResponse(bool OK);
239     bool IsSamplingRunning();
240     // for cmdline client
241     std::string controlCmd_ = {};
242     bool isFifoServer_ = false;
243     bool isFifoClient_ = false;
244     bool ProcessControl();
245     bool CreateFifoServer();
246     bool SendFifoAndWaitReply(const std::string &cmd, const std::chrono::milliseconds &timeOut);
247     bool WaitFifoReply(int fd, const std::chrono::milliseconds &timeOut);
248     void CloseClientThread();
249 
250     bool PreparePerfEvent();
251     bool PrepareSysKernel();
252     bool PrepareVirtualRuntime();
253 
254     size_t recordSamples_ = 0;
255     size_t recordNoSamples_ = 0;
256 
257     bool isNeedSetPerfHarden_ = false;
258 
259     // callback to process record
260     bool ProcessRecord(std::unique_ptr<PerfEventRecord>);
261     bool SaveRecord(std::unique_ptr<PerfEventRecord>);
262 
263     // file format like as 0,1-3,4-6,7,8
264     uint32_t GetCountFromFile(const std::string &fileName);
265     std::string GetCpuDescFromFile();
266     bool AddCpuFeature();
267     void AddMemTotalFeature();
268     void AddEventDescFeature();
269     void AddRecordTimeFeature();
270     void AddWorkloadCmdFeature();
271     void AddCommandLineFeature();
272     void AddCpuOffFeature();
273     bool AddFeatureRecordFile();
274 
275     bool CreateInitRecordFile(bool compressData = false);
276     bool FinishWriteRecordFile();
277     bool PostProcessRecordFile();
278     bool RecordCompleted();
279 #ifdef HIPERF_DEBUG_TIME
280     void ReportTime();
281 #endif
282 
283     bool CollectionSymbol(std::unique_ptr<PerfEventRecord> record);
284 
285     bool SetPerfLimit(const std::string& file, int value, std::function<bool (int, int)> const& cmd,
286         const std::string& param);
287     bool SetPerfCpuMaxPercent();
288     bool SetPerfMaxSampleRate();
289     bool SetPerfEventMlock();
290     bool SetPerfHarden();
291 
292     bool TraceOffCpu();
293     bool ParseCallStackOption(const std::vector<std::string> &callStackType);
294     bool ParseDataLimitOption(const std::string &str);
295     bool ParseBranchSampleType(const std::vector<std::string> &vecBranchSampleTypes);
296     bool ParseControlCmd(const std::string cmd);
297     bool CheckTargetProcessOptions();
298     bool CheckTargetPids();
299 
300     VirtualRuntime virtualRuntime_;
301 #if USE_COLLECT_SYMBOLIC
302     std::unordered_set<uint64_t> kernelSymbolsHits_;
303     std::unordered_map<pid_t, std::unordered_set<uint64_t>> userSymbolsHits_;
304     void SymbolicHits();
305 #endif
306 
307 #ifdef HIPERF_DEBUG_TIME
308     std::chrono::microseconds prcessRecordTimes_ = std::chrono::microseconds::zero();
309     std::chrono::microseconds saveRecordTimes_ = std::chrono::microseconds::zero();
310     std::chrono::microseconds saveFeatureTimes_ = std::chrono::microseconds::zero();
311 #endif
312     std::chrono::time_point<std::chrono::steady_clock> startSaveFileTimes_;
313 };
314 } // namespace HiPerf
315 } // namespace Developtools
316 } // namespace OHOS
317 #endif // SUBCOMMAND_RECORD_H
318