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