forked from LineageOS/android_art
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathodr_compilation_log.cc
215 lines (179 loc) · 5.89 KB
/
odr_compilation_log.cc
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
/*
* Copyright (C) 2021 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.
*/
#include <odr_compilation_log.h>
#include <errno.h>
#include <fstream>
#include <ios>
#include <iosfwd>
#include <istream>
#include <ostream>
#include <streambuf>
#include <string>
#include <vector>
#include "android-base/logging.h"
#include "base/os.h"
#include "odrefresh/odrefresh.h"
#include "odr_metrics.h"
namespace art {
namespace odrefresh {
std::istream& operator>>(std::istream& is, OdrCompilationLogEntry& entry) {
// Block I/O related exceptions
auto saved_exceptions = is.exceptions();
is.exceptions(std::ios_base::iostate {});
// Write log entry. NB update OdrCompilationLog::kLogVersion if changing the format here.
is >> entry.apex_version >> std::ws;
is >> entry.last_update_millis >> std::ws;
is >> entry.trigger >> std::ws;
is >> entry.when >> std::ws;
is >> entry.exit_code >> std::ws;
// Restore I/O related exceptions
is.exceptions(saved_exceptions);
return is;
}
std::ostream& operator<<(std::ostream& os, const OdrCompilationLogEntry& entry) {
static const char kSpace = ' ';
// Block I/O related exceptions
auto saved_exceptions = os.exceptions();
os.exceptions(std::ios_base::iostate {});
os << entry.apex_version << kSpace;
os << entry.last_update_millis << kSpace;
os << entry.trigger << kSpace;
os << entry.when << kSpace;
os << entry.exit_code << std::endl;
// Restore I/O related exceptions
os.exceptions(saved_exceptions);
return os;
}
bool operator==(const OdrCompilationLogEntry& lhs, const OdrCompilationLogEntry& rhs) {
return lhs.apex_version == rhs.apex_version && lhs.last_update_millis == rhs.last_update_millis &&
lhs.trigger == rhs.trigger && lhs.when == rhs.when && lhs.exit_code == rhs.exit_code;
}
bool operator!=(const OdrCompilationLogEntry& lhs, const OdrCompilationLogEntry& rhs) {
return !(lhs == rhs);
}
OdrCompilationLog::OdrCompilationLog(const char* compilation_log_path)
: log_path_(compilation_log_path) {
if (log_path_ != nullptr && OS::FileExists(log_path_)) {
if (!Read()) {
PLOG(ERROR) << "Failed to read compilation log: " << log_path_;
}
}
}
OdrCompilationLog::~OdrCompilationLog() {
if (log_path_ != nullptr && !Write()) {
PLOG(ERROR) << "Failed to write compilation log: " << log_path_;
}
}
bool OdrCompilationLog::Read() {
std::ifstream ifs(log_path_);
if (!ifs.good()) {
return false;
}
std::string log_version;
ifs >> log_version >> std::ws;
if (log_version != kLogVersion) {
return false;
}
while (!ifs.eof()) {
OdrCompilationLogEntry entry;
ifs >> entry;
if (ifs.fail()) {
entries_.clear();
return false;
}
entries_.push_back(entry);
}
return true;
}
bool OdrCompilationLog::Write() const {
std::ofstream ofs(log_path_, std::ofstream::trunc);
if (!ofs.good()) {
return false;
}
ofs << kLogVersion << std::endl;
for (const auto& entry : entries_) {
ofs << entry;
if (ofs.fail()) {
return false;
}
}
return true;
}
void OdrCompilationLog::Truncate() {
if (entries_.size() < kMaxLoggedEntries) {
return;
}
size_t excess = entries_.size() - kMaxLoggedEntries;
entries_.erase(entries_.begin(), entries_.begin() + excess);
}
size_t OdrCompilationLog::NumberOfEntries() const {
return entries_.size();
}
const OdrCompilationLogEntry* OdrCompilationLog::Peek(size_t index) const {
if (index >= entries_.size()) {
return nullptr;
}
return &entries_[index];
}
void OdrCompilationLog::Log(int64_t apex_version,
int64_t last_update_millis,
OdrMetrics::Trigger trigger,
ExitCode compilation_result) {
time_t now;
time(&now);
Log(apex_version, last_update_millis, trigger, now, compilation_result);
}
void OdrCompilationLog::Log(int64_t apex_version,
int64_t last_update_millis,
OdrMetrics::Trigger trigger,
time_t when,
ExitCode compilation_result) {
entries_.push_back(OdrCompilationLogEntry{apex_version,
last_update_millis,
static_cast<int32_t>(trigger),
when,
static_cast<int32_t>(compilation_result)});
Truncate();
}
bool OdrCompilationLog::ShouldAttemptCompile(OdrMetrics::Trigger trigger, time_t now) const {
if (entries_.size() == 0) {
// We have no history, try to compile.
return true;
}
if (trigger == OdrMetrics::Trigger::kApexVersionMismatch ||
trigger == OdrMetrics::Trigger::kDexFilesChanged) {
// Things have changed since the last run.
return true;
}
// Compute the backoff time based on the number of consecutive failures.
//
// Wait 12 hrs * pow(2, consecutive_failures) since the last compilation attempt.
static const int kSecondsPerDay = 86'400;
time_t backoff = kSecondsPerDay / 2;
for (auto it = entries_.crbegin(); it != entries_.crend(); ++it, backoff *= 2) {
if (it->exit_code == ExitCode::kCompilationSuccess) {
break;
}
}
if (now == 0) {
time(&now);
}
const time_t last_attempt = entries_.back().when;
const time_t threshold = last_attempt + backoff;
return now >= threshold;
}
} // namespace odrefresh
} // namespace art