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 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"}));
168 GTEST_LOG_(INFO) << "This test does nothing as dwarf callchain sampling is "
169 "not supported on this device.";
173 TEST(record_cmd, system_wide_dwarf_callchain_sampling) {
174 if (IsDwarfCallChainSamplingSupported()) {
175 TEST_IN_ROOT(RunRecordCmd({"-a", "--call-graph", "dwarf"}));
177 GTEST_LOG_(INFO) << "This test does nothing as dwarf callchain sampling is "
178 "not supported on this device.";
182 TEST(record_cmd, no_unwind_option) {
183 if (IsDwarfCallChainSamplingSupported()) {
184 ASSERT_TRUE(RunRecordCmd({"--call-graph", "dwarf", "--no-unwind"}));
186 GTEST_LOG_(INFO) << "This test does nothing as dwarf callchain sampling is "
187 "not supported on this device.";
189 ASSERT_FALSE(RunRecordCmd({"--no-unwind"}));
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"}));
199 GTEST_LOG_(INFO) << "This test does nothing as dwarf callchain sampling is "
200 "not supported on this device.";
202 ASSERT_FALSE(RunRecordCmd({"--post-unwind"}));
204 RunRecordCmd({"--call-graph", "dwarf", "--no-unwind", "--post-unwind"}));
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}));
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}));
224 TEST(record_cmd, no_monitored_threads) { ASSERT_FALSE(RecordCmd()->Run({""})); }
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"}));
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"}));
237 static void CheckKernelSymbol(const std::string& path, bool need_kallsyms,
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;
250 bool require_kallsyms = need_kallsyms && CheckKernelSymbolAddresses();
251 ASSERT_EQ(require_kallsyms, has_kernel_symbol_records);
255 TEST(record_cmd, kernel_symbol) {
256 TemporaryFile tmpfile;
257 ASSERT_TRUE(RunRecordCmd({"--no-dump-symbols"}, tmpfile.path));
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);
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,
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);
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);
300 ASSERT_FALSE(has_dso_record);
301 ASSERT_FALSE(has_symbol_record);
306 TEST(record_cmd, no_dump_symbols) {
307 TemporaryFile tmpfile;
308 ASSERT_TRUE(RunRecordCmd({}, tmpfile.path));
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));
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);
329 TEST(record_cmd, dump_kernel_symbols) {
331 GTEST_LOG_(INFO) << "Test requires root privilege";
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;
344 std::vector<Symbol> symbols;
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;
352 ASSERT_TRUE(has_kernel_symbols);
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"}));
362 TEST(record_cmd, symfs_option) { ASSERT_TRUE(RunRecordCmd({"--symfs", "/"})); }
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}));
369 RecordCmd()->Run({"--duration", "1", "-o", tmpfile.path, "sleep", "2"}));
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 << ":"
381 TEST(record_cmd, handle_SIGHUP) {
382 TemporaryFile tmpfile;
383 std::thread thread([]() {
385 kill(getpid(), SIGHUP);
388 ASSERT_TRUE(RecordCmd()->Run({"-o", tmpfile.path, "sleep", "1000000"}));
391 TEST(record_cmd, stop_when_no_more_targets) {
392 TemporaryFile tmpfile;
393 std::atomic<int> tid(0);
394 std::thread thread([&]() {
400 ASSERT_TRUE(RecordCmd()->Run({"-o", tmpfile.path, "-t", std::to_string(tid)}));
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));
414 TEST(record_cmd, start_profiling_fd_option) {
416 ASSERT_EQ(0, pipe(pipefd));
417 int read_fd = pipefd[0];
418 int write_fd = pipefd[1];
422 exit(RunRecordCmd({"--start_profiling_fd", std::to_string(write_fd)}) ? 0 : 1);
424 testing::ExitedWithCode(0), "");
427 ASSERT_TRUE(android::base::ReadFdToString(read_fd, &s));
429 ASSERT_EQ("STARTED", s);
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());