blob: 94ad879599ec27ac3ddbf518468a0a8b095add12 [file] [log] [blame]
Yi Kongf8abfdb2021-02-18 16:23:37 +08001//
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
20use std::ffi::CString;
21use std::path::Path;
22use std::time::Duration;
23
24fn path_to_cstr(path: &Path) -> CString {
25 CString::new(path.to_str().unwrap()).unwrap()
26}
27
Yabin Cuif158a752022-01-10 15:35:59 -080028/// Returns whether the system has etm driver. ETM driver should be available immediately
29/// after boot.
30pub 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.
36pub fn has_device_support() -> bool {
37 unsafe { simpleperf_profcollect_bindgen::HasDeviceSupport() }
Yi Kongf8abfdb2021-02-18 16:23:37 +080038}
39
Yabin Cui4abcd872021-03-23 15:51:08 -070040/// ETM recording scope
41pub 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
Yi Kongf8abfdb2021-02-18 16:23:37 +080050/// Trigger an ETM trace event.
Yabin Cui4b467902023-05-02 15:12:32 -070051pub fn record(trace_file: &Path, duration: &Duration, binary_filter: &str, scope: RecordScope) {
Yabin Cui4abcd872021-03-23 15:51:08 -070052 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 };
Yi Kongf8abfdb2021-02-18 16:23:37 +080057 let trace_file = path_to_cstr(trace_file);
58 let duration = duration.as_secs_f32();
Yabin Cui4b467902023-05-02 15:12:32 -070059 let binary_filter = CString::new(binary_filter).unwrap();
Yi Kongf8abfdb2021-02-18 16:23:37 +080060
61 unsafe {
Yabin Cui4b467902023-05-02 15:12:32 -070062 simpleperf_profcollect_bindgen::Record(
63 event_name.as_ptr(),
64 trace_file.as_ptr(),
65 duration,
66 binary_filter.as_ptr(),
67 );
Yi Kongf8abfdb2021-02-18 16:23:37 +080068 }
69}
70
71/// Translate ETM trace to profile.
Yi Kong49eb6ff2021-11-17 00:09:02 +080072pub fn process(trace_path: &Path, profile_path: &Path, binary_filter: &str) {
Yi Kongf8abfdb2021-02-18 16:23:37 +080073 let trace_path = path_to_cstr(trace_path);
74 let profile_path = path_to_cstr(profile_path);
Yi Kong49eb6ff2021-11-17 00:09:02 +080075 let binary_filter = CString::new(binary_filter).unwrap();
Yi Kongf8abfdb2021-02-18 16:23:37 +080076
77 unsafe {
Yi Kong49eb6ff2021-11-17 00:09:02 +080078 simpleperf_profcollect_bindgen::Inject(
79 trace_path.as_ptr(),
80 profile_path.as_ptr(),
81 binary_filter.as_ptr(),
82 );
Yi Kongf8abfdb2021-02-18 16:23:37 +080083 }
84}
Yabin Cui9eb30932023-04-27 12:50:59 -070085
86/// Save logs in file.
87pub 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.
95pub fn reset_log_file() {
96 unsafe {
97 simpleperf_profcollect_bindgen::ResetLogFile();
98 }
99}