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
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/arc/tracing/arc_cpu_event.h"
#include "base/logging.h"
#include "base/types/cxx23_to_underlying.h"
namespace arc {
ArcCpuEvent::ArcCpuEvent(uint64_t timestamp, Type type, uint32_t tid)
: timestamp(timestamp), type(type), tid(tid) {}
bool ArcCpuEvent::operator==(const ArcCpuEvent& other) const {
return timestamp == other.timestamp && type == other.type && tid == other.tid;
}
bool AddCpuEvent(CpuEvents* cpu_events,
uint64_t timestamp,
ArcCpuEvent::Type type,
uint32_t tid) {
// Base validation.
switch (type) {
case ArcCpuEvent::Type::kIdleIn:
case ArcCpuEvent::Type::kIdleOut:
if (tid) {
LOG(ERROR) << "Idle should always be bound to the idle process";
return false;
}
break;
case ArcCpuEvent::Type::kWakeUp:
if (!tid) {
LOG(ERROR) << "Cannot wake-up to idle process";
return false;
}
break;
case ArcCpuEvent::Type::kActive:
break;
}
if (cpu_events->empty()) {
cpu_events->emplace_back(timestamp, type, tid);
return true;
}
// Verify new event relative to the last event.
const ArcCpuEvent& last = cpu_events->back();
if (last.timestamp > timestamp) {
LOG(ERROR) << "Time sequence is broken for cpu events: " << last.timestamp
<< " vs " << timestamp;
return false;
}
// Check transitions from->to.
switch (last.type) {
case ArcCpuEvent::Type::kIdleIn:
switch (type) {
case ArcCpuEvent::Type::kIdleOut:
break;
case ArcCpuEvent::Type::kWakeUp:
break;
default:
LOG(ERROR) << "Unknown CPU transition: " << last.type << "=>" << type;
return false;
}
break;
case ArcCpuEvent::Type::kIdleOut:
switch (type) {
case ArcCpuEvent::Type::kIdleIn:
break;
case ArcCpuEvent::Type::kWakeUp:
break;
case ArcCpuEvent::Type::kActive:
break;
default:
LOG(ERROR) << "Unknown CPU transition: " << last.type << "=>" << type;
return false;
}
break;
case ArcCpuEvent::Type::kWakeUp:
switch (type) {
case ArcCpuEvent::Type::kIdleIn:
break;
case ArcCpuEvent::Type::kIdleOut:
break;
case ArcCpuEvent::Type::kWakeUp:
break;
case ArcCpuEvent::Type::kActive:
break;
default:
LOG(ERROR) << "Unknown CPU transition: " << last.type << "=>" << type;
return false;
}
break;
case ArcCpuEvent::Type::kActive:
switch (type) {
case ArcCpuEvent::Type::kIdleIn:
break;
case ArcCpuEvent::Type::kWakeUp:
break;
case ArcCpuEvent::Type::kActive:
break;
default:
LOG(ERROR) << "Unknown CPU transition: " << last.type << "=>" << type;
return false;
}
break;
}
cpu_events->emplace_back(timestamp, type, tid);
return true;
}
bool AddAllCpuEvent(AllCpuEvents* all_cpu_events,
uint32_t cpu_id,
uint64_t timestamp,
ArcCpuEvent::Type type,
uint32_t tid) {
if (all_cpu_events->size() <= cpu_id) {
all_cpu_events->resize(cpu_id + 1);
}
return AddCpuEvent(&(*all_cpu_events)[cpu_id], timestamp, type, tid);
}
base::Value::List SerializeCpuEvents(const CpuEvents& cpu_events) {
base::Value::List list;
for (const auto& event : cpu_events) {
base::Value::List event_value;
event_value.Append(base::Value(static_cast<int>(event.type)));
event_value.Append(base::Value(static_cast<double>(event.timestamp)));
event_value.Append(base::Value(static_cast<int>(event.tid)));
list.Append(std::move(event_value));
}
return list;
}
base::Value::List SerializeAllCpuEvents(const AllCpuEvents& all_cpu_events) {
base::Value::List list;
for (const auto& cpu_events : all_cpu_events) {
list.Append(SerializeCpuEvents(cpu_events));
}
return list;
}
bool LoadCpuEvents(const base::Value* value, CpuEvents* cpu_events) {
if (!value || !value->is_list()) {
return false;
}
uint64_t previous_timestamp = 0;
for (const auto& entry : value->GetList()) {
if (!entry.is_list() || entry.GetList().size() != 3) {
return false;
}
if (!entry.GetList()[0].is_int()) {
return false;
}
const ArcCpuEvent::Type type =
static_cast<ArcCpuEvent::Type>(entry.GetList()[0].GetInt());
switch (type) {
case ArcCpuEvent::Type::kIdleIn:
case ArcCpuEvent::Type::kIdleOut:
case ArcCpuEvent::Type::kWakeUp:
case ArcCpuEvent::Type::kActive:
break;
default:
return false;
}
if (!entry.GetList()[1].is_double() && !entry.GetList()[1].is_int()) {
return false;
}
const uint64_t timestamp = entry.GetList()[1].GetDouble();
if (timestamp < previous_timestamp) {
return false;
}
if (!entry.GetList()[2].is_int()) {
return false;
}
const int tid = entry.GetList()[2].GetInt();
cpu_events->emplace_back(timestamp, type, tid);
previous_timestamp = timestamp;
}
return true;
}
bool LoadAllCpuEvents(const base::Value* value, AllCpuEvents* all_cpu_events) {
if (!value || !value->is_list()) {
return false;
}
for (const auto& entry : value->GetList()) {
CpuEvents cpu_events;
if (!LoadCpuEvents(&entry, &cpu_events)) {
return false;
}
all_cpu_events->emplace_back(std::move(cpu_events));
}
return true;
}
std::ostream& operator<<(std::ostream& os, ArcCpuEvent::Type event_type) {
return os << base::to_underlying(event_type);
}
} // namespace arc
|