42 #ifndef AVB_TRACE_PUB_H
43 #define AVB_TRACE_PUB_H 1
45 #include "openavb_platform_pub.h"
47 #include "openavb_types_pub.h"
53 #define AVB_TRACE_MODE_NONE 0
54 #define AVB_TRACE_MODE_MINIMAL 1
55 #define AVB_TRACE_MODE_NORMAL 2
56 #define AVB_TRACE_MODE_DELTA_TIME 3
57 #define AVB_TRACE_MODE_DELTA_STATS 4
58 #define AVB_TRACE_MODE_FUNC_TIME 5
59 #define AVB_TRACE_MODE_DOC 6
62 #define AVB_TRACE_MODE AVB_TRACE_MODE_NORMAL
65 #define AVB_TRACE_OPT_DELTA_STATS_INTERVAL 80000
68 #define AVB_TRACE_OPT_FUNC_TIME_INTERVAL 80000
77 #define AVB_TRACE_PRINTF(FMT, ...) printf(FMT, __VA_ARGS__)
81 #define AVB_TRACE_MAP 0
82 #define AVB_TRACE_MAP_DETAIL 0
83 #define AVB_TRACE_MAP_LINE 0
84 #define AVB_TRACE_INTF 0
85 #define AVB_TRACE_INTF_DETAIL 0
86 #define AVB_TRACE_INTF_LINE 0
87 #define AVB_TRACE_HOST 0
89 #define TRACE_VAR1(x, y) x ## y
90 #define TRACE_VAR2(x, y) TRACE_VAR1(x, y)
92 #ifdef AVB_TRACE_OPT_SHOW_FILE_NAME
93 #define TRACE_FILE_NAME __FILE__
95 #define TRACE_FILE_NAME ""
99 #if !defined(AVB_TRACE_ON) || (AVB_TRACE_MODE == AVB_TRACE_MODE_NONE)
100 #define AVB_TRACE_LINE(FEATURE)
101 #define AVB_TRACE_ENTRY(FEATURE)
102 #define AVB_TRACE_EXIT(FEATURE)
103 #define AVB_TRACE_LOOP_ENTRY(FEATURE)
104 #define AVB_TRACE_LOOP_EXIT(FEATURE)
105 #elif (AVB_TRACE_MODE == AVB_TRACE_MODE_MINIMAL)
106 #define AVB_TRACE_LINE(FEATURE) avbTraceMinimalFn(FEATURE, "TRACE LINE ", __FUNCTION__, TRACE_FILE_NAME, __LINE__)
107 #define AVB_TRACE_ENTRY(FEATURE) avbTraceMinimalFn(FEATURE, "TRACE ENTRY", __FUNCTION__, TRACE_FILE_NAME, __LINE__)
108 #define AVB_TRACE_EXIT(FEATURE) avbTraceMinimalFn(FEATURE, "TRACE EXIT ", __FUNCTION__, TRACE_FILE_NAME, __LINE__)
109 #define AVB_TRACE_LOOP_ENTRY(FEATURE) avbTraceMinimalFn(FEATURE, "TRACE LOOP{", __FUNCTION__, TRACE_FILE_NAME, __LINE__)
110 #define AVB_TRACE_LOOP_EXIT(FEATURE) avbTraceMinimalFn(FEATURE, "TRACE LOOP}", __FUNCTION__, TRACE_FILE_NAME, __LINE__)
111 #elif (AVB_TRACE_MODE == AVB_TRACE_MODE_NORMAL)
112 #define AVB_TRACE_LINE(FEATURE) avbTraceNormalFn(FEATURE, "TRACE LINE ", __FUNCTION__, TRACE_FILE_NAME, __LINE__)
113 #define AVB_TRACE_ENTRY(FEATURE) avbTraceNormalFn(FEATURE, "TRACE ENTRY", __FUNCTION__, TRACE_FILE_NAME, __LINE__)
114 #define AVB_TRACE_EXIT(FEATURE) avbTraceNormalFn(FEATURE, "TRACE EXIT ", __FUNCTION__, TRACE_FILE_NAME, __LINE__)
115 #define AVB_TRACE_LOOP_ENTRY(FEATURE) avbTraceNormalFn(FEATURE, "TRACE LOOP{", __FUNCTION__, TRACE_FILE_NAME, __LINE__)
116 #define AVB_TRACE_LOOP_EXIT(FEATURE) avbTraceNormalFn(FEATURE, "TRACE LOOP}", __FUNCTION__, TRACE_FILE_NAME, __LINE__)
117 #elif (AVB_TRACE_MODE == AVB_TRACE_MODE_DELTA_TIME)
118 #define AVB_TRACE_LINE(FEATURE) avbTraceDeltaTimeFn(FEATURE, "TRACE LINE ", __FUNCTION__, TRACE_FILE_NAME, __LINE__, NULL, NULL)
119 #define AVB_TRACE_ENTRY(FEATURE) avbTraceDeltaTimeFn(FEATURE, "TRACE ENTRY", __FUNCTION__, TRACE_FILE_NAME, __LINE__, NULL, NULL)
120 #define AVB_TRACE_EXIT(FEATURE) avbTraceDeltaTimeFn(FEATURE, "TRACE EXIT ", __FUNCTION__, TRACE_FILE_NAME, __LINE__, NULL, NULL)
121 #define AVB_TRACE_LOOP_ENTRY(FEATURE) avbTraceDeltaTimeFn(FEATURE, "TRACE LOOP{", __FUNCTION__, TRACE_FILE_NAME, __LINE__, NULL, NULL)
122 #define AVB_TRACE_LOOP_EXIT(FEATURE) avbTraceDeltaTimeFn(FEATURE, "TRACE LOOP}", __FUNCTION__, TRACE_FILE_NAME, __LINE__, NULL, NULL)
123 #elif (AVB_TRACE_MODE == AVB_TRACE_MODE_DELTA_STATS)
124 #define AVB_TRACE_LINE(FEATURE) static U64 TRACE_VAR2(traceCnt,__LINE__); static U64 TRACE_VAR2(traceNSec, __LINE__); avbTraceDeltaTimeFn(FEATURE, "TRACE LINE ", __FUNCTION__, TRACE_FILE_NAME, __LINE__, &TRACE_VAR2(traceCnt,__LINE__), &TRACE_VAR2(traceNSec, __LINE__))
125 #define AVB_TRACE_ENTRY(FEATURE) static U64 TRACE_VAR2(traceCnt,__LINE__); static U64 TRACE_VAR2(traceNSec, __LINE__); avbTraceDeltaTimeFn(FEATURE, "TRACE ENTRY", __FUNCTION__, TRACE_FILE_NAME, __LINE__, &TRACE_VAR2(traceCnt,__LINE__), &TRACE_VAR2(traceNSec, __LINE__))
126 #define AVB_TRACE_EXIT(FEATURE) static U64 TRACE_VAR2(traceCnt,__LINE__); static U64 TRACE_VAR2(traceNSec, __LINE__); avbTraceDeltaTimeFn(FEATURE, "TRACE EXIT ", __FUNCTION__, TRACE_FILE_NAME, __LINE__, &TRACE_VAR2(traceCnt,__LINE__), &TRACE_VAR2(traceNSec, __LINE__))
127 #define AVB_TRACE_LOOP_ENTRY(FEATURE) static U64 TRACE_VAR2(traceCnt,__LINE__); static U64 TRACE_VAR2(traceNSec, __LINE__); avbTraceDeltaTimeFn(FEATURE, "TRACE LOOP{", __FUNCTION__, TRACE_FILE_NAME, __LINE__, &TRACE_VAR2(traceCnt,__LINE__), &TRACE_VAR2(traceNSec, __LINE__))
128 #define AVB_TRACE_LOOP_EXIT(FEATURE) static U64 TRACE_VAR2(traceCnt,__LINE__); static U64 TRACE_VAR2(traceNSec, __LINE__); avbTraceDeltaTimeFn(FEATURE, "TRACE LOOP}", __FUNCTION__, TRACE_FILE_NAME, __LINE__, &TRACE_VAR2(traceCnt,__LINE__), &TRACE_VAR2(traceNSec, __LINE__))
129 #elif (AVB_TRACE_MODE == AVB_TRACE_MODE_FUNC_TIME)
130 #define AVB_TRACE_LINE(FEATURE)
131 #define AVB_TRACE_ENTRY(FEATURE) static struct timespec TRACE_VAR2(tsFuncEntry,__FUNCTION__); avbTraceFuncTimeFn(FEATURE, TRUE, __FUNCTION__, TRACE_FILE_NAME, __LINE__, &TRACE_VAR2(tsFuncEntry,__FUNCTION__), NULL, NULL)
132 #define AVB_TRACE_EXIT(FEATURE) static U64 TRACE_VAR2(traceCnt,__LINE__); static U64 TRACE_VAR2(traceNSec, __LINE__); avbTraceFuncTimeFn(FEATURE, FALSE, __FUNCTION__, TRACE_FILE_NAME, __LINE__, &TRACE_VAR2(tsFuncEntry,__FUNCTION__), &TRACE_VAR2(traceCnt,__LINE__), &TRACE_VAR2(traceNSec, __LINE__))
133 #define AVB_TRACE_LOOP_ENTRY(FEATURE) static struct timespec TRACE_VAR2(tsLoopEntry,__FUNCTION__); avbTraceFuncTimeFn(FEATURE, TRUE, __FUNCTION__, TRACE_FILE_NAME, __LINE__, &TRACE_VAR2(tsLoopEntry,__FUNCTION__), NULL, NULL)
134 #define AVB_TRACE_LOOP_EXIT(FEATURE) static U64 TRACE_VAR2(traceCnt,__LINE__); static U64 TRACE_VAR2(traceNSec, __LINE__); avbTraceFuncTimeFn(FEATURE, FALSE, __FUNCTION__, TRACE_FILE_NAME, __LINE__, &TRACE_VAR2(tsLoopEntry,__FUNCTION__), &TRACE_VAR2(traceCnt,__LINE__), &TRACE_VAR2(traceNSec, __LINE__))
135 #elif (AVB_TRACE_MODE == AVB_TRACE_MODE_DOC)
136 #define AVB_TRACE_LINE(FEATURE) avbTraceDocFn(FEATURE, "|", __FUNCTION__, TRACE_FILE_NAME, __LINE__, NULL, NULL)
137 #define AVB_TRACE_ENTRY(FEATURE) avbTraceDocFn(FEATURE, ">", __FUNCTION__, TRACE_FILE_NAME, __LINE__, NULL, NULL)
138 #define AVB_TRACE_EXIT(FEATURE) avbTraceDocFn(FEATURE, "<", __FUNCTION__, TRACE_FILE_NAME, __LINE__, NULL, NULL)
139 #define AVB_TRACE_LOOP_ENTRY(FEATURE)
140 #define AVB_TRACE_LOOP_EXIT(FEATURE)
145 static inline void avbTraceMinimalFn(
int featureOn,
const char *tag,
const char *
function,
const char *file,
int line)
148 AVB_TRACE_PRINTF(
"%s: %s():%d %s\n", tag,
function, line, file);
152 static inline void avbTraceNormalFn(
int featureOn,
const char *tag,
const char *
function,
const char *file,
int line)
155 time_t tNow = time(
NULL);
157 localtime_r(&tNow, &tmNow);
159 AVB_TRACE_PRINTF(
"[%2.2d:%2.2d:%2.2d] [P:%5.5d T:%lu] %s: %s():%d %s\n",
160 tmNow.tm_hour, tmNow.tm_min, tmNow.tm_sec, GET_PID(), THREAD_SELF(), tag,
function, line, file);
164 static inline void avbTraceDeltaTimeFn(
int featureOn,
const char *tag,
const char *
function,
const char *file,
int line,
U64 *pCnt,
U64 *pNSec)
167 static struct timespec traceDeltaTimePrevTS;
169 struct timespec nowTS;
170 CLOCK_GETTIME(OPENAVB_CLOCK_REALTIME, &nowTS);
172 time_t tNow = time(
NULL);
174 localtime_r(&tNow, &tmNow);
178 U64 deltaNSec = nowNSec - prevNSec;
185 if (*pCnt % AVB_TRACE_OPT_DELTA_STATS_INTERVAL == 0) {
186 AVB_TRACE_PRINTF(
"[%2.2d:%2.2d:%2.2d] [P:%5.5d T:%lu] [cnt:%6llu deltaUS:%10llu totalUS:%10llu] %s: %s():%d %s\n",
187 tmNow.tm_hour, tmNow.tm_min, tmNow.tm_sec, GET_PID(), THREAD_SELF(), (
unsigned long long)(*pCnt), (
unsigned long long)(deltaNSec / 1000), (
unsigned long long)(*pNSec / 1000), tag,
function, line, file);
191 AVB_TRACE_PRINTF(
"[%2.2d:%2.2d:%2.2d] [P:%5.5d T:%lu] [deltaUS:%10llu] %s: %s():%d %s\n",
192 tmNow.tm_hour, tmNow.tm_min, tmNow.tm_sec, GET_PID(), THREAD_SELF(), (
unsigned long long)(deltaNSec / 1000), tag,
function, line, file);
195 CLOCK_GETTIME(OPENAVB_CLOCK_REALTIME, &traceDeltaTimePrevTS);
199 static inline void avbTraceFuncTimeFn(
int featureOn,
bool bEntry,
const char *
function,
const char *file,
int line,
struct timespec *pTS,
U64 *pCnt,
U64 *pNSec)
203 CLOCK_GETTIME(OPENAVB_CLOCK_REALTIME, pTS);
206 struct timespec nowTS;
207 CLOCK_GETTIME(OPENAVB_CLOCK_REALTIME, &nowTS);
209 time_t tNow = time(
NULL);
211 localtime_r(&tNow, &tmNow);
215 U64 deltaNSec = nowNSec - entryNSec;
220 if (*pCnt % AVB_TRACE_OPT_FUNC_TIME_INTERVAL == 0) {
221 AVB_TRACE_PRINTF(
"[%2.2d:%2.2d:%2.2d] [P:%5.5d T:%lu] [cnt:%6llu lastUS:%10llu totalUS:%10llu avgUS:%10llu] %s():%d %s\n",
222 tmNow.tm_hour, tmNow.tm_min, tmNow.tm_sec, GET_PID(), THREAD_SELF(), (
unsigned long long)(*pCnt), (
unsigned long long)(deltaNSec / 1000), (
unsigned long long)(*pNSec / 1000), (
unsigned long long)((*pNSec / *pCnt) / 1000),
function, line, file);
228 static inline void avbTraceDocFn(
int featureOn,
const char *tag,
const char *
function,
const char *file,
int line,
U64 *pCnt,
U64 *pNSec)
231 static int depthTrace = 0;
233 AVB_TRACE_PRINTF(
"%*s%s %s [%s]\n", depthTrace * 4,
"", tag,
function, file);
236 else if (tag[0] ==
'<') {
238 AVB_TRACE_PRINTF(
"%*s%s %s [%s]\n", depthTrace * 4,
"", tag,
function, file);
241 AVB_TRACE_PRINTF(
"%*s%s %s [%s]\n", depthTrace * 4,
"", tag,
function, file);
246 #endif // AVB_TRACE_ON
247 #endif // AVB_TRACE_PUB_H
uint64_t U64
Unsigned 64 bit type.
Definition: openavb_types_base_pub.h:89
#define NANOSECONDS_PER_SECOND
Number of nanoseconds in second.
Definition: openavb_types_base_pub.h:52
#define NULL
Null pointer value.
Definition: openavb_types_base_pub.h:71