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 <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22
23 #include <filesystem>
24 #include <map>
25 #include <memory>
26 #include <regex>
27 #include <thread>
28
29 #include <android-base/file.h>
30 #include <android-base/logging.h>
31 #include <android-base/properties.h>
32 #include <android-base/stringprintf.h>
33 #include <android-base/strings.h>
34 #include <android-base/test_utils.h>
35
36 #include "ETMRecorder.h"
37 #include "JITDebugReader.h"
38 #include "ProbeEvents.h"
39 #include "cmd_record_impl.h"
40 #include "command.h"
41 #include "environment.h"
42 #include "event_selection_set.h"
43 #include "get_test_data.h"
44 #include "kallsyms.h"
45 #include "record.h"
46 #include "record_file.h"
47 #include "test_util.h"
48 #include "thread_tree.h"
49
50 using android::base::Realpath;
51 using android::base::StringPrintf;
52 using namespace simpleperf;
53 using namespace PerfFileFormat;
54 namespace fs = std::filesystem;
55
RecordCmd()56 static std::unique_ptr<Command> RecordCmd() {
57 return CreateCommandInstance("record");
58 }
59
GetDefaultEvent()60 static const char* GetDefaultEvent() {
61 return HasHardwareCounter() ? "cpu-cycles" : "task-clock";
62 }
63
RunRecordCmd(std::vector<std::string> v,const char * output_file=nullptr)64 static bool RunRecordCmd(std::vector<std::string> v, const char* output_file = nullptr) {
65 bool has_event = false;
66 for (auto& arg : v) {
67 if (arg == "-e" || arg == "--group") {
68 has_event = true;
69 break;
70 }
71 }
72 if (!has_event) {
73 v.insert(v.end(), {"-e", GetDefaultEvent()});
74 }
75
76 std::unique_ptr<TemporaryFile> tmpfile;
77 std::string out_file;
78 if (output_file != nullptr) {
79 out_file = output_file;
80 } else {
81 tmpfile.reset(new TemporaryFile);
82 out_file = tmpfile->path;
83 }
84 v.insert(v.end(), {"-o", out_file, "sleep", SLEEP_SEC});
85 return RecordCmd()->Run(v);
86 }
87
88 // @CddTest = 6.1/C-0-2
TEST(record_cmd,no_options)89 TEST(record_cmd, no_options) {
90 ASSERT_TRUE(RunRecordCmd({}));
91 }
92
93 // @CddTest = 6.1/C-0-2
TEST(record_cmd,system_wide_option)94 TEST(record_cmd, system_wide_option) {
95 TEST_IN_ROOT(ASSERT_TRUE(RunRecordCmd({"-a"})));
96 }
97
CheckEventType(const std::string & record_file,const std::string & event_type,uint64_t sample_period,uint64_t sample_freq)98 static void CheckEventType(const std::string& record_file, const std::string& event_type,
99 uint64_t sample_period, uint64_t sample_freq) {
100 const EventType* type = FindEventTypeByName(event_type);
101 ASSERT_TRUE(type != nullptr);
102 std::unique_ptr<RecordFileReader> reader = RecordFileReader::CreateInstance(record_file);
103 ASSERT_TRUE(reader);
104 for (const auto& attr_with_id : reader->AttrSection()) {
105 const perf_event_attr& attr = attr_with_id.attr;
106 if (attr.type == type->type && attr.config == type->config) {
107 if (attr.freq == 0) {
108 ASSERT_EQ(sample_period, attr.sample_period);
109 ASSERT_EQ(sample_freq, 0u);
110 } else {
111 ASSERT_EQ(sample_period, 0u);
112 ASSERT_EQ(sample_freq, attr.sample_freq);
113 }
114 return;
115 }
116 }
117 FAIL();
118 }
119
120 // @CddTest = 6.1/C-0-2
TEST(record_cmd,sample_period_option)121 TEST(record_cmd, sample_period_option) {
122 TemporaryFile tmpfile;
123 ASSERT_TRUE(RunRecordCmd({"-c", "100000"}, tmpfile.path));
124 CheckEventType(tmpfile.path, GetDefaultEvent(), 100000u, 0);
125 }
126
127 // @CddTest = 6.1/C-0-2
TEST(record_cmd,event_option)128 TEST(record_cmd, event_option) {
129 ASSERT_TRUE(RunRecordCmd({"-e", "cpu-clock"}));
130 }
131
132 // @CddTest = 6.1/C-0-2
TEST(record_cmd,freq_option)133 TEST(record_cmd, freq_option) {
134 TemporaryFile tmpfile;
135 ASSERT_TRUE(RunRecordCmd({"-f", "99"}, tmpfile.path));
136 CheckEventType(tmpfile.path, GetDefaultEvent(), 0, 99u);
137 ASSERT_TRUE(RunRecordCmd({"-e", "cpu-clock", "-f", "99"}, tmpfile.path));
138 CheckEventType(tmpfile.path, "cpu-clock", 0, 99u);
139 ASSERT_FALSE(RunRecordCmd({"-f", std::to_string(UINT_MAX)}));
140 }
141
142 // @CddTest = 6.1/C-0-2
TEST(record_cmd,multiple_freq_or_sample_period_option)143 TEST(record_cmd, multiple_freq_or_sample_period_option) {
144 TemporaryFile tmpfile;
145 ASSERT_TRUE(RunRecordCmd({"-f", "99", "-e", "task-clock", "-c", "1000000", "-e", "cpu-clock"},
146 tmpfile.path));
147 CheckEventType(tmpfile.path, "task-clock", 0, 99u);
148 CheckEventType(tmpfile.path, "cpu-clock", 1000000u, 0u);
149 }
150
151 // @CddTest = 6.1/C-0-2
TEST(record_cmd,output_file_option)152 TEST(record_cmd, output_file_option) {
153 TemporaryFile tmpfile;
154 ASSERT_TRUE(RecordCmd()->Run({"-o", tmpfile.path, "-e", GetDefaultEvent(), "sleep", SLEEP_SEC}));
155 }
156
157 // @CddTest = 6.1/C-0-2
TEST(record_cmd,dump_kernel_mmap)158 TEST(record_cmd, dump_kernel_mmap) {
159 TemporaryFile tmpfile;
160 ASSERT_TRUE(RunRecordCmd({}, tmpfile.path));
161 std::unique_ptr<RecordFileReader> reader = RecordFileReader::CreateInstance(tmpfile.path);
162 ASSERT_TRUE(reader != nullptr);
163 std::vector<std::unique_ptr<Record>> records = reader->DataSection();
164 ASSERT_GT(records.size(), 0U);
165 bool have_kernel_mmap = false;
166 for (auto& record : records) {
167 if (record->type() == PERF_RECORD_MMAP) {
168 const MmapRecord* mmap_record = static_cast<const MmapRecord*>(record.get());
169 if (android::base::StartsWith(mmap_record->filename, DEFAULT_KERNEL_MMAP_NAME)) {
170 have_kernel_mmap = true;
171 break;
172 }
173 }
174 }
175 ASSERT_TRUE(have_kernel_mmap);
176 }
177
178 // @CddTest = 6.1/C-0-2
TEST(record_cmd,dump_build_id_feature)179 TEST(record_cmd, dump_build_id_feature) {
180 TemporaryFile tmpfile;
181 ASSERT_TRUE(RunRecordCmd({}, tmpfile.path));
182 std::unique_ptr<RecordFileReader> reader = RecordFileReader::CreateInstance(tmpfile.path);
183 ASSERT_TRUE(reader != nullptr);
184 const FileHeader& file_header = reader->FileHeader();
185 ASSERT_TRUE(file_header.features[FEAT_BUILD_ID / 8] & (1 << (FEAT_BUILD_ID % 8)));
186 ASSERT_GT(reader->FeatureSectionDescriptors().size(), 0u);
187 }
188
189 // @CddTest = 6.1/C-0-2
TEST(record_cmd,tracepoint_event)190 TEST(record_cmd, tracepoint_event) {
191 TEST_IN_ROOT(ASSERT_TRUE(RunRecordCmd({"-a", "-e", "sched:sched_switch"})));
192 }
193
194 // @CddTest = 6.1/C-0-2
TEST(record_cmd,rN_event)195 TEST(record_cmd, rN_event) {
196 TEST_REQUIRE_HW_COUNTER();
197 OMIT_TEST_ON_NON_NATIVE_ABIS();
198 size_t event_number;
199 if (GetTargetArch() == ARCH_ARM64 || GetTargetArch() == ARCH_ARM) {
200 // As in D5.10.2 of the ARMv8 manual, ARM defines the event number space for PMU. part of the
201 // space is for common event numbers (which will stay the same for all ARM chips), part of the
202 // space is for implementation defined events. Here 0x08 is a common event for instructions.
203 event_number = 0x08;
204 } else if (GetTargetArch() == ARCH_X86_32 || GetTargetArch() == ARCH_X86_64) {
205 // As in volume 3 chapter 19 of the Intel manual, 0x00c0 is the event number for instruction.
206 event_number = 0x00c0;
207 } else if (GetTargetArch() == ARCH_RISCV64) {
208 // RISCV_PMU_INSTRET = 1
209 event_number = 0x1;
210 } else {
211 GTEST_LOG_(INFO) << "Omit arch " << GetTargetArch();
212 return;
213 }
214 std::string event_name = android::base::StringPrintf("r%zx", event_number);
215 TemporaryFile tmpfile;
216 ASSERT_TRUE(RunRecordCmd({"-e", event_name}, tmpfile.path));
217 std::unique_ptr<RecordFileReader> reader = RecordFileReader::CreateInstance(tmpfile.path);
218 ASSERT_TRUE(reader);
219 const EventAttrIds& attrs = reader->AttrSection();
220 ASSERT_EQ(1u, attrs.size());
221 ASSERT_EQ(PERF_TYPE_RAW, attrs[0].attr.type);
222 ASSERT_EQ(event_number, attrs[0].attr.config);
223 }
224
225 // @CddTest = 6.1/C-0-2
TEST(record_cmd,branch_sampling)226 TEST(record_cmd, branch_sampling) {
227 TEST_REQUIRE_HW_COUNTER();
228 if (IsBranchSamplingSupported()) {
229 ASSERT_TRUE(RunRecordCmd({"-b"}));
230 ASSERT_TRUE(RunRecordCmd({"-j", "any,any_call,any_ret,ind_call"}));
231 ASSERT_TRUE(RunRecordCmd({"-j", "any,k"}));
232 ASSERT_TRUE(RunRecordCmd({"-j", "any,u"}));
233 ASSERT_FALSE(RunRecordCmd({"-j", "u"}));
234 } else {
235 GTEST_LOG_(INFO) << "This test does nothing as branch stack sampling is "
236 "not supported on this device.";
237 }
238 }
239
240 // @CddTest = 6.1/C-0-2
TEST(record_cmd,event_modifier)241 TEST(record_cmd, event_modifier) {
242 ASSERT_TRUE(RunRecordCmd({"-e", GetDefaultEvent() + std::string(":u")}));
243 }
244
245 // @CddTest = 6.1/C-0-2
TEST(record_cmd,fp_callchain_sampling)246 TEST(record_cmd, fp_callchain_sampling) {
247 ASSERT_TRUE(RunRecordCmd({"--call-graph", "fp"}));
248 }
249
250 // @CddTest = 6.1/C-0-2
TEST(record_cmd,fp_callchain_sampling_warning_on_arm)251 TEST(record_cmd, fp_callchain_sampling_warning_on_arm) {
252 if (GetTargetArch() != ARCH_ARM) {
253 GTEST_LOG_(INFO) << "This test does nothing as it only tests on arm arch.";
254 return;
255 }
256 ASSERT_EXIT(
257 { exit(RunRecordCmd({"--call-graph", "fp"}) ? 0 : 1); }, testing::ExitedWithCode(0),
258 "doesn't work well on arm");
259 }
260
261 // @CddTest = 6.1/C-0-2
TEST(record_cmd,system_wide_fp_callchain_sampling)262 TEST(record_cmd, system_wide_fp_callchain_sampling) {
263 TEST_IN_ROOT(ASSERT_TRUE(RunRecordCmd({"-a", "--call-graph", "fp"})));
264 }
265
266 // @CddTest = 6.1/C-0-2
TEST(record_cmd,dwarf_callchain_sampling)267 TEST(record_cmd, dwarf_callchain_sampling) {
268 OMIT_TEST_ON_NON_NATIVE_ABIS();
269 ASSERT_TRUE(IsDwarfCallChainSamplingSupported());
270 std::vector<std::unique_ptr<Workload>> workloads;
271 CreateProcesses(1, &workloads);
272 std::string pid = std::to_string(workloads[0]->GetPid());
273 ASSERT_TRUE(RunRecordCmd({"-p", pid, "--call-graph", "dwarf"}));
274 ASSERT_TRUE(RunRecordCmd({"-p", pid, "--call-graph", "dwarf,16384"}));
275 ASSERT_FALSE(RunRecordCmd({"-p", pid, "--call-graph", "dwarf,65536"}));
276 TemporaryFile tmpfile;
277 ASSERT_TRUE(RunRecordCmd({"-p", pid, "-g"}, tmpfile.path));
278 auto reader = RecordFileReader::CreateInstance(tmpfile.path);
279 ASSERT_TRUE(reader);
280 const EventAttrIds& attrs = reader->AttrSection();
281 ASSERT_GT(attrs.size(), 0);
282 // Check that reg and stack fields are removed after unwinding.
283 for (const auto& attr : attrs) {
284 ASSERT_EQ(attr.attr.sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER), 0);
285 }
286 }
287
288 // @CddTest = 6.1/C-0-2
TEST(record_cmd,system_wide_dwarf_callchain_sampling)289 TEST(record_cmd, system_wide_dwarf_callchain_sampling) {
290 OMIT_TEST_ON_NON_NATIVE_ABIS();
291 ASSERT_TRUE(IsDwarfCallChainSamplingSupported());
292 TEST_IN_ROOT(RunRecordCmd({"-a", "--call-graph", "dwarf"}));
293 }
294
295 // @CddTest = 6.1/C-0-2
TEST(record_cmd,no_unwind_option)296 TEST(record_cmd, no_unwind_option) {
297 OMIT_TEST_ON_NON_NATIVE_ABIS();
298 ASSERT_TRUE(IsDwarfCallChainSamplingSupported());
299 ASSERT_TRUE(RunRecordCmd({"--call-graph", "dwarf", "--no-unwind"}));
300 ASSERT_FALSE(RunRecordCmd({"--no-unwind"}));
301 }
302
303 // @CddTest = 6.1/C-0-2
TEST(record_cmd,post_unwind_option)304 TEST(record_cmd, post_unwind_option) {
305 OMIT_TEST_ON_NON_NATIVE_ABIS();
306 ASSERT_TRUE(IsDwarfCallChainSamplingSupported());
307 std::vector<std::unique_ptr<Workload>> workloads;
308 CreateProcesses(1, &workloads);
309 std::string pid = std::to_string(workloads[0]->GetPid());
310 ASSERT_TRUE(RunRecordCmd({"-p", pid, "--call-graph", "dwarf", "--post-unwind"}));
311 ASSERT_TRUE(RunRecordCmd({"-p", pid, "--call-graph", "dwarf", "--post-unwind=yes"}));
312 ASSERT_TRUE(RunRecordCmd({"-p", pid, "--call-graph", "dwarf", "--post-unwind=no"}));
313 }
314
315 // @CddTest = 6.1/C-0-2
TEST(record_cmd,existing_processes)316 TEST(record_cmd, existing_processes) {
317 std::vector<std::unique_ptr<Workload>> workloads;
318 CreateProcesses(2, &workloads);
319 std::string pid_list =
320 android::base::StringPrintf("%d,%d", workloads[0]->GetPid(), workloads[1]->GetPid());
321 ASSERT_TRUE(RunRecordCmd({"-p", pid_list}));
322 }
323
324 // @CddTest = 6.1/C-0-2
TEST(record_cmd,existing_threads)325 TEST(record_cmd, existing_threads) {
326 std::vector<std::unique_ptr<Workload>> workloads;
327 CreateProcesses(2, &workloads);
328 // Process id can also be used as thread id in linux.
329 std::string tid_list =
330 android::base::StringPrintf("%d,%d", workloads[0]->GetPid(), workloads[1]->GetPid());
331 ASSERT_TRUE(RunRecordCmd({"-t", tid_list}));
332 }
333
334 // @CddTest = 6.1/C-0-2
TEST(record_cmd,no_monitored_threads)335 TEST(record_cmd, no_monitored_threads) {
336 TemporaryFile tmpfile;
337 ASSERT_FALSE(RecordCmd()->Run({"-o", tmpfile.path}));
338 ASSERT_FALSE(RecordCmd()->Run({"-o", tmpfile.path, ""}));
339 }
340
341 // @CddTest = 6.1/C-0-2
TEST(record_cmd,more_than_one_event_types)342 TEST(record_cmd, more_than_one_event_types) {
343 ASSERT_TRUE(RunRecordCmd({"-e", "task-clock,cpu-clock"}));
344 ASSERT_TRUE(RunRecordCmd({"-e", "task-clock", "-e", "cpu-clock"}));
345 }
346
347 // @CddTest = 6.1/C-0-2
TEST(record_cmd,mmap_page_option)348 TEST(record_cmd, mmap_page_option) {
349 ASSERT_TRUE(RunRecordCmd({"-m", "1"}));
350 ASSERT_FALSE(RunRecordCmd({"-m", "0"}));
351 ASSERT_FALSE(RunRecordCmd({"-m", "7"}));
352 }
353
CheckKernelSymbol(const std::string & path,bool need_kallsyms,bool * success)354 static void CheckKernelSymbol(const std::string& path, bool need_kallsyms, bool* success) {
355 *success = false;
356 std::unique_ptr<RecordFileReader> reader = RecordFileReader::CreateInstance(path);
357 ASSERT_TRUE(reader != nullptr);
358 std::vector<std::unique_ptr<Record>> records = reader->DataSection();
359 bool has_kernel_symbol_records = false;
360 for (const auto& record : records) {
361 if (record->type() == SIMPLE_PERF_RECORD_KERNEL_SYMBOL) {
362 has_kernel_symbol_records = true;
363 }
364 }
365 std::string kallsyms;
366 bool require_kallsyms = need_kallsyms && LoadKernelSymbols(&kallsyms);
367 ASSERT_EQ(require_kallsyms, has_kernel_symbol_records);
368 *success = true;
369 }
370
371 // @CddTest = 6.1/C-0-2
TEST(record_cmd,kernel_symbol)372 TEST(record_cmd, kernel_symbol) {
373 TemporaryFile tmpfile;
374 ASSERT_TRUE(RunRecordCmd({"--no-dump-symbols"}, tmpfile.path));
375 bool success;
376 CheckKernelSymbol(tmpfile.path, true, &success);
377 ASSERT_TRUE(success);
378 ASSERT_TRUE(RunRecordCmd({"--no-dump-symbols", "--no-dump-kernel-symbols"}, tmpfile.path));
379 CheckKernelSymbol(tmpfile.path, false, &success);
380 ASSERT_TRUE(success);
381 }
382
ProcessSymbolsInPerfDataFile(const std::string & perf_data_file,const std::function<bool (const Symbol &,uint32_t)> & callback)383 static void ProcessSymbolsInPerfDataFile(
384 const std::string& perf_data_file,
385 const std::function<bool(const Symbol&, uint32_t)>& callback) {
386 auto reader = RecordFileReader::CreateInstance(perf_data_file);
387 ASSERT_TRUE(reader);
388 FileFeature file;
389 uint64_t read_pos = 0;
390 bool error = false;
391 while (reader->ReadFileFeature(read_pos, file, error)) {
392 for (const auto& symbol : file.symbols) {
393 if (callback(symbol, file.type)) {
394 return;
395 }
396 }
397 }
398 ASSERT_FALSE(error);
399 }
400
401 // Check if dumped symbols in perf.data matches our expectation.
CheckDumpedSymbols(const std::string & path,bool allow_dumped_symbols)402 static bool CheckDumpedSymbols(const std::string& path, bool allow_dumped_symbols) {
403 bool has_dumped_symbols = false;
404 auto callback = [&](const Symbol&, uint32_t) {
405 has_dumped_symbols = true;
406 return true;
407 };
408 ProcessSymbolsInPerfDataFile(path, callback);
409 // It is possible that there are no samples hitting functions having symbols.
410 // So "allow_dumped_symbols = true" doesn't guarantee "has_dumped_symbols = true".
411 if (!allow_dumped_symbols && has_dumped_symbols) {
412 return false;
413 }
414 return true;
415 }
416
417 // @CddTest = 6.1/C-0-2
TEST(record_cmd,no_dump_symbols)418 TEST(record_cmd, no_dump_symbols) {
419 TemporaryFile tmpfile;
420 ASSERT_TRUE(RunRecordCmd({}, tmpfile.path));
421 ASSERT_TRUE(CheckDumpedSymbols(tmpfile.path, true));
422 ASSERT_TRUE(RunRecordCmd({"--no-dump-symbols", "--no-dump-kernel-symbols"}, tmpfile.path));
423 ASSERT_TRUE(CheckDumpedSymbols(tmpfile.path, false));
424 OMIT_TEST_ON_NON_NATIVE_ABIS();
425 ASSERT_TRUE(IsDwarfCallChainSamplingSupported());
426 std::vector<std::unique_ptr<Workload>> workloads;
427 CreateProcesses(1, &workloads);
428 std::string pid = std::to_string(workloads[0]->GetPid());
429 ASSERT_TRUE(RunRecordCmd({"-p", pid, "-g"}, tmpfile.path));
430 ASSERT_TRUE(CheckDumpedSymbols(tmpfile.path, true));
431 ASSERT_TRUE(RunRecordCmd({"-p", pid, "-g", "--no-dump-symbols", "--no-dump-kernel-symbols"},
432 tmpfile.path));
433 ASSERT_TRUE(CheckDumpedSymbols(tmpfile.path, false));
434 }
435
436 // @CddTest = 6.1/C-0-2
TEST(record_cmd,dump_kernel_symbols)437 TEST(record_cmd, dump_kernel_symbols) {
438 TEST_REQUIRE_ROOT();
439 TemporaryFile tmpfile;
440 ASSERT_TRUE(RecordCmd()->Run({"-a", "-o", tmpfile.path, "-e", GetDefaultEvent(), "sleep", "1"}));
441 bool has_kernel_symbols = false;
442 auto callback = [&](const Symbol&, uint32_t file_type) {
443 if (file_type == DSO_KERNEL) {
444 has_kernel_symbols = true;
445 }
446 return has_kernel_symbols;
447 };
448 ProcessSymbolsInPerfDataFile(tmpfile.path, callback);
449 ASSERT_TRUE(has_kernel_symbols);
450 }
451
452 // @CddTest = 6.1/C-0-2
TEST(record_cmd,group_option)453 TEST(record_cmd, group_option) {
454 ASSERT_TRUE(RunRecordCmd({"--group", "task-clock,cpu-clock", "-m", "16"}));
455 ASSERT_TRUE(
456 RunRecordCmd({"--group", "task-clock,cpu-clock", "--group", "task-clock:u,cpu-clock:u",
457 "--group", "task-clock:k,cpu-clock:k", "-m", "16"}));
458 }
459
460 // @CddTest = 6.1/C-0-2
TEST(record_cmd,symfs_option)461 TEST(record_cmd, symfs_option) {
462 ASSERT_TRUE(RunRecordCmd({"--symfs", "/"}));
463 }
464
465 // @CddTest = 6.1/C-0-2
TEST(record_cmd,duration_option)466 TEST(record_cmd, duration_option) {
467 TemporaryFile tmpfile;
468 ASSERT_TRUE(RecordCmd()->Run({"--duration", "1.2", "-p", std::to_string(getpid()), "-o",
469 tmpfile.path, "--in-app", "-e", GetDefaultEvent()}));
470 ASSERT_TRUE(RecordCmd()->Run(
471 {"--duration", "1", "-o", tmpfile.path, "-e", GetDefaultEvent(), "sleep", "2"}));
472 }
473
474 // @CddTest = 6.1/C-0-2
TEST(record_cmd,support_modifier_for_clock_events)475 TEST(record_cmd, support_modifier_for_clock_events) {
476 for (const std::string& e : {"cpu-clock", "task-clock"}) {
477 for (const std::string& m : {"u", "k"}) {
478 ASSERT_TRUE(RunRecordCmd({"-e", e + ":" + m})) << "event " << e << ":" << m;
479 }
480 }
481 }
482
483 // @CddTest = 6.1/C-0-2
TEST(record_cmd,handle_SIGHUP)484 TEST(record_cmd, handle_SIGHUP) {
485 TemporaryFile tmpfile;
486 int pipefd[2];
487 ASSERT_EQ(0, pipe(pipefd));
488 int read_fd = pipefd[0];
489 int write_fd = pipefd[1];
490 char data[8] = {};
491 std::thread thread([&]() {
492 android::base::ReadFully(read_fd, data, 7);
493 kill(getpid(), SIGHUP);
494 });
495 ASSERT_TRUE(
496 RecordCmd()->Run({"-o", tmpfile.path, "--start_profiling_fd", std::to_string(write_fd), "-e",
497 GetDefaultEvent(), "sleep", "1000000"}));
498 thread.join();
499 close(write_fd);
500 close(read_fd);
501 ASSERT_STREQ(data, "STARTED");
502 }
503
504 // @CddTest = 6.1/C-0-2
TEST(record_cmd,stop_when_no_more_targets)505 TEST(record_cmd, stop_when_no_more_targets) {
506 TemporaryFile tmpfile;
507 std::atomic<int> tid(0);
508 std::thread thread([&]() {
509 tid = gettid();
510 sleep(1);
511 });
512 thread.detach();
513 while (tid == 0);
514 ASSERT_TRUE(RecordCmd()->Run(
515 {"-o", tmpfile.path, "-t", std::to_string(tid), "--in-app", "-e", GetDefaultEvent()}));
516 }
517
518 // @CddTest = 6.1/C-0-2
TEST(record_cmd,donot_stop_when_having_targets)519 TEST(record_cmd, donot_stop_when_having_targets) {
520 std::vector<std::unique_ptr<Workload>> workloads;
521 CreateProcesses(1, &workloads);
522 std::string pid = std::to_string(workloads[0]->GetPid());
523 uint64_t start_time_in_ns = GetSystemClock();
524 TemporaryFile tmpfile;
525 ASSERT_TRUE(RecordCmd()->Run(
526 {"-o", tmpfile.path, "-p", pid, "--duration", "3", "-e", GetDefaultEvent()}));
527 uint64_t end_time_in_ns = GetSystemClock();
528 ASSERT_GT(end_time_in_ns - start_time_in_ns, static_cast<uint64_t>(2e9));
529 }
530
531 // @CddTest = 6.1/C-0-2
TEST(record_cmd,start_profiling_fd_option)532 TEST(record_cmd, start_profiling_fd_option) {
533 int pipefd[2];
534 ASSERT_EQ(0, pipe(pipefd));
535 int read_fd = pipefd[0];
536 int write_fd = pipefd[1];
537 ASSERT_EXIT(
538 {
539 close(read_fd);
540 exit(RunRecordCmd({"--start_profiling_fd", std::to_string(write_fd)}) ? 0 : 1);
541 },
542 testing::ExitedWithCode(0), "");
543 close(write_fd);
544 std::string s;
545 ASSERT_TRUE(android::base::ReadFdToString(read_fd, &s));
546 close(read_fd);
547 ASSERT_EQ("STARTED", s);
548 }
549
550 // @CddTest = 6.1/C-0-2
TEST(record_cmd,record_meta_info_feature)551 TEST(record_cmd, record_meta_info_feature) {
552 TemporaryFile tmpfile;
553 ASSERT_TRUE(RunRecordCmd({}, tmpfile.path));
554 std::unique_ptr<RecordFileReader> reader = RecordFileReader::CreateInstance(tmpfile.path);
555 ASSERT_TRUE(reader);
556 auto& info_map = reader->GetMetaInfoFeature();
557 ASSERT_NE(info_map.find("simpleperf_version"), info_map.end());
558 ASSERT_NE(info_map.find("timestamp"), info_map.end());
559 ASSERT_NE(info_map.find("record_stat"), info_map.end());
560 #if defined(__ANDROID__)
561 ASSERT_NE(info_map.find("product_props"), info_map.end());
562 ASSERT_NE(info_map.find("android_version"), info_map.end());
563 #endif
564 }
565
566 // See http://b/63135835.
567 // @CddTest = 6.1/C-0-2
TEST(record_cmd,cpu_clock_for_a_long_time)568 TEST(record_cmd, cpu_clock_for_a_long_time) {
569 std::vector<std::unique_ptr<Workload>> workloads;
570 CreateProcesses(1, &workloads);
571 std::string pid = std::to_string(workloads[0]->GetPid());
572 TemporaryFile tmpfile;
573 ASSERT_TRUE(
574 RecordCmd()->Run({"-e", "cpu-clock", "-o", tmpfile.path, "-p", pid, "--duration", "3"}));
575 }
576
577 // @CddTest = 6.1/C-0-2
TEST(record_cmd,dump_regs_for_tracepoint_events)578 TEST(record_cmd, dump_regs_for_tracepoint_events) {
579 TEST_REQUIRE_HOST_ROOT();
580 TEST_REQUIRE_TRACEPOINT_EVENTS();
581 OMIT_TEST_ON_NON_NATIVE_ABIS();
582 // Check if the kernel can dump registers for tracepoint events.
583 // If not, probably a kernel patch below is missing:
584 // "5b09a094f2 arm64: perf: Fix callchain parse error with kernel tracepoint events"
585 ASSERT_TRUE(IsDumpingRegsForTracepointEventsSupported());
586 }
587
588 // @CddTest = 6.1/C-0-2
TEST(record_cmd,trace_offcpu_option)589 TEST(record_cmd, trace_offcpu_option) {
590 // On linux host, we need root privilege to read tracepoint events.
591 TEST_REQUIRE_HOST_ROOT();
592 TEST_REQUIRE_TRACEPOINT_EVENTS();
593 OMIT_TEST_ON_NON_NATIVE_ABIS();
594 TemporaryFile tmpfile;
595 ASSERT_TRUE(RunRecordCmd({"--trace-offcpu", "-e", "cpu-clock", "-f", "1000"}, tmpfile.path));
596 CheckEventType(tmpfile.path, "sched:sched_switch", 1u, 0u);
597 std::unique_ptr<RecordFileReader> reader = RecordFileReader::CreateInstance(tmpfile.path);
598 ASSERT_TRUE(reader);
599 auto info_map = reader->GetMetaInfoFeature();
600 ASSERT_EQ(info_map["trace_offcpu"], "true");
601 if (IsSwitchRecordSupported()) {
602 ASSERT_EQ(reader->AttrSection()[0].attr.context_switch, 1);
603 }
604 // Release recording environment in perf.data, to avoid affecting tests below.
605 reader.reset();
606
607 // --trace-offcpu only works with cpu-clock and task-clock. cpu-clock has been tested above.
608 ASSERT_TRUE(RunRecordCmd({"--trace-offcpu", "-e", "task-clock"}));
609 ASSERT_FALSE(RunRecordCmd({"--trace-offcpu", "-e", "page-faults"}));
610 // --trace-offcpu doesn't work with more than one event.
611 ASSERT_FALSE(RunRecordCmd({"--trace-offcpu", "-e", "cpu-clock,task-clock"}));
612 }
613
614 // @CddTest = 6.1/C-0-2
TEST(record_cmd,exit_with_parent_option)615 TEST(record_cmd, exit_with_parent_option) {
616 ASSERT_TRUE(RunRecordCmd({"--exit-with-parent"}));
617 }
618
619 // @CddTest = 6.1/C-0-2
TEST(record_cmd,use_cmd_exit_code_option)620 TEST(record_cmd, use_cmd_exit_code_option) {
621 TemporaryFile tmpfile;
622 int exit_code;
623 RecordCmd()->Run({"-e", GetDefaultEvent(), "--use-cmd-exit-code", "-o", tmpfile.path, "ls", "."},
624 &exit_code);
625 ASSERT_EQ(exit_code, 0);
626 RecordCmd()->Run(
627 {"-e", GetDefaultEvent(), "--use-cmd-exit-code", "-o", tmpfile.path, "ls", "/not_exist_path"},
628 &exit_code);
629 ASSERT_NE(exit_code, 0);
630 }
631
632 // @CddTest = 6.1/C-0-2
TEST(record_cmd,clockid_option)633 TEST(record_cmd, clockid_option) {
634 if (!IsSettingClockIdSupported()) {
635 ASSERT_FALSE(RunRecordCmd({"--clockid", "monotonic"}));
636 } else {
637 TemporaryFile tmpfile;
638 ASSERT_TRUE(RunRecordCmd({"--clockid", "monotonic"}, tmpfile.path));
639 std::unique_ptr<RecordFileReader> reader = RecordFileReader::CreateInstance(tmpfile.path);
640 ASSERT_TRUE(reader);
641 auto info_map = reader->GetMetaInfoFeature();
642 ASSERT_EQ(info_map["clockid"], "monotonic");
643 }
644 }
645
646 // @CddTest = 6.1/C-0-2
TEST(record_cmd,generate_samples_by_hw_counters)647 TEST(record_cmd, generate_samples_by_hw_counters) {
648 TEST_REQUIRE_HW_COUNTER();
649 std::vector<std::string> events = {"cpu-cycles", "instructions"};
650 for (auto& event : events) {
651 TemporaryFile tmpfile;
652 ASSERT_TRUE(RecordCmd()->Run({"-e", event, "-o", tmpfile.path, "sleep", "1"}));
653 std::unique_ptr<RecordFileReader> reader = RecordFileReader::CreateInstance(tmpfile.path);
654 ASSERT_TRUE(reader);
655 bool has_sample = false;
656 ASSERT_TRUE(reader->ReadDataSection([&](std::unique_ptr<Record> r) {
657 if (r->type() == PERF_RECORD_SAMPLE) {
658 has_sample = true;
659 }
660 return true;
661 }));
662 ASSERT_TRUE(has_sample);
663 }
664 }
665
666 // @CddTest = 6.1/C-0-2
TEST(record_cmd,callchain_joiner_options)667 TEST(record_cmd, callchain_joiner_options) {
668 ASSERT_TRUE(RunRecordCmd({"--no-callchain-joiner"}));
669 ASSERT_TRUE(RunRecordCmd({"--callchain-joiner-min-matching-nodes", "2"}));
670 }
671
672 // @CddTest = 6.1/C-0-2
TEST(record_cmd,dashdash)673 TEST(record_cmd, dashdash) {
674 TemporaryFile tmpfile;
675 ASSERT_TRUE(RecordCmd()->Run({"-o", tmpfile.path, "-e", GetDefaultEvent(), "--", "sleep", "1"}));
676 }
677
678 // @CddTest = 6.1/C-0-2
TEST(record_cmd,size_limit_option)679 TEST(record_cmd, size_limit_option) {
680 std::vector<std::unique_ptr<Workload>> workloads;
681 CreateProcesses(1, &workloads);
682 std::string pid = std::to_string(workloads[0]->GetPid());
683 TemporaryFile tmpfile;
684 ASSERT_TRUE(RecordCmd()->Run({"-o", tmpfile.path, "-p", pid, "--size-limit", "1k", "--duration",
685 "1", "-e", GetDefaultEvent()}));
686 std::unique_ptr<RecordFileReader> reader = RecordFileReader::CreateInstance(tmpfile.path);
687 ASSERT_TRUE(reader);
688 ASSERT_GT(reader->FileHeader().data.size, 1000u);
689 ASSERT_LT(reader->FileHeader().data.size, 2000u);
690 ASSERT_FALSE(RunRecordCmd({"--size-limit", "0"}));
691 }
692
693 // @CddTest = 6.1/C-0-2
TEST(record_cmd,support_mmap2)694 TEST(record_cmd, support_mmap2) {
695 // mmap2 is supported in kernel >= 3.16. If not supported, please cherry pick below kernel
696 // patches:
697 // 13d7a2410fa637 perf: Add attr->mmap2 attribute to an event
698 // f972eb63b1003f perf: Pass protection and flags bits through mmap2 interface.
699 ASSERT_TRUE(IsMmap2Supported());
700 }
701
702 // @CddTest = 6.1/C-0-2
TEST(record_cmd,kernel_bug_making_zero_dyn_size)703 TEST(record_cmd, kernel_bug_making_zero_dyn_size) {
704 // Test a kernel bug that makes zero dyn_size in kernel < 3.13. If it fails, please cherry pick
705 // below kernel patch: 0a196848ca365e perf: Fix arch_perf_out_copy_user default
706 OMIT_TEST_ON_NON_NATIVE_ABIS();
707 std::vector<std::unique_ptr<Workload>> workloads;
708 CreateProcesses(1, &workloads);
709 std::string pid = std::to_string(workloads[0]->GetPid());
710 TemporaryFile tmpfile;
711 ASSERT_TRUE(RecordCmd()->Run({"-o", tmpfile.path, "-p", pid, "--call-graph", "dwarf,8",
712 "--no-unwind", "--duration", "1", "-e", GetDefaultEvent()}));
713 std::unique_ptr<RecordFileReader> reader = RecordFileReader::CreateInstance(tmpfile.path);
714 ASSERT_TRUE(reader);
715 bool has_sample = false;
716 ASSERT_TRUE(reader->ReadDataSection([&](std::unique_ptr<Record> r) {
717 if (r->type() == PERF_RECORD_SAMPLE && !r->InKernel()) {
718 SampleRecord* sr = static_cast<SampleRecord*>(r.get());
719 if (sr->stack_user_data.dyn_size == 0) {
720 return false;
721 }
722 has_sample = true;
723 }
724 return true;
725 }));
726 ASSERT_TRUE(has_sample);
727 }
728
729 // @CddTest = 6.1/C-0-2
TEST(record_cmd,kernel_bug_making_zero_dyn_size_for_kernel_samples)730 TEST(record_cmd, kernel_bug_making_zero_dyn_size_for_kernel_samples) {
731 // Test a kernel bug that makes zero dyn_size for syscalls of 32-bit applications in 64-bit
732 // kernels. If it fails, please cherry pick below kernel patch:
733 // 02e184476eff8 perf/core: Force USER_DS when recording user stack data
734 OMIT_TEST_ON_NON_NATIVE_ABIS();
735 TEST_REQUIRE_HOST_ROOT();
736 TEST_REQUIRE_TRACEPOINT_EVENTS();
737 std::vector<std::unique_ptr<Workload>> workloads;
738 CreateProcesses(1, &workloads);
739 std::string pid = std::to_string(workloads[0]->GetPid());
740 TemporaryFile tmpfile;
741 ASSERT_TRUE(RecordCmd()->Run({"-e", "sched:sched_switch", "-o", tmpfile.path, "-p", pid,
742 "--call-graph", "dwarf,8", "--no-unwind", "--duration", "1"}));
743 std::unique_ptr<RecordFileReader> reader = RecordFileReader::CreateInstance(tmpfile.path);
744 ASSERT_TRUE(reader);
745 bool has_sample = false;
746 ASSERT_TRUE(reader->ReadDataSection([&](std::unique_ptr<Record> r) {
747 if (r->type() == PERF_RECORD_SAMPLE && r->InKernel()) {
748 SampleRecord* sr = static_cast<SampleRecord*>(r.get());
749 if (sr->stack_user_data.dyn_size == 0) {
750 return false;
751 }
752 has_sample = true;
753 }
754 return true;
755 }));
756 ASSERT_TRUE(has_sample);
757 }
758
759 // @CddTest = 6.1/C-0-2
TEST(record_cmd,cpu_percent_option)760 TEST(record_cmd, cpu_percent_option) {
761 ASSERT_TRUE(RunRecordCmd({"--cpu-percent", "50"}));
762 ASSERT_FALSE(RunRecordCmd({"--cpu-percent", "0"}));
763 ASSERT_FALSE(RunRecordCmd({"--cpu-percent", "101"}));
764 }
765
766 class RecordingAppHelper {
767 public:
InstallApk(const std::string & apk_path,const std::string & package_name)768 bool InstallApk(const std::string& apk_path, const std::string& package_name) {
769 return app_helper_.InstallApk(apk_path, package_name);
770 }
771
StartApp(const std::string & start_cmd)772 bool StartApp(const std::string& start_cmd) { return app_helper_.StartApp(start_cmd); }
773
RecordData(const std::string & record_cmd)774 bool RecordData(const std::string& record_cmd) {
775 std::vector<std::string> args = android::base::Split(record_cmd, " ");
776 // record_cmd may end with child command. We should put output options before it.
777 args.emplace(args.begin(), "-o");
778 args.emplace(args.begin() + 1, GetDataPath());
779 return RecordCmd()->Run(args);
780 }
781
CheckData(const std::function<bool (const char *)> & process_symbol)782 bool CheckData(const std::function<bool(const char*)>& process_symbol) {
783 bool success = false;
784 auto callback = [&](const Symbol& symbol, uint32_t) {
785 if (process_symbol(symbol.DemangledName())) {
786 success = true;
787 }
788 return success;
789 };
790 ProcessSymbolsInPerfDataFile(GetDataPath(), callback);
791 size_t sample_count = GetSampleCount();
792 if (!success) {
793 if (IsInEmulator()) {
794 // In emulator, the monitored app may not have a chance to run.
795 constexpr size_t MIN_SAMPLES_TO_CHECK_SYMBOLS = 1000;
796 if (size_t sample_count = GetSampleCount(); sample_count < MIN_SAMPLES_TO_CHECK_SYMBOLS) {
797 GTEST_LOG_(INFO) << "Only " << sample_count
798 << " samples recorded in the emulator. Skip checking symbols (need "
799 << MIN_SAMPLES_TO_CHECK_SYMBOLS << " samples).";
800 return true;
801 }
802 }
803 DumpData();
804 }
805 return success;
806 }
807
DumpData()808 void DumpData() { CreateCommandInstance("report")->Run({"-i", GetDataPath()}); }
809
GetDataPath() const810 std::string GetDataPath() const { return perf_data_file_.path; }
811
812 private:
GetSampleCount()813 size_t GetSampleCount() {
814 size_t sample_count = 0;
815 std::unique_ptr<RecordFileReader> reader = RecordFileReader::CreateInstance(GetDataPath());
816 if (!reader) {
817 return sample_count;
818 }
819 auto process_record = [&](std::unique_ptr<Record> r) {
820 if (r->type() == PERF_RECORD_SAMPLE) {
821 sample_count++;
822 }
823 return true;
824 };
825 if (!reader->ReadDataSection(process_record)) {
826 return sample_count;
827 }
828 return sample_count;
829 }
830
831 AppHelper app_helper_;
832 TemporaryFile perf_data_file_;
833 };
834
TestRecordingApps(const std::string & app_name,const std::string & app_type)835 static void TestRecordingApps(const std::string& app_name, const std::string& app_type) {
836 RecordingAppHelper helper;
837 // Bring the app to foreground to avoid no samples.
838 ASSERT_TRUE(helper.StartApp("am start " + app_name + "/.MainActivity"));
839
840 ASSERT_TRUE(helper.RecordData("--app " + app_name + " -g --duration 10 -e " + GetDefaultEvent()));
841
842 // Check if we can profile Java code by looking for a Java method name in dumped symbols, which
843 // is app_name + ".MainActivity$1.run".
844 const std::string expected_class_name = app_name + ".MainActivity";
845 const std::string expected_method_name = "run";
846 auto process_symbol = [&](const char* name) {
847 return strstr(name, expected_class_name.c_str()) != nullptr &&
848 strstr(name, expected_method_name.c_str()) != nullptr;
849 };
850 ASSERT_TRUE(helper.CheckData(process_symbol));
851
852 // Check app_package_name and app_type.
853 auto reader = RecordFileReader::CreateInstance(helper.GetDataPath());
854 ASSERT_TRUE(reader);
855 const std::unordered_map<std::string, std::string>& meta_info = reader->GetMetaInfoFeature();
856 auto it = meta_info.find("app_package_name");
857 ASSERT_NE(it, meta_info.end());
858 ASSERT_EQ(it->second, app_name);
859 it = meta_info.find("app_type");
860 ASSERT_NE(it, meta_info.end());
861 ASSERT_EQ(it->second, app_type);
862 reader.reset(nullptr);
863
864 // Check that simpleperf can't execute child command in app uid.
865 if (!IsRoot()) {
866 ASSERT_FALSE(helper.RecordData("--app " + app_name + " -e " + GetDefaultEvent() + " sleep 1"));
867 }
868 }
869
870 // @CddTest = 6.1/C-0-2
TEST(record_cmd,app_option_for_debuggable_app)871 TEST(record_cmd, app_option_for_debuggable_app) {
872 OMIT_TEST_ON_NON_NATIVE_ABIS();
873 TEST_REQUIRE_APPS();
874 SetRunInAppToolForTesting(true, false);
875 TestRecordingApps("com.android.simpleperf.debuggable", "debuggable");
876 SetRunInAppToolForTesting(false, true);
877 TestRecordingApps("com.android.simpleperf.debuggable", "debuggable");
878 }
879
880 // @CddTest = 6.1/C-0-2
TEST(record_cmd,app_option_for_profileable_app)881 TEST(record_cmd, app_option_for_profileable_app) {
882 OMIT_TEST_ON_NON_NATIVE_ABIS();
883 TEST_REQUIRE_APPS();
884 SetRunInAppToolForTesting(false, true);
885 TestRecordingApps("com.android.simpleperf.profileable", "profileable");
886 }
887
888 #if defined(__ANDROID__)
RecordJavaApp(RecordingAppHelper & helper)889 static void RecordJavaApp(RecordingAppHelper& helper) {
890 // 1. Install apk.
891 ASSERT_TRUE(helper.InstallApk(GetTestData("DisplayBitmaps.apk"),
892 "com.example.android.displayingbitmaps"));
893 ASSERT_TRUE(helper.InstallApk(GetTestData("DisplayBitmapsTest.apk"),
894 "com.example.android.displayingbitmaps.test"));
895
896 // 2. Start the app.
897 ASSERT_TRUE(
898 helper.StartApp("am instrument -w -r -e debug false -e class "
899 "com.example.android.displayingbitmaps.tests.GridViewTest "
900 "com.example.android.displayingbitmaps.test/"
901 "androidx.test.runner.AndroidJUnitRunner"));
902
903 // 3. Record perf.data.
904 SetRunInAppToolForTesting(true, true);
905 ASSERT_TRUE(helper.RecordData(
906 "-e cpu-clock --app com.example.android.displayingbitmaps -g --duration 15"));
907 }
908 #endif // defined(__ANDROID__)
909
910 // @CddTest = 6.1/C-0-2
TEST(record_cmd,record_java_app)911 TEST(record_cmd, record_java_app) {
912 #if defined(__ANDROID__)
913 OMIT_TEST_ON_NON_NATIVE_ABIS();
914 RecordingAppHelper helper;
915
916 RecordJavaApp(helper);
917 if (HasFailure()) {
918 return;
919 }
920
921 // Check perf.data by looking for java symbols.
922 const char* java_symbols[] = {
923 "androidx.test.runner",
924 "androidx.test.espresso",
925 "android.app.ActivityThread.main",
926 };
927 auto process_symbol = [&](const char* name) {
928 for (const char* java_symbol : java_symbols) {
929 if (strstr(name, java_symbol) != nullptr) {
930 return true;
931 }
932 }
933 return false;
934 };
935 ASSERT_TRUE(helper.CheckData(process_symbol));
936 #else
937 GTEST_LOG_(INFO) << "This test tests a function only available on Android.";
938 #endif
939 }
940
941 // @CddTest = 6.1/C-0-2
TEST(record_cmd,record_native_app)942 TEST(record_cmd, record_native_app) {
943 #if defined(__ANDROID__)
944 // In case of non-native ABI guest symbols are never directly executed, thus
945 // don't appear in perf.data. Instead binary translator executes code
946 // translated from guest at runtime.
947 OMIT_TEST_ON_NON_NATIVE_ABIS();
948
949 RecordingAppHelper helper;
950 // 1. Install apk.
951 ASSERT_TRUE(helper.InstallApk(GetTestData("EndlessTunnel.apk"), "com.google.sample.tunnel"));
952
953 // 2. Start the app.
954 ASSERT_TRUE(
955 helper.StartApp("am start -n com.google.sample.tunnel/android.app.NativeActivity -a "
956 "android.intent.action.MAIN -c android.intent.category.LAUNCHER"));
957
958 // 3. Record perf.data.
959 SetRunInAppToolForTesting(true, true);
960 ASSERT_TRUE(helper.RecordData("-e cpu-clock --app com.google.sample.tunnel -g --duration 10"));
961
962 // 4. Check perf.data.
963 auto process_symbol = [&](const char* name) {
964 const char* expected_name_with_keyguard = "NativeActivity"; // when screen is locked
965 if (strstr(name, expected_name_with_keyguard) != nullptr) {
966 return true;
967 }
968 const char* expected_name = "PlayScene::DoFrame"; // when screen is awake
969 return strstr(name, expected_name) != nullptr;
970 };
971 ASSERT_TRUE(helper.CheckData(process_symbol));
972 #else
973 GTEST_LOG_(INFO) << "This test tests a function only available on Android.";
974 #endif
975 }
976
977 // @CddTest = 6.1/C-0-2
TEST(record_cmd,check_trampoline_after_art_jni_methods)978 TEST(record_cmd, check_trampoline_after_art_jni_methods) {
979 // Test if art jni methods are called by art_jni_trampoline.
980 #if defined(__ANDROID__)
981 OMIT_TEST_ON_NON_NATIVE_ABIS();
982 RecordingAppHelper helper;
983
984 RecordJavaApp(helper);
985 if (HasFailure()) {
986 return;
987 }
988
989 // Check if art::Method_invoke() is called by art_jni_trampoline.
990 auto reader = RecordFileReader::CreateInstance(helper.GetDataPath());
991 ASSERT_TRUE(reader);
992 ThreadTree thread_tree;
993 ASSERT_TRUE(reader->LoadBuildIdAndFileFeatures(thread_tree));
994
995 auto get_symbol_name = [&](ThreadEntry* thread, uint64_t ip) -> std::string {
996 const MapEntry* map = thread_tree.FindMap(thread, ip, false);
997 const Symbol* symbol = thread_tree.FindSymbol(map, ip, nullptr, nullptr);
998 return symbol->DemangledName();
999 };
1000
1001 bool has_check = false;
1002
1003 auto process_record = [&](std::unique_ptr<Record> r) {
1004 thread_tree.Update(*r);
1005 if (r->type() == PERF_RECORD_SAMPLE) {
1006 auto sample = static_cast<SampleRecord*>(r.get());
1007 ThreadEntry* thread = thread_tree.FindThreadOrNew(sample->tid_data.pid, sample->tid_data.tid);
1008 size_t kernel_ip_count;
1009 std::vector<uint64_t> ips = sample->GetCallChain(&kernel_ip_count);
1010 for (size_t i = kernel_ip_count; i < ips.size(); i++) {
1011 std::string sym_name = get_symbol_name(thread, ips[i]);
1012 if (android::base::StartsWith(sym_name, "art::Method_invoke") && i + 1 < ips.size()) {
1013 has_check = true;
1014 std::string name = get_symbol_name(thread, ips[i + 1]);
1015 if (android::base::EndsWith(name, "jni_trampoline")) {
1016 continue;
1017 }
1018 // When the jni_trampoline function is from JIT cache, we may not get map info in time.
1019 // To avoid test flakiness, we accept this.
1020 // Case 1: It doesn't hit any maps.
1021 if (name == "unknown") {
1022 continue;
1023 }
1024 // Case 2: It hits an old map for JIT cache.
1025 if (const MapEntry* map = thread_tree.FindMap(thread, ips[i + 1], false);
1026 JITDebugReader::IsPathInJITSymFile(map->dso->Path())) {
1027 continue;
1028 }
1029
1030 GTEST_LOG_(ERROR) << "unexpected symbol after art::Method_invoke: " << name;
1031 return false;
1032 }
1033 }
1034 }
1035 return true;
1036 };
1037 ASSERT_TRUE(reader->ReadDataSection(process_record));
1038 ASSERT_TRUE(has_check);
1039 #else
1040 GTEST_LOG_(INFO) << "This test tests a function only available on Android.";
1041 #endif
1042 }
1043
1044 // @CddTest = 6.1/C-0-2
TEST(record_cmd,no_cut_samples_option)1045 TEST(record_cmd, no_cut_samples_option) {
1046 ASSERT_TRUE(RunRecordCmd({"--no-cut-samples"}));
1047 }
1048
1049 // @CddTest = 6.1/C-0-2
TEST(record_cmd,cs_etm_event)1050 TEST(record_cmd, cs_etm_event) {
1051 if (!ETMRecorder::GetInstance().CheckEtmSupport().ok()) {
1052 GTEST_LOG_(INFO) << "Omit this test since etm isn't supported on this device";
1053 return;
1054 }
1055 TemporaryFile tmpfile;
1056 ASSERT_TRUE(RunRecordCmd({"-e", "cs-etm"}, tmpfile.path));
1057 std::unique_ptr<RecordFileReader> reader = RecordFileReader::CreateInstance(tmpfile.path);
1058 ASSERT_TRUE(reader);
1059
1060 // cs-etm uses sample period instead of sample freq.
1061 ASSERT_EQ(reader->AttrSection().size(), 1u);
1062 const perf_event_attr& attr = reader->AttrSection()[0].attr;
1063 ASSERT_EQ(attr.freq, 0);
1064 ASSERT_EQ(attr.sample_period, 1);
1065
1066 bool has_auxtrace_info = false;
1067 bool has_auxtrace = false;
1068 bool has_aux = false;
1069 ASSERT_TRUE(reader->ReadDataSection([&](std::unique_ptr<Record> r) {
1070 if (r->type() == PERF_RECORD_AUXTRACE_INFO) {
1071 has_auxtrace_info = true;
1072 } else if (r->type() == PERF_RECORD_AUXTRACE) {
1073 has_auxtrace = true;
1074 } else if (r->type() == PERF_RECORD_AUX) {
1075 has_aux = true;
1076 }
1077 return true;
1078 }));
1079 ASSERT_TRUE(has_auxtrace_info);
1080 ASSERT_TRUE(has_auxtrace);
1081 ASSERT_TRUE(has_aux);
1082 ASSERT_TRUE(!reader->ReadBuildIdFeature().empty());
1083 }
1084
1085 // @CddTest = 6.1/C-0-2
TEST(record_cmd,cs_etm_system_wide)1086 TEST(record_cmd, cs_etm_system_wide) {
1087 TEST_REQUIRE_ROOT();
1088 if (!ETMRecorder::GetInstance().CheckEtmSupport().ok()) {
1089 GTEST_LOG_(INFO) << "Omit this test since etm isn't supported on this device";
1090 return;
1091 }
1092 TemporaryFile tmpfile;
1093 ASSERT_TRUE(RunRecordCmd({"-e", "cs-etm", "-a"}, tmpfile.path));
1094 // Check if build ids are dumped.
1095 std::unique_ptr<RecordFileReader> reader = RecordFileReader::CreateInstance(tmpfile.path);
1096 ASSERT_TRUE(reader);
1097
1098 bool has_kernel_build_id = false;
1099 for (const auto& build_id_record : reader->ReadBuildIdFeature()) {
1100 if (strcmp(build_id_record.filename, DEFAULT_KERNEL_MMAP_NAME) == 0) {
1101 has_kernel_build_id = true;
1102 break;
1103 }
1104 }
1105 ASSERT_TRUE(has_kernel_build_id);
1106
1107 // build ids are not dumped if --no-dump-build-id is used.
1108 ASSERT_TRUE(RunRecordCmd({"-e", "cs-etm", "-a", "--no-dump-build-id"}, tmpfile.path));
1109 reader = RecordFileReader::CreateInstance(tmpfile.path);
1110 ASSERT_TRUE(reader);
1111 ASSERT_TRUE(reader->ReadBuildIdFeature().empty());
1112 }
1113
1114 // @CddTest = 6.1/C-0-2
TEST(record_cmd,aux_buffer_size_option)1115 TEST(record_cmd, aux_buffer_size_option) {
1116 if (!ETMRecorder::GetInstance().CheckEtmSupport().ok()) {
1117 GTEST_LOG_(INFO) << "Omit this test since etm isn't supported on this device";
1118 return;
1119 }
1120 ASSERT_TRUE(RunRecordCmd({"-e", "cs-etm", "--aux-buffer-size", "1m"}));
1121 // not page size aligned
1122 ASSERT_FALSE(RunRecordCmd({"-e", "cs-etm", "--aux-buffer-size", "1024"}));
1123 // not power of two
1124 ASSERT_FALSE(RunRecordCmd({"-e", "cs-etm", "--aux-buffer-size", "12k"}));
1125 }
1126
1127 // @CddTest = 6.1/C-0-2
TEST(record_cmd,addr_filter_option)1128 TEST(record_cmd, addr_filter_option) {
1129 TEST_REQUIRE_HW_COUNTER();
1130 if (!ETMRecorder::GetInstance().CheckEtmSupport().ok()) {
1131 GTEST_LOG_(INFO) << "Omit this test since etm isn't supported on this device";
1132 return;
1133 }
1134 FILE* fp = popen("which sleep", "r");
1135 ASSERT_TRUE(fp != nullptr);
1136 std::string path;
1137 ASSERT_TRUE(android::base::ReadFdToString(fileno(fp), &path));
1138 pclose(fp);
1139 path = android::base::Trim(path);
1140 std::string sleep_exec_path;
1141 ASSERT_TRUE(Realpath(path, &sleep_exec_path));
1142 // --addr-filter doesn't apply to cpu-cycles.
1143 ASSERT_FALSE(RunRecordCmd({"--addr-filter", "filter " + sleep_exec_path}));
1144 TemporaryFile record_file;
1145 ASSERT_TRUE(RunRecordCmd({"-e", "cs-etm", "--addr-filter", "filter " + sleep_exec_path},
1146 record_file.path));
1147 TemporaryFile inject_file;
1148 ASSERT_TRUE(
1149 CreateCommandInstance("inject")->Run({"-i", record_file.path, "-o", inject_file.path}));
1150 std::string data;
1151 ASSERT_TRUE(android::base::ReadFileToString(inject_file.path, &data));
1152 // Only instructions in sleep_exec_path are traced.
1153 for (auto& line : android::base::Split(data, "\n")) {
1154 if (android::base::StartsWith(line, "dso ")) {
1155 std::string dso = line.substr(strlen("dso "), sleep_exec_path.size());
1156 ASSERT_EQ(dso, sleep_exec_path);
1157 }
1158 }
1159
1160 // Test if different filter types are accepted by the kernel.
1161 auto elf = ElfFile::Open(sleep_exec_path);
1162 uint64_t off;
1163 uint64_t addr = elf->ReadMinExecutableVaddr(&off);
1164 // file start
1165 std::string filter = StringPrintf("start 0x%" PRIx64 "@%s", addr, sleep_exec_path.c_str());
1166 ASSERT_TRUE(RunRecordCmd({"-e", "cs-etm", "--addr-filter", filter}));
1167 // file stop
1168 filter = StringPrintf("stop 0x%" PRIx64 "@%s", addr, sleep_exec_path.c_str());
1169 ASSERT_TRUE(RunRecordCmd({"-e", "cs-etm", "--addr-filter", filter}));
1170 // file range
1171 filter = StringPrintf("filter 0x%" PRIx64 "-0x%" PRIx64 "@%s", addr, addr + 4,
1172 sleep_exec_path.c_str());
1173 ASSERT_TRUE(RunRecordCmd({"-e", "cs-etm", "--addr-filter", filter}));
1174 // If kernel panic, try backporting "perf/core: Fix crash when using HW tracing kernel
1175 // filters".
1176 // kernel start
1177 uint64_t fake_kernel_addr = (1ULL << 63);
1178 filter = StringPrintf("start 0x%" PRIx64, fake_kernel_addr);
1179 ASSERT_TRUE(RunRecordCmd({"-e", "cs-etm", "--addr-filter", filter}));
1180 // kernel stop
1181 filter = StringPrintf("stop 0x%" PRIx64, fake_kernel_addr);
1182 ASSERT_TRUE(RunRecordCmd({"-e", "cs-etm", "--addr-filter", filter}));
1183 // kernel range
1184 filter = StringPrintf("filter 0x%" PRIx64 "-0x%" PRIx64, fake_kernel_addr, fake_kernel_addr + 4);
1185 ASSERT_TRUE(RunRecordCmd({"-e", "cs-etm", "--addr-filter", filter}));
1186 }
1187
1188 // @CddTest = 6.1/C-0-2
TEST(record_cmd,decode_etm_option)1189 TEST(record_cmd, decode_etm_option) {
1190 if (!ETMRecorder::GetInstance().CheckEtmSupport().ok()) {
1191 GTEST_LOG_(INFO) << "Omit this test since etm isn't supported on this device";
1192 return;
1193 }
1194 ASSERT_TRUE(RunRecordCmd({"-e", "cs-etm", "--decode-etm"}));
1195 ASSERT_TRUE(RunRecordCmd({"-e", "cs-etm", "--decode-etm", "--exclude-perf"}));
1196 }
1197
1198 // @CddTest = 6.1/C-0-2
TEST(record_cmd,record_timestamp)1199 TEST(record_cmd, record_timestamp) {
1200 if (!ETMRecorder::GetInstance().CheckEtmSupport().ok()) {
1201 GTEST_LOG_(INFO) << "Omit this test since etm isn't supported on this device";
1202 return;
1203 }
1204 ASSERT_TRUE(RunRecordCmd({"-e", "cs-etm", "--record-timestamp"}));
1205 }
1206
1207 // @CddTest = 6.1/C-0-2
TEST(record_cmd,record_cycles)1208 TEST(record_cmd, record_cycles) {
1209 if (!ETMRecorder::GetInstance().CheckEtmSupport().ok()) {
1210 GTEST_LOG_(INFO) << "Omit this test since etm isn't supported on this device";
1211 return;
1212 }
1213 ASSERT_TRUE(RunRecordCmd({"-e", "cs-etm", "--record-cycles"}));
1214 }
1215
1216 // @CddTest = 6.1/C-0-2
TEST(record_cmd,cycle_threshold)1217 TEST(record_cmd, cycle_threshold) {
1218 if (!ETMRecorder::GetInstance().CheckEtmSupport().ok()) {
1219 GTEST_LOG_(INFO) << "Omit this test since etm isn't supported on this device";
1220 return;
1221 }
1222 ASSERT_TRUE(RunRecordCmd({"-e", "cs-etm", "--record-cycles", "--cycle-threshold", "8"}));
1223 }
1224
1225 // @CddTest = 6.1/C-0-2
TEST(record_cmd,binary_option)1226 TEST(record_cmd, binary_option) {
1227 if (!ETMRecorder::GetInstance().CheckEtmSupport().ok()) {
1228 GTEST_LOG_(INFO) << "Omit this test since etm isn't supported on this device";
1229 return;
1230 }
1231 ASSERT_TRUE(RunRecordCmd({"-e", "cs-etm", "--decode-etm", "--binary", ".*"}));
1232 }
1233
1234 // @CddTest = 6.1/C-0-2
TEST(record_cmd,etm_flush_interval_option)1235 TEST(record_cmd, etm_flush_interval_option) {
1236 if (!ETMRecorder::GetInstance().CheckEtmSupport().ok()) {
1237 GTEST_LOG_(INFO) << "Omit this test since etm isn't supported on this device";
1238 return;
1239 }
1240 ASSERT_TRUE(RunRecordCmd({"-e", "cs-etm", "--etm-flush-interval", "10"}));
1241 }
1242
1243 // @CddTest = 6.1/C-0-2
TEST(record_cmd,pmu_event_option)1244 TEST(record_cmd, pmu_event_option) {
1245 TEST_REQUIRE_PMU_COUNTER();
1246 TEST_REQUIRE_HW_COUNTER();
1247 std::string event_string;
1248 if (GetTargetArch() == ARCH_X86_64) {
1249 event_string = "cpu/cpu-cycles/";
1250 } else if (GetTargetArch() == ARCH_ARM64) {
1251 event_string = "armv8_pmuv3/cpu_cycles/";
1252 } else {
1253 GTEST_LOG_(INFO) << "Omit arch " << GetTargetArch();
1254 return;
1255 }
1256 TEST_IN_ROOT(ASSERT_TRUE(RunRecordCmd({"-e", event_string})));
1257 }
1258
1259 // @CddTest = 6.1/C-0-2
TEST(record_cmd,exclude_perf_option)1260 TEST(record_cmd, exclude_perf_option) {
1261 ASSERT_TRUE(RunRecordCmd({"--exclude-perf"}));
1262 if (IsRoot()) {
1263 TemporaryFile tmpfile;
1264 ASSERT_TRUE(RecordCmd()->Run(
1265 {"-a", "--exclude-perf", "--duration", "1", "-e", GetDefaultEvent(), "-o", tmpfile.path}));
1266 std::unique_ptr<RecordFileReader> reader = RecordFileReader::CreateInstance(tmpfile.path);
1267 ASSERT_TRUE(reader);
1268 pid_t perf_pid = getpid();
1269 ASSERT_TRUE(reader->ReadDataSection([&](std::unique_ptr<Record> r) {
1270 if (r->type() == PERF_RECORD_SAMPLE) {
1271 if (static_cast<SampleRecord*>(r.get())->tid_data.pid == perf_pid) {
1272 return false;
1273 }
1274 }
1275 return true;
1276 }));
1277 }
1278 }
1279
1280 // @CddTest = 6.1/C-0-2
TEST(record_cmd,tp_filter_option)1281 TEST(record_cmd, tp_filter_option) {
1282 TEST_REQUIRE_HOST_ROOT();
1283 TEST_REQUIRE_TRACEPOINT_EVENTS();
1284 // Test string operands both with quotes and without quotes.
1285 for (const auto& filter :
1286 std::vector<std::string>({"prev_comm != 'sleep'", "prev_comm != sleep"})) {
1287 TemporaryFile tmpfile;
1288 ASSERT_TRUE(RunRecordCmd({"-e", "sched:sched_switch", "--tp-filter", filter}, tmpfile.path))
1289 << filter;
1290 CaptureStdout capture;
1291 ASSERT_TRUE(capture.Start());
1292 ASSERT_TRUE(CreateCommandInstance("dump")->Run({tmpfile.path}));
1293 std::string data = capture.Finish();
1294 // Check that samples with prev_comm == sleep are filtered out. Although we do the check all the
1295 // time, it only makes sense when running as root. Tracepoint event fields are not allowed
1296 // to record unless running as root.
1297 ASSERT_EQ(data.find("prev_comm: sleep"), std::string::npos) << filter;
1298 }
1299 }
1300
1301 // @CddTest = 6.1/C-0-2
TEST(record_cmd,ParseAddrFilterOption)1302 TEST(record_cmd, ParseAddrFilterOption) {
1303 auto option_to_str = [](const std::string& option) {
1304 auto filters = ParseAddrFilterOption(option);
1305 std::string s;
1306 for (auto& filter : filters) {
1307 if (!s.empty()) {
1308 s += ',';
1309 }
1310 s += filter.ToString();
1311 }
1312 return s;
1313 };
1314 std::string path;
1315 ASSERT_TRUE(Realpath(GetTestData(ELF_FILE), &path));
1316
1317 // Test file filters.
1318 ASSERT_EQ(option_to_str("filter " + path), "filter 0x0/0x73c@" + path);
1319 ASSERT_EQ(option_to_str("filter 0x400502-0x400527@" + path), "filter 0x502/0x25@" + path);
1320 ASSERT_EQ(option_to_str("start 0x400502@" + path + ",stop 0x400527@" + path),
1321 "start 0x502@" + path + ",stop 0x527@" + path);
1322
1323 // Test '-' in file path. Create a temporary file with '-' in name.
1324 TemporaryDir tmpdir;
1325 fs::path tmpfile = fs::path(tmpdir.path) / "elf-with-hyphen";
1326 ASSERT_TRUE(fs::copy_file(path, tmpfile));
1327 ASSERT_EQ(option_to_str("filter " + tmpfile.string()), "filter 0x0/0x73c@" + tmpfile.string());
1328
1329 // Test kernel filters.
1330 ASSERT_EQ(option_to_str("filter 0x12345678-0x1234567a"), "filter 0x12345678/0x2");
1331 ASSERT_EQ(option_to_str("start 0x12345678,stop 0x1234567a"), "start 0x12345678,stop 0x1234567a");
1332 }
1333
1334 // @CddTest = 6.1/C-0-2
TEST(record_cmd,kprobe_option)1335 TEST(record_cmd, kprobe_option) {
1336 TEST_REQUIRE_ROOT();
1337 EventSelectionSet event_selection_set(false);
1338 ProbeEvents probe_events(event_selection_set);
1339 if (!probe_events.IsKprobeSupported()) {
1340 GTEST_LOG_(INFO) << "Skip this test as kprobe isn't supported by the kernel.";
1341 return;
1342 }
1343 ASSERT_TRUE(RunRecordCmd({"-e", "kprobes:myprobe", "--kprobe", "p:myprobe do_sys_openat2"}));
1344 // A default kprobe event is created if not given an explicit --kprobe option.
1345 ASSERT_TRUE(RunRecordCmd({"-e", "kprobes:do_sys_openat2"}));
1346 ASSERT_TRUE(RunRecordCmd({"--group", "kprobes:do_sys_openat2"}));
1347 }
1348
1349 // @CddTest = 6.1/C-0-2
TEST(record_cmd,record_filter_options)1350 TEST(record_cmd, record_filter_options) {
1351 ASSERT_TRUE(
1352 RunRecordCmd({"--exclude-pid", "1,2", "--exclude-tid", "3,4", "--exclude-process-name",
1353 "processA", "--exclude-thread-name", "threadA", "--exclude-uid", "5,6"}));
1354 ASSERT_TRUE(
1355 RunRecordCmd({"--include-pid", "1,2", "--include-tid", "3,4", "--include-process-name",
1356 "processB", "--include-thread-name", "threadB", "--include-uid", "5,6"}));
1357 }
1358
1359 // @CddTest = 6.1/C-0-2
TEST(record_cmd,keep_failed_unwinding_result_option)1360 TEST(record_cmd, keep_failed_unwinding_result_option) {
1361 OMIT_TEST_ON_NON_NATIVE_ABIS();
1362 std::vector<std::unique_ptr<Workload>> workloads;
1363 CreateProcesses(1, &workloads);
1364 std::string pid = std::to_string(workloads[0]->GetPid());
1365 ASSERT_TRUE(RunRecordCmd(
1366 {"-p", pid, "-g", "--keep-failed-unwinding-result", "--keep-failed-unwinding-debug-info"}));
1367 }
1368
1369 // @CddTest = 6.1/C-0-2
TEST(record_cmd,kernel_address_warning)1370 TEST(record_cmd, kernel_address_warning) {
1371 TEST_REQUIRE_NON_ROOT();
1372 const std::string warning_msg = "Access to kernel symbol addresses is restricted.";
1373 CapturedStderr capture;
1374
1375 // When excluding kernel samples, no kernel address warning is printed.
1376 ResetKernelAddressWarning();
1377 TemporaryFile tmpfile;
1378 ASSERT_TRUE(RunRecordCmd({"-e", "cpu-clock:u"}, tmpfile.path));
1379 capture.Stop();
1380 ASSERT_EQ(capture.str().find(warning_msg), std::string::npos);
1381
1382 // When not excluding kernel samples, kernel address warning is printed once.
1383 capture.Reset();
1384 capture.Start();
1385 ResetKernelAddressWarning();
1386 ASSERT_TRUE(RunRecordCmd({"-e", "cpu-clock"}, tmpfile.path));
1387 capture.Stop();
1388 std::string output = capture.str();
1389 auto pos = output.find(warning_msg);
1390 ASSERT_NE(pos, std::string::npos);
1391 ASSERT_EQ(output.find(warning_msg, pos + warning_msg.size()), std::string::npos);
1392 }
1393
1394 // @CddTest = 6.1/C-0-2
TEST(record_cmd,add_meta_info_option)1395 TEST(record_cmd, add_meta_info_option) {
1396 TemporaryFile tmpfile;
1397 ASSERT_TRUE(RunRecordCmd({"--add-meta-info", "key1=value1", "--add-meta-info", "key2=value2"},
1398 tmpfile.path));
1399 auto reader = RecordFileReader::CreateInstance(tmpfile.path);
1400 ASSERT_TRUE(reader);
1401
1402 const std::unordered_map<std::string, std::string>& meta_info = reader->GetMetaInfoFeature();
1403 auto it = meta_info.find("key1");
1404 ASSERT_NE(it, meta_info.end());
1405 ASSERT_EQ(it->second, "value1");
1406 it = meta_info.find("key2");
1407 ASSERT_NE(it, meta_info.end());
1408 ASSERT_EQ(it->second, "value2");
1409
1410 // Report error for invalid meta info.
1411 ASSERT_FALSE(RunRecordCmd({"--add-meta-info", "key1"}, tmpfile.path));
1412 ASSERT_FALSE(RunRecordCmd({"--add-meta-info", "key1="}, tmpfile.path));
1413 ASSERT_FALSE(RunRecordCmd({"--add-meta-info", "=value1"}, tmpfile.path));
1414 }
1415
1416 // @CddTest = 6.1/C-0-2
TEST(record_cmd,device_meta_info)1417 TEST(record_cmd, device_meta_info) {
1418 #if defined(__ANDROID__)
1419 TemporaryFile tmpfile;
1420 ASSERT_TRUE(RunRecordCmd({}, tmpfile.path));
1421 auto reader = RecordFileReader::CreateInstance(tmpfile.path);
1422 ASSERT_TRUE(reader);
1423
1424 const std::unordered_map<std::string, std::string>& meta_info = reader->GetMetaInfoFeature();
1425 auto it = meta_info.find("android_sdk_version");
1426 ASSERT_NE(it, meta_info.end());
1427 ASSERT_FALSE(it->second.empty());
1428 it = meta_info.find("android_build_type");
1429 ASSERT_NE(it, meta_info.end());
1430 ASSERT_FALSE(it->second.empty());
1431 #else
1432 GTEST_LOG_(INFO) << "This test tests a function only available on Android.";
1433 #endif
1434 }
1435
1436 // @CddTest = 6.1/C-0-2
TEST(record_cmd,add_counter_option)1437 TEST(record_cmd, add_counter_option) {
1438 TEST_REQUIRE_HW_COUNTER();
1439 TemporaryFile tmpfile;
1440 ASSERT_TRUE(RecordCmd()->Run({"-e", "cpu-cycles", "--add-counter", "instructions", "--no-inherit",
1441 "-o", tmpfile.path, "sleep", "1"}));
1442 std::unique_ptr<RecordFileReader> reader = RecordFileReader::CreateInstance(tmpfile.path);
1443 ASSERT_TRUE(reader);
1444 bool has_sample = false;
1445 ASSERT_TRUE(reader->ReadDataSection([&](std::unique_ptr<Record> r) {
1446 if (r->type() == PERF_RECORD_SAMPLE) {
1447 has_sample = true;
1448 auto sr = static_cast<SampleRecord*>(r.get());
1449 if (sr->read_data.counts.size() != 2 || sr->read_data.ids.size() != 2) {
1450 return false;
1451 }
1452 }
1453 return true;
1454 }));
1455 ASSERT_TRUE(has_sample);
1456 }
1457
1458 // @CddTest = 6.1/C-0-2
TEST(record_cmd,user_buffer_size_option)1459 TEST(record_cmd, user_buffer_size_option) {
1460 ASSERT_TRUE(RunRecordCmd({"--user-buffer-size", "256M"}));
1461 }
1462
1463 // @CddTest = 6.1/C-0-2
TEST(record_cmd,record_process_name)1464 TEST(record_cmd, record_process_name) {
1465 TemporaryFile tmpfile;
1466 ASSERT_TRUE(RecordCmd()->Run({"-e", GetDefaultEvent(), "-o", tmpfile.path, "sleep", SLEEP_SEC}));
1467 std::unique_ptr<RecordFileReader> reader = RecordFileReader::CreateInstance(tmpfile.path);
1468 ASSERT_TRUE(reader);
1469 bool has_comm = false;
1470 ASSERT_TRUE(reader->ReadDataSection([&](std::unique_ptr<Record> r) {
1471 if (r->type() == PERF_RECORD_COMM) {
1472 CommRecord* cr = static_cast<CommRecord*>(r.get());
1473 if (strcmp(cr->comm, "sleep") == 0) {
1474 has_comm = true;
1475 }
1476 }
1477 return true;
1478 }));
1479 ASSERT_TRUE(has_comm);
1480 }
1481
1482 // @CddTest = 6.1/C-0-2
TEST(record_cmd,delay_option)1483 TEST(record_cmd, delay_option) {
1484 TemporaryFile tmpfile;
1485 ASSERT_TRUE(RecordCmd()->Run(
1486 {"-o", tmpfile.path, "-e", GetDefaultEvent(), "--delay", "100", "sleep", "1"}));
1487 }
1488
1489 // @CddTest = 6.1/C-0-2
TEST(record_cmd,compression_option)1490 TEST(record_cmd, compression_option) {
1491 TemporaryFile tmpfile;
1492 ASSERT_TRUE(RunRecordCmd({"-z"}, tmpfile.path));
1493 std::unique_ptr<RecordFileReader> reader = RecordFileReader::CreateInstance(tmpfile.path);
1494 ASSERT_TRUE(reader != nullptr);
1495 std::vector<std::unique_ptr<Record>> records = reader->DataSection();
1496 ASSERT_GT(records.size(), 0U);
1497
1498 ASSERT_TRUE(RunRecordCmd({"-z=3"}, tmpfile.path));
1499 }
1500