1 //
2 // Copyright (C) 2021 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 //! This module implements safe wrappers for simpleperf etm operations required
18 //! by profcollect.
19 
20 use std::ffi::CString;
21 use std::path::Path;
22 use std::time::Duration;
23 
path_to_cstr(path: &Path) -> CString24 fn path_to_cstr(path: &Path) -> CString {
25     CString::new(path.to_str().unwrap()).unwrap()
26 }
27 
28 /// Returns whether the system has support for simpleperf etm.
has_support() -> bool29 pub fn has_support() -> bool {
30     unsafe { simpleperf_profcollect_bindgen::HasSupport() }
31 }
32 
33 /// ETM recording scope
34 pub enum RecordScope {
35     /// Record etm data only for userspace.
36     USERSPACE,
37     /// Record etm data only for kernel.
38     KERNEL,
39     /// Record etm data for both userspace and kernel.
40     BOTH,
41 }
42 
43 /// Trigger an ETM trace event.
record(trace_file: &Path, duration: &Duration, scope: RecordScope)44 pub fn record(trace_file: &Path, duration: &Duration, scope: RecordScope) {
45     let event_name: CString = match scope {
46         RecordScope::USERSPACE => CString::new("cs-etm:u").unwrap(),
47         RecordScope::KERNEL => CString::new("cs-etm:k").unwrap(),
48         RecordScope::BOTH => CString::new("cs-etm").unwrap(),
49     };
50     let trace_file = path_to_cstr(trace_file);
51     let duration = duration.as_secs_f32();
52 
53     unsafe {
54         simpleperf_profcollect_bindgen::Record(event_name.as_ptr(), trace_file.as_ptr(), duration);
55     }
56 }
57 
58 /// Translate ETM trace to profile.
process(trace_path: &Path, profile_path: &Path)59 pub fn process(trace_path: &Path, profile_path: &Path) {
60     let trace_path = path_to_cstr(trace_path);
61     let profile_path = path_to_cstr(profile_path);
62 
63     unsafe {
64         simpleperf_profcollect_bindgen::Inject(trace_path.as_ptr(), profile_path.as_ptr());
65     }
66 }
67