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>
22 #include "environment.h"
23 #include "event_selection_set.h"
25 #include "record_file.h"
26 #include "test_util.h"
28 using namespace PerfFileFormat;
30 static std::unique_ptr<Command> RecordCmd() {
31 return CreateCommandInstance("record");
34 TEST(record_cmd, no_options) {
35 ASSERT_TRUE(RecordCmd()->Run({"sleep", "1"}));
38 TEST(record_cmd, system_wide_option) {
39 ASSERT_TRUE(RecordCmd()->Run({"-a", "sleep", "1"}));
42 TEST(record_cmd, sample_period_option) {
43 ASSERT_TRUE(RecordCmd()->Run({"-c", "100000", "sleep", "1"}));
46 TEST(record_cmd, event_option) {
47 ASSERT_TRUE(RecordCmd()->Run({"-e", "cpu-clock", "sleep", "1"}));
50 TEST(record_cmd, freq_option) {
51 ASSERT_TRUE(RecordCmd()->Run({"-f", "99", "sleep", "1"}));
52 ASSERT_TRUE(RecordCmd()->Run({"-F", "99", "sleep", "1"}));
55 TEST(record_cmd, output_file_option) {
56 ASSERT_TRUE(RecordCmd()->Run({"-o", "perf2.data", "sleep", "1"}));
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;
75 ASSERT_TRUE(have_kernel_mmap);
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);
87 TEST(record_cmd, tracepoint_event) {
88 ASSERT_TRUE(RecordCmd()->Run({"-a", "-e", "sched:sched_switch", "sleep", "1"}));
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"}));
100 << "This test does nothing as branch stack sampling is not supported on this device.";
104 TEST(record_cmd, event_modifier) {
105 ASSERT_TRUE(RecordCmd()->Run({"-e", "cpu-cycles:u", "sleep", "1"}));
108 TEST(record_cmd, fp_callchain_sampling) {
109 ASSERT_TRUE(RecordCmd()->Run({"--call-graph", "fp", "sleep", "1"}));
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"}));
119 << "This test does nothing as dwarf callchain sampling is not supported on this device.";
123 TEST(record_cmd, no_unwind_option) {
124 if (IsDwarfCallChainSamplingSupported()) {
125 ASSERT_TRUE(RecordCmd()->Run({"--call-graph", "dwarf", "--no-unwind", "sleep", "1"}));
128 << "This test does nothing as dwarf callchain sampling is not supported on this device.";
130 ASSERT_FALSE(RecordCmd()->Run({"--no-unwind", "sleep", "1"}));
133 TEST(record_cmd, post_unwind_option) {
134 if (IsDwarfCallChainSamplingSupported()) {
135 ASSERT_TRUE(RecordCmd()->Run({"--call-graph", "dwarf", "--post-unwind", "sleep", "1"}));
138 << "This test does nothing as dwarf callchain sampling is not supported on this device.";
140 ASSERT_FALSE(RecordCmd()->Run({"--post-unwind", "sleep", "1"}));
142 RecordCmd()->Run({"--call-graph", "dwarf", "--no-unwind", "--post-unwind", "sleep", "1"}));
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}));
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}));
162 TEST(record_cmd, no_monitored_threads) {
163 ASSERT_FALSE(RecordCmd()->Run({""}));
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"}));
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"}));