OSDN Git Service

0a9dfde26f53c91df93e6aa850874cf5fe704d4e
[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
21 #include "command.h"
22 #include "environment.h"
23 #include "event_selection_set.h"
24 #include "record.h"
25 #include "record_file.h"
26 #include "test_util.h"
27
28 using namespace PerfFileFormat;
29
30 static std::unique_ptr<Command> RecordCmd() {
31   return CreateCommandInstance("record");
32 }
33
34 TEST(record_cmd, no_options) {
35   ASSERT_TRUE(RecordCmd()->Run({"sleep", "1"}));
36 }
37
38 TEST(record_cmd, system_wide_option) {
39   ASSERT_TRUE(RecordCmd()->Run({"-a", "sleep", "1"}));
40 }
41
42 TEST(record_cmd, sample_period_option) {
43   ASSERT_TRUE(RecordCmd()->Run({"-c", "100000", "sleep", "1"}));
44 }
45
46 TEST(record_cmd, event_option) {
47   ASSERT_TRUE(RecordCmd()->Run({"-e", "cpu-clock", "sleep", "1"}));
48 }
49
50 TEST(record_cmd, freq_option) {
51   ASSERT_TRUE(RecordCmd()->Run({"-f", "99", "sleep", "1"}));
52   ASSERT_TRUE(RecordCmd()->Run({"-F", "99", "sleep", "1"}));
53 }
54
55 TEST(record_cmd, output_file_option) {
56   ASSERT_TRUE(RecordCmd()->Run({"-o", "perf2.data", "sleep", "1"}));
57 }
58
59 TEST(record_cmd, dump_kernel_mmap) {
60   ASSERT_TRUE(RecordCmd()->Run({"sleep", "1"}));
61   std::unique_ptr<RecordFileReader> reader = RecordFileReader::CreateInstance("perf.data");
62   ASSERT_TRUE(reader != nullptr);
63   std::vector<std::unique_ptr<Record>> records = reader->DataSection();
64   ASSERT_GT(records.size(), 0U);
65   bool have_kernel_mmap = false;
66   for (auto& record : records) {
67     if (record->header.type == PERF_RECORD_MMAP) {
68       const MmapRecord* mmap_record = static_cast<const MmapRecord*>(record.get());
69       if (mmap_record->filename == DEFAULT_KERNEL_MMAP_NAME) {
70         have_kernel_mmap = true;
71         break;
72       }
73     }
74   }
75   ASSERT_TRUE(have_kernel_mmap);
76 }
77
78 TEST(record_cmd, dump_build_id_feature) {
79   ASSERT_TRUE(RecordCmd()->Run({"sleep", "1"}));
80   std::unique_ptr<RecordFileReader> reader = RecordFileReader::CreateInstance("perf.data");
81   ASSERT_TRUE(reader != nullptr);
82   const FileHeader& file_header = reader->FileHeader();
83   ASSERT_TRUE(file_header.features[FEAT_BUILD_ID / 8] & (1 << (FEAT_BUILD_ID % 8)));
84   ASSERT_GT(reader->FeatureSectionDescriptors().size(), 0u);
85 }
86
87 TEST(record_cmd, tracepoint_event) {
88   ASSERT_TRUE(RecordCmd()->Run({"-a", "-e", "sched:sched_switch", "sleep", "1"}));
89 }
90
91 TEST(record_cmd, branch_sampling) {
92   if (IsBranchSamplingSupported()) {
93     ASSERT_TRUE(RecordCmd()->Run({"-a", "-b", "sleep", "1"}));
94     ASSERT_TRUE(RecordCmd()->Run({"-j", "any,any_call,any_ret,ind_call", "sleep", "1"}));
95     ASSERT_TRUE(RecordCmd()->Run({"-j", "any,k", "sleep", "1"}));
96     ASSERT_TRUE(RecordCmd()->Run({"-j", "any,u", "sleep", "1"}));
97     ASSERT_FALSE(RecordCmd()->Run({"-j", "u", "sleep", "1"}));
98   } else {
99     GTEST_LOG_(INFO)
100         << "This test does nothing as branch stack sampling is not supported on this device.";
101   }
102 }
103
104 TEST(record_cmd, event_modifier) {
105   ASSERT_TRUE(RecordCmd()->Run({"-e", "cpu-cycles:u", "sleep", "1"}));
106 }
107
108 TEST(record_cmd, fp_callchain_sampling) {
109   ASSERT_TRUE(RecordCmd()->Run({"--call-graph", "fp", "sleep", "1"}));
110 }
111
112 TEST(record_cmd, dwarf_callchain_sampling) {
113   if (IsDwarfCallChainSamplingSupported()) {
114     ASSERT_TRUE(RecordCmd()->Run({"--call-graph", "dwarf", "sleep", "1"}));
115     ASSERT_TRUE(RecordCmd()->Run({"--call-graph", "dwarf,16384", "sleep", "1"}));
116     ASSERT_TRUE(RecordCmd()->Run({"-g", "sleep", "1"}));
117   } else {
118     GTEST_LOG_(INFO)
119         << "This test does nothing as dwarf callchain sampling is not supported on this device.";
120   }
121 }
122
123 TEST(record_cmd, no_unwind_option) {
124   if (IsDwarfCallChainSamplingSupported()) {
125     ASSERT_TRUE(RecordCmd()->Run({"--call-graph", "dwarf", "--no-unwind", "sleep", "1"}));
126   } else {
127     GTEST_LOG_(INFO)
128         << "This test does nothing as dwarf callchain sampling is not supported on this device.";
129   }
130   ASSERT_FALSE(RecordCmd()->Run({"--no-unwind", "sleep", "1"}));
131 }
132
133 TEST(record_cmd, post_unwind_option) {
134   if (IsDwarfCallChainSamplingSupported()) {
135     ASSERT_TRUE(RecordCmd()->Run({"--call-graph", "dwarf", "--post-unwind", "sleep", "1"}));
136   } else {
137     GTEST_LOG_(INFO)
138         << "This test does nothing as dwarf callchain sampling is not supported on this device.";
139   }
140   ASSERT_FALSE(RecordCmd()->Run({"--post-unwind", "sleep", "1"}));
141   ASSERT_FALSE(
142       RecordCmd()->Run({"--call-graph", "dwarf", "--no-unwind", "--post-unwind", "sleep", "1"}));
143 }
144
145 TEST(record_cmd, existing_processes) {
146   std::vector<std::unique_ptr<Workload>> workloads;
147   CreateProcesses(2, &workloads);
148   std::string pid_list =
149       android::base::StringPrintf("%d,%d", workloads[0]->GetPid(), workloads[1]->GetPid());
150   ASSERT_TRUE(RecordCmd()->Run({"-p", pid_list}));
151 }
152
153 TEST(record_cmd, existing_threads) {
154   std::vector<std::unique_ptr<Workload>> workloads;
155   CreateProcesses(2, &workloads);
156   // Process id can also be used as thread id in linux.
157   std::string tid_list =
158       android::base::StringPrintf("%d,%d", workloads[0]->GetPid(), workloads[1]->GetPid());
159   ASSERT_TRUE(RecordCmd()->Run({"-t", tid_list}));
160 }
161
162 TEST(record_cmd, no_monitored_threads) {
163   ASSERT_FALSE(RecordCmd()->Run({""}));
164 }
165
166 TEST(record_cmd, more_than_one_event_types) {
167   ASSERT_TRUE(RecordCmd()->Run({"-e", "cpu-cycles,cpu-clock", "sleep", "1"}));
168   ASSERT_TRUE(RecordCmd()->Run({"-e", "cpu-cycles", "-e", "cpu-clock", "sleep", "1"}));
169 }
170
171 TEST(record_cmd, cpu_option) {
172   ASSERT_TRUE(RecordCmd()->Run({"--cpu", "0", "sleep", "1"}));
173   ASSERT_TRUE(RecordCmd()->Run({"--cpu", "0", "-a", "sleep", "1"}));
174 }