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>
19 #include <android-base/stringprintf.h>
20 #include <android-base/test_utils.h>
26 #include "environment.h"
27 #include "event_selection_set.h"
28 #include "get_test_data.h"
30 #include "record_file.h"
31 #include "test_util.h"
32 #include "thread_tree.h"
34 using namespace PerfFileFormat;
36 static std::unique_ptr<Command> RecordCmd() {
37 return CreateCommandInstance("record");
40 static bool RunRecordCmd(std::vector<std::string> v,
41 const char* output_file = nullptr) {
42 std::unique_ptr<TemporaryFile> tmpfile;
44 if (output_file != nullptr) {
45 out_file = output_file;
47 tmpfile.reset(new TemporaryFile);
48 out_file = tmpfile->path;
50 v.insert(v.end(), {"-o", out_file, "sleep", SLEEP_SEC});
51 return RecordCmd()->Run(v);
54 TEST(record_cmd, no_options) { ASSERT_TRUE(RunRecordCmd({})); }
56 TEST(record_cmd, system_wide_option) {
57 TEST_IN_ROOT(ASSERT_TRUE(RunRecordCmd({"-a"})));
60 TEST(record_cmd, sample_period_option) {
61 ASSERT_TRUE(RunRecordCmd({"-c", "100000"}));
64 TEST(record_cmd, event_option) {
65 ASSERT_TRUE(RunRecordCmd({"-e", "cpu-clock"}));
68 TEST(record_cmd, freq_option) {
69 ASSERT_TRUE(RunRecordCmd({"-f", "99"}));
70 ASSERT_TRUE(RunRecordCmd({"-F", "99"}));
73 TEST(record_cmd, output_file_option) {
74 TemporaryFile tmpfile;
75 ASSERT_TRUE(RecordCmd()->Run({"-o", tmpfile.path, "sleep", SLEEP_SEC}));
78 TEST(record_cmd, dump_kernel_mmap) {
79 TemporaryFile tmpfile;
80 ASSERT_TRUE(RunRecordCmd({}, tmpfile.path));
81 std::unique_ptr<RecordFileReader> reader =
82 RecordFileReader::CreateInstance(tmpfile.path);
83 ASSERT_TRUE(reader != nullptr);
84 std::vector<std::unique_ptr<Record>> records = reader->DataSection();
85 ASSERT_GT(records.size(), 0U);
86 bool have_kernel_mmap = false;
87 for (auto& record : records) {
88 if (record->type() == PERF_RECORD_MMAP) {
89 const MmapRecord* mmap_record =
90 static_cast<const MmapRecord*>(record.get());
91 if (strcmp(mmap_record->filename, DEFAULT_KERNEL_MMAP_NAME) == 0 ||
92 strcmp(mmap_record->filename, DEFAULT_KERNEL_MMAP_NAME_PERF) == 0) {
93 have_kernel_mmap = true;
98 ASSERT_TRUE(have_kernel_mmap);
101 TEST(record_cmd, dump_build_id_feature) {
102 TemporaryFile tmpfile;
103 ASSERT_TRUE(RunRecordCmd({}, tmpfile.path));
104 std::unique_ptr<RecordFileReader> reader =
105 RecordFileReader::CreateInstance(tmpfile.path);
106 ASSERT_TRUE(reader != nullptr);
107 const FileHeader& file_header = reader->FileHeader();
108 ASSERT_TRUE(file_header.features[FEAT_BUILD_ID / 8] &
109 (1 << (FEAT_BUILD_ID % 8)));
110 ASSERT_GT(reader->FeatureSectionDescriptors().size(), 0u);
113 TEST(record_cmd, tracepoint_event) {
114 TEST_IN_ROOT(ASSERT_TRUE(RunRecordCmd({"-a", "-e", "sched:sched_switch"})));
117 TEST(record_cmd, branch_sampling) {
118 if (IsBranchSamplingSupported()) {
119 ASSERT_TRUE(RunRecordCmd({"-b"}));
120 ASSERT_TRUE(RunRecordCmd({"-j", "any,any_call,any_ret,ind_call"}));
121 ASSERT_TRUE(RunRecordCmd({"-j", "any,k"}));
122 ASSERT_TRUE(RunRecordCmd({"-j", "any,u"}));
123 ASSERT_FALSE(RunRecordCmd({"-j", "u"}));
125 GTEST_LOG_(INFO) << "This test does nothing as branch stack sampling is "
126 "not supported on this device.";
130 TEST(record_cmd, event_modifier) {
131 ASSERT_TRUE(RunRecordCmd({"-e", "cpu-cycles:u"}));
134 TEST(record_cmd, fp_callchain_sampling) {
135 ASSERT_TRUE(RunRecordCmd({"--call-graph", "fp"}));
138 TEST(record_cmd, system_wide_fp_callchain_sampling) {
139 TEST_IN_ROOT(ASSERT_TRUE(RunRecordCmd({"-a", "--call-graph", "fp"})));
142 TEST(record_cmd, dwarf_callchain_sampling) {
143 if (IsDwarfCallChainSamplingSupported()) {
144 ASSERT_TRUE(RunRecordCmd({"--call-graph", "dwarf"}));
145 ASSERT_TRUE(RunRecordCmd({"--call-graph", "dwarf,16384"}));
146 ASSERT_FALSE(RunRecordCmd({"--call-graph", "dwarf,65536"}));
147 ASSERT_TRUE(RunRecordCmd({"-g"}));
149 GTEST_LOG_(INFO) << "This test does nothing as dwarf callchain sampling is "
150 "not supported on this device.";
154 TEST(record_cmd, system_wide_dwarf_callchain_sampling) {
155 if (IsDwarfCallChainSamplingSupported()) {
156 TEST_IN_ROOT(RunRecordCmd({"-a", "--call-graph", "dwarf"}));
158 GTEST_LOG_(INFO) << "This test does nothing as dwarf callchain sampling is "
159 "not supported on this device.";
163 TEST(record_cmd, no_unwind_option) {
164 if (IsDwarfCallChainSamplingSupported()) {
165 ASSERT_TRUE(RunRecordCmd({"--call-graph", "dwarf", "--no-unwind"}));
167 GTEST_LOG_(INFO) << "This test does nothing as dwarf callchain sampling is "
168 "not supported on this device.";
170 ASSERT_FALSE(RunRecordCmd({"--no-unwind"}));
173 TEST(record_cmd, post_unwind_option) {
174 if (IsDwarfCallChainSamplingSupported()) {
175 ASSERT_TRUE(RunRecordCmd({"--call-graph", "dwarf", "--post-unwind"}));
177 GTEST_LOG_(INFO) << "This test does nothing as dwarf callchain sampling is "
178 "not supported on this device.";
180 ASSERT_FALSE(RunRecordCmd({"--post-unwind"}));
182 RunRecordCmd({"--call-graph", "dwarf", "--no-unwind", "--post-unwind"}));
185 TEST(record_cmd, existing_processes) {
186 std::vector<std::unique_ptr<Workload>> workloads;
187 CreateProcesses(2, &workloads);
188 std::string pid_list = android::base::StringPrintf(
189 "%d,%d", workloads[0]->GetPid(), workloads[1]->GetPid());
190 ASSERT_TRUE(RunRecordCmd({"-p", pid_list}));
193 TEST(record_cmd, existing_threads) {
194 std::vector<std::unique_ptr<Workload>> workloads;
195 CreateProcesses(2, &workloads);
196 // Process id can also be used as thread id in linux.
197 std::string tid_list = android::base::StringPrintf(
198 "%d,%d", workloads[0]->GetPid(), workloads[1]->GetPid());
199 ASSERT_TRUE(RunRecordCmd({"-t", tid_list}));
202 TEST(record_cmd, no_monitored_threads) { ASSERT_FALSE(RecordCmd()->Run({""})); }
204 TEST(record_cmd, more_than_one_event_types) {
205 ASSERT_TRUE(RunRecordCmd({"-e", "cpu-cycles,cpu-clock"}));
206 ASSERT_TRUE(RunRecordCmd({"-e", "cpu-cycles", "-e", "cpu-clock"}));
209 TEST(record_cmd, mmap_page_option) {
210 ASSERT_TRUE(RunRecordCmd({"-m", "1"}));
211 ASSERT_FALSE(RunRecordCmd({"-m", "0"}));
212 ASSERT_FALSE(RunRecordCmd({"-m", "7"}));
215 static void CheckKernelSymbol(const std::string& path, bool need_kallsyms,
218 std::unique_ptr<RecordFileReader> reader =
219 RecordFileReader::CreateInstance(path);
220 ASSERT_TRUE(reader != nullptr);
221 std::vector<std::unique_ptr<Record>> records = reader->DataSection();
222 bool has_kernel_symbol_records = false;
223 for (const auto& record : records) {
224 if (record->type() == SIMPLE_PERF_RECORD_KERNEL_SYMBOL) {
225 has_kernel_symbol_records = true;
228 bool require_kallsyms = need_kallsyms && CheckKernelSymbolAddresses();
229 ASSERT_EQ(require_kallsyms, has_kernel_symbol_records);
233 TEST(record_cmd, kernel_symbol) {
234 TemporaryFile tmpfile;
235 ASSERT_TRUE(RunRecordCmd({}, tmpfile.path));
237 CheckKernelSymbol(tmpfile.path, true, &success);
238 ASSERT_TRUE(success);
239 ASSERT_TRUE(RunRecordCmd({"--no-dump-kernel-symbols"}, tmpfile.path));
240 CheckKernelSymbol(tmpfile.path, false, &success);
241 ASSERT_TRUE(success);
244 // Check if the dso/symbol records in perf.data matches our expectation.
245 static void CheckDsoSymbolRecords(const std::string& path,
246 bool can_have_dso_symbol_records,
249 std::unique_ptr<RecordFileReader> reader =
250 RecordFileReader::CreateInstance(path);
251 ASSERT_TRUE(reader != nullptr);
252 std::vector<std::unique_ptr<Record>> records = reader->DataSection();
253 bool has_dso_record = false;
254 bool has_symbol_record = false;
255 std::map<uint64_t, bool> dso_hit_map;
256 for (const auto& record : records) {
257 if (record->type() == SIMPLE_PERF_RECORD_DSO) {
258 has_dso_record = true;
259 uint64_t dso_id = static_cast<const DsoRecord*>(record.get())->dso_id;
260 ASSERT_EQ(dso_hit_map.end(), dso_hit_map.find(dso_id));
261 dso_hit_map.insert(std::make_pair(dso_id, false));
262 } else if (record->type() == SIMPLE_PERF_RECORD_SYMBOL) {
263 has_symbol_record = true;
264 uint64_t dso_id = static_cast<const SymbolRecord*>(record.get())->dso_id;
265 auto it = dso_hit_map.find(dso_id);
266 ASSERT_NE(dso_hit_map.end(), it);
270 if (can_have_dso_symbol_records) {
271 // It is possible that there are no samples hitting functions having symbol.
272 // In that case, there are no dso/symbol records.
273 ASSERT_EQ(has_dso_record, has_symbol_record);
274 for (auto& pair : dso_hit_map) {
275 ASSERT_TRUE(pair.second);
278 ASSERT_FALSE(has_dso_record);
279 ASSERT_FALSE(has_symbol_record);
284 TEST(record_cmd, dump_symbols) {
285 TemporaryFile tmpfile;
286 ASSERT_TRUE(RunRecordCmd({}, tmpfile.path));
288 CheckDsoSymbolRecords(tmpfile.path, false, &success);
289 ASSERT_TRUE(success);
290 ASSERT_TRUE(RunRecordCmd({"--dump-symbols"}, tmpfile.path));
291 CheckDsoSymbolRecords(tmpfile.path, true, &success);
292 ASSERT_TRUE(success);
293 if (IsDwarfCallChainSamplingSupported()) {
294 ASSERT_TRUE(RunRecordCmd({"-g"}, tmpfile.path));
296 CheckDsoSymbolRecords(tmpfile.path, false, &success);
297 ASSERT_TRUE(success);
298 ASSERT_TRUE(RunRecordCmd({"-g", "--dump-symbols"}, tmpfile.path));
299 CheckDsoSymbolRecords(tmpfile.path, true, &success);
300 ASSERT_TRUE(success);
304 TEST(record_cmd, group_option) {
305 ASSERT_TRUE(RunRecordCmd({"--group", "cpu-cycles,cpu-clock", "-m", "16"}));
306 ASSERT_TRUE(RunRecordCmd({"--group", "cpu-cycles,cpu-clock", "--group",
307 "cpu-cycles:u,cpu-clock:u", "--group",
308 "cpu-cycles:k,cpu-clock:k", "-m", "16"}));
311 TEST(record_cmd, symfs_option) { ASSERT_TRUE(RunRecordCmd({"--symfs", "/"})); }
313 TEST(record_cmd, duration_option) {
314 TemporaryFile tmpfile;
315 ASSERT_TRUE(RecordCmd()->Run({"--duration", "1.2", "-p",
316 std::to_string(getpid()), "-o", tmpfile.path}));
318 RecordCmd()->Run({"--duration", "1", "-o", tmpfile.path, "sleep", "2"}));
321 TEST(record_cmd, support_modifier_for_clock_events) {
322 for (const std::string& e : {"cpu-clock", "task-clock"}) {
323 for (const std::string& m : {"u", "k"}) {
324 ASSERT_TRUE(RunRecordCmd({"-e", e + ":" + m})) << "event " << e << ":"