1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2021 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker *
10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker */
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker
18*795d594fSAndroid Build Coastguard Worker #include "odr_statslog/odr_statslog.h"
19*795d594fSAndroid Build Coastguard Worker
20*795d594fSAndroid Build Coastguard Worker #include <cstdint>
21*795d594fSAndroid Build Coastguard Worker #include <fstream>
22*795d594fSAndroid Build Coastguard Worker #include <istream>
23*795d594fSAndroid Build Coastguard Worker #include <string>
24*795d594fSAndroid Build Coastguard Worker
25*795d594fSAndroid Build Coastguard Worker #include "android-base/logging.h"
26*795d594fSAndroid Build Coastguard Worker #include "android-base/stringprintf.h"
27*795d594fSAndroid Build Coastguard Worker #include "odr_metrics.h"
28*795d594fSAndroid Build Coastguard Worker #include "odr_metrics_record.h"
29*795d594fSAndroid Build Coastguard Worker #include "statslog_odrefresh.h"
30*795d594fSAndroid Build Coastguard Worker
31*795d594fSAndroid Build Coastguard Worker namespace art {
32*795d594fSAndroid Build Coastguard Worker namespace odrefresh {
33*795d594fSAndroid Build Coastguard Worker
34*795d594fSAndroid Build Coastguard Worker using android::base::StringPrintf;
35*795d594fSAndroid Build Coastguard Worker
36*795d594fSAndroid Build Coastguard Worker namespace {
37*795d594fSAndroid Build Coastguard Worker
ReadValues(const char * metrics_file,OdrMetricsRecord * record,std::string * error_msg)38*795d594fSAndroid Build Coastguard Worker bool ReadValues(const char* metrics_file,
39*795d594fSAndroid Build Coastguard Worker /*out*/ OdrMetricsRecord* record,
40*795d594fSAndroid Build Coastguard Worker /*out*/ std::string* error_msg) {
41*795d594fSAndroid Build Coastguard Worker const android::base::Result<void>& result = record->ReadFromFile(metrics_file);
42*795d594fSAndroid Build Coastguard Worker if (!result.ok()) {
43*795d594fSAndroid Build Coastguard Worker *error_msg = android::base::StringPrintf("Unable to open or parse metrics file %s (error: %s)",
44*795d594fSAndroid Build Coastguard Worker metrics_file,
45*795d594fSAndroid Build Coastguard Worker result.error().message().data());
46*795d594fSAndroid Build Coastguard Worker return false;
47*795d594fSAndroid Build Coastguard Worker }
48*795d594fSAndroid Build Coastguard Worker
49*795d594fSAndroid Build Coastguard Worker return true;
50*795d594fSAndroid Build Coastguard Worker }
51*795d594fSAndroid Build Coastguard Worker
52*795d594fSAndroid Build Coastguard Worker } // namespace
53*795d594fSAndroid Build Coastguard Worker
UploadStatsIfAvailable(std::string * error_msg)54*795d594fSAndroid Build Coastguard Worker bool UploadStatsIfAvailable(/*out*/std::string* error_msg) {
55*795d594fSAndroid Build Coastguard Worker OdrMetricsRecord record;
56*795d594fSAndroid Build Coastguard Worker if (!ReadValues(kOdrefreshMetricsFile, &record, error_msg)) {
57*795d594fSAndroid Build Coastguard Worker return false;
58*795d594fSAndroid Build Coastguard Worker }
59*795d594fSAndroid Build Coastguard Worker
60*795d594fSAndroid Build Coastguard Worker // Write values to statsd. The order of values passed is the same as the order of the
61*795d594fSAndroid Build Coastguard Worker // fields in OdrMetricsRecord.
62*795d594fSAndroid Build Coastguard Worker int bytes_written =
63*795d594fSAndroid Build Coastguard Worker art::metrics::statsd::stats_write(metrics::statsd::ODREFRESH_REPORTED,
64*795d594fSAndroid Build Coastguard Worker record.art_apex_version,
65*795d594fSAndroid Build Coastguard Worker record.trigger,
66*795d594fSAndroid Build Coastguard Worker record.stage_reached,
67*795d594fSAndroid Build Coastguard Worker record.status,
68*795d594fSAndroid Build Coastguard Worker record.primary_bcp_compilation_millis / 1000,
69*795d594fSAndroid Build Coastguard Worker record.secondary_bcp_compilation_millis / 1000,
70*795d594fSAndroid Build Coastguard Worker record.system_server_compilation_millis / 1000,
71*795d594fSAndroid Build Coastguard Worker record.cache_space_free_start_mib,
72*795d594fSAndroid Build Coastguard Worker record.cache_space_free_end_mib,
73*795d594fSAndroid Build Coastguard Worker record.primary_bcp_compilation_millis,
74*795d594fSAndroid Build Coastguard Worker record.secondary_bcp_compilation_millis,
75*795d594fSAndroid Build Coastguard Worker record.system_server_compilation_millis,
76*795d594fSAndroid Build Coastguard Worker record.primary_bcp_dex2oat_result.status,
77*795d594fSAndroid Build Coastguard Worker record.primary_bcp_dex2oat_result.exit_code,
78*795d594fSAndroid Build Coastguard Worker record.primary_bcp_dex2oat_result.signal,
79*795d594fSAndroid Build Coastguard Worker record.secondary_bcp_dex2oat_result.status,
80*795d594fSAndroid Build Coastguard Worker record.secondary_bcp_dex2oat_result.exit_code,
81*795d594fSAndroid Build Coastguard Worker record.secondary_bcp_dex2oat_result.signal,
82*795d594fSAndroid Build Coastguard Worker record.system_server_dex2oat_result.status,
83*795d594fSAndroid Build Coastguard Worker record.system_server_dex2oat_result.exit_code,
84*795d594fSAndroid Build Coastguard Worker record.system_server_dex2oat_result.signal,
85*795d594fSAndroid Build Coastguard Worker record.primary_bcp_compilation_type,
86*795d594fSAndroid Build Coastguard Worker record.secondary_bcp_compilation_type);
87*795d594fSAndroid Build Coastguard Worker if (bytes_written <= 0) {
88*795d594fSAndroid Build Coastguard Worker *error_msg = android::base::StringPrintf("stats_write returned %d", bytes_written);
89*795d594fSAndroid Build Coastguard Worker return false;
90*795d594fSAndroid Build Coastguard Worker }
91*795d594fSAndroid Build Coastguard Worker
92*795d594fSAndroid Build Coastguard Worker if (unlink(kOdrefreshMetricsFile) != 0) {
93*795d594fSAndroid Build Coastguard Worker *error_msg = StringPrintf("failed to unlink '%s': %s", kOdrefreshMetricsFile, strerror(errno));
94*795d594fSAndroid Build Coastguard Worker return false;
95*795d594fSAndroid Build Coastguard Worker }
96*795d594fSAndroid Build Coastguard Worker
97*795d594fSAndroid Build Coastguard Worker return true;
98*795d594fSAndroid Build Coastguard Worker }
99*795d594fSAndroid Build Coastguard Worker
100*795d594fSAndroid Build Coastguard Worker } // namespace odrefresh
101*795d594fSAndroid Build Coastguard Worker } // namespace art
102