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 etm driver. ETM driver should be available immediately
29 /// after boot.
has_driver_support() -> bool30 pub fn has_driver_support() -> bool {
31 unsafe { simpleperf_profcollect_bindgen::HasDriverSupport() }
32 }
33
34 /// Returns whether the system has etm device. ETM device may not be available immediately
35 /// after boot.
has_device_support() -> bool36 pub fn has_device_support() -> bool {
37 unsafe { simpleperf_profcollect_bindgen::HasDeviceSupport() }
38 }
39
40 /// ETM recording scope
41 pub enum RecordScope {
42 /// Record etm data only for userspace.
43 USERSPACE,
44 /// Record etm data only for kernel.
45 KERNEL,
46 /// Record etm data for both userspace and kernel.
47 BOTH,
48 }
49
50 /// Trigger an ETM trace event.
record(trace_file: &Path, duration: &Duration, binary_filter: &str, scope: RecordScope)51 pub fn record(trace_file: &Path, duration: &Duration, binary_filter: &str, scope: RecordScope) {
52 let event_name: CString = match scope {
53 RecordScope::USERSPACE => CString::new("cs-etm:u").unwrap(),
54 RecordScope::KERNEL => CString::new("cs-etm:k").unwrap(),
55 RecordScope::BOTH => CString::new("cs-etm").unwrap(),
56 };
57 let trace_file = path_to_cstr(trace_file);
58 let duration = duration.as_secs_f32();
59 let binary_filter = CString::new(binary_filter).unwrap();
60
61 unsafe {
62 simpleperf_profcollect_bindgen::Record(
63 event_name.as_ptr(),
64 trace_file.as_ptr(),
65 duration,
66 binary_filter.as_ptr(),
67 );
68 }
69 }
70
71 /// Translate ETM trace to profile.
process(trace_path: &Path, profile_path: &Path, binary_filter: &str)72 pub fn process(trace_path: &Path, profile_path: &Path, binary_filter: &str) {
73 let trace_path = path_to_cstr(trace_path);
74 let profile_path = path_to_cstr(profile_path);
75 let binary_filter = CString::new(binary_filter).unwrap();
76
77 unsafe {
78 simpleperf_profcollect_bindgen::Inject(
79 trace_path.as_ptr(),
80 profile_path.as_ptr(),
81 binary_filter.as_ptr(),
82 );
83 }
84 }
85
86 /// Save logs in file.
set_log_file(filename: &Path)87 pub fn set_log_file(filename: &Path) {
88 let log_file = path_to_cstr(filename);
89 unsafe {
90 simpleperf_profcollect_bindgen::SetLogFile(log_file.as_ptr());
91 }
92 }
93
94 /// Stop using log file.
reset_log_file()95 pub fn reset_log_file() {
96 unsafe {
97 simpleperf_profcollect_bindgen::ResetLogFile();
98 }
99 }
100