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 | 4abcd87 | 2021-03-23 15:51:08 -0700 | [diff] [blame] | 51 | pub fn record(trace_file: &Path, duration: &Duration, 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 | }; |
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(); |
| 59 | |
| 60 | unsafe { |
Yabin Cui | 4abcd87 | 2021-03-23 15:51:08 -0700 | [diff] [blame] | 61 | simpleperf_profcollect_bindgen::Record(event_name.as_ptr(), trace_file.as_ptr(), duration); |
Yi Kong | f8abfdb | 2021-02-18 16:23:37 +0800 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
| 65 | /// Translate ETM trace to profile. |
Yi Kong | 49eb6ff | 2021-11-17 00:09:02 +0800 | [diff] [blame] | 66 | pub fn process(trace_path: &Path, profile_path: &Path, binary_filter: &str) { |
Yi Kong | f8abfdb | 2021-02-18 16:23:37 +0800 | [diff] [blame] | 67 | let trace_path = path_to_cstr(trace_path); |
| 68 | let profile_path = path_to_cstr(profile_path); |
Yi Kong | 49eb6ff | 2021-11-17 00:09:02 +0800 | [diff] [blame] | 69 | let binary_filter = CString::new(binary_filter).unwrap(); |
Yi Kong | f8abfdb | 2021-02-18 16:23:37 +0800 | [diff] [blame] | 70 | |
| 71 | unsafe { |
Yi Kong | 49eb6ff | 2021-11-17 00:09:02 +0800 | [diff] [blame] | 72 | simpleperf_profcollect_bindgen::Inject( |
| 73 | trace_path.as_ptr(), |
| 74 | profile_path.as_ptr(), |
| 75 | binary_filter.as_ptr(), |
| 76 | ); |
Yi Kong | f8abfdb | 2021-02-18 16:23:37 +0800 | [diff] [blame] | 77 | } |
| 78 | } |