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 | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 22 | use anyhow::{bail, Context, Result}; |
| 23 | use compos_aidl_interface::aidl::com::android::compos::ICompOsService::ICompOsService; |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 24 | use compos_aidl_interface::binder::Strong; |
Alan Stokes | d21764c | 2021-10-25 15:33:40 +0100 | [diff] [blame] | 25 | use compos_common::compos_client::VmParameters; |
Jiyong Park | 165921b | 2022-01-14 00:49:33 +0900 | [diff] [blame^] | 26 | use compos_common::{ |
| 27 | DEX2OAT_CPU_SET_PROP_NAME, DEX2OAT_THREADS_PROP_NAME, PENDING_INSTANCE_DIR, |
| 28 | PREFER_STAGED_VM_CONFIG_PATH, TEST_INSTANCE_DIR, |
| 29 | }; |
| 30 | use rustutils::system_properties; |
| 31 | use std::num::NonZeroU32; |
| 32 | use std::str::FromStr; |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 33 | use std::sync::{Arc, Mutex, Weak}; |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 34 | use virtualizationservice::IVirtualizationService::IVirtualizationService; |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 35 | |
Alan Stokes | 69c610f | 2021-09-27 14:03:31 +0100 | [diff] [blame] | 36 | pub struct InstanceManager { |
| 37 | service: Strong<dyn IVirtualizationService>, |
| 38 | state: Mutex<State>, |
| 39 | } |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 40 | |
| 41 | impl InstanceManager { |
Alan Stokes | 69c610f | 2021-09-27 14:03:31 +0100 | [diff] [blame] | 42 | pub fn new(service: Strong<dyn IVirtualizationService>) -> Self { |
| 43 | Self { service, state: Default::default() } |
| 44 | } |
| 45 | |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 46 | pub fn get_running_service(&self) -> Result<Strong<dyn ICompOsService>> { |
Alan Stokes | 69c610f | 2021-09-27 14:03:31 +0100 | [diff] [blame] | 47 | let mut state = self.state.lock().unwrap(); |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 48 | let instance = state.get_running_instance().context("No running instance")?; |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 49 | Ok(instance.get_service()) |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 50 | } |
| 51 | |
Alan Stokes | 6fc1837 | 2021-11-25 17:50:27 +0000 | [diff] [blame] | 52 | pub fn start_pending_instance(&self) -> Result<Arc<CompOsInstance>> { |
Alan Stokes | b4a0e91 | 2021-12-01 11:43:59 +0000 | [diff] [blame] | 53 | let config_path = Some(PREFER_STAGED_VM_CONFIG_PATH.to_owned()); |
Jiyong Park | 165921b | 2022-01-14 00:49:33 +0900 | [diff] [blame^] | 54 | let mut vm_parameters = VmParameters { config_path, ..Default::default() }; |
| 55 | vm_parameters.cpus = NonZeroU32::from_str( |
| 56 | &system_properties::read(DEX2OAT_THREADS_PROP_NAME).unwrap_or_default(), |
| 57 | ) |
| 58 | .ok(); |
| 59 | vm_parameters.cpu_set = system_properties::read(DEX2OAT_CPU_SET_PROP_NAME).ok(); |
Alan Stokes | b4a0e91 | 2021-12-01 11:43:59 +0000 | [diff] [blame] | 60 | self.start_instance(PENDING_INSTANCE_DIR, vm_parameters) |
Alan Stokes | 388b88a | 2021-10-13 16:03:17 +0100 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | pub fn start_test_instance(&self) -> Result<Arc<CompOsInstance>> { |
Alan Stokes | b4a0e91 | 2021-12-01 11:43:59 +0000 | [diff] [blame] | 64 | let vm_parameters = VmParameters { debug_mode: true, ..Default::default() }; |
Alan Stokes | d21764c | 2021-10-25 15:33:40 +0100 | [diff] [blame] | 65 | self.start_instance(TEST_INSTANCE_DIR, vm_parameters) |
Alan Stokes | 388b88a | 2021-10-13 16:03:17 +0100 | [diff] [blame] | 66 | } |
| 67 | |
Alan Stokes | d21764c | 2021-10-25 15:33:40 +0100 | [diff] [blame] | 68 | fn start_instance( |
| 69 | &self, |
| 70 | instance_name: &str, |
| 71 | vm_parameters: VmParameters, |
| 72 | ) -> Result<Arc<CompOsInstance>> { |
Alan Stokes | 69c610f | 2021-09-27 14:03:31 +0100 | [diff] [blame] | 73 | let mut state = self.state.lock().unwrap(); |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 74 | state.mark_starting()?; |
| 75 | // Don't hold the lock while we start the instance to avoid blocking other callers. |
| 76 | drop(state); |
| 77 | |
Alan Stokes | d21764c | 2021-10-25 15:33:40 +0100 | [diff] [blame] | 78 | let instance_starter = InstanceStarter::new(instance_name, vm_parameters); |
| 79 | let instance = self.try_start_instance(instance_starter); |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 80 | |
Alan Stokes | 69c610f | 2021-09-27 14:03:31 +0100 | [diff] [blame] | 81 | let mut state = self.state.lock().unwrap(); |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 82 | if let Ok(ref instance) = instance { |
| 83 | state.mark_started(instance)?; |
| 84 | } else { |
| 85 | state.mark_stopped(); |
| 86 | } |
| 87 | instance |
| 88 | } |
| 89 | |
Alan Stokes | d21764c | 2021-10-25 15:33:40 +0100 | [diff] [blame] | 90 | fn try_start_instance(&self, instance_starter: InstanceStarter) -> Result<Arc<CompOsInstance>> { |
Alan Stokes | 6b2d0a8 | 2021-09-29 11:30:39 +0100 | [diff] [blame] | 91 | let compos_instance = instance_starter.create_or_start_instance(&*self.service)?; |
Alan Stokes | 69c610f | 2021-09-27 14:03:31 +0100 | [diff] [blame] | 92 | Ok(Arc::new(compos_instance)) |
| 93 | } |
| 94 | } |
| 95 | |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 96 | // Ensures we only run one instance at a time. |
| 97 | // Valid states: |
| 98 | // Starting: is_starting is true, running_instance is None. |
| 99 | // Started: is_starting is false, running_instance is Some(x) and there is a strong ref to x. |
| 100 | // 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] | 101 | // The panic calls here should never happen, unless the code above in InstanceManager is buggy. |
| 102 | // In particular nothing the client does should be able to trigger them. |
Alan Stokes | a2869d2 | 2021-09-22 09:06:41 +0100 | [diff] [blame] | 103 | #[derive(Default)] |
| 104 | struct State { |
| 105 | running_instance: Option<Weak<CompOsInstance>>, |
| 106 | is_starting: bool, |
| 107 | } |
| 108 | |
| 109 | impl State { |
| 110 | // Move to Starting iff we are Stopped. |
| 111 | fn mark_starting(&mut self) -> Result<()> { |
| 112 | if self.is_starting { |
| 113 | bail!("An instance is already starting"); |
| 114 | } |
| 115 | if let Some(weak) = &self.running_instance { |
| 116 | if weak.strong_count() != 0 { |
| 117 | bail!("An instance is already running"); |
| 118 | } |
| 119 | } |
| 120 | self.running_instance = None; |
| 121 | self.is_starting = true; |
| 122 | Ok(()) |
| 123 | } |
| 124 | |
| 125 | // Move from Starting to Stopped. |
| 126 | fn mark_stopped(&mut self) { |
| 127 | if !self.is_starting || self.running_instance.is_some() { |
| 128 | panic!("Tried to mark stopped when not starting"); |
| 129 | } |
| 130 | self.is_starting = false; |
| 131 | } |
| 132 | |
| 133 | // Move from Starting to Started. |
| 134 | fn mark_started(&mut self, instance: &Arc<CompOsInstance>) -> Result<()> { |
| 135 | if !self.is_starting { |
| 136 | panic!("Tried to mark started when not starting") |
| 137 | } |
| 138 | if self.running_instance.is_some() { |
| 139 | panic!("Attempted to mark started when already started"); |
| 140 | } |
| 141 | self.is_starting = false; |
| 142 | self.running_instance = Some(Arc::downgrade(instance)); |
| 143 | Ok(()) |
| 144 | } |
| 145 | |
| 146 | // Return the running instance if we are in the Started state. |
| 147 | fn get_running_instance(&mut self) -> Option<Arc<CompOsInstance>> { |
| 148 | if self.is_starting { |
| 149 | return None; |
| 150 | } |
| 151 | let instance = self.running_instance.as_ref()?.upgrade(); |
| 152 | if instance.is_none() { |
| 153 | // No point keeping an orphaned weak reference |
| 154 | self.running_instance = None; |
| 155 | } |
| 156 | instance |
| 157 | } |
| 158 | } |