blob: 01f3e271a82d1f3380b04d2f530c267ecb5a29f7 [file] [log] [blame]
Seungjae Yoo0a8c84c2022-07-11 08:19:15 +00001// Copyright 2022, The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Functions for creating and collecting atoms.
16
17use crate::aidl::clone_file;
18use android_system_virtualizationservice::aidl::android::system::virtualizationservice::{
Seungjae Yoob4c07ba2022-08-12 04:44:52 +000019 DeathReason::DeathReason, IVirtualMachine::IVirtualMachine,
20 VirtualMachineAppConfig::VirtualMachineAppConfig, VirtualMachineConfig::VirtualMachineConfig,
Seungjae Yoo0a8c84c2022-07-11 08:19:15 +000021};
22use android_system_virtualizationservice::binder::{Status, Strong};
23use anyhow::{anyhow, Result};
Seungjae Yoo62085c02022-08-12 04:44:52 +000024use binder::ThreadState;
Seungjae Yoo0a8c84c2022-07-11 08:19:15 +000025use log::{trace, warn};
26use microdroid_payload_config::VmPayloadConfig;
Seungjae Yoob4c07ba2022-08-12 04:44:52 +000027use statslog_virtualization_rust::{vm_booted, vm_creation_requested, vm_exited};
Seungjae Yoo2e7beea2022-08-24 16:09:12 +090028use std::time::{Duration, SystemTime};
Seungjae Yoo0a8c84c2022-07-11 08:19:15 +000029use zip::ZipArchive;
30
31fn get_vm_payload_config(config: &VirtualMachineAppConfig) -> Result<VmPayloadConfig> {
32 let apk = config.apk.as_ref().ok_or_else(|| anyhow!("APK is none"))?;
33 let apk_file = clone_file(apk)?;
34 let mut apk_zip = ZipArchive::new(&apk_file)?;
35 let config_file = apk_zip.by_name(&config.configPath)?;
36 let vm_payload_config: VmPayloadConfig = serde_json::from_reader(config_file)?;
37 Ok(vm_payload_config)
38}
39
Seungjae Yoo2e7beea2022-08-24 16:09:12 +090040fn get_duration(vm_start_timestamp: Option<SystemTime>) -> Duration {
41 match vm_start_timestamp {
42 Some(vm_start_timestamp) => vm_start_timestamp.elapsed().unwrap_or_default(),
43 None => Duration::default(),
44 }
45}
46
Seungjae Yoo0a8c84c2022-07-11 08:19:15 +000047/// Write the stats of VMCreation to statsd
48pub fn write_vm_creation_stats(
49 config: &VirtualMachineConfig,
50 is_protected: bool,
51 ret: &binder::Result<Strong<dyn IVirtualMachine>>,
52) {
53 let creation_succeeded;
54 let binder_exception_code;
55 match ret {
56 Ok(_) => {
57 creation_succeeded = true;
58 binder_exception_code = Status::ok().exception_code() as i32;
59 }
60 Err(ref e) => {
61 creation_succeeded = false;
62 binder_exception_code = e.exception_code() as i32;
63 }
64 }
65
Seungjae Yoo62085c02022-08-12 04:44:52 +000066 let vm_identifier;
Seungjae Yoo0a8c84c2022-07-11 08:19:15 +000067 let config_type;
68 let num_cpus;
69 let cpu_affinity;
70 let memory_mib;
71 let apexes;
72 match config {
73 VirtualMachineConfig::AppConfig(config) => {
Seungjae Yoo62085c02022-08-12 04:44:52 +000074 vm_identifier = &config.name;
Seungjae Yoo0a8c84c2022-07-11 08:19:15 +000075 config_type = vm_creation_requested::ConfigType::VirtualMachineAppConfig;
76 num_cpus = config.numCpus;
77 cpu_affinity = config.cpuAffinity.clone().unwrap_or_default();
78 memory_mib = config.memoryMib;
79
80 let vm_payload_config = get_vm_payload_config(config);
81 if let Ok(vm_payload_config) = vm_payload_config {
82 apexes = vm_payload_config
83 .apexes
84 .iter()
85 .map(|x| x.name.clone())
86 .collect::<Vec<String>>()
87 .join(":");
88 } else {
89 apexes = "INFO: Can't get VmPayloadConfig".into();
90 }
91 }
92 VirtualMachineConfig::RawConfig(config) => {
Seungjae Yoo62085c02022-08-12 04:44:52 +000093 vm_identifier = &config.name;
Seungjae Yoo0a8c84c2022-07-11 08:19:15 +000094 config_type = vm_creation_requested::ConfigType::VirtualMachineRawConfig;
95 num_cpus = config.numCpus;
96 cpu_affinity = config.cpuAffinity.clone().unwrap_or_default();
97 memory_mib = config.memoryMib;
98 apexes = String::new();
99 }
100 }
101
Seungjae Yoo0a8c84c2022-07-11 08:19:15 +0000102 let vm_creation_requested = vm_creation_requested::VmCreationRequested {
Seungjae Yoo62085c02022-08-12 04:44:52 +0000103 uid: ThreadState::get_calling_uid() as i32,
104 vm_identifier,
Seungjae Yoo0a8c84c2022-07-11 08:19:15 +0000105 hypervisor: vm_creation_requested::Hypervisor::Pkvm,
106 is_protected,
107 creation_succeeded,
108 binder_exception_code,
109 config_type,
110 num_cpus,
111 cpu_affinity: &cpu_affinity,
112 memory_mib,
113 apexes: &apexes,
114 // TODO(seungjaeyoo) Fill information about task_profile
115 // TODO(seungjaeyoo) Fill information about disk_image for raw config
116 };
117
118 match vm_creation_requested.stats_write() {
119 Err(e) => {
120 warn!("statslog_rust failed with error: {}", e);
121 }
122 Ok(_) => trace!("statslog_rust succeeded for virtualization service"),
123 }
124}
Seungjae Yooacf559a2022-08-12 04:44:51 +0000125
126/// Write the stats of VM boot to statsd
Seungjae Yoo2e7beea2022-08-24 16:09:12 +0900127pub fn write_vm_booted_stats(
128 uid: i32,
129 vm_identifier: &String,
130 vm_start_timestamp: Option<SystemTime>,
131) {
132 let duration = get_duration(vm_start_timestamp);
133 let vm_booted = vm_booted::VmBooted {
134 uid,
135 vm_identifier,
136 elapsed_time_millis: duration.as_millis() as i64,
137 };
Seungjae Yooacf559a2022-08-12 04:44:51 +0000138 match vm_booted.stats_write() {
139 Err(e) => {
140 warn!("statslog_rust failed with error: {}", e);
141 }
142 Ok(_) => trace!("statslog_rust succeeded for virtualization service"),
143 }
144}
Seungjae Yoob4c07ba2022-08-12 04:44:52 +0000145
146/// Write the stats of VM exit to statsd
Seungjae Yoo2e7beea2022-08-24 16:09:12 +0900147pub fn write_vm_exited_stats(
148 uid: i32,
149 vm_identifier: &String,
150 reason: DeathReason,
151 vm_start_timestamp: Option<SystemTime>,
152) {
153 let duration = get_duration(vm_start_timestamp);
Seungjae Yoob4c07ba2022-08-12 04:44:52 +0000154 let vm_exited = vm_exited::VmExited {
Seungjae Yoo62085c02022-08-12 04:44:52 +0000155 uid,
156 vm_identifier,
Seungjae Yoo2e7beea2022-08-24 16:09:12 +0900157 elapsed_time_millis: duration.as_millis() as i64,
Seungjae Yoob4c07ba2022-08-12 04:44:52 +0000158 death_reason: match reason {
159 DeathReason::INFRASTRUCTURE_ERROR => vm_exited::DeathReason::InfrastructureError,
160 DeathReason::KILLED => vm_exited::DeathReason::Killed,
161 DeathReason::UNKNOWN => vm_exited::DeathReason::Unknown,
162 DeathReason::SHUTDOWN => vm_exited::DeathReason::Shutdown,
163 DeathReason::ERROR => vm_exited::DeathReason::Error,
164 DeathReason::REBOOT => vm_exited::DeathReason::Reboot,
165 DeathReason::CRASH => vm_exited::DeathReason::Crash,
166 DeathReason::PVM_FIRMWARE_PUBLIC_KEY_MISMATCH => {
167 vm_exited::DeathReason::PvmFirmwarePublicKeyMismatch
168 }
169 DeathReason::PVM_FIRMWARE_INSTANCE_IMAGE_CHANGED => {
170 vm_exited::DeathReason::PvmFirmwareInstanceImageChanged
171 }
172 DeathReason::BOOTLOADER_PUBLIC_KEY_MISMATCH => {
173 vm_exited::DeathReason::BootloaderPublicKeyMismatch
174 }
175 DeathReason::BOOTLOADER_INSTANCE_IMAGE_CHANGED => {
176 vm_exited::DeathReason::BootloaderInstanceImageChanged
177 }
178 DeathReason::MICRODROID_FAILED_TO_CONNECT_TO_VIRTUALIZATION_SERVICE => {
179 vm_exited::DeathReason::MicrodroidFailedToConnectToVirtualizationService
180 }
181 DeathReason::MICRODROID_PAYLOAD_HAS_CHANGED => {
182 vm_exited::DeathReason::MicrodroidPayloadHasChanged
183 }
184 DeathReason::MICRODROID_PAYLOAD_VERIFICATION_FAILED => {
185 vm_exited::DeathReason::MicrodroidPayloadVerificationFailed
186 }
187 DeathReason::MICRODROID_INVALID_PAYLOAD_CONFIG => {
188 vm_exited::DeathReason::MicrodroidInvalidPayloadConfig
189 }
190 DeathReason::MICRODROID_UNKNOWN_RUNTIME_ERROR => {
191 vm_exited::DeathReason::MicrodroidUnknownRuntimeError
192 }
193 DeathReason::HANGUP => vm_exited::DeathReason::Hangup,
194 _ => vm_exited::DeathReason::Unknown,
195 },
196 };
197 match vm_exited.stats_write() {
198 Err(e) => {
199 warn!("statslog_rust failed with error: {}", e);
200 }
201 Ok(_) => trace!("statslog_rust succeeded for virtualization service"),
202 }
203}