diff --git a/osal/CMakeLists.txt b/osal/CMakeLists.txt index 55ef8be7..4d2dc36c 100644 --- a/osal/CMakeLists.txt +++ b/osal/CMakeLists.txt @@ -40,6 +40,7 @@ add_library(osal STATIC mpp_thread.cpp mpp_common.cpp mpp_queue.cpp + mpp_trace.cpp mpp_time.cpp mpp_list.cpp mpp_mem.cpp diff --git a/osal/inc/mpp_trace.h b/osal/inc/mpp_trace.h new file mode 100644 index 00000000..beaff3d3 --- /dev/null +++ b/osal/inc/mpp_trace.h @@ -0,0 +1,37 @@ +/* + * Copyright 2021 Rockchip Electronics 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. + */ + +#ifndef __MPP_TRACE_H__ +#define __MPP_TRACE_H__ + +#include "rk_type.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void mpp_trace_begin(const char* name); +void mpp_trace_end(const char* name); +void mpp_trace_async_begin(const char* name, RK_S32 cookie); +void mpp_trace_async_end(const char* name, RK_S32 cookie); +void mpp_trace_int32(const char* name, RK_S32 value); +void mpp_trace_int64(const char* name, RK_S64 value); + +#ifdef __cplusplus +} +#endif + +#endif /*__MPP_TRACE_H__*/ \ No newline at end of file diff --git a/osal/mpp_trace.cpp b/osal/mpp_trace.cpp new file mode 100644 index 00000000..4a70cabf --- /dev/null +++ b/osal/mpp_trace.cpp @@ -0,0 +1,170 @@ +/* + * Copyright 2021 Rockchip Electronics 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. + */ + +#define MODULE_TAG "mpp_trace" + +#include +#include + +#include "mpp_common.h" +#include "mpp_trace.h" + +#define ATRACE_MESSAGE_LENGTH 256 + +class MppTraceService +{ +private: + // avoid any unwanted function + MppTraceService(); + ~MppTraceService(); + MppTraceService(const MppTraceService &); + MppTraceService &operator=(const MppTraceService &); + + void trace_write(const char *fmt, ...); + + RK_S32 mTraceFd; + +public: + static MppTraceService *get_inst() { + static MppTraceService inst; + return &inst; + } + + void trace_begin(const char* name); + void trace_end(const char* name); + void trace_async_begin(const char* name, RK_S32 cookie); + void trace_async_end(const char* name, RK_S32 cookie); + void trace_int32(const char* name, RK_S32 val); + void trace_int64(const char* name, RK_S64 val); +}; + +MppTraceService::MppTraceService() +{ + static const char *ftrace_paths[] = { + "/sys/kernel/debug/tracing/trace_marker", + "/debug/tracing/trace_marker", + "/debugfs/tracing/trace_marker", + }; + + RK_U32 i; + + for (i = 0; i < MPP_ARRAY_ELEMS(ftrace_paths); i++) { + if (!access(ftrace_paths[i], F_OK)) { + mTraceFd = open(ftrace_paths[i], O_WRONLY | O_CLOEXEC); + if (mTraceFd >= 0) + break; + } + } +} + +MppTraceService::~MppTraceService() +{ + if (mTraceFd >= 0) { + close(mTraceFd); + mTraceFd = -1; + } +} + +void MppTraceService::trace_write(const char *fmt, ...) +{ + char buf[ATRACE_MESSAGE_LENGTH]; + va_list ap; + RK_S32 len; + + va_start(ap, fmt); + len = vsnprintf(buf, sizeof(buf) - 1, fmt, ap); + va_end(ap); + + write(mTraceFd, buf, len); +} + +void MppTraceService::trace_begin(const char* name) +{ + if (mTraceFd < 0) + return; + + trace_write("B|%d|%s", getpid(), name); +} + +void MppTraceService::trace_end(const char* name) +{ + if (mTraceFd < 0) + return; + + trace_write("E|%d|%s", getpid(), name); +} + +void MppTraceService::trace_async_begin(const char* name, RK_S32 cookie) +{ + if (mTraceFd < 0) + return; + + trace_write("S|%d|%s|%d", getpid(), name, cookie); +} + +void MppTraceService::trace_async_end(const char* name, RK_S32 cookie) +{ + if (mTraceFd < 0) + return; + + trace_write("F|%d|%s|%d", getpid(), name, cookie); +} + +void MppTraceService::trace_int32(const char* name, RK_S32 value) +{ + if (mTraceFd < 0) + return; + + trace_write("C|%d|%s|%d", getpid(), name, value); +} + +void MppTraceService::trace_int64(const char* name, RK_S64 value) +{ + if (mTraceFd < 0) + return; + + trace_write("C|%d|%s|%lld", getpid(), name, value); +} + +void mpp_trace_begin(const char* name) +{ + MppTraceService::get_inst()->trace_begin(name); +} + +void mpp_trace_end(const char* name) +{ + MppTraceService::get_inst()->trace_end(name); +} + +void mpp_trace_async_begin(const char* name, RK_S32 cookie) +{ + MppTraceService::get_inst()->trace_async_begin(name, cookie); +} + +void mpp_trace_async_end(const char* name, RK_S32 cookie) +{ + MppTraceService::get_inst()->trace_async_end(name, cookie); +} + +void mpp_trace_int32(const char* name, RK_S32 value) +{ + MppTraceService::get_inst()->trace_int32(name, value); +} + +void mpp_trace_int64(const char* name, RK_S64 value) +{ + MppTraceService::get_inst()->trace_int64(name, value); +} diff --git a/osal/test/CMakeLists.txt b/osal/test/CMakeLists.txt index 1762460c..e58bece3 100644 --- a/osal/test/CMakeLists.txt +++ b/osal/test/CMakeLists.txt @@ -31,6 +31,9 @@ add_mpp_osal_test(mpp_mem) # time system unit test add_mpp_osal_test(mpp_time) +# trace system unit test +add_mpp_osal_test(mpp_trace) + # hardware platform feature detection unit test add_mpp_osal_test(mpp_platform) diff --git a/osal/test/mpp_trace_test.c b/osal/test/mpp_trace_test.c new file mode 100644 index 00000000..24f1e865 --- /dev/null +++ b/osal/test/mpp_trace_test.c @@ -0,0 +1,39 @@ +/* + * Copyright 2021 Rockchip Electronics 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. + */ + +#define MODULE_TAG "mpp_trace_test" + +#include "mpp_log.h" + +#include "mpp_trace.h" + +int main(void) +{ + mpp_log("mpp trace test start\n"); + + mpp_trace_begin("mpp_trace_test"); + mpp_trace_end("mpp_trace_test"); + + mpp_trace_async_begin("mpp_trace_test async", 10); + mpp_trace_async_end("mpp_trace_test async", 10); + + mpp_trace_int32("mpp_trace_test int32", 256); + mpp_trace_int64("mpp_trace_test int64", 100000000); + + mpp_log("mpp trace test done\n"); + + return 0; +}