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