Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 19 | use android_system_composd::{ |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 20 | aidl::android::system::composd::{ |
Alan Stokes | 6fc1837 | 2021-11-25 17:50:27 +0000 | [diff] [blame] | 21 | ICompilationTask::ICompilationTask, |
Alan Stokes | 81c96f3 | 2022-04-07 14:13:19 +0100 | [diff] [blame] | 22 | ICompilationTaskCallback::{ |
| 23 | BnCompilationTaskCallback, FailureReason::FailureReason, ICompilationTaskCallback, |
| 24 | }, |
Victor Hsieh | bac923e | 2022-02-22 21:43:36 +0000 | [diff] [blame] | 25 | IIsolatedCompilationService::ApexSource::ApexSource, |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 26 | IIsolatedCompilationService::IIsolatedCompilationService, |
| 27 | }, |
Alan Stokes | dcb9602 | 2021-10-26 15:23:31 +0100 | [diff] [blame] | 28 | binder::{ |
| 29 | wait_for_interface, BinderFeatures, DeathRecipient, IBinder, Interface, ProcessState, |
Alan Stokes | 6fc1837 | 2021-11-25 17:50:27 +0000 | [diff] [blame] | 30 | Result as BinderResult, Strong, |
Alan Stokes | dcb9602 | 2021-10-26 15:23:31 +0100 | [diff] [blame] | 31 | }, |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 32 | }; |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 33 | use anyhow::{bail, Context, Result}; |
Victor Hsieh | 1732c0d | 2022-09-12 14:36:03 -0700 | [diff] [blame^] | 34 | use clap::Parser; |
Alan Stokes | 7140377 | 2022-06-21 14:56:28 +0100 | [diff] [blame] | 35 | use compos_common::timeouts::TIMEOUTS; |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 36 | use std::sync::{Arc, Condvar, Mutex}; |
| 37 | use std::time::Duration; |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 38 | |
Victor Hsieh | 1732c0d | 2022-09-12 14:36:03 -0700 | [diff] [blame^] | 39 | #[derive(Parser)] |
| 40 | enum 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, |
| 49 | }, |
| 50 | } |
| 51 | |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 52 | fn main() -> Result<()> { |
Victor Hsieh | 1732c0d | 2022-09-12 14:36:03 -0700 | [diff] [blame^] | 53 | let action = Actions::parse(); |
Alan Stokes | 388b88a | 2021-10-13 16:03:17 +0100 | [diff] [blame] | 54 | |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 55 | ProcessState::start_thread_pool(); |
| 56 | |
Victor Hsieh | 1732c0d | 2022-09-12 14:36:03 -0700 | [diff] [blame^] | 57 | match action { |
| 58 | Actions::StagedApexCompile {} => run_staged_apex_compile()?, |
| 59 | Actions::TestCompile { prefer_staged } => run_test_compile(prefer_staged)?, |
Alan Stokes | 388b88a | 2021-10-13 16:03:17 +0100 | [diff] [blame] | 60 | } |
Alan Stokes | b2cc79e | 2021-09-14 14:08:46 +0100 | [diff] [blame] | 61 | |
| 62 | println!("All Ok!"); |
| 63 | |
| 64 | Ok(()) |
| 65 | } |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 66 | |
| 67 | struct Callback(Arc<State>); |
| 68 | |
| 69 | #[derive(Default)] |
| 70 | struct State { |
| 71 | mutex: Mutex<Option<Outcome>>, |
| 72 | completed: Condvar, |
| 73 | } |
| 74 | |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 75 | enum Outcome { |
| 76 | Succeeded, |
Alan Stokes | 81c96f3 | 2022-04-07 14:13:19 +0100 | [diff] [blame] | 77 | Failed(FailureReason, String), |
| 78 | TaskDied, |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | impl Interface for Callback {} |
| 82 | |
| 83 | impl ICompilationTaskCallback for Callback { |
| 84 | fn onSuccess(&self) -> BinderResult<()> { |
| 85 | self.0.set_outcome(Outcome::Succeeded); |
| 86 | Ok(()) |
| 87 | } |
| 88 | |
Alan Stokes | 81c96f3 | 2022-04-07 14:13:19 +0100 | [diff] [blame] | 89 | fn onFailure(&self, reason: FailureReason, message: &str) -> BinderResult<()> { |
| 90 | self.0.set_outcome(Outcome::Failed(reason, message.to_owned())); |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 91 | Ok(()) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | impl State { |
| 96 | fn set_outcome(&self, outcome: Outcome) { |
| 97 | let mut guard = self.mutex.lock().unwrap(); |
| 98 | *guard = Some(outcome); |
| 99 | drop(guard); |
| 100 | self.completed.notify_all(); |
| 101 | } |
| 102 | |
| 103 | fn wait(&self, duration: Duration) -> Result<Outcome> { |
Alan Stokes | 81c96f3 | 2022-04-07 14:13:19 +0100 | [diff] [blame] | 104 | let (mut outcome, result) = self |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 105 | .completed |
| 106 | .wait_timeout_while(self.mutex.lock().unwrap(), duration, |outcome| outcome.is_none()) |
| 107 | .unwrap(); |
| 108 | if result.timed_out() { |
| 109 | bail!("Timed out waiting for compilation") |
| 110 | } |
Alan Stokes | 81c96f3 | 2022-04-07 14:13:19 +0100 | [diff] [blame] | 111 | Ok(outcome.take().unwrap()) |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
Alan Stokes | 6fc1837 | 2021-11-25 17:50:27 +0000 | [diff] [blame] | 115 | fn run_staged_apex_compile() -> Result<()> { |
| 116 | run_async_compilation(|service, callback| service.startStagedApexCompile(callback)) |
| 117 | } |
| 118 | |
Victor Hsieh | bac923e | 2022-02-22 21:43:36 +0000 | [diff] [blame] | 119 | fn run_test_compile(prefer_staged: bool) -> Result<()> { |
| 120 | let apex_source = if prefer_staged { ApexSource::PreferStaged } else { ApexSource::NoStaged }; |
| 121 | run_async_compilation(|service, callback| service.startTestCompile(apex_source, callback)) |
Alan Stokes | 6fc1837 | 2021-11-25 17:50:27 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | fn run_async_compilation<F>(start_compile_fn: F) -> Result<()> |
| 125 | where |
| 126 | F: FnOnce( |
| 127 | &dyn IIsolatedCompilationService, |
| 128 | &Strong<dyn ICompilationTaskCallback>, |
| 129 | ) -> BinderResult<Strong<dyn ICompilationTask>>, |
| 130 | { |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 131 | let service = wait_for_interface::<dyn IIsolatedCompilationService>("android.system.composd") |
| 132 | .context("Failed to connect to composd service")?; |
| 133 | |
| 134 | let state = Arc::new(State::default()); |
| 135 | let callback = Callback(state.clone()); |
| 136 | let callback = BnCompilationTaskCallback::new_binder(callback, BinderFeatures::default()); |
Alan Stokes | 6fc1837 | 2021-11-25 17:50:27 +0000 | [diff] [blame] | 137 | let task = start_compile_fn(&*service, &callback).context("Compilation failed")?; |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 138 | |
| 139 | // Make sure composd keeps going even if we don't hold a reference to its service. |
| 140 | drop(service); |
| 141 | |
Alan Stokes | dcb9602 | 2021-10-26 15:23:31 +0100 | [diff] [blame] | 142 | let state_clone = state.clone(); |
| 143 | let mut death_recipient = DeathRecipient::new(move || { |
| 144 | eprintln!("CompilationTask died"); |
Alan Stokes | 81c96f3 | 2022-04-07 14:13:19 +0100 | [diff] [blame] | 145 | state_clone.set_outcome(Outcome::TaskDied); |
Alan Stokes | dcb9602 | 2021-10-26 15:23:31 +0100 | [diff] [blame] | 146 | }); |
| 147 | // Note that dropping death_recipient cancels this, so we can't use a temporary here. |
| 148 | task.as_binder().link_to_death(&mut death_recipient)?; |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 149 | |
| 150 | println!("Waiting"); |
| 151 | |
Alan Stokes | 7140377 | 2022-06-21 14:56:28 +0100 | [diff] [blame] | 152 | match state.wait(TIMEOUTS.odrefresh_max_execution_time) { |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 153 | Ok(Outcome::Succeeded) => Ok(()), |
Alan Stokes | 81c96f3 | 2022-04-07 14:13:19 +0100 | [diff] [blame] | 154 | Ok(Outcome::TaskDied) => bail!("Compilation task died"), |
| 155 | Ok(Outcome::Failed(reason, message)) => { |
| 156 | bail!("Compilation failed: {:?}: {}", reason, message) |
| 157 | } |
Alan Stokes | 9ca14ca | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 158 | Err(e) => { |
| 159 | if let Err(e) = task.cancel() { |
| 160 | eprintln!("Failed to cancel compilation: {:?}", e); |
| 161 | } |
| 162 | Err(e) |
| 163 | } |
| 164 | } |
| 165 | } |