2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <gtest/gtest.h>
19 #include <android-base/stringprintf.h>
20 #include <android-base/test_utils.h>
25 #include "environment.h"
26 #include "event_selection_set.h"
27 #include "get_test_data.h"
29 #include "record_file.h"
30 #include "test_util.h"
32 using namespace PerfFileFormat;
34 static std::unique_ptr<Command> RecordCmd() {
35 return CreateCommandInstance("record");
38 static bool RunRecordCmd(std::vector<std::string> v, const char* output_file = nullptr) {
39 std::unique_ptr<TemporaryFile> tmpfile;
41 if (output_file != nullptr) {
42 out_file = output_file;
44 tmpfile.reset(new TemporaryFile);
45 out_file = tmpfile->path;
47 v.insert(v.end(), {"-o", out_file, "sleep", SLEEP_SEC});
48 return RecordCmd()->Run(v);
51 TEST(record_cmd, no_options) {
52 ASSERT_TRUE(RunRecordCmd({}));
55 TEST(record_cmd, system_wide_option) {
57 ASSERT_TRUE(RunRecordCmd({"-a"}));
61 TEST(record_cmd, sample_period_option) {
62 ASSERT_TRUE(RunRecordCmd({"-c", "100000"}));
65 TEST(record_cmd, event_option) {
66 ASSERT_TRUE(RunRecordCmd({"-e", "cpu-clock"}));
69 TEST(record_cmd, freq_option) {
70 ASSERT_TRUE(RunRecordCmd({"-f", "99"}));
71 ASSERT_TRUE(RunRecordCmd({"-F", "99"}));
74 TEST(record_cmd, output_file_option) {
75 TemporaryFile tmpfile;
76 ASSERT_TRUE(RecordCmd()->Run({"-o", tmpfile.path, "sleep", SLEEP_SEC}));
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;
96 ASSERT_TRUE(have_kernel_mmap);
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);
109 TEST(record_cmd, tracepoint_event) {
111 ASSERT_TRUE(RunRecordCmd({"-a", "-e", "sched:sched_switch"}));
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"}));
124 << "This test does nothing as branch stack sampling is not supported on this device.";
128 TEST(record_cmd, event_modifier) {
129 ASSERT_TRUE(RunRecordCmd({"-e", "cpu-cycles:u"}));
132 TEST(record_cmd, fp_callchain_sampling) {
133 ASSERT_TRUE(RunRecordCmd({"--call-graph", "fp"}));
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"}));
143 << "This test does nothing as dwarf callchain sampling is not supported on this device.";
147 TEST(record_cmd, no_unwind_option) {
148 if (IsDwarfCallChainSamplingSupported()) {
149 ASSERT_TRUE(RunRecordCmd({"--call-graph", "dwarf", "--no-unwind"}));
152 << "This test does nothing as dwarf callchain sampling is not supported on this device.";
154 ASSERT_FALSE(RunRecordCmd({"--no-unwind"}));
157 TEST(record_cmd, post_unwind_option) {
158 if (IsDwarfCallChainSamplingSupported()) {
159 ASSERT_TRUE(RunRecordCmd({"--call-graph", "dwarf", "--post-unwind"}));
162 << "This test does nothing as dwarf callchain sampling is not supported on this device.";
164 ASSERT_FALSE(RunRecordCmd({"--post-unwind"}));
166 RunRecordCmd({"--call-graph", "dwarf", "--no-unwind", "--post-unwind"}));
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}));
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}));
187 TEST(record_cmd, no_monitored_threads) {
188 ASSERT_FALSE(RecordCmd()->Run({""}));
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"}));
196 TEST(record_cmd, cpu_option) {
197 ASSERT_TRUE(RunRecordCmd({"--cpu", "0"}));
199 ASSERT_TRUE(RunRecordCmd({"--cpu", "0", "-a"}));
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"}));