Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +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 | |
| 17 | //! Simple command-line tool to drive composd for testing and debugging. |
| 18 | |
| 19 | use android_system_composd::{ |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 20 | aidl::android::system::composd::{ |
Alan Stokes | 6fc1837 | 2021-11-25 17:50:27 +0000 | [diff] [blame] | 21 | ICompilationTask::ICompilationTask, |
Alan Stokes | 81c96f3 | 2022-04-07 14:13:19 +0100 | [diff] [blame] | 22 | ICompilationTaskCallback::{ |
| 23 | BnCompilationTaskCallback, FailureReason::FailureReason, ICompilationTaskCallback, |
| 24 | }, |
Victor Hsieh | bac923e | 2022-02-22 21:43:36 +0000 | [diff] [blame] | 25 | IIsolatedCompilationService::ApexSource::ApexSource, |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 26 | IIsolatedCompilationService::IIsolatedCompilationService, |
| 27 | }, |
Alan Stokes | dcb9602 | 2021-10-26 15:23:31 +0100 | [diff] [blame] | 28 | binder::{ |
| 29 | wait_for_interface, BinderFeatures, DeathRecipient, IBinder, Interface, ProcessState, |
Alan Stokes | 6fc1837 | 2021-11-25 17:50:27 +0000 | [diff] [blame] | 30 | Result as BinderResult, Strong, |
Alan Stokes | dcb9602 | 2021-10-26 15:23:31 +0100 | [diff] [blame] | 31 | }, |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 32 | }; |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 33 | use anyhow::{bail, Context, Result}; |
Victor Hsieh | 1732c0d | 2022-09-12 14:36:03 -0700 | [diff] [blame] | 34 | use clap::Parser; |
Alan Stokes | 7140377 | 2022-06-21 14:56:28 +0100 | [diff] [blame] | 35 | use compos_common::timeouts::TIMEOUTS; |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 36 | use std::sync::{Arc, Condvar, Mutex}; |
| 37 | use std::time::Duration; |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 38 | |
Victor Hsieh | 1732c0d | 2022-09-12 14:36:03 -0700 | [diff] [blame] | 39 | #[derive(Parser)] |
| 40 | enum Actions { |
| 41 | /// Compile classpath for real. Output can be used after a reboot. |
Inseob Kim | 74a4b05 | 2025-01-24 16:50:57 +0900 | [diff] [blame] | 42 | StagedApexCompile { |
| 43 | /// OS for the VM. |
| 44 | #[clap(long, default_value = "microdroid")] |
| 45 | os: String, |
| 46 | }, |
Victor Hsieh | 1732c0d | 2022-09-12 14:36:03 -0700 | [diff] [blame] | 47 | |
| 48 | /// Compile classpath in a debugging VM. Output is ignored. |
| 49 | TestCompile { |
| 50 | /// If any APEX is staged, prefer the staged version. |
| 51 | #[clap(long)] |
| 52 | prefer_staged: bool, |
Inseob Kim | 4657d0e | 2024-11-28 13:34:10 +0900 | [diff] [blame] | 53 | |
| 54 | /// OS for the VM. |
| 55 | #[clap(long, default_value = "microdroid")] |
| 56 | os: String, |
Victor Hsieh | 1732c0d | 2022-09-12 14:36:03 -0700 | [diff] [blame] | 57 | }, |
| 58 | } |
| 59 | |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 60 | fn main() -> Result<()> { |
Victor Hsieh | 1732c0d | 2022-09-12 14:36:03 -0700 | [diff] [blame] | 61 | let action = Actions::parse(); |
Alan Stokes | 388b88a | 2021-10-13 16:03:17 +0100 | [diff] [blame] | 62 | |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 63 | ProcessState::start_thread_pool(); |
| 64 | |
Victor Hsieh | 1732c0d | 2022-09-12 14:36:03 -0700 | [diff] [blame] | 65 | match action { |
Inseob Kim | 74a4b05 | 2025-01-24 16:50:57 +0900 | [diff] [blame] | 66 | Actions::StagedApexCompile { os } => run_staged_apex_compile(&os)?, |
Inseob Kim | 4657d0e | 2024-11-28 13:34:10 +0900 | [diff] [blame] | 67 | Actions::TestCompile { prefer_staged, os } => run_test_compile(prefer_staged, &os)?, |
Alan Stokes | 388b88a | 2021-10-13 16:03:17 +0100 | [diff] [blame] | 68 | } |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 69 | |
| 70 | println!("All Ok!"); |
| 71 | |
| 72 | Ok(()) |
| 73 | } |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 74 | |
| 75 | struct Callback(Arc<State>); |
| 76 | |
| 77 | #[derive(Default)] |
| 78 | struct State { |
| 79 | mutex: Mutex<Option<Outcome>>, |
| 80 | completed: Condvar, |
| 81 | } |
| 82 | |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 83 | enum Outcome { |
| 84 | Succeeded, |
Alan Stokes | 81c96f3 | 2022-04-07 14:13:19 +0100 | [diff] [blame] | 85 | Failed(FailureReason, String), |
| 86 | TaskDied, |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | impl Interface for Callback {} |
| 90 | |
| 91 | impl ICompilationTaskCallback for Callback { |
| 92 | fn onSuccess(&self) -> BinderResult<()> { |
| 93 | self.0.set_outcome(Outcome::Succeeded); |
| 94 | Ok(()) |
| 95 | } |
| 96 | |
Alan Stokes | 81c96f3 | 2022-04-07 14:13:19 +0100 | [diff] [blame] | 97 | fn onFailure(&self, reason: FailureReason, message: &str) -> BinderResult<()> { |
| 98 | self.0.set_outcome(Outcome::Failed(reason, message.to_owned())); |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 99 | Ok(()) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | impl State { |
| 104 | fn set_outcome(&self, outcome: Outcome) { |
| 105 | let mut guard = self.mutex.lock().unwrap(); |
| 106 | *guard = Some(outcome); |
| 107 | drop(guard); |
| 108 | self.completed.notify_all(); |
| 109 | } |
| 110 | |
| 111 | fn wait(&self, duration: Duration) -> Result<Outcome> { |
Alan Stokes | 81c96f3 | 2022-04-07 14:13:19 +0100 | [diff] [blame] | 112 | let (mut outcome, result) = self |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 113 | .completed |
| 114 | .wait_timeout_while(self.mutex.lock().unwrap(), duration, |outcome| outcome.is_none()) |
| 115 | .unwrap(); |
| 116 | if result.timed_out() { |
| 117 | bail!("Timed out waiting for compilation") |
| 118 | } |
Alan Stokes | 81c96f3 | 2022-04-07 14:13:19 +0100 | [diff] [blame] | 119 | Ok(outcome.take().unwrap()) |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
Inseob Kim | 74a4b05 | 2025-01-24 16:50:57 +0900 | [diff] [blame] | 123 | fn run_staged_apex_compile(os: &str) -> Result<()> { |
| 124 | run_async_compilation(|service, callback| service.startStagedApexCompile(callback, os)) |
Alan Stokes | 6fc1837 | 2021-11-25 17:50:27 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Inseob Kim | 4657d0e | 2024-11-28 13:34:10 +0900 | [diff] [blame] | 127 | fn run_test_compile(prefer_staged: bool, os: &str) -> Result<()> { |
Victor Hsieh | bac923e | 2022-02-22 21:43:36 +0000 | [diff] [blame] | 128 | let apex_source = if prefer_staged { ApexSource::PreferStaged } else { ApexSource::NoStaged }; |
Inseob Kim | 4657d0e | 2024-11-28 13:34:10 +0900 | [diff] [blame] | 129 | run_async_compilation(|service, callback| service.startTestCompile(apex_source, callback, os)) |
Alan Stokes | 6fc1837 | 2021-11-25 17:50:27 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | fn run_async_compilation<F>(start_compile_fn: F) -> Result<()> |
| 133 | where |
| 134 | F: FnOnce( |
| 135 | &dyn IIsolatedCompilationService, |
| 136 | &Strong<dyn ICompilationTaskCallback>, |
| 137 | ) -> BinderResult<Strong<dyn ICompilationTask>>, |
| 138 | { |
Alan Stokes | c4d5def | 2023-02-14 17:01:59 +0000 | [diff] [blame] | 139 | if !hypervisor_props::is_any_vm_supported()? { |
| 140 | // Give up now, before trying to start composd, or we may end up waiting forever |
| 141 | // as it repeatedly starts and then aborts (b/254599807). |
| 142 | bail!("Device doesn't support protected or non-protected VMs") |
| 143 | } |
| 144 | |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 145 | let service = wait_for_interface::<dyn IIsolatedCompilationService>("android.system.composd") |
| 146 | .context("Failed to connect to composd service")?; |
| 147 | |
| 148 | let state = Arc::new(State::default()); |
| 149 | let callback = Callback(state.clone()); |
| 150 | let callback = BnCompilationTaskCallback::new_binder(callback, BinderFeatures::default()); |
Alan Stokes | 6fc1837 | 2021-11-25 17:50:27 +0000 | [diff] [blame] | 151 | let task = start_compile_fn(&*service, &callback).context("Compilation failed")?; |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 152 | |
| 153 | // Make sure composd keeps going even if we don't hold a reference to its service. |
| 154 | drop(service); |
| 155 | |
Alan Stokes | dcb9602 | 2021-10-26 15:23:31 +0100 | [diff] [blame] | 156 | let state_clone = state.clone(); |
| 157 | let mut death_recipient = DeathRecipient::new(move || { |
| 158 | eprintln!("CompilationTask died"); |
Alan Stokes | 81c96f3 | 2022-04-07 14:13:19 +0100 | [diff] [blame] | 159 | state_clone.set_outcome(Outcome::TaskDied); |
Alan Stokes | dcb9602 | 2021-10-26 15:23:31 +0100 | [diff] [blame] | 160 | }); |
| 161 | // Note that dropping death_recipient cancels this, so we can't use a temporary here. |
| 162 | task.as_binder().link_to_death(&mut death_recipient)?; |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 163 | |
| 164 | println!("Waiting"); |
| 165 | |
Alan Stokes | 7140377 | 2022-06-21 14:56:28 +0100 | [diff] [blame] | 166 | match state.wait(TIMEOUTS.odrefresh_max_execution_time) { |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 167 | Ok(Outcome::Succeeded) => Ok(()), |
Alan Stokes | 81c96f3 | 2022-04-07 14:13:19 +0100 | [diff] [blame] | 168 | Ok(Outcome::TaskDied) => bail!("Compilation task died"), |
| 169 | Ok(Outcome::Failed(reason, message)) => { |
| 170 | bail!("Compilation failed: {:?}: {}", reason, message) |
| 171 | } |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 172 | Err(e) => { |
| 173 | if let Err(e) = task.cancel() { |
| 174 | eprintln!("Failed to cancel compilation: {:?}", e); |
| 175 | } |
| 176 | Err(e) |
| 177 | } |
| 178 | } |
| 179 | } |
Andrew Walbran | da8786d | 2022-12-01 14:54:27 +0000 | [diff] [blame] | 180 | |
| 181 | #[cfg(test)] |
| 182 | mod tests { |
| 183 | use super::*; |
| 184 | use clap::CommandFactory; |
| 185 | |
| 186 | #[test] |
| 187 | fn verify_actions() { |
| 188 | // Check that the command parsing has been configured in a valid way. |
| 189 | Actions::command().debug_assert(); |
| 190 | } |
| 191 | } |