xref: /aosp_15_r20/art/odrefresh/odr_compilation_log.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2021 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #ifndef ART_ODREFRESH_ODR_COMPILATION_LOG_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_ODREFRESH_ODR_COMPILATION_LOG_H_
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include <time.h>
21*795d594fSAndroid Build Coastguard Worker 
22*795d594fSAndroid Build Coastguard Worker #include <cstdint>
23*795d594fSAndroid Build Coastguard Worker #include <iosfwd>
24*795d594fSAndroid Build Coastguard Worker #include <vector>
25*795d594fSAndroid Build Coastguard Worker 
26*795d594fSAndroid Build Coastguard Worker #include <odrefresh/odrefresh.h>
27*795d594fSAndroid Build Coastguard Worker #include <odr_metrics.h>
28*795d594fSAndroid Build Coastguard Worker 
29*795d594fSAndroid Build Coastguard Worker namespace art {
30*795d594fSAndroid Build Coastguard Worker namespace odrefresh {
31*795d594fSAndroid Build Coastguard Worker 
32*795d594fSAndroid Build Coastguard Worker // OdrCompilationLogEntry represents the result of a compilation attempt by odrefresh.
33*795d594fSAndroid Build Coastguard Worker struct OdrCompilationLogEntry {
34*795d594fSAndroid Build Coastguard Worker   int64_t apex_version;
35*795d594fSAndroid Build Coastguard Worker   int64_t last_update_millis;
36*795d594fSAndroid Build Coastguard Worker   int32_t trigger;
37*795d594fSAndroid Build Coastguard Worker   time_t when;
38*795d594fSAndroid Build Coastguard Worker   int32_t exit_code;
39*795d594fSAndroid Build Coastguard Worker };
40*795d594fSAndroid Build Coastguard Worker 
41*795d594fSAndroid Build Coastguard Worker // Read an `OdrCompilationLogEntry` from an input stream.
42*795d594fSAndroid Build Coastguard Worker std::istream& operator>>(std::istream& is, OdrCompilationLogEntry& entry);
43*795d594fSAndroid Build Coastguard Worker 
44*795d594fSAndroid Build Coastguard Worker // Write an `OdrCompilationLogEntry` to an output stream.
45*795d594fSAndroid Build Coastguard Worker std::ostream& operator<<(std::ostream& os, const OdrCompilationLogEntry& entry);
46*795d594fSAndroid Build Coastguard Worker 
47*795d594fSAndroid Build Coastguard Worker // Equality test for two `OdrCompilationLogEntry` instances.
48*795d594fSAndroid Build Coastguard Worker bool operator==(const OdrCompilationLogEntry& lhs, const OdrCompilationLogEntry& rhs);
49*795d594fSAndroid Build Coastguard Worker bool operator!=(const OdrCompilationLogEntry& lhs, const OdrCompilationLogEntry& rhs);
50*795d594fSAndroid Build Coastguard Worker 
51*795d594fSAndroid Build Coastguard Worker class OdrCompilationLog {
52*795d594fSAndroid Build Coastguard Worker  public:
53*795d594fSAndroid Build Coastguard Worker   // The compilation log location is in the same directory as used for the metricss.log. This
54*795d594fSAndroid Build Coastguard Worker   // directory is only used by odrefresh whereas the ART apexdata directory is also used by odsign
55*795d594fSAndroid Build Coastguard Worker   // and others which may lead to the deletion (or rollback) of the log file.
56*795d594fSAndroid Build Coastguard Worker   static constexpr const char* kCompilationLogFile = "/data/misc/odrefresh/compilation-log.txt";
57*795d594fSAndroid Build Coastguard Worker 
58*795d594fSAndroid Build Coastguard Worker   // Version string that appears on the first line of the compilation log.
59*795d594fSAndroid Build Coastguard Worker   static constexpr const char kLogVersion[] = "CompilationLog/1.0";
60*795d594fSAndroid Build Coastguard Worker 
61*795d594fSAndroid Build Coastguard Worker   // Number of log entries in the compilation log.
62*795d594fSAndroid Build Coastguard Worker   static constexpr const size_t kMaxLoggedEntries = 4;
63*795d594fSAndroid Build Coastguard Worker 
64*795d594fSAndroid Build Coastguard Worker   explicit OdrCompilationLog(const char* compilation_log_path = kCompilationLogFile);
65*795d594fSAndroid Build Coastguard Worker   ~OdrCompilationLog();
66*795d594fSAndroid Build Coastguard Worker 
67*795d594fSAndroid Build Coastguard Worker   // Applies policy to compilation log to determine whether to recompile.
68*795d594fSAndroid Build Coastguard Worker   bool ShouldAttemptCompile(OdrMetrics::Trigger trigger, time_t now = 0) const;
69*795d594fSAndroid Build Coastguard Worker 
70*795d594fSAndroid Build Coastguard Worker   // Returns the number of entries in the log. The log never exceeds `kMaxLoggedEntries`.
71*795d594fSAndroid Build Coastguard Worker   size_t NumberOfEntries() const;
72*795d594fSAndroid Build Coastguard Worker 
73*795d594fSAndroid Build Coastguard Worker   // Returns the entry at position `index` or nullptr if `index` is out of bounds.
74*795d594fSAndroid Build Coastguard Worker   const OdrCompilationLogEntry* Peek(size_t index) const;
75*795d594fSAndroid Build Coastguard Worker 
76*795d594fSAndroid Build Coastguard Worker   void Log(int64_t apex_version,
77*795d594fSAndroid Build Coastguard Worker            int64_t last_update_millis,
78*795d594fSAndroid Build Coastguard Worker            OdrMetrics::Trigger trigger,
79*795d594fSAndroid Build Coastguard Worker            ExitCode compilation_result);
80*795d594fSAndroid Build Coastguard Worker 
81*795d594fSAndroid Build Coastguard Worker   void Log(int64_t apex_version,
82*795d594fSAndroid Build Coastguard Worker            int64_t last_update_millis,
83*795d594fSAndroid Build Coastguard Worker            OdrMetrics::Trigger trigger,
84*795d594fSAndroid Build Coastguard Worker            time_t when,
85*795d594fSAndroid Build Coastguard Worker            ExitCode compilation_result);
86*795d594fSAndroid Build Coastguard Worker 
87*795d594fSAndroid Build Coastguard Worker   // Truncates the in memory log to have `kMaxLoggedEntries` records.
88*795d594fSAndroid Build Coastguard Worker   void Truncate();
89*795d594fSAndroid Build Coastguard Worker 
90*795d594fSAndroid Build Coastguard Worker  private:
91*795d594fSAndroid Build Coastguard Worker   bool Read();
92*795d594fSAndroid Build Coastguard Worker   bool Write() const;
93*795d594fSAndroid Build Coastguard Worker 
94*795d594fSAndroid Build Coastguard Worker   std::vector<OdrCompilationLogEntry> entries_;
95*795d594fSAndroid Build Coastguard Worker   const char* log_path_;
96*795d594fSAndroid Build Coastguard Worker };
97*795d594fSAndroid Build Coastguard Worker 
98*795d594fSAndroid Build Coastguard Worker }  // namespace odrefresh
99*795d594fSAndroid Build Coastguard Worker }  // namespace art
100*795d594fSAndroid Build Coastguard Worker 
101*795d594fSAndroid Build Coastguard Worker #endif  // ART_ODREFRESH_ODR_COMPILATION_LOG_H_
102