xref: /aosp_15_r20/system/apex/apexd/apexd_metrics.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.h"
18 
19 #include <android-base/logging.h>
20 #include <android-base/result.h>
21 #include <android-base/strings.h>
22 #include <sys/stat.h>
23 
24 #include <utility>
25 
26 #include "apex_constants.h"
27 #include "apex_file.h"
28 #include "apex_file_repository.h"
29 #include "apex_sha.h"
30 #include "apexd_session.h"
31 #include "apexd_vendor_apex.h"
32 
33 using android::base::Result;
34 using android::base::StartsWith;
35 
36 namespace android::apex {
37 
38 namespace {
39 
40 std::unique_ptr<Metrics> gMetrics;
41 
42 }  // namespace
43 
InitMetrics(std::unique_ptr<Metrics> metrics)44 std::unique_ptr<Metrics> InitMetrics(std::unique_ptr<Metrics> metrics) {
45   std::swap(gMetrics, metrics);
46   return metrics;
47 }
48 
SendApexInstallationEndedAtom(const std::string & package_path,InstallResult install_result)49 void SendApexInstallationEndedAtom(const std::string& package_path,
50                                    InstallResult install_result) {
51   if (!gMetrics) {
52     return;
53   }
54   Result<std::string> hash = CalculateSha256(package_path);
55   if (!hash.ok()) {
56     LOG(WARNING) << "Unable to get sha256 of ApexFile: " << hash.error();
57     return;
58   }
59   gMetrics->SendInstallationEnded(*hash, install_result);
60 }
61 
SendSessionApexInstallationEndedAtom(const ApexSession & session,InstallResult install_result)62 void SendSessionApexInstallationEndedAtom(const ApexSession& session,
63                                           InstallResult install_result) {
64   if (!gMetrics) {
65     return;
66   }
67 
68   for (const auto& hash : session.GetApexFileHashes()) {
69     gMetrics->SendInstallationEnded(hash, install_result);
70   }
71 }
72 
~InstallRequestedEvent()73 InstallRequestedEvent::~InstallRequestedEvent() {
74   if (!gMetrics) {
75     return;
76   }
77   for (const auto& info : files_) {
78     gMetrics->SendInstallationRequested(install_type_, is_rollback_, info);
79   }
80   // Staged installation ends later. No need to send "end" event now.
81   if (succeeded_ && install_type_ == InstallType::Staged) {
82     return;
83   }
84   auto result = succeeded_ ? InstallResult::Success : InstallResult::Failure;
85   for (const auto& info : files_) {
86     gMetrics->SendInstallationEnded(info.file_hash, result);
87   }
88 }
89 
MarkSucceeded()90 void InstallRequestedEvent::MarkSucceeded() { succeeded_ = true; }
91 
AddFiles(std::span<const ApexFile> files)92 void InstallRequestedEvent::AddFiles(std::span<const ApexFile> files) {
93   auto& repo = ApexFileRepository::GetInstance();
94   files_.reserve(files.size());
95   for (const auto& file : files) {
96     Metrics::ApexFileInfo info;
97     info.name = file.GetManifest().name();
98     info.version = file.GetManifest().version();
99     info.shared_libs = file.GetManifest().providesharedapexlibs();
100 
101     const auto& file_path = file.GetPath();
102     struct stat stat_buf;
103     if (stat(file_path.c_str(), &stat_buf) == 0) {
104       info.file_size = stat_buf.st_size;
105     } else {
106       PLOG(WARNING) << "Failed to stat " << file_path;
107       continue;
108     }
109 
110     if (auto result = CalculateSha256(file_path); result.ok()) {
111       info.file_hash = result.value();
112     } else {
113       LOG(WARNING) << "Unable to get sha256 of " << file_path << ": "
114                    << result.error();
115       continue;
116     }
117 
118     if (auto result = repo.GetPartition(file); result.ok()) {
119       info.partition = result.value();
120     } else {
121       LOG(WARNING) << "Failed to get partition of " << file_path << ": "
122                    << result.error();
123       continue;
124     }
125 
126     files_.push_back(std::move(info));
127   }
128 }
129 
AddHals(const std::map<std::string,std::vector<std::string>> & hals)130 void InstallRequestedEvent::AddHals(
131     const std::map<std::string, std::vector<std::string>>& hals) {
132   for (auto& info : files_) {
133     if (auto it = hals.find(info.name); it != hals.end()) {
134       info.hals = it->second;
135     }
136   }
137 }
138 
GetFileHashes() const139 std::vector<std::string> InstallRequestedEvent::GetFileHashes() const {
140   std::vector<std::string> hashes;
141   hashes.reserve(files_.size());
142   for (const auto& info : files_) {
143     hashes.push_back(info.file_hash);
144   }
145   return hashes;
146 }
147 
148 }  // namespace android::apex
149