diff options
author | Yi Kong <yikong@google.com> | 2021-11-29 19:57:55 +0800 |
---|---|---|
committer | Yi Kong <yikong@google.com> | 2021-11-30 08:00:03 +0000 |
commit | 970ba98ee22d24d42c5f1ee05aa8ec25fe90ea36 (patch) | |
tree | 65239b8e4ece24724d798a698406673fffb3e88a | |
parent | c0e6b3f2b147fb889f7e8af2db21924622d8b621 (diff) | |
download | extras-970ba98ee22d24d42c5f1ee05aa8ec25fe90ea36.tar.gz |
profcollectd: Remove local files once disabled
This saves storage space on users' devices if profcollect is no longer
enabled.
Test: manual
Bug: 207426233
Change-Id: I04f19ba7ddbf9d9f977bb40ac4bc74a09369350f
Merged-In: I04f19ba7ddbf9d9f977bb40ac4bc74a09369350f
-rw-r--r-- | profcollectd/libprofcollectd/config.rs | 19 | ||||
-rw-r--r-- | profcollectd/libprofcollectd/lib.rs | 6 | ||||
-rw-r--r-- | profcollectd/libprofcollectd/service.rs | 13 | ||||
-rw-r--r-- | profcollectd/profcollectctl.rs | 7 | ||||
-rw-r--r-- | profcollectd/profcollectd.rc | 12 |
5 files changed, 42 insertions, 15 deletions
diff --git a/profcollectd/libprofcollectd/config.rs b/profcollectd/libprofcollectd/config.rs index ea1a67c8..902b5b5e 100644 --- a/profcollectd/libprofcollectd/config.rs +++ b/profcollectd/libprofcollectd/config.rs @@ -22,6 +22,7 @@ use macaddr::MacAddr6; use rand::Rng; use serde::{Deserialize, Serialize}; use std::error::Error; +use std::fs::{read_dir, remove_file}; use std::path::Path; use std::str::FromStr; use std::time::Duration; @@ -31,6 +32,7 @@ const PROFCOLLECT_NODE_ID_PROPERTY: &str = "persist.profcollectd.node_id"; 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/"); @@ -42,6 +44,7 @@ lazy_static! { Path::new("/data/misc/profcollectd/output/config.json"); } +/// Dynamic configs, stored in config.json. #[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)] pub struct Config { /// Version of config file scheme, always equals to 1. @@ -144,3 +147,19 @@ fn generate_random_node_id() -> MacAddr6 { node_id[0] |= 0x1; MacAddr6::from(node_id) } + +pub fn clear_data() -> Result<()> { + fn remove_files(path: &Path) -> Result<()> { + read_dir(path)? + .filter_map(|e| e.ok()) + .map(|e| e.path()) + .filter(|e| e.is_file()) + .try_for_each(remove_file)?; + Ok(()) + } + + remove_files(&TRACE_OUTPUT_DIR)?; + remove_files(&PROFILE_OUTPUT_DIR)?; + remove_files(&REPORT_OUTPUT_DIR)?; + Ok(()) +} diff --git a/profcollectd/libprofcollectd/lib.rs b/profcollectd/libprofcollectd/lib.rs index f417bd8c..5892e259 100644 --- a/profcollectd/libprofcollectd/lib.rs +++ b/profcollectd/libprofcollectd/lib.rs @@ -91,6 +91,12 @@ pub fn report() -> Result<String> { Ok(get_profcollectd_service()?.report()?) } +/// Clear all local data. +pub fn reset() -> Result<()> { + config::clear_data()?; + Ok(()) +} + /// Inits logging for Android pub fn init_logging() { let min_log_level = if cfg!(feature = "test") { log::Level::Info } else { log::Level::Error }; diff --git a/profcollectd/libprofcollectd/service.rs b/profcollectd/libprofcollectd/service.rs index 04f30f89..a7fdce73 100644 --- a/profcollectd/libprofcollectd/service.rs +++ b/profcollectd/libprofcollectd/service.rs @@ -21,15 +21,15 @@ use binder::public_api::Result as BinderResult; use binder::Status; use profcollectd_aidl_interface::aidl::com::android::server::profcollect::IProfCollectd::IProfCollectd; use std::ffi::CString; -use std::fs::{copy, create_dir, read_dir, read_to_string, remove_dir_all, remove_file, write}; +use std::fs::{copy, read_dir, read_to_string, remove_file, write}; use std::path::PathBuf; use std::str::FromStr; use std::sync::{Mutex, MutexGuard}; use std::time::Duration; use crate::config::{ - Config, BETTERBUG_CACHE_DIR_PREFIX, BETTERBUG_CACHE_DIR_SUFFIX, CONFIG_FILE, - PROFILE_OUTPUT_DIR, REPORT_OUTPUT_DIR, REPORT_RETENTION_SECS, TRACE_OUTPUT_DIR, + clear_data, Config, BETTERBUG_CACHE_DIR_PREFIX, BETTERBUG_CACHE_DIR_SUFFIX, CONFIG_FILE, + PROFILE_OUTPUT_DIR, REPORT_OUTPUT_DIR, REPORT_RETENTION_SECS, }; use crate::report::{get_report_ts, pack_report}; use crate::scheduler::Scheduler; @@ -147,11 +147,8 @@ impl ProfcollectdBinderService { .is_none(); if config_changed { - log::info!("Config change detected, clearing traces."); - remove_dir_all(*PROFILE_OUTPUT_DIR)?; - remove_dir_all(*TRACE_OUTPUT_DIR)?; - create_dir(*PROFILE_OUTPUT_DIR)?; - create_dir(*TRACE_OUTPUT_DIR)?; + log::info!("Config change detected, resetting profcollect."); + clear_data()?; write(*CONFIG_FILE, &new_config.to_string())?; } diff --git a/profcollectd/profcollectctl.rs b/profcollectd/profcollectctl.rs index c825f550..6778465e 100644 --- a/profcollectd/profcollectctl.rs +++ b/profcollectd/profcollectctl.rs @@ -29,8 +29,9 @@ command: stop Terminate periodic collection. once Request an one-off trace. process Convert traces to perf profiles. - report Create a report containing all profiles. reconfig Refresh configuration. + report Create a report containing all profiles. + reset Clear all local data. help Print this message. "#; @@ -65,6 +66,10 @@ fn main() -> Result<()> { let path = libprofcollectd::report().context("Failed to create profile report.")?; println!("Report created at: {}", &path); } + "reset" => { + libprofcollectd::reset().context("Failed to reset.")?; + println!("Reset done."); + } "help" => println!("{}", &HELP_MSG), arg => bail!("Unknown argument: {}\n{}", &arg, &HELP_MSG), } diff --git a/profcollectd/profcollectd.rc b/profcollectd/profcollectd.rc index 1100bd2e..b92fccba 100644 --- a/profcollectd/profcollectd.rc +++ b/profcollectd/profcollectd.rc @@ -6,15 +6,15 @@ service profcollectd /system/bin/profcollectd group root wakelock writepid /dev/cpuset/system-background/tasks -on property:persist.device_config.profcollect_native_boot.enabled=true - start profcollectd - -on property:persist.profcollectd.enabled_override=true - start profcollectd - on post-fs-data # Create directory for profcollectd. mkdir /data/misc/profcollectd 0770 shell shell mkdir /data/misc/profcollectd/trace 0770 shell shell mkdir /data/misc/profcollectd/output 0770 shell shell mkdir /data/misc/profcollectd/report 0770 shell shell + +on boot && property:persist.device_config.profcollect_native_boot.enabled=true + start profcollectd + +on boot && property:persist.device_config.profcollect_native_boot.enabled= + exec_background - root shell -- /system/bin/profcollectctl reset |