1 // Copyright (c) 2017 Intel Corporation
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in all
11 // copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 // SOFTWARE.
20 
21 #ifndef __MFX_TRACE_H__
22 #define __MFX_TRACE_H__
23 
24 #include "mfxdefs.h"
25 
26 #ifndef MFX_TRACE_DISABLE
27 // Uncomment one or several lines below to enable tracing
28 
29 //#define MFX_TRACE_ENABLE_ITT
30 //#define MFX_TRACE_ENABLE_TEXTLOG
31 //#define MFX_TRACE_ENABLE_STAT
32 
33 #if (defined(LINUX32) || defined(ANDROID)) && defined(MFX_TRACE_ENABLE_ITT) && !defined(MFX_TRACE_ENABLE_FTRACE)
34     // Accompany ITT trace with ftrace. This combination is used by VTune.
35     #define MFX_TRACE_ENABLE_FTRACE
36 #endif
37 
38 #if (MFX_VERSION >= 1025) && defined(MFX_TRACE_ENABLE_TEXTLOG)
39     // mfx reflect mechanism feeds output to one of the mfx trace modules
40     // thus, there is no reason for it to be enabled if no trace module
41     // is enabled
42     #define MFX_TRACE_ENABLE_REFLECT
43 #endif
44 
45 #if defined(MFX_TRACE_ENABLE_TEXTLOG) || defined(MFX_TRACE_ENABLE_STAT) || defined(MFX_TRACE_ENABLE_ITT) || defined(MFX_TRACE_ENABLE_FTRACE)
46 #define MFX_TRACE_ENABLE
47 #endif
48 
49 #endif // #ifndef MFX_TRACE_DISABLE
50 
51 #include <stdarg.h>
52 
53     #define MAX_PATH 260
54 
55     #define __INT64   long long
56     #define __UINT64  unsigned long long
57 
58     typedef char mfxTraceChar;
59 
60     #define MFX_TRACE_STRING(x) x
61 
62     #define DISABLE_WARN_HIDE_PREV_LOCAL_DECLARATION
63     #define ROLLBACK_WARN_HIDE_PREV_LOCAL_DECLARATION
64 
65 typedef unsigned int mfxTraceU32;
66 typedef __UINT64 mfxTraceU64;
67 
68 /*------------------------------------------------------------------------------*/
69 // C section
70 
71 #ifdef __cplusplus
72 extern "C"
73 {
74 #endif
75 
76 // list of output modes
77 enum
78 {
79     MFX_TRACE_OUTPUT_TRASH  = 0x00,
80     MFX_TRACE_OUTPUT_TEXTLOG = 0x01,
81     MFX_TRACE_OUTPUT_STAT   = 0x02,
82     MFX_TRACE_OUTPUT_ETW    = 0x04,
83     MFX_TRACE_OUTPUT_TAL    = 0x08,
84 
85     MFX_TRACE_OUTPUT_ITT    = 0x10,
86     MFX_TRACE_OUTPUT_FTRACE = 0x20,
87     // special keys
88     MFX_TRACE_OUTPUT_ALL     = 0xFFFFFFFF,
89     MFX_TRACE_OUTPUT_REG     = MFX_TRACE_OUTPUT_ALL // output mode should be read from registry
90 };
91 
92 // enumeration of the trace levels inside any category
93 typedef enum
94 {
95     MFX_TRACE_LEVEL_0 = 0,
96     MFX_TRACE_LEVEL_1 = 1,
97     MFX_TRACE_LEVEL_2 = 2,
98     MFX_TRACE_LEVEL_3 = 3,
99     MFX_TRACE_LEVEL_4 = 4,
100     MFX_TRACE_LEVEL_5 = 5,
101     MFX_TRACE_LEVEL_6 = 6,
102     MFX_TRACE_LEVEL_7 = 7,
103     MFX_TRACE_LEVEL_8 = 8,
104     MFX_TRACE_LEVEL_9 = 9,
105     MFX_TRACE_LEVEL_10 = 10,
106     MFX_TRACE_LEVEL_11 = 11,
107     MFX_TRACE_LEVEL_12 = 12,
108     MFX_TRACE_LEVEL_13 = 13,
109     MFX_TRACE_LEVEL_14 = 14,
110     MFX_TRACE_LEVEL_15 = 15,
111     MFX_TRACE_LEVEL_16 = 16,
112 
113     MFX_TRACE_LEVEL_MAX = 0xFF
114 } mfxTraceLevel;
115 
116 
117 // TODO delete the following levels completely
118 #define MFX_TRACE_LEVEL_INTERNAL_VTUNE  MFX_TRACE_LEVEL_2
119 #define MFX_TRACE_LEVEL_SCHED       MFX_TRACE_LEVEL_10
120 #define MFX_TRACE_LEVEL_PRIVATE     MFX_TRACE_LEVEL_16
121 
122 // TODO the following levels should remain only
123 
124 /** API level
125  * - Media SDK library entry points exposed in Media SDK official API
126  * - Media SDK library important internal entry points
127  */
128 #define MFX_TRACE_LEVEL_API         MFX_TRACE_LEVEL_1
129 
130 /** HOTSPOTS level
131  * - Known Media SDK library internal hotspots (functions taking > ~50us on selected platforms/contents)
132  */
133 #define MFX_TRACE_LEVEL_HOTSPOTS    MFX_TRACE_LEVEL_INTERNAL_VTUNE  // TODO should be MFX_TRACE_LEVEL_2
134 
135 /** EXTCALL level
136  * - Calls to external libaries (DXVA, LibVA, MDF/CM, etc.)
137  */
138 #define MFX_TRACE_LEVEL_EXTCALL     MFX_TRACE_LEVEL_INTERNAL_VTUNE  // TODO should be MFX_TRACE_LEVEL_3
139 
140 /** SCHEDULER level
141  * - Media SDK internal scheduler functions calls
142  */
143 #define MFX_TRACE_LEVEL_SCHEDULER   MFX_TRACE_LEVEL_4
144 
145 /** INTERNAL level
146  * - Media SDK components function calls. Use this level to get more deeper knowledge of
147  * calling stack.
148  */
149 #define MFX_TRACE_LEVEL_INTERNAL    MFX_TRACE_LEVEL_5
150 
151 /** PARAMS level
152  * - Tracing of function parameters, variables, etc. Note that not all tracing modules support this.
153  */
154 #define MFX_TRACE_LEVEL_PARAMS      MFX_TRACE_LEVEL_6
155 
156 // defines default trace category
157 #define MFX_TRACE_CATEGORY_DEFAULT  NULL
158 
159 // defines category for the current module
160 #ifndef MFX_TRACE_CATEGORY
161     #define MFX_TRACE_CATEGORY      MFX_TRACE_CATEGORY_DEFAULT
162 #endif
163 
164 // defines default trace level
165 #define MFX_TRACE_LEVEL_DEFAULT     MFX_TRACE_LEVEL_MAX
166 
167 // defines default trace level for the current module
168 #ifndef MFX_TRACE_LEVEL
169     #define MFX_TRACE_LEVEL         MFX_TRACE_LEVEL_DEFAULT
170 #endif
171 
172 /*------------------------------------------------------------------------------*/
173 
174 #ifdef MFX_TRACE_ENABLE
175 
176 typedef union
177 {
178     unsigned int  uint32;
179     __UINT64      uint64;
180     __INT64       tick;
181     char*         str;
182     void*         ptr;
183     mfxTraceChar* category;
184     mfxTraceLevel level;
185 } mfxTraceHandle;
186 
187 typedef struct
188 {
189     mfxTraceChar* category;
190     mfxTraceLevel level;
191     // reserved for stat dump:
192     mfxTraceHandle sd1;
193     mfxTraceHandle sd2;
194     mfxTraceHandle sd3;
195     mfxTraceHandle sd4;
196     mfxTraceHandle sd5;
197     mfxTraceHandle sd6;
198     mfxTraceHandle sd7;
199     // reserved for itt
200     mfxTraceHandle itt1;
201 } mfxTraceStaticHandle;
202 
203 typedef struct
204 {
205     // reserved for file dump:
206     mfxTraceHandle fd1;
207     mfxTraceHandle fd2;
208     mfxTraceHandle fd3;
209     mfxTraceHandle fd4;
210     // reserved for stat dump:
211     mfxTraceHandle sd1;
212     // reserved for TAL:
213     mfxTraceHandle tal1;
214     // reserved for ETW:
215     mfxTraceHandle etw1;
216     mfxTraceHandle etw2;
217     // reserved for itt
218     mfxTraceHandle itt1;
219 } mfxTraceTaskHandle;
220 
221 /*------------------------------------------------------------------------------*/
222 
223 
224 // basic trace functions (macroses are recommended to use instead)
225 
226 mfxTraceU32 MFXTrace_Init();
227 
228 mfxTraceU32 MFXTrace_Close(void);
229 
230 mfxTraceU32 MFXTrace_SetLevel(mfxTraceChar* category,
231                               mfxTraceLevel level);
232 
233 mfxTraceU32 MFXTrace_DebugMessage(mfxTraceStaticHandle *static_handle,
234                              const char *file_name, mfxTraceU32 line_num,
235                              const char *function_name,
236                              mfxTraceChar* category, mfxTraceLevel level,
237                              const char *message,
238                              const char *format, ...);
239 
240 mfxTraceU32 MFXTrace_vDebugMessage(mfxTraceStaticHandle *static_handle,
241                               const char *file_name, mfxTraceU32 line_num,
242                               const char *function_name,
243                               mfxTraceChar* category, mfxTraceLevel level,
244                               const char *message,
245                               const char *format, va_list args);
246 
247 mfxTraceU32 MFXTrace_BeginTask(mfxTraceStaticHandle *static_handle,
248                           const char *file_name, mfxTraceU32 line_num,
249                           const char *function_name,
250                           mfxTraceChar* category, mfxTraceLevel level,
251                           const char *task_name, mfxTraceTaskHandle *task_handle,
252                           const void *task_params);
253 
254 mfxTraceU32 MFXTrace_EndTask(mfxTraceStaticHandle *static_handle,
255                              mfxTraceTaskHandle *task_handle);
256 
257 /*------------------------------------------------------------------------------*/
258 // basic macroses
259 
260 #define MFX_TRACE_PARAMS \
261     &_trace_static_handle, __FILE__, __LINE__, __FUNCTION__, MFX_TRACE_CATEGORY
262 
263 
264 #define MFX_TRACE_INIT() \
265     MFXTrace_Init();
266 
267 #define MFX_TRACE_INIT_RES(_res) \
268     _res = MFXTrace_Init();
269 
270 #define MFX_TRACE_CLOSE() \
271     MFXTrace_Close();
272 
273 #define MFX_TRACE_CLOSE_RES(_res) \
274     _res = MFXTrace_Close();
275 
276 #define MFX_LTRACE(_trace_all_params)                       \
277 {                                                           \
278     DISABLE_WARN_HIDE_PREV_LOCAL_DECLARATION                \
279     static mfxTraceStaticHandle _trace_static_handle = {};  \
280     MFXTrace_DebugMessage _trace_all_params;                \
281     ROLLBACK_WARN_HIDE_PREV_LOCAL_DECLARATION               \
282 }
283 #else
284 #define MFX_TRACE_INIT()
285 #define MFX_TRACE_INIT_RES(res)
286 #define MFX_TRACE_CLOSE()
287 #define MFX_TRACE_CLOSE_RES(res)
288 #define MFX_LTRACE(_trace_all_params)
289 #endif
290 
291 /*------------------------------------------------------------------------------*/
292 // standard formats
293 
294 #define MFX_TRACE_FORMAT_S    "%s"
295 #define MFX_TRACE_FORMAT_WS   "%S"
296 #define MFX_TRACE_FORMAT_P    "%p"
297 #define MFX_TRACE_FORMAT_I    "%d"
298 #define MFX_TRACE_FORMAT_X    "%x"
299 #define MFX_TRACE_FORMAT_D    "%d (0x%x)"
300 #define MFX_TRACE_FORMAT_F    "%g"
301 #define MFX_TRACE_FORMAT_GUID "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}"
302 
303 /*------------------------------------------------------------------------------*/
304 // these macroses permit to set trace level
305 
306 #define MFX_LTRACE_1(_level, _message, _format, _arg1) \
307     MFX_LTRACE((MFX_TRACE_PARAMS, _level, _message, _format, _arg1))
308 
309 #define MFX_LTRACE_2(_level, _message, _format, _arg1, _arg2) \
310     MFX_LTRACE((MFX_TRACE_PARAMS, _level, _message, _format, _arg1, _arg2))
311 
312 #define MFX_LTRACE_3(_level, _message, _format, _arg1, _arg2, _arg3) \
313     MFX_LTRACE((MFX_TRACE_PARAMS, _level, _message, _format, _arg1, _arg2, _arg3))
314 
315 #define MFX_LTRACE_MSG(_level, _message) \
316     MFX_LTRACE_1(_level, _message, NULL, 0)
317 
318 #define MFX_LTRACE_S(_level, _string) \
319     MFX_LTRACE_1(_level, #_string " = ", MFX_TRACE_FORMAT_S, _string)
320 
321 #define MFX_LTRACE_WS(_level, _string) \
322     MFX_LTRACE_1(_level, #_string " = ", MFX_TRACE_FORMAT_WS, _string)
323 
324 #define MFX_LTRACE_P(_level, _arg1) \
325     MFX_LTRACE_1(_level, #_arg1 " = ", MFX_TRACE_FORMAT_P, _arg1)
326 
327 #define MFX_LTRACE_I(_level, _arg1) \
328     MFX_LTRACE_1(_level, #_arg1 " = ", MFX_TRACE_FORMAT_I, _arg1)
329 
330 #define MFX_LTRACE_X(_level, _arg1) \
331     MFX_LTRACE_1(_level, #_arg1 " = ", MFX_TRACE_FORMAT_X, _arg1)
332 
333 #define MFX_LTRACE_D(_level, _arg1) \
334     MFX_LTRACE_2(_level, #_arg1 " = ", MFX_TRACE_FORMAT_D, _arg1, _arg1)
335 
336 #define MFX_LTRACE_F(_level, _arg1) \
337     MFX_LTRACE_1(_level, #_arg1 " = ", MFX_TRACE_FORMAT_F, _arg1)
338 
339 #ifdef MFX_TRACE_ENABLE
340 #define MFX_LTRACE_BUFFER_S(_level, _name, _buffer, _size)  \
341     if (_buffer)                                            \
342     MFX_LTRACE_2(_level, _name, "%p[%d]", _buffer, _size)
343 #else
344 #define MFX_LTRACE_BUFFER_S(_level, _name, _buffer, _size)
345 #endif
346 
347 #define MFX_LTRACE_BUFFER(_level, _buffer) \
348     MFX_LTRACE_BUFFER_S(_level, #_buffer, _buffer, sizeof(*_buffer)) \
349 
350 #define MFX_LTRACE_GUID(_level, _guid) \
351     MFX_LTRACE((MFX_TRACE_PARAMS, _level, #_guid " = ", \
352                MFX_TRACE_FORMAT_GUID, \
353                (_guid).Data1, (_guid).Data2, (_guid).Data3, \
354                (_guid).Data4[0], (_guid).Data4[1], (_guid).Data4[2], (_guid).Data4[3], \
355                (_guid).Data4[4], (_guid).Data4[5], (_guid).Data4[6], (_guid).Data4[7]))
356 
357 /*------------------------------------------------------------------------------*/
358 // these macroses uses default trace level
359 
360 #define MFX_TRACE_1(_message, _format, _arg1) \
361     MFX_LTRACE_1(MFX_TRACE_LEVEL, _message, _format, _arg1)
362 
363 #define MFX_TRACE_2(_message, _format, _arg1, _arg2) \
364     MFX_LTRACE_2(MFX_TRACE_LEVEL, _message, _format, _arg1, _arg2)
365 
366 #define MFX_TRACE_3(_message, _format, _arg1, _arg2, _arg3) \
367     MFX_LTRACE_3(MFX_TRACE_LEVEL, _message, _format, _arg1, _arg2, _arg3)
368 
369 #define MFX_TRACE_S(_arg1) \
370     MFX_LTRACE_S(MFX_TRACE_LEVEL, _arg1)
371 
372 #define MFX_TRACE_WS(_message) \
373     MFX_LTRACE_WS(MFX_TRACE_LEVEL, _message)
374 
375 #define MFX_TRACE_P(_arg1) \
376     MFX_LTRACE_P(MFX_TRACE_LEVEL, _arg1)
377 
378 #define MFX_TRACE_I(_arg1) \
379     MFX_LTRACE_I(MFX_TRACE_LEVEL, _arg1)
380 
381 #define MFX_TRACE_X(_arg1) \
382     MFX_LTRACE_X(MFX_TRACE_LEVEL, _arg1)
383 
384 #define MFX_TRACE_D(_arg1) \
385     MFX_LTRACE_D(MFX_TRACE_LEVEL, _arg1)
386 
387 #define MFX_TRACE_F(_arg1) \
388     MFX_LTRACE_F(MFX_TRACE_LEVEL, _arg1)
389 
390 #define MFX_TRACE_GUID(_guid) \
391     MFX_LTRACE_GUID(MFX_TRACE_LEVEL, _guid)
392 
393 #define MFX_TRACE_BUFFER(_name, _buffer, _size) \
394     MFX_LTRACE_BUFFER(MFX_TRACE_LEVEL, _name, _buffer, _size)
395 
396 /*------------------------------------------------------------------------------*/
397 
398 #ifdef __cplusplus
399 } // extern "C"
400 #endif
401 
402 /*------------------------------------------------------------------------------*/
403 // C++ section
404 
405 #ifdef __cplusplus
406 
407 
408 #ifdef MFX_TRACE_ENABLE
409 // C++ class for BeginTask/EndTask
410 class MFXTraceTask
411 {
412 public:
413     MFXTraceTask(mfxTraceStaticHandle *static_handle,
414                  const char *file_name, mfxTraceU32 line_num,
415                  const char *function_name,
416                  mfxTraceChar* category, mfxTraceLevel level,
417                  const char *task_name,
418                  const bool bCreateID = false);
419     mfxTraceU32 GetID();
420     void        Stop();
421     ~MFXTraceTask();
422 
423 private:
424     bool                    m_bStarted;
425     mfxTraceU32             m_TaskID;
426     mfxTraceStaticHandle    *m_pStaticHandle;
427     mfxTraceTaskHandle      m_TraceTaskHandle;
428 };
429 #endif // #ifdef MFX_TRACE_ENABLE
430 
431 /*------------------------------------------------------------------------------*/
432 // auto tracing of the functions
433 
434 #ifdef MFX_TRACE_ENABLE
435     #define _MFX_AUTO_LTRACE_(_level, _task_name, _bCreateID)       \
436         DISABLE_WARN_HIDE_PREV_LOCAL_DECLARATION                    \
437         static mfxTraceStaticHandle _trace_static_handle;           \
438         MFXTraceTask                _mfx_trace_task(MFX_TRACE_PARAMS, _level, _task_name, _bCreateID); \
439         ROLLBACK_WARN_HIDE_PREV_LOCAL_DECLARATION
440 
441     #define MFX_AUTO_TRACE_STOP()   _mfx_trace_task.Stop()
442     #define MFX_AUTO_TRACE_GETID()  _mfx_trace_task.GetID()
443 #else
444     #define _MFX_AUTO_LTRACE_(_level, _task_name, _bCreateID)
445     #define MFX_AUTO_TRACE_STOP()
446     #define MFX_AUTO_TRACE_GETID()  0
447 #endif
448 
449 #define MFX_AUTO_LTRACE(_level, _task_name)     \
450     _MFX_AUTO_LTRACE_(_level, _task_name, false)
451 
452 #define MFX_AUTO_TRACE(_task_name) \
453     _MFX_AUTO_LTRACE_(MFX_TRACE_LEVEL, _task_name, false)
454 
455 #define MFX_AUTO_TRACE_FUNC() \
456     _MFX_AUTO_LTRACE_(MFX_TRACE_LEVEL, __FUNCTION__, false)
457 
458 #define MFX_AUTO_LTRACE_FUNC(_level) \
459     _MFX_AUTO_LTRACE_(_level, __FUNCTION__, false)
460 
461 #define MFX_AUTO_LTRACE_WITHID(_level, _task_name)     \
462     _MFX_AUTO_LTRACE_(_level, _task_name, true)
463 
464 #define MFX_AUTO_TRACE_WITHID(_task_name) \
465     _MFX_AUTO_LTRACE_(MFX_TRACE_LEVEL, _task_name, true)
466 
467 #endif // ifdef __cplusplus
468 
469 #endif // #ifndef __MFX_TRACE_H__
470