123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- /*
- * 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 <fcntl.h>
- #include <stdarg.h>
- #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);
- (void)!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);
- }
|