1b0e7dd80Sopenharmony_ci/* 2b0e7dd80Sopenharmony_ci * Copyright (C) 2023 Huawei Device Co., Ltd. 3b0e7dd80Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4b0e7dd80Sopenharmony_ci * you may not use this file except in compliance with the License. 5b0e7dd80Sopenharmony_ci * You may obtain a copy of the License at 6b0e7dd80Sopenharmony_ci * 7b0e7dd80Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8b0e7dd80Sopenharmony_ci * 9b0e7dd80Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10b0e7dd80Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11b0e7dd80Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12b0e7dd80Sopenharmony_ci * See the License for the specific language governing permissions and 13b0e7dd80Sopenharmony_ci * limitations under the License. 14b0e7dd80Sopenharmony_ci */ 15b0e7dd80Sopenharmony_ci 16b0e7dd80Sopenharmony_ci//! hitrace_meter dylib_create for rust. 17b0e7dd80Sopenharmony_ciuse std::ffi::{CString, c_char, c_int, c_longlong, c_ulonglong}; 18b0e7dd80Sopenharmony_ci 19b0e7dd80Sopenharmony_ci/// Track the beginning of a context 20b0e7dd80Sopenharmony_cipub fn start_trace(label: u64, value: &str) { 21b0e7dd80Sopenharmony_ci let value_raw_ptr = CString::new(value).unwrap(); 22b0e7dd80Sopenharmony_ci // Safty: call C ffi border function, all risks are under control. 23b0e7dd80Sopenharmony_ci unsafe { 24b0e7dd80Sopenharmony_ci StartTraceWrapper(label, value_raw_ptr.as_ptr() as *const c_char); 25b0e7dd80Sopenharmony_ci } 26b0e7dd80Sopenharmony_ci} 27b0e7dd80Sopenharmony_ci 28b0e7dd80Sopenharmony_ci/// Track the end of a context 29b0e7dd80Sopenharmony_cipub fn finish_trace(label: u64) { 30b0e7dd80Sopenharmony_ci // Safty: call C ffi border function, all risks are under control. 31b0e7dd80Sopenharmony_ci unsafe { 32b0e7dd80Sopenharmony_ci FinishTrace(label); 33b0e7dd80Sopenharmony_ci } 34b0e7dd80Sopenharmony_ci} 35b0e7dd80Sopenharmony_ci 36b0e7dd80Sopenharmony_ci/// Track the beginning of an asynchronous event 37b0e7dd80Sopenharmony_cipub fn start_trace_async(label: u64, value: &str, task_id: i32) { 38b0e7dd80Sopenharmony_ci let value_raw_ptr = CString::new(value).unwrap(); 39b0e7dd80Sopenharmony_ci // Safty: call C ffi border function, all risks are under control. 40b0e7dd80Sopenharmony_ci unsafe { 41b0e7dd80Sopenharmony_ci StartAsyncTraceWrapper(label, value_raw_ptr.as_ptr() as *const c_char, task_id); 42b0e7dd80Sopenharmony_ci } 43b0e7dd80Sopenharmony_ci} 44b0e7dd80Sopenharmony_ci 45b0e7dd80Sopenharmony_ci/// Track the end of an asynchronous event 46b0e7dd80Sopenharmony_cipub fn finish_trace_async(label: u64, value: &str, task_id: i32) { 47b0e7dd80Sopenharmony_ci let value_raw_ptr = CString::new(value).unwrap(); 48b0e7dd80Sopenharmony_ci // Safty: call C ffi border function, all risks are under control. 49b0e7dd80Sopenharmony_ci unsafe { 50b0e7dd80Sopenharmony_ci FinishAsyncTraceWrapper(label, value_raw_ptr.as_ptr() as *const c_char, task_id); 51b0e7dd80Sopenharmony_ci } 52b0e7dd80Sopenharmony_ci} 53b0e7dd80Sopenharmony_ci 54b0e7dd80Sopenharmony_ci/// Track the 64-bit integer counter value 55b0e7dd80Sopenharmony_cipub fn count_trace(label: u64, name: &str, count: i64) { 56b0e7dd80Sopenharmony_ci let name_raw_ptr = CString::new(name).unwrap(); 57b0e7dd80Sopenharmony_ci // Safty: call C ffi border function, all risks are under control. 58b0e7dd80Sopenharmony_ci unsafe { 59b0e7dd80Sopenharmony_ci CountTraceWrapper(label, name_raw_ptr.as_ptr() as *const c_char, count); 60b0e7dd80Sopenharmony_ci } 61b0e7dd80Sopenharmony_ci} 62b0e7dd80Sopenharmony_ci 63b0e7dd80Sopenharmony_ciextern "C" { 64b0e7dd80Sopenharmony_ci /// ffi border function -> start trace 65b0e7dd80Sopenharmony_ci pub(crate) fn StartTraceWrapper(label: c_ulonglong, value: *const c_char); 66b0e7dd80Sopenharmony_ci 67b0e7dd80Sopenharmony_ci /// ffi border function -> finish trace 68b0e7dd80Sopenharmony_ci pub(crate) fn FinishTrace(label: c_ulonglong); 69b0e7dd80Sopenharmony_ci 70b0e7dd80Sopenharmony_ci /// ffi border function -> start async trace 71b0e7dd80Sopenharmony_ci pub(crate) fn StartAsyncTraceWrapper(label: c_ulonglong, value: *const c_char, taskId: c_int); 72b0e7dd80Sopenharmony_ci 73b0e7dd80Sopenharmony_ci /// ffi border function -> finish async trace 74b0e7dd80Sopenharmony_ci pub(crate) fn FinishAsyncTraceWrapper(label: c_ulonglong, value: *const c_char, taskId: c_int); 75b0e7dd80Sopenharmony_ci 76b0e7dd80Sopenharmony_ci /// ffi border function -> count trace 77b0e7dd80Sopenharmony_ci pub(crate) fn CountTraceWrapper(label: c_ulonglong, name: *const c_char, count: c_longlong); 78b0e7dd80Sopenharmony_ci} 79