• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(OdrMetrics::Trigger trigger, time_t now = 0) const;
69 
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 
76   void Log(int64_t apex_version,
77            int64_t last_update_millis,
78            OdrMetrics::Trigger trigger,
79            ExitCode compilation_result);
80 
81   void Log(int64_t apex_version,
82            int64_t last_update_millis,
83            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_
102