OSDN Git Service

ANRdaemon: move trace result from /sdcard to /data am: d93aa41807
[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   if (IsRoot()) {
57     ASSERT_TRUE(RunRecordCmd({"-a"}));
58   }
59 }
60
61 TEST(record_cmd, sample_period_option) {
62   ASSERT_TRUE(RunRecordCmd({"-c", "100000"}));
63 }
64
65 TEST(record_cmd, event_option) {
66   ASSERT_TRUE(RunRecordCmd({"-e", "cpu-clock"}));
67 }
68
69 TEST(record_cmd, freq_option) {
70   ASSERT_TRUE(RunRecordCmd({"-f", "99"}));
71   ASSERT_TRUE(RunRecordCmd({"-F", "99"}));
72 }
73
74 TEST(record_cmd, output_file_option) {
75   TemporaryFile tmpfile;
76   ASSERT_TRUE(RecordCmd()->Run({"-o", tmpfile.path, "sleep", SLEEP_SEC}));
77 }
78
79 TEST(record_cmd, dump_kernel_mmap) {
80   TemporaryFile tmpfile;
81   ASSERT_TRUE(RunRecordCmd({}, tmpfile.path));
82   std::unique_ptr<RecordFileReader> reader = 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->header.type == PERF_RECORD_MMAP) {
89       const MmapRecord* mmap_record = static_cast<const MmapRecord*>(record.get());
90       if (mmap_record->filename == DEFAULT_KERNEL_MMAP_NAME) {
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 = RecordFileReader::CreateInstance(tmpfile.path);
103   ASSERT_TRUE(reader != nullptr);
104   const FileHeader& file_header = reader->FileHeader();
105   ASSERT_TRUE(file_header.features[FEAT_BUILD_ID / 8] & (1 << (FEAT_BUILD_ID % 8)));
106   ASSERT_GT(reader->FeatureSectionDescriptors().size(), 0u);
107 }
108
109 TEST(record_cmd, tracepoint_event) {
110   if (IsRoot()) {
111     ASSERT_TRUE(RunRecordCmd({"-a", "-e", "sched:sched_switch"}));
112   }
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)
124         << "This test does nothing as branch stack sampling is 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, 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, no_unwind_option) {
148   if (IsDwarfCallChainSamplingSupported()) {
149     ASSERT_TRUE(RunRecordCmd({"--call-graph", "dwarf", "--no-unwind"}));
150   } else {
151     GTEST_LOG_(INFO)
152         << "This test does nothing as dwarf callchain sampling is not supported on this device.";
153   }
154   ASSERT_FALSE(RunRecordCmd({"--no-unwind"}));
155 }
156
157 TEST(record_cmd, post_unwind_option) {
158   if (IsDwarfCallChainSamplingSupported()) {
159     ASSERT_TRUE(RunRecordCmd({"--call-graph", "dwarf", "--post-unwind"}));
160   } else {
161     GTEST_LOG_(INFO)
162         << "This test does nothing as dwarf callchain sampling is not supported on this device.";
163   }
164   ASSERT_FALSE(RunRecordCmd({"--post-unwind"}));
165   ASSERT_FALSE(
166       RunRecordCmd({"--call-graph", "dwarf", "--no-unwind", "--post-unwind"}));
167 }
168
169 TEST(record_cmd, existing_processes) {
170   std::vector<std::unique_ptr<Workload>> workloads;
171   CreateProcesses(2, &workloads);
172   std::string pid_list =
173       android::base::StringPrintf("%d,%d", workloads[0]->GetPid(), workloads[1]->GetPid());
174   ASSERT_TRUE(RunRecordCmd({"-p", pid_list}));
175 }
176
177 TEST(record_cmd, existing_threads) {
178   std::vector<std::unique_ptr<Workload>> workloads;
179   CreateProcesses(2, &workloads);
180   // Process id can also be used as thread id in linux.
181   std::string tid_list =
182       android::base::StringPrintf("%d,%d", workloads[0]->GetPid(), workloads[1]->GetPid());
183   TemporaryFile tmpfile;
184   ASSERT_TRUE(RunRecordCmd({"-t", tid_list}));
185 }
186
187 TEST(record_cmd, no_monitored_threads) {
188   ASSERT_FALSE(RecordCmd()->Run({""}));
189 }
190
191 TEST(record_cmd, more_than_one_event_types) {
192   ASSERT_TRUE(RunRecordCmd({"-e", "cpu-cycles,cpu-clock"}));
193   ASSERT_TRUE(RunRecordCmd({"-e", "cpu-cycles", "-e", "cpu-clock"}));
194 }
195
196 TEST(record_cmd, cpu_option) {
197   ASSERT_TRUE(RunRecordCmd({"--cpu", "0"}));
198   if (IsRoot()) {
199     ASSERT_TRUE(RunRecordCmd({"--cpu", "0", "-a"}));
200   }
201 }
202
203 TEST(record_cmd, mmap_page_option) {
204   ASSERT_TRUE(RunRecordCmd({"-m", "1"}));
205   ASSERT_FALSE(RunRecordCmd({"-m", "0"}));
206   ASSERT_FALSE(RunRecordCmd({"-m", "7"}));
207 }