Alan Stokes | 17aed5c | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 20 | use anyhow::Result; |
| 21 | use rustutils::system_properties; |
| 22 | use std::time::Duration; |
| 23 | |
| 24 | /// Holder for the various timeouts we use. |
| 25 | #[derive(Debug, Copy, Clone)] |
| 26 | pub struct Timeouts { |
| 27 | /// Total time that odrefresh may take to perform compilation |
| 28 | pub odrefresh_max_execution_time: Duration, |
Alan Stokes | 17aed5c | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 29 | /// Time allowed for the CompOS VM to start up and become ready. |
| 30 | pub vm_max_time_to_ready: Duration, |
| 31 | } |
| 32 | |
| 33 | /// Whether the current platform requires extra time for operations inside a VM. |
| 34 | pub fn need_extra_time() -> Result<bool> { |
| 35 | // Nested virtualization is slow. Check if we are running on vsoc as a proxy for this. |
Andrew Walbran | 014efb5 | 2022-02-03 17:43:11 +0000 | [diff] [blame] | 36 | if let Some(value) = system_properties::read("ro.build.product")? { |
| 37 | Ok(value == "vsoc_x86_64" || value == "vsoc_x86") |
| 38 | } else { |
| 39 | Ok(false) |
| 40 | } |
Alan Stokes | 17aed5c | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /// Return the timeouts that are appropriate on the current platform. |
| 44 | pub fn timeouts() -> Result<&'static Timeouts> { |
| 45 | if need_extra_time()? { |
| 46 | Ok(&EXTENDED_TIMEOUTS) |
| 47 | } else { |
| 48 | Ok(&NORMAL_TIMEOUTS) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /// The timeouts that we use normally. |
| 53 | pub const NORMAL_TIMEOUTS: Timeouts = Timeouts { |
| 54 | // Note: the source of truth for these odrefresh timeouts is art/odrefresh/odr_config.h. |
| 55 | odrefresh_max_execution_time: Duration::from_secs(300), |
Alan Stokes | 758f9fd | 2022-03-17 16:10:48 +0000 | [diff] [blame] | 56 | vm_max_time_to_ready: Duration::from_secs(15), |
Alan Stokes | 17aed5c | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | /// The timeouts that we use when need_extra_time() returns true. |
| 60 | pub const EXTENDED_TIMEOUTS: Timeouts = Timeouts { |
| 61 | odrefresh_max_execution_time: Duration::from_secs(480), |
Alan Stokes | 17aed5c | 2021-10-20 14:25:57 +0100 | [diff] [blame] | 62 | vm_max_time_to_ready: Duration::from_secs(120), |
| 63 | }; |