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
18 #include "odr_statslog/odr_statslog.h"
19
20 #include <cstdint>
21 #include <fstream>
22 #include <istream>
23 #include <string>
24
25 #include "android-base/logging.h"
26 #include "android-base/stringprintf.h"
27 #include "odr_metrics.h"
28 #include "odr_metrics_record.h"
29 #include "statslog_odrefresh.h"
30
31 namespace art {
32 namespace odrefresh {
33
34 using android::base::StringPrintf;
35
36 namespace {
37
ReadValues(const char * metrics_file,OdrMetricsRecord * record,std::string * error_msg)38 bool ReadValues(const char* metrics_file,
39 /*out*/ OdrMetricsRecord* record,
40 /*out*/ std::string* error_msg) {
41 const android::base::Result<void>& result = record->ReadFromFile(metrics_file);
42 if (!result.ok()) {
43 *error_msg = android::base::StringPrintf("Unable to open or parse metrics file %s (error: %s)",
44 metrics_file,
45 result.error().message().data());
46 return false;
47 }
48
49 return true;
50 }
51
52 } // namespace
53
UploadStatsIfAvailable(std::string * error_msg)54 bool UploadStatsIfAvailable(/*out*/std::string* error_msg) {
55 OdrMetricsRecord record;
56 if (!ReadValues(kOdrefreshMetricsFile, &record, error_msg)) {
57 return false;
58 }
59
60 // Write values to statsd. The order of values passed is the same as the order of the
61 // fields in OdrMetricsRecord.
62 int bytes_written =
63 art::metrics::statsd::stats_write(metrics::statsd::ODREFRESH_REPORTED,
64 record.art_apex_version,
65 record.trigger,
66 record.stage_reached,
67 record.status,
68 record.primary_bcp_compilation_millis / 1000,
69 record.secondary_bcp_compilation_millis / 1000,
70 record.system_server_compilation_millis / 1000,
71 record.cache_space_free_start_mib,
72 record.cache_space_free_end_mib,
73 record.primary_bcp_compilation_millis,
74 record.secondary_bcp_compilation_millis,
75 record.system_server_compilation_millis,
76 record.primary_bcp_dex2oat_result.status,
77 record.primary_bcp_dex2oat_result.exit_code,
78 record.primary_bcp_dex2oat_result.signal,
79 record.secondary_bcp_dex2oat_result.status,
80 record.secondary_bcp_dex2oat_result.exit_code,
81 record.secondary_bcp_dex2oat_result.signal,
82 record.system_server_dex2oat_result.status,
83 record.system_server_dex2oat_result.exit_code,
84 record.system_server_dex2oat_result.signal,
85 record.primary_bcp_compilation_type,
86 record.secondary_bcp_compilation_type);
87 if (bytes_written <= 0) {
88 *error_msg = android::base::StringPrintf("stats_write returned %d", bytes_written);
89 return false;
90 }
91
92 if (unlink(kOdrefreshMetricsFile) != 0) {
93 *error_msg = StringPrintf("failed to unlink '%s': %s", kOdrefreshMetricsFile, strerror(errno));
94 return false;
95 }
96
97 return true;
98 }
99
100 } // namespace odrefresh
101 } // namespace art
102