1 // Copyright (C) 2023 The Android Open Source Project 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 //! Utilities to benchmark ATrace in Rust. 16 17 use criterion::Criterion; 18 19 // We could use bindgen to generate these bindings automatically but the signatures are simple and 20 // we don't expect them to change much (if at all). So we specify them manually and skip having an 21 // intermediate target. 22 extern "C" { disable_app_atrace()23 fn disable_app_atrace(); enable_atrace_for_single_app(name: *const std::os::raw::c_char)24 fn enable_atrace_for_single_app(name: *const std::os::raw::c_char); 25 } 26 27 /// Disables ATrace for all apps (ATRACE_TAG_APP). turn_tracing_off()28pub fn turn_tracing_off() { 29 // SAFETY: This call is always safe. 30 unsafe { 31 disable_app_atrace(); 32 } 33 } 34 35 /// Enables ATrace for this app. turn_tracing_on()36pub fn turn_tracing_on() { 37 // ATrace uses command line for per-process tracing control, so env::current_exe won't work. 38 let procname = std::ffi::CString::new(std::env::args().next().unwrap()).unwrap(); 39 // SAFETY: `procname` is a valid C string and the function doesn't store it after it returns. 40 unsafe { 41 enable_atrace_for_single_app(procname.as_ptr()); 42 } 43 } 44 45 /// Creates a new configured instance of Criterion for benchmarking. new_criterion() -> Criterion46pub fn new_criterion() -> Criterion { 47 let path = "/data/local/tmp/criterion/benchmarks"; 48 std::fs::create_dir_all(path).unwrap_or_else(|e| { 49 panic!("The criterion folder should be possible to create at {}: {}", path, e) 50 }); 51 std::env::set_var("CRITERION_HOME", path); 52 Criterion::default() 53 } 54