OSDN Git Service

Merge "simpleperf: monitor cpu hotplug in stat cmd."
[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
33 using namespace PerfFileFormat;
34
35 static std::unique_ptr<Command> RecordCmd() {
36   return CreateCommandInstance("record");
37 }
38
39 static bool RunRecordCmd(std::vector<std::string> v,
40                          const char* output_file = nullptr) {
41   std::unique_ptr<TemporaryFile> tmpfile;
42   std::string out_file;
43   if (output_file != nullptr) {
44     out_file = output_file;
45   } else {
46     tmpfile.reset(new TemporaryFile);
47     out_file = tmpfile->path;
48   }
49   v.insert(v.end(), {"-o", out_file, "sleep", SLEEP_SEC});
50   return RecordCmd()->Run(v);
51 }
52
53 TEST(record_cmd, no_options) { ASSERT_TRUE(RunRecordCmd({})); }
54
55 TEST(record_cmd, system_wide_option) {
56   TEST_IN_ROOT(ASSERT_TRUE(RunRecordCmd({"-a"})));
57 }
58
59 TEST(record_cmd, sample_period_option) {
60   ASSERT_TRUE(RunRecordCmd({"-c", "100000"}));
61 }
62
63 TEST(record_cmd, event_option) {
64   ASSERT_TRUE(RunRecordCmd({"-e", "cpu-clock"}));
65 }
66
67 TEST(record_cmd, freq_option) {
68   ASSERT_TRUE(RunRecordCmd({"-f", "99"}));
69   ASSERT_TRUE(RunRecordCmd({"-F", "99"}));
70 }
71
72 TEST(record_cmd, output_file_option) {
73   TemporaryFile tmpfile;
74   ASSERT_TRUE(RecordCmd()->Run({"-o", tmpfile.path, "sleep", SLEEP_SEC}));
75 }
76
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;
92         break;
93       }
94     }
95   }
96   ASSERT_TRUE(have_kernel_mmap);
97 }
98
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);
109 }
110
111 TEST(record_cmd, tracepoint_event) {
112   TEST_IN_ROOT(ASSERT_TRUE(RunRecordCmd({"-a", "-e", "sched:sched_switch"})));
113 }
114
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"}));
122   } else {
123     GTEST_LOG_(INFO) << "This test does nothing as branch stack sampling is "
124                         "not supported on this device.";
125   }
126 }
127
128 TEST(record_cmd, event_modifier) {
129   ASSERT_TRUE(RunRecordCmd({"-e", "cpu-cycles:u"}));
130 }
131
132 TEST(record_cmd, fp_callchain_sampling) {
133   ASSERT_TRUE(RunRecordCmd({"--call-graph", "fp"}));
134 }
135
136 TEST(record_cmd, system_wide_fp_callchain_sampling) {
137   TEST_IN_ROOT(ASSERT_TRUE(RunRecordCmd({"-a", "--call-graph", "fp"})));
138 }
139
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"}));
146   } else {
147     GTEST_LOG_(INFO) << "This test does nothing as dwarf callchain sampling is "
148                         "not supported on this device.";
149   }
150 }
151
152 TEST(record_cmd, system_wide_dwarf_callchain_sampling) {
153   if (IsDwarfCallChainSamplingSupported()) {
154     TEST_IN_ROOT(RunRecordCmd({"-a", "--call-graph", "dwarf"}));
155   } else {
156     GTEST_LOG_(INFO) << "This test does nothing as dwarf callchain sampling is "
157                         "not supported on this device.";
158   }
159 }
160
161 TEST(record_cmd, no_unwind_option) {
162   if (IsDwarfCallChainSamplingSupported()) {
163     ASSERT_TRUE(RunRecordCmd({"--call-graph", "dwarf", "--no-unwind"}));
164   } else {
165     GTEST_LOG_(INFO) << "This test does nothing as dwarf callchain sampling is "
166                         "not supported on this device.";
167   }
168   ASSERT_FALSE(RunRecordCmd({"--no-unwind"}));
169 }
170
171 TEST(record_cmd, post_unwind_option) {
172   if (IsDwarfCallChainSamplingSupported()) {
173     ASSERT_TRUE(RunRecordCmd({"--call-graph", "dwarf", "--post-unwind"}));
174   } else {
175     GTEST_LOG_(INFO) << "This test does nothing as dwarf callchain sampling is "
176                         "not supported on this device.";
177   }
178   ASSERT_FALSE(RunRecordCmd({"--post-unwind"}));
179   ASSERT_FALSE(
180       RunRecordCmd({"--call-graph", "dwarf", "--no-unwind", "--post-unwind"}));
181 }
182
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}));
189 }
190
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}));
198 }
199
200 TEST(record_cmd, no_monitored_threads) { ASSERT_FALSE(RecordCmd()->Run({""})); }
201
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"}));
205 }
206
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"}));
211 }
212
213 static void CheckKernelSymbol(const std::string& path, bool need_kallsyms,
214                               bool* success) {
215   *success = false;
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;
224     }
225   }
226   ASSERT_EQ(need_kallsyms, has_kernel_symbol_records);
227   *success = true;
228 }
229
230 TEST(record_cmd, kernel_symbol) {
231   TemporaryFile tmpfile;
232   ASSERT_TRUE(RunRecordCmd({}, tmpfile.path));
233   bool success;
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);
239 }
240
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,
244                                   bool* success) {
245   *success = false;
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);
264       it->second = true;
265     }
266   }
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);
273     }
274   } else {
275     ASSERT_FALSE(has_dso_record);
276     ASSERT_FALSE(has_symbol_record);
277   }
278   *success = true;
279 }
280
281 TEST(record_cmd, dump_symbols) {
282   TemporaryFile tmpfile;
283   ASSERT_TRUE(RunRecordCmd({}, tmpfile.path));
284   bool success;
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));
292     bool success;
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);
298   }
299 }
300
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"}));
306 }
307
308 TEST(record_cmd, symfs_option) { ASSERT_TRUE(RunRecordCmd({"--symfs", "/"})); }
309
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}));
314   ASSERT_TRUE(
315       RecordCmd()->Run({"--duration", "1", "-o", tmpfile.path, "sleep", "2"}));
316 }