1/*
2 * Copyright (C) 2023 Huawei Device Co., Ltd.
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
16//! hitrace_meter dylib_create for rust.
17use std::ffi::{CString, c_char, c_int, c_longlong, c_ulonglong};
18
19/// Track the beginning of a context
20pub fn start_trace(label: u64, value: &str) {
21    let value_raw_ptr = CString::new(value).unwrap();
22    // Safty: call C ffi border function, all risks are under control.
23    unsafe {
24        StartTraceWrapper(label, value_raw_ptr.as_ptr() as *const c_char);
25    }
26}
27
28/// Track the end of a context
29pub fn finish_trace(label: u64) {
30    // Safty: call C ffi border function, all risks are under control.
31    unsafe {
32        FinishTrace(label);
33    }
34}
35
36/// Track the beginning of an asynchronous event
37pub fn start_trace_async(label: u64, value: &str, task_id: i32) {
38    let value_raw_ptr = CString::new(value).unwrap();
39    // Safty: call C ffi border function, all risks are under control.
40    unsafe {
41        StartAsyncTraceWrapper(label, value_raw_ptr.as_ptr() as *const c_char, task_id);
42    }
43}
44
45/// Track the end of an asynchronous event
46pub fn finish_trace_async(label: u64, value: &str, task_id: i32) {
47    let value_raw_ptr = CString::new(value).unwrap();
48    // Safty: call C ffi border function, all risks are under control.
49    unsafe {
50        FinishAsyncTraceWrapper(label, value_raw_ptr.as_ptr() as *const c_char, task_id);
51    }
52}
53
54/// Track the 64-bit integer counter value
55pub fn count_trace(label: u64, name: &str, count: i64) {
56    let name_raw_ptr = CString::new(name).unwrap();
57    // Safty: call C ffi border function, all risks are under control.
58    unsafe {
59        CountTraceWrapper(label, name_raw_ptr.as_ptr() as *const c_char, count);
60    }
61}
62
63extern "C" {
64    /// ffi border function -> start trace
65    pub(crate) fn StartTraceWrapper(label: c_ulonglong, value: *const c_char);
66
67    /// ffi border function -> finish trace
68    pub(crate) fn FinishTrace(label: c_ulonglong);
69
70    /// ffi border function -> start async trace
71    pub(crate) fn StartAsyncTraceWrapper(label: c_ulonglong, value: *const c_char, taskId: c_int);
72
73    /// ffi border function -> finish async trace
74    pub(crate) fn FinishAsyncTraceWrapper(label: c_ulonglong, value: *const c_char, taskId: c_int);
75
76    /// ffi border function -> count trace
77    pub(crate) fn CountTraceWrapper(label: c_ulonglong, name: *const c_char, count: c_longlong);
78}
79