| // Copyright 2022, The Android Open Source Project | |
| // | |
| // Licensed under the Apache License, Version 2.0 (the "License"); | |
| // you may not use this file except in compliance with the License. | |
| // You may obtain a copy of the License at | |
| // | |
| // http://www.apache.org/licenses/LICENSE-2.0 | |
| // | |
| // Unless required by applicable law or agreed to in writing, software | |
| // distributed under the License is distributed on an "AS IS" BASIS, | |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| // See the License for the specific language governing permissions and | |
| // limitations under the License. | |
| //! Functions for creating and collecting atoms. | |
| use crate::aidl as I; | |
| use crate::crosvm::VmMetric; | |
| use crate::get_calling_uid; | |
| use crate::virtualmachine; | |
| use anyhow::{Result, anyhow}; | |
| use binder::{ParcelFileDescriptor, Status, Strong}; | |
| use log::{info, warn}; | |
| use microdroid_payload_config::VmPayloadConfig; | |
| use statslog_virtualization_rust::vm_creation_requested; | |
| use std::time::{Duration, Instant}; | |
| use vm_profiles::cpu_utils::get_num_cpus; | |
| use zip::ZipArchive; | |
| const INVALID_NUM_CPUS: i32 = -1; | |
| fn get_apex_list(config: &I::VirtualMachineAppConfig) -> String { | |
| match &config.payload { | |
| I::Payload::PayloadConfig(_) => String::new(), | |
| I::Payload::ConfigPath(config_path) => { | |
| let vm_payload_config = get_vm_payload_config(config.apk.as_ref(), config_path); | |
| if let Ok(vm_payload_config) = vm_payload_config { | |
| vm_payload_config | |
| .apexes | |
| .iter() | |
| .map(|x| x.name.clone()) | |
| .collect::<Vec<String>>() | |
| .join(":") | |
| } else { | |
| "INFO: Can't get VmPayloadConfig".to_owned() | |
| } | |
| } | |
| } | |
| } | |
| fn get_vm_payload_config( | |
| apk_fd: Option<&ParcelFileDescriptor>, | |
| config_path: &str, | |
| ) -> Result<VmPayloadConfig> { | |
| let apk = apk_fd.as_ref().ok_or_else(|| anyhow!("APK is none"))?; | |
| let apk_file = virtualmachine::clone_file(apk)?; | |
| let mut apk_zip = ZipArchive::new(&apk_file)?; | |
| let config_file = apk_zip.by_name(config_path)?; | |
| let vm_payload_config: VmPayloadConfig = serde_json::from_reader(config_file)?; | |
| Ok(vm_payload_config) | |
| } | |
| fn get_duration(vm_start_timestamp: Option<Instant>) -> Duration { | |
| match vm_start_timestamp { | |
| Some(vm_start_timestamp) => vm_start_timestamp.elapsed(), | |
| None => Duration::default(), | |
| } | |
| } | |
| // TODO(ioffe): this should take into account vm_profiles | |
| fn get_num_vcpus(cpu_options: &I::CpuOptions) -> i32 { | |
| match cpu_options.cpuTopology { | |
| I::CpuTopology::MatchHost(_) => get_num_cpus().map(|v| v as i32).unwrap_or_else(|e| { | |
| warn!("Failed to determine the number of CPUs in the host: {e:#?}"); | |
| INVALID_NUM_CPUS | |
| }), | |
| I::CpuTopology::CpuCount(count) => count, | |
| } | |
| } | |
| /// Write the stats of VMCreation to statsd | |
| pub fn write_vm_creation_stats( | |
| config: &I::VirtualMachineConfig, | |
| is_protected: bool, | |
| ret: &binder::Result<Strong<dyn I::IVirtualMachine>>, | |
| ) { | |
| if cfg!(early) { | |
| info!("Writing VmCreationRequested atom for early VMs is not implemented; skipping"); | |
| return; | |
| } | |
| let creation_succeeded; | |
| let binder_exception_code; | |
| match ret { | |
| Ok(_) => { | |
| creation_succeeded = true; | |
| binder_exception_code = Status::ok().exception_code() as i32; | |
| } | |
| Err(e) => { | |
| creation_succeeded = false; | |
| binder_exception_code = e.exception_code() as i32; | |
| } | |
| } | |
| let (vm_identifier, config_type, num_cpus, memory_mib, apexes) = match config { | |
| I::VirtualMachineConfig::AppConfig(config) => ( | |
| config.name.clone(), | |
| vm_creation_requested::ConfigType::VirtualMachineAppConfig, | |
| get_num_vcpus(&config.cpuOptions), | |
| config.memoryMib, | |
| get_apex_list(config), | |
| ), | |
| I::VirtualMachineConfig::RawConfig(config) => ( | |
| config.name.clone(), | |
| vm_creation_requested::ConfigType::VirtualMachineRawConfig, | |
| get_num_vcpus(&config.cpuOptions), | |
| config.memoryMib, | |
| String::new(), | |
| ), | |
| }; | |
| let uid = get_calling_uid() as i32; | |
| let atom = I::Atom::VmCreationRequested(I::VmCreationRequested { | |
| isProtected: is_protected, | |
| creationSucceeded: creation_succeeded, | |
| binderExceptionCode: binder_exception_code, | |
| configType: config_type as i32, | |
| numCpus: num_cpus, | |
| memoryMib: memory_mib, | |
| apexes, | |
| }); | |
| info!("Writing VmCreationRequested atom into statsd."); | |
| virtualmachine::global_service() | |
| .unwrap() | |
| .forwardAtom(&atom, uid, &vm_identifier) | |
| .unwrap_or_else(|e| { | |
| warn!("Failed to write VmCreationRequested atom: {e}"); | |
| }); | |
| } | |
| /// Write the stats of VM boot to statsd | |
| pub fn write_vm_booted_stats(uid: i32, vm_identifier: &str, vm_start_timestamp: Option<Instant>) { | |
| if cfg!(early) { | |
| info!("Writing VmCreationRequested atom for early VMs is not implemented; skipping"); | |
| return; | |
| } | |
| let vm_identifier = vm_identifier.to_owned(); | |
| let duration = get_duration(vm_start_timestamp); | |
| let atom = I::Atom::VmBooted(I::VmBooted { elapsedTimeMillis: duration.as_millis() as i64 }); | |
| info!("Writing VmBooted atom into statsd."); | |
| virtualmachine::global_service() | |
| .unwrap() | |
| .forwardAtom(&atom, uid, &vm_identifier) | |
| .unwrap_or_else(|e| { | |
| warn!("Failed to write VmBooted atom: {e}"); | |
| }); | |
| } | |
| /// Write the stats of VM exit to statsd | |
| pub fn write_vm_exited_stats_sync( | |
| uid: i32, | |
| vm_identifier: &str, | |
| reason: I::DeathReason, | |
| exit_signal: Option<i32>, | |
| vm_metric: &VmMetric, | |
| ) { | |
| if cfg!(early) { | |
| info!("Writing VmExited atom for early VMs is not implemented; skipping"); | |
| return; | |
| } | |
| let elapsed_time_millis = get_duration(vm_metric.start_timestamp).as_millis() as i64; | |
| let guest_time_millis = vm_metric.cpu_guest_time.unwrap_or_default(); | |
| let rss_kb = vm_metric.rss.unwrap_or_default(); | |
| let atom = I::Atom::VmExited(I::VmExited { | |
| elapsedTimeMillis: elapsed_time_millis, | |
| deathReason: reason, | |
| guestTimeMillis: guest_time_millis, | |
| rssKb: rss_kb, | |
| exitSignal: exit_signal.unwrap_or_default(), | |
| }); | |
| info!("Writing VmExited atom into statsd."); | |
| virtualmachine::global_service() | |
| .unwrap() | |
| .forwardAtom(&atom, uid, vm_identifier) | |
| .unwrap_or_else(|e| { | |
| warn!("Failed to write VmExited atom: {e}"); | |
| }); | |
| } |