blob: b3ec1e5df1389256384ead0250f8c252c8aa6baf [file] [log] [blame]
Alan Stokes17aed5c2021-10-20 14:25:57 +01001/*
2 * Copyright 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//! Timeouts for common situations, with support for longer timeouts when using nested
18//! virtualization.
19
20use anyhow::Result;
21use rustutils::system_properties;
22use std::time::Duration;
23
24/// Holder for the various timeouts we use.
25#[derive(Debug, Copy, Clone)]
26pub struct Timeouts {
27 /// Total time that odrefresh may take to perform compilation
28 pub odrefresh_max_execution_time: Duration,
29 /// Time allowed for a single compilation step run by odrefresh
30 pub odrefresh_max_child_process_time: Duration,
31 /// Time allowed for the CompOS VM to start up and become ready.
32 pub vm_max_time_to_ready: Duration,
33}
34
35/// Whether the current platform requires extra time for operations inside a VM.
36pub fn need_extra_time() -> Result<bool> {
37 // Nested virtualization is slow. Check if we are running on vsoc as a proxy for this.
Andrew Walbran014efb52022-02-03 17:43:11 +000038 if let Some(value) = system_properties::read("ro.build.product")? {
39 Ok(value == "vsoc_x86_64" || value == "vsoc_x86")
40 } else {
41 Ok(false)
42 }
Alan Stokes17aed5c2021-10-20 14:25:57 +010043}
44
45/// Return the timeouts that are appropriate on the current platform.
46pub fn timeouts() -> Result<&'static Timeouts> {
47 if need_extra_time()? {
48 Ok(&EXTENDED_TIMEOUTS)
49 } else {
50 Ok(&NORMAL_TIMEOUTS)
51 }
52}
53
54/// The timeouts that we use normally.
55pub const NORMAL_TIMEOUTS: Timeouts = Timeouts {
56 // Note: the source of truth for these odrefresh timeouts is art/odrefresh/odr_config.h.
57 odrefresh_max_execution_time: Duration::from_secs(300),
58 odrefresh_max_child_process_time: Duration::from_secs(90),
Alan Stokes16fb8552022-02-10 15:07:27 +000059 vm_max_time_to_ready: Duration::from_secs(20),
Alan Stokes17aed5c2021-10-20 14:25:57 +010060};
61
62/// The timeouts that we use when need_extra_time() returns true.
63pub const EXTENDED_TIMEOUTS: Timeouts = Timeouts {
64 odrefresh_max_execution_time: Duration::from_secs(480),
65 odrefresh_max_child_process_time: Duration::from_secs(150),
66 vm_max_time_to_ready: Duration::from_secs(120),
67};