blob: 0422b448dbe16e412f042a4d474be1c565c2c806 [file] [log] [blame]
Alan Stokesb2cc79e2021-09-14 14:08:46 +01001/*
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
19use android_system_composd::{
Alan Stokes9ca14ca2021-10-20 14:25:57 +010020 aidl::android::system::composd::{
21 ICompilationTaskCallback::{BnCompilationTaskCallback, ICompilationTaskCallback},
22 IIsolatedCompilationService::IIsolatedCompilationService,
23 },
Alan Stokesdcb96022021-10-26 15:23:31 +010024 binder::{
25 wait_for_interface, BinderFeatures, DeathRecipient, IBinder, Interface, ProcessState,
26 Result as BinderResult,
27 },
Alan Stokesb2cc79e2021-09-14 14:08:46 +010028};
Alan Stokes9ca14ca2021-10-20 14:25:57 +010029use anyhow::{bail, Context, Result};
Alan Stokes17aed5c2021-10-20 14:25:57 +010030use compos_common::timeouts::timeouts;
Alan Stokes9ca14ca2021-10-20 14:25:57 +010031use std::sync::{Arc, Condvar, Mutex};
32use std::time::Duration;
Alan Stokesb2cc79e2021-09-14 14:08:46 +010033
34fn main() -> Result<()> {
Alan Stokes388b88a2021-10-13 16:03:17 +010035 let app = clap::App::new("composd_cmd").arg(
36 clap::Arg::with_name("command")
37 .index(1)
38 .takes_value(true)
39 .required(true)
40 .possible_values(&["forced-compile-test"]),
41 );
42 let args = app.get_matches();
43 let command = args.value_of("command").unwrap();
44
Alan Stokesb2cc79e2021-09-14 14:08:46 +010045 ProcessState::start_thread_pool();
46
Alan Stokes388b88a2021-10-13 16:03:17 +010047 match command {
Alan Stokes9ca14ca2021-10-20 14:25:57 +010048 "forced-compile-test" => run_forced_compile_for_test()?,
Alan Stokes388b88a2021-10-13 16:03:17 +010049 _ => panic!("Unexpected command {}", command),
50 }
Alan Stokesb2cc79e2021-09-14 14:08:46 +010051
52 println!("All Ok!");
53
54 Ok(())
55}
Alan Stokes9ca14ca2021-10-20 14:25:57 +010056
57struct Callback(Arc<State>);
58
59#[derive(Default)]
60struct State {
61 mutex: Mutex<Option<Outcome>>,
62 completed: Condvar,
63}
64
65#[derive(Copy, Clone)]
66enum Outcome {
67 Succeeded,
68 Failed,
69}
70
71impl Interface for Callback {}
72
73impl ICompilationTaskCallback for Callback {
74 fn onSuccess(&self) -> BinderResult<()> {
75 self.0.set_outcome(Outcome::Succeeded);
76 Ok(())
77 }
78
79 fn onFailure(&self) -> BinderResult<()> {
80 self.0.set_outcome(Outcome::Failed);
81 Ok(())
82 }
83}
84
85impl State {
86 fn set_outcome(&self, outcome: Outcome) {
87 let mut guard = self.mutex.lock().unwrap();
88 *guard = Some(outcome);
89 drop(guard);
90 self.completed.notify_all();
91 }
92
93 fn wait(&self, duration: Duration) -> Result<Outcome> {
94 let (outcome, result) = self
95 .completed
96 .wait_timeout_while(self.mutex.lock().unwrap(), duration, |outcome| outcome.is_none())
97 .unwrap();
98 if result.timed_out() {
99 bail!("Timed out waiting for compilation")
100 }
101 Ok(outcome.unwrap())
102 }
103}
104
105fn run_forced_compile_for_test() -> Result<()> {
106 let service = wait_for_interface::<dyn IIsolatedCompilationService>("android.system.composd")
107 .context("Failed to connect to composd service")?;
108
109 let state = Arc::new(State::default());
110 let callback = Callback(state.clone());
111 let callback = BnCompilationTaskCallback::new_binder(callback, BinderFeatures::default());
112 let task = service.startTestCompile(&callback).context("Compilation failed")?;
113
114 // Make sure composd keeps going even if we don't hold a reference to its service.
115 drop(service);
116
Alan Stokesdcb96022021-10-26 15:23:31 +0100117 let state_clone = state.clone();
118 let mut death_recipient = DeathRecipient::new(move || {
119 eprintln!("CompilationTask died");
120 state_clone.set_outcome(Outcome::Failed);
121 });
122 // Note that dropping death_recipient cancels this, so we can't use a temporary here.
123 task.as_binder().link_to_death(&mut death_recipient)?;
Alan Stokes9ca14ca2021-10-20 14:25:57 +0100124
125 println!("Waiting");
126
Alan Stokes17aed5c2021-10-20 14:25:57 +0100127 match state.wait(timeouts()?.odrefresh_max_execution_time) {
Alan Stokes9ca14ca2021-10-20 14:25:57 +0100128 Ok(Outcome::Succeeded) => Ok(()),
129 Ok(Outcome::Failed) => bail!("Compilation failed"),
130 Err(e) => {
131 if let Err(e) = task.cancel() {
132 eprintln!("Failed to cancel compilation: {:?}", e);
133 }
134 Err(e)
135 }
136 }
137}