1 //
2 // Copyright (C) 2020 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 client interface.
18 
19 mod config;
20 mod report;
21 mod scheduler;
22 mod service;
23 mod simpleperf_etm_trace_provider;
24 mod simpleperf_lbr_trace_provider;
25 mod trace_provider;
26 
27 #[cfg(feature = "test")]
28 mod logging_trace_provider;
29 
30 use anyhow::{Context, Result};
31 use profcollectd_aidl_interface::aidl::com::android::server::profcollect::IProfCollectd::{
32     self, BnProfCollectd,
33 };
34 use profcollectd_aidl_interface::aidl::com::android::server::profcollect::IProviderStatusCallback::{IProviderStatusCallback, BnProviderStatusCallback};
35 use profcollectd_aidl_interface::binder::{self, BinderFeatures};
36 use service::{err_to_binder_status, ProfcollectdBinderService};
37 use std::time::{Duration, Instant};
38 
39 const PROFCOLLECTD_SERVICE_NAME: &str = "profcollectd";
40 
41 struct ProviderStatusCallback {
42     service_start_time: Instant,
43 }
44 
45 impl binder::Interface for ProviderStatusCallback {}
46 
47 impl IProviderStatusCallback for ProviderStatusCallback {
onProviderReady(&self) -> binder::Result<()>48     fn onProviderReady(&self) -> binder::Result<()> {
49         // If we have waited too long for the provider to be ready, then we have passed
50         // boot phase, and no need to collect boot profile.
51         // TODO: should we check boottime instead?
52         const TIMEOUT_TO_COLLECT_BOOT_PROFILE: Duration = Duration::from_secs(3);
53         let elapsed = Instant::now().duration_since(self.service_start_time);
54         if elapsed < TIMEOUT_TO_COLLECT_BOOT_PROFILE {
55             trace_once("boot").map_err(err_to_binder_status)?;
56         }
57         schedule().map_err(err_to_binder_status)?;
58         Ok(())
59     }
60 }
61 
62 /// Initialise profcollectd service.
63 /// * `schedule_now` - Immediately schedule collection after service is initialised.
init_service(schedule_now: bool) -> Result<()>64 pub fn init_service(schedule_now: bool) -> Result<()> {
65     binder::ProcessState::start_thread_pool();
66 
67     let profcollect_binder_service = ProfcollectdBinderService::new()?;
68     binder::add_service(
69         PROFCOLLECTD_SERVICE_NAME,
70         BnProfCollectd::new_binder(profcollect_binder_service, BinderFeatures::default())
71             .as_binder(),
72     )
73     .context("Failed to register service.")?;
74 
75     if schedule_now {
76         let cb = BnProviderStatusCallback::new_binder(
77             ProviderStatusCallback { service_start_time: Instant::now() },
78             BinderFeatures::default(),
79         );
80         get_profcollectd_service()?.registerProviderStatusCallback(&cb)?;
81     }
82 
83     binder::ProcessState::join_thread_pool();
84     Ok(())
85 }
86 
get_profcollectd_service() -> Result<binder::Strong<dyn IProfCollectd::IProfCollectd>>87 fn get_profcollectd_service() -> Result<binder::Strong<dyn IProfCollectd::IProfCollectd>> {
88     binder::wait_for_interface(PROFCOLLECTD_SERVICE_NAME)
89         .context("Failed to get profcollectd binder service, is profcollectd running?")
90 }
91 
92 /// Schedule periodic profile collection.
schedule() -> Result<()>93 pub fn schedule() -> Result<()> {
94     get_profcollectd_service()?.schedule()?;
95     Ok(())
96 }
97 
98 /// Terminate periodic profile collection.
terminate() -> Result<()>99 pub fn terminate() -> Result<()> {
100     get_profcollectd_service()?.terminate()?;
101     Ok(())
102 }
103 
104 /// Immediately schedule a one-off trace.
trace_once(tag: &str) -> Result<()>105 pub fn trace_once(tag: &str) -> Result<()> {
106     get_profcollectd_service()?.trace_once(tag)?;
107     Ok(())
108 }
109 
110 /// Process traces.
process() -> Result<()>111 pub fn process() -> Result<()> {
112     get_profcollectd_service()?.process()?;
113     Ok(())
114 }
115 
116 /// Process traces and report profile.
report() -> Result<String>117 pub fn report() -> Result<String> {
118     Ok(get_profcollectd_service()?.report(report::NO_USAGE_SETTING)?)
119 }
120 
121 /// Clear all local data.
reset() -> Result<()>122 pub fn reset() -> Result<()> {
123     config::clear_data()?;
124     Ok(())
125 }
126 
127 /// Inits logging for Android
init_logging()128 pub fn init_logging() {
129     let max_log_level =
130         if cfg!(feature = "test") { log::LevelFilter::Info } else { log::LevelFilter::Error };
131     android_logger::init_once(
132         android_logger::Config::default()
133             .with_tag("profcollectd")
134             .with_max_level(max_log_level)
135             .with_log_buffer(android_logger::LogId::System),
136     );
137 }
138