Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 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 <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 Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 28 | #include "event_selection_set.h" |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 29 | #include "event_type.h" |
| 30 | #include "perf_event.h" |
| 31 | #include "utils.h" |
| 32 | #include "workload.h" |
| 33 | |
| 34 | static 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 | |
| 39 | class 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 Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 48 | bool AddMeasuredEventType(const std::string& event_type_name, bool report_unsupported_type = true); |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 49 | bool AddDefaultMeasuredEventTypes(); |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 50 | bool ShowCounters(const std::map<const EventType*, std::vector<PerfCounter>>& counters_map, |
| 51 | std::chrono::steady_clock::duration counting_duration); |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 52 | |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 53 | EventSelectionSet event_selection_set_; |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 54 | bool verbose_mode_; |
| 55 | bool system_wide_collection_; |
| 56 | }; |
| 57 | |
| 58 | bool 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 Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 66 | if (event_selection_set_.Empty()) { |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 67 | if (!AddDefaultMeasuredEventTypes()) { |
| 68 | return false; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // 3. Create workload. |
| 73 | if (workload_args.empty()) { |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 74 | // TODO: change default workload to sleep 99999, and run stat until Ctrl-C. |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 75 | 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 Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 84 | if (!event_selection_set_.OpenEventFilesForAllCpus()) { |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 85 | return false; |
| 86 | } |
| 87 | } else { |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 88 | event_selection_set_.EnableOnExec(); |
| 89 | if (!event_selection_set_.OpenEventFilesForProcess(workload->GetPid())) { |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 90 | return false; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | // 5. Count events while workload running. |
| 95 | auto start_time = std::chrono::steady_clock::now(); |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 96 | // 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 Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 102 | } |
| 103 | if (!workload->Start()) { |
| 104 | return false; |
| 105 | } |
| 106 | workload->WaitFinish(); |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 107 | auto end_time = std::chrono::steady_clock::now(); |
| 108 | |
| 109 | // 6. Read and print counters. |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 110 | std::map<const EventType*, std::vector<PerfCounter>> counters_map; |
| 111 | if (!event_selection_set_.ReadCounters(&counters_map)) { |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 112 | return false; |
| 113 | } |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 114 | if (!ShowCounters(counters_map, end_time - start_time)) { |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 115 | return false; |
| 116 | } |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 117 | return true; |
| 118 | } |
| 119 | |
| 120 | bool StatCommandImpl::ParseOptions(const std::vector<std::string>& args, |
| 121 | std::vector<std::string>* non_option_args) { |
| 122 | size_t i; |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 123 | for (i = 1; i < args.size() && args[i].size() > 0 && args[i][0] == '-'; ++i) { |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 124 | if (args[i] == "-a") { |
| 125 | system_wide_collection_ = true; |
| 126 | } else if (args[i] == "-e") { |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 127 | if (!NextArgumentOrError(args, &i)) { |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 128 | return false; |
| 129 | } |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 130 | 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 | |
| 154 | bool StatCommandImpl::AddMeasuredEventType(const std::string& event_type_name, |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 155 | bool report_unsupported_type) { |
| 156 | const EventType* event_type = |
| 157 | EventTypeFactory::FindEventTypeByName(event_type_name, report_unsupported_type); |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 158 | if (event_type == nullptr) { |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 159 | return false; |
| 160 | } |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 161 | event_selection_set_.AddEventType(*event_type); |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 162 | return true; |
| 163 | } |
| 164 | |
| 165 | bool 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 Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 170 | if (event_selection_set_.Empty()) { |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 171 | LOG(ERROR) << "Failed to add any supported default measured types"; |
| 172 | return false; |
| 173 | } |
| 174 | return true; |
| 175 | } |
| 176 | |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 177 | bool StatCommandImpl::ShowCounters( |
| 178 | const std::map<const EventType*, std::vector<PerfCounter>>& counters_map, |
| 179 | std::chrono::steady_clock::duration counting_duration) { |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 180 | printf("Performance counter statistics:\n\n"); |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 181 | |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 182 | for (auto& pair : counters_map) { |
| 183 | auto& event_type = pair.first; |
| 184 | auto& counters = pair.second; |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 185 | if (verbose_mode_) { |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 186 | for (auto& counter : counters) { |
| 187 | printf("%s: value %'" PRId64 ", time_enabled %" PRId64 ", time_running %" PRId64 |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 188 | ", id %" PRId64 "\n", |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 189 | event_selection_set_.FindEventFileNameById(counter.id).c_str(), counter.value, |
| 190 | counter.time_enabled, counter.time_running, counter.id); |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 194 | 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 Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 201 | bool scaled = false; |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 202 | 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 Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 205 | scaled_count = 0; |
| 206 | } else { |
| 207 | scaled = true; |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 208 | scaled_count = static_cast<int64_t>(static_cast<double>(sum_counter.value) * |
| 209 | sum_counter.time_enabled / sum_counter.time_running); |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | printf("%'30" PRId64 "%s %s\n", scaled_count, scaled ? "(scaled)" : " ", |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 213 | event_type->name); |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 214 | } |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 215 | printf("\nTotal test time: %lf seconds.\n", |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 216 | std::chrono::duration_cast<std::chrono::duration<double>>(counting_duration).count()); |
| 217 | return true; |
| 218 | } |
| 219 | |
| 220 | class 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 Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 230 | " --verbose Show result in verbose mode.\n") { |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | bool Run(const std::vector<std::string>& args) override { |
Yabin Cui | ed91cd9 | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 234 | // 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 Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 237 | StatCommandImpl impl; |
| 238 | return impl.Run(args); |
| 239 | } |
| 240 | }; |
| 241 | |
| 242 | StatCommand stat_command; |