1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define SIMPLEPERF_EXPORT __attribute__((visibility("default")))
#include "include/simpleperf.h"
#include <memory>
#include <set>
#include <string>
#include <vector>
#include <android-base/logging.h>
#include "environment.h"
#include "event_attr.h"
#include "event_fd.h"
#include "event_selection_set.h"
#include "event_type.h"
namespace simpleperf {
std::vector<std::string> GetAllEvents() {
std::vector<std::string> result;
if (!CheckPerfEventLimit()) {
return result;
}
for (auto& type : GetAllEventTypes()) {
perf_event_attr attr = CreateDefaultPerfEventAttr(type);
if (IsEventAttrSupported(attr)) {
result.push_back(type.name);
}
}
return result;
}
bool IsEventSupported(const std::string& name) {
if (!CheckPerfEventLimit()) {
return false;
}
std::unique_ptr<EventTypeAndModifier> type = ParseEventType(name);
if (type == nullptr) {
return false;
}
perf_event_attr attr = CreateDefaultPerfEventAttr(type->event_type);
return IsEventAttrSupported(attr);
}
class PerfEventSetImpl : public PerfEventSet {
public:
virtual ~PerfEventSetImpl() {}
bool AddEvent(const std::string& name) override {
if (!IsEventSupported(name)) {
return false;
}
event_names_.push_back(name);
return true;
}
bool MonitorCurrentProcess() override {
whole_process_ = true;
return true;
}
bool MonitorCurrentThread() override {
whole_process_ = false;
threads_.insert(gettid());
return true;
}
bool MonitorThreadsInCurrentProcess(const std::vector<pid_t>& threads) override {
whole_process_ = false;
std::vector<pid_t> tids = GetThreadsInProcess(getpid());
for (auto& tid : threads) {
if (std::find(tids.begin(), tids.end(), tid) == tids.end()) {
LOG(ERROR) << "Thread " << tid << " doesn't exist in current process.";
return false;
}
}
threads_.insert(threads.begin(), threads.end());
return true;
}
protected:
PerfEventSetImpl() : whole_process_(false) {}
std::vector<std::string> event_names_;
bool whole_process_;
std::set<pid_t> threads_;
};
class PerfEventSetForCounting : public PerfEventSetImpl {
public:
PerfEventSetForCounting() : in_counting_state_(false) {}
virtual ~PerfEventSetForCounting() {}
bool StartCounters() override;
bool StopCounters() override;
bool ReadCounters(std::vector<Counter>* counters) override;
private:
bool CreateEventSelectionSet();
void InitAccumulatedCounters();
bool ReadRawCounters(std::vector<Counter>* counters);
// Add counter b to a.
void AddCounter(Counter& a, const Counter& b);
// Sub counter b from a.
void SubCounter(Counter& a, const Counter& b);
bool in_counting_state_;
std::unique_ptr<EventSelectionSet> event_selection_set_;
// The counters at the last time calling StartCounting().
std::vector<Counter> last_start_counters_;
// The accumulated counters of counting periods, excluding
// the last one.
std::vector<Counter> accumulated_counters_;
};
bool PerfEventSetForCounting::CreateEventSelectionSet() {
std::unique_ptr<EventSelectionSet> set(new EventSelectionSet(true));
if (event_names_.empty()) {
LOG(ERROR) << "No events.";
return false;
}
for (const auto& name : event_names_) {
if (!set->AddEventType(name)) {
return false;
}
}
if (whole_process_) {
set->AddMonitoredProcesses({getpid()});
} else {
if (threads_.empty()) {
LOG(ERROR) << "No monitored threads.";
return false;
}
set->AddMonitoredThreads(threads_);
}
if (!set->OpenEventFiles({-1})) {
return false;
}
event_selection_set_ = std::move(set);
return true;
}
void PerfEventSetForCounting::InitAccumulatedCounters() {
for (const auto& name : event_names_) {
Counter counter;
counter.event = name;
counter.value = 0;
counter.time_enabled_in_ns = 0;
counter.time_running_in_ns = 0;
accumulated_counters_.push_back(counter);
}
}
bool PerfEventSetForCounting::ReadRawCounters(std::vector<Counter>* counters) {
CHECK(event_selection_set_);
std::vector<CountersInfo> s;
if (!event_selection_set_->ReadCounters(&s)) {
return false;
}
CHECK_EQ(s.size(), event_names_.size());
counters->resize(s.size());
for (size_t i = 0; i < s.size(); ++i) {
CountersInfo& info = s[i];
std::string name = info.event_modifier.empty() ? info.event_name :
info.event_name + ":" + info.event_modifier;
CHECK_EQ(name, event_names_[i]);
Counter& sum = (*counters)[i];
sum.event = name;
sum.value = 0;
sum.time_enabled_in_ns = 0;
sum.time_running_in_ns = 0;
for (CounterInfo& c : info.counters) {
sum.value += c.counter.value;
sum.time_enabled_in_ns += c.counter.time_enabled;
sum.time_running_in_ns += c.counter.time_running;
}
}
return true;
}
void PerfEventSetForCounting::AddCounter(Counter& a, const Counter& b) {
a.value += b.value;
a.time_enabled_in_ns += b.time_enabled_in_ns;
a.time_running_in_ns += b.time_enabled_in_ns;
}
void PerfEventSetForCounting::SubCounter(Counter& a, const Counter& b) {
a.value -= b.value;
a.time_enabled_in_ns -= b.time_enabled_in_ns;
a.time_running_in_ns -= b.time_running_in_ns;
}
bool PerfEventSetForCounting::StartCounters() {
if (in_counting_state_) {
return true;
}
if (event_selection_set_ == nullptr) {
if (!CreateEventSelectionSet()) {
return false;
}
InitAccumulatedCounters();
}
if (!ReadRawCounters(&last_start_counters_)) {
return false;
}
in_counting_state_ = true;
return true;
}
bool PerfEventSetForCounting::StopCounters() {
if (!in_counting_state_) {
return true;
}
std::vector<Counter> cur;
if (!ReadRawCounters(&cur)) {
return false;
}
for (size_t i = 0; i < event_names_.size(); ++i) {
SubCounter(cur[i], last_start_counters_[i]);
AddCounter(accumulated_counters_[i], cur[i]);
}
in_counting_state_ = false;
return true;
}
bool PerfEventSetForCounting::ReadCounters(std::vector<Counter>* counters) {
if (!in_counting_state_) {
*counters = accumulated_counters_;
return true;
}
if (!ReadRawCounters(counters)) {
return false;
}
for (size_t i = 0; i < event_names_.size(); ++i) {
SubCounter((*counters)[i], last_start_counters_[i]);
AddCounter((*counters)[i], accumulated_counters_[i]);
}
return true;
}
PerfEventSet* PerfEventSet::CreateInstance(PerfEventSet::Type type) {
if (!CheckPerfEventLimit()) {
return nullptr;
}
if (type == Type::kPerfForCounting) {
return new PerfEventSetForCounting;
}
return nullptr;
}
bool PerfEventSet::AddEvent(const std::string&) {
return false;
}
bool PerfEventSet::MonitorCurrentProcess() {
return false;
}
bool PerfEventSet::MonitorCurrentThread() {
return false;
}
bool PerfEventSet::MonitorThreadsInCurrentProcess(const std::vector<pid_t>&) {
return false;
}
bool PerfEventSet::StartCounters() {
return false;
}
bool PerfEventSet::StopCounters() {
return false;
}
bool PerfEventSet::ReadCounters(std::vector<Counter>*) {
return false;
}
} // namespace simpleperf
|