Seungjae Yoo | 0a8c84c | 2022-07-11 08:19:15 +0000 | [diff] [blame] | 1 | // 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 | |
| 17 | use crate::aidl::clone_file; |
Seungjae Yoo | 6d265d9 | 2022-11-15 10:51:33 +0900 | [diff] [blame^] | 18 | use crate::crosvm::VmMetric; |
Seungjae Yoo | 0a8c84c | 2022-07-11 08:19:15 +0000 | [diff] [blame] | 19 | use android_system_virtualizationservice::aidl::android::system::virtualizationservice::{ |
Alan Stokes | 0d1ef78 | 2022-09-27 13:46:35 +0100 | [diff] [blame] | 20 | DeathReason::DeathReason, |
| 21 | IVirtualMachine::IVirtualMachine, |
| 22 | VirtualMachineAppConfig::{Payload::Payload, VirtualMachineAppConfig}, |
| 23 | VirtualMachineConfig::VirtualMachineConfig, |
Seungjae Yoo | 0a8c84c | 2022-07-11 08:19:15 +0000 | [diff] [blame] | 24 | }; |
| 25 | use android_system_virtualizationservice::binder::{Status, Strong}; |
| 26 | use anyhow::{anyhow, Result}; |
Alan Stokes | 0d1ef78 | 2022-09-27 13:46:35 +0100 | [diff] [blame] | 27 | use binder::{ParcelFileDescriptor, ThreadState}; |
Seungjae Yoo | 0a8c84c | 2022-07-11 08:19:15 +0000 | [diff] [blame] | 28 | use log::{trace, warn}; |
| 29 | use microdroid_payload_config::VmPayloadConfig; |
Shikha Panwar | c93a42b | 2022-11-09 14:12:25 +0000 | [diff] [blame] | 30 | use rustutils::system_properties; |
Seungjae Yoo | dd91f0f | 2022-11-09 15:25:21 +0900 | [diff] [blame] | 31 | use statslog_virtualization_rust::{vm_booted, vm_creation_requested, vm_exited}; |
Shikha Panwar | c93a42b | 2022-11-09 14:12:25 +0000 | [diff] [blame] | 32 | use std::thread; |
Seungjae Yoo | 2e7beea | 2022-08-24 16:09:12 +0900 | [diff] [blame] | 33 | use std::time::{Duration, SystemTime}; |
Seungjae Yoo | 0a8c84c | 2022-07-11 08:19:15 +0000 | [diff] [blame] | 34 | use zip::ZipArchive; |
| 35 | |
Alan Stokes | 0d1ef78 | 2022-09-27 13:46:35 +0100 | [diff] [blame] | 36 | fn get_apex_list(config: &VirtualMachineAppConfig) -> String { |
| 37 | match &config.payload { |
| 38 | Payload::PayloadConfig(_) => String::new(), |
| 39 | Payload::ConfigPath(config_path) => { |
| 40 | let vm_payload_config = get_vm_payload_config(&config.apk, config_path); |
| 41 | if let Ok(vm_payload_config) = vm_payload_config { |
| 42 | vm_payload_config |
| 43 | .apexes |
| 44 | .iter() |
| 45 | .map(|x| x.name.clone()) |
| 46 | .collect::<Vec<String>>() |
| 47 | .join(":") |
| 48 | } else { |
| 49 | "INFO: Can't get VmPayloadConfig".to_owned() |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | fn get_vm_payload_config( |
| 56 | apk_fd: &Option<ParcelFileDescriptor>, |
| 57 | config_path: &str, |
| 58 | ) -> Result<VmPayloadConfig> { |
| 59 | let apk = apk_fd.as_ref().ok_or_else(|| anyhow!("APK is none"))?; |
Seungjae Yoo | 0a8c84c | 2022-07-11 08:19:15 +0000 | [diff] [blame] | 60 | let apk_file = clone_file(apk)?; |
| 61 | let mut apk_zip = ZipArchive::new(&apk_file)?; |
Alan Stokes | 0d1ef78 | 2022-09-27 13:46:35 +0100 | [diff] [blame] | 62 | let config_file = apk_zip.by_name(config_path)?; |
Seungjae Yoo | 0a8c84c | 2022-07-11 08:19:15 +0000 | [diff] [blame] | 63 | let vm_payload_config: VmPayloadConfig = serde_json::from_reader(config_file)?; |
| 64 | Ok(vm_payload_config) |
| 65 | } |
| 66 | |
Seungjae Yoo | 2e7beea | 2022-08-24 16:09:12 +0900 | [diff] [blame] | 67 | fn get_duration(vm_start_timestamp: Option<SystemTime>) -> Duration { |
| 68 | match vm_start_timestamp { |
| 69 | Some(vm_start_timestamp) => vm_start_timestamp.elapsed().unwrap_or_default(), |
| 70 | None => Duration::default(), |
| 71 | } |
| 72 | } |
| 73 | |
Seungjae Yoo | 0a8c84c | 2022-07-11 08:19:15 +0000 | [diff] [blame] | 74 | /// Write the stats of VMCreation to statsd |
| 75 | pub fn write_vm_creation_stats( |
| 76 | config: &VirtualMachineConfig, |
| 77 | is_protected: bool, |
| 78 | ret: &binder::Result<Strong<dyn IVirtualMachine>>, |
| 79 | ) { |
| 80 | let creation_succeeded; |
| 81 | let binder_exception_code; |
| 82 | match ret { |
| 83 | Ok(_) => { |
| 84 | creation_succeeded = true; |
| 85 | binder_exception_code = Status::ok().exception_code() as i32; |
| 86 | } |
| 87 | Err(ref e) => { |
| 88 | creation_succeeded = false; |
| 89 | binder_exception_code = e.exception_code() as i32; |
| 90 | } |
| 91 | } |
Alan Stokes | 0d1ef78 | 2022-09-27 13:46:35 +0100 | [diff] [blame] | 92 | let (vm_identifier, config_type, num_cpus, memory_mib, apexes) = match config { |
| 93 | VirtualMachineConfig::AppConfig(config) => ( |
Shikha Panwar | c93a42b | 2022-11-09 14:12:25 +0000 | [diff] [blame] | 94 | config.name.clone(), |
Alan Stokes | 0d1ef78 | 2022-09-27 13:46:35 +0100 | [diff] [blame] | 95 | vm_creation_requested::ConfigType::VirtualMachineAppConfig, |
| 96 | config.numCpus, |
| 97 | config.memoryMib, |
| 98 | get_apex_list(config), |
| 99 | ), |
| 100 | VirtualMachineConfig::RawConfig(config) => ( |
Shikha Panwar | c93a42b | 2022-11-09 14:12:25 +0000 | [diff] [blame] | 101 | config.name.clone(), |
Alan Stokes | 0d1ef78 | 2022-09-27 13:46:35 +0100 | [diff] [blame] | 102 | vm_creation_requested::ConfigType::VirtualMachineRawConfig, |
| 103 | config.numCpus, |
| 104 | config.memoryMib, |
| 105 | String::new(), |
| 106 | ), |
| 107 | }; |
Seungjae Yoo | 0a8c84c | 2022-07-11 08:19:15 +0000 | [diff] [blame] | 108 | |
Shikha Panwar | c93a42b | 2022-11-09 14:12:25 +0000 | [diff] [blame] | 109 | let uid = ThreadState::get_calling_uid() as i32; |
| 110 | thread::spawn(move || { |
| 111 | let vm_creation_requested = vm_creation_requested::VmCreationRequested { |
| 112 | uid, |
| 113 | vm_identifier: &vm_identifier, |
| 114 | hypervisor: vm_creation_requested::Hypervisor::Pkvm, |
| 115 | is_protected, |
| 116 | creation_succeeded, |
| 117 | binder_exception_code, |
| 118 | config_type, |
| 119 | num_cpus, |
| 120 | cpu_affinity: "", // deprecated |
| 121 | memory_mib, |
| 122 | apexes: &apexes, |
| 123 | // TODO(seungjaeyoo) Fill information about task_profile |
| 124 | // TODO(seungjaeyoo) Fill information about disk_image for raw config |
| 125 | }; |
Seungjae Yoo | 0a8c84c | 2022-07-11 08:19:15 +0000 | [diff] [blame] | 126 | |
Shikha Panwar | c93a42b | 2022-11-09 14:12:25 +0000 | [diff] [blame] | 127 | wait_for_statsd().unwrap_or_else(|e| warn!("failed to wait for statsd with error: {}", e)); |
| 128 | match vm_creation_requested.stats_write() { |
| 129 | Err(e) => { |
| 130 | warn!("statslog_rust failed with error: {}", e); |
| 131 | } |
| 132 | Ok(_) => trace!("statslog_rust succeeded for virtualization service"), |
Seungjae Yoo | 0a8c84c | 2022-07-11 08:19:15 +0000 | [diff] [blame] | 133 | } |
Shikha Panwar | c93a42b | 2022-11-09 14:12:25 +0000 | [diff] [blame] | 134 | }); |
Seungjae Yoo | 0a8c84c | 2022-07-11 08:19:15 +0000 | [diff] [blame] | 135 | } |
Seungjae Yoo | acf559a | 2022-08-12 04:44:51 +0000 | [diff] [blame] | 136 | |
| 137 | /// Write the stats of VM boot to statsd |
Shikha Panwar | c93a42b | 2022-11-09 14:12:25 +0000 | [diff] [blame] | 138 | /// The function creates a separate thread which waits fro statsd to start to push atom |
Seungjae Yoo | 2e7beea | 2022-08-24 16:09:12 +0900 | [diff] [blame] | 139 | pub fn write_vm_booted_stats( |
| 140 | uid: i32, |
Shikha Panwar | c93a42b | 2022-11-09 14:12:25 +0000 | [diff] [blame] | 141 | vm_identifier: &str, |
Seungjae Yoo | 2e7beea | 2022-08-24 16:09:12 +0900 | [diff] [blame] | 142 | vm_start_timestamp: Option<SystemTime>, |
| 143 | ) { |
Shikha Panwar | c93a42b | 2022-11-09 14:12:25 +0000 | [diff] [blame] | 144 | let vm_identifier = vm_identifier.to_owned(); |
Seungjae Yoo | 2e7beea | 2022-08-24 16:09:12 +0900 | [diff] [blame] | 145 | let duration = get_duration(vm_start_timestamp); |
Shikha Panwar | c93a42b | 2022-11-09 14:12:25 +0000 | [diff] [blame] | 146 | thread::spawn(move || { |
| 147 | let vm_booted = vm_booted::VmBooted { |
| 148 | uid, |
| 149 | vm_identifier: &vm_identifier, |
| 150 | elapsed_time_millis: duration.as_millis() as i64, |
| 151 | }; |
| 152 | wait_for_statsd().unwrap_or_else(|e| warn!("failed to wait for statsd with error: {}", e)); |
| 153 | match vm_booted.stats_write() { |
| 154 | Err(e) => { |
| 155 | warn!("statslog_rust failed with error: {}", e); |
| 156 | } |
| 157 | Ok(_) => trace!("statslog_rust succeeded for virtualization service"), |
Seungjae Yoo | acf559a | 2022-08-12 04:44:51 +0000 | [diff] [blame] | 158 | } |
Shikha Panwar | c93a42b | 2022-11-09 14:12:25 +0000 | [diff] [blame] | 159 | }); |
Seungjae Yoo | acf559a | 2022-08-12 04:44:51 +0000 | [diff] [blame] | 160 | } |
Seungjae Yoo | b4c07ba | 2022-08-12 04:44:52 +0000 | [diff] [blame] | 161 | |
| 162 | /// Write the stats of VM exit to statsd |
Shikha Panwar | c93a42b | 2022-11-09 14:12:25 +0000 | [diff] [blame] | 163 | /// The function creates a separate thread which waits fro statsd to start to push atom |
Seungjae Yoo | 2e7beea | 2022-08-24 16:09:12 +0900 | [diff] [blame] | 164 | pub fn write_vm_exited_stats( |
| 165 | uid: i32, |
Shikha Panwar | c93a42b | 2022-11-09 14:12:25 +0000 | [diff] [blame] | 166 | vm_identifier: &str, |
Seungjae Yoo | 2e7beea | 2022-08-24 16:09:12 +0900 | [diff] [blame] | 167 | reason: DeathReason, |
Seungjae Yoo | 6d265d9 | 2022-11-15 10:51:33 +0900 | [diff] [blame^] | 168 | vm_metric: &VmMetric, |
Seungjae Yoo | 2e7beea | 2022-08-24 16:09:12 +0900 | [diff] [blame] | 169 | ) { |
Shikha Panwar | c93a42b | 2022-11-09 14:12:25 +0000 | [diff] [blame] | 170 | let vm_identifier = vm_identifier.to_owned(); |
Seungjae Yoo | 6d265d9 | 2022-11-15 10:51:33 +0900 | [diff] [blame^] | 171 | let elapsed_time_millis = get_duration(vm_metric.start_timestamp).as_millis() as i64; |
| 172 | let guest_time_millis = vm_metric.cpu_guest_time.unwrap_or_default(); |
| 173 | let rss = vm_metric.rss.unwrap_or_default(); |
| 174 | |
Shikha Panwar | c93a42b | 2022-11-09 14:12:25 +0000 | [diff] [blame] | 175 | thread::spawn(move || { |
| 176 | let vm_exited = vm_exited::VmExited { |
| 177 | uid, |
| 178 | vm_identifier: &vm_identifier, |
Seungjae Yoo | 6d265d9 | 2022-11-15 10:51:33 +0900 | [diff] [blame^] | 179 | elapsed_time_millis, |
Shikha Panwar | c93a42b | 2022-11-09 14:12:25 +0000 | [diff] [blame] | 180 | death_reason: match reason { |
| 181 | DeathReason::INFRASTRUCTURE_ERROR => vm_exited::DeathReason::InfrastructureError, |
| 182 | DeathReason::KILLED => vm_exited::DeathReason::Killed, |
| 183 | DeathReason::UNKNOWN => vm_exited::DeathReason::Unknown, |
| 184 | DeathReason::SHUTDOWN => vm_exited::DeathReason::Shutdown, |
| 185 | DeathReason::ERROR => vm_exited::DeathReason::Error, |
| 186 | DeathReason::REBOOT => vm_exited::DeathReason::Reboot, |
| 187 | DeathReason::CRASH => vm_exited::DeathReason::Crash, |
| 188 | DeathReason::PVM_FIRMWARE_PUBLIC_KEY_MISMATCH => { |
| 189 | vm_exited::DeathReason::PvmFirmwarePublicKeyMismatch |
| 190 | } |
| 191 | DeathReason::PVM_FIRMWARE_INSTANCE_IMAGE_CHANGED => { |
| 192 | vm_exited::DeathReason::PvmFirmwareInstanceImageChanged |
| 193 | } |
| 194 | DeathReason::BOOTLOADER_PUBLIC_KEY_MISMATCH => { |
| 195 | vm_exited::DeathReason::BootloaderPublicKeyMismatch |
| 196 | } |
| 197 | DeathReason::BOOTLOADER_INSTANCE_IMAGE_CHANGED => { |
| 198 | vm_exited::DeathReason::BootloaderInstanceImageChanged |
| 199 | } |
| 200 | DeathReason::MICRODROID_FAILED_TO_CONNECT_TO_VIRTUALIZATION_SERVICE => { |
| 201 | vm_exited::DeathReason::MicrodroidFailedToConnectToVirtualizationService |
| 202 | } |
| 203 | DeathReason::MICRODROID_PAYLOAD_HAS_CHANGED => { |
| 204 | vm_exited::DeathReason::MicrodroidPayloadHasChanged |
| 205 | } |
| 206 | DeathReason::MICRODROID_PAYLOAD_VERIFICATION_FAILED => { |
| 207 | vm_exited::DeathReason::MicrodroidPayloadVerificationFailed |
| 208 | } |
| 209 | DeathReason::MICRODROID_INVALID_PAYLOAD_CONFIG => { |
| 210 | vm_exited::DeathReason::MicrodroidInvalidPayloadConfig |
| 211 | } |
| 212 | DeathReason::MICRODROID_UNKNOWN_RUNTIME_ERROR => { |
| 213 | vm_exited::DeathReason::MicrodroidUnknownRuntimeError |
| 214 | } |
| 215 | DeathReason::HANGUP => vm_exited::DeathReason::Hangup, |
| 216 | _ => vm_exited::DeathReason::Unknown, |
| 217 | }, |
Seungjae Yoo | 6d265d9 | 2022-11-15 10:51:33 +0900 | [diff] [blame^] | 218 | guest_time_millis, |
| 219 | rss_vm_kb: rss.vm, |
| 220 | rss_crosvm_kb: rss.crosvm, |
Shikha Panwar | c93a42b | 2022-11-09 14:12:25 +0000 | [diff] [blame] | 221 | }; |
| 222 | wait_for_statsd().unwrap_or_else(|e| warn!("failed to wait for statsd with error: {}", e)); |
| 223 | match vm_exited.stats_write() { |
| 224 | Err(e) => { |
| 225 | warn!("statslog_rust failed with error: {}", e); |
Seungjae Yoo | b4c07ba | 2022-08-12 04:44:52 +0000 | [diff] [blame] | 226 | } |
Shikha Panwar | c93a42b | 2022-11-09 14:12:25 +0000 | [diff] [blame] | 227 | Ok(_) => trace!("statslog_rust succeeded for virtualization service"), |
Seungjae Yoo | b4c07ba | 2022-08-12 04:44:52 +0000 | [diff] [blame] | 228 | } |
Shikha Panwar | c93a42b | 2022-11-09 14:12:25 +0000 | [diff] [blame] | 229 | }); |
| 230 | } |
| 231 | |
| 232 | fn wait_for_statsd() -> Result<()> { |
| 233 | let mut prop = system_properties::PropertyWatcher::new("init.svc.statsd")?; |
| 234 | loop { |
| 235 | prop.wait()?; |
| 236 | match system_properties::read("init.svc.statsd")? { |
| 237 | Some(s) => { |
| 238 | if s == "running" { |
| 239 | break; |
| 240 | } |
| 241 | } |
| 242 | None => { |
| 243 | // This case never really happens because |
| 244 | // prop.wait() waits for property to be non-null. |
| 245 | break; |
| 246 | } |
| 247 | } |
Seungjae Yoo | b4c07ba | 2022-08-12 04:44:52 +0000 | [diff] [blame] | 248 | } |
Shikha Panwar | c93a42b | 2022-11-09 14:12:25 +0000 | [diff] [blame] | 249 | Ok(()) |
Seungjae Yoo | b4c07ba | 2022-08-12 04:44:52 +0000 | [diff] [blame] | 250 | } |