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::binder::{self, BinderFeatures};
34 use service::ProfcollectdBinderService;
35
36 const PROFCOLLECTD_SERVICE_NAME: &str = "profcollectd";
37
38 /// Initialise profcollectd service.
39 /// * `schedule_now` - Immediately schedule collection after service is initialised.
init_service(schedule_now: bool) -> Result<()>40 pub fn init_service(schedule_now: bool) -> Result<()> {
41 binder::ProcessState::start_thread_pool();
42
43 let profcollect_binder_service = ProfcollectdBinderService::new()?;
44 binder::add_service(
45 &PROFCOLLECTD_SERVICE_NAME,
46 BnProfCollectd::new_binder(profcollect_binder_service, BinderFeatures::default())
47 .as_binder(),
48 )
49 .context("Failed to register service.")?;
50
51 if schedule_now {
52 trace_once("boot")?;
53 schedule()?;
54 }
55
56 binder::ProcessState::join_thread_pool();
57 Ok(())
58 }
59
get_profcollectd_service() -> Result<binder::Strong<dyn IProfCollectd::IProfCollectd>>60 fn get_profcollectd_service() -> Result<binder::Strong<dyn IProfCollectd::IProfCollectd>> {
61 binder::get_interface(&PROFCOLLECTD_SERVICE_NAME)
62 .context("Failed to get profcollectd binder service, is profcollectd running?")
63 }
64
65 /// Schedule periodic profile collection.
schedule() -> Result<()>66 pub fn schedule() -> Result<()> {
67 get_profcollectd_service()?.schedule()?;
68 Ok(())
69 }
70
71 /// Terminate periodic profile collection.
terminate() -> Result<()>72 pub fn terminate() -> Result<()> {
73 get_profcollectd_service()?.terminate()?;
74 Ok(())
75 }
76
77 /// Immediately schedule a one-off trace.
trace_once(tag: &str) -> Result<()>78 pub fn trace_once(tag: &str) -> Result<()> {
79 get_profcollectd_service()?.trace_once(tag)?;
80 Ok(())
81 }
82
83 /// Process traces.
process() -> Result<()>84 pub fn process() -> Result<()> {
85 get_profcollectd_service()?.process(true)?;
86 Ok(())
87 }
88
89 /// Process traces and report profile.
report() -> Result<String>90 pub fn report() -> Result<String> {
91 Ok(get_profcollectd_service()?.report()?)
92 }
93
94 /// Clear all local data.
reset() -> Result<()>95 pub fn reset() -> Result<()> {
96 config::clear_data()?;
97 Ok(())
98 }
99
100 /// Inits logging for Android
init_logging()101 pub fn init_logging() {
102 let min_log_level = if cfg!(feature = "test") { log::Level::Info } else { log::Level::Error };
103 android_logger::init_once(
104 android_logger::Config::default().with_tag("profcollectd").with_min_level(min_log_level),
105 );
106 }
107