forked from GPUOpen-Tools/gpu_performance_api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogging.h
More file actions
350 lines (290 loc) · 11.8 KB
/
Copy pathlogging.h
File metadata and controls
350 lines (290 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
//==============================================================================
// Copyright (c) 2016-2020 Advanced Micro Devices, Inc. All rights reserved.
/// \author AMD Developer Tools Team
/// \file
/// \brief Logging utility
//==============================================================================
#ifndef GPA_LOGGING_H_
#define GPA_LOGGING_H_
#ifdef _WIN32
#include <Windows.h>
#endif
#ifdef _LINUX
#include <pthread.h>
#include <string.h>
#define EnterCriticalSection pthread_mutex_lock
#define LeaveCriticalSection pthread_mutex_unlock
#include <stdarg.h>
#include <stdio.h>
#endif
#include <string>
#include <sstream>
#include <mutex>
#include <map>
#include <thread>
#include <fstream>
#include "gpu_perf_api_types.h"
#include "gpu_perf_api_function_types.h"
#define ENABLE_TRACING 1 ///< Macro to determine if tracing is enabled
#if ENABLE_TRACING
#undef TRACE_FUNCTION
/// macro for tracing function calls
#define TRACE_FUNCTION(func) ScopeTrace _tempScopeTraceObject(#func) ///< Macro used for tracing functions
#ifdef AMDT_INTERNAL
#undef TRACE_PRIVATE_FUNCTION
#undef TRACE_PRIVATE_FUNCTION_WITH_ARGS
/// macro for tracing private function calls
#define TRACE_PRIVATE_FUNCTION(func) ScopeTrace _tempScopeTraceObject(#func) ///< Macro used for tracing private functions
#define TRACE_PRIVATE_FUNCTION_WITH_ARGS(func, ...) \
TRACE_PRIVATE_FUNCTION(func); \
{ \
std::ostringstream o; \
o << ##__VA_ARGS__; \
gTracerSingleton.OutputFunctionData(o.str().c_str()); \
} ///< Macro used for tracing private function with parameters
#else // public build
#undef TRACE_PRIVATE_FUNCTION
#undef TRACE_PRIVATE_FUNCTION_WITH_ARGS
/// macro for tracing private function calls
#define TRACE_PRIVATE_FUNCTION(func) ///< Macro used for tracing private functions
#define TRACE_PRIVATE_FUNCTION_WITH_ARGS(func, ...) ///< Macro used for tracing private function with parameters
#endif // AMDT_INTERNAL / public build
#else // disable trace functions
#undef TRACE_FUNCTION
#define TRACE_FUNCTION(func) ///< Macro used for tracing functions
#undef TRACE_PRIVATE_FUNCTION
#define TRACE_PRIVATE_FUNCTION(func) ///< Macro used for tracing private functions
#define TRACE_PRIVATE_FUNCTION_WITH_ARGS(func, ...) ///< Macro used for tracing private function with parameters
#endif // trace functions
/// Internal GPA logger function
/// \param[in] logType logging type
/// \param[in] pLogMsg logging message
extern void GPAInternalLogger(GPA_Logging_Type logType, const char* pLogMsg);
#define GPA_INTERNAL_LOG(func, ...) \
std::stringstream logAdditionalMessage; \
logAdditionalMessage << "ThreadId: " << std::this_thread::get_id() << " " << #func << ": " << __VA_ARGS__; \
GPAInternalLogger(GPA_LOGGING_INTERNAL, logAdditionalMessage.str().c_str());
/// Passes log messages of various types to a user-supplied callback function
/// if the user has elected to receive messages of that particular type.
class GPALogger
{
public:
/// Default constructor
GPALogger();
/// Destructor
virtual ~GPALogger();
/// Sets the type of message the user would like to be informed of and a pointer to the callback function.
/// \param loggingType the type of messages to pass on to the callback function
/// \param loggingCallback a pointer to the callback function
void SetLoggingCallback(GPA_Logging_Type loggingType, GPA_LoggingCallbackPtrType loggingCallback);
/// Passes the supplied message to the callback function if the user has accepted that type of message.
/// \param logType the type of message being supplied
/// \param pMessage the message to pass along
void Log(GPA_Logging_Type logType, const char* pMessage);
/// Logs an error message.
/// \param pMessage the message to pass along
inline void LogError(const char* pMessage)
{
Log(GPA_LOGGING_ERROR, pMessage);
}
/// Logs an informational message.
/// \param pMessage the message to pass along
inline void LogMessage(const char* pMessage)
{
Log(GPA_LOGGING_MESSAGE, pMessage);
}
/// Logs a trace message.
/// \param pMessage the message to pass along
inline void LogTrace(const char* pMessage)
{
Log(GPA_LOGGING_TRACE, pMessage);
}
/// Logs a formatted message in internal builds; does nothing in release.
/// \param pMsgFmt the message to format and pass along
void LogDebugMessage(const char* pMsgFmt, ...)
{
// if the supplied message type is among those that the user wants be notified of,
// then pass the message along.
if (GPA_LOGGING_DEBUG_MESSAGE & m_loggingType)
{
EnterCriticalSection(&m_hLock);
// Format string
char buffer[1024 * 50];
va_list arglist;
va_start(arglist, pMsgFmt);
#ifdef WIN32
vsprintf_s(buffer, pMsgFmt, arglist);
#else
vsprintf(buffer, pMsgFmt, arglist);
#endif
va_end(arglist);
Log(GPA_LOGGING_DEBUG_MESSAGE, buffer);
LeaveCriticalSection(&m_hLock);
}
}
/// Logs a formatted error message in debug builds; does nothing in release.
/// \param pMsgFmt the message to format and pass along
void LogDebugError(const char* pMsgFmt, ...)
{
// if the supplied message type is among those that the user wants be notified of,
// then pass the message along.
if (GPA_LOGGING_DEBUG_ERROR & m_loggingType)
{
EnterCriticalSection(&m_hLock);
// Format string
char buffer[1024 * 50];
va_list arglist;
va_start(arglist, pMsgFmt);
#ifdef WIN32
vsprintf_s(buffer, pMsgFmt, arglist);
#else
vsprintf(buffer, pMsgFmt, arglist);
#endif
va_end(arglist);
Log(GPA_LOGGING_DEBUG_ERROR, buffer);
LeaveCriticalSection(&m_hLock);
}
}
/// Logs a formatted error message in debug builds; does nothing in release.
/// \param pMsgFmt the message to format and pass along
void LogDebugTrace(const char* pMsgFmt, ...)
{
// if the supplied message type is among those that the user wants be notified of,
// then pass the message along.
if (GPA_LOGGING_DEBUG_TRACE & m_loggingType)
{
EnterCriticalSection(&m_hLock);
// Format string
char buffer[1024 * 50];
va_list arglist;
va_start(arglist, pMsgFmt);
#ifdef WIN32
vsprintf_s(buffer, pMsgFmt, arglist);
#else
vsprintf(buffer, pMsgFmt, arglist);
#endif
va_end(arglist);
Log(GPA_LOGGING_DEBUG_TRACE, buffer);
LeaveCriticalSection(&m_hLock);
}
}
/// Logs a formatted message in internal builds; does nothing in public builds.
/// \param pMsgFmt the message to format and pass along
void LogDebugCounterDefs(const char* pMsgFmt, ...)
{
// if the supplied message type is among those that the user wants be notified of,
// then pass the message along.
if (GPA_LOGGING_DEBUG_COUNTERDEFS & m_loggingType)
{
EnterCriticalSection(&m_hLock);
// Format string
char buffer[1024 * 50];
va_list arglist;
va_start(arglist, pMsgFmt);
#ifdef WIN32
vsprintf_s(buffer, pMsgFmt, arglist);
#else
vsprintf(buffer, pMsgFmt, arglist);
#endif
va_end(arglist);
Log(GPA_LOGGING_DEBUG_COUNTERDEFS, buffer);
LeaveCriticalSection(&m_hLock);
}
}
/// Checks whether the tracing is enabled or not
/// \return true if either tracing or debug tracing is enabled otherwise false
bool IsTracingEnabled() const
{
if (nullptr != m_loggingCallback)
{
return m_loggingType & GPA_LOGGING_TRACE || m_loggingType & GPA_LOGGING_DEBUG_TRACE;
}
return false;
}
/// Internal logging file stream
std::fstream m_internalLoggingFileStream;
/// Internal logging file
std::string m_internalLogFileName;
protected:
/// User selected logging type that defines what messages they want to be notified of
GPA_Logging_Type m_loggingType;
/// User-supplied callback function
GPA_LoggingCallbackPtrType m_loggingCallback;
/// Internal logger of GPA for debugging purposes
GPA_LoggingCallbackPtrType m_gpaInternalLogger = GPAInternalLogger;
/// Mutex for internal logging flag
std::mutex m_internalLoggingMutex;
/// Internal logging flag
bool m_enableInternalLogging;
#ifdef _WIN32
CRITICAL_SECTION m_hLock; ///< lock for thread-safe access
#endif
#ifdef _LINUX
pthread_mutex_t m_hLock; ///< lock for thread-safe access
#endif
};
/// Singleton instance of the GPALogger class
extern GPALogger g_loggerSingleton;
// define C-style functions for simplified logging
/// macro for logging
#define GPA_Log g_loggerSingleton.Log
/// macro for logging of errors
#define GPA_LogError g_loggerSingleton.LogError
/// macro for logging of messages
#define GPA_LogMessage g_loggerSingleton.LogMessage
/// macro for logging of trace items
#define GPA_LogTrace g_loggerSingleton.LogTrace
/// macro for debug logging of messages
#define GPA_LogDebugMessage g_loggerSingleton.LogDebugMessage
/// macro for debug logging of errors
#define GPA_LogDebugError g_loggerSingleton.LogDebugError
/// macro for debug logging of trace items
#define GPA_LogDebugTrace g_loggerSingleton.LogDebugTrace
/// macro for debug logging of counter defs
#define GPA_LogDebugCounterDefs g_loggerSingleton.LogDebugCounterDefs
/// Utility class for tracing the start and end of functions.
class GPATracer
{
public:
/// Default constructor
GPATracer();
/// Should be called when a function is entered.
/// \param pFunctionName the function that is being entered
void EnterFunction(const char* pFunctionName);
/// Should be called when a function is exited.
/// \param pFunctionName the function that is being left
void LeaveFunction(const char* pFunctionName);
/// Called if a function has additional data to output
/// Information is tabbed under the function
/// \param pData the additional data to output
void OutputFunctionData(const char* pData);
private:
/// Returns the pointer to the tab counter
/// \param[out] pCurrentThreadId thread id of the caller
/// \return pointer to the tab counter
std::map<std::thread::id, int32_t>::iterator GetTabCounter(std::thread::id* pCurrentThreadId);
/// Indicates whether to only show the top level of functions (true), or also show nested function calls (false).
bool m_topLevelOnly;
/// Mutex for the thread and tab counter map
std::mutex m_tracerMutex;
/// Map of the thread and its associated tab counter
std::map<std::thread::id, int32_t> m_threadTabCountMap;
};
/// Singleton instance of the GPATracer class
extern GPATracer gTracerSingleton;
/// Allows for easy tracing of exiting a function.
/// Calls GPATracer::EnterFunction in the constructor
/// and GPATracer::LeaveFunction in the destructor.
class ScopeTrace
{
public:
/// Constructor which calls GPATracer::EnterFunction.
/// \param pTraceFunction the function which is being traced.
ScopeTrace(const char* pTraceFunction);
/// Destructor which calls GPATracer::LeaveFunction.
~ScopeTrace();
protected:
/// Stores the function being traced.
std::string m_traceFunction;
};
#endif //GPA_LOGGING_H_