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