xref: /aosp_15_r20/system/apex/apexd/apexd_metrics_stats.cpp (revision 33f3758387333dbd2962d7edbd98681940d895da)
1 /*
2  * Copyright (C) 2024 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 #include "apexd_metrics_stats.h"
18 
19 #include <android-base/logging.h>
20 #include <unistd.h>
21 
22 #include "apex_constants.h"
23 #include "apexd_metrics.h"
24 #include "statslog_apex.h"
25 
26 namespace android::apex {
27 
28 namespace {
29 
Cast(InstallType install_type)30 int Cast(InstallType install_type) {
31   switch (install_type) {
32     case InstallType::Staged:
33       return stats::apex::
34           APEX_INSTALLATION_REQUESTED__INSTALLATION_TYPE__STAGED;
35     case InstallType::NonStaged:
36       return stats::apex::
37           APEX_INSTALLATION_REQUESTED__INSTALLATION_TYPE__REBOOTLESS;
38   }
39 }
40 
Cast(InstallResult install_result)41 int Cast(InstallResult install_result) {
42   switch (install_result) {
43     case InstallResult::Success:
44       return stats::apex::
45           APEX_INSTALLATION_ENDED__INSTALLATION_RESULT__INSTALL_SUCCESSFUL;
46     case InstallResult::Failure:
47       return stats::apex::
48           APEX_INSTALLATION_ENDED__INSTALLATION_RESULT__INSTALL_FAILURE_APEX_INSTALLATION;
49   }
50 }
51 
Cast(ApexPartition partition)52 int Cast(ApexPartition partition) {
53   switch (partition) {
54     case ApexPartition::System:
55       return stats::apex::
56           APEX_INSTALLATION_REQUESTED__APEX_PREINSTALL_PARTITION__PARTITION_SYSTEM;
57     case ApexPartition::SystemExt:
58       return stats::apex::
59           APEX_INSTALLATION_REQUESTED__APEX_PREINSTALL_PARTITION__PARTITION_SYSTEM_EXT;
60     case ApexPartition::Product:
61       return stats::apex::
62           APEX_INSTALLATION_REQUESTED__APEX_PREINSTALL_PARTITION__PARTITION_PRODUCT;
63     case ApexPartition::Vendor:
64       return stats::apex::
65           APEX_INSTALLATION_REQUESTED__APEX_PREINSTALL_PARTITION__PARTITION_VENDOR;
66     case ApexPartition::Odm:
67       return stats::apex::
68           APEX_INSTALLATION_REQUESTED__APEX_PREINSTALL_PARTITION__PARTITION_ODM;
69   }
70 }
71 
72 }  // namespace
73 
SendInstallationRequested(InstallType install_type,bool is_rollback,const ApexFileInfo & info)74 void StatsLog::SendInstallationRequested(InstallType install_type,
75                                          bool is_rollback,
76                                          const ApexFileInfo& info) {
77   if (!IsAvailable()) {
78     LOG(WARNING) << "Unable to send atom: libstatssocket is not available";
79     return;
80   }
81   std::vector<const char*> hals_cstr;
82   for (const auto& hal : info.hals) {
83     hals_cstr.push_back(hal.c_str());
84   }
85   int ret = stats::apex::stats_write(
86       stats::apex::APEX_INSTALLATION_REQUESTED, info.name.c_str(), info.version,
87       info.file_size, info.file_hash.c_str(), Cast(info.partition),
88       Cast(install_type), is_rollback, info.shared_libs, hals_cstr);
89   if (ret < 0) {
90     LOG(WARNING) << "Failed to report apex_installation_requested stats";
91   }
92 }
93 
SendInstallationEnded(const std::string & file_hash,InstallResult result)94 void StatsLog::SendInstallationEnded(const std::string& file_hash,
95                                      InstallResult result) {
96   if (!IsAvailable()) {
97     LOG(WARNING) << "Unable to send atom: libstatssocket is not available";
98     return;
99   }
100   int ret = stats::apex::stats_write(stats::apex::APEX_INSTALLATION_ENDED,
101                                      file_hash.c_str(), Cast(result));
102   if (ret < 0) {
103     LOG(WARNING) << "Failed to report apex_installation_ended stats";
104   }
105 }
106 
IsAvailable()107 bool StatsLog::IsAvailable() {
108   return access("/apex/com.android.os.statsd", F_OK) == 0;
109 }
110 
111 }  // namespace android::apex
112