OSDN Git Service

am ad06e79d: (-s ours) resolved conflicts for merge of 743e4b08 to mnc-dev-plus-aosp
[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 <base/stringprintf.h>
20
21 #include "command.h"
22 #include "environment.h"
23 #include "event_selection_set.h"
24 #include "record.h"
25 #include "record_file.h"
26 #include "test_util.h"
27
28 using namespace PerfFileFormat;
29
30 static std::unique_ptr<Command> RecordCmd() {
31   return CreateCommandInstance("record");
32 }
33
34 TEST(record_cmd, no_options) {
35   ASSERT_TRUE(RecordCmd()->Run({"sleep", "1"}));
36 }
37
38 TEST(record_cmd, system_wide_option) {
39   ASSERT_TRUE(RecordCmd()->Run({"-a", "sleep", "1"}));
40 }
41
42 TEST(record_cmd, sample_period_option) {
43   ASSERT_TRUE(RecordCmd()->Run({"-c", "100000", "sleep", "1"}));
44 }
45
46 TEST(record_cmd, event_option) {
47   ASSERT_TRUE(RecordCmd()->Run({"-e", "cpu-clock", "sleep", "1"}));
48 }
49
50 TEST(record_cmd, freq_option) {
51   ASSERT_TRUE(RecordCmd()->Run({"-f", "99", "sleep", "1"}));
52   ASSERT_TRUE(RecordCmd()->Run({"-F", "99", "sleep", "1"}));
53 }
54
55 TEST(record_cmd, output_file_option) {
56   ASSERT_TRUE(RecordCmd()->Run({"-o", "perf2.data", "sleep", "1"}));
57 }
58
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;
71         break;
72       }
73     }
74   }
75   ASSERT_TRUE(have_kernel_mmap);
76 }
77
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);
86 }
87
88 TEST(record_cmd, tracepoint_event) {
89   ASSERT_TRUE(RecordCmd()->Run({"-a", "-e", "sched:sched_switch", "sleep", "1"}));
90 }
91
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"}));
99   } else {
100     GTEST_LOG_(INFO)
101         << "This test does nothing as branch stack sampling is not supported on this device.";
102   }
103 }
104
105 TEST(record_cmd, event_modifier) {
106   ASSERT_TRUE(RecordCmd()->Run({"-e", "cpu-cycles:u", "sleep", "1"}));
107 }
108
109 TEST(record_cmd, fp_callchain_sampling) {
110   ASSERT_TRUE(RecordCmd()->Run({"--call-graph", "fp", "sleep", "1"}));
111 }
112
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"}));
118   } else {
119     GTEST_LOG_(INFO)
120         << "This test does nothing as dwarf callchain sampling is not supported on this device.";
121   }
122 }
123
124 TEST(record_cmd, existing_processes) {
125   std::vector<std::unique_ptr<Workload>> workloads;
126   CreateProcesses(2, &workloads);
127   std::string pid_list =
128       android::base::StringPrintf("%d,%d", workloads[0]->GetPid(), workloads[1]->GetPid());
129   ASSERT_TRUE(RecordCmd()->Run({"-p", pid_list}));
130 }
131
132 TEST(record_cmd, existing_threads) {
133   std::vector<std::unique_ptr<Workload>> workloads;
134   CreateProcesses(2, &workloads);
135   // Process id can also be used as thread id in linux.
136   std::string tid_list =
137       android::base::StringPrintf("%d,%d", workloads[0]->GetPid(), workloads[1]->GetPid());
138   ASSERT_TRUE(RecordCmd()->Run({"-t", tid_list}));
139 }
140
141 TEST(record_cmd, no_monitored_threads) {
142   ASSERT_FALSE(RecordCmd()->Run({""}));
143 }
144
145 TEST(record_cmd, more_than_one_event_types) {
146   ASSERT_TRUE(RecordCmd()->Run({"-e", "cpu-cycles,cpu-clock", "sleep", "1"}));
147   ASSERT_TRUE(RecordCmd()->Run({"-e", "cpu-cycles", "-e", "cpu-clock", "sleep", "1"}));
148 }