blob: 37d5378d2190d3b7ec0a267c505c2c61b9fd8a60 [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(
Alan Stokes8c840442021-11-26 15:54:30 +000037 clap::Arg::with_name("command").index(1).takes_value(true).required(true).possible_values(
38 &["staged-apex-compile", "forced-compile-test", "forced-odrefresh", "async-odrefresh"],
39 ),
Alan Stokes388b88a2021-10-13 16:03:17 +010040 );
41 let args = app.get_matches();
42 let command = args.value_of("command").unwrap();
43
Alan Stokesb2cc79e2021-09-14 14:08:46 +010044 ProcessState::start_thread_pool();
45
Alan Stokes388b88a2021-10-13 16:03:17 +010046 match command {
Alan Stokes6fc18372021-11-25 17:50:27 +000047 "staged-apex-compile" => run_staged_apex_compile()?,
Alan Stokes9ca14ca2021-10-20 14:25:57 +010048 "forced-compile-test" => run_forced_compile_for_test()?,
Victor Hsieh72c774c2021-11-18 15:52:28 -080049 "forced-odrefresh" => run_forced_odrefresh_for_test()?,
Alan Stokes8c840442021-11-26 15:54:30 +000050 "async-odrefresh" => run_async_odrefresh_for_test()?,
Alan Stokes388b88a2021-10-13 16:03:17 +010051 _ => panic!("Unexpected command {}", command),
52 }
Alan Stokesb2cc79e2021-09-14 14:08:46 +010053
54 println!("All Ok!");
55
56 Ok(())
57}
Alan Stokes9ca14ca2021-10-20 14:25:57 +010058
59struct Callback(Arc<State>);
60
61#[derive(Default)]
62struct State {
63 mutex: Mutex<Option<Outcome>>,
64 completed: Condvar,
65}
66
67#[derive(Copy, Clone)]
68enum Outcome {
69 Succeeded,
70 Failed,
71}
72
73impl Interface for Callback {}
74
75impl ICompilationTaskCallback for Callback {
76 fn onSuccess(&self) -> BinderResult<()> {
77 self.0.set_outcome(Outcome::Succeeded);
78 Ok(())
79 }
80
81 fn onFailure(&self) -> BinderResult<()> {
82 self.0.set_outcome(Outcome::Failed);
83 Ok(())
84 }
85}
86
87impl State {
88 fn set_outcome(&self, outcome: Outcome) {
89 let mut guard = self.mutex.lock().unwrap();
90 *guard = Some(outcome);
91 drop(guard);
92 self.completed.notify_all();
93 }
94
95 fn wait(&self, duration: Duration) -> Result<Outcome> {
96 let (outcome, result) = self
97 .completed
98 .wait_timeout_while(self.mutex.lock().unwrap(), duration, |outcome| outcome.is_none())
99 .unwrap();
100 if result.timed_out() {
101 bail!("Timed out waiting for compilation")
102 }
103 Ok(outcome.unwrap())
104 }
105}
106
Alan Stokes6fc18372021-11-25 17:50:27 +0000107fn run_staged_apex_compile() -> Result<()> {
108 run_async_compilation(|service, callback| service.startStagedApexCompile(callback))
109}
110
Alan Stokes9ca14ca2021-10-20 14:25:57 +0100111fn run_forced_compile_for_test() -> Result<()> {
Alan Stokes6fc18372021-11-25 17:50:27 +0000112 run_async_compilation(|service, callback| service.startTestCompile(callback))
113}
114
Alan Stokes8c840442021-11-26 15:54:30 +0000115fn run_async_odrefresh_for_test() -> Result<()> {
116 run_async_compilation(|service, callback| service.startAsyncOdrefresh(callback))
117}
118
Alan Stokes6fc18372021-11-25 17:50:27 +0000119fn run_async_compilation<F>(start_compile_fn: F) -> Result<()>
120where
121 F: FnOnce(
122 &dyn IIsolatedCompilationService,
123 &Strong<dyn ICompilationTaskCallback>,
124 ) -> BinderResult<Strong<dyn ICompilationTask>>,
125{
Alan Stokes9ca14ca2021-10-20 14:25:57 +0100126 let service = wait_for_interface::<dyn IIsolatedCompilationService>("android.system.composd")
127 .context("Failed to connect to composd service")?;
128
129 let state = Arc::new(State::default());
130 let callback = Callback(state.clone());
131 let callback = BnCompilationTaskCallback::new_binder(callback, BinderFeatures::default());
Alan Stokes6fc18372021-11-25 17:50:27 +0000132 let task = start_compile_fn(&*service, &callback).context("Compilation failed")?;
Alan Stokes9ca14ca2021-10-20 14:25:57 +0100133
134 // Make sure composd keeps going even if we don't hold a reference to its service.
135 drop(service);
136
Alan Stokesdcb96022021-10-26 15:23:31 +0100137 let state_clone = state.clone();
138 let mut death_recipient = DeathRecipient::new(move || {
139 eprintln!("CompilationTask died");
140 state_clone.set_outcome(Outcome::Failed);
141 });
142 // Note that dropping death_recipient cancels this, so we can't use a temporary here.
143 task.as_binder().link_to_death(&mut death_recipient)?;
Alan Stokes9ca14ca2021-10-20 14:25:57 +0100144
145 println!("Waiting");
146
Alan Stokes17aed5c2021-10-20 14:25:57 +0100147 match state.wait(timeouts()?.odrefresh_max_execution_time) {
Alan Stokes9ca14ca2021-10-20 14:25:57 +0100148 Ok(Outcome::Succeeded) => Ok(()),
149 Ok(Outcome::Failed) => bail!("Compilation failed"),
150 Err(e) => {
151 if let Err(e) = task.cancel() {
152 eprintln!("Failed to cancel compilation: {:?}", e);
153 }
154 Err(e)
155 }
156 }
157}
Victor Hsieh72c774c2021-11-18 15:52:28 -0800158
159fn run_forced_odrefresh_for_test() -> Result<()> {
160 let service = wait_for_interface::<dyn IIsolatedCompilationService>("android.system.composd")
161 .context("Failed to connect to composd service")?;
162 let compilation_result = service.startTestOdrefresh().context("Compilation failed")?;
163 println!("odrefresh exit code: {:?}", compilation_result);
164 Ok(())
165}