summaryrefslogtreecommitdiff
path: root/odrefresh/odr_compilation_log.h
blob: 894079c3f9d7f841115016ac3a0104d98258e3b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef ART_ODREFRESH_ODR_COMPILATION_LOG_H_
#define ART_ODREFRESH_ODR_COMPILATION_LOG_H_

#include <time.h>

#include <cstdint>
#include <iosfwd>
#include <vector>

#include <odrefresh/odrefresh.h>
#include <odr_metrics.h>

namespace art {
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;
};

// Read an `OdrCompilationLogEntry` from an input stream.
std::istream& operator>>(std::istream& is, OdrCompilationLogEntry& entry);

// Write an `OdrCompilationLogEntry` to an output stream.
std::ostream& operator<<(std::ostream& os, const OdrCompilationLogEntry& entry);

// Equality test for two `OdrCompilationLogEntry` instances.
bool operator==(const OdrCompilationLogEntry& lhs, const OdrCompilationLogEntry& rhs);
bool operator!=(const OdrCompilationLogEntry& lhs, const OdrCompilationLogEntry& rhs);

class OdrCompilationLog {
 public:
  // The compilation log location is in the same directory as used for the metricss.log. This
  // 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;

  // Returns the number of entries in the log. The log never exceeds `kMaxLoggedEntries`.
  size_t NumberOfEntries() const;

  // 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,
           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);

  // Truncates the in memory log to have `kMaxLoggedEntries` records.
  void Truncate();

 private:
  bool Read();
  bool Write() const;

  std::vector<OdrCompilationLogEntry> entries_;
  const char* log_path_;
};

}  // namespace odrefresh
}  // namespace art

#endif  // ART_ODREFRESH_ODR_COMPILATION_LOG_H_