blob: 3188888f7c6bfa79ab7f1f2f540df41a85b095c8 [file] [log] [blame]
Yi Konge3aab142021-03-02 13:58:25 +08001//
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 Binder service implementation.
18
Yi Kong6e3fe962021-11-30 14:24:10 +080019use anyhow::{anyhow, Context, Error, Result};
Stephen Crane23e8e532022-01-19 17:49:46 +000020use binder::Result as BinderResult;
Yabin Cuif158a752022-01-10 15:35:59 -080021use binder::{SpIBinder, Status};
Yi Konge3aab142021-03-02 13:58:25 +080022use profcollectd_aidl_interface::aidl::com::android::server::profcollect::IProfCollectd::IProfCollectd;
Yabin Cuif158a752022-01-10 15:35:59 -080023use profcollectd_aidl_interface::aidl::com::android::server::profcollect::IProviderStatusCallback::IProviderStatusCallback;
Yi Konge3aab142021-03-02 13:58:25 +080024use std::ffi::CString;
Yi Kong6e3fe962021-11-30 14:24:10 +080025use std::fs::{read_dir, read_to_string, remove_file, write};
Yi Konge30762a2021-03-24 02:41:49 +080026use std::str::FromStr;
27use std::sync::{Mutex, MutexGuard};
Yi Kong8dffc122021-07-20 16:55:39 +080028use std::time::Duration;
Yi Konge3aab142021-03-02 13:58:25 +080029
Yi Kong4e33c572021-03-04 14:47:48 +080030use crate::config::{
Yi Kong6e3fe962021-11-30 14:24:10 +080031 clear_data, Config, CONFIG_FILE, PROFILE_OUTPUT_DIR, REPORT_OUTPUT_DIR, REPORT_RETENTION_SECS,
Yi Kong4e33c572021-03-04 14:47:48 +080032};
Yi Kong8dffc122021-07-20 16:55:39 +080033use crate::report::{get_report_ts, pack_report};
Yi Konge3aab142021-03-02 13:58:25 +080034use crate::scheduler::Scheduler;
35
Yabin Cuif158a752022-01-10 15:35:59 -080036pub fn err_to_binder_status(msg: Error) -> Status {
Yi Kongbbc9b4e2021-03-29 16:53:03 +080037 let msg = format!("{:#?}", msg);
38 let msg = CString::new(msg).expect("Failed to convert to CString");
Yi Konge3aab142021-03-02 13:58:25 +080039 Status::new_service_specific_error(1, Some(&msg))
40}
41
42pub struct ProfcollectdBinderService {
43 lock: Mutex<Lock>,
44}
45
46struct Lock {
47 config: Config,
48 scheduler: Scheduler,
49}
50
51impl binder::Interface for ProfcollectdBinderService {}
52
53impl IProfCollectd for ProfcollectdBinderService {
54 fn schedule(&self) -> BinderResult<()> {
55 let lock = &mut *self.lock();
56 lock.scheduler
57 .schedule_periodic(&lock.config)
58 .context("Failed to schedule collection.")
59 .map_err(err_to_binder_status)
60 }
61 fn terminate(&self) -> BinderResult<()> {
62 self.lock()
63 .scheduler
64 .terminate_periodic()
65 .context("Failed to terminate collection.")
66 .map_err(err_to_binder_status)
67 }
68 fn trace_once(&self, tag: &str) -> BinderResult<()> {
69 let lock = &mut *self.lock();
70 lock.scheduler
71 .one_shot(&lock.config, tag)
72 .context("Failed to initiate an one-off trace.")
73 .map_err(err_to_binder_status)
74 }
Yi Konge7627422021-11-15 16:20:02 +080075 fn process(&self) -> BinderResult<()> {
Yi Konge3aab142021-03-02 13:58:25 +080076 let lock = &mut *self.lock();
77 lock.scheduler
Yi Kong87d0a172021-12-09 01:37:57 +080078 .process(&lock.config)
Yi Konge3aab142021-03-02 13:58:25 +080079 .context("Failed to process profiles.")
80 .map_err(err_to_binder_status)
81 }
Yabin Cui1a2601f2023-05-11 14:28:57 -070082 fn report(&self, usage_setting: i32) -> BinderResult<String> {
Yi Konge7627422021-11-15 16:20:02 +080083 self.process()?;
Yi Kong037bde82021-03-23 14:25:38 +080084
85 let lock = &mut *self.lock();
Yabin Cui1a2601f2023-05-11 14:28:57 -070086 pack_report(&PROFILE_OUTPUT_DIR, &REPORT_OUTPUT_DIR, &lock.config, usage_setting)
Yi Konge3aab142021-03-02 13:58:25 +080087 .context("Failed to create profile report.")
88 .map_err(err_to_binder_status)
89 }
90 fn get_supported_provider(&self) -> BinderResult<String> {
91 Ok(self.lock().scheduler.get_trace_provider_name().to_string())
92 }
Yabin Cuif158a752022-01-10 15:35:59 -080093
94 fn registerProviderStatusCallback(
95 &self,
96 cb: &binder::Strong<(dyn IProviderStatusCallback)>,
97 ) -> BinderResult<()> {
98 if self.lock().scheduler.is_provider_ready() {
99 if let Err(e) = cb.onProviderReady() {
100 log::error!("Failed to call ProviderStatusCallback {:?}", e);
101 }
102 return Ok(());
103 }
104
105 let cb_binder: SpIBinder = cb.as_binder();
106 self.lock().scheduler.register_provider_ready_callback(Box::new(move || {
107 if let Ok(cb) = cb_binder.into_interface::<dyn IProviderStatusCallback>() {
108 if let Err(e) = cb.onProviderReady() {
109 log::error!("Failed to call ProviderStatusCallback {:?}", e)
110 }
111 } else {
112 log::error!("SpIBinder is not a IProviderStatusCallback.");
113 }
114 }));
115 Ok(())
116 }
Yi Konge3aab142021-03-02 13:58:25 +0800117}
118
119impl ProfcollectdBinderService {
120 pub fn new() -> Result<Self> {
121 let new_scheduler = Scheduler::new()?;
122 let new_config = Config::from_env()?;
123
124 let config_changed = read_to_string(*CONFIG_FILE)
125 .ok()
126 .and_then(|s| Config::from_str(&s).ok())
127 .filter(|c| new_config == *c)
128 .is_none();
129
130 if config_changed {
Yi Kong34ebf872021-11-29 19:57:55 +0800131 log::info!("Config change detected, resetting profcollect.");
132 clear_data()?;
Yi Kong4e33c572021-03-04 14:47:48 +0800133
Chris Wailesc270ca52023-01-19 16:35:09 -0800134 write(*CONFIG_FILE, new_config.to_string())?;
Yabin Cui9eb30932023-04-27 12:50:59 -0700135 new_scheduler.clear_trace_log()?;
Yi Konge3aab142021-03-02 13:58:25 +0800136 }
137
Yi Kong8dffc122021-07-20 16:55:39 +0800138 // Clear profile reports out of rentention period.
139 for report in read_dir(*REPORT_OUTPUT_DIR)? {
140 let report = report?.path();
141 let report_name = report
142 .file_stem()
143 .and_then(|f| f.to_str())
144 .ok_or_else(|| anyhow!("Malformed path {}", report.display()))?;
145 let report_ts = get_report_ts(report_name);
146 if let Err(e) = report_ts {
147 log::error!(
148 "Cannot decode creation timestamp for report {}, caused by {}, deleting",
149 report_name,
150 e
151 );
152 remove_file(report)?;
153 continue;
154 }
155 let report_age = report_ts.unwrap().elapsed()?;
156 if report_age > Duration::from_secs(REPORT_RETENTION_SECS) {
157 log::info!("Report {} past rentention period, deleting", report_name);
158 remove_file(report)?;
159 }
160 }
161
Yi Konge3aab142021-03-02 13:58:25 +0800162 Ok(ProfcollectdBinderService {
163 lock: Mutex::new(Lock { scheduler: new_scheduler, config: new_config }),
164 })
165 }
166
167 fn lock(&self) -> MutexGuard<Lock> {
168 self.lock.lock().unwrap()
169 }
170}