Use LazyLock rather than lazy_static.
Bug: 364211748
Test: m virtmgr virtualizationservice libcompos_common libservice_vm_manager libvm_payload_rs
Change-Id: I3e92489a925709c3ab91968d1a38c9b52a54f3c5
diff --git a/android/virtmgr/Android.bp b/android/virtmgr/Android.bp
index 4828057..62ff8d8 100644
--- a/android/virtmgr/Android.bp
+++ b/android/virtmgr/Android.bp
@@ -44,7 +44,6 @@
"libglob",
"libhex",
"libhypervisor_props",
- "liblazy_static",
"liblibc",
"liblog_rust",
"libmicrodroid_metadata",
diff --git a/android/virtmgr/src/aidl.rs b/android/virtmgr/src/aidl.rs
index 7a357f3..158c564 100644
--- a/android/virtmgr/src/aidl.rs
+++ b/android/virtmgr/src/aidl.rs
@@ -67,7 +67,6 @@
};
use cstr::cstr;
use glob::glob;
-use lazy_static::lazy_static;
use log::{debug, error, info, warn};
use microdroid_payload_config::{ApkConfig, Task, TaskType, VmPayloadConfig};
use nix::unistd::pipe;
@@ -86,7 +85,7 @@
use std::os::unix::io::{AsRawFd, IntoRawFd};
use std::os::unix::raw::pid_t;
use std::path::{Path, PathBuf};
-use std::sync::{Arc, Mutex, Weak};
+use std::sync::{Arc, Mutex, Weak, LazyLock};
use vbmeta::VbMetaImage;
use vmconfig::{VmConfig, get_debug_level};
use vsock::VsockStream;
@@ -119,13 +118,13 @@
const VM_REFERENCE_DT_ON_HOST_PATH: &str = "/proc/device-tree/avf/reference";
-lazy_static! {
- pub static ref GLOBAL_SERVICE: Strong<dyn IVirtualizationServiceInternal> =
+pub static GLOBAL_SERVICE: LazyLock<Strong<dyn IVirtualizationServiceInternal>> =
+ LazyLock::new(|| {
wait_for_interface(BINDER_SERVICE_IDENTIFIER)
- .expect("Could not connect to VirtualizationServiceInternal");
- static ref SUPPORTED_OS_NAMES: HashSet<String> =
- get_supported_os_names().expect("Failed to get list of supported os names");
-}
+ .expect("Could not connect to VirtualizationServiceInternal")
+ });
+static SUPPORTED_OS_NAMES: LazyLock<HashSet<String>> =
+ LazyLock::new(|| get_supported_os_names().expect("Failed to get list of supported os names"));
fn create_or_update_idsig_file(
input_fd: &ParcelFileDescriptor,
diff --git a/android/virtmgr/src/crosvm.rs b/android/virtmgr/src/crosvm.rs
index 08a9e47..fff0399 100644
--- a/android/virtmgr/src/crosvm.rs
+++ b/android/virtmgr/src/crosvm.rs
@@ -20,7 +20,6 @@
use anyhow::{anyhow, bail, Context, Error, Result};
use binder::ParcelFileDescriptor;
use command_fds::CommandFdExt;
-use lazy_static::lazy_static;
use libc::{sysconf, _SC_CLK_TCK};
use log::{debug, error, info};
use semver::{Version, VersionReq};
@@ -39,7 +38,7 @@
use std::os::unix::process::ExitStatusExt;
use std::path::{Path, PathBuf};
use std::process::{Command, ExitStatus};
-use std::sync::{Arc, Condvar, Mutex};
+use std::sync::{Arc, Condvar, Mutex, LazyLock};
use std::time::{Duration, SystemTime};
use std::thread::{self, JoinHandle};
use android_system_virtualizationcommon::aidl::android::system::virtualizationcommon::DeathReason::DeathReason;
@@ -89,16 +88,16 @@
/// Serial (emulated uart)
const CONSOLE_TTYS0: &str = "ttyS0";
-lazy_static! {
- /// If the VM doesn't move to the Started state within this amount time, a hang-up error is
- /// triggered.
- static ref BOOT_HANGUP_TIMEOUT: Duration = if nested_virt::is_nested_virtualization().unwrap() {
+/// If the VM doesn't move to the Started state within this amount time, a hang-up error is
+/// triggered.
+static BOOT_HANGUP_TIMEOUT: LazyLock<Duration> = LazyLock::new(|| {
+ if nested_virt::is_nested_virtualization().unwrap() {
// Nested virtualization is slow, so we need a longer timeout.
Duration::from_secs(300)
} else {
Duration::from_secs(30)
- };
-}
+ }
+});
/// Configuration for a VM to run with crosvm.
#[derive(Debug)]
diff --git a/android/virtmgr/src/debug_config.rs b/android/virtmgr/src/debug_config.rs
index 52ac964..74559de 100644
--- a/android/virtmgr/src/debug_config.rs
+++ b/android/virtmgr/src/debug_config.rs
@@ -18,7 +18,6 @@
VirtualMachineAppConfig::DebugLevel::DebugLevel, VirtualMachineConfig::VirtualMachineConfig,
};
use anyhow::{anyhow, Context, Error, Result};
-use lazy_static::lazy_static;
use libfdt::{Fdt, FdtError};
use log::{info, warn};
use rustutils::system_properties;
@@ -26,6 +25,7 @@
use std::fs;
use std::io::ErrorKind;
use std::path::{Path, PathBuf};
+use std::sync::LazyLock;
use vmconfig::get_debug_level;
const CUSTOM_DEBUG_POLICY_OVERLAY_SYSPROP: &str =
@@ -56,11 +56,12 @@
}
}
-lazy_static! {
- static ref DP_LOG_PATH: DPPath = DPPath::new("/avf/guest/common", "log").unwrap();
- static ref DP_RAMDUMP_PATH: DPPath = DPPath::new("/avf/guest/common", "ramdump").unwrap();
- static ref DP_ADB_PATH: DPPath = DPPath::new("/avf/guest/microdroid", "adb").unwrap();
-}
+static DP_LOG_PATH: LazyLock<DPPath> =
+ LazyLock::new(|| DPPath::new("/avf/guest/common", "log").unwrap());
+static DP_RAMDUMP_PATH: LazyLock<DPPath> =
+ LazyLock::new(|| DPPath::new("/avf/guest/common", "ramdump").unwrap());
+static DP_ADB_PATH: LazyLock<DPPath> =
+ LazyLock::new(|| DPPath::new("/avf/guest/microdroid", "adb").unwrap());
/// Get debug policy value in bool. It's true iff the value is explicitly set to <1>.
fn get_debug_policy_bool(path: &Path) -> Result<bool> {
diff --git a/android/virtmgr/src/main.rs b/android/virtmgr/src/main.rs
index 7d29685..a4e75a7 100644
--- a/android/virtmgr/src/main.rs
+++ b/android/virtmgr/src/main.rs
@@ -27,10 +27,10 @@
use android_system_virtualizationservice::aidl::android::system::virtualizationservice::IVirtualizationService::BnVirtualizationService;
use anyhow::{bail, Result};
use binder::{BinderFeatures, ProcessState};
-use lazy_static::lazy_static;
use log::{info, LevelFilter};
use rpcbinder::{FileDescriptorTransportMode, RpcServer};
use std::os::unix::io::{AsFd, RawFd};
+use std::sync::LazyLock;
use clap::Parser;
use nix::unistd::{write, Pid, Uid};
use std::os::unix::raw::{pid_t, uid_t};
@@ -38,11 +38,9 @@
const LOG_TAG: &str = "virtmgr";
-lazy_static! {
- static ref PID_CURRENT: Pid = Pid::this();
- static ref PID_PARENT: Pid = Pid::parent();
- static ref UID_CURRENT: Uid = Uid::current();
-}
+static PID_CURRENT: LazyLock<Pid> = LazyLock::new(Pid::this);
+static PID_PARENT: LazyLock<Pid> = LazyLock::new(Pid::parent);
+static UID_CURRENT: LazyLock<Uid> = LazyLock::new(Uid::current);
fn get_this_pid() -> pid_t {
// Return the process ID of this process.