blob: 0a6c3d64e52189c37bd3a1e207a27e0d4e3cc4bc [file] [log] [blame]
Alan Stokesa2869d22021-09-22 09:06:41 +01001/*
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 Stokes6b2d0a82021-09-29 11:30:39 +010017//! Manages running instances of the CompOS VM. At most one instance should be running at
18//! a time, started on demand.
Alan Stokesa2869d22021-09-22 09:06:41 +010019
Alan Stokes6b2d0a82021-09-29 11:30:39 +010020use crate::instance_starter::{CompOsInstance, InstanceStarter};
21use android_system_virtualizationservice::aidl::android::system::virtualizationservice;
Victor Hsieh616f8222022-01-14 13:06:32 -080022use anyhow::{bail, Result};
Alan Stokes0e82b502022-08-08 14:44:48 +010023use binder::Strong;
Alan Stokesd21764c2021-10-25 15:33:40 +010024use compos_common::compos_client::VmParameters;
David Brazdil025c7cc2023-02-06 16:14:23 +000025use compos_common::{CURRENT_INSTANCE_DIR, TEST_INSTANCE_DIR};
Jiyong Park165921b2022-01-14 00:49:33 +090026use std::num::NonZeroU32;
Alan Stokesa2869d22021-09-22 09:06:41 +010027use std::sync::{Arc, Mutex, Weak};
Alan Stokes6b2d0a82021-09-29 11:30:39 +010028use virtualizationservice::IVirtualizationService::IVirtualizationService;
Alan Stokesa2869d22021-09-22 09:06:41 +010029
Alan Stokes378f7382022-03-09 15:12:11 +000030// Enough memory to complete odrefresh in the VM.
Alan Stokes39da7692023-02-03 11:19:36 +000031const VM_MEMORY_MIB: i32 = 1280;
Alan Stokes378f7382022-03-09 15:12:11 +000032
Alan Stokes69c610f2021-09-27 14:03:31 +010033pub struct InstanceManager {
34 service: Strong<dyn IVirtualizationService>,
35 state: Mutex<State>,
36}
Alan Stokesa2869d22021-09-22 09:06:41 +010037
38impl InstanceManager {
Alan Stokes69c610f2021-09-27 14:03:31 +010039 pub fn new(service: Strong<dyn IVirtualizationService>) -> Self {
40 Self { service, state: Default::default() }
41 }
42
Alan Stokes93863602022-08-03 17:23:25 +010043 pub fn start_current_instance(&self) -> Result<CompOsInstance> {
Victor Hsiehda8ca3b2022-01-26 12:36:54 -080044 let mut vm_parameters = new_vm_parameters()?;
Victor Hsieha61ec2e2022-09-21 16:25:27 -070045 vm_parameters.prefer_staged = true;
Alan Stokes6542fdd2022-02-17 15:21:46 +000046 self.start_instance(CURRENT_INSTANCE_DIR, vm_parameters)
Alan Stokes388b88a2021-10-13 16:03:17 +010047 }
48
Alan Stokes93863602022-08-03 17:23:25 +010049 pub fn start_test_instance(&self, prefer_staged: bool) -> Result<CompOsInstance> {
Victor Hsiehda8ca3b2022-01-26 12:36:54 -080050 let mut vm_parameters = new_vm_parameters()?;
51 vm_parameters.debug_mode = true;
Victor Hsieha61ec2e2022-09-21 16:25:27 -070052 vm_parameters.prefer_staged = prefer_staged;
Alan Stokesd21764c2021-10-25 15:33:40 +010053 self.start_instance(TEST_INSTANCE_DIR, vm_parameters)
Alan Stokes388b88a2021-10-13 16:03:17 +010054 }
55
Alan Stokesd21764c2021-10-25 15:33:40 +010056 fn start_instance(
57 &self,
58 instance_name: &str,
59 vm_parameters: VmParameters,
Alan Stokes93863602022-08-03 17:23:25 +010060 ) -> Result<CompOsInstance> {
Alan Stokes69c610f2021-09-27 14:03:31 +010061 let mut state = self.state.lock().unwrap();
Alan Stokesa2869d22021-09-22 09:06:41 +010062 state.mark_starting()?;
63 // Don't hold the lock while we start the instance to avoid blocking other callers.
64 drop(state);
65
Alan Stokesd21764c2021-10-25 15:33:40 +010066 let instance_starter = InstanceStarter::new(instance_name, vm_parameters);
Alan Stokes93863602022-08-03 17:23:25 +010067 let instance = instance_starter.start_new_instance(&*self.service);
Alan Stokesa2869d22021-09-22 09:06:41 +010068
Alan Stokes69c610f2021-09-27 14:03:31 +010069 let mut state = self.state.lock().unwrap();
Alan Stokesa2869d22021-09-22 09:06:41 +010070 if let Ok(ref instance) = instance {
Alan Stokes93863602022-08-03 17:23:25 +010071 state.mark_started(instance.get_instance_tracker())?;
Alan Stokesa2869d22021-09-22 09:06:41 +010072 } else {
73 state.mark_stopped();
74 }
75 instance
76 }
Alan Stokes69c610f2021-09-27 14:03:31 +010077}
78
Victor Hsiehda8ca3b2022-01-26 12:36:54 -080079fn new_vm_parameters() -> Result<VmParameters> {
David Brazdil025c7cc2023-02-06 16:14:23 +000080 // By default, dex2oat starts as many threads as there are CPUs. This can be overridden with
81 // a system property. Start the VM with all CPUs and assume the guest will start a suitable
82 // number of dex2oat threads.
83 let cpus = NonZeroU32::new(num_cpus::get() as u32);
Victor Hsieh63a18ce2022-05-23 11:10:24 -070084 let task_profiles = vec!["SCHED_SP_COMPUTE".to_string()];
Victor Hsiehf8900bd2022-09-09 10:59:58 -070085 Ok(VmParameters { cpus, task_profiles, memory_mib: Some(VM_MEMORY_MIB), ..Default::default() })
Victor Hsiehda8ca3b2022-01-26 12:36:54 -080086}
87
Alan Stokesa2869d22021-09-22 09:06:41 +010088// Ensures we only run one instance at a time.
89// Valid states:
Alan Stokes93863602022-08-03 17:23:25 +010090// Starting: is_starting is true, instance_tracker is None.
91// Started: is_starting is false, instance_tracker is Some(x) and there is a strong ref to x.
92// Stopped: is_starting is false and instance_tracker is None or a weak ref to a dropped instance.
Alan Stokes69c610f2021-09-27 14:03:31 +010093// The panic calls here should never happen, unless the code above in InstanceManager is buggy.
94// In particular nothing the client does should be able to trigger them.
Alan Stokesa2869d22021-09-22 09:06:41 +010095#[derive(Default)]
96struct State {
Alan Stokes93863602022-08-03 17:23:25 +010097 instance_tracker: Option<Weak<()>>,
Alan Stokesa2869d22021-09-22 09:06:41 +010098 is_starting: bool,
99}
100
101impl State {
102 // Move to Starting iff we are Stopped.
103 fn mark_starting(&mut self) -> Result<()> {
104 if self.is_starting {
105 bail!("An instance is already starting");
106 }
Alan Stokes93863602022-08-03 17:23:25 +0100107 if let Some(weak) = &self.instance_tracker {
Alan Stokesa2869d22021-09-22 09:06:41 +0100108 if weak.strong_count() != 0 {
109 bail!("An instance is already running");
110 }
111 }
Alan Stokes93863602022-08-03 17:23:25 +0100112 self.instance_tracker = None;
Alan Stokesa2869d22021-09-22 09:06:41 +0100113 self.is_starting = true;
114 Ok(())
115 }
116
117 // Move from Starting to Stopped.
118 fn mark_stopped(&mut self) {
Alan Stokes93863602022-08-03 17:23:25 +0100119 if !self.is_starting || self.instance_tracker.is_some() {
Alan Stokesa2869d22021-09-22 09:06:41 +0100120 panic!("Tried to mark stopped when not starting");
121 }
122 self.is_starting = false;
123 }
124
125 // Move from Starting to Started.
Alan Stokes93863602022-08-03 17:23:25 +0100126 fn mark_started(&mut self, instance_tracker: &Arc<()>) -> Result<()> {
Alan Stokesa2869d22021-09-22 09:06:41 +0100127 if !self.is_starting {
128 panic!("Tried to mark started when not starting")
129 }
Alan Stokes93863602022-08-03 17:23:25 +0100130 if self.instance_tracker.is_some() {
Alan Stokesa2869d22021-09-22 09:06:41 +0100131 panic!("Attempted to mark started when already started");
132 }
133 self.is_starting = false;
Alan Stokes93863602022-08-03 17:23:25 +0100134 self.instance_tracker = Some(Arc::downgrade(instance_tracker));
Alan Stokesa2869d22021-09-22 09:06:41 +0100135 Ok(())
136 }
Alan Stokesa2869d22021-09-22 09:06:41 +0100137}