Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 17 | //! Manages running instances of the CompOS VM. At most one instance should be running at |
| 18 | //! a time, started on demand. |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 19 | |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 20 | use crate::instance_starter::{CompOsInstance, InstanceStarter}; |
| 21 | use android_system_virtualizationservice::aidl::android::system::virtualizationservice; |
Alan Stokes | 8d35160 | 2023-03-23 16:44:58 +0000 | [diff] [blame] | 22 | use anyhow::{anyhow, bail, Context, Result}; |
Alan Stokes | 0e82b50 | 2022-08-08 14:44:48 +0100 | [diff] [blame] | 23 | use binder::Strong; |
David Brazdil | 7d1e5ec | 2023-02-06 17:56:29 +0000 | [diff] [blame] | 24 | use compos_common::compos_client::{VmCpuTopology, VmParameters}; |
David Brazdil | 025c7cc | 2023-02-06 16:14:23 +0000 | [diff] [blame] | 25 | use compos_common::{CURRENT_INSTANCE_DIR, TEST_INSTANCE_DIR}; |
Alan Stokes | 8d35160 | 2023-03-23 16:44:58 +0000 | [diff] [blame] | 26 | use log::info; |
| 27 | use rustutils::system_properties; |
| 28 | use std::str::FromStr; |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 29 | use std::sync::{Arc, Mutex, Weak}; |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 30 | use virtualizationservice::IVirtualizationService::IVirtualizationService; |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 31 | |
Alan Stokes | 69c610f | 2021-09-27 14:03:31 +0100 | [diff] [blame] | 32 | pub struct InstanceManager { |
| 33 | service: Strong<dyn IVirtualizationService>, |
| 34 | state: Mutex<State>, |
| 35 | } |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 36 | |
| 37 | impl InstanceManager { |
Alan Stokes | 69c610f | 2021-09-27 14:03:31 +0100 | [diff] [blame] | 38 | pub fn new(service: Strong<dyn IVirtualizationService>) -> Self { |
| 39 | Self { service, state: Default::default() } |
| 40 | } |
| 41 | |
Alan Stokes | 9386360 | 2022-08-03 17:23:25 +0100 | [diff] [blame] | 42 | pub fn start_current_instance(&self) -> Result<CompOsInstance> { |
Victor Hsieh | da8ca3b | 2022-01-26 12:36:54 -0800 | [diff] [blame] | 43 | let mut vm_parameters = new_vm_parameters()?; |
Victor Hsieh | a61ec2e | 2022-09-21 16:25:27 -0700 | [diff] [blame] | 44 | vm_parameters.prefer_staged = true; |
Alan Stokes | 6542fdd | 2022-02-17 15:21:46 +0000 | [diff] [blame] | 45 | self.start_instance(CURRENT_INSTANCE_DIR, vm_parameters) |
Alan Stokes | 388b88a | 2021-10-13 16:03:17 +0100 | [diff] [blame] | 46 | } |
| 47 | |
Alan Stokes | 9386360 | 2022-08-03 17:23:25 +0100 | [diff] [blame] | 48 | pub fn start_test_instance(&self, prefer_staged: bool) -> Result<CompOsInstance> { |
Victor Hsieh | da8ca3b | 2022-01-26 12:36:54 -0800 | [diff] [blame] | 49 | let mut vm_parameters = new_vm_parameters()?; |
| 50 | vm_parameters.debug_mode = true; |
Victor Hsieh | a61ec2e | 2022-09-21 16:25:27 -0700 | [diff] [blame] | 51 | vm_parameters.prefer_staged = prefer_staged; |
Alan Stokes | d21764c | 2021-10-25 15:33:40 +0100 | [diff] [blame] | 52 | self.start_instance(TEST_INSTANCE_DIR, vm_parameters) |
Alan Stokes | 388b88a | 2021-10-13 16:03:17 +0100 | [diff] [blame] | 53 | } |
| 54 | |
Alan Stokes | d21764c | 2021-10-25 15:33:40 +0100 | [diff] [blame] | 55 | fn start_instance( |
| 56 | &self, |
| 57 | instance_name: &str, |
| 58 | vm_parameters: VmParameters, |
Alan Stokes | 9386360 | 2022-08-03 17:23:25 +0100 | [diff] [blame] | 59 | ) -> Result<CompOsInstance> { |
Alan Stokes | 69c610f | 2021-09-27 14:03:31 +0100 | [diff] [blame] | 60 | let mut state = self.state.lock().unwrap(); |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 61 | state.mark_starting()?; |
| 62 | // Don't hold the lock while we start the instance to avoid blocking other callers. |
| 63 | drop(state); |
| 64 | |
Alan Stokes | d21764c | 2021-10-25 15:33:40 +0100 | [diff] [blame] | 65 | let instance_starter = InstanceStarter::new(instance_name, vm_parameters); |
Alan Stokes | 9386360 | 2022-08-03 17:23:25 +0100 | [diff] [blame] | 66 | let instance = instance_starter.start_new_instance(&*self.service); |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 67 | |
Alan Stokes | 69c610f | 2021-09-27 14:03:31 +0100 | [diff] [blame] | 68 | let mut state = self.state.lock().unwrap(); |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 69 | if let Ok(ref instance) = instance { |
Alan Stokes | 9386360 | 2022-08-03 17:23:25 +0100 | [diff] [blame] | 70 | state.mark_started(instance.get_instance_tracker())?; |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 71 | } else { |
| 72 | state.mark_stopped(); |
| 73 | } |
| 74 | instance |
| 75 | } |
Alan Stokes | 69c610f | 2021-09-27 14:03:31 +0100 | [diff] [blame] | 76 | } |
| 77 | |
Victor Hsieh | da8ca3b | 2022-01-26 12:36:54 -0800 | [diff] [blame] | 78 | fn new_vm_parameters() -> Result<VmParameters> { |
David Brazdil | 025c7cc | 2023-02-06 16:14:23 +0000 | [diff] [blame] | 79 | // By default, dex2oat starts as many threads as there are CPUs. This can be overridden with |
| 80 | // a system property. Start the VM with all CPUs and assume the guest will start a suitable |
| 81 | // number of dex2oat threads. |
David Brazdil | 7d1e5ec | 2023-02-06 17:56:29 +0000 | [diff] [blame] | 82 | let cpu_topology = VmCpuTopology::MatchHost; |
Victor Hsieh | 63a18ce | 2022-05-23 11:10:24 -0700 | [diff] [blame] | 83 | let task_profiles = vec!["SCHED_SP_COMPUTE".to_string()]; |
Alan Stokes | 8d35160 | 2023-03-23 16:44:58 +0000 | [diff] [blame] | 84 | let memory_mib = Some(compos_memory_mib()?); |
| 85 | Ok(VmParameters { cpu_topology, task_profiles, memory_mib, ..Default::default() }) |
| 86 | } |
| 87 | |
| 88 | fn compos_memory_mib() -> Result<i32> { |
| 89 | // Enough memory to complete odrefresh in the VM, for older versions of ART that don't set the |
| 90 | // property explicitly. |
| 91 | const DEFAULT_MEMORY_MIB: u32 = 400; |
| 92 | |
| 93 | let art_requested_mib = |
| 94 | read_property("composd.vm.art.memory_mib.config")?.unwrap_or(DEFAULT_MEMORY_MIB); |
| 95 | |
| 96 | let vm_adjustment_mib = read_property("composd.vm.vendor.memory_mib.config")?.unwrap_or(0); |
| 97 | |
| 98 | info!( |
| 99 | "Compilation VM memory: ART requests {art_requested_mib} MiB, \ |
| 100 | VM adjust is {vm_adjustment_mib}" |
| 101 | ); |
| 102 | art_requested_mib |
| 103 | .checked_add_signed(vm_adjustment_mib) |
| 104 | .and_then(|x| x.try_into().ok()) |
| 105 | .context("Invalid vm memory adjustment") |
| 106 | } |
| 107 | |
| 108 | fn read_property<T: FromStr>(name: &str) -> Result<Option<T>> { |
| 109 | let str = system_properties::read(name).context("Failed to read {name}")?; |
| 110 | str.map(|s| s.parse().map_err(|_| anyhow!("Invalid {name}: {s}"))).transpose() |
Victor Hsieh | da8ca3b | 2022-01-26 12:36:54 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 113 | // Ensures we only run one instance at a time. |
| 114 | // Valid states: |
Alan Stokes | 9386360 | 2022-08-03 17:23:25 +0100 | [diff] [blame] | 115 | // Starting: is_starting is true, instance_tracker is None. |
| 116 | // Started: is_starting is false, instance_tracker is Some(x) and there is a strong ref to x. |
| 117 | // Stopped: is_starting is false and instance_tracker is None or a weak ref to a dropped instance. |
Alan Stokes | 69c610f | 2021-09-27 14:03:31 +0100 | [diff] [blame] | 118 | // The panic calls here should never happen, unless the code above in InstanceManager is buggy. |
| 119 | // In particular nothing the client does should be able to trigger them. |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 120 | #[derive(Default)] |
| 121 | struct State { |
Alan Stokes | 9386360 | 2022-08-03 17:23:25 +0100 | [diff] [blame] | 122 | instance_tracker: Option<Weak<()>>, |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 123 | is_starting: bool, |
| 124 | } |
| 125 | |
| 126 | impl State { |
| 127 | // Move to Starting iff we are Stopped. |
| 128 | fn mark_starting(&mut self) -> Result<()> { |
| 129 | if self.is_starting { |
| 130 | bail!("An instance is already starting"); |
| 131 | } |
Alan Stokes | 9386360 | 2022-08-03 17:23:25 +0100 | [diff] [blame] | 132 | if let Some(weak) = &self.instance_tracker { |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 133 | if weak.strong_count() != 0 { |
| 134 | bail!("An instance is already running"); |
| 135 | } |
| 136 | } |
Alan Stokes | 9386360 | 2022-08-03 17:23:25 +0100 | [diff] [blame] | 137 | self.instance_tracker = None; |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 138 | self.is_starting = true; |
| 139 | Ok(()) |
| 140 | } |
| 141 | |
| 142 | // Move from Starting to Stopped. |
| 143 | fn mark_stopped(&mut self) { |
Alan Stokes | 9386360 | 2022-08-03 17:23:25 +0100 | [diff] [blame] | 144 | if !self.is_starting || self.instance_tracker.is_some() { |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 145 | panic!("Tried to mark stopped when not starting"); |
| 146 | } |
| 147 | self.is_starting = false; |
| 148 | } |
| 149 | |
| 150 | // Move from Starting to Started. |
Alan Stokes | 9386360 | 2022-08-03 17:23:25 +0100 | [diff] [blame] | 151 | fn mark_started(&mut self, instance_tracker: &Arc<()>) -> Result<()> { |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 152 | if !self.is_starting { |
| 153 | panic!("Tried to mark started when not starting") |
| 154 | } |
Alan Stokes | 9386360 | 2022-08-03 17:23:25 +0100 | [diff] [blame] | 155 | if self.instance_tracker.is_some() { |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 156 | panic!("Attempted to mark started when already started"); |
| 157 | } |
| 158 | self.is_starting = false; |
Alan Stokes | 9386360 | 2022-08-03 17:23:25 +0100 | [diff] [blame] | 159 | self.instance_tracker = Some(Arc::downgrade(instance_tracker)); |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 160 | Ok(()) |
| 161 | } |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 162 | } |