Yi Kong | f8abfdb | 2021-02-18 16:23:37 +0800 | [diff] [blame] | 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 | |
Yabin Cui | ffc25f8 | 2023-12-13 11:04:40 -0800 | [diff] [blame] | 17 | //! This module implements safe wrappers for simpleperf operations required |
Yi Kong | f8abfdb | 2021-02-18 16:23:37 +0800 | [diff] [blame] | 18 | //! by profcollect. |
| 19 | |
Yabin Cui | ffc25f8 | 2023-12-13 11:04:40 -0800 | [diff] [blame] | 20 | use std::ffi::{c_char, CString}; |
Yi Kong | f8abfdb | 2021-02-18 16:23:37 +0800 | [diff] [blame] | 21 | use std::path::Path; |
Yi Kong | f8abfdb | 2021-02-18 16:23:37 +0800 | [diff] [blame] | 22 | |
| 23 | fn path_to_cstr(path: &Path) -> CString { |
| 24 | CString::new(path.to_str().unwrap()).unwrap() |
| 25 | } |
| 26 | |
Yabin Cui | f158a75 | 2022-01-10 15:35:59 -0800 | [diff] [blame] | 27 | /// Returns whether the system has etm driver. ETM driver should be available immediately |
| 28 | /// after boot. |
Yabin Cui | ffc25f8 | 2023-12-13 11:04:40 -0800 | [diff] [blame] | 29 | pub fn is_etm_driver_available() -> bool { |
Andrew Walbran | b044aad | 2023-07-14 13:58:29 +0100 | [diff] [blame] | 30 | // SAFETY: This is always safe to call. |
Yabin Cui | ffc25f8 | 2023-12-13 11:04:40 -0800 | [diff] [blame] | 31 | unsafe { simpleperf_profcollect_bindgen::IsETMDriverAvailable() } |
Yabin Cui | f158a75 | 2022-01-10 15:35:59 -0800 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | /// Returns whether the system has etm device. ETM device may not be available immediately |
| 35 | /// after boot. |
Yabin Cui | ffc25f8 | 2023-12-13 11:04:40 -0800 | [diff] [blame] | 36 | pub fn is_etm_device_available() -> bool { |
Andrew Walbran | b044aad | 2023-07-14 13:58:29 +0100 | [diff] [blame] | 37 | // SAFETY: This is always safe to call. |
Yabin Cui | ffc25f8 | 2023-12-13 11:04:40 -0800 | [diff] [blame] | 38 | unsafe { simpleperf_profcollect_bindgen::IsETMDeviceAvailable() } |
Yi Kong | f8abfdb | 2021-02-18 16:23:37 +0800 | [diff] [blame] | 39 | } |
| 40 | |
Yabin Cui | ffc25f8 | 2023-12-13 11:04:40 -0800 | [diff] [blame] | 41 | /// Returns whether the system support LBR recording. |
| 42 | pub fn is_lbr_available() -> bool { |
| 43 | // SAFETY: This is always safe to call. |
| 44 | unsafe { simpleperf_profcollect_bindgen::IsLBRAvailable() } |
Yabin Cui | 4abcd87 | 2021-03-23 15:51:08 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Yabin Cui | ffc25f8 | 2023-12-13 11:04:40 -0800 | [diff] [blame] | 47 | /// Run the record command to record ETM/LBR data. |
| 48 | pub fn run_record_cmd(args: &[&str]) -> bool { |
| 49 | let c_args: Vec<CString> = args.iter().map(|s| CString::new(s.as_bytes()).unwrap()).collect(); |
| 50 | let mut pointer_args: Vec<*const c_char> = c_args.iter().map(|s| s.as_ptr()).collect(); |
| 51 | let arg_count: i32 = pointer_args.len().try_into().unwrap(); |
Yabin Cui | 101d2aa | 2023-12-18 14:42:47 -0800 | [diff] [blame] | 52 | // SAFETY: pointer_args is an array of valid C strings. Its length is defined by arg_count. |
Yabin Cui | ffc25f8 | 2023-12-13 11:04:40 -0800 | [diff] [blame] | 53 | unsafe { simpleperf_profcollect_bindgen::RunRecordCmd(pointer_args.as_mut_ptr(), arg_count) } |
Yi Kong | f8abfdb | 2021-02-18 16:23:37 +0800 | [diff] [blame] | 54 | } |
| 55 | |
Yabin Cui | ffc25f8 | 2023-12-13 11:04:40 -0800 | [diff] [blame] | 56 | /// Run the inject command to process ETM/LBR data. |
| 57 | pub fn run_inject_cmd(args: &[&str]) -> bool { |
| 58 | let c_args: Vec<CString> = args.iter().map(|s| CString::new(s.as_bytes()).unwrap()).collect(); |
| 59 | let mut pointer_args: Vec<*const c_char> = c_args.iter().map(|s| s.as_ptr()).collect(); |
| 60 | let arg_count: i32 = pointer_args.len().try_into().unwrap(); |
Yabin Cui | 101d2aa | 2023-12-18 14:42:47 -0800 | [diff] [blame] | 61 | // SAFETY: pointer_args is an array of valid C strings. Its length is defined by arg_count. |
Yabin Cui | ffc25f8 | 2023-12-13 11:04:40 -0800 | [diff] [blame] | 62 | unsafe { simpleperf_profcollect_bindgen::RunInjectCmd(pointer_args.as_mut_ptr(), arg_count) } |
Yi Kong | f8abfdb | 2021-02-18 16:23:37 +0800 | [diff] [blame] | 63 | } |
Yabin Cui | f1d91d2 | 2023-04-27 12:50:59 -0700 | [diff] [blame] | 64 | |
| 65 | /// Save logs in file. |
| 66 | pub fn set_log_file(filename: &Path) { |
| 67 | let log_file = path_to_cstr(filename); |
Andrew Walbran | b044aad | 2023-07-14 13:58:29 +0100 | [diff] [blame] | 68 | // SAFETY: The pointer is a valid C string, and isn't retained after the function call returns. |
Yabin Cui | f1d91d2 | 2023-04-27 12:50:59 -0700 | [diff] [blame] | 69 | unsafe { |
| 70 | simpleperf_profcollect_bindgen::SetLogFile(log_file.as_ptr()); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /// Stop using log file. |
| 75 | pub fn reset_log_file() { |
Andrew Walbran | b044aad | 2023-07-14 13:58:29 +0100 | [diff] [blame] | 76 | // SAFETY: This is always safe to call. |
Yabin Cui | f1d91d2 | 2023-04-27 12:50:59 -0700 | [diff] [blame] | 77 | unsafe { |
| 78 | simpleperf_profcollect_bindgen::ResetLogFile(); |
| 79 | } |
| 80 | } |