summaryrefslogtreecommitdiff
path: root/profcollectd/libprofcollectd/trace_provider.rs
blob: 1305919918c91538b9f261600dc51a0eea305ff5 (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
//
// Copyright (C) 2021 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 trace provider trait and helper functions.

use anyhow::{anyhow, Result};
use chrono::Utc;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::time::Duration;

use crate::simpleperf_etm_trace_provider::SimpleperfEtmTraceProvider;

#[cfg(feature = "test")]
use crate::logging_trace_provider::LoggingTraceProvider;

pub trait TraceProvider {
    fn get_name(&self) -> &'static str;
    fn is_ready(&self) -> bool;
    fn trace(&self, trace_dir: &Path, tag: &str, sampling_period: &Duration);
    fn process(&self, trace_dir: &Path, profile_dir: &Path, binary_filter: &str) -> Result<()>;
}

pub fn get_trace_provider() -> Result<Arc<Mutex<dyn TraceProvider + Send>>> {
    if SimpleperfEtmTraceProvider::supported() {
        log::info!("simpleperf_etm trace provider registered.");
        return Ok(Arc::new(Mutex::new(SimpleperfEtmTraceProvider {})));
    }

    #[cfg(feature = "test")]
    if LoggingTraceProvider::supported() {
        log::info!("logging trace provider registered.");
        return Ok(Arc::new(Mutex::new(LoggingTraceProvider {})));
    }

    Err(anyhow!("No trace provider found for this device."))
}

pub fn get_path(dir: &Path, tag: &str, ext: &str) -> Box<Path> {
    let filename = format!("{}_{}", Utc::now().format("%Y%m%d-%H%M%S"), tag);
    let mut trace_file = PathBuf::from(dir);
    trace_file.push(filename);
    trace_file.set_extension(ext);
    trace_file.into_boxed_path()
}