summaryrefslogtreecommitdiff
path: root/profcollectd/libprofcollectd/lib.rs
blob: f95132f4da00690240f4bdf936a30256ad32ff25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//
// Copyright (C) 2020 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

//! ProfCollect Binder client interface.

mod config;
mod report;
mod scheduler;
mod service;
mod simpleperf_etm_trace_provider;
mod trace_provider;

#[cfg(feature = "test")]
mod logging_trace_provider;

use anyhow::{Context, Result};
use profcollectd_aidl_interface::aidl::com::android::server::profcollect::IProfCollectd::{
    self, BnProfCollectd,
};
use profcollectd_aidl_interface::aidl::com::android::server::profcollect::IProviderStatusCallback::{IProviderStatusCallback, BnProviderStatusCallback};
use profcollectd_aidl_interface::binder::{self, BinderFeatures};
use service::{err_to_binder_status, ProfcollectdBinderService};
use std::time::{Duration, Instant};

const PROFCOLLECTD_SERVICE_NAME: &str = "profcollectd";

struct ProviderStatusCallback {
    service_start_time: Instant,
}

impl binder::Interface for ProviderStatusCallback {}

impl IProviderStatusCallback for ProviderStatusCallback {
    fn onProviderReady(&self) -> binder::Result<()> {
        // If we have waited too long for the provider to be ready, then we have passed
        // boot phase, and no need to collect boot profile.
        // TODO: should we check boottime instead?
        const TIMEOUT_TO_COLLECT_BOOT_PROFILE: Duration = Duration::from_secs(3);
        let elapsed = Instant::now().duration_since(self.service_start_time);
        if elapsed < TIMEOUT_TO_COLLECT_BOOT_PROFILE {
            trace_once("boot").map_err(err_to_binder_status)?;
        }
        schedule().map_err(err_to_binder_status)?;
        Ok(())
    }
}

/// Initialise profcollectd service.
/// * `schedule_now` - Immediately schedule collection after service is initialised.
pub fn init_service(schedule_now: bool) -> Result<()> {
    binder::ProcessState::start_thread_pool();

    let profcollect_binder_service = ProfcollectdBinderService::new()?;
    binder::add_service(
        PROFCOLLECTD_SERVICE_NAME,
        BnProfCollectd::new_binder(profcollect_binder_service, BinderFeatures::default())
            .as_binder(),
    )
    .context("Failed to register service.")?;

    if schedule_now {
        let cb = BnProviderStatusCallback::new_binder(
            ProviderStatusCallback { service_start_time: Instant::now() },
            BinderFeatures::default(),
        );
        get_profcollectd_service()?.registerProviderStatusCallback(&cb)?;
    }

    binder::ProcessState::join_thread_pool();
    Ok(())
}

fn get_profcollectd_service() -> Result<binder::Strong<dyn IProfCollectd::IProfCollectd>> {
    binder::get_interface(PROFCOLLECTD_SERVICE_NAME)
        .context("Failed to get profcollectd binder service, is profcollectd running?")
}

/// Schedule periodic profile collection.
pub fn schedule() -> Result<()> {
    get_profcollectd_service()?.schedule()?;
    Ok(())
}

/// Terminate periodic profile collection.
pub fn terminate() -> Result<()> {
    get_profcollectd_service()?.terminate()?;
    Ok(())
}

/// Immediately schedule a one-off trace.
pub fn trace_once(tag: &str) -> Result<()> {
    get_profcollectd_service()?.trace_once(tag)?;
    Ok(())
}

/// Process traces.
pub fn process() -> Result<()> {
    get_profcollectd_service()?.process()?;
    Ok(())
}

/// Process traces and report profile.
pub fn report() -> Result<String> {
    Ok(get_profcollectd_service()?.report()?)
}

/// Clear all local data.
pub fn reset() -> Result<()> {
    config::clear_data()?;
    Ok(())
}

/// Inits logging for Android
pub fn init_logging() {
    let min_log_level = if cfg!(feature = "test") { log::Level::Info } else { log::Level::Error };
    android_logger::init_once(
        android_logger::Config::default().with_tag("profcollectd").with_min_level(min_log_level),
    );
}