2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <gtest/gtest.h>
21 #include <android-base/file.h>
22 #include <android-base/stringprintf.h>
23 #include <android-base/test_utils.h>
30 #include "environment.h"
31 #include "event_selection_set.h"
32 #include "get_test_data.h"
34 #include "record_file.h"
35 #include "test_util.h"
36 #include "thread_tree.h"
38 using namespace PerfFileFormat;
40 static std::unique_ptr<Command> RecordCmd() {
41 return CreateCommandInstance("record");
44 static bool RunRecordCmd(std::vector<std::string> v,
45 const char* output_file = nullptr) {
46 std::unique_ptr<TemporaryFile> tmpfile;
48 if (output_file != nullptr) {
49 out_file = output_file;
51 tmpfile.reset(new TemporaryFile);
52 out_file = tmpfile->path;
54 v.insert(v.end(), {"-o", out_file, "sleep", SLEEP_SEC});
55 return RecordCmd()->Run(v);
58 TEST(record_cmd, no_options) { ASSERT_TRUE(RunRecordCmd({})); }
60 TEST(record_cmd, system_wide_option) {
61 TEST_IN_ROOT(ASSERT_TRUE(RunRecordCmd({"-a"})));
64 TEST(record_cmd, sample_period_option) {
65 ASSERT_TRUE(RunRecordCmd({"-c", "100000"}));
68 TEST(record_cmd, event_option) {
69 ASSERT_TRUE(RunRecordCmd({"-e", "cpu-clock"}));
72 TEST(record_cmd, freq_option) {
73 ASSERT_TRUE(RunRecordCmd({"-f", "99"}));
74 ASSERT_TRUE(RunRecordCmd({"-F", "99"}));
77 TEST(record_cmd, output_file_option) {
78 TemporaryFile tmpfile;
79 ASSERT_TRUE(RecordCmd()->Run({"-o", tmpfile.path, "sleep", SLEEP_SEC}));
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;
102 ASSERT_TRUE(have_kernel_mmap);
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);
117 TEST(record_cmd, tracepoint_event) {
118 TEST_IN_ROOT(ASSERT_TRUE(RunRecordCmd({"-a", "-e", "sched:sched_switch"})));
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"}));
129 GTEST_LOG_(INFO) << "This test does nothing as branch stack sampling is "
130 "not supported on this device.";
134 TEST(record_cmd, event_modifier) {
135 ASSERT_TRUE(RunRecordCmd({"-e", "cpu-cycles:u"}));
138 TEST(record_cmd, fp_callchain_sampling) {
139 ASSERT_TRUE(RunRecordCmd({"--call-graph", "fp"}));
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.";
149 exit(RunRecordCmd({"--call-graph", "fp"}) ? 0 : 1);
151 testing::ExitedWithCode(0), "doesn't work well on arm");
154 TEST(record_cmd, system_wide_fp_callchain_sampling) {
155 TEST_IN_ROOT(ASSERT_TRUE(RunRecordCmd({"-a", "--call-graph", "fp"})));
158 TEST(record_cmd, dwarf_callchain_sampling) {
159 ASSERT_TRUE(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"}));
169 TEST(record_cmd, system_wide_dwarf_callchain_sampling) {
170 ASSERT_TRUE(IsDwarfCallChainSamplingSupported());
171 TEST_IN_ROOT(RunRecordCmd({"-a", "--call-graph", "dwarf"}));
174 TEST(record_cmd, no_unwind_option) {
175 ASSERT_TRUE(IsDwarfCallChainSamplingSupported());
176 ASSERT_TRUE(RunRecordCmd({"--call-graph", "dwarf", "--no-unwind"}));
177 ASSERT_FALSE(RunRecordCmd({"--no-unwind"}));
180 TEST(record_cmd, post_unwind_option) {
181 ASSERT_TRUE(IsDwarfCallChainSamplingSupported());
182 std::vector<std::unique_ptr<Workload>> workloads;
183 CreateProcesses(1, &workloads);
184 std::string pid = std::to_string(workloads[0]->GetPid());
185 ASSERT_TRUE(RunRecordCmd({"-p", pid, "--call-graph", "dwarf", "--post-unwind"}));
186 ASSERT_FALSE(RunRecordCmd({"--post-unwind"}));
188 RunRecordCmd({"--call-graph", "dwarf", "--no-unwind", "--post-unwind"}));
191 TEST(record_cmd, existing_processes) {
192 std::vector<std::unique_ptr<Workload>> workloads;
193 CreateProcesses(2, &workloads);
194 std::string pid_list = android::base::StringPrintf(
195 "%d,%d", workloads[0]->GetPid(), workloads[1]->GetPid());
196 ASSERT_TRUE(RunRecordCmd({"-p", pid_list}));
199 TEST(record_cmd, existing_threads) {
200 std::vector<std::unique_ptr<Workload>> workloads;
201 CreateProcesses(2, &workloads);
202 // Process id can also be used as thread id in linux.
203 std::string tid_list = android::base::StringPrintf(
204 "%d,%d", workloads[0]->GetPid(), workloads[1]->GetPid());
205 ASSERT_TRUE(RunRecordCmd({"-t", tid_list}));
208 TEST(record_cmd, no_monitored_threads) { ASSERT_FALSE(RecordCmd()->Run({""})); }
210 TEST(record_cmd, more_than_one_event_types) {
211 ASSERT_TRUE(RunRecordCmd({"-e", "cpu-cycles,cpu-clock"}));
212 ASSERT_TRUE(RunRecordCmd({"-e", "cpu-cycles", "-e", "cpu-clock"}));
215 TEST(record_cmd, mmap_page_option) {
216 ASSERT_TRUE(RunRecordCmd({"-m", "1"}));
217 ASSERT_FALSE(RunRecordCmd({"-m", "0"}));
218 ASSERT_FALSE(RunRecordCmd({"-m", "7"}));
221 static void CheckKernelSymbol(const std::string& path, bool need_kallsyms,
224 std::unique_ptr<RecordFileReader> reader =
225 RecordFileReader::CreateInstance(path);
226 ASSERT_TRUE(reader != nullptr);
227 std::vector<std::unique_ptr<Record>> records = reader->DataSection();
228 bool has_kernel_symbol_records = false;
229 for (const auto& record : records) {
230 if (record->type() == SIMPLE_PERF_RECORD_KERNEL_SYMBOL) {
231 has_kernel_symbol_records = true;
234 bool require_kallsyms = need_kallsyms && CheckKernelSymbolAddresses();
235 ASSERT_EQ(require_kallsyms, has_kernel_symbol_records);
239 TEST(record_cmd, kernel_symbol) {
240 TemporaryFile tmpfile;
241 ASSERT_TRUE(RunRecordCmd({"--no-dump-symbols"}, tmpfile.path));
243 CheckKernelSymbol(tmpfile.path, true, &success);
244 ASSERT_TRUE(success);
245 ASSERT_TRUE(RunRecordCmd({"--no-dump-symbols", "--no-dump-kernel-symbols"}, tmpfile.path));
246 CheckKernelSymbol(tmpfile.path, false, &success);
247 ASSERT_TRUE(success);
250 // Check if the dso/symbol records in perf.data matches our expectation.
251 static void CheckDsoSymbolRecords(const std::string& path,
252 bool can_have_dso_symbol_records,
255 std::unique_ptr<RecordFileReader> reader =
256 RecordFileReader::CreateInstance(path);
257 ASSERT_TRUE(reader != nullptr);
258 std::vector<std::unique_ptr<Record>> records = reader->DataSection();
259 bool has_dso_record = false;
260 bool has_symbol_record = false;
261 std::map<uint64_t, bool> dso_hit_map;
262 for (const auto& record : records) {
263 if (record->type() == SIMPLE_PERF_RECORD_DSO) {
264 has_dso_record = true;
265 uint64_t dso_id = static_cast<const DsoRecord*>(record.get())->dso_id;
266 ASSERT_EQ(dso_hit_map.end(), dso_hit_map.find(dso_id));
267 dso_hit_map.insert(std::make_pair(dso_id, false));
268 } else if (record->type() == SIMPLE_PERF_RECORD_SYMBOL) {
269 has_symbol_record = true;
270 uint64_t dso_id = static_cast<const SymbolRecord*>(record.get())->dso_id;
271 auto it = dso_hit_map.find(dso_id);
272 ASSERT_NE(dso_hit_map.end(), it);
276 if (can_have_dso_symbol_records) {
277 // It is possible that there are no samples hitting functions having symbol.
278 // In that case, there are no dso/symbol records.
279 ASSERT_EQ(has_dso_record, has_symbol_record);
280 for (auto& pair : dso_hit_map) {
281 ASSERT_TRUE(pair.second);
284 ASSERT_FALSE(has_dso_record);
285 ASSERT_FALSE(has_symbol_record);
290 TEST(record_cmd, no_dump_symbols) {
291 TemporaryFile tmpfile;
292 ASSERT_TRUE(RunRecordCmd({}, tmpfile.path));
294 CheckDsoSymbolRecords(tmpfile.path, true, &success);
295 ASSERT_TRUE(success);
296 ASSERT_TRUE(RunRecordCmd({"--no-dump-symbols"}, tmpfile.path));
297 CheckDsoSymbolRecords(tmpfile.path, false, &success);
298 ASSERT_TRUE(success);
299 ASSERT_TRUE(IsDwarfCallChainSamplingSupported());
300 std::vector<std::unique_ptr<Workload>> workloads;
301 CreateProcesses(1, &workloads);
302 std::string pid = std::to_string(workloads[0]->GetPid());
303 ASSERT_TRUE(RunRecordCmd({"-p", pid, "-g"}, tmpfile.path));
304 CheckDsoSymbolRecords(tmpfile.path, true, &success);
305 ASSERT_TRUE(success);
306 ASSERT_TRUE(RunRecordCmd({"-p", pid, "-g", "--no-dump-symbols"}, tmpfile.path));
307 CheckDsoSymbolRecords(tmpfile.path, false, &success);
308 ASSERT_TRUE(success);
311 TEST(record_cmd, dump_kernel_symbols) {
313 GTEST_LOG_(INFO) << "Test requires root privilege";
316 TemporaryFile tmpfile;
317 ASSERT_TRUE(RunRecordCmd({"-a", "-o", tmpfile.path, "sleep", "1"}));
318 std::unique_ptr<RecordFileReader> reader = RecordFileReader::CreateInstance(tmpfile.path);
319 ASSERT_TRUE(reader != nullptr);
320 std::map<int, SectionDesc> section_map = reader->FeatureSectionDescriptors();
321 ASSERT_NE(section_map.find(FEAT_FILE), section_map.end());
322 std::string file_path;
325 std::vector<Symbol> symbols;
327 bool has_kernel_symbols = false;
328 while (reader->ReadFileFeature(read_pos, &file_path, &file_type, &min_vaddr, &symbols)) {
329 if (file_type == DSO_KERNEL && !symbols.empty()) {
330 has_kernel_symbols = true;
333 ASSERT_TRUE(has_kernel_symbols);
336 TEST(record_cmd, group_option) {
337 ASSERT_TRUE(RunRecordCmd({"--group", "cpu-cycles,cpu-clock", "-m", "16"}));
338 ASSERT_TRUE(RunRecordCmd({"--group", "cpu-cycles,cpu-clock", "--group",
339 "cpu-cycles:u,cpu-clock:u", "--group",
340 "cpu-cycles:k,cpu-clock:k", "-m", "16"}));
343 TEST(record_cmd, symfs_option) { ASSERT_TRUE(RunRecordCmd({"--symfs", "/"})); }
345 TEST(record_cmd, duration_option) {
346 TemporaryFile tmpfile;
347 ASSERT_TRUE(RecordCmd()->Run({"--duration", "1.2", "-p",
348 std::to_string(getpid()), "-o", tmpfile.path, "--in-app"}));
350 RecordCmd()->Run({"--duration", "1", "-o", tmpfile.path, "sleep", "2"}));
353 TEST(record_cmd, support_modifier_for_clock_events) {
354 for (const std::string& e : {"cpu-clock", "task-clock"}) {
355 for (const std::string& m : {"u", "k"}) {
356 ASSERT_TRUE(RunRecordCmd({"-e", e + ":" + m})) << "event " << e << ":"
362 TEST(record_cmd, handle_SIGHUP) {
363 TemporaryFile tmpfile;
364 std::thread thread([]() {
366 kill(getpid(), SIGHUP);
369 ASSERT_TRUE(RecordCmd()->Run({"-o", tmpfile.path, "sleep", "1000000"}));
372 TEST(record_cmd, stop_when_no_more_targets) {
373 TemporaryFile tmpfile;
374 std::atomic<int> tid(0);
375 std::thread thread([&]() {
381 ASSERT_TRUE(RecordCmd()->Run({"-o", tmpfile.path, "-t", std::to_string(tid), "--in-app"}));
384 TEST(record_cmd, donot_stop_when_having_targets) {
385 std::vector<std::unique_ptr<Workload>> workloads;
386 CreateProcesses(1, &workloads);
387 std::string pid = std::to_string(workloads[0]->GetPid());
388 uint64_t start_time_in_ns = GetSystemClock();
389 TemporaryFile tmpfile;
390 ASSERT_TRUE(RecordCmd()->Run({"-o", tmpfile.path, "-p", pid, "--duration", "3"}));
391 uint64_t end_time_in_ns = GetSystemClock();
392 ASSERT_GT(end_time_in_ns - start_time_in_ns, static_cast<uint64_t>(2e9));
395 TEST(record_cmd, start_profiling_fd_option) {
397 ASSERT_EQ(0, pipe(pipefd));
398 int read_fd = pipefd[0];
399 int write_fd = pipefd[1];
403 exit(RunRecordCmd({"--start_profiling_fd", std::to_string(write_fd)}) ? 0 : 1);
405 testing::ExitedWithCode(0), "");
408 ASSERT_TRUE(android::base::ReadFdToString(read_fd, &s));
410 ASSERT_EQ("STARTED", s);
413 TEST(record_cmd, record_meta_info_feature) {
414 TemporaryFile tmpfile;
415 ASSERT_TRUE(RunRecordCmd({}, tmpfile.path));
416 std::unique_ptr<RecordFileReader> reader = RecordFileReader::CreateInstance(tmpfile.path);
417 ASSERT_TRUE(reader != nullptr);
418 std::unordered_map<std::string, std::string> info_map;
419 ASSERT_TRUE(reader->ReadMetaInfoFeature(&info_map));
420 ASSERT_NE(info_map.find("simpleperf_version"), info_map.end());
423 // See http://b/63135835.
424 TEST(record_cmd, cpu_clock_for_a_long_time) {
425 std::vector<std::unique_ptr<Workload>> workloads;
426 CreateProcesses(1, &workloads);
427 std::string pid = std::to_string(workloads[0]->GetPid());
428 TemporaryFile tmpfile;
429 ASSERT_TRUE(RecordCmd()->Run(
430 {"-e", "cpu-clock", "-o", tmpfile.path, "-p", pid, "--duration", "3"}));
433 TEST(record_cmd, dump_regs_for_tracepoint_events) {
434 // Check if the kernel can dump registers for tracepoint events.
435 // If not, probably a kernel patch below is missing:
436 // "5b09a094f2 arm64: perf: Fix callchain parse error with kernel tracepoint events"
437 std::vector<std::unique_ptr<Workload>> workloads;
438 CreateProcesses(1, &workloads);
439 std::string pid = std::to_string(workloads[0]->GetPid());
440 TemporaryFile tmpfile;
441 ASSERT_TRUE(RecordCmd()->Run({"-o", tmpfile.path, "-p", pid, "-e", "sched:sched_switch",
442 "-g", "--no-unwind", "--duration", "1"}));
444 // If the kernel patch is missing, all regs dumped in sample records are zero.
445 std::unique_ptr<RecordFileReader> reader = RecordFileReader::CreateInstance(tmpfile.path);
446 CHECK(reader != nullptr);
447 std::unique_ptr<Record> r;
448 bool regs_all_zero = true;
449 while (reader->ReadRecord(r) && r && regs_all_zero) {
450 if (r->type() != PERF_RECORD_SAMPLE) {
453 SampleRecord* s = static_cast<SampleRecord*>(r.get());
454 for (size_t i = 0; i < s->regs_user_data.reg_nr; ++i) {
455 if (s->regs_user_data.regs[i] != 0u) {
456 regs_all_zero = false;
461 ASSERT_FALSE(regs_all_zero);