Yi Kong | e3aab14 | 2021-03-02 13:58:25 +0800 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 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 | //! ProfCollect configurations. |
| 18 | |
| 19 | use anyhow::Result; |
| 20 | use lazy_static::lazy_static; |
Yi Kong | 037bde8 | 2021-03-23 14:25:38 +0800 | [diff] [blame] | 21 | use macaddr::MacAddr6; |
| 22 | use rand::Rng; |
Yi Kong | e3aab14 | 2021-03-02 13:58:25 +0800 | [diff] [blame] | 23 | use serde::{Deserialize, Serialize}; |
| 24 | use std::error::Error; |
Yi Kong | 34ebf87 | 2021-11-29 19:57:55 +0800 | [diff] [blame] | 25 | use std::fs::{read_dir, remove_file}; |
Yi Kong | e3aab14 | 2021-03-02 13:58:25 +0800 | [diff] [blame] | 26 | use std::path::Path; |
| 27 | use std::str::FromStr; |
| 28 | use std::time::Duration; |
| 29 | |
| 30 | const PROFCOLLECT_CONFIG_NAMESPACE: &str = "profcollect_native_boot"; |
Yi Kong | 037bde8 | 2021-03-23 14:25:38 +0800 | [diff] [blame] | 31 | const PROFCOLLECT_NODE_ID_PROPERTY: &str = "persist.profcollectd.node_id"; |
Yi Kong | e3aab14 | 2021-03-02 13:58:25 +0800 | [diff] [blame] | 32 | |
Yi Kong | 87d0a17 | 2021-12-09 01:37:57 +0800 | [diff] [blame] | 33 | const DEFAULT_BINARY_FILTER: &str = "^/(system|apex/.+)/(bin|lib|lib64)/.+"; |
Yi Kong | 8dffc12 | 2021-07-20 16:55:39 +0800 | [diff] [blame] | 34 | pub const REPORT_RETENTION_SECS: u64 = 14 * 24 * 60 * 60; // 14 days. |
| 35 | |
Yi Kong | 34ebf87 | 2021-11-29 19:57:55 +0800 | [diff] [blame] | 36 | // Static configs that cannot be changed. |
Yi Kong | e3aab14 | 2021-03-02 13:58:25 +0800 | [diff] [blame] | 37 | lazy_static! { |
| 38 | pub static ref TRACE_OUTPUT_DIR: &'static Path = Path::new("/data/misc/profcollectd/trace/"); |
| 39 | pub static ref PROFILE_OUTPUT_DIR: &'static Path = Path::new("/data/misc/profcollectd/output/"); |
| 40 | pub static ref REPORT_OUTPUT_DIR: &'static Path = Path::new("/data/misc/profcollectd/report/"); |
Yi Kong | e3aab14 | 2021-03-02 13:58:25 +0800 | [diff] [blame] | 41 | pub static ref CONFIG_FILE: &'static Path = |
| 42 | Path::new("/data/misc/profcollectd/output/config.json"); |
Yabin Cui | f1d91d2 | 2023-04-27 12:50:59 -0700 | [diff] [blame] | 43 | pub static ref LOG_FILE: &'static Path = Path::new("/data/misc/profcollectd/output/trace.log"); |
Yi Kong | e3aab14 | 2021-03-02 13:58:25 +0800 | [diff] [blame] | 44 | } |
| 45 | |
Yi Kong | 34ebf87 | 2021-11-29 19:57:55 +0800 | [diff] [blame] | 46 | /// Dynamic configs, stored in config.json. |
Yi Kong | e3aab14 | 2021-03-02 13:58:25 +0800 | [diff] [blame] | 47 | #[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)] |
| 48 | pub struct Config { |
| 49 | /// Version of config file scheme, always equals to 1. |
| 50 | version: u32, |
Yi Kong | 037bde8 | 2021-03-23 14:25:38 +0800 | [diff] [blame] | 51 | /// Application specific node ID. |
| 52 | pub node_id: MacAddr6, |
Yi Kong | e3aab14 | 2021-03-02 13:58:25 +0800 | [diff] [blame] | 53 | /// Device build fingerprint. |
| 54 | pub build_fingerprint: String, |
| 55 | /// Interval between collections. |
| 56 | pub collection_interval: Duration, |
| 57 | /// Length of time each collection lasts for. |
| 58 | pub sampling_period: Duration, |
| 59 | /// An optional filter to limit which binaries to or not to profile. |
| 60 | pub binary_filter: String, |
Yi Kong | 581aa3a | 2021-11-24 00:19:30 +0800 | [diff] [blame] | 61 | /// Maximum size of the trace directory. |
| 62 | pub max_trace_limit: u64, |
Yi Kong | e3aab14 | 2021-03-02 13:58:25 +0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | impl Config { |
| 66 | pub fn from_env() -> Result<Self> { |
| 67 | Ok(Config { |
| 68 | version: 1, |
Yi Kong | 037bde8 | 2021-03-23 14:25:38 +0800 | [diff] [blame] | 69 | node_id: get_or_initialise_node_id()?, |
| 70 | build_fingerprint: get_build_fingerprint()?, |
Yi Kong | e3aab14 | 2021-03-02 13:58:25 +0800 | [diff] [blame] | 71 | collection_interval: Duration::from_secs(get_device_config( |
| 72 | "collection_interval", |
| 73 | 600, |
| 74 | )?), |
| 75 | sampling_period: Duration::from_millis(get_device_config("sampling_period", 500)?), |
Yi Kong | 87d0a17 | 2021-12-09 01:37:57 +0800 | [diff] [blame] | 76 | binary_filter: get_device_config("binary_filter", DEFAULT_BINARY_FILTER.to_string())?, |
Yi Kong | 581aa3a | 2021-11-24 00:19:30 +0800 | [diff] [blame] | 77 | max_trace_limit: get_device_config( |
| 78 | "max_trace_limit", |
| 79 | /* 512MB */ 512 * 1024 * 1024, |
| 80 | )?, |
Yi Kong | e3aab14 | 2021-03-02 13:58:25 +0800 | [diff] [blame] | 81 | }) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | impl ToString for Config { |
| 86 | fn to_string(&self) -> String { |
| 87 | serde_json::to_string(self).expect("Failed to deserialise configuration.") |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | impl FromStr for Config { |
| 92 | type Err = serde_json::Error; |
| 93 | fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 94 | serde_json::from_str::<Config>(s) |
| 95 | } |
| 96 | } |
| 97 | |
Yi Kong | 037bde8 | 2021-03-23 14:25:38 +0800 | [diff] [blame] | 98 | fn get_or_initialise_node_id() -> Result<MacAddr6> { |
Chris Wailes | 8f571e1 | 2021-07-27 16:04:09 -0700 | [diff] [blame] | 99 | let mut node_id = get_property(PROFCOLLECT_NODE_ID_PROPERTY, MacAddr6::nil())?; |
Yi Kong | 037bde8 | 2021-03-23 14:25:38 +0800 | [diff] [blame] | 100 | if node_id.is_nil() { |
| 101 | node_id = generate_random_node_id(); |
Chris Wailes | 8f571e1 | 2021-07-27 16:04:09 -0700 | [diff] [blame] | 102 | set_property(PROFCOLLECT_NODE_ID_PROPERTY, node_id)?; |
Yi Kong | 037bde8 | 2021-03-23 14:25:38 +0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | Ok(node_id) |
Yi Kong | e3aab14 | 2021-03-02 13:58:25 +0800 | [diff] [blame] | 106 | } |
| 107 | |
Yi Kong | 037bde8 | 2021-03-23 14:25:38 +0800 | [diff] [blame] | 108 | fn get_build_fingerprint() -> Result<String> { |
| 109 | get_property("ro.build.fingerprint", "unknown".to_string()) |
| 110 | } |
| 111 | |
| 112 | fn get_device_config<T>(key: &str, default_value: T) -> Result<T> |
Yi Kong | e3aab14 | 2021-03-02 13:58:25 +0800 | [diff] [blame] | 113 | where |
| 114 | T: FromStr + ToString, |
| 115 | T::Err: Error + Send + Sync + 'static, |
| 116 | { |
Yi Kong | 037bde8 | 2021-03-23 14:25:38 +0800 | [diff] [blame] | 117 | let default_value = default_value.to_string(); |
Dennis Shen | f419545 | 2023-05-30 14:55:44 +0000 | [diff] [blame] | 118 | let config = flags_rust::GetServerConfigurableFlag( |
Chris Wailes | 8f571e1 | 2021-07-27 16:04:09 -0700 | [diff] [blame] | 119 | PROFCOLLECT_CONFIG_NAMESPACE, |
| 120 | key, |
Yi Kong | 037bde8 | 2021-03-23 14:25:38 +0800 | [diff] [blame] | 121 | &default_value, |
Yi Kong | e3aab14 | 2021-03-02 13:58:25 +0800 | [diff] [blame] | 122 | ); |
| 123 | Ok(T::from_str(&config)?) |
| 124 | } |
Yi Kong | 037bde8 | 2021-03-23 14:25:38 +0800 | [diff] [blame] | 125 | |
| 126 | fn get_property<T>(key: &str, default_value: T) -> Result<T> |
| 127 | where |
| 128 | T: FromStr + ToString, |
| 129 | T::Err: Error + Send + Sync + 'static, |
| 130 | { |
| 131 | let default_value = default_value.to_string(); |
Andrew Walbran | 0aaa3a2 | 2022-02-07 12:36:22 +0000 | [diff] [blame] | 132 | let value = rustutils::system_properties::read(key).unwrap_or(None).unwrap_or(default_value); |
Yi Kong | 037bde8 | 2021-03-23 14:25:38 +0800 | [diff] [blame] | 133 | Ok(T::from_str(&value)?) |
| 134 | } |
| 135 | |
Joel Galenson | 0ab9111 | 2021-07-20 14:51:42 -0700 | [diff] [blame] | 136 | fn set_property<T>(key: &str, value: T) -> Result<()> |
Yi Kong | 037bde8 | 2021-03-23 14:25:38 +0800 | [diff] [blame] | 137 | where |
| 138 | T: ToString, |
| 139 | { |
| 140 | let value = value.to_string(); |
Andrew Walbran | 0aaa3a2 | 2022-02-07 12:36:22 +0000 | [diff] [blame] | 141 | Ok(rustutils::system_properties::write(key, &value)?) |
Yi Kong | 037bde8 | 2021-03-23 14:25:38 +0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | fn generate_random_node_id() -> MacAddr6 { |
| 145 | let mut node_id = rand::thread_rng().gen::<[u8; 6]>(); |
| 146 | node_id[0] |= 0x1; |
| 147 | MacAddr6::from(node_id) |
| 148 | } |
Yi Kong | 34ebf87 | 2021-11-29 19:57:55 +0800 | [diff] [blame] | 149 | |
| 150 | pub fn clear_data() -> Result<()> { |
| 151 | fn remove_files(path: &Path) -> Result<()> { |
| 152 | read_dir(path)? |
| 153 | .filter_map(|e| e.ok()) |
| 154 | .map(|e| e.path()) |
Yabin Cui | f1d91d2 | 2023-04-27 12:50:59 -0700 | [diff] [blame] | 155 | .filter(|e| e.is_file() && e != *LOG_FILE) |
Yi Kong | 34ebf87 | 2021-11-29 19:57:55 +0800 | [diff] [blame] | 156 | .try_for_each(remove_file)?; |
| 157 | Ok(()) |
| 158 | } |
| 159 | |
| 160 | remove_files(&TRACE_OUTPUT_DIR)?; |
| 161 | remove_files(&PROFILE_OUTPUT_DIR)?; |
| 162 | remove_files(&REPORT_OUTPUT_DIR)?; |
| 163 | Ok(()) |
| 164 | } |