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