OSDN Git Service

Merge "Fix google-explicit-constructor warnings." am: 7ec2e36 am: 1ce5f21
[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 <memory>
23
24 #include "command.h"
25 #include "environment.h"
26 #include "event_selection_set.h"
27 #include "get_test_data.h"
28 #include "record.h"
29 #include "record_file.h"
30 #include "test_util.h"
31
32 using namespace PerfFileFormat;
33
34 static std::unique_ptr<Command> RecordCmd() {
35   return CreateCommandInstance("record");
36 }
37
38 static bool RunRecordCmd(std::vector<std::string> v, const char* output_file = nullptr) {
39   std::unique_ptr<TemporaryFile> tmpfile;
40   std::string out_file;
41   if (output_file != nullptr) {
42     out_file = output_file;
43   } else {
44     tmpfile.reset(new TemporaryFile);
45     out_file = tmpfile->path;
46   }
47   v.insert(v.end(), {"-o", out_file, "sleep", SLEEP_SEC});
48   return RecordCmd()->Run(v);
49 }
50
51 TEST(record_cmd, no_options) {
52   ASSERT_TRUE(RunRecordCmd({}));
53 }
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 = RecordFileReader::CreateInstance(tmpfile.path);
81   ASSERT_TRUE(reader != nullptr);
82   std::vector<std::unique_ptr<Record>> records = reader->DataSection();
83   ASSERT_GT(records.size(), 0U);
84   bool have_kernel_mmap = false;
85   for (auto& record : records) {
86     if (record->header.type == PERF_RECORD_MMAP) {
87       const MmapRecord* mmap_record = static_cast<const MmapRecord*>(record.get());
88       if (mmap_record->filename == DEFAULT_KERNEL_MMAP_NAME) {
89         have_kernel_mmap = true;
90         break;
91       }
92     }
93   }
94   ASSERT_TRUE(have_kernel_mmap);
95 }
96
97 TEST(record_cmd, dump_build_id_feature) {
98   TemporaryFile tmpfile;
99   ASSERT_TRUE(RunRecordCmd({}, tmpfile.path));
100   std::unique_ptr<RecordFileReader> reader = RecordFileReader::CreateInstance(tmpfile.path);
101   ASSERT_TRUE(reader != nullptr);
102   const FileHeader& file_header = reader->FileHeader();
103   ASSERT_TRUE(file_header.features[FEAT_BUILD_ID / 8] & (1 << (FEAT_BUILD_ID % 8)));
104   ASSERT_GT(reader->FeatureSectionDescriptors().size(), 0u);
105 }
106
107 TEST(record_cmd, tracepoint_event) {
108   TEST_IN_ROOT(ASSERT_TRUE(RunRecordCmd({"-a", "-e", "sched:sched_switch"})));
109 }
110
111 TEST(record_cmd, branch_sampling) {
112   if (IsBranchSamplingSupported()) {
113     ASSERT_TRUE(RunRecordCmd({"-b"}));
114     ASSERT_TRUE(RunRecordCmd({"-j", "any,any_call,any_ret,ind_call"}));
115     ASSERT_TRUE(RunRecordCmd({"-j", "any,k"}));
116     ASSERT_TRUE(RunRecordCmd({"-j", "any,u"}));
117     ASSERT_FALSE(RunRecordCmd({"-j", "u"}));
118   } else {
119     GTEST_LOG_(INFO)
120         << "This test does nothing as branch stack sampling is not supported on this device.";
121   }
122 }
123
124 TEST(record_cmd, event_modifier) {
125   ASSERT_TRUE(RunRecordCmd({"-e", "cpu-cycles:u"}));
126 }
127
128 TEST(record_cmd, fp_callchain_sampling) {
129   ASSERT_TRUE(RunRecordCmd({"--call-graph", "fp"}));
130 }
131
132 TEST(record_cmd, system_wide_fp_callchain_sampling) {
133   TEST_IN_ROOT(ASSERT_TRUE(RunRecordCmd({"-a", "--call-graph", "fp"})));
134 }
135
136 TEST(record_cmd, dwarf_callchain_sampling) {
137   if (IsDwarfCallChainSamplingSupported()) {
138     ASSERT_TRUE(RunRecordCmd({"--call-graph", "dwarf"}));
139     ASSERT_TRUE(RunRecordCmd({"--call-graph", "dwarf,16384"}));
140     ASSERT_TRUE(RunRecordCmd({"-g"}));
141   } else {
142     GTEST_LOG_(INFO)
143         << "This test does nothing as dwarf callchain sampling is not supported on this device.";
144   }
145 }
146
147 TEST(record_cmd, system_wide_dwarf_callchain_sampling) {
148   if (IsDwarfCallChainSamplingSupported()) {
149     TEST_IN_ROOT(RunRecordCmd({"-a", "--call-graph", "dwarf"}));
150   } else {
151     GTEST_LOG_(INFO)
152         << "This test does nothing as dwarf callchain sampling is not supported on this device.";
153   }
154 }
155
156 TEST(record_cmd, no_unwind_option) {
157   if (IsDwarfCallChainSamplingSupported()) {
158     ASSERT_TRUE(RunRecordCmd({"--call-graph", "dwarf", "--no-unwind"}));
159   } else {
160     GTEST_LOG_(INFO)
161         << "This test does nothing as dwarf callchain sampling is not supported on this device.";
162   }
163   ASSERT_FALSE(RunRecordCmd({"--no-unwind"}));
164 }
165
166 TEST(record_cmd, post_unwind_option) {
167   if (IsDwarfCallChainSamplingSupported()) {
168     ASSERT_TRUE(RunRecordCmd({"--call-graph", "dwarf", "--post-unwind"}));
169   } else {
170     GTEST_LOG_(INFO)
171         << "This test does nothing as dwarf callchain sampling is not supported on this device.";
172   }
173   ASSERT_FALSE(RunRecordCmd({"--post-unwind"}));
174   ASSERT_FALSE(
175       RunRecordCmd({"--call-graph", "dwarf", "--no-unwind", "--post-unwind"}));
176 }
177
178 TEST(record_cmd, existing_processes) {
179   std::vector<std::unique_ptr<Workload>> workloads;
180   CreateProcesses(2, &workloads);
181   std::string pid_list =
182       android::base::StringPrintf("%d,%d", workloads[0]->GetPid(), workloads[1]->GetPid());
183   TemporaryFile tmpfile;
184   ASSERT_TRUE(RecordCmd()->Run({"-p", pid_list, "-o", tmpfile.path}));
185 }
186
187 TEST(record_cmd, existing_threads) {
188   std::vector<std::unique_ptr<Workload>> workloads;
189   CreateProcesses(2, &workloads);
190   // Process id can also be used as thread id in linux.
191   std::string tid_list =
192       android::base::StringPrintf("%d,%d", workloads[0]->GetPid(), workloads[1]->GetPid());
193   TemporaryFile tmpfile;
194   ASSERT_TRUE(RecordCmd()->Run({"-t", tid_list, "-o", tmpfile.path}));
195 }
196
197 TEST(record_cmd, no_monitored_threads) {
198   ASSERT_FALSE(RecordCmd()->Run({""}));
199 }
200
201 TEST(record_cmd, more_than_one_event_types) {
202   ASSERT_TRUE(RunRecordCmd({"-e", "cpu-cycles,cpu-clock"}));
203   ASSERT_TRUE(RunRecordCmd({"-e", "cpu-cycles", "-e", "cpu-clock"}));
204 }
205
206 TEST(record_cmd, cpu_option) {
207   ASSERT_TRUE(RunRecordCmd({"--cpu", "0"}));
208   TEST_IN_ROOT(ASSERT_TRUE(RunRecordCmd({"--cpu", "0", "-a"})));
209 }
210
211 TEST(record_cmd, mmap_page_option) {
212   ASSERT_TRUE(RunRecordCmd({"-m", "1"}));
213   ASSERT_FALSE(RunRecordCmd({"-m", "0"}));
214   ASSERT_FALSE(RunRecordCmd({"-m", "7"}));
215 }