blob: 6afd711e749af5a042ce058ed45449c75cf0ca0f [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 Stokes81c96f32022-04-07 14:13:19 +010022 ICompilationTaskCallback::{
23 BnCompilationTaskCallback, FailureReason::FailureReason, ICompilationTaskCallback,
24 },
Victor Hsiehbac923e2022-02-22 21:43:36 +000025 IIsolatedCompilationService::ApexSource::ApexSource,
Alan Stokes9ca14ca2021-10-20 14:25:57 +010026 IIsolatedCompilationService::IIsolatedCompilationService,
27 },
Alan Stokesdcb96022021-10-26 15:23:31 +010028 binder::{
29 wait_for_interface, BinderFeatures, DeathRecipient, IBinder, Interface, ProcessState,
Alan Stokes6fc18372021-11-25 17:50:27 +000030 Result as BinderResult, Strong,
Alan Stokesdcb96022021-10-26 15:23:31 +010031 },
Alan Stokesb2cc79e2021-09-14 14:08:46 +010032};
Alan Stokes9ca14ca2021-10-20 14:25:57 +010033use anyhow::{bail, Context, Result};
Alan Stokes17aed5c2021-10-20 14:25:57 +010034use compos_common::timeouts::timeouts;
Alan Stokes9ca14ca2021-10-20 14:25:57 +010035use std::sync::{Arc, Condvar, Mutex};
36use std::time::Duration;
Alan Stokesb2cc79e2021-09-14 14:08:46 +010037
38fn main() -> Result<()> {
Victor Hsiehbac923e2022-02-22 21:43:36 +000039 #[rustfmt::skip]
40 let app = clap::App::new("composd_cmd")
41 .subcommand(
42 clap::SubCommand::with_name("staged-apex-compile"))
43 .subcommand(
44 clap::SubCommand::with_name("test-compile")
45 .arg(clap::Arg::with_name("prefer-staged").long("prefer-staged")),
46 );
Alan Stokes388b88a2021-10-13 16:03:17 +010047 let args = app.get_matches();
Alan Stokes388b88a2021-10-13 16:03:17 +010048
Alan Stokesb2cc79e2021-09-14 14:08:46 +010049 ProcessState::start_thread_pool();
50
Victor Hsiehbac923e2022-02-22 21:43:36 +000051 match args.subcommand() {
52 ("staged-apex-compile", _) => run_staged_apex_compile()?,
53 ("test-compile", Some(sub_matches)) => {
54 let prefer_staged = sub_matches.is_present("prefer-staged");
55 run_test_compile(prefer_staged)?;
56 }
57 _ => panic!("Unrecognized subcommand"),
Alan Stokes388b88a2021-10-13 16:03:17 +010058 }
Alan Stokesb2cc79e2021-09-14 14:08:46 +010059
60 println!("All Ok!");
61
62 Ok(())
63}
Alan Stokes9ca14ca2021-10-20 14:25:57 +010064
65struct Callback(Arc<State>);
66
67#[derive(Default)]
68struct State {
69 mutex: Mutex<Option<Outcome>>,
70 completed: Condvar,
71}
72
Alan Stokes9ca14ca2021-10-20 14:25:57 +010073enum Outcome {
74 Succeeded,
Alan Stokes81c96f32022-04-07 14:13:19 +010075 Failed(FailureReason, String),
76 TaskDied,
Alan Stokes9ca14ca2021-10-20 14:25:57 +010077}
78
79impl Interface for Callback {}
80
81impl ICompilationTaskCallback for Callback {
82 fn onSuccess(&self) -> BinderResult<()> {
83 self.0.set_outcome(Outcome::Succeeded);
84 Ok(())
85 }
86
Alan Stokes81c96f32022-04-07 14:13:19 +010087 fn onFailure(&self, reason: FailureReason, message: &str) -> BinderResult<()> {
88 self.0.set_outcome(Outcome::Failed(reason, message.to_owned()));
Alan Stokes9ca14ca2021-10-20 14:25:57 +010089 Ok(())
90 }
91}
92
93impl State {
94 fn set_outcome(&self, outcome: Outcome) {
95 let mut guard = self.mutex.lock().unwrap();
96 *guard = Some(outcome);
97 drop(guard);
98 self.completed.notify_all();
99 }
100
101 fn wait(&self, duration: Duration) -> Result<Outcome> {
Alan Stokes81c96f32022-04-07 14:13:19 +0100102 let (mut outcome, result) = self
Alan Stokes9ca14ca2021-10-20 14:25:57 +0100103 .completed
104 .wait_timeout_while(self.mutex.lock().unwrap(), duration, |outcome| outcome.is_none())
105 .unwrap();
106 if result.timed_out() {
107 bail!("Timed out waiting for compilation")
108 }
Alan Stokes81c96f32022-04-07 14:13:19 +0100109 Ok(outcome.take().unwrap())
Alan Stokes9ca14ca2021-10-20 14:25:57 +0100110 }
111}
112
Alan Stokes6fc18372021-11-25 17:50:27 +0000113fn run_staged_apex_compile() -> Result<()> {
114 run_async_compilation(|service, callback| service.startStagedApexCompile(callback))
115}
116
Victor Hsiehbac923e2022-02-22 21:43:36 +0000117fn run_test_compile(prefer_staged: bool) -> Result<()> {
118 let apex_source = if prefer_staged { ApexSource::PreferStaged } else { ApexSource::NoStaged };
119 run_async_compilation(|service, callback| service.startTestCompile(apex_source, callback))
Alan Stokes6fc18372021-11-25 17:50:27 +0000120}
121
122fn run_async_compilation<F>(start_compile_fn: F) -> Result<()>
123where
124 F: FnOnce(
125 &dyn IIsolatedCompilationService,
126 &Strong<dyn ICompilationTaskCallback>,
127 ) -> BinderResult<Strong<dyn ICompilationTask>>,
128{
Alan Stokes9ca14ca2021-10-20 14:25:57 +0100129 let service = wait_for_interface::<dyn IIsolatedCompilationService>("android.system.composd")
130 .context("Failed to connect to composd service")?;
131
132 let state = Arc::new(State::default());
133 let callback = Callback(state.clone());
134 let callback = BnCompilationTaskCallback::new_binder(callback, BinderFeatures::default());
Alan Stokes6fc18372021-11-25 17:50:27 +0000135 let task = start_compile_fn(&*service, &callback).context("Compilation failed")?;
Alan Stokes9ca14ca2021-10-20 14:25:57 +0100136
137 // Make sure composd keeps going even if we don't hold a reference to its service.
138 drop(service);
139
Alan Stokesdcb96022021-10-26 15:23:31 +0100140 let state_clone = state.clone();
141 let mut death_recipient = DeathRecipient::new(move || {
142 eprintln!("CompilationTask died");
Alan Stokes81c96f32022-04-07 14:13:19 +0100143 state_clone.set_outcome(Outcome::TaskDied);
Alan Stokesdcb96022021-10-26 15:23:31 +0100144 });
145 // Note that dropping death_recipient cancels this, so we can't use a temporary here.
146 task.as_binder().link_to_death(&mut death_recipient)?;
Alan Stokes9ca14ca2021-10-20 14:25:57 +0100147
148 println!("Waiting");
149
Alan Stokes17aed5c2021-10-20 14:25:57 +0100150 match state.wait(timeouts()?.odrefresh_max_execution_time) {
Alan Stokes9ca14ca2021-10-20 14:25:57 +0100151 Ok(Outcome::Succeeded) => Ok(()),
Alan Stokes81c96f32022-04-07 14:13:19 +0100152 Ok(Outcome::TaskDied) => bail!("Compilation task died"),
153 Ok(Outcome::Failed(reason, message)) => {
154 bail!("Compilation failed: {:?}: {}", reason, message)
155 }
Alan Stokes9ca14ca2021-10-20 14:25:57 +0100156 Err(e) => {
157 if let Err(e) = task.cancel() {
158 eprintln!("Failed to cancel compilation: {:?}", e);
159 }
160 Err(e)
161 }
162 }
163}