blob: c8e59d9758bd5764c7496e7fe4b79560277d5fea [file] [log] [blame]
Yabin Cui323e9452015-04-20 18:07:17 -07001/*
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 <inttypes.h>
18#include <stdio.h>
19#include <chrono>
20#include <string>
21#include <vector>
22
23#include <base/logging.h>
24#include <base/strings.h>
25
26#include "command.h"
27#include "environment.h"
Yabin Cuied91cd92015-04-28 15:54:13 -070028#include "event_selection_set.h"
Yabin Cui323e9452015-04-20 18:07:17 -070029#include "event_type.h"
30#include "perf_event.h"
31#include "utils.h"
32#include "workload.h"
33
34static std::vector<std::string> default_measured_event_types{
35 "cpu-cycles", "stalled-cycles-frontend", "stalled-cycles-backend", "instructions",
36 "branch-instructions", "branch-misses", "task-clock", "context-switches", "page-faults",
37};
38
39class StatCommandImpl {
40 public:
41 StatCommandImpl() : verbose_mode_(false), system_wide_collection_(false) {
42 }
43
44 bool Run(const std::vector<std::string>& args);
45
46 private:
47 bool ParseOptions(const std::vector<std::string>& args, std::vector<std::string>* non_option_args);
Yabin Cuied91cd92015-04-28 15:54:13 -070048 bool AddMeasuredEventType(const std::string& event_type_name, bool report_unsupported_type = true);
Yabin Cui323e9452015-04-20 18:07:17 -070049 bool AddDefaultMeasuredEventTypes();
Yabin Cuied91cd92015-04-28 15:54:13 -070050 bool ShowCounters(const std::map<const EventType*, std::vector<PerfCounter>>& counters_map,
51 std::chrono::steady_clock::duration counting_duration);
Yabin Cui323e9452015-04-20 18:07:17 -070052
Yabin Cuied91cd92015-04-28 15:54:13 -070053 EventSelectionSet event_selection_set_;
Yabin Cui323e9452015-04-20 18:07:17 -070054 bool verbose_mode_;
55 bool system_wide_collection_;
56};
57
58bool StatCommandImpl::Run(const std::vector<std::string>& args) {
59 // 1. Parse options.
60 std::vector<std::string> workload_args;
61 if (!ParseOptions(args, &workload_args)) {
62 return false;
63 }
64
65 // 2. Add default measured event types.
Yabin Cuied91cd92015-04-28 15:54:13 -070066 if (event_selection_set_.Empty()) {
Yabin Cui323e9452015-04-20 18:07:17 -070067 if (!AddDefaultMeasuredEventTypes()) {
68 return false;
69 }
70 }
71
72 // 3. Create workload.
73 if (workload_args.empty()) {
Yabin Cuied91cd92015-04-28 15:54:13 -070074 // TODO: change default workload to sleep 99999, and run stat until Ctrl-C.
Yabin Cui323e9452015-04-20 18:07:17 -070075 workload_args = std::vector<std::string>({"sleep", "1"});
76 }
77 std::unique_ptr<Workload> workload = Workload::CreateWorkload(workload_args);
78 if (workload == nullptr) {
79 return false;
80 }
81
82 // 4. Open perf_event_files.
83 if (system_wide_collection_) {
Yabin Cuied91cd92015-04-28 15:54:13 -070084 if (!event_selection_set_.OpenEventFilesForAllCpus()) {
Yabin Cui323e9452015-04-20 18:07:17 -070085 return false;
86 }
87 } else {
Yabin Cuied91cd92015-04-28 15:54:13 -070088 event_selection_set_.EnableOnExec();
89 if (!event_selection_set_.OpenEventFilesForProcess(workload->GetPid())) {
Yabin Cui323e9452015-04-20 18:07:17 -070090 return false;
91 }
92 }
93
94 // 5. Count events while workload running.
95 auto start_time = std::chrono::steady_clock::now();
Yabin Cuied91cd92015-04-28 15:54:13 -070096 // If monitoring only one process, we use the enable_on_exec flag, and don't need to start
97 // counting manually.
98 if (system_wide_collection_) {
99 if (!event_selection_set_.EnableEvents()) {
100 return false;
101 }
Yabin Cui323e9452015-04-20 18:07:17 -0700102 }
103 if (!workload->Start()) {
104 return false;
105 }
106 workload->WaitFinish();
Yabin Cui323e9452015-04-20 18:07:17 -0700107 auto end_time = std::chrono::steady_clock::now();
108
109 // 6. Read and print counters.
Yabin Cuied91cd92015-04-28 15:54:13 -0700110 std::map<const EventType*, std::vector<PerfCounter>> counters_map;
111 if (!event_selection_set_.ReadCounters(&counters_map)) {
Yabin Cui323e9452015-04-20 18:07:17 -0700112 return false;
113 }
Yabin Cuied91cd92015-04-28 15:54:13 -0700114 if (!ShowCounters(counters_map, end_time - start_time)) {
Yabin Cui323e9452015-04-20 18:07:17 -0700115 return false;
116 }
Yabin Cui323e9452015-04-20 18:07:17 -0700117 return true;
118}
119
120bool StatCommandImpl::ParseOptions(const std::vector<std::string>& args,
121 std::vector<std::string>* non_option_args) {
122 size_t i;
Yabin Cuied91cd92015-04-28 15:54:13 -0700123 for (i = 1; i < args.size() && args[i].size() > 0 && args[i][0] == '-'; ++i) {
Yabin Cui323e9452015-04-20 18:07:17 -0700124 if (args[i] == "-a") {
125 system_wide_collection_ = true;
126 } else if (args[i] == "-e") {
Yabin Cuied91cd92015-04-28 15:54:13 -0700127 if (!NextArgumentOrError(args, &i)) {
Yabin Cui323e9452015-04-20 18:07:17 -0700128 return false;
129 }
Yabin Cui323e9452015-04-20 18:07:17 -0700130 std::vector<std::string> event_types = android::base::Split(args[i], ",");
131 for (auto& event_type : event_types) {
132 if (!AddMeasuredEventType(event_type)) {
133 return false;
134 }
135 }
136 } else if (args[i] == "--verbose") {
137 verbose_mode_ = true;
138 } else {
139 LOG(ERROR) << "Unknown option for stat command: " << args[i];
140 LOG(ERROR) << "Try `simpleperf help stat`";
141 return false;
142 }
143 }
144
145 if (non_option_args != nullptr) {
146 non_option_args->clear();
147 for (; i < args.size(); ++i) {
148 non_option_args->push_back(args[i]);
149 }
150 }
151 return true;
152}
153
154bool StatCommandImpl::AddMeasuredEventType(const std::string& event_type_name,
Yabin Cuied91cd92015-04-28 15:54:13 -0700155 bool report_unsupported_type) {
156 const EventType* event_type =
157 EventTypeFactory::FindEventTypeByName(event_type_name, report_unsupported_type);
Yabin Cui323e9452015-04-20 18:07:17 -0700158 if (event_type == nullptr) {
Yabin Cui323e9452015-04-20 18:07:17 -0700159 return false;
160 }
Yabin Cuied91cd92015-04-28 15:54:13 -0700161 event_selection_set_.AddEventType(*event_type);
Yabin Cui323e9452015-04-20 18:07:17 -0700162 return true;
163}
164
165bool StatCommandImpl::AddDefaultMeasuredEventTypes() {
166 for (auto& name : default_measured_event_types) {
167 // It is not an error when some event types in the default list are not supported by the kernel.
168 AddMeasuredEventType(name, false);
169 }
Yabin Cuied91cd92015-04-28 15:54:13 -0700170 if (event_selection_set_.Empty()) {
Yabin Cui323e9452015-04-20 18:07:17 -0700171 LOG(ERROR) << "Failed to add any supported default measured types";
172 return false;
173 }
174 return true;
175}
176
Yabin Cuied91cd92015-04-28 15:54:13 -0700177bool StatCommandImpl::ShowCounters(
178 const std::map<const EventType*, std::vector<PerfCounter>>& counters_map,
179 std::chrono::steady_clock::duration counting_duration) {
Yabin Cui323e9452015-04-20 18:07:17 -0700180 printf("Performance counter statistics:\n\n");
Yabin Cui323e9452015-04-20 18:07:17 -0700181
Yabin Cuied91cd92015-04-28 15:54:13 -0700182 for (auto& pair : counters_map) {
183 auto& event_type = pair.first;
184 auto& counters = pair.second;
Yabin Cui323e9452015-04-20 18:07:17 -0700185 if (verbose_mode_) {
Yabin Cuied91cd92015-04-28 15:54:13 -0700186 for (auto& counter : counters) {
187 printf("%s: value %'" PRId64 ", time_enabled %" PRId64 ", time_running %" PRId64
Yabin Cui323e9452015-04-20 18:07:17 -0700188 ", id %" PRId64 "\n",
Yabin Cuied91cd92015-04-28 15:54:13 -0700189 event_selection_set_.FindEventFileNameById(counter.id).c_str(), counter.value,
190 counter.time_enabled, counter.time_running, counter.id);
Yabin Cui323e9452015-04-20 18:07:17 -0700191 }
192 }
193
Yabin Cuied91cd92015-04-28 15:54:13 -0700194 PerfCounter sum_counter;
195 memset(&sum_counter, 0, sizeof(sum_counter));
196 for (auto& counter : counters) {
197 sum_counter.value += counter.value;
198 sum_counter.time_enabled += counter.time_enabled;
199 sum_counter.time_running += counter.time_running;
200 }
Yabin Cui323e9452015-04-20 18:07:17 -0700201 bool scaled = false;
Yabin Cuied91cd92015-04-28 15:54:13 -0700202 int64_t scaled_count = sum_counter.value;
203 if (sum_counter.time_running < sum_counter.time_enabled) {
204 if (sum_counter.time_running == 0) {
Yabin Cui323e9452015-04-20 18:07:17 -0700205 scaled_count = 0;
206 } else {
207 scaled = true;
Yabin Cuied91cd92015-04-28 15:54:13 -0700208 scaled_count = static_cast<int64_t>(static_cast<double>(sum_counter.value) *
209 sum_counter.time_enabled / sum_counter.time_running);
Yabin Cui323e9452015-04-20 18:07:17 -0700210 }
211 }
212 printf("%'30" PRId64 "%s %s\n", scaled_count, scaled ? "(scaled)" : " ",
Yabin Cuied91cd92015-04-28 15:54:13 -0700213 event_type->name);
Yabin Cui323e9452015-04-20 18:07:17 -0700214 }
Yabin Cuied91cd92015-04-28 15:54:13 -0700215 printf("\nTotal test time: %lf seconds.\n",
Yabin Cui323e9452015-04-20 18:07:17 -0700216 std::chrono::duration_cast<std::chrono::duration<double>>(counting_duration).count());
217 return true;
218}
219
220class StatCommand : public Command {
221 public:
222 StatCommand()
223 : Command("stat", "gather performance counter information",
224 "Usage: simpleperf stat [options] [command [command-args]]\n"
225 " Gather performance counter information of running [command]. If [command]\n"
226 " is not specified, sleep 1 is used instead.\n\n"
227 " -a Collect system-wide information.\n"
228 " -e event1,event2,... Select the event list to count. Use `simpleperf list`\n"
229 " to find all possible event names.\n"
Yabin Cuied91cd92015-04-28 15:54:13 -0700230 " --verbose Show result in verbose mode.\n") {
Yabin Cui323e9452015-04-20 18:07:17 -0700231 }
232
233 bool Run(const std::vector<std::string>& args) override {
Yabin Cuied91cd92015-04-28 15:54:13 -0700234 // Keep the implementation in StatCommandImpl, so the resources used are cleaned up when the
235 // command finishes. This is useful when we need to call some commands multiple times, like
236 // in unit tests.
Yabin Cui323e9452015-04-20 18:07:17 -0700237 StatCommandImpl impl;
238 return impl.Run(args);
239 }
240};
241
242StatCommand stat_command;