OSDN Git Service

Merge "boot_control_copy should #include <sys/sysmacros.h>."
[android-x86/system-extras.git] / simpleperf / cmd_record_test.cpp
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <gtest/gtest.h>
18
19 #include <unistd.h>
20
21 #include <android-base/file.h>
22 #include <android-base/stringprintf.h>
23 #include <android-base/test_utils.h>
24
25 #include <map>
26 #include <memory>
27 #include <thread>
28
29 #include "command.h"
30 #include "environment.h"
31 #include "event_selection_set.h"
32 #include "get_test_data.h"
33 #include "record.h"
34 #include "record_file.h"
35 #include "test_util.h"
36 #include "thread_tree.h"
37
38 using namespace PerfFileFormat;
39
40 static std::unique_ptr<Command> RecordCmd() {
41   return CreateCommandInstance("record");
42 }
43
44 static bool RunRecordCmd(std::vector<std::string> v,
45                          const char* output_file = nullptr) {
46   std::unique_ptr<TemporaryFile> tmpfile;
47   std::string out_file;
48   if (output_file != nullptr) {
49     out_file = output_file;
50   } else {
51     tmpfile.reset(new TemporaryFile);
52     out_file = tmpfile->path;
53   }
54   v.insert(v.end(), {"-o", out_file, "sleep", SLEEP_SEC});
55   return RecordCmd()->Run(v);
56 }
57
58 TEST(record_cmd, no_options) { ASSERT_TRUE(RunRecordCmd({})); }
59
60 TEST(record_cmd, system_wide_option) {
61   TEST_IN_ROOT(ASSERT_TRUE(RunRecordCmd({"-a"})));
62 }
63
64 TEST(record_cmd, sample_period_option) {
65   ASSERT_TRUE(RunRecordCmd({"-c", "100000"}));
66 }
67
68 TEST(record_cmd, event_option) {
69   ASSERT_TRUE(RunRecordCmd({"-e", "cpu-clock"}));
70 }
71
72 TEST(record_cmd, freq_option) {
73   ASSERT_TRUE(RunRecordCmd({"-f", "99"}));
74   ASSERT_TRUE(RunRecordCmd({"-F", "99"}));
75 }
76
77 TEST(record_cmd, output_file_option) {
78   TemporaryFile tmpfile;
79   ASSERT_TRUE(RecordCmd()->Run({"-o", tmpfile.path, "sleep", SLEEP_SEC}));
80 }
81
82 TEST(record_cmd, dump_kernel_mmap) {
83   TemporaryFile tmpfile;
84   ASSERT_TRUE(RunRecordCmd({}, tmpfile.path));
85   std::unique_ptr<RecordFileReader> reader =
86       RecordFileReader::CreateInstance(tmpfile.path);
87   ASSERT_TRUE(reader != nullptr);
88   std::vector<std::unique_ptr<Record>> records = reader->DataSection();
89   ASSERT_GT(records.size(), 0U);
90   bool have_kernel_mmap = false;
91   for (auto& record : records) {
92     if (record->type() == PERF_RECORD_MMAP) {
93       const MmapRecord* mmap_record =
94           static_cast<const MmapRecord*>(record.get());
95       if (strcmp(mmap_record->filename, DEFAULT_KERNEL_MMAP_NAME) == 0 ||
96           strcmp(mmap_record->filename, DEFAULT_KERNEL_MMAP_NAME_PERF) == 0) {
97         have_kernel_mmap = true;
98         break;
99       }
100     }
101   }
102   ASSERT_TRUE(have_kernel_mmap);
103 }
104
105 TEST(record_cmd, dump_build_id_feature) {
106   TemporaryFile tmpfile;
107   ASSERT_TRUE(RunRecordCmd({}, tmpfile.path));
108   std::unique_ptr<RecordFileReader> reader =
109       RecordFileReader::CreateInstance(tmpfile.path);
110   ASSERT_TRUE(reader != nullptr);
111   const FileHeader& file_header = reader->FileHeader();
112   ASSERT_TRUE(file_header.features[FEAT_BUILD_ID / 8] &
113               (1 << (FEAT_BUILD_ID % 8)));
114   ASSERT_GT(reader->FeatureSectionDescriptors().size(), 0u);
115 }
116
117 TEST(record_cmd, tracepoint_event) {
118   TEST_IN_ROOT(ASSERT_TRUE(RunRecordCmd({"-a", "-e", "sched:sched_switch"})));
119 }
120
121 TEST(record_cmd, branch_sampling) {
122   if (IsBranchSamplingSupported()) {
123     ASSERT_TRUE(RunRecordCmd({"-b"}));
124     ASSERT_TRUE(RunRecordCmd({"-j", "any,any_call,any_ret,ind_call"}));
125     ASSERT_TRUE(RunRecordCmd({"-j", "any,k"}));
126     ASSERT_TRUE(RunRecordCmd({"-j", "any,u"}));
127     ASSERT_FALSE(RunRecordCmd({"-j", "u"}));
128   } else {
129     GTEST_LOG_(INFO) << "This test does nothing as branch stack sampling is "
130                         "not supported on this device.";
131   }
132 }
133
134 TEST(record_cmd, event_modifier) {
135   ASSERT_TRUE(RunRecordCmd({"-e", "cpu-cycles:u"}));
136 }
137
138 TEST(record_cmd, fp_callchain_sampling) {
139   ASSERT_TRUE(RunRecordCmd({"--call-graph", "fp"}));
140 }
141
142 TEST(record_cmd, fp_callchain_sampling_warning_on_arm) {
143   if (GetBuildArch() != ARCH_ARM) {
144     GTEST_LOG_(INFO) << "This test does nothing as it only tests on arm arch.";
145     return;
146   }
147   ASSERT_EXIT(
148       {
149         exit(RunRecordCmd({"--call-graph", "fp"}) ? 0 : 1);
150       },
151       testing::ExitedWithCode(0), "doesn't work well on arm");
152 }
153
154 TEST(record_cmd, system_wide_fp_callchain_sampling) {
155   TEST_IN_ROOT(ASSERT_TRUE(RunRecordCmd({"-a", "--call-graph", "fp"})));
156 }
157
158 TEST(record_cmd, dwarf_callchain_sampling) {
159   if (IsDwarfCallChainSamplingSupported()) {
160     std::vector<std::unique_ptr<Workload>> workloads;
161     CreateProcesses(1, &workloads);
162     std::string pid = std::to_string(workloads[0]->GetPid());
163     ASSERT_TRUE(RunRecordCmd({"-p", pid, "--call-graph", "dwarf"}));
164     ASSERT_TRUE(RunRecordCmd({"-p", pid, "--call-graph", "dwarf,16384"}));
165     ASSERT_FALSE(RunRecordCmd({"-p", pid, "--call-graph", "dwarf,65536"}));
166     ASSERT_TRUE(RunRecordCmd({"-p", pid, "-g"}));
167   } else {
168     GTEST_LOG_(INFO) << "This test does nothing as dwarf callchain sampling is "
169                         "not supported on this device.";
170   }
171 }
172
173 TEST(record_cmd, system_wide_dwarf_callchain_sampling) {
174   if (IsDwarfCallChainSamplingSupported()) {
175     TEST_IN_ROOT(RunRecordCmd({"-a", "--call-graph", "dwarf"}));
176   } else {
177     GTEST_LOG_(INFO) << "This test does nothing as dwarf callchain sampling is "
178                         "not supported on this device.";
179   }
180 }
181
182 TEST(record_cmd, no_unwind_option) {
183   if (IsDwarfCallChainSamplingSupported()) {
184     ASSERT_TRUE(RunRecordCmd({"--call-graph", "dwarf", "--no-unwind"}));
185   } else {
186     GTEST_LOG_(INFO) << "This test does nothing as dwarf callchain sampling is "
187                         "not supported on this device.";
188   }
189   ASSERT_FALSE(RunRecordCmd({"--no-unwind"}));
190 }
191
192 TEST(record_cmd, post_unwind_option) {
193   if (IsDwarfCallChainSamplingSupported()) {
194     std::vector<std::unique_ptr<Workload>> workloads;
195     CreateProcesses(1, &workloads);
196     std::string pid = std::to_string(workloads[0]->GetPid());
197     ASSERT_TRUE(RunRecordCmd({"-p", pid, "--call-graph", "dwarf", "--post-unwind"}));
198   } else {
199     GTEST_LOG_(INFO) << "This test does nothing as dwarf callchain sampling is "
200                         "not supported on this device.";
201   }
202   ASSERT_FALSE(RunRecordCmd({"--post-unwind"}));
203   ASSERT_FALSE(
204       RunRecordCmd({"--call-graph", "dwarf", "--no-unwind", "--post-unwind"}));
205 }
206
207 TEST(record_cmd, existing_processes) {
208   std::vector<std::unique_ptr<Workload>> workloads;
209   CreateProcesses(2, &workloads);
210   std::string pid_list = android::base::StringPrintf(
211       "%d,%d", workloads[0]->GetPid(), workloads[1]->GetPid());
212   ASSERT_TRUE(RunRecordCmd({"-p", pid_list}));
213 }
214
215 TEST(record_cmd, existing_threads) {
216   std::vector<std::unique_ptr<Workload>> workloads;
217   CreateProcesses(2, &workloads);
218   // Process id can also be used as thread id in linux.
219   std::string tid_list = android::base::StringPrintf(
220       "%d,%d", workloads[0]->GetPid(), workloads[1]->GetPid());
221   ASSERT_TRUE(RunRecordCmd({"-t", tid_list}));
222 }
223
224 TEST(record_cmd, no_monitored_threads) { ASSERT_FALSE(RecordCmd()->Run({""})); }
225
226 TEST(record_cmd, more_than_one_event_types) {
227   ASSERT_TRUE(RunRecordCmd({"-e", "cpu-cycles,cpu-clock"}));
228   ASSERT_TRUE(RunRecordCmd({"-e", "cpu-cycles", "-e", "cpu-clock"}));
229 }
230
231 TEST(record_cmd, mmap_page_option) {
232   ASSERT_TRUE(RunRecordCmd({"-m", "1"}));
233   ASSERT_FALSE(RunRecordCmd({"-m", "0"}));
234   ASSERT_FALSE(RunRecordCmd({"-m", "7"}));
235 }
236
237 static void CheckKernelSymbol(const std::string& path, bool need_kallsyms,
238                               bool* success) {
239   *success = false;
240   std::unique_ptr<RecordFileReader> reader =
241       RecordFileReader::CreateInstance(path);
242   ASSERT_TRUE(reader != nullptr);
243   std::vector<std::unique_ptr<Record>> records = reader->DataSection();
244   bool has_kernel_symbol_records = false;
245   for (const auto& record : records) {
246     if (record->type() == SIMPLE_PERF_RECORD_KERNEL_SYMBOL) {
247       has_kernel_symbol_records = true;
248     }
249   }
250   bool require_kallsyms = need_kallsyms && CheckKernelSymbolAddresses();
251   ASSERT_EQ(require_kallsyms, has_kernel_symbol_records);
252   *success = true;
253 }
254
255 TEST(record_cmd, kernel_symbol) {
256   TemporaryFile tmpfile;
257   ASSERT_TRUE(RunRecordCmd({"--no-dump-symbols"}, tmpfile.path));
258   bool success;
259   CheckKernelSymbol(tmpfile.path, true, &success);
260   ASSERT_TRUE(success);
261   ASSERT_TRUE(RunRecordCmd({"--no-dump-symbols", "--no-dump-kernel-symbols"}, tmpfile.path));
262   CheckKernelSymbol(tmpfile.path, false, &success);
263   ASSERT_TRUE(success);
264 }
265
266 // Check if the dso/symbol records in perf.data matches our expectation.
267 static void CheckDsoSymbolRecords(const std::string& path,
268                                   bool can_have_dso_symbol_records,
269                                   bool* success) {
270   *success = false;
271   std::unique_ptr<RecordFileReader> reader =
272       RecordFileReader::CreateInstance(path);
273   ASSERT_TRUE(reader != nullptr);
274   std::vector<std::unique_ptr<Record>> records = reader->DataSection();
275   bool has_dso_record = false;
276   bool has_symbol_record = false;
277   std::map<uint64_t, bool> dso_hit_map;
278   for (const auto& record : records) {
279     if (record->type() == SIMPLE_PERF_RECORD_DSO) {
280       has_dso_record = true;
281       uint64_t dso_id = static_cast<const DsoRecord*>(record.get())->dso_id;
282       ASSERT_EQ(dso_hit_map.end(), dso_hit_map.find(dso_id));
283       dso_hit_map.insert(std::make_pair(dso_id, false));
284     } else if (record->type() == SIMPLE_PERF_RECORD_SYMBOL) {
285       has_symbol_record = true;
286       uint64_t dso_id = static_cast<const SymbolRecord*>(record.get())->dso_id;
287       auto it = dso_hit_map.find(dso_id);
288       ASSERT_NE(dso_hit_map.end(), it);
289       it->second = true;
290     }
291   }
292   if (can_have_dso_symbol_records) {
293     // It is possible that there are no samples hitting functions having symbol.
294     // In that case, there are no dso/symbol records.
295     ASSERT_EQ(has_dso_record, has_symbol_record);
296     for (auto& pair : dso_hit_map) {
297       ASSERT_TRUE(pair.second);
298     }
299   } else {
300     ASSERT_FALSE(has_dso_record);
301     ASSERT_FALSE(has_symbol_record);
302   }
303   *success = true;
304 }
305
306 TEST(record_cmd, no_dump_symbols) {
307   TemporaryFile tmpfile;
308   ASSERT_TRUE(RunRecordCmd({}, tmpfile.path));
309   bool success;
310   CheckDsoSymbolRecords(tmpfile.path, true, &success);
311   ASSERT_TRUE(success);
312   ASSERT_TRUE(RunRecordCmd({"--no-dump-symbols"}, tmpfile.path));
313   CheckDsoSymbolRecords(tmpfile.path, false, &success);
314   ASSERT_TRUE(success);
315   if (IsDwarfCallChainSamplingSupported()) {
316     std::vector<std::unique_ptr<Workload>> workloads;
317     CreateProcesses(1, &workloads);
318     std::string pid = std::to_string(workloads[0]->GetPid());
319     ASSERT_TRUE(RunRecordCmd({"-p", pid, "-g"}, tmpfile.path));
320     bool success;
321     CheckDsoSymbolRecords(tmpfile.path, true, &success);
322     ASSERT_TRUE(success);
323     ASSERT_TRUE(RunRecordCmd({"-p", pid, "-g", "--no-dump-symbols"}, tmpfile.path));
324     CheckDsoSymbolRecords(tmpfile.path, false, &success);
325     ASSERT_TRUE(success);
326   }
327 }
328
329 TEST(record_cmd, dump_kernel_symbols) {
330   if (!IsRoot()) {
331     GTEST_LOG_(INFO) << "Test requires root privilege";
332     return;
333   }
334   system("echo 0 >/proc/sys/kernel/kptr_restrict");
335   TemporaryFile tmpfile;
336   ASSERT_TRUE(RunRecordCmd({"-a", "-o", tmpfile.path, "sleep", "1"}));
337   std::unique_ptr<RecordFileReader> reader = RecordFileReader::CreateInstance(tmpfile.path);
338   ASSERT_TRUE(reader != nullptr);
339   std::map<int, SectionDesc> section_map = reader->FeatureSectionDescriptors();
340   ASSERT_NE(section_map.find(FEAT_FILE), section_map.end());
341   std::string file_path;
342   uint32_t file_type;
343   uint64_t min_vaddr;
344   std::vector<Symbol> symbols;
345   size_t read_pos = 0;
346   bool has_kernel_symbols = false;
347   while (reader->ReadFileFeature(read_pos, &file_path, &file_type, &min_vaddr, &symbols)) {
348     if (file_type == DSO_KERNEL && !symbols.empty()) {
349       has_kernel_symbols = true;
350     }
351   }
352   ASSERT_TRUE(has_kernel_symbols);
353 }
354
355 TEST(record_cmd, group_option) {
356   ASSERT_TRUE(RunRecordCmd({"--group", "cpu-cycles,cpu-clock", "-m", "16"}));
357   ASSERT_TRUE(RunRecordCmd({"--group", "cpu-cycles,cpu-clock", "--group",
358                             "cpu-cycles:u,cpu-clock:u", "--group",
359                             "cpu-cycles:k,cpu-clock:k", "-m", "16"}));
360 }
361
362 TEST(record_cmd, symfs_option) { ASSERT_TRUE(RunRecordCmd({"--symfs", "/"})); }
363
364 TEST(record_cmd, duration_option) {
365   TemporaryFile tmpfile;
366   ASSERT_TRUE(RecordCmd()->Run({"--duration", "1.2", "-p",
367                                 std::to_string(getpid()), "-o", tmpfile.path}));
368   ASSERT_TRUE(
369       RecordCmd()->Run({"--duration", "1", "-o", tmpfile.path, "sleep", "2"}));
370 }
371
372 TEST(record_cmd, support_modifier_for_clock_events) {
373   for (const std::string& e : {"cpu-clock", "task-clock"}) {
374     for (const std::string& m : {"u", "k"}) {
375       ASSERT_TRUE(RunRecordCmd({"-e", e + ":" + m})) << "event " << e << ":"
376                                                      << m;
377     }
378   }
379 }
380
381 TEST(record_cmd, handle_SIGHUP) {
382   TemporaryFile tmpfile;
383   std::thread thread([]() {
384     sleep(1);
385     kill(getpid(), SIGHUP);
386   });
387   thread.detach();
388   ASSERT_TRUE(RecordCmd()->Run({"-o", tmpfile.path, "sleep", "1000000"}));
389 }
390
391 TEST(record_cmd, stop_when_no_more_targets) {
392   TemporaryFile tmpfile;
393   std::atomic<int> tid(0);
394   std::thread thread([&]() {
395     tid = gettid();
396     sleep(1);
397   });
398   thread.detach();
399   while (tid == 0);
400   ASSERT_TRUE(RecordCmd()->Run({"-o", tmpfile.path, "-t", std::to_string(tid)}));
401 }
402
403 TEST(record_cmd, donot_stop_when_having_targets) {
404   std::vector<std::unique_ptr<Workload>> workloads;
405   CreateProcesses(1, &workloads);
406   std::string pid = std::to_string(workloads[0]->GetPid());
407   uint64_t start_time_in_ns = GetSystemClock();
408   TemporaryFile tmpfile;
409   ASSERT_TRUE(RecordCmd()->Run({"-o", tmpfile.path, "-p", pid, "--duration", "3"}));
410   uint64_t end_time_in_ns = GetSystemClock();
411   ASSERT_GT(end_time_in_ns - start_time_in_ns, static_cast<uint64_t>(2e9));
412 }
413
414 TEST(record_cmd, start_profiling_fd_option) {
415   int pipefd[2];
416   ASSERT_EQ(0, pipe(pipefd));
417   int read_fd = pipefd[0];
418   int write_fd = pipefd[1];
419   ASSERT_EXIT(
420       {
421         close(read_fd);
422         exit(RunRecordCmd({"--start_profiling_fd", std::to_string(write_fd)}) ? 0 : 1);
423       },
424       testing::ExitedWithCode(0), "");
425   close(write_fd);
426   std::string s;
427   ASSERT_TRUE(android::base::ReadFdToString(read_fd, &s));
428   close(read_fd);
429   ASSERT_EQ("STARTED", s);
430 }
431
432 TEST(record_cmd, record_meta_info_feature) {
433   TemporaryFile tmpfile;
434   ASSERT_TRUE(RunRecordCmd({}, tmpfile.path));
435   std::unique_ptr<RecordFileReader> reader = RecordFileReader::CreateInstance(tmpfile.path);
436   ASSERT_TRUE(reader != nullptr);
437   std::unordered_map<std::string, std::string> info_map;
438   ASSERT_TRUE(reader->ReadMetaInfoFeature(&info_map));
439   ASSERT_NE(info_map.find("simpleperf_version"), info_map.end());
440 }