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