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