diff --git a/bundle.json b/bundle.json index 9dd7470..021ff53 100644 --- a/bundle.json +++ b/bundle.json @@ -37,7 +37,8 @@ "//base/hiviewdfx/hitrace/interfaces/native/innerkits:hitrace_meter", "//base/hiviewdfx/hitrace/interfaces/native/innerkits:libhitracechain", "//base/hiviewdfx/hitrace/interfaces/js/kits:hitrace_napi", - "//base/hiviewdfx/hitrace/interfaces/rust/innerkits/hitracechain:hitracechain_rust" + "//base/hiviewdfx/hitrace/interfaces/rust/innerkits/hitracechain:hitracechain_rust", + "//base/hiviewdfx/hitrace/interfaces/rust/innerkits/hitrace_meter:hitrace_meter_rust" ], "inner_kits": [ { diff --git a/cmd/BUILD.gn b/cmd/BUILD.gn index e439a38..73b7255 100644 --- a/cmd/BUILD.gn +++ b/cmd/BUILD.gn @@ -66,6 +66,15 @@ ohos_executable("hitrace_example") { part_name = "hitrace_native" } +ohos_rust_executable("hitrace_example_rust") { + sources = [ "example/rust/main.rs" ] + + external_deps = [ "../interfaces/rust/innerkits/hitrace_meter:hitrace_meter_rust" ] + install_enable = false + subsystem_name = "hiviewdfx" + part_name = "hitrace_native" +} + group("hitrace_target") { deps = [ ":hitrace", diff --git a/cmd/example/rust/main.rs b/cmd/example/rust/main.rs new file mode 100644 index 0000000..1a13208 --- /dev/null +++ b/cmd/example/rust/main.rs @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +//! hitrace_meter example_create for rust. +extern create hitrace_meter_rust; +use hitrace_meter_rust::{start_trace, finish_trace, start_trace_async, finish_trace_async, count_trace}; +const HITRACE_TAG_OHOS : u64 = 1 << 30; +const HITRACE_TASK_ID : i32 = 666; + +/// example function A +fn func_a() { + println!("funcA!!!"); +} + +/// example function B +fn func_b() { + println!("funcB!!!"); +} + +/// trace example for rust +fn main() { + start_trace(HITRACE_TAG_OHOS, "rust test start trace funcA!!!!!!"); + func_a(); + count_trace(HITRACE_TAG_OHOS, "rust test count trace funcA!!!!!!", 0); + finish_trace(HITRACE_TAG_OHOS); + + start_trace_async(HITRACE_TAG_OHOS, "rust test start trace async funcB!!!!!!", HITRACE_TASK_ID); + func_b(); + count_trace(HITRACE_TAG_OHOS, "rust test count trace funcB!!!!!!", 1); + finish_trace_async(HITRACE_TAG_OHOS, "rust finish trace async funcB!!!!!!", HITRACE_TASK_ID); +} diff --git a/interfaces/native/innerkits/include/hitrace_meter/hitrace_meter.h b/interfaces/native/innerkits/include/hitrace_meter/hitrace_meter.h index c6e5b4b..64d52da 100644 --- a/interfaces/native/innerkits/include/hitrace_meter/hitrace_meter.h +++ b/interfaces/native/innerkits/include/hitrace_meter/hitrace_meter.h @@ -108,6 +108,7 @@ void StartTrace(uint64_t label, const std::string& value, float limit = -1); void StartTraceDebug(bool isDebug, uint64_t label, const std::string& value, float limit = -1); void StartTraceArgs(uint64_t label, const char *fmt, ...); void StartTraceArgsDebug(bool isDebug, uint64_t label, const char *fmt, ...); +void StartTraceWrapper(uint64_t label, const char *value); /** * Track the end of a context. @@ -122,6 +123,7 @@ void StartAsyncTrace(uint64_t label, const std::string& value, int32_t taskId, f void StartAsyncTraceDebug(bool isDebug, uint64_t label, const std::string& value, int32_t taskId, float limit = -1); void StartAsyncTraceArgs(uint64_t label, int32_t taskId, const char *fmt, ...); void StartAsyncTraceArgsDebug(bool isDebug, uint64_t label, int32_t taskId, const char *fmt, ...); +void StartAsyncTraceWrapper(uint64_t label, const char *value, int32_t taskId); /** * Track the end of an asynchronous event. @@ -130,6 +132,7 @@ void FinishAsyncTrace(uint64_t label, const std::string& value, int32_t taskId); void FinishAsyncTraceDebug(bool isDebug, uint64_t label, const std::string& value, int32_t taskId); void FinishAsyncTraceArgs(uint64_t label, int32_t taskId, const char *fmt, ...); void FinishAsyncTraceArgsDebug(bool isDebug, uint64_t label, int32_t taskId, const char *fmt, ...); +void FinishAsyncTraceWrapper(uint64_t label, const char *value, int32_t taskId); /** * Track the middle of a context. Match the previous function of StartTrace before it. @@ -142,6 +145,7 @@ void MiddleTraceDebug(bool isDebug, uint64_t label, const std::string& beforeVal */ void CountTrace(uint64_t label, const std::string& name, int64_t count); void CountTraceDebug(bool isDebug, uint64_t label, const std::string& name, int64_t count); +void CountTraceWrapper(uint64_t label, const char *name, int64_t count); class HitraceScoped { public: diff --git a/interfaces/native/innerkits/src/hitrace_meter.cpp b/interfaces/native/innerkits/src/hitrace_meter.cpp index 1bd05db..8c75a1a 100644 --- a/interfaces/native/innerkits/src/hitrace_meter.cpp +++ b/interfaces/native/innerkits/src/hitrace_meter.cpp @@ -242,6 +242,12 @@ void SetTraceDisabled(bool disable) g_isHitraceMeterDisabled = disable; } +void StartTraceWrapper(uint64_t label, const char *value) +{ + std::string traceValue = value; + StartTrace(label, traceValue); +} + void StartTrace(uint64_t label, const string& value, float limit UNUSED_PARAM) { AddHitraceMeterMarker(MARKER_BEGIN, label, value, 0); @@ -305,6 +311,12 @@ void StartAsyncTrace(uint64_t label, const string& value, int32_t taskId, float AddHitraceMeterMarker(MARKER_ASYNC_BEGIN, label, value, taskId); } +void StartAsyncTraceWrapper(uint64_t label, const char *value, int32_t taskId) +{ + std::string traceValue = value; + StartAsyncTrace(label, traceValue, taskId); +} + void StartAsyncTraceDebug(bool isDebug, uint64_t label, const string& value, int32_t taskId, float limit UNUSED_PARAM) { if (!isDebug) { @@ -351,6 +363,12 @@ void FinishAsyncTrace(uint64_t label, const string& value, int32_t taskId) AddHitraceMeterMarker(MARKER_ASYNC_END, label, value, taskId); } +void FinishAsyncTraceWrapper(uint64_t label, const char *value, int32_t taskId) +{ + std::string traceValue = value; + FinishAsyncTrace(label, traceValue, taskId); +} + void FinishAsyncTraceDebug(bool isDebug, uint64_t label, const string& value, int32_t taskId) { if (!isDebug) { @@ -421,6 +439,12 @@ void CountTraceDebug(bool isDebug, uint64_t label, const string& name, int64_t c AddHitraceMeterMarker(MARKER_INT, label, name, count); } +void CountTraceWrapper(uint64_t label, const char *name, int64_t count) +{ + std::string traceName = name; + CountTrace(label, traceName, count); +} + HitraceMeterFmtScoped::HitraceMeterFmtScoped(uint64_t label, const char *fmt, ...) : mTag(label) { char name[VAR_NAME_MAX_SIZE] = { 0 }; diff --git a/interfaces/rust/innerkits/hitrace_meter/BUILD.gn b/interfaces/rust/innerkits/hitrace_meter/BUILD.gn new file mode 100644 index 0000000..e48dbe7 --- /dev/null +++ b/interfaces/rust/innerkits/hitrace_meter/BUILD.gn @@ -0,0 +1,26 @@ +# Copyright (C) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") + +ohos_rust_shared_library("hitrace_meter_rust") { + sources = [ "src/lib.rs" ] + + external_deps = [ "hitrace_native:hitrace_meter" ] + + crate_name = "hitrace_meter_rust" + crate_type = "dylib" + + subsystem_name = "hiviewdfx" + part_name = "hitrace_native" +} diff --git a/interfaces/rust/innerkits/hitrace_meter/src/lib.rs b/interfaces/rust/innerkits/hitrace_meter/src/lib.rs new file mode 100644 index 0000000..9560825 --- /dev/null +++ b/interfaces/rust/innerkits/hitrace_meter/src/lib.rs @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +//! hitrace_meter dylib_create for rust. +use std::ffi::{CString, c_char, c_int, c_longlong, c_ulonglong}; + +/// Track the beginning of a context +pub fn start_trace(label: u64, value: &str) { + let value_raw_ptr = CString::new(value).unwrap(); + unsafe { + StartTraceWrapper(label, value_raw_ptr.as_ptr() as *const c_char); + } +} + +/// Track the end of a context +pub fn finish_trace(label: u64) { + unsafe { + FinishTrace(label); + } +} + +/// Track the beginning of an asynchronous event +pub fn start_trace_async(label: u64, value: &str, task_id: i32) { + let value_raw_ptr = CString::new(value).unwrap(); + unsafe { + StartAsyncTraceWrapper(label, value_raw_ptr.as_ptr() as *const c_char, task_id); + } +} + +/// Track the end of an asynchronous event +pub fn finish_trace_async(label: u64, value: &str, task_id: i32) { + let value_raw_ptr = CString::new(value).unwrap(); + unsafe { + FinishAsyncTraceWrapper(label, value_raw_ptr.as_ptr() as *const c_char, task_id); + } +} + +/// Track the 64-bit integer counter value +pub fn count_trace(label: u64, value: &str, count: i64) { + let value_raw_ptr = CString::new(value).unwrap(); + unsafe { + CountTraceWrapper(label, value_raw_ptr.as_ptr() as *const c_char, count); + } +} + +extern "C" { + /// ffi border function -> start trace + pub(create) fn StartTraceWrapper(label: c_ulonglong, value: *const c_char); + + /// ffi border function -> finish trace + pub(create) fn FinishTrace(label: c_ulonglong); + + /// ffi border function -> start async trace + pub(create) fn StartAsyncTraceWrapper(label: c_ulonglong, value: *const c_char, taskId: c_int); + + /// ffi border function -> finish async trace + pub(create) fn FinishAsyncTraceWrapper(label: c_ulonglong, value: *const c_char, taskId: c_int); + + /// ffi border function -> count trace + pub(create) fn CounttTraceWrapper(label: c_ulonglong, value: *const c_char, count: c_longlong); +}