blob: f22dc134fe107288e0caea82b5fe8d80078f414a [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::{
Alan Stokes6fc18372021-11-25 17:50:27 +000021 ICompilationTask::ICompilationTask,
Alan Stokes9ca14ca2021-10-20 14:25:57 +010022 ICompilationTaskCallback::{BnCompilationTaskCallback, ICompilationTaskCallback},
23 IIsolatedCompilationService::IIsolatedCompilationService,
24 },
Alan Stokesdcb96022021-10-26 15:23:31 +010025 binder::{
26 wait_for_interface, BinderFeatures, DeathRecipient, IBinder, Interface, ProcessState,
Alan Stokes6fc18372021-11-25 17:50:27 +000027 Result as BinderResult, Strong,
Alan Stokesdcb96022021-10-26 15:23:31 +010028 },
Alan Stokesb2cc79e2021-09-14 14:08:46 +010029};
Alan Stokes9ca14ca2021-10-20 14:25:57 +010030use anyhow::{bail, Context, Result};
Alan Stokes17aed5c2021-10-20 14:25:57 +010031use compos_common::timeouts::timeouts;
Alan Stokes9ca14ca2021-10-20 14:25:57 +010032use std::sync::{Arc, Condvar, Mutex};
33use std::time::Duration;
Alan Stokesb2cc79e2021-09-14 14:08:46 +010034
35fn main() -> Result<()> {
Alan Stokes388b88a2021-10-13 16:03:17 +010036 let app = clap::App::new("composd_cmd").arg(
37 clap::Arg::with_name("command")
38 .index(1)
39 .takes_value(true)
40 .required(true)
Alan Stokes6fc18372021-11-25 17:50:27 +000041 .possible_values(&["staged-apex-compile", "forced-compile-test", "forced-odrefresh"]),
Alan Stokes388b88a2021-10-13 16:03:17 +010042 );
43 let args = app.get_matches();
44 let command = args.value_of("command").unwrap();
45
Alan Stokesb2cc79e2021-09-14 14:08:46 +010046 ProcessState::start_thread_pool();
47
Alan Stokes388b88a2021-10-13 16:03:17 +010048 match command {
Alan Stokes6fc18372021-11-25 17:50:27 +000049 "staged-apex-compile" => run_staged_apex_compile()?,
Alan Stokes9ca14ca2021-10-20 14:25:57 +010050 "forced-compile-test" => run_forced_compile_for_test()?,
Victor Hsieh72c774c2021-11-18 15:52:28 -080051 "forced-odrefresh" => run_forced_odrefresh_for_test()?,
Alan Stokes388b88a2021-10-13 16:03:17 +010052 _ => panic!("Unexpected command {}", command),
53 }
Alan Stokesb2cc79e2021-09-14 14:08:46 +010054
55 println!("All Ok!");
56
57 Ok(())
58}
Alan Stokes9ca14ca2021-10-20 14:25:57 +010059
60struct Callback(Arc<State>);
61
62#[derive(Default)]
63struct State {
64 mutex: Mutex<Option<Outcome>>,
65 completed: Condvar,
66}
67
68#[derive(Copy, Clone)]
69enum Outcome {
70 Succeeded,
71 Failed,
72}
73
74impl Interface for Callback {}
75
76impl ICompilationTaskCallback for Callback {
77 fn onSuccess(&self) -> BinderResult<()> {
78 self.0.set_outcome(Outcome::Succeeded);
79 Ok(())
80 }
81
82 fn onFailure(&self) -> BinderResult<()> {
83 self.0.set_outcome(Outcome::Failed);
84 Ok(())
85 }
86}
87
88impl State {
89 fn set_outcome(&self, outcome: Outcome) {
90 let mut guard = self.mutex.lock().unwrap();
91 *guard = Some(outcome);
92 drop(guard);
93 self.completed.notify_all();
94 }
95
96 fn wait(&self, duration: Duration) -> Result<Outcome> {
97 let (outcome, result) = self
98 .completed
99 .wait_timeout_while(self.mutex.lock().unwrap(), duration, |outcome| outcome.is_none())
100 .unwrap();
101 if result.timed_out() {
102 bail!("Timed out waiting for compilation")
103 }
104 Ok(outcome.unwrap())
105 }
106}
107
Alan Stokes6fc18372021-11-25 17:50:27 +0000108fn run_staged_apex_compile() -> Result<()> {
109 run_async_compilation(|service, callback| service.startStagedApexCompile(callback))
110}
111
Alan Stokes9ca14ca2021-10-20 14:25:57 +0100112fn run_forced_compile_for_test() -> Result<()> {
Alan Stokes6fc18372021-11-25 17:50:27 +0000113 run_async_compilation(|service, callback| service.startTestCompile(callback))
114}
115
116fn run_async_compilation<F>(start_compile_fn: F) -> Result<()>
117where
118 F: FnOnce(
119 &dyn IIsolatedCompilationService,
120 &Strong<dyn ICompilationTaskCallback>,
121 ) -> BinderResult<Strong<dyn ICompilationTask>>,
122{
Alan Stokes9ca14ca2021-10-20 14:25:57 +0100123 let service = wait_for_interface::<dyn IIsolatedCompilationService>("android.system.composd")
124 .context("Failed to connect to composd service")?;
125
126 let state = Arc::new(State::default());
127 let callback = Callback(state.clone());
128 let callback = BnCompilationTaskCallback::new_binder(callback, BinderFeatures::default());
Alan Stokes6fc18372021-11-25 17:50:27 +0000129 let task = start_compile_fn(&*service, &callback).context("Compilation failed")?;
Alan Stokes9ca14ca2021-10-20 14:25:57 +0100130
131 // Make sure composd keeps going even if we don't hold a reference to its service.
132 drop(service);
133
Alan Stokesdcb96022021-10-26 15:23:31 +0100134 let state_clone = state.clone();
135 let mut death_recipient = DeathRecipient::new(move || {
136 eprintln!("CompilationTask died");
137 state_clone.set_outcome(Outcome::Failed);
138 });
139 // Note that dropping death_recipient cancels this, so we can't use a temporary here.
140 task.as_binder().link_to_death(&mut death_recipient)?;
Alan Stokes9ca14ca2021-10-20 14:25:57 +0100141
142 println!("Waiting");
143
Alan Stokes17aed5c2021-10-20 14:25:57 +0100144 match state.wait(timeouts()?.odrefresh_max_execution_time) {
Alan Stokes9ca14ca2021-10-20 14:25:57 +0100145 Ok(Outcome::Succeeded) => Ok(()),
146 Ok(Outcome::Failed) => bail!("Compilation failed"),
147 Err(e) => {
148 if let Err(e) = task.cancel() {
149 eprintln!("Failed to cancel compilation: {:?}", e);
150 }
151 Err(e)
152 }
153 }
154}
Victor Hsieh72c774c2021-11-18 15:52:28 -0800155
156fn run_forced_odrefresh_for_test() -> Result<()> {
157 let service = wait_for_interface::<dyn IIsolatedCompilationService>("android.system.composd")
158 .context("Failed to connect to composd service")?;
159 let compilation_result = service.startTestOdrefresh().context("Compilation failed")?;
160 println!("odrefresh exit code: {:?}", compilation_result);
161 Ok(())
162}