Skip to content

Commit

Permalink
odrefresh: add support for lastUpdateMillis as part of version check
Browse files Browse the repository at this point in the history
This is to support samegrade updates for module evaluation.

Bug: 192647837
Test: atest art_standalone_odrefresh_tests
Test: atest odsign_e2e_tests
Change-Id: Ied43ebdcc4b2ec57e337e709970fab948cf5f992
  • Loading branch information
ohodson committed Jul 8, 2021
1 parent 4f62327 commit 79f874d
Show file tree
Hide file tree
Showing 7 changed files with 271 additions and 56 deletions.
2 changes: 2 additions & 0 deletions odrefresh/CacheInfo.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
<xs:attribute name="versionCode" type="xs:long" use="required" />
<!-- Module versionName for the active ART APEX from `/apex/apex-info-list.xml`. -->
<xs:attribute name="versionName" type="xs:string" use="required" />
<!-- Module lastUpdateMillis for the active ART APEX from `/apex/apex-info-list.xml`. -->
<xs:attribute name="lastUpdateMillis" type="xs:long" use="required" />
</xs:complexType>

<!-- Components of the `DEX2OATBOOTCLASSPATH`. -->
Expand Down
33 changes: 27 additions & 6 deletions odrefresh/odr_compilation_log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ std::istream& operator>>(std::istream& is, OdrCompilationLogEntry& entry) {
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;
Expand All @@ -59,6 +61,7 @@ std::ostream& operator<<(std::ostream& os, const OdrCompilationLogEntry& entry)
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;
Expand All @@ -69,8 +72,8 @@ std::ostream& operator<<(std::ostream& os, const OdrCompilationLogEntry& entry)
}

bool operator==(const OdrCompilationLogEntry& lhs, const OdrCompilationLogEntry& rhs) {
return lhs.apex_version == rhs.apex_version && lhs.trigger == rhs.trigger &&
lhs.when == rhs.when && lhs.exit_code == rhs.exit_code;
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) {
Expand Down Expand Up @@ -98,6 +101,12 @@ bool OdrCompilationLog::Read() {
return false;
}

std::string log_version;
ifs >> log_version >> std::ws;
if (log_version != kLogVersion) {
return false;
}

while (!ifs.eof()) {
OdrCompilationLogEntry entry;
ifs >> entry;
Expand All @@ -117,6 +126,7 @@ bool OdrCompilationLog::Write() const {
return false;
}

ofs << kLogVersion << std::endl;
for (const auto& entry : entries_) {
ofs << entry;
if (ofs.fail()) {
Expand Down Expand Up @@ -148,23 +158,29 @@ const OdrCompilationLogEntry* OdrCompilationLog::Peek(size_t index) const {
}

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, trigger, now, compilation_result);
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, static_cast<int32_t>(trigger), when, static_cast<int32_t>(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(int64_t apex_version,
int64_t last_update_millis,
OdrMetrics::Trigger trigger,
time_t now) const {
if (entries_.size() == 0) {
Expand All @@ -173,7 +189,12 @@ bool OdrCompilationLog::ShouldAttemptCompile(int64_t apex_version,
}

if (apex_version != entries_.back().apex_version) {
// There is a new ART APEX, we should use compile right away.
// There is a new ART APEX, we should compile right away.
return true;
}

if (last_update_millis != entries_.back().last_update_millis) {
// There is a samegrade ART APEX update, we should compile right away.
return true;
}

Expand Down
13 changes: 12 additions & 1 deletion odrefresh/odr_compilation_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ namespace odrefresh {
// OdrCompilationLogEntry represents the result of a compilation attempt by odrefresh.
struct OdrCompilationLogEntry {
int64_t apex_version;
int64_t last_update_millis;
int32_t trigger;
time_t when;
int32_t exit_code;
Expand All @@ -53,13 +54,19 @@ class OdrCompilationLog {
// directory is only used by odrefresh whereas the ART apexdata directory is also used by odsign
// and others which may lead to the deletion (or rollback) of the log file.
static constexpr const char* kCompilationLogFile = "/data/misc/odrefresh/compilation-log.txt";

// Version string that appears on the first line of the compilation log.
static constexpr const char kLogVersion[] = "CompilationLog/1.0";

// Number of log entries in the compilation log.
static constexpr const size_t kMaxLoggedEntries = 4;

explicit OdrCompilationLog(const char* compilation_log_path = kCompilationLogFile);
~OdrCompilationLog();

// Applies policy to compilation log to determine whether to recompile.
bool ShouldAttemptCompile(int64_t apex_version,
int64_t last_update_millis,
OdrMetrics::Trigger trigger,
time_t now = 0) const;

Expand All @@ -69,9 +76,13 @@ class OdrCompilationLog {
// Returns the entry at position `index` or nullptr if `index` is out of bounds.
const OdrCompilationLogEntry* Peek(size_t index) const;

void Log(int64_t apex_version, OdrMetrics::Trigger trigger, ExitCode compilation_result);
void Log(int64_t apex_version,
int64_t last_update_millis,
OdrMetrics::Trigger trigger,
ExitCode compilation_result);

void Log(int64_t apex_version,
int64_t last_update_millis,
OdrMetrics::Trigger trigger,
time_t when,
ExitCode compilation_result);
Expand Down
Loading

0 comments on commit 79f874d

Please sign in to comment.