xref: /aosp_15_r20/art/odrefresh/odr_metrics_record.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
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_METRICS_RECORD_H_
18 #define ART_ODREFRESH_ODR_METRICS_RECORD_H_
19 
20 #include <cstdint>
21 #include <iosfwd>  // For forward-declaration of std::string.
22 
23 #include "android-base/result.h"
24 #include "exec_utils.h"
25 #include "tinyxml2.h"
26 
27 namespace art {
28 namespace odrefresh {
29 
30 // Default location for storing metrics from odrefresh.
31 constexpr const char* kOdrefreshMetricsFile = "/data/misc/odrefresh/odrefresh-metrics.xml";
32 
33 // Initial OdrefreshMetrics version
34 static constexpr int32_t kOdrefreshMetricsVersion = 4;
35 
36 // Constant value used in ExecResult when the process was not run at all.
37 // Mirrors EXEC_RESULT_STATUS_NOT_RUN contained in
38 // frameworks/proto_logging/stats/enums/art/common_enums.proto.
39 static constexpr int32_t kExecResultNotRun = 5;
40 static_assert(kExecResultNotRun > ExecResult::Status::kLast,
41               "`art::odrefresh::kExecResultNotRun` value should not overlap with"
42               " values of enum `art::ExecResult::Status`");
43 
44 
45 // MetricsRecord is a simpler container for Odrefresh metric values reported to statsd. The order
46 // and types of fields here mirror definition of `OdrefreshReported` in
47 // frameworks/proto_logging/stats/atoms.proto.
48 struct OdrMetricsRecord {
49   struct Dex2OatExecResult {
50     int32_t status;
51     int32_t exit_code;
52     int32_t signal;
53 
Dex2OatExecResultOdrMetricsRecord::Dex2OatExecResult54     explicit Dex2OatExecResult(int32_t status, int32_t exit_code, int32_t signal)
55       : status(status), exit_code(exit_code), signal(signal) {}
56 
Dex2OatExecResultOdrMetricsRecord::Dex2OatExecResult57     explicit Dex2OatExecResult(const ExecResult& result)
58       : status(result.status), exit_code(result.exit_code), signal(result.signal) {}
59 
Dex2OatExecResultOdrMetricsRecord::Dex2OatExecResult60     Dex2OatExecResult() : status(kExecResultNotRun), exit_code(-1), signal(0) {}
61   };
62 
63   int32_t odrefresh_metrics_version;
64   int64_t art_apex_version;
65   int32_t trigger;
66   int32_t stage_reached;
67   int32_t status;
68   int32_t cache_space_free_start_mib;
69   int32_t cache_space_free_end_mib;
70   int32_t primary_bcp_compilation_millis;
71   int32_t secondary_bcp_compilation_millis;
72   int32_t system_server_compilation_millis;
73   Dex2OatExecResult primary_bcp_dex2oat_result;
74   Dex2OatExecResult secondary_bcp_dex2oat_result;
75   Dex2OatExecResult system_server_dex2oat_result;
76   int32_t primary_bcp_compilation_type;
77   int32_t secondary_bcp_compilation_type;
78 
79   // Reads a `MetricsRecord` from an XML file.
80   // Returns an error if the XML document was not found or parsed correctly.
81   android::base::Result<void> ReadFromFile(const std::string& filename);
82 
83   // Writes a `MetricsRecord` to an XML file.
84   // Returns an error if the XML document was not saved correctly.
85   android::base::Result<void> WriteToFile(const std::string& filename) const;
86 };
87 
88 }  // namespace odrefresh
89 }  // namespace art
90 
91 #endif  // ART_ODREFRESH_ODR_METRICS_RECORD_H_
92