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 | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 22 | ICompilationTaskCallback::{BnCompilationTaskCallback, ICompilationTaskCallback}, |
| 23 | IIsolatedCompilationService::IIsolatedCompilationService, |
| 24 | }, |
Alan Stokes | dcb9602 | 2021-10-26 15:23:31 +0100 | [diff] [blame] | 25 | binder::{ |
| 26 | wait_for_interface, BinderFeatures, DeathRecipient, IBinder, Interface, ProcessState, |
Alan Stokes | 6fc1837 | 2021-11-25 17:50:27 +0000 | [diff] [blame] | 27 | Result as BinderResult, Strong, |
Alan Stokes | dcb9602 | 2021-10-26 15:23:31 +0100 | [diff] [blame] | 28 | }, |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 29 | }; |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 30 | use anyhow::{bail, Context, Result}; |
Alan Stokes | 17aed5c | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 31 | use compos_common::timeouts::timeouts; |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 32 | use std::sync::{Arc, Condvar, Mutex}; |
| 33 | use std::time::Duration; |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 34 | |
| 35 | fn main() -> Result<()> { |
Alan Stokes | 388b88a | 2021-10-13 16:03:17 +0100 | [diff] [blame] | 36 | let app = clap::App::new("composd_cmd").arg( |
Alan Stokes | da59693 | 2021-12-15 17:48:55 +0000 | [diff] [blame] | 37 | clap::Arg::with_name("command") |
| 38 | .index(1) |
| 39 | .takes_value(true) |
| 40 | .required(true) |
Alan Stokes | 165e7db | 2022-01-21 16:31:30 +0000 | [diff] [blame] | 41 | .possible_values(&["staged-apex-compile", "test-compile"]), |
Alan Stokes | 388b88a | 2021-10-13 16:03:17 +0100 | [diff] [blame] | 42 | ); |
| 43 | let args = app.get_matches(); |
| 44 | let command = args.value_of("command").unwrap(); |
| 45 | |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 46 | ProcessState::start_thread_pool(); |
| 47 | |
Alan Stokes | 388b88a | 2021-10-13 16:03:17 +0100 | [diff] [blame] | 48 | match command { |
Alan Stokes | 6fc1837 | 2021-11-25 17:50:27 +0000 | [diff] [blame] | 49 | "staged-apex-compile" => run_staged_apex_compile()?, |
Alan Stokes | 768ba9c | 2021-12-20 14:32:12 +0000 | [diff] [blame] | 50 | "test-compile" => run_test_compile()?, |
Alan Stokes | 388b88a | 2021-10-13 16:03:17 +0100 | [diff] [blame] | 51 | _ => panic!("Unexpected command {}", command), |
| 52 | } |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 53 | |
| 54 | println!("All Ok!"); |
| 55 | |
| 56 | Ok(()) |
| 57 | } |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 58 | |
| 59 | struct Callback(Arc<State>); |
| 60 | |
| 61 | #[derive(Default)] |
| 62 | struct State { |
| 63 | mutex: Mutex<Option<Outcome>>, |
| 64 | completed: Condvar, |
| 65 | } |
| 66 | |
| 67 | #[derive(Copy, Clone)] |
| 68 | enum Outcome { |
| 69 | Succeeded, |
| 70 | Failed, |
| 71 | } |
| 72 | |
| 73 | impl Interface for Callback {} |
| 74 | |
| 75 | impl ICompilationTaskCallback for Callback { |
| 76 | fn onSuccess(&self) -> BinderResult<()> { |
| 77 | self.0.set_outcome(Outcome::Succeeded); |
| 78 | Ok(()) |
| 79 | } |
| 80 | |
| 81 | fn onFailure(&self) -> BinderResult<()> { |
| 82 | self.0.set_outcome(Outcome::Failed); |
| 83 | Ok(()) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | impl State { |
| 88 | fn set_outcome(&self, outcome: Outcome) { |
| 89 | let mut guard = self.mutex.lock().unwrap(); |
| 90 | *guard = Some(outcome); |
| 91 | drop(guard); |
| 92 | self.completed.notify_all(); |
| 93 | } |
| 94 | |
| 95 | fn wait(&self, duration: Duration) -> Result<Outcome> { |
| 96 | let (outcome, result) = self |
| 97 | .completed |
| 98 | .wait_timeout_while(self.mutex.lock().unwrap(), duration, |outcome| outcome.is_none()) |
| 99 | .unwrap(); |
| 100 | if result.timed_out() { |
| 101 | bail!("Timed out waiting for compilation") |
| 102 | } |
| 103 | Ok(outcome.unwrap()) |
| 104 | } |
| 105 | } |
| 106 | |
Alan Stokes | 6fc1837 | 2021-11-25 17:50:27 +0000 | [diff] [blame] | 107 | fn run_staged_apex_compile() -> Result<()> { |
| 108 | run_async_compilation(|service, callback| service.startStagedApexCompile(callback)) |
| 109 | } |
| 110 | |
Alan Stokes | 768ba9c | 2021-12-20 14:32:12 +0000 | [diff] [blame] | 111 | fn run_test_compile() -> Result<()> { |
Alan Stokes | 6fc1837 | 2021-11-25 17:50:27 +0000 | [diff] [blame] | 112 | run_async_compilation(|service, callback| service.startTestCompile(callback)) |
| 113 | } |
| 114 | |
| 115 | fn run_async_compilation<F>(start_compile_fn: F) -> Result<()> |
| 116 | where |
| 117 | F: FnOnce( |
| 118 | &dyn IIsolatedCompilationService, |
| 119 | &Strong<dyn ICompilationTaskCallback>, |
| 120 | ) -> BinderResult<Strong<dyn ICompilationTask>>, |
| 121 | { |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 122 | let service = wait_for_interface::<dyn IIsolatedCompilationService>("android.system.composd") |
| 123 | .context("Failed to connect to composd service")?; |
| 124 | |
| 125 | let state = Arc::new(State::default()); |
| 126 | let callback = Callback(state.clone()); |
| 127 | let callback = BnCompilationTaskCallback::new_binder(callback, BinderFeatures::default()); |
Alan Stokes | 6fc1837 | 2021-11-25 17:50:27 +0000 | [diff] [blame] | 128 | let task = start_compile_fn(&*service, &callback).context("Compilation failed")?; |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 129 | |
| 130 | // Make sure composd keeps going even if we don't hold a reference to its service. |
| 131 | drop(service); |
| 132 | |
Alan Stokes | dcb9602 | 2021-10-26 15:23:31 +0100 | [diff] [blame] | 133 | let state_clone = state.clone(); |
| 134 | let mut death_recipient = DeathRecipient::new(move || { |
| 135 | eprintln!("CompilationTask died"); |
| 136 | state_clone.set_outcome(Outcome::Failed); |
| 137 | }); |
| 138 | // Note that dropping death_recipient cancels this, so we can't use a temporary here. |
| 139 | task.as_binder().link_to_death(&mut death_recipient)?; |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 140 | |
| 141 | println!("Waiting"); |
| 142 | |
Alan Stokes | 17aed5c | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 143 | match state.wait(timeouts()?.odrefresh_max_execution_time) { |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 144 | Ok(Outcome::Succeeded) => Ok(()), |
| 145 | Ok(Outcome::Failed) => bail!("Compilation failed"), |
| 146 | Err(e) => { |
| 147 | if let Err(e) = task.cancel() { |
| 148 | eprintln!("Failed to cancel compilation: {:?}", e); |
| 149 | } |
| 150 | Err(e) |
| 151 | } |
| 152 | } |
| 153 | } |