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 | |
| 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 | |
| 24 | fn path_to_cstr(path: &Path) -> CString { |
| 25 | CString::new(path.to_str().unwrap()).unwrap() |
| 26 | } |
| 27 | |
Yabin Cui | f158a75 | 2022-01-10 15:35:59 -0800 | [diff] [blame] | 28 | /// Returns whether the system has etm driver. ETM driver should be available immediately |
| 29 | /// after boot. |
| 30 | 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. |
| 36 | pub fn has_device_support() -> bool { |
| 37 | unsafe { simpleperf_profcollect_bindgen::HasDeviceSupport() } |
Yi Kong | f8abfdb | 2021-02-18 16:23:37 +0800 | [diff] [blame] | 38 | } |
| 39 | |
Yabin Cui | 4abcd87 | 2021-03-23 15:51:08 -0700 | [diff] [blame] | 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 | |
Yi Kong | f8abfdb | 2021-02-18 16:23:37 +0800 | [diff] [blame] | 50 | /// Trigger an ETM trace event. |
Yabin Cui | 4b46790 | 2023-05-02 15:12:32 -0700 | [diff] [blame] | 51 | pub fn record(trace_file: &Path, duration: &Duration, binary_filter: &str, scope: RecordScope) { |
Yabin Cui | 4abcd87 | 2021-03-23 15:51:08 -0700 | [diff] [blame] | 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 | }; |
Yi Kong | f8abfdb | 2021-02-18 16:23:37 +0800 | [diff] [blame] | 57 | let trace_file = path_to_cstr(trace_file); |
| 58 | let duration = duration.as_secs_f32(); |
Yabin Cui | 4b46790 | 2023-05-02 15:12:32 -0700 | [diff] [blame] | 59 | let binary_filter = CString::new(binary_filter).unwrap(); |
Yi Kong | f8abfdb | 2021-02-18 16:23:37 +0800 | [diff] [blame] | 60 | |
| 61 | unsafe { |
Yabin Cui | 4b46790 | 2023-05-02 15:12:32 -0700 | [diff] [blame] | 62 | simpleperf_profcollect_bindgen::Record( |
| 63 | event_name.as_ptr(), |
| 64 | trace_file.as_ptr(), |
| 65 | duration, |
| 66 | binary_filter.as_ptr(), |
| 67 | ); |
Yi Kong | f8abfdb | 2021-02-18 16:23:37 +0800 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
| 71 | /// Translate ETM trace to profile. |
Yi Kong | 49eb6ff | 2021-11-17 00:09:02 +0800 | [diff] [blame] | 72 | pub fn process(trace_path: &Path, profile_path: &Path, binary_filter: &str) { |
Yi Kong | f8abfdb | 2021-02-18 16:23:37 +0800 | [diff] [blame] | 73 | let trace_path = path_to_cstr(trace_path); |
| 74 | let profile_path = path_to_cstr(profile_path); |
Yi Kong | 49eb6ff | 2021-11-17 00:09:02 +0800 | [diff] [blame] | 75 | let binary_filter = CString::new(binary_filter).unwrap(); |
Yi Kong | f8abfdb | 2021-02-18 16:23:37 +0800 | [diff] [blame] | 76 | |
| 77 | unsafe { |
Yi Kong | 49eb6ff | 2021-11-17 00:09:02 +0800 | [diff] [blame] | 78 | simpleperf_profcollect_bindgen::Inject( |
| 79 | trace_path.as_ptr(), |
| 80 | profile_path.as_ptr(), |
| 81 | binary_filter.as_ptr(), |
| 82 | ); |
Yi Kong | f8abfdb | 2021-02-18 16:23:37 +0800 | [diff] [blame] | 83 | } |
| 84 | } |
Yabin Cui | 9eb3093 | 2023-04-27 12:50:59 -0700 | [diff] [blame] | 85 | |
| 86 | /// Save logs in file. |
| 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. |
| 95 | pub fn reset_log_file() { |
| 96 | unsafe { |
| 97 | simpleperf_profcollect_bindgen::ResetLogFile(); |
| 98 | } |
| 99 | } |