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; |
| 18 | use android_system_virtualizationservice::aidl::android::system::virtualizationservice::{ |
| 19 | IVirtualMachine::IVirtualMachine, VirtualMachineAppConfig::VirtualMachineAppConfig, |
| 20 | VirtualMachineConfig::VirtualMachineConfig, |
| 21 | }; |
| 22 | use android_system_virtualizationservice::binder::{Status, Strong}; |
| 23 | use anyhow::{anyhow, Result}; |
| 24 | use log::{trace, warn}; |
| 25 | use microdroid_payload_config::VmPayloadConfig; |
Seungjae Yoo | acf559a | 2022-08-12 04:44:51 +0000 | [diff] [blame^] | 26 | use statslog_virtualization_rust::{vm_booted, vm_creation_requested}; |
Seungjae Yoo | 0a8c84c | 2022-07-11 08:19:15 +0000 | [diff] [blame] | 27 | use zip::ZipArchive; |
| 28 | |
| 29 | fn get_vm_payload_config(config: &VirtualMachineAppConfig) -> Result<VmPayloadConfig> { |
| 30 | let apk = config.apk.as_ref().ok_or_else(|| anyhow!("APK is none"))?; |
| 31 | let apk_file = clone_file(apk)?; |
| 32 | let mut apk_zip = ZipArchive::new(&apk_file)?; |
| 33 | let config_file = apk_zip.by_name(&config.configPath)?; |
| 34 | let vm_payload_config: VmPayloadConfig = serde_json::from_reader(config_file)?; |
| 35 | Ok(vm_payload_config) |
| 36 | } |
| 37 | |
| 38 | /// Write the stats of VMCreation to statsd |
| 39 | pub fn write_vm_creation_stats( |
| 40 | config: &VirtualMachineConfig, |
| 41 | is_protected: bool, |
| 42 | ret: &binder::Result<Strong<dyn IVirtualMachine>>, |
| 43 | ) { |
| 44 | let creation_succeeded; |
| 45 | let binder_exception_code; |
| 46 | match ret { |
| 47 | Ok(_) => { |
| 48 | creation_succeeded = true; |
| 49 | binder_exception_code = Status::ok().exception_code() as i32; |
| 50 | } |
| 51 | Err(ref e) => { |
| 52 | creation_succeeded = false; |
| 53 | binder_exception_code = e.exception_code() as i32; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | let config_type; |
| 58 | let num_cpus; |
| 59 | let cpu_affinity; |
| 60 | let memory_mib; |
| 61 | let apexes; |
| 62 | match config { |
| 63 | VirtualMachineConfig::AppConfig(config) => { |
| 64 | config_type = vm_creation_requested::ConfigType::VirtualMachineAppConfig; |
| 65 | num_cpus = config.numCpus; |
| 66 | cpu_affinity = config.cpuAffinity.clone().unwrap_or_default(); |
| 67 | memory_mib = config.memoryMib; |
| 68 | |
| 69 | let vm_payload_config = get_vm_payload_config(config); |
| 70 | if let Ok(vm_payload_config) = vm_payload_config { |
| 71 | apexes = vm_payload_config |
| 72 | .apexes |
| 73 | .iter() |
| 74 | .map(|x| x.name.clone()) |
| 75 | .collect::<Vec<String>>() |
| 76 | .join(":"); |
| 77 | } else { |
| 78 | apexes = "INFO: Can't get VmPayloadConfig".into(); |
| 79 | } |
| 80 | } |
| 81 | VirtualMachineConfig::RawConfig(config) => { |
| 82 | config_type = vm_creation_requested::ConfigType::VirtualMachineRawConfig; |
| 83 | num_cpus = config.numCpus; |
| 84 | cpu_affinity = config.cpuAffinity.clone().unwrap_or_default(); |
| 85 | memory_mib = config.memoryMib; |
| 86 | apexes = String::new(); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | let empty_string = String::new(); |
| 91 | let vm_creation_requested = vm_creation_requested::VmCreationRequested { |
| 92 | // TODO(seungjaeyoo) Implement sending proper data about uid & vm_identifier |
| 93 | uid: -1, |
| 94 | vm_identifier: &empty_string, |
| 95 | hypervisor: vm_creation_requested::Hypervisor::Pkvm, |
| 96 | is_protected, |
| 97 | creation_succeeded, |
| 98 | binder_exception_code, |
| 99 | config_type, |
| 100 | num_cpus, |
| 101 | cpu_affinity: &cpu_affinity, |
| 102 | memory_mib, |
| 103 | apexes: &apexes, |
| 104 | // TODO(seungjaeyoo) Fill information about task_profile |
| 105 | // TODO(seungjaeyoo) Fill information about disk_image for raw config |
| 106 | }; |
| 107 | |
| 108 | match vm_creation_requested.stats_write() { |
| 109 | Err(e) => { |
| 110 | warn!("statslog_rust failed with error: {}", e); |
| 111 | } |
| 112 | Ok(_) => trace!("statslog_rust succeeded for virtualization service"), |
| 113 | } |
| 114 | } |
Seungjae Yoo | acf559a | 2022-08-12 04:44:51 +0000 | [diff] [blame^] | 115 | |
| 116 | /// Write the stats of VM boot to statsd |
| 117 | pub fn write_vm_booted_stats() { |
| 118 | let empty_string = String::new(); |
| 119 | let vm_booted = vm_booted::VmBooted { |
| 120 | // TODO(seungjaeyoo) Implement sending proper data about uid & vm_identifier |
| 121 | uid: -1, |
| 122 | vm_identifier: &empty_string, |
| 123 | }; |
| 124 | match vm_booted.stats_write() { |
| 125 | Err(e) => { |
| 126 | warn!("statslog_rust failed with error: {}", e); |
| 127 | } |
| 128 | Ok(_) => trace!("statslog_rust succeeded for virtualization service"), |
| 129 | } |
| 130 | } |