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 schedule().map_err(err_to_binder_status)?;
53 Ok(())
54 }
55 }
56
57 /// Initialise profcollectd service.
58 /// * `schedule_now` - Immediately schedule collection after service is initialised.
init_service(schedule_now: bool) -> Result<()>59 pub fn init_service(schedule_now: bool) -> Result<()> {
60 binder::ProcessState::start_thread_pool();
61
62 let profcollect_binder_service = ProfcollectdBinderService::new()?;
63 binder::add_service(
64 PROFCOLLECTD_SERVICE_NAME,
65 BnProfCollectd::new_binder(profcollect_binder_service, BinderFeatures::default())
66 .as_binder(),
67 )
68 .context("Failed to register service.")?;
69
70 if schedule_now {
71 let cb = BnProviderStatusCallback::new_binder(
72 ProviderStatusCallback { service_start_time: Instant::now() },
73 BinderFeatures::default(),
74 );
75 get_profcollectd_service()?.registerProviderStatusCallback(&cb)?;
76 }
77
78 binder::ProcessState::join_thread_pool();
79 Ok(())
80 }
81
get_profcollectd_service() -> Result<binder::Strong<dyn IProfCollectd::IProfCollectd>>82 fn get_profcollectd_service() -> Result<binder::Strong<dyn IProfCollectd::IProfCollectd>> {
83 binder::wait_for_interface(PROFCOLLECTD_SERVICE_NAME)
84 .context("Failed to get profcollectd binder service, is profcollectd running?")
85 }
86
87 /// Schedule periodic profile collection.
schedule() -> Result<()>88 pub fn schedule() -> Result<()> {
89 get_profcollectd_service()?.schedule()?;
90 Ok(())
91 }
92
93 /// Terminate periodic profile collection.
terminate() -> Result<()>94 pub fn terminate() -> Result<()> {
95 get_profcollectd_service()?.terminate()?;
96 Ok(())
97 }
98
99 /// Immediately schedule a one-off trace.
trace_system(tag: &str) -> Result<()>100 pub fn trace_system(tag: &str) -> Result<()> {
101 get_profcollectd_service()?.trace_system(tag)?;
102 Ok(())
103 }
104
105 /// Process traces.
process() -> Result<()>106 pub fn process() -> Result<()> {
107 get_profcollectd_service()?.process()?;
108 Ok(())
109 }
110
111 /// Process traces and report profile.
report() -> Result<String>112 pub fn report() -> Result<String> {
113 Ok(get_profcollectd_service()?.report(report::NO_USAGE_SETTING)?)
114 }
115
116 /// Clear all local data.
reset() -> Result<()>117 pub fn reset() -> Result<()> {
118 config::clear_data()?;
119 Ok(())
120 }
121
122 /// Inits logging for Android
init_logging()123 pub fn init_logging() {
124 let max_log_level =
125 if cfg!(feature = "test") { log::LevelFilter::Info } else { log::LevelFilter::Error };
126 android_logger::init_once(
127 android_logger::Config::default()
128 .with_tag("profcollectd")
129 .with_max_level(max_log_level)
130 .with_log_buffer(android_logger::LogId::System),
131 );
132 }
133