blob: 48d8c68ec41089072379d249ce631572f80077f8 [file] [log] [blame]
Orion Hodsonf761f582021-06-09 10:50:57 +01001/*
2 * Copyright (C) 2021 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#ifndef ART_ODREFRESH_ODR_COMPILATION_LOG_H_
18#define ART_ODREFRESH_ODR_COMPILATION_LOG_H_
19
20#include <time.h>
21
22#include <cstdint>
23#include <iosfwd>
24#include <vector>
25
26#include <odrefresh/odrefresh.h>
27#include <odr_metrics.h>
28
29namespace art {
30namespace odrefresh {
31
32// OdrCompilationLogEntry represents the result of a compilation attempt by odrefresh.
33struct OdrCompilationLogEntry {
34 int64_t apex_version;
Orion Hodson79f874d2021-07-07 08:28:31 +010035 int64_t last_update_millis;
Orion Hodsonf761f582021-06-09 10:50:57 +010036 int32_t trigger;
37 time_t when;
38 int32_t exit_code;
39};
40
41// Read an `OdrCompilationLogEntry` from an input stream.
42std::istream& operator>>(std::istream& is, OdrCompilationLogEntry& entry);
43
44// Write an `OdrCompilationLogEntry` to an output stream.
45std::ostream& operator<<(std::ostream& os, const OdrCompilationLogEntry& entry);
46
47// Equality test for two `OdrCompilationLogEntry` instances.
48bool operator==(const OdrCompilationLogEntry& lhs, const OdrCompilationLogEntry& rhs);
49bool operator!=(const OdrCompilationLogEntry& lhs, const OdrCompilationLogEntry& rhs);
50
51class OdrCompilationLog {
52 public:
53 // The compilation log location is in the same directory as used for the metricss.log. This
54 // directory is only used by odrefresh whereas the ART apexdata directory is also used by odsign
55 // and others which may lead to the deletion (or rollback) of the log file.
56 static constexpr const char* kCompilationLogFile = "/data/misc/odrefresh/compilation-log.txt";
Orion Hodson79f874d2021-07-07 08:28:31 +010057
58 // Version string that appears on the first line of the compilation log.
59 static constexpr const char kLogVersion[] = "CompilationLog/1.0";
60
61 // Number of log entries in the compilation log.
Orion Hodsonf761f582021-06-09 10:50:57 +010062 static constexpr const size_t kMaxLoggedEntries = 4;
63
64 explicit OdrCompilationLog(const char* compilation_log_path = kCompilationLogFile);
65 ~OdrCompilationLog();
66
67 // Applies policy to compilation log to determine whether to recompile.
Jiakai Zhang5a65d032021-11-12 20:09:20 +000068 bool ShouldAttemptCompile(OdrMetrics::Trigger trigger, time_t now = 0) const;
Orion Hodsonf761f582021-06-09 10:50:57 +010069
70 // Returns the number of entries in the log. The log never exceeds `kMaxLoggedEntries`.
71 size_t NumberOfEntries() const;
72
73 // Returns the entry at position `index` or nullptr if `index` is out of bounds.
74 const OdrCompilationLogEntry* Peek(size_t index) const;
75
Orion Hodson79f874d2021-07-07 08:28:31 +010076 void Log(int64_t apex_version,
77 int64_t last_update_millis,
78 OdrMetrics::Trigger trigger,
79 ExitCode compilation_result);
Orion Hodsonf761f582021-06-09 10:50:57 +010080
81 void Log(int64_t apex_version,
Orion Hodson79f874d2021-07-07 08:28:31 +010082 int64_t last_update_millis,
Orion Hodsonf761f582021-06-09 10:50:57 +010083 OdrMetrics::Trigger trigger,
84 time_t when,
85 ExitCode compilation_result);
86
87 // Truncates the in memory log to have `kMaxLoggedEntries` records.
88 void Truncate();
89
90 private:
91 bool Read();
92 bool Write() const;
93
94 std::vector<OdrCompilationLogEntry> entries_;
95 const char* log_path_;
96};
97
98} // namespace odrefresh
99} // namespace art
100
101#endif // ART_ODREFRESH_ODR_COMPILATION_LOG_H_