blob: 6281bd021d19c4cc4afc4bb80c8dff48f1ef672d [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};
Victor Hsieh1732c0d2022-09-12 14:36:03 -070034use clap::Parser;
Alan Stokes71403772022-06-21 14:56:28 +010035use compos_common::timeouts::TIMEOUTS;
Alan Stokes9ca14ca2021-10-20 14:25:57 +010036use std::sync::{Arc, Condvar, Mutex};
37use std::time::Duration;
Alan Stokesb2cc79e2021-09-14 14:08:46 +010038
Victor Hsieh1732c0d2022-09-12 14:36:03 -070039#[derive(Parser)]
40enum Actions {
41 /// Compile classpath for real. Output can be used after a reboot.
42 StagedApexCompile {},
43
44 /// Compile classpath in a debugging VM. Output is ignored.
45 TestCompile {
46 /// If any APEX is staged, prefer the staged version.
47 #[clap(long)]
48 prefer_staged: bool,
Inseob Kim4657d0e2024-11-28 13:34:10 +090049
50 /// OS for the VM.
51 #[clap(long, default_value = "microdroid")]
52 os: String,
Victor Hsieh1732c0d2022-09-12 14:36:03 -070053 },
54}
55
Alan Stokesb2cc79e2021-09-14 14:08:46 +010056fn main() -> Result<()> {
Victor Hsieh1732c0d2022-09-12 14:36:03 -070057 let action = Actions::parse();
Alan Stokes388b88a2021-10-13 16:03:17 +010058
Alan Stokesb2cc79e2021-09-14 14:08:46 +010059 ProcessState::start_thread_pool();
60
Victor Hsieh1732c0d2022-09-12 14:36:03 -070061 match action {
62 Actions::StagedApexCompile {} => run_staged_apex_compile()?,
Inseob Kim4657d0e2024-11-28 13:34:10 +090063 Actions::TestCompile { prefer_staged, os } => run_test_compile(prefer_staged, &os)?,
Alan Stokes388b88a2021-10-13 16:03:17 +010064 }
Alan Stokesb2cc79e2021-09-14 14:08:46 +010065
66 println!("All Ok!");
67
68 Ok(())
69}
Alan Stokes9ca14ca2021-10-20 14:25:57 +010070
71struct Callback(Arc<State>);
72
73#[derive(Default)]
74struct State {
75 mutex: Mutex<Option<Outcome>>,
76 completed: Condvar,
77}
78
Alan Stokes9ca14ca2021-10-20 14:25:57 +010079enum Outcome {
80 Succeeded,
Alan Stokes81c96f32022-04-07 14:13:19 +010081 Failed(FailureReason, String),
82 TaskDied,
Alan Stokes9ca14ca2021-10-20 14:25:57 +010083}
84
85impl Interface for Callback {}
86
87impl ICompilationTaskCallback for Callback {
88 fn onSuccess(&self) -> BinderResult<()> {
89 self.0.set_outcome(Outcome::Succeeded);
90 Ok(())
91 }
92
Alan Stokes81c96f32022-04-07 14:13:19 +010093 fn onFailure(&self, reason: FailureReason, message: &str) -> BinderResult<()> {
94 self.0.set_outcome(Outcome::Failed(reason, message.to_owned()));
Alan Stokes9ca14ca2021-10-20 14:25:57 +010095 Ok(())
96 }
97}
98
99impl State {
100 fn set_outcome(&self, outcome: Outcome) {
101 let mut guard = self.mutex.lock().unwrap();
102 *guard = Some(outcome);
103 drop(guard);
104 self.completed.notify_all();
105 }
106
107 fn wait(&self, duration: Duration) -> Result<Outcome> {
Alan Stokes81c96f32022-04-07 14:13:19 +0100108 let (mut outcome, result) = self
Alan Stokes9ca14ca2021-10-20 14:25:57 +0100109 .completed
110 .wait_timeout_while(self.mutex.lock().unwrap(), duration, |outcome| outcome.is_none())
111 .unwrap();
112 if result.timed_out() {
113 bail!("Timed out waiting for compilation")
114 }
Alan Stokes81c96f32022-04-07 14:13:19 +0100115 Ok(outcome.take().unwrap())
Alan Stokes9ca14ca2021-10-20 14:25:57 +0100116 }
117}
118
Alan Stokes6fc18372021-11-25 17:50:27 +0000119fn run_staged_apex_compile() -> Result<()> {
120 run_async_compilation(|service, callback| service.startStagedApexCompile(callback))
121}
122
Inseob Kim4657d0e2024-11-28 13:34:10 +0900123fn run_test_compile(prefer_staged: bool, os: &str) -> Result<()> {
Victor Hsiehbac923e2022-02-22 21:43:36 +0000124 let apex_source = if prefer_staged { ApexSource::PreferStaged } else { ApexSource::NoStaged };
Inseob Kim4657d0e2024-11-28 13:34:10 +0900125 run_async_compilation(|service, callback| service.startTestCompile(apex_source, callback, os))
Alan Stokes6fc18372021-11-25 17:50:27 +0000126}
127
128fn run_async_compilation<F>(start_compile_fn: F) -> Result<()>
129where
130 F: FnOnce(
131 &dyn IIsolatedCompilationService,
132 &Strong<dyn ICompilationTaskCallback>,
133 ) -> BinderResult<Strong<dyn ICompilationTask>>,
134{
Alan Stokesc4d5def2023-02-14 17:01:59 +0000135 if !hypervisor_props::is_any_vm_supported()? {
136 // Give up now, before trying to start composd, or we may end up waiting forever
137 // as it repeatedly starts and then aborts (b/254599807).
138 bail!("Device doesn't support protected or non-protected VMs")
139 }
140
Alan Stokes9ca14ca2021-10-20 14:25:57 +0100141 let service = wait_for_interface::<dyn IIsolatedCompilationService>("android.system.composd")
142 .context("Failed to connect to composd service")?;
143
144 let state = Arc::new(State::default());
145 let callback = Callback(state.clone());
146 let callback = BnCompilationTaskCallback::new_binder(callback, BinderFeatures::default());
Alan Stokes6fc18372021-11-25 17:50:27 +0000147 let task = start_compile_fn(&*service, &callback).context("Compilation failed")?;
Alan Stokes9ca14ca2021-10-20 14:25:57 +0100148
149 // Make sure composd keeps going even if we don't hold a reference to its service.
150 drop(service);
151
Alan Stokesdcb96022021-10-26 15:23:31 +0100152 let state_clone = state.clone();
153 let mut death_recipient = DeathRecipient::new(move || {
154 eprintln!("CompilationTask died");
Alan Stokes81c96f32022-04-07 14:13:19 +0100155 state_clone.set_outcome(Outcome::TaskDied);
Alan Stokesdcb96022021-10-26 15:23:31 +0100156 });
157 // Note that dropping death_recipient cancels this, so we can't use a temporary here.
158 task.as_binder().link_to_death(&mut death_recipient)?;
Alan Stokes9ca14ca2021-10-20 14:25:57 +0100159
160 println!("Waiting");
161
Alan Stokes71403772022-06-21 14:56:28 +0100162 match state.wait(TIMEOUTS.odrefresh_max_execution_time) {
Alan Stokes9ca14ca2021-10-20 14:25:57 +0100163 Ok(Outcome::Succeeded) => Ok(()),
Alan Stokes81c96f32022-04-07 14:13:19 +0100164 Ok(Outcome::TaskDied) => bail!("Compilation task died"),
165 Ok(Outcome::Failed(reason, message)) => {
166 bail!("Compilation failed: {:?}: {}", reason, message)
167 }
Alan Stokes9ca14ca2021-10-20 14:25:57 +0100168 Err(e) => {
169 if let Err(e) = task.cancel() {
170 eprintln!("Failed to cancel compilation: {:?}", e);
171 }
172 Err(e)
173 }
174 }
175}
Andrew Walbranda8786d2022-12-01 14:54:27 +0000176
177#[cfg(test)]
178mod tests {
179 use super::*;
180 use clap::CommandFactory;
181
182 #[test]
183 fn verify_actions() {
184 // Check that the command parsing has been configured in a valid way.
185 Actions::command().debug_assert();
186 }
187}