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; |
Victor Hsieh | 616f822 | 2022-01-14 13:06:32 -0800 | [diff] [blame] | 22 | use anyhow::{bail, Result}; |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 23 | use compos_aidl_interface::binder::Strong; |
Alan Stokes | d21764c | 2021-10-25 15:33:40 +0100 | [diff] [blame] | 24 | use compos_common::compos_client::VmParameters; |
Jiyong Park | 165921b | 2022-01-14 00:49:33 +0900 | [diff] [blame] | 25 | use compos_common::{ |
Alan Stokes | 6542fdd | 2022-02-17 15:21:46 +0000 | [diff] [blame] | 26 | CURRENT_INSTANCE_DIR, DEX2OAT_CPU_SET_PROP_NAME, DEX2OAT_THREADS_PROP_NAME, |
Jiyong Park | 165921b | 2022-01-14 00:49:33 +0900 | [diff] [blame] | 27 | PREFER_STAGED_VM_CONFIG_PATH, TEST_INSTANCE_DIR, |
| 28 | }; |
| 29 | use rustutils::system_properties; |
| 30 | use std::num::NonZeroU32; |
| 31 | use std::str::FromStr; |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 32 | use std::sync::{Arc, Mutex, Weak}; |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 33 | use virtualizationservice::IVirtualizationService::IVirtualizationService; |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 34 | |
Alan Stokes | 378f738 | 2022-03-09 15:12:11 +0000 | [diff] [blame] | 35 | // Enough memory to complete odrefresh in the VM. |
| 36 | const VM_MEMORY_MIB: i32 = 1024; |
| 37 | |
Alan Stokes | 69c610f | 2021-09-27 14:03:31 +0100 | [diff] [blame] | 38 | pub struct InstanceManager { |
| 39 | service: Strong<dyn IVirtualizationService>, |
| 40 | state: Mutex<State>, |
| 41 | } |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 42 | |
| 43 | impl InstanceManager { |
Alan Stokes | 69c610f | 2021-09-27 14:03:31 +0100 | [diff] [blame] | 44 | pub fn new(service: Strong<dyn IVirtualizationService>) -> Self { |
| 45 | Self { service, state: Default::default() } |
| 46 | } |
| 47 | |
Alan Stokes | 6542fdd | 2022-02-17 15:21:46 +0000 | [diff] [blame] | 48 | pub fn start_current_instance(&self) -> Result<Arc<CompOsInstance>> { |
Victor Hsieh | da8ca3b | 2022-01-26 12:36:54 -0800 | [diff] [blame] | 49 | let mut vm_parameters = new_vm_parameters()?; |
| 50 | vm_parameters.config_path = Some(PREFER_STAGED_VM_CONFIG_PATH.to_owned()); |
Alan Stokes | 6542fdd | 2022-02-17 15:21:46 +0000 | [diff] [blame] | 51 | self.start_instance(CURRENT_INSTANCE_DIR, vm_parameters) |
Alan Stokes | 388b88a | 2021-10-13 16:03:17 +0100 | [diff] [blame] | 52 | } |
| 53 | |
Victor Hsieh | bac923e | 2022-02-22 21:43:36 +0000 | [diff] [blame] | 54 | pub fn start_test_instance(&self, prefer_staged: bool) -> Result<Arc<CompOsInstance>> { |
Victor Hsieh | da8ca3b | 2022-01-26 12:36:54 -0800 | [diff] [blame] | 55 | let mut vm_parameters = new_vm_parameters()?; |
| 56 | vm_parameters.debug_mode = true; |
Victor Hsieh | bac923e | 2022-02-22 21:43:36 +0000 | [diff] [blame] | 57 | if prefer_staged { |
| 58 | vm_parameters.config_path = Some(PREFER_STAGED_VM_CONFIG_PATH.to_owned()); |
| 59 | } |
Alan Stokes | d21764c | 2021-10-25 15:33:40 +0100 | [diff] [blame] | 60 | self.start_instance(TEST_INSTANCE_DIR, vm_parameters) |
Alan Stokes | 388b88a | 2021-10-13 16:03:17 +0100 | [diff] [blame] | 61 | } |
| 62 | |
Alan Stokes | d21764c | 2021-10-25 15:33:40 +0100 | [diff] [blame] | 63 | fn start_instance( |
| 64 | &self, |
| 65 | instance_name: &str, |
| 66 | vm_parameters: VmParameters, |
| 67 | ) -> Result<Arc<CompOsInstance>> { |
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 | state.mark_starting()?; |
| 70 | // Don't hold the lock while we start the instance to avoid blocking other callers. |
| 71 | drop(state); |
| 72 | |
Alan Stokes | d21764c | 2021-10-25 15:33:40 +0100 | [diff] [blame] | 73 | let instance_starter = InstanceStarter::new(instance_name, vm_parameters); |
| 74 | let instance = self.try_start_instance(instance_starter); |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 75 | |
Alan Stokes | 69c610f | 2021-09-27 14:03:31 +0100 | [diff] [blame] | 76 | let mut state = self.state.lock().unwrap(); |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 77 | if let Ok(ref instance) = instance { |
| 78 | state.mark_started(instance)?; |
| 79 | } else { |
| 80 | state.mark_stopped(); |
| 81 | } |
| 82 | instance |
| 83 | } |
| 84 | |
Alan Stokes | d21764c | 2021-10-25 15:33:40 +0100 | [diff] [blame] | 85 | fn try_start_instance(&self, instance_starter: InstanceStarter) -> Result<Arc<CompOsInstance>> { |
Alan Stokes | 6542fdd | 2022-02-17 15:21:46 +0000 | [diff] [blame] | 86 | let compos_instance = instance_starter.start_new_instance(&*self.service)?; |
Alan Stokes | 69c610f | 2021-09-27 14:03:31 +0100 | [diff] [blame] | 87 | Ok(Arc::new(compos_instance)) |
| 88 | } |
| 89 | } |
| 90 | |
Victor Hsieh | da8ca3b | 2022-01-26 12:36:54 -0800 | [diff] [blame] | 91 | fn new_vm_parameters() -> Result<VmParameters> { |
Andrew Walbran | 014efb5 | 2022-02-03 17:43:11 +0000 | [diff] [blame] | 92 | let cpus = match system_properties::read(DEX2OAT_THREADS_PROP_NAME)? { |
| 93 | Some(s) => Some(NonZeroU32::from_str(&s)?), |
| 94 | None => { |
Victor Hsieh | da8ca3b | 2022-01-26 12:36:54 -0800 | [diff] [blame] | 95 | // dex2oat uses all CPUs by default. To match the behavior, give the VM all CPUs by |
| 96 | // default. |
| 97 | NonZeroU32::new(num_cpus::get() as u32) |
| 98 | } |
| 99 | }; |
Andrew Walbran | 014efb5 | 2022-02-03 17:43:11 +0000 | [diff] [blame] | 100 | let cpu_set = system_properties::read(DEX2OAT_CPU_SET_PROP_NAME)?; |
Victor Hsieh | 63a18ce | 2022-05-23 11:10:24 -0700 | [diff] [blame^] | 101 | let task_profiles = vec!["SCHED_SP_COMPUTE".to_string()]; |
Jiyong Park | dfe16d6 | 2022-04-20 17:32:12 +0900 | [diff] [blame] | 102 | Ok(VmParameters { |
| 103 | cpus, |
| 104 | cpu_set, |
| 105 | task_profiles, |
| 106 | memory_mib: Some(VM_MEMORY_MIB), |
| 107 | ..Default::default() |
| 108 | }) |
Victor Hsieh | da8ca3b | 2022-01-26 12:36:54 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 111 | // Ensures we only run one instance at a time. |
| 112 | // Valid states: |
| 113 | // Starting: is_starting is true, running_instance is None. |
| 114 | // Started: is_starting is false, running_instance is Some(x) and there is a strong ref to x. |
| 115 | // Stopped: is_starting is false and running_instance is None or a weak ref to a dropped instance. |
Alan Stokes | 69c610f | 2021-09-27 14:03:31 +0100 | [diff] [blame] | 116 | // The panic calls here should never happen, unless the code above in InstanceManager is buggy. |
| 117 | // In particular nothing the client does should be able to trigger them. |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 118 | #[derive(Default)] |
| 119 | struct State { |
| 120 | running_instance: Option<Weak<CompOsInstance>>, |
| 121 | is_starting: bool, |
| 122 | } |
| 123 | |
| 124 | impl State { |
| 125 | // Move to Starting iff we are Stopped. |
| 126 | fn mark_starting(&mut self) -> Result<()> { |
| 127 | if self.is_starting { |
| 128 | bail!("An instance is already starting"); |
| 129 | } |
| 130 | if let Some(weak) = &self.running_instance { |
| 131 | if weak.strong_count() != 0 { |
| 132 | bail!("An instance is already running"); |
| 133 | } |
| 134 | } |
| 135 | self.running_instance = None; |
| 136 | self.is_starting = true; |
| 137 | Ok(()) |
| 138 | } |
| 139 | |
| 140 | // Move from Starting to Stopped. |
| 141 | fn mark_stopped(&mut self) { |
| 142 | if !self.is_starting || self.running_instance.is_some() { |
| 143 | panic!("Tried to mark stopped when not starting"); |
| 144 | } |
| 145 | self.is_starting = false; |
| 146 | } |
| 147 | |
| 148 | // Move from Starting to Started. |
| 149 | fn mark_started(&mut self, instance: &Arc<CompOsInstance>) -> Result<()> { |
| 150 | if !self.is_starting { |
| 151 | panic!("Tried to mark started when not starting") |
| 152 | } |
| 153 | if self.running_instance.is_some() { |
| 154 | panic!("Attempted to mark started when already started"); |
| 155 | } |
| 156 | self.is_starting = false; |
| 157 | self.running_instance = Some(Arc::downgrade(instance)); |
| 158 | Ok(()) |
| 159 | } |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 160 | } |