1 /*
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 
29 namespace art {
30 namespace odrefresh {
31 
32 // OdrCompilationLogEntry represents the result of a compilation attempt by odrefresh.
33 struct OdrCompilationLogEntry {
34   int64_t apex_version;
35   int64_t last_update_millis;
36   int32_t trigger;
37   time_t when;
38   int32_t exit_code;
39 };
40 
41 // Read an `OdrCompilationLogEntry` from an input stream.
42 std::istream& operator>>(std::istream& is, OdrCompilationLogEntry& entry);
43 
44 // Write an `OdrCompilationLogEntry` to an output stream.
45 std::ostream& operator<<(std::ostream& os, const OdrCompilationLogEntry& entry);
46 
47 // Equality test for two `OdrCompilationLogEntry` instances.
48 bool operator==(const OdrCompilationLogEntry& lhs, const OdrCompilationLogEntry& rhs);
49 bool operator!=(const OdrCompilationLogEntry& lhs, const OdrCompilationLogEntry& rhs);
50 
51 class 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";
57 
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.
62   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.
68   bool ShouldAttemptCompile(int64_t apex_version,
69                             int64_t last_update_millis,
70                             OdrMetrics::Trigger trigger,
71                             time_t now = 0) const;
72 
73   // Returns the number of entries in the log. The log never exceeds `kMaxLoggedEntries`.
74   size_t NumberOfEntries() const;
75 
76   // Returns the entry at position `index` or nullptr if `index` is out of bounds.
77   const OdrCompilationLogEntry* Peek(size_t index) const;
78 
79   void Log(int64_t apex_version,
80            int64_t last_update_millis,
81            OdrMetrics::Trigger trigger,
82            ExitCode compilation_result);
83 
84   void Log(int64_t apex_version,
85            int64_t last_update_millis,
86            OdrMetrics::Trigger trigger,
87            time_t when,
88            ExitCode compilation_result);
89 
90   // Truncates the in memory log to have `kMaxLoggedEntries` records.
91   void Truncate();
92 
93  private:
94   bool Read();
95   bool Write() const;
96 
97   std::vector<OdrCompilationLogEntry> entries_;
98   const char* log_path_;
99 };
100 
101 }  // namespace odrefresh
102 }  // namespace art
103 
104 #endif  // ART_ODREFRESH_ODR_COMPILATION_LOG_H_
105