1 /*
2 * Copyright 2021 Rockchip Electronics Co. LTD
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define MODULE_TAG "mpp_trace"
18
19 #include <fcntl.h>
20 #include <stdarg.h>
21
22 #include "mpp_common.h"
23 #include "mpp_trace.h"
24
25 #define ATRACE_MESSAGE_LENGTH 256
26
27 class MppTraceService
28 {
29 private:
30 // avoid any unwanted function
31 MppTraceService();
32 ~MppTraceService();
33 MppTraceService(const MppTraceService &);
34 MppTraceService &operator=(const MppTraceService &);
35
36 void trace_write(const char *fmt, ...);
37
38 RK_S32 mTraceFd;
39
40 public:
get_inst()41 static MppTraceService *get_inst() {
42 static MppTraceService inst;
43 return &inst;
44 }
45
46 void trace_begin(const char* name);
47 void trace_end(const char* name);
48 void trace_async_begin(const char* name, RK_S32 cookie);
49 void trace_async_end(const char* name, RK_S32 cookie);
50 void trace_int32(const char* name, RK_S32 val);
51 void trace_int64(const char* name, RK_S64 val);
52 };
53
MppTraceService()54 MppTraceService::MppTraceService()
55 {
56 static const char *ftrace_paths[] = {
57 "/sys/kernel/debug/tracing/trace_marker",
58 "/debug/tracing/trace_marker",
59 "/debugfs/tracing/trace_marker",
60 };
61
62 RK_U32 i;
63
64 for (i = 0; i < MPP_ARRAY_ELEMS(ftrace_paths); i++) {
65 if (!access(ftrace_paths[i], F_OK)) {
66 mTraceFd = open(ftrace_paths[i], O_WRONLY | O_CLOEXEC);
67 if (mTraceFd >= 0)
68 break;
69 }
70 }
71 }
72
~MppTraceService()73 MppTraceService::~MppTraceService()
74 {
75 if (mTraceFd >= 0) {
76 close(mTraceFd);
77 mTraceFd = -1;
78 }
79 }
80
trace_write(const char * fmt,...)81 void MppTraceService::trace_write(const char *fmt, ...)
82 {
83 char buf[ATRACE_MESSAGE_LENGTH];
84 va_list ap;
85 RK_S32 len;
86
87 va_start(ap, fmt);
88 len = vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
89 va_end(ap);
90
91 (void)!write(mTraceFd, buf, len);
92 }
93
trace_begin(const char * name)94 void MppTraceService::trace_begin(const char* name)
95 {
96 if (mTraceFd < 0)
97 return;
98
99 trace_write("B|%d|%s", getpid(), name);
100 }
101
trace_end(const char * name)102 void MppTraceService::trace_end(const char* name)
103 {
104 if (mTraceFd < 0)
105 return;
106
107 trace_write("E|%d|%s", getpid(), name);
108 }
109
trace_async_begin(const char * name,RK_S32 cookie)110 void MppTraceService::trace_async_begin(const char* name, RK_S32 cookie)
111 {
112 if (mTraceFd < 0)
113 return;
114
115 trace_write("S|%d|%s|%d", getpid(), name, cookie);
116 }
117
trace_async_end(const char * name,RK_S32 cookie)118 void MppTraceService::trace_async_end(const char* name, RK_S32 cookie)
119 {
120 if (mTraceFd < 0)
121 return;
122
123 trace_write("F|%d|%s|%d", getpid(), name, cookie);
124 }
125
trace_int32(const char * name,RK_S32 value)126 void MppTraceService::trace_int32(const char* name, RK_S32 value)
127 {
128 if (mTraceFd < 0)
129 return;
130
131 trace_write("C|%d|%s|%d", getpid(), name, value);
132 }
133
trace_int64(const char * name,RK_S64 value)134 void MppTraceService::trace_int64(const char* name, RK_S64 value)
135 {
136 if (mTraceFd < 0)
137 return;
138
139 trace_write("C|%d|%s|%lld", getpid(), name, value);
140 }
141
mpp_trace_begin(const char * name)142 void mpp_trace_begin(const char* name)
143 {
144 MppTraceService::get_inst()->trace_begin(name);
145 }
146
mpp_trace_end(const char * name)147 void mpp_trace_end(const char* name)
148 {
149 MppTraceService::get_inst()->trace_end(name);
150 }
151
mpp_trace_async_begin(const char * name,RK_S32 cookie)152 void mpp_trace_async_begin(const char* name, RK_S32 cookie)
153 {
154 MppTraceService::get_inst()->trace_async_begin(name, cookie);
155 }
156
mpp_trace_async_end(const char * name,RK_S32 cookie)157 void mpp_trace_async_end(const char* name, RK_S32 cookie)
158 {
159 MppTraceService::get_inst()->trace_async_end(name, cookie);
160 }
161
mpp_trace_int32(const char * name,RK_S32 value)162 void mpp_trace_int32(const char* name, RK_S32 value)
163 {
164 MppTraceService::get_inst()->trace_int32(name, value);
165 }
166
mpp_trace_int64(const char * name,RK_S64 value)167 void mpp_trace_int64(const char* name, RK_S64 value)
168 {
169 MppTraceService::get_inst()->trace_int64(name, value);
170 }
171