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 <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 != nullptr);
84 ASSERT_TRUE(file_header->features[FEAT_BUILD_ID / 8] & (1 << (FEAT_BUILD_ID % 8)));
85 ASSERT_GT(reader->FeatureSectionDescriptors().size(), 0u);
88 TEST(record_cmd, tracepoint_event) {
89 ASSERT_TRUE(RecordCmd()->Run({"-a", "-e", "sched:sched_switch", "sleep", "1"}));
92 TEST(record_cmd, branch_sampling) {
93 if (IsBranchSamplingSupported()) {
94 ASSERT_TRUE(RecordCmd()->Run({"-a", "-b", "sleep", "1"}));
95 ASSERT_TRUE(RecordCmd()->Run({"-j", "any,any_call,any_ret,ind_call", "sleep", "1"}));
96 ASSERT_TRUE(RecordCmd()->Run({"-j", "any,k", "sleep", "1"}));
97 ASSERT_TRUE(RecordCmd()->Run({"-j", "any,u", "sleep", "1"}));
98 ASSERT_FALSE(RecordCmd()->Run({"-j", "u", "sleep", "1"}));
101 << "This test does nothing as branch stack sampling is not supported on this device.";
105 TEST(record_cmd, event_modifier) {
106 ASSERT_TRUE(RecordCmd()->Run({"-e", "cpu-cycles:u", "sleep", "1"}));
109 TEST(record_cmd, fp_callchain_sampling) {
110 ASSERT_TRUE(RecordCmd()->Run({"--call-graph", "fp", "sleep", "1"}));
113 TEST(record_cmd, dwarf_callchain_sampling) {
114 if (IsDwarfCallChainSamplingSupported()) {
115 ASSERT_TRUE(RecordCmd()->Run({"--call-graph", "dwarf", "sleep", "1"}));
116 ASSERT_TRUE(RecordCmd()->Run({"--call-graph", "dwarf,16384", "sleep", "1"}));
117 ASSERT_TRUE(RecordCmd()->Run({"-g", "sleep", "1"}));
120 << "This test does nothing as dwarf callchain sampling is not supported on this device.";
124 TEST(record_cmd, no_unwind_option) {
125 if (IsDwarfCallChainSamplingSupported()) {
126 ASSERT_TRUE(RecordCmd()->Run({"--call-graph", "dwarf", "--no-unwind", "sleep", "1"}));
129 << "This test does nothing as dwarf callchain sampling is not supported on this device.";
131 ASSERT_FALSE(RecordCmd()->Run({"--no-unwind", "sleep", "1"}));
134 TEST(record_cmd, post_unwind_option) {
135 if (IsDwarfCallChainSamplingSupported()) {
136 ASSERT_TRUE(RecordCmd()->Run({"--call-graph", "dwarf", "--post-unwind", "sleep", "1"}));
139 << "This test does nothing as dwarf callchain sampling is not supported on this device.";
141 ASSERT_FALSE(RecordCmd()->Run({"--post-unwind", "sleep", "1"}));
143 RecordCmd()->Run({"--call-graph", "dwarf", "--no-unwind", "--post-unwind", "sleep", "1"}));
146 TEST(record_cmd, existing_processes) {
147 std::vector<std::unique_ptr<Workload>> workloads;
148 CreateProcesses(2, &workloads);
149 std::string pid_list =
150 android::base::StringPrintf("%d,%d", workloads[0]->GetPid(), workloads[1]->GetPid());
151 ASSERT_TRUE(RecordCmd()->Run({"-p", pid_list}));
154 TEST(record_cmd, existing_threads) {
155 std::vector<std::unique_ptr<Workload>> workloads;
156 CreateProcesses(2, &workloads);
157 // Process id can also be used as thread id in linux.
158 std::string tid_list =
159 android::base::StringPrintf("%d,%d", workloads[0]->GetPid(), workloads[1]->GetPid());
160 ASSERT_TRUE(RecordCmd()->Run({"-t", tid_list}));
163 TEST(record_cmd, no_monitored_threads) {
164 ASSERT_FALSE(RecordCmd()->Run({""}));
167 TEST(record_cmd, more_than_one_event_types) {
168 ASSERT_TRUE(RecordCmd()->Run({"-e", "cpu-cycles,cpu-clock", "sleep", "1"}));
169 ASSERT_TRUE(RecordCmd()->Run({"-e", "cpu-cycles", "-e", "cpu-clock", "sleep", "1"}));