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