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 <android-base/file.h>
24#include <android-base/stringprintf.h>
25#include <android-base/test_utils.h>
26
27#include <map>
28#include <memory>
29#include <thread>
30
31#include "command.h"
32#include "environment.h"
33#include "event_selection_set.h"
34#include "get_test_data.h"
35#include "record.h"
36#include "record_file.h"
37#include "test_util.h"
38#include "thread_tree.h"
39
40using namespace PerfFileFormat;
41
42static std::unique_ptr<Command> RecordCmd() {
43  return CreateCommandInstance("record");
44}
45
46static bool RunRecordCmd(std::vector<std::string> v,
47                         const char* output_file = nullptr) {
48  std::unique_ptr<TemporaryFile> tmpfile;
49  std::string out_file;
50  if (output_file != nullptr) {
51    out_file = output_file;
52  } else {
53    tmpfile.reset(new TemporaryFile);
54    out_file = tmpfile->path;
55  }
56  v.insert(v.end(), {"-o", out_file, "sleep", SLEEP_SEC});
57  return RecordCmd()->Run(v);
58}
59
60TEST(record_cmd, no_options) { ASSERT_TRUE(RunRecordCmd({})); }
61
62TEST(record_cmd, system_wide_option) {
63  TEST_IN_ROOT(ASSERT_TRUE(RunRecordCmd({"-a"})));
64}
65
66TEST(record_cmd, sample_period_option) {
67  ASSERT_TRUE(RunRecordCmd({"-c", "100000"}));
68}
69
70TEST(record_cmd, event_option) {
71  ASSERT_TRUE(RunRecordCmd({"-e", "cpu-clock"}));
72}
73
74TEST(record_cmd, freq_option) {
75  ASSERT_TRUE(RunRecordCmd({"-f", "99"}));
76  ASSERT_TRUE(RunRecordCmd({"-F", "99"}));
77}
78
79TEST(record_cmd, output_file_option) {
80  TemporaryFile tmpfile;
81  ASSERT_TRUE(RecordCmd()->Run({"-o", tmpfile.path, "sleep", SLEEP_SEC}));
82}
83
84TEST(record_cmd, dump_kernel_mmap) {
85  TemporaryFile tmpfile;
86  ASSERT_TRUE(RunRecordCmd({}, tmpfile.path));
87  std::unique_ptr<RecordFileReader> reader =
88      RecordFileReader::CreateInstance(tmpfile.path);
89  ASSERT_TRUE(reader != nullptr);
90  std::vector<std::unique_ptr<Record>> records = reader->DataSection();
91  ASSERT_GT(records.size(), 0U);
92  bool have_kernel_mmap = false;
93  for (auto& record : records) {
94    if (record->type() == PERF_RECORD_MMAP) {
95      const MmapRecord* mmap_record =
96          static_cast<const MmapRecord*>(record.get());
97      if (strcmp(mmap_record->filename, DEFAULT_KERNEL_MMAP_NAME) == 0 ||
98          strcmp(mmap_record->filename, DEFAULT_KERNEL_MMAP_NAME_PERF) == 0) {
99        have_kernel_mmap = true;
100        break;
101      }
102    }
103  }
104  ASSERT_TRUE(have_kernel_mmap);
105}
106
107TEST(record_cmd, dump_build_id_feature) {
108  TemporaryFile tmpfile;
109  ASSERT_TRUE(RunRecordCmd({}, tmpfile.path));
110  std::unique_ptr<RecordFileReader> reader =
111      RecordFileReader::CreateInstance(tmpfile.path);
112  ASSERT_TRUE(reader != nullptr);
113  const FileHeader& file_header = reader->FileHeader();
114  ASSERT_TRUE(file_header.features[FEAT_BUILD_ID / 8] &
115              (1 << (FEAT_BUILD_ID % 8)));
116  ASSERT_GT(reader->FeatureSectionDescriptors().size(), 0u);
117}
118
119TEST(record_cmd, tracepoint_event) {
120  TEST_IN_ROOT(ASSERT_TRUE(RunRecordCmd({"-a", "-e", "sched:sched_switch"})));
121}
122
123TEST(record_cmd, branch_sampling) {
124  if (IsBranchSamplingSupported()) {
125    ASSERT_TRUE(RunRecordCmd({"-b"}));
126    ASSERT_TRUE(RunRecordCmd({"-j", "any,any_call,any_ret,ind_call"}));
127    ASSERT_TRUE(RunRecordCmd({"-j", "any,k"}));
128    ASSERT_TRUE(RunRecordCmd({"-j", "any,u"}));
129    ASSERT_FALSE(RunRecordCmd({"-j", "u"}));
130  } else {
131    GTEST_LOG_(INFO) << "This test does nothing as branch stack sampling is "
132                        "not supported on this device.";
133  }
134}
135
136TEST(record_cmd, event_modifier) {
137  ASSERT_TRUE(RunRecordCmd({"-e", "cpu-cycles:u"}));
138}
139
140TEST(record_cmd, fp_callchain_sampling) {
141  ASSERT_TRUE(RunRecordCmd({"--call-graph", "fp"}));
142}
143
144TEST(record_cmd, fp_callchain_sampling_warning_on_arm) {
145  if (GetBuildArch() != ARCH_ARM) {
146    GTEST_LOG_(INFO) << "This test does nothing as it only tests on arm arch.";
147    return;
148  }
149  ASSERT_EXIT(
150      {
151        exit(RunRecordCmd({"--call-graph", "fp"}) ? 0 : 1);
152      },
153      testing::ExitedWithCode(0), "doesn't work well on arm");
154}
155
156TEST(record_cmd, system_wide_fp_callchain_sampling) {
157  TEST_IN_ROOT(ASSERT_TRUE(RunRecordCmd({"-a", "--call-graph", "fp"})));
158}
159
160bool IsInNativeAbi() {
161  static int in_native_abi = -1;
162  if (in_native_abi == -1) {
163    FILE* fp = popen("uname -m", "re");
164    char buf[40];
165    memset(buf, '\0', sizeof(buf));
166    fgets(buf, sizeof(buf), fp);
167    pclose(fp);
168    std::string s = buf;
169    in_native_abi = 1;
170    if (GetBuildArch() == ARCH_X86_32 || GetBuildArch() == ARCH_X86_64) {
171      if (s.find("86") == std::string::npos) {
172        in_native_abi = 0;
173      }
174    } else if (GetBuildArch() == ARCH_ARM || GetBuildArch() == ARCH_ARM64) {
175      if (s.find("arm") == std::string::npos && s.find("aarch64") == std::string::npos) {
176        in_native_abi = 0;
177      }
178    }
179  }
180  return in_native_abi == 1;
181}
182
183TEST(record_cmd, dwarf_callchain_sampling) {
184  OMIT_TEST_ON_NON_NATIVE_ABIS();
185  ASSERT_TRUE(IsDwarfCallChainSamplingSupported());
186  std::vector<std::unique_ptr<Workload>> workloads;
187  CreateProcesses(1, &workloads);
188  std::string pid = std::to_string(workloads[0]->GetPid());
189  ASSERT_TRUE(RunRecordCmd({"-p", pid, "--call-graph", "dwarf"}));
190  ASSERT_TRUE(RunRecordCmd({"-p", pid, "--call-graph", "dwarf,16384"}));
191  ASSERT_FALSE(RunRecordCmd({"-p", pid, "--call-graph", "dwarf,65536"}));
192  ASSERT_TRUE(RunRecordCmd({"-p", pid, "-g"}));
193}
194
195TEST(record_cmd, system_wide_dwarf_callchain_sampling) {
196  OMIT_TEST_ON_NON_NATIVE_ABIS();
197  ASSERT_TRUE(IsDwarfCallChainSamplingSupported());
198  TEST_IN_ROOT(RunRecordCmd({"-a", "--call-graph", "dwarf"}));
199}
200
201TEST(record_cmd, no_unwind_option) {
202  OMIT_TEST_ON_NON_NATIVE_ABIS();
203  ASSERT_TRUE(IsDwarfCallChainSamplingSupported());
204  ASSERT_TRUE(RunRecordCmd({"--call-graph", "dwarf", "--no-unwind"}));
205  ASSERT_FALSE(RunRecordCmd({"--no-unwind"}));
206}
207
208TEST(record_cmd, post_unwind_option) {
209  OMIT_TEST_ON_NON_NATIVE_ABIS();
210  ASSERT_TRUE(IsDwarfCallChainSamplingSupported());
211  std::vector<std::unique_ptr<Workload>> workloads;
212  CreateProcesses(1, &workloads);
213  std::string pid = std::to_string(workloads[0]->GetPid());
214  ASSERT_TRUE(RunRecordCmd({"-p", pid, "--call-graph", "dwarf", "--post-unwind"}));
215  ASSERT_FALSE(RunRecordCmd({"--post-unwind"}));
216  ASSERT_FALSE(
217      RunRecordCmd({"--call-graph", "dwarf", "--no-unwind", "--post-unwind"}));
218}
219
220TEST(record_cmd, existing_processes) {
221  std::vector<std::unique_ptr<Workload>> workloads;
222  CreateProcesses(2, &workloads);
223  std::string pid_list = android::base::StringPrintf(
224      "%d,%d", workloads[0]->GetPid(), workloads[1]->GetPid());
225  ASSERT_TRUE(RunRecordCmd({"-p", pid_list}));
226}
227
228TEST(record_cmd, existing_threads) {
229  std::vector<std::unique_ptr<Workload>> workloads;
230  CreateProcesses(2, &workloads);
231  // Process id can also be used as thread id in linux.
232  std::string tid_list = android::base::StringPrintf(
233      "%d,%d", workloads[0]->GetPid(), workloads[1]->GetPid());
234  ASSERT_TRUE(RunRecordCmd({"-t", tid_list}));
235}
236
237TEST(record_cmd, no_monitored_threads) { ASSERT_FALSE(RecordCmd()->Run({""})); }
238
239TEST(record_cmd, more_than_one_event_types) {
240  ASSERT_TRUE(RunRecordCmd({"-e", "cpu-cycles,cpu-clock"}));
241  ASSERT_TRUE(RunRecordCmd({"-e", "cpu-cycles", "-e", "cpu-clock"}));
242}
243
244TEST(record_cmd, mmap_page_option) {
245  ASSERT_TRUE(RunRecordCmd({"-m", "1"}));
246  ASSERT_FALSE(RunRecordCmd({"-m", "0"}));
247  ASSERT_FALSE(RunRecordCmd({"-m", "7"}));
248}
249
250static void CheckKernelSymbol(const std::string& path, bool need_kallsyms,
251                              bool* success) {
252  *success = false;
253  std::unique_ptr<RecordFileReader> reader =
254      RecordFileReader::CreateInstance(path);
255  ASSERT_TRUE(reader != nullptr);
256  std::vector<std::unique_ptr<Record>> records = reader->DataSection();
257  bool has_kernel_symbol_records = false;
258  for (const auto& record : records) {
259    if (record->type() == SIMPLE_PERF_RECORD_KERNEL_SYMBOL) {
260      has_kernel_symbol_records = true;
261    }
262  }
263  bool require_kallsyms = need_kallsyms && CheckKernelSymbolAddresses();
264  ASSERT_EQ(require_kallsyms, has_kernel_symbol_records);
265  *success = true;
266}
267
268TEST(record_cmd, kernel_symbol) {
269  TemporaryFile tmpfile;
270  ASSERT_TRUE(RunRecordCmd({"--no-dump-symbols"}, tmpfile.path));
271  bool success;
272  CheckKernelSymbol(tmpfile.path, true, &success);
273  ASSERT_TRUE(success);
274  ASSERT_TRUE(RunRecordCmd({"--no-dump-symbols", "--no-dump-kernel-symbols"}, tmpfile.path));
275  CheckKernelSymbol(tmpfile.path, false, &success);
276  ASSERT_TRUE(success);
277}
278
279// Check if the dso/symbol records in perf.data matches our expectation.
280static void CheckDsoSymbolRecords(const std::string& path,
281                                  bool can_have_dso_symbol_records,
282                                  bool* success) {
283  *success = false;
284  std::unique_ptr<RecordFileReader> reader =
285      RecordFileReader::CreateInstance(path);
286  ASSERT_TRUE(reader != nullptr);
287  std::vector<std::unique_ptr<Record>> records = reader->DataSection();
288  bool has_dso_record = false;
289  bool has_symbol_record = false;
290  std::map<uint64_t, bool> dso_hit_map;
291  for (const auto& record : records) {
292    if (record->type() == SIMPLE_PERF_RECORD_DSO) {
293      has_dso_record = true;
294      uint64_t dso_id = static_cast<const DsoRecord*>(record.get())->dso_id;
295      ASSERT_EQ(dso_hit_map.end(), dso_hit_map.find(dso_id));
296      dso_hit_map.insert(std::make_pair(dso_id, false));
297    } else if (record->type() == SIMPLE_PERF_RECORD_SYMBOL) {
298      has_symbol_record = true;
299      uint64_t dso_id = static_cast<const SymbolRecord*>(record.get())->dso_id;
300      auto it = dso_hit_map.find(dso_id);
301      ASSERT_NE(dso_hit_map.end(), it);
302      it->second = true;
303    }
304  }
305  if (can_have_dso_symbol_records) {
306    // It is possible that there are no samples hitting functions having symbol.
307    // In that case, there are no dso/symbol records.
308    ASSERT_EQ(has_dso_record, has_symbol_record);
309    for (auto& pair : dso_hit_map) {
310      ASSERT_TRUE(pair.second);
311    }
312  } else {
313    ASSERT_FALSE(has_dso_record);
314    ASSERT_FALSE(has_symbol_record);
315  }
316  *success = true;
317}
318
319TEST(record_cmd, no_dump_symbols) {
320  TemporaryFile tmpfile;
321  ASSERT_TRUE(RunRecordCmd({}, tmpfile.path));
322  bool success;
323  CheckDsoSymbolRecords(tmpfile.path, true, &success);
324  ASSERT_TRUE(success);
325  ASSERT_TRUE(RunRecordCmd({"--no-dump-symbols"}, tmpfile.path));
326  CheckDsoSymbolRecords(tmpfile.path, false, &success);
327  ASSERT_TRUE(success);
328  OMIT_TEST_ON_NON_NATIVE_ABIS();
329  ASSERT_TRUE(IsDwarfCallChainSamplingSupported());
330  std::vector<std::unique_ptr<Workload>> workloads;
331  CreateProcesses(1, &workloads);
332  std::string pid = std::to_string(workloads[0]->GetPid());
333  ASSERT_TRUE(RunRecordCmd({"-p", pid, "-g"}, tmpfile.path));
334  CheckDsoSymbolRecords(tmpfile.path, true, &success);
335  ASSERT_TRUE(success);
336  ASSERT_TRUE(RunRecordCmd({"-p", pid, "-g", "--no-dump-symbols"}, tmpfile.path));
337  CheckDsoSymbolRecords(tmpfile.path, false, &success);
338  ASSERT_TRUE(success);
339}
340
341TEST(record_cmd, dump_kernel_symbols) {
342  if (!IsRoot()) {
343    GTEST_LOG_(INFO) << "Test requires root privilege";
344    return;
345  }
346  TemporaryFile tmpfile;
347  ASSERT_TRUE(RunRecordCmd({"-a", "-o", tmpfile.path, "sleep", "1"}));
348  std::unique_ptr<RecordFileReader> reader = RecordFileReader::CreateInstance(tmpfile.path);
349  ASSERT_TRUE(reader != nullptr);
350  std::map<int, SectionDesc> section_map = reader->FeatureSectionDescriptors();
351  ASSERT_NE(section_map.find(FEAT_FILE), section_map.end());
352  std::string file_path;
353  uint32_t file_type;
354  uint64_t min_vaddr;
355  std::vector<Symbol> symbols;
356  size_t read_pos = 0;
357  bool has_kernel_symbols = false;
358  while (reader->ReadFileFeature(read_pos, &file_path, &file_type, &min_vaddr, &symbols)) {
359    if (file_type == DSO_KERNEL && !symbols.empty()) {
360      has_kernel_symbols = true;
361    }
362  }
363  ASSERT_TRUE(has_kernel_symbols);
364}
365
366TEST(record_cmd, group_option) {
367  ASSERT_TRUE(RunRecordCmd({"--group", "cpu-cycles,cpu-clock", "-m", "16"}));
368  ASSERT_TRUE(RunRecordCmd({"--group", "cpu-cycles,cpu-clock", "--group",
369                            "cpu-cycles:u,cpu-clock:u", "--group",
370                            "cpu-cycles:k,cpu-clock:k", "-m", "16"}));
371}
372
373TEST(record_cmd, symfs_option) { ASSERT_TRUE(RunRecordCmd({"--symfs", "/"})); }
374
375TEST(record_cmd, duration_option) {
376  TemporaryFile tmpfile;
377  ASSERT_TRUE(RecordCmd()->Run({"--duration", "1.2", "-p",
378                                std::to_string(getpid()), "-o", tmpfile.path, "--in-app"}));
379  ASSERT_TRUE(
380      RecordCmd()->Run({"--duration", "1", "-o", tmpfile.path, "sleep", "2"}));
381}
382
383TEST(record_cmd, support_modifier_for_clock_events) {
384  for (const std::string& e : {"cpu-clock", "task-clock"}) {
385    for (const std::string& m : {"u", "k"}) {
386      ASSERT_TRUE(RunRecordCmd({"-e", e + ":" + m})) << "event " << e << ":"
387                                                     << m;
388    }
389  }
390}
391
392TEST(record_cmd, handle_SIGHUP) {
393  TemporaryFile tmpfile;
394  std::thread thread([]() {
395    sleep(1);
396    kill(getpid(), SIGHUP);
397  });
398  thread.detach();
399  ASSERT_TRUE(RecordCmd()->Run({"-o", tmpfile.path, "sleep", "1000000"}));
400}
401
402TEST(record_cmd, stop_when_no_more_targets) {
403  TemporaryFile tmpfile;
404  std::atomic<int> tid(0);
405  std::thread thread([&]() {
406    tid = gettid();
407    sleep(1);
408  });
409  thread.detach();
410  while (tid == 0);
411  ASSERT_TRUE(RecordCmd()->Run({"-o", tmpfile.path, "-t", std::to_string(tid), "--in-app"}));
412}
413
414TEST(record_cmd, donot_stop_when_having_targets) {
415  std::vector<std::unique_ptr<Workload>> workloads;
416  CreateProcesses(1, &workloads);
417  std::string pid = std::to_string(workloads[0]->GetPid());
418  uint64_t start_time_in_ns = GetSystemClock();
419  TemporaryFile tmpfile;
420  ASSERT_TRUE(RecordCmd()->Run({"-o", tmpfile.path, "-p", pid, "--duration", "3"}));
421  uint64_t end_time_in_ns = GetSystemClock();
422  ASSERT_GT(end_time_in_ns - start_time_in_ns, static_cast<uint64_t>(2e9));
423}
424
425TEST(record_cmd, start_profiling_fd_option) {
426  int pipefd[2];
427  ASSERT_EQ(0, pipe(pipefd));
428  int read_fd = pipefd[0];
429  int write_fd = pipefd[1];
430  ASSERT_EXIT(
431      {
432        close(read_fd);
433        exit(RunRecordCmd({"--start_profiling_fd", std::to_string(write_fd)}) ? 0 : 1);
434      },
435      testing::ExitedWithCode(0), "");
436  close(write_fd);
437  std::string s;
438  ASSERT_TRUE(android::base::ReadFdToString(read_fd, &s));
439  close(read_fd);
440  ASSERT_EQ("STARTED", s);
441}
442
443TEST(record_cmd, record_meta_info_feature) {
444  TemporaryFile tmpfile;
445  ASSERT_TRUE(RunRecordCmd({}, tmpfile.path));
446  std::unique_ptr<RecordFileReader> reader = RecordFileReader::CreateInstance(tmpfile.path);
447  ASSERT_TRUE(reader != nullptr);
448  std::unordered_map<std::string, std::string> info_map;
449  ASSERT_TRUE(reader->ReadMetaInfoFeature(&info_map));
450  ASSERT_NE(info_map.find("simpleperf_version"), info_map.end());
451}
452
453// See http://b/63135835.
454TEST(record_cmd, cpu_clock_for_a_long_time) {
455  std::vector<std::unique_ptr<Workload>> workloads;
456  CreateProcesses(1, &workloads);
457  std::string pid = std::to_string(workloads[0]->GetPid());
458  TemporaryFile tmpfile;
459  ASSERT_TRUE(RecordCmd()->Run(
460      {"-e", "cpu-clock", "-o", tmpfile.path, "-p", pid, "--duration", "3"}));
461}
462
463TEST(record_cmd, dump_regs_for_tracepoint_events) {
464  OMIT_TEST_ON_NON_NATIVE_ABIS();
465  // Check if the kernel can dump registers for tracepoint events.
466  // If not, probably a kernel patch below is missing:
467  // "5b09a094f2 arm64: perf: Fix callchain parse error with kernel tracepoint events"
468  std::vector<std::unique_ptr<Workload>> workloads;
469  CreateProcesses(1, &workloads);
470  std::string pid = std::to_string(workloads[0]->GetPid());
471  TemporaryFile tmpfile;
472  ASSERT_TRUE(RecordCmd()->Run({"-o", tmpfile.path, "-p", pid, "-e", "sched:sched_switch",
473                                "-g", "--no-unwind", "--duration", "1"}));
474
475  // If the kernel patch is missing, all regs dumped in sample records are zero.
476  std::unique_ptr<RecordFileReader> reader = RecordFileReader::CreateInstance(tmpfile.path);
477  CHECK(reader != nullptr);
478  std::unique_ptr<Record> r;
479  bool regs_all_zero = true;
480  while (reader->ReadRecord(r) && r && regs_all_zero) {
481    if (r->type() != PERF_RECORD_SAMPLE) {
482      continue;
483    }
484    SampleRecord* s = static_cast<SampleRecord*>(r.get());
485    for (size_t i = 0; i < s->regs_user_data.reg_nr; ++i) {
486      if (s->regs_user_data.regs[i] != 0u) {
487        regs_all_zero = false;
488        break;
489      }
490    }
491  }
492  ASSERT_FALSE(regs_all_zero);
493}
494