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 | |
David Brazdil | f50c7a6 | 2023-04-19 14:22:42 +0000 | [diff] [blame] | 17 | use crate::aidl::{clone_file, GLOBAL_SERVICE}; |
Seungjae Yoo | 6d265d9 | 2022-11-15 10:51:33 +0900 | [diff] [blame] | 18 | use crate::crosvm::VmMetric; |
David Brazdil | 1f53070 | 2022-10-03 12:18:10 +0100 | [diff] [blame] | 19 | use crate::get_calling_uid; |
David Brazdil | 49f96f5 | 2022-12-16 21:29:13 +0000 | [diff] [blame] | 20 | use android_system_virtualizationcommon::aidl::android::system::virtualizationcommon::DeathReason::DeathReason; |
Seungjae Yoo | 0a8c84c | 2022-07-11 08:19:15 +0000 | [diff] [blame] | 21 | use android_system_virtualizationservice::aidl::android::system::virtualizationservice::{ |
David Brazdil | 7d1e5ec | 2023-02-06 17:56:29 +0000 | [diff] [blame] | 22 | CpuTopology::CpuTopology, |
Alan Stokes | 0d1ef78 | 2022-09-27 13:46:35 +0100 | [diff] [blame] | 23 | IVirtualMachine::IVirtualMachine, |
| 24 | VirtualMachineAppConfig::{Payload::Payload, VirtualMachineAppConfig}, |
| 25 | VirtualMachineConfig::VirtualMachineConfig, |
Seungjae Yoo | 0a8c84c | 2022-07-11 08:19:15 +0000 | [diff] [blame] | 26 | }; |
| 27 | use android_system_virtualizationservice::binder::{Status, Strong}; |
David Brazdil | 49f96f5 | 2022-12-16 21:29:13 +0000 | [diff] [blame] | 28 | use android_system_virtualizationservice_internal::aidl::android::system::virtualizationservice_internal::{ |
| 29 | AtomVmBooted::AtomVmBooted, |
| 30 | AtomVmCreationRequested::AtomVmCreationRequested, |
| 31 | AtomVmExited::AtomVmExited, |
| 32 | }; |
Seungjae Yoo | 0a8c84c | 2022-07-11 08:19:15 +0000 | [diff] [blame] | 33 | use anyhow::{anyhow, Result}; |
David Brazdil | 1f53070 | 2022-10-03 12:18:10 +0100 | [diff] [blame] | 34 | use binder::ParcelFileDescriptor; |
Seungjae Yoo | eebe644 | 2023-04-04 13:02:46 +0900 | [diff] [blame] | 35 | use log::{info, warn}; |
Seungjae Yoo | 0a8c84c | 2022-07-11 08:19:15 +0000 | [diff] [blame] | 36 | use microdroid_payload_config::VmPayloadConfig; |
David Brazdil | afc9a9e | 2023-01-12 16:08:10 +0000 | [diff] [blame] | 37 | use statslog_virtualization_rust::vm_creation_requested; |
Shikha Panwar | c93a42b | 2022-11-09 14:12:25 +0000 | [diff] [blame] | 38 | use std::thread; |
Seungjae Yoo | 2e7beea | 2022-08-24 16:09:12 +0900 | [diff] [blame] | 39 | use std::time::{Duration, SystemTime}; |
Seungjae Yoo | 0a8c84c | 2022-07-11 08:19:15 +0000 | [diff] [blame] | 40 | use zip::ZipArchive; |
| 41 | |
David Brazdil | 7d1e5ec | 2023-02-06 17:56:29 +0000 | [diff] [blame] | 42 | const INVALID_NUM_CPUS: i32 = -1; |
| 43 | |
Alan Stokes | 0d1ef78 | 2022-09-27 13:46:35 +0100 | [diff] [blame] | 44 | fn get_apex_list(config: &VirtualMachineAppConfig) -> String { |
| 45 | match &config.payload { |
| 46 | Payload::PayloadConfig(_) => String::new(), |
| 47 | Payload::ConfigPath(config_path) => { |
| 48 | let vm_payload_config = get_vm_payload_config(&config.apk, config_path); |
| 49 | if let Ok(vm_payload_config) = vm_payload_config { |
| 50 | vm_payload_config |
| 51 | .apexes |
| 52 | .iter() |
| 53 | .map(|x| x.name.clone()) |
| 54 | .collect::<Vec<String>>() |
| 55 | .join(":") |
| 56 | } else { |
| 57 | "INFO: Can't get VmPayloadConfig".to_owned() |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | fn get_vm_payload_config( |
| 64 | apk_fd: &Option<ParcelFileDescriptor>, |
| 65 | config_path: &str, |
| 66 | ) -> Result<VmPayloadConfig> { |
| 67 | 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] | 68 | let apk_file = clone_file(apk)?; |
| 69 | let mut apk_zip = ZipArchive::new(&apk_file)?; |
Alan Stokes | 0d1ef78 | 2022-09-27 13:46:35 +0100 | [diff] [blame] | 70 | let config_file = apk_zip.by_name(config_path)?; |
Seungjae Yoo | 0a8c84c | 2022-07-11 08:19:15 +0000 | [diff] [blame] | 71 | let vm_payload_config: VmPayloadConfig = serde_json::from_reader(config_file)?; |
| 72 | Ok(vm_payload_config) |
| 73 | } |
| 74 | |
Seungjae Yoo | 2e7beea | 2022-08-24 16:09:12 +0900 | [diff] [blame] | 75 | fn get_duration(vm_start_timestamp: Option<SystemTime>) -> Duration { |
| 76 | match vm_start_timestamp { |
| 77 | Some(vm_start_timestamp) => vm_start_timestamp.elapsed().unwrap_or_default(), |
| 78 | None => Duration::default(), |
| 79 | } |
| 80 | } |
| 81 | |
David Brazdil | 7d1e5ec | 2023-02-06 17:56:29 +0000 | [diff] [blame] | 82 | // Returns the number of CPUs configured in the host system. |
| 83 | // This matches how crosvm determines the number of logical cores. |
| 84 | // For telemetry purposes only. |
David Brazdil | 25e8c05 | 2023-02-17 12:53:01 +0000 | [diff] [blame] | 85 | pub(crate) fn get_num_cpus() -> Option<usize> { |
Andrew Walbran | b58d1b4 | 2023-07-07 13:54:49 +0100 | [diff] [blame] | 86 | // SAFETY: Only integer constants passed back and forth. |
David Brazdil | 7d1e5ec | 2023-02-06 17:56:29 +0000 | [diff] [blame] | 87 | let ret = unsafe { libc::sysconf(libc::_SC_NPROCESSORS_CONF) }; |
| 88 | if ret > 0 { |
| 89 | ret.try_into().ok() |
| 90 | } else { |
| 91 | None |
| 92 | } |
| 93 | } |
| 94 | |
Seungjae Yoo | 0a8c84c | 2022-07-11 08:19:15 +0000 | [diff] [blame] | 95 | /// Write the stats of VMCreation to statsd |
Seungjae Yoo | b2db3d2 | 2023-04-06 18:05:43 +0900 | [diff] [blame] | 96 | /// The function creates a separate thread which waits for statsd to start to push atom |
Seungjae Yoo | 0a8c84c | 2022-07-11 08:19:15 +0000 | [diff] [blame] | 97 | pub fn write_vm_creation_stats( |
| 98 | config: &VirtualMachineConfig, |
| 99 | is_protected: bool, |
| 100 | ret: &binder::Result<Strong<dyn IVirtualMachine>>, |
| 101 | ) { |
Inseob Kim | ecde8c0 | 2024-09-03 13:11:08 +0900 | [diff] [blame^] | 102 | if cfg!(early) { |
| 103 | info!("Writing VmCreationRequested atom for early VMs is not implemented; skipping"); |
| 104 | return; |
| 105 | } |
Seungjae Yoo | 0a8c84c | 2022-07-11 08:19:15 +0000 | [diff] [blame] | 106 | let creation_succeeded; |
| 107 | let binder_exception_code; |
| 108 | match ret { |
| 109 | Ok(_) => { |
| 110 | creation_succeeded = true; |
| 111 | binder_exception_code = Status::ok().exception_code() as i32; |
| 112 | } |
| 113 | Err(ref e) => { |
| 114 | creation_succeeded = false; |
| 115 | binder_exception_code = e.exception_code() as i32; |
| 116 | } |
| 117 | } |
David Brazdil | 7d1e5ec | 2023-02-06 17:56:29 +0000 | [diff] [blame] | 118 | let (vm_identifier, config_type, cpu_topology, memory_mib, apexes) = match config { |
Alan Stokes | 0d1ef78 | 2022-09-27 13:46:35 +0100 | [diff] [blame] | 119 | VirtualMachineConfig::AppConfig(config) => ( |
Shikha Panwar | c93a42b | 2022-11-09 14:12:25 +0000 | [diff] [blame] | 120 | config.name.clone(), |
Alan Stokes | 0d1ef78 | 2022-09-27 13:46:35 +0100 | [diff] [blame] | 121 | vm_creation_requested::ConfigType::VirtualMachineAppConfig, |
David Brazdil | 7d1e5ec | 2023-02-06 17:56:29 +0000 | [diff] [blame] | 122 | config.cpuTopology, |
Alan Stokes | 0d1ef78 | 2022-09-27 13:46:35 +0100 | [diff] [blame] | 123 | config.memoryMib, |
| 124 | get_apex_list(config), |
| 125 | ), |
| 126 | VirtualMachineConfig::RawConfig(config) => ( |
Shikha Panwar | c93a42b | 2022-11-09 14:12:25 +0000 | [diff] [blame] | 127 | config.name.clone(), |
Alan Stokes | 0d1ef78 | 2022-09-27 13:46:35 +0100 | [diff] [blame] | 128 | vm_creation_requested::ConfigType::VirtualMachineRawConfig, |
David Brazdil | 7d1e5ec | 2023-02-06 17:56:29 +0000 | [diff] [blame] | 129 | config.cpuTopology, |
Alan Stokes | 0d1ef78 | 2022-09-27 13:46:35 +0100 | [diff] [blame] | 130 | config.memoryMib, |
| 131 | String::new(), |
| 132 | ), |
| 133 | }; |
Seungjae Yoo | 0a8c84c | 2022-07-11 08:19:15 +0000 | [diff] [blame] | 134 | |
David Brazdil | 7d1e5ec | 2023-02-06 17:56:29 +0000 | [diff] [blame] | 135 | let num_cpus: i32 = match cpu_topology { |
| 136 | CpuTopology::MATCH_HOST => { |
| 137 | get_num_cpus().and_then(|v| v.try_into().ok()).unwrap_or_else(|| { |
| 138 | warn!("Failed to determine the number of CPUs in the host"); |
| 139 | INVALID_NUM_CPUS |
| 140 | }) |
| 141 | } |
| 142 | _ => 1, |
| 143 | }; |
| 144 | |
David Brazdil | 49f96f5 | 2022-12-16 21:29:13 +0000 | [diff] [blame] | 145 | let atom = AtomVmCreationRequested { |
David Brazdil | 1f53070 | 2022-10-03 12:18:10 +0100 | [diff] [blame] | 146 | uid: get_calling_uid() as i32, |
David Brazdil | 49f96f5 | 2022-12-16 21:29:13 +0000 | [diff] [blame] | 147 | vmIdentifier: vm_identifier, |
| 148 | isProtected: is_protected, |
| 149 | creationSucceeded: creation_succeeded, |
| 150 | binderExceptionCode: binder_exception_code, |
| 151 | configType: config_type as i32, |
| 152 | numCpus: num_cpus, |
| 153 | memoryMib: memory_mib, |
| 154 | apexes, |
| 155 | }; |
Seungjae Yoo | 0a8c84c | 2022-07-11 08:19:15 +0000 | [diff] [blame] | 156 | |
Seungjae Yoo | eebe644 | 2023-04-04 13:02:46 +0900 | [diff] [blame] | 157 | info!("Writing VmCreationRequested atom into statsd."); |
David Brazdil | 49f96f5 | 2022-12-16 21:29:13 +0000 | [diff] [blame] | 158 | thread::spawn(move || { |
| 159 | GLOBAL_SERVICE.atomVmCreationRequested(&atom).unwrap_or_else(|e| { |
| 160 | warn!("Failed to write VmCreationRequested atom: {e}"); |
| 161 | }); |
Shikha Panwar | c93a42b | 2022-11-09 14:12:25 +0000 | [diff] [blame] | 162 | }); |
Seungjae Yoo | 0a8c84c | 2022-07-11 08:19:15 +0000 | [diff] [blame] | 163 | } |
Seungjae Yoo | acf559a | 2022-08-12 04:44:51 +0000 | [diff] [blame] | 164 | |
| 165 | /// Write the stats of VM boot to statsd |
Seungjae Yoo | b2db3d2 | 2023-04-06 18:05:43 +0900 | [diff] [blame] | 166 | /// The function creates a separate thread which waits for statsd to start to push atom |
Seungjae Yoo | 2e7beea | 2022-08-24 16:09:12 +0900 | [diff] [blame] | 167 | pub fn write_vm_booted_stats( |
| 168 | uid: i32, |
Shikha Panwar | c93a42b | 2022-11-09 14:12:25 +0000 | [diff] [blame] | 169 | vm_identifier: &str, |
Seungjae Yoo | 2e7beea | 2022-08-24 16:09:12 +0900 | [diff] [blame] | 170 | vm_start_timestamp: Option<SystemTime>, |
| 171 | ) { |
Inseob Kim | ecde8c0 | 2024-09-03 13:11:08 +0900 | [diff] [blame^] | 172 | if cfg!(early) { |
| 173 | info!("Writing VmCreationRequested atom for early VMs is not implemented; skipping"); |
| 174 | return; |
| 175 | } |
| 176 | |
Shikha Panwar | c93a42b | 2022-11-09 14:12:25 +0000 | [diff] [blame] | 177 | let vm_identifier = vm_identifier.to_owned(); |
Seungjae Yoo | 2e7beea | 2022-08-24 16:09:12 +0900 | [diff] [blame] | 178 | let duration = get_duration(vm_start_timestamp); |
David Brazdil | 49f96f5 | 2022-12-16 21:29:13 +0000 | [diff] [blame] | 179 | |
| 180 | let atom = AtomVmBooted { |
| 181 | uid, |
| 182 | vmIdentifier: vm_identifier, |
| 183 | elapsedTimeMillis: duration.as_millis() as i64, |
| 184 | }; |
| 185 | |
Seungjae Yoo | eebe644 | 2023-04-04 13:02:46 +0900 | [diff] [blame] | 186 | info!("Writing VmBooted atom into statsd."); |
Shikha Panwar | c93a42b | 2022-11-09 14:12:25 +0000 | [diff] [blame] | 187 | thread::spawn(move || { |
David Brazdil | 49f96f5 | 2022-12-16 21:29:13 +0000 | [diff] [blame] | 188 | GLOBAL_SERVICE.atomVmBooted(&atom).unwrap_or_else(|e| { |
Seungjae Yoo | eebe644 | 2023-04-04 13:02:46 +0900 | [diff] [blame] | 189 | warn!("Failed to write VmBooted atom: {e}"); |
David Brazdil | 49f96f5 | 2022-12-16 21:29:13 +0000 | [diff] [blame] | 190 | }); |
Shikha Panwar | c93a42b | 2022-11-09 14:12:25 +0000 | [diff] [blame] | 191 | }); |
Seungjae Yoo | acf559a | 2022-08-12 04:44:51 +0000 | [diff] [blame] | 192 | } |
Seungjae Yoo | b4c07ba | 2022-08-12 04:44:52 +0000 | [diff] [blame] | 193 | |
| 194 | /// Write the stats of VM exit to statsd |
Seungjae Yoo | b2db3d2 | 2023-04-06 18:05:43 +0900 | [diff] [blame] | 195 | pub fn write_vm_exited_stats_sync( |
Seungjae Yoo | 2e7beea | 2022-08-24 16:09:12 +0900 | [diff] [blame] | 196 | uid: i32, |
Shikha Panwar | c93a42b | 2022-11-09 14:12:25 +0000 | [diff] [blame] | 197 | vm_identifier: &str, |
Seungjae Yoo | 2e7beea | 2022-08-24 16:09:12 +0900 | [diff] [blame] | 198 | reason: DeathReason, |
Seungjae Yoo | 93430e8 | 2022-12-05 16:37:42 +0900 | [diff] [blame] | 199 | exit_signal: Option<i32>, |
Seungjae Yoo | 6d265d9 | 2022-11-15 10:51:33 +0900 | [diff] [blame] | 200 | vm_metric: &VmMetric, |
Seungjae Yoo | 2e7beea | 2022-08-24 16:09:12 +0900 | [diff] [blame] | 201 | ) { |
Inseob Kim | ecde8c0 | 2024-09-03 13:11:08 +0900 | [diff] [blame^] | 202 | if cfg!(early) { |
| 203 | info!("Writing VmExited atom for early VMs is not implemented; skipping"); |
| 204 | return; |
| 205 | } |
Shikha Panwar | c93a42b | 2022-11-09 14:12:25 +0000 | [diff] [blame] | 206 | let vm_identifier = vm_identifier.to_owned(); |
Seungjae Yoo | 6d265d9 | 2022-11-15 10:51:33 +0900 | [diff] [blame] | 207 | let elapsed_time_millis = get_duration(vm_metric.start_timestamp).as_millis() as i64; |
| 208 | let guest_time_millis = vm_metric.cpu_guest_time.unwrap_or_default(); |
| 209 | let rss = vm_metric.rss.unwrap_or_default(); |
| 210 | |
David Brazdil | 49f96f5 | 2022-12-16 21:29:13 +0000 | [diff] [blame] | 211 | let atom = AtomVmExited { |
| 212 | uid, |
| 213 | vmIdentifier: vm_identifier, |
| 214 | elapsedTimeMillis: elapsed_time_millis, |
| 215 | deathReason: reason, |
| 216 | guestTimeMillis: guest_time_millis, |
| 217 | rssVmKb: rss.vm, |
| 218 | rssCrosvmKb: rss.crosvm, |
| 219 | exitSignal: exit_signal.unwrap_or_default(), |
| 220 | }; |
| 221 | |
Seungjae Yoo | eebe644 | 2023-04-04 13:02:46 +0900 | [diff] [blame] | 222 | info!("Writing VmExited atom into statsd."); |
Seungjae Yoo | b2db3d2 | 2023-04-06 18:05:43 +0900 | [diff] [blame] | 223 | GLOBAL_SERVICE.atomVmExited(&atom).unwrap_or_else(|e| { |
| 224 | warn!("Failed to write VmExited atom: {e}"); |
Shikha Panwar | c93a42b | 2022-11-09 14:12:25 +0000 | [diff] [blame] | 225 | }); |
| 226 | } |