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