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"
33 using namespace PerfFileFormat;
35 static std::unique_ptr<Command> RecordCmd() {
36 return CreateCommandInstance("record");
39 static bool RunRecordCmd(std::vector<std::string> v,
40 const char* output_file = nullptr) {
41 std::unique_ptr<TemporaryFile> tmpfile;
43 if (output_file != nullptr) {
44 out_file = output_file;
46 tmpfile.reset(new TemporaryFile);
47 out_file = tmpfile->path;
49 v.insert(v.end(), {"-o", out_file, "sleep", SLEEP_SEC});
50 return RecordCmd()->Run(v);
53 TEST(record_cmd, no_options) { ASSERT_TRUE(RunRecordCmd({})); }
55 TEST(record_cmd, system_wide_option) {
56 TEST_IN_ROOT(ASSERT_TRUE(RunRecordCmd({"-a"})));
59 TEST(record_cmd, sample_period_option) {
60 ASSERT_TRUE(RunRecordCmd({"-c", "100000"}));
63 TEST(record_cmd, event_option) {
64 ASSERT_TRUE(RunRecordCmd({"-e", "cpu-clock"}));
67 TEST(record_cmd, freq_option) {
68 ASSERT_TRUE(RunRecordCmd({"-f", "99"}));
69 ASSERT_TRUE(RunRecordCmd({"-F", "99"}));
72 TEST(record_cmd, output_file_option) {
73 TemporaryFile tmpfile;
74 ASSERT_TRUE(RecordCmd()->Run({"-o", tmpfile.path, "sleep", SLEEP_SEC}));
77 TEST(record_cmd, dump_kernel_mmap) {
78 TemporaryFile tmpfile;
79 ASSERT_TRUE(RunRecordCmd({}, tmpfile.path));
80 std::unique_ptr<RecordFileReader> reader =
81 RecordFileReader::CreateInstance(tmpfile.path);
82 ASSERT_TRUE(reader != nullptr);
83 std::vector<std::unique_ptr<Record>> records = reader->DataSection();
84 ASSERT_GT(records.size(), 0U);
85 bool have_kernel_mmap = false;
86 for (auto& record : records) {
87 if (record->type() == PERF_RECORD_MMAP) {
88 const MmapRecord* mmap_record =
89 static_cast<const MmapRecord*>(record.get());
90 if (strcmp(mmap_record->filename, DEFAULT_KERNEL_MMAP_NAME) == 0) {
91 have_kernel_mmap = true;
96 ASSERT_TRUE(have_kernel_mmap);
99 TEST(record_cmd, dump_build_id_feature) {
100 TemporaryFile tmpfile;
101 ASSERT_TRUE(RunRecordCmd({}, tmpfile.path));
102 std::unique_ptr<RecordFileReader> reader =
103 RecordFileReader::CreateInstance(tmpfile.path);
104 ASSERT_TRUE(reader != nullptr);
105 const FileHeader& file_header = reader->FileHeader();
106 ASSERT_TRUE(file_header.features[FEAT_BUILD_ID / 8] &
107 (1 << (FEAT_BUILD_ID % 8)));
108 ASSERT_GT(reader->FeatureSectionDescriptors().size(), 0u);
111 TEST(record_cmd, tracepoint_event) {
112 TEST_IN_ROOT(ASSERT_TRUE(RunRecordCmd({"-a", "-e", "sched:sched_switch"})));
115 TEST(record_cmd, branch_sampling) {
116 if (IsBranchSamplingSupported()) {
117 ASSERT_TRUE(RunRecordCmd({"-b"}));
118 ASSERT_TRUE(RunRecordCmd({"-j", "any,any_call,any_ret,ind_call"}));
119 ASSERT_TRUE(RunRecordCmd({"-j", "any,k"}));
120 ASSERT_TRUE(RunRecordCmd({"-j", "any,u"}));
121 ASSERT_FALSE(RunRecordCmd({"-j", "u"}));
123 GTEST_LOG_(INFO) << "This test does nothing as branch stack sampling is "
124 "not supported on this device.";
128 TEST(record_cmd, event_modifier) {
129 ASSERT_TRUE(RunRecordCmd({"-e", "cpu-cycles:u"}));
132 TEST(record_cmd, fp_callchain_sampling) {
133 ASSERT_TRUE(RunRecordCmd({"--call-graph", "fp"}));
136 TEST(record_cmd, system_wide_fp_callchain_sampling) {
137 TEST_IN_ROOT(ASSERT_TRUE(RunRecordCmd({"-a", "--call-graph", "fp"})));
140 TEST(record_cmd, dwarf_callchain_sampling) {
141 if (IsDwarfCallChainSamplingSupported()) {
142 ASSERT_TRUE(RunRecordCmd({"--call-graph", "dwarf"}));
143 ASSERT_TRUE(RunRecordCmd({"--call-graph", "dwarf,16384"}));
144 ASSERT_FALSE(RunRecordCmd({"--call-graph", "dwarf,65536"}));
145 ASSERT_TRUE(RunRecordCmd({"-g"}));
147 GTEST_LOG_(INFO) << "This test does nothing as dwarf callchain sampling is "
148 "not supported on this device.";
152 TEST(record_cmd, system_wide_dwarf_callchain_sampling) {
153 if (IsDwarfCallChainSamplingSupported()) {
154 TEST_IN_ROOT(RunRecordCmd({"-a", "--call-graph", "dwarf"}));
156 GTEST_LOG_(INFO) << "This test does nothing as dwarf callchain sampling is "
157 "not supported on this device.";
161 TEST(record_cmd, no_unwind_option) {
162 if (IsDwarfCallChainSamplingSupported()) {
163 ASSERT_TRUE(RunRecordCmd({"--call-graph", "dwarf", "--no-unwind"}));
165 GTEST_LOG_(INFO) << "This test does nothing as dwarf callchain sampling is "
166 "not supported on this device.";
168 ASSERT_FALSE(RunRecordCmd({"--no-unwind"}));
171 TEST(record_cmd, post_unwind_option) {
172 if (IsDwarfCallChainSamplingSupported()) {
173 ASSERT_TRUE(RunRecordCmd({"--call-graph", "dwarf", "--post-unwind"}));
175 GTEST_LOG_(INFO) << "This test does nothing as dwarf callchain sampling is "
176 "not supported on this device.";
178 ASSERT_FALSE(RunRecordCmd({"--post-unwind"}));
180 RunRecordCmd({"--call-graph", "dwarf", "--no-unwind", "--post-unwind"}));
183 TEST(record_cmd, existing_processes) {
184 std::vector<std::unique_ptr<Workload>> workloads;
185 CreateProcesses(2, &workloads);
186 std::string pid_list = android::base::StringPrintf(
187 "%d,%d", workloads[0]->GetPid(), workloads[1]->GetPid());
188 ASSERT_TRUE(RunRecordCmd({"-p", pid_list}));
191 TEST(record_cmd, existing_threads) {
192 std::vector<std::unique_ptr<Workload>> workloads;
193 CreateProcesses(2, &workloads);
194 // Process id can also be used as thread id in linux.
195 std::string tid_list = android::base::StringPrintf(
196 "%d,%d", workloads[0]->GetPid(), workloads[1]->GetPid());
197 ASSERT_TRUE(RunRecordCmd({"-t", tid_list}));
200 TEST(record_cmd, no_monitored_threads) { ASSERT_FALSE(RecordCmd()->Run({""})); }
202 TEST(record_cmd, more_than_one_event_types) {
203 ASSERT_TRUE(RunRecordCmd({"-e", "cpu-cycles,cpu-clock"}));
204 ASSERT_TRUE(RunRecordCmd({"-e", "cpu-cycles", "-e", "cpu-clock"}));
207 TEST(record_cmd, mmap_page_option) {
208 ASSERT_TRUE(RunRecordCmd({"-m", "1"}));
209 ASSERT_FALSE(RunRecordCmd({"-m", "0"}));
210 ASSERT_FALSE(RunRecordCmd({"-m", "7"}));
213 static void CheckKernelSymbol(const std::string& path, bool need_kallsyms,
216 std::unique_ptr<RecordFileReader> reader =
217 RecordFileReader::CreateInstance(path);
218 ASSERT_TRUE(reader != nullptr);
219 std::vector<std::unique_ptr<Record>> records = reader->DataSection();
220 bool has_kernel_symbol_records = false;
221 for (const auto& record : records) {
222 if (record->type() == SIMPLE_PERF_RECORD_KERNEL_SYMBOL) {
223 has_kernel_symbol_records = true;
226 ASSERT_EQ(need_kallsyms, has_kernel_symbol_records);
230 TEST(record_cmd, kernel_symbol) {
231 TemporaryFile tmpfile;
232 ASSERT_TRUE(RunRecordCmd({}, tmpfile.path));
234 CheckKernelSymbol(tmpfile.path, true, &success);
235 ASSERT_TRUE(success);
236 ASSERT_TRUE(RunRecordCmd({"--no-dump-kernel-symbols"}, tmpfile.path));
237 CheckKernelSymbol(tmpfile.path, false, &success);
238 ASSERT_TRUE(success);
241 // Check if the dso/symbol records in perf.data matches our expectation.
242 static void CheckDsoSymbolRecords(const std::string& path,
243 bool can_have_dso_symbol_records,
246 std::unique_ptr<RecordFileReader> reader =
247 RecordFileReader::CreateInstance(path);
248 ASSERT_TRUE(reader != nullptr);
249 std::vector<std::unique_ptr<Record>> records = reader->DataSection();
250 bool has_dso_record = false;
251 bool has_symbol_record = false;
252 std::map<uint64_t, bool> dso_hit_map;
253 for (const auto& record : records) {
254 if (record->type() == SIMPLE_PERF_RECORD_DSO) {
255 has_dso_record = true;
256 uint64_t dso_id = static_cast<const DsoRecord*>(record.get())->dso_id;
257 ASSERT_EQ(dso_hit_map.end(), dso_hit_map.find(dso_id));
258 dso_hit_map.insert(std::make_pair(dso_id, false));
259 } else if (record->type() == SIMPLE_PERF_RECORD_SYMBOL) {
260 has_symbol_record = true;
261 uint64_t dso_id = static_cast<const SymbolRecord*>(record.get())->dso_id;
262 auto it = dso_hit_map.find(dso_id);
263 ASSERT_NE(dso_hit_map.end(), it);
267 if (can_have_dso_symbol_records) {
268 // It is possible that there are no samples hitting functions having symbol.
269 // In that case, there are no dso/symbol records.
270 ASSERT_EQ(has_dso_record, has_symbol_record);
271 for (auto& pair : dso_hit_map) {
272 ASSERT_TRUE(pair.second);
275 ASSERT_FALSE(has_dso_record);
276 ASSERT_FALSE(has_symbol_record);
281 TEST(record_cmd, dump_symbols) {
282 TemporaryFile tmpfile;
283 ASSERT_TRUE(RunRecordCmd({}, tmpfile.path));
285 CheckDsoSymbolRecords(tmpfile.path, false, &success);
286 ASSERT_TRUE(success);
287 ASSERT_TRUE(RunRecordCmd({"--dump-symbols"}, tmpfile.path));
288 CheckDsoSymbolRecords(tmpfile.path, true, &success);
289 ASSERT_TRUE(success);
290 if (IsDwarfCallChainSamplingSupported()) {
291 ASSERT_TRUE(RunRecordCmd({"-g"}, tmpfile.path));
293 CheckDsoSymbolRecords(tmpfile.path, false, &success);
294 ASSERT_TRUE(success);
295 ASSERT_TRUE(RunRecordCmd({"-g", "--dump-symbols"}, tmpfile.path));
296 CheckDsoSymbolRecords(tmpfile.path, true, &success);
297 ASSERT_TRUE(success);
301 TEST(record_cmd, group_option) {
302 ASSERT_TRUE(RunRecordCmd({"--group", "cpu-cycles,cpu-clock", "-m", "16"}));
303 ASSERT_TRUE(RunRecordCmd({"--group", "cpu-cycles,cpu-clock", "--group",
304 "cpu-cycles:u,cpu-clock:u", "--group",
305 "cpu-cycles:k,cpu-clock:k", "-m", "16"}));
308 TEST(record_cmd, symfs_option) { ASSERT_TRUE(RunRecordCmd({"--symfs", "/"})); }
310 TEST(record_cmd, duration_option) {
311 TemporaryFile tmpfile;
312 ASSERT_TRUE(RecordCmd()->Run({"--duration", "1.2", "-p",
313 std::to_string(getpid()), "-o", tmpfile.path}));
315 RecordCmd()->Run({"--duration", "1", "-o", tmpfile.path, "sleep", "2"}));