summaryrefslogtreecommitdiff
path: root/profcollectd
diff options
context:
space:
mode:
authorLuca Stefani <luca.stefani.ge1@gmail.com>2024-01-22 19:07:38 +0100
committerLuca Stefani <luca.stefani.ge1@gmail.com>2024-01-22 19:10:19 +0100
commitce7ab9a78889d17ce471b999dde3a9aff71d537a (patch)
tree61cadc792b487b06b072b9d65669220300792b7a /profcollectd
parentb0bb4d345b84141954d8f6e92603891a030ce70c (diff)
downloadextras-ce7ab9a78889d17ce471b999dde3a9aff71d537a.tar.gz
Replace lazy_static with once_cell
Using lazy_static is now discouraged as unmaintained and once_cell is the recommended replacement. On top of that a similar implementation found in once_cell is being tracked for inclusion under the `lazy_cell` feature gate [0] Also there's no need to lazy init the Timestamp Context as it's const. [0] https://github.com/rust-lang/rust/issues/109736 Test: m libprofcollectd Change-Id: I6314a47519b87e027005ccb6173a742a18050305
Diffstat (limited to 'profcollectd')
-rw-r--r--profcollectd/libprofcollectd/Android.bp2
-rw-r--r--profcollectd/libprofcollectd/config.rs27
-rw-r--r--profcollectd/libprofcollectd/report.rs8
3 files changed, 16 insertions, 21 deletions
diff --git a/profcollectd/libprofcollectd/Android.bp b/profcollectd/libprofcollectd/Android.bp
index 39177ec4..62aa6734 100644
--- a/profcollectd/libprofcollectd/Android.bp
+++ b/profcollectd/libprofcollectd/Android.bp
@@ -45,9 +45,9 @@ rust_library {
"libanyhow",
"libbinder_rs", // Remove once b/179041241 is fixed.
"libchrono",
- "liblazy_static",
"liblog_rust",
"libmacaddr",
+ "libonce_cell",
"librand",
"librustutils",
"libserde", // Remove once b/179041241 is fixed.
diff --git a/profcollectd/libprofcollectd/config.rs b/profcollectd/libprofcollectd/config.rs
index 46cd7f25..14236ab9 100644
--- a/profcollectd/libprofcollectd/config.rs
+++ b/profcollectd/libprofcollectd/config.rs
@@ -17,8 +17,8 @@
//! ProfCollect configurations.
use anyhow::Result;
-use lazy_static::lazy_static;
use macaddr::MacAddr6;
+use once_cell::sync::Lazy;
use rand::Rng;
use serde::{Deserialize, Serialize};
use std::error::Error;
@@ -34,14 +34,16 @@ const DEFAULT_BINARY_FILTER: &str = "^/(system|apex/.+)/(bin|lib|lib64)/.+";
pub const REPORT_RETENTION_SECS: u64 = 14 * 24 * 60 * 60; // 14 days.
// Static configs that cannot be changed.
-lazy_static! {
- pub static ref TRACE_OUTPUT_DIR: &'static Path = Path::new("/data/misc/profcollectd/trace/");
- pub static ref PROFILE_OUTPUT_DIR: &'static Path = Path::new("/data/misc/profcollectd/output/");
- pub static ref REPORT_OUTPUT_DIR: &'static Path = Path::new("/data/misc/profcollectd/report/");
- pub static ref CONFIG_FILE: &'static Path =
- Path::new("/data/misc/profcollectd/output/config.json");
- pub static ref LOG_FILE: &'static Path = Path::new("/data/misc/profcollectd/output/trace.log");
-}
+pub static TRACE_OUTPUT_DIR: Lazy<&'static Path> =
+ Lazy::new(|| Path::new("/data/misc/profcollectd/trace/"));
+pub static PROFILE_OUTPUT_DIR: Lazy<&'static Path> =
+ Lazy::new(|| Path::new("/data/misc/profcollectd/output/"));
+pub static REPORT_OUTPUT_DIR: Lazy<&'static Path> =
+ Lazy::new(|| Path::new("/data/misc/profcollectd/report/"));
+pub static CONFIG_FILE: Lazy<&'static Path> =
+ Lazy::new(|| Path::new("/data/misc/profcollectd/output/config.json"));
+pub static LOG_FILE: Lazy<&'static Path> =
+ Lazy::new(|| Path::new("/data/misc/profcollectd/output/trace.log"));
/// Dynamic configs, stored in config.json.
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
@@ -115,11 +117,8 @@ where
T::Err: Error + Send + Sync + 'static,
{
let default_value = default_value.to_string();
- let config = flags_rust::GetServerConfigurableFlag(
- PROFCOLLECT_CONFIG_NAMESPACE,
- key,
- &default_value,
- );
+ let config =
+ flags_rust::GetServerConfigurableFlag(PROFCOLLECT_CONFIG_NAMESPACE, key, &default_value);
Ok(T::from_str(&config)?)
}
diff --git a/profcollectd/libprofcollectd/report.rs b/profcollectd/libprofcollectd/report.rs
index cbce1d61..e0f2ec84 100644
--- a/profcollectd/libprofcollectd/report.rs
+++ b/profcollectd/libprofcollectd/report.rs
@@ -17,7 +17,6 @@
//! Pack profiles into reports.
use anyhow::{anyhow, Result};
-use lazy_static::lazy_static;
use macaddr::MacAddr6;
use std::fs::{self, File, Permissions};
use std::io::{Read, Write};
@@ -34,9 +33,7 @@ use crate::config::Config;
pub const NO_USAGE_SETTING: i32 = -1;
-lazy_static! {
- pub static ref UUID_CONTEXT: Context = Context::new(0);
-}
+pub static UUID_CONTEXT: Context = Context::new(0);
pub fn pack_report(
profile: &Path,
@@ -89,8 +86,7 @@ pub fn pack_report(
fn get_report_filename(node_id: &MacAddr6) -> Result<String> {
let since_epoch = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?;
- let ts =
- Timestamp::from_unix(&*UUID_CONTEXT, since_epoch.as_secs(), since_epoch.subsec_nanos());
+ let ts = Timestamp::from_unix(&UUID_CONTEXT, since_epoch.as_secs(), since_epoch.subsec_nanos());
let uuid = Uuid::new_v1(
ts,
node_id.as_bytes().try_into().expect("Invalid number of bytes in V1 UUID"),