blob: c944c17b05e37b3102ea258c3775ae1fb19a2059 [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.
Inseob Kim74a4b052025-01-24 16:50:57 +090042 StagedApexCompile {
43 /// OS for the VM.
44 #[clap(long, default_value = "microdroid")]
45 os: String,
46 },
Victor Hsieh1732c0d2022-09-12 14:36:03 -070047
48 /// Compile classpath in a debugging VM. Output is ignored.
49 TestCompile {
50 /// If any APEX is staged, prefer the staged version.
51 #[clap(long)]
52 prefer_staged: bool,
Inseob Kim4657d0e2024-11-28 13:34:10 +090053
54 /// OS for the VM.
55 #[clap(long, default_value = "microdroid")]
56 os: String,
Victor Hsieh1732c0d2022-09-12 14:36:03 -070057 },
58}
59
Alan Stokesb2cc79e2021-09-14 14:08:46 +010060fn main() -> Result<()> {
Victor Hsieh1732c0d2022-09-12 14:36:03 -070061 let action = Actions::parse();
Alan Stokes388b88a2021-10-13 16:03:17 +010062
Alan Stokesb2cc79e2021-09-14 14:08:46 +010063 ProcessState::start_thread_pool();
64
Victor Hsieh1732c0d2022-09-12 14:36:03 -070065 match action {
Inseob Kim74a4b052025-01-24 16:50:57 +090066 Actions::StagedApexCompile { os } => run_staged_apex_compile(&os)?,
Inseob Kim4657d0e2024-11-28 13:34:10 +090067 Actions::TestCompile { prefer_staged, os } => run_test_compile(prefer_staged, &os)?,
Alan Stokes388b88a2021-10-13 16:03:17 +010068 }
Alan Stokesb2cc79e2021-09-14 14:08:46 +010069
70 println!("All Ok!");
71
72 Ok(())
73}
Alan Stokes9ca14ca2021-10-20 14:25:57 +010074
75struct Callback(Arc<State>);
76
77#[derive(Default)]
78struct State {
79 mutex: Mutex<Option<Outcome>>,
80 completed: Condvar,
81}
82
Alan Stokes9ca14ca2021-10-20 14:25:57 +010083enum Outcome {
84 Succeeded,
Alan Stokes81c96f32022-04-07 14:13:19 +010085 Failed(FailureReason, String),
86 TaskDied,
Alan Stokes9ca14ca2021-10-20 14:25:57 +010087}
88
89impl Interface for Callback {}
90
91impl ICompilationTaskCallback for Callback {
92 fn onSuccess(&self) -> BinderResult<()> {
93 self.0.set_outcome(Outcome::Succeeded);
94 Ok(())
95 }
96
Alan Stokes81c96f32022-04-07 14:13:19 +010097 fn onFailure(&self, reason: FailureReason, message: &str) -> BinderResult<()> {
98 self.0.set_outcome(Outcome::Failed(reason, message.to_owned()));
Alan Stokes9ca14ca2021-10-20 14:25:57 +010099 Ok(())
100 }
101}
102
103impl State {
104 fn set_outcome(&self, outcome: Outcome) {
105 let mut guard = self.mutex.lock().unwrap();
106 *guard = Some(outcome);
107 drop(guard);
108 self.completed.notify_all();
109 }
110
111 fn wait(&self, duration: Duration) -> Result<Outcome> {
Alan Stokes81c96f32022-04-07 14:13:19 +0100112 let (mut outcome, result) = self
Alan Stokes9ca14ca2021-10-20 14:25:57 +0100113 .completed
114 .wait_timeout_while(self.mutex.lock().unwrap(), duration, |outcome| outcome.is_none())
115 .unwrap();
116 if result.timed_out() {
117 bail!("Timed out waiting for compilation")
118 }
Alan Stokes81c96f32022-04-07 14:13:19 +0100119 Ok(outcome.take().unwrap())
Alan Stokes9ca14ca2021-10-20 14:25:57 +0100120 }
121}
122
Inseob Kim74a4b052025-01-24 16:50:57 +0900123fn run_staged_apex_compile(os: &str) -> Result<()> {
124 run_async_compilation(|service, callback| service.startStagedApexCompile(callback, os))
Alan Stokes6fc18372021-11-25 17:50:27 +0000125}
126
Inseob Kim4657d0e2024-11-28 13:34:10 +0900127fn run_test_compile(prefer_staged: bool, os: &str) -> Result<()> {
Victor Hsiehbac923e2022-02-22 21:43:36 +0000128 let apex_source = if prefer_staged { ApexSource::PreferStaged } else { ApexSource::NoStaged };
Inseob Kim4657d0e2024-11-28 13:34:10 +0900129 run_async_compilation(|service, callback| service.startTestCompile(apex_source, callback, os))
Alan Stokes6fc18372021-11-25 17:50:27 +0000130}
131
132fn run_async_compilation<F>(start_compile_fn: F) -> Result<()>
133where
134 F: FnOnce(
135 &dyn IIsolatedCompilationService,
136 &Strong<dyn ICompilationTaskCallback>,
137 ) -> BinderResult<Strong<dyn ICompilationTask>>,
138{
Alan Stokesc4d5def2023-02-14 17:01:59 +0000139 if !hypervisor_props::is_any_vm_supported()? {
140 // Give up now, before trying to start composd, or we may end up waiting forever
141 // as it repeatedly starts and then aborts (b/254599807).
142 bail!("Device doesn't support protected or non-protected VMs")
143 }
144
Alan Stokes9ca14ca2021-10-20 14:25:57 +0100145 let service = wait_for_interface::<dyn IIsolatedCompilationService>("android.system.composd")
146 .context("Failed to connect to composd service")?;
147
148 let state = Arc::new(State::default());
149 let callback = Callback(state.clone());
150 let callback = BnCompilationTaskCallback::new_binder(callback, BinderFeatures::default());
Alan Stokes6fc18372021-11-25 17:50:27 +0000151 let task = start_compile_fn(&*service, &callback).context("Compilation failed")?;
Alan Stokes9ca14ca2021-10-20 14:25:57 +0100152
153 // Make sure composd keeps going even if we don't hold a reference to its service.
154 drop(service);
155
Alan Stokesdcb96022021-10-26 15:23:31 +0100156 let state_clone = state.clone();
157 let mut death_recipient = DeathRecipient::new(move || {
158 eprintln!("CompilationTask died");
Alan Stokes81c96f32022-04-07 14:13:19 +0100159 state_clone.set_outcome(Outcome::TaskDied);
Alan Stokesdcb96022021-10-26 15:23:31 +0100160 });
161 // Note that dropping death_recipient cancels this, so we can't use a temporary here.
162 task.as_binder().link_to_death(&mut death_recipient)?;
Alan Stokes9ca14ca2021-10-20 14:25:57 +0100163
164 println!("Waiting");
165
Alan Stokes71403772022-06-21 14:56:28 +0100166 match state.wait(TIMEOUTS.odrefresh_max_execution_time) {
Alan Stokes9ca14ca2021-10-20 14:25:57 +0100167 Ok(Outcome::Succeeded) => Ok(()),
Alan Stokes81c96f32022-04-07 14:13:19 +0100168 Ok(Outcome::TaskDied) => bail!("Compilation task died"),
169 Ok(Outcome::Failed(reason, message)) => {
170 bail!("Compilation failed: {:?}: {}", reason, message)
171 }
Alan Stokes9ca14ca2021-10-20 14:25:57 +0100172 Err(e) => {
173 if let Err(e) = task.cancel() {
174 eprintln!("Failed to cancel compilation: {:?}", e);
175 }
176 Err(e)
177 }
178 }
179}
Andrew Walbranda8786d2022-12-01 14:54:27 +0000180
181#[cfg(test)]
182mod tests {
183 use super::*;
184 use clap::CommandFactory;
185
186 #[test]
187 fn verify_actions() {
188 // Check that the command parsing has been configured in a valid way.
189 Actions::command().debug_assert();
190 }
191}