blob: 0b7bd25b2e49efca24681e260e190cb1142f99f5 [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::{
21 ICompilationTaskCallback::{BnCompilationTaskCallback, ICompilationTaskCallback},
22 IIsolatedCompilationService::IIsolatedCompilationService,
23 },
24 binder::{wait_for_interface, Interface, ProcessState, Result as BinderResult},
Alan Stokesb2cc79e2021-09-14 14:08:46 +010025};
Alan Stokes9ca14ca2021-10-20 14:25:57 +010026use anyhow::{bail, Context, Result};
27use binder::BinderFeatures;
28use std::sync::{Arc, Condvar, Mutex};
29use std::time::Duration;
Alan Stokesb2cc79e2021-09-14 14:08:46 +010030
31fn main() -> Result<()> {
Alan Stokes388b88a2021-10-13 16:03:17 +010032 let app = clap::App::new("composd_cmd").arg(
33 clap::Arg::with_name("command")
34 .index(1)
35 .takes_value(true)
36 .required(true)
37 .possible_values(&["forced-compile-test"]),
38 );
39 let args = app.get_matches();
40 let command = args.value_of("command").unwrap();
41
Alan Stokesb2cc79e2021-09-14 14:08:46 +010042 ProcessState::start_thread_pool();
43
Alan Stokes388b88a2021-10-13 16:03:17 +010044 match command {
Alan Stokes9ca14ca2021-10-20 14:25:57 +010045 "forced-compile-test" => run_forced_compile_for_test()?,
Alan Stokes388b88a2021-10-13 16:03:17 +010046 _ => panic!("Unexpected command {}", command),
47 }
Alan Stokesb2cc79e2021-09-14 14:08:46 +010048
49 println!("All Ok!");
50
51 Ok(())
52}
Alan Stokes9ca14ca2021-10-20 14:25:57 +010053
54struct Callback(Arc<State>);
55
56#[derive(Default)]
57struct State {
58 mutex: Mutex<Option<Outcome>>,
59 completed: Condvar,
60}
61
62#[derive(Copy, Clone)]
63enum Outcome {
64 Succeeded,
65 Failed,
66}
67
68impl Interface for Callback {}
69
70impl ICompilationTaskCallback for Callback {
71 fn onSuccess(&self) -> BinderResult<()> {
72 self.0.set_outcome(Outcome::Succeeded);
73 Ok(())
74 }
75
76 fn onFailure(&self) -> BinderResult<()> {
77 self.0.set_outcome(Outcome::Failed);
78 Ok(())
79 }
80}
81
82impl State {
83 fn set_outcome(&self, outcome: Outcome) {
84 let mut guard = self.mutex.lock().unwrap();
85 *guard = Some(outcome);
86 drop(guard);
87 self.completed.notify_all();
88 }
89
90 fn wait(&self, duration: Duration) -> Result<Outcome> {
91 let (outcome, result) = self
92 .completed
93 .wait_timeout_while(self.mutex.lock().unwrap(), duration, |outcome| outcome.is_none())
94 .unwrap();
95 if result.timed_out() {
96 bail!("Timed out waiting for compilation")
97 }
98 Ok(outcome.unwrap())
99 }
100}
101
102fn run_forced_compile_for_test() -> Result<()> {
103 let service = wait_for_interface::<dyn IIsolatedCompilationService>("android.system.composd")
104 .context("Failed to connect to composd service")?;
105
106 let state = Arc::new(State::default());
107 let callback = Callback(state.clone());
108 let callback = BnCompilationTaskCallback::new_binder(callback, BinderFeatures::default());
109 let task = service.startTestCompile(&callback).context("Compilation failed")?;
110
111 // Make sure composd keeps going even if we don't hold a reference to its service.
112 drop(service);
113
114 // TODO: Handle composd dying without sending callback?
115
116 println!("Waiting");
117
118 match state.wait(Duration::from_secs(480)) {
119 Ok(Outcome::Succeeded) => Ok(()),
120 Ok(Outcome::Failed) => bail!("Compilation failed"),
121 Err(e) => {
122 if let Err(e) = task.cancel() {
123 eprintln!("Failed to cancel compilation: {:?}", e);
124 }
125 Err(e)
126 }
127 }
128}