OSDN Git Service

DO NOT MERGE: fec: remove unused mmap code
[android-x86/system-extras.git] / simpleperf / cmd_record_test.cpp
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 <android-base/stringprintf.h>
20 #include <android-base/test_utils.h>
21
22 #include <map>
23 #include <memory>
24
25 #include "command.h"
26 #include "environment.h"
27 #include "event_selection_set.h"
28 #include "get_test_data.h"
29 #include "record.h"
30 #include "record_file.h"
31 #include "test_util.h"
32 #include "thread_tree.h"
33
34 using namespace PerfFileFormat;
35
36 static std::unique_ptr<Command> RecordCmd() {
37   return CreateCommandInstance("record");
38 }
39
40 static bool RunRecordCmd(std::vector<std::string> v,
41                          const char* output_file = nullptr) {
42   std::unique_ptr<TemporaryFile> tmpfile;
43   std::string out_file;
44   if (output_file != nullptr) {
45     out_file = output_file;
46   } else {
47     tmpfile.reset(new TemporaryFile);
48     out_file = tmpfile->path;
49   }
50   v.insert(v.end(), {"-o", out_file, "sleep", SLEEP_SEC});
51   return RecordCmd()->Run(v);
52 }
53
54 TEST(record_cmd, no_options) { ASSERT_TRUE(RunRecordCmd({})); }
55
56 TEST(record_cmd, system_wide_option) {
57   TEST_IN_ROOT(ASSERT_TRUE(RunRecordCmd({"-a"})));
58 }
59
60 TEST(record_cmd, sample_period_option) {
61   ASSERT_TRUE(RunRecordCmd({"-c", "100000"}));
62 }
63
64 TEST(record_cmd, event_option) {
65   ASSERT_TRUE(RunRecordCmd({"-e", "cpu-clock"}));
66 }
67
68 TEST(record_cmd, freq_option) {
69   ASSERT_TRUE(RunRecordCmd({"-f", "99"}));
70   ASSERT_TRUE(RunRecordCmd({"-F", "99"}));
71 }
72
73 TEST(record_cmd, output_file_option) {
74   TemporaryFile tmpfile;
75   ASSERT_TRUE(RecordCmd()->Run({"-o", tmpfile.path, "sleep", SLEEP_SEC}));
76 }
77
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;
94         break;
95       }
96     }
97   }
98   ASSERT_TRUE(have_kernel_mmap);
99 }
100
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);
111 }
112
113 TEST(record_cmd, tracepoint_event) {
114   TEST_IN_ROOT(ASSERT_TRUE(RunRecordCmd({"-a", "-e", "sched:sched_switch"})));
115 }
116
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"}));
124   } else {
125     GTEST_LOG_(INFO) << "This test does nothing as branch stack sampling is "
126                         "not supported on this device.";
127   }
128 }
129
130 TEST(record_cmd, event_modifier) {
131   ASSERT_TRUE(RunRecordCmd({"-e", "cpu-cycles:u"}));
132 }
133
134 TEST(record_cmd, fp_callchain_sampling) {
135   ASSERT_TRUE(RunRecordCmd({"--call-graph", "fp"}));
136 }
137
138 TEST(record_cmd, system_wide_fp_callchain_sampling) {
139   TEST_IN_ROOT(ASSERT_TRUE(RunRecordCmd({"-a", "--call-graph", "fp"})));
140 }
141
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"}));
148   } else {
149     GTEST_LOG_(INFO) << "This test does nothing as dwarf callchain sampling is "
150                         "not supported on this device.";
151   }
152 }
153
154 TEST(record_cmd, system_wide_dwarf_callchain_sampling) {
155   if (IsDwarfCallChainSamplingSupported()) {
156     TEST_IN_ROOT(RunRecordCmd({"-a", "--call-graph", "dwarf"}));
157   } else {
158     GTEST_LOG_(INFO) << "This test does nothing as dwarf callchain sampling is "
159                         "not supported on this device.";
160   }
161 }
162
163 TEST(record_cmd, no_unwind_option) {
164   if (IsDwarfCallChainSamplingSupported()) {
165     ASSERT_TRUE(RunRecordCmd({"--call-graph", "dwarf", "--no-unwind"}));
166   } else {
167     GTEST_LOG_(INFO) << "This test does nothing as dwarf callchain sampling is "
168                         "not supported on this device.";
169   }
170   ASSERT_FALSE(RunRecordCmd({"--no-unwind"}));
171 }
172
173 TEST(record_cmd, post_unwind_option) {
174   if (IsDwarfCallChainSamplingSupported()) {
175     ASSERT_TRUE(RunRecordCmd({"--call-graph", "dwarf", "--post-unwind"}));
176   } else {
177     GTEST_LOG_(INFO) << "This test does nothing as dwarf callchain sampling is "
178                         "not supported on this device.";
179   }
180   ASSERT_FALSE(RunRecordCmd({"--post-unwind"}));
181   ASSERT_FALSE(
182       RunRecordCmd({"--call-graph", "dwarf", "--no-unwind", "--post-unwind"}));
183 }
184
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}));
191 }
192
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}));
200 }
201
202 TEST(record_cmd, no_monitored_threads) { ASSERT_FALSE(RecordCmd()->Run({""})); }
203
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"}));
207 }
208
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"}));
213 }
214
215 static void CheckKernelSymbol(const std::string& path, bool need_kallsyms,
216                               bool* success) {
217   *success = false;
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;
226     }
227   }
228   bool require_kallsyms = need_kallsyms && CheckKernelSymbolAddresses();
229   ASSERT_EQ(require_kallsyms, has_kernel_symbol_records);
230   *success = true;
231 }
232
233 TEST(record_cmd, kernel_symbol) {
234   TemporaryFile tmpfile;
235   ASSERT_TRUE(RunRecordCmd({}, tmpfile.path));
236   bool success;
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);
242 }
243
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,
247                                   bool* success) {
248   *success = false;
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);
267       it->second = true;
268     }
269   }
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);
276     }
277   } else {
278     ASSERT_FALSE(has_dso_record);
279     ASSERT_FALSE(has_symbol_record);
280   }
281   *success = true;
282 }
283
284 TEST(record_cmd, dump_symbols) {
285   TemporaryFile tmpfile;
286   ASSERT_TRUE(RunRecordCmd({}, tmpfile.path));
287   bool success;
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));
295     bool success;
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);
301   }
302 }
303
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"}));
309 }
310
311 TEST(record_cmd, symfs_option) { ASSERT_TRUE(RunRecordCmd({"--symfs", "/"})); }
312
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}));
317   ASSERT_TRUE(
318       RecordCmd()->Run({"--duration", "1", "-o", tmpfile.path, "sleep", "2"}));
319 }
320
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 << ":"
325                                                      << m;
326     }
327   }
328 }