This repository has been archived by the owner on Apr 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
AMDTActivityLogger.cpp
executable file
·536 lines (435 loc) · 13.9 KB
/
AMDTActivityLogger.cpp
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
//==============================================================================
// Copyright (c) 2015 Advanced Micro Devices, Inc. All rights reserved.
/// \author AMD Developer Tools Team
/// \file
/// \brief Implementation of the AMDTActivityLogger lib
//==============================================================================
#include <fstream>
#include <iostream>
#include <iomanip>
#include <map>
#include <sstream>
#include <mutex>
#include <AMDTOSWrappers/Include/osProcess.h>
#include <AMDTOSWrappers/Include/osThread.h>
#include <AMDTOSWrappers/Include/osFile.h>
#include "CXLActivityLogger.h"
#include "AMDTActivityLoggerProfileControl.h"
#include "AMDTGPUProfilerDefs.h"
#include "AMDTActivityLoggerTimeStamp.h"
using namespace std;
#define INDENT " "
#define DEFAULT_GROUP "Default"
/// Class to track a perf marker
class PerfMarkerItem
{
public:
/// Constructor
PerfMarkerItem()
{
m_pOstream = nullptr;
m_depth = 0;
}
/// Destructor
~PerfMarkerItem()
{
delete m_pOstream;
m_pOstream = nullptr;
}
ostream* m_pOstream; ///< output stream used to write the perf marker data
int m_depth; ///< depth of this perf marker
private:
/// Disabled copy contructor
PerfMarkerItem(const PerfMarkerItem& obj);
/// Disabled assignment operator
PerfMarkerItem& operator = (const PerfMarkerItem& obj);
};
std::mutex g_mtx; ///< mutex to protect access to marker API
bool g_bInit = false; ///< global flag indicating if the library has been initialized
bool g_bFinalized = false; ///< global flag indicating if the library has been finalized
bool g_isTimeoutMode = false; ///< global flag indicating if timeout mode is being used
string g_tempPerfMarkerFile; ///< name of the temp perf marker file
string g_perfFileName; ///< name of the perf marker file
map<osThreadId, PerfMarkerItem*> g_perfMarkerItemMap; ///< map from thread id to permarker items
/// ofstream descendant which specifies a file name
class ofstream_with_filename : public ofstream
{
public:
/// Constructor
ofstream_with_filename(const char* file)
: ofstream(file)
{
m_fileName = file;
}
string m_fileName; ///< the filename
};
/// Gets the name of the temp file used to pass params betwee the GPU profiler and the ActivityLogger
/// \param[out] tempParamsFile the name of the params file
void GetTempActivityLoggerParamsFile(osFilePath& tempParamsFile)
{
#ifdef _WIN32
tempParamsFile.setPath(osFilePath::OS_TEMP_DIRECTORY);
tempParamsFile.setFileName(L"rcpdata");
tempParamsFile.setFileExtension(AL_PERFMARKER_EXT);
#else //_LINUX || LINUX
tempParamsFile.setPath(osFilePath::OS_USER_DOCUMENTS);
tempParamsFile.setFileName(L".rcpdata");
tempParamsFile.setFileExtension(AL_PERFMARKER_EXT);
#endif
}
/// Reads the temp params file and sets the global vars
/// \return true on success
bool GetParametersFromFile()
{
bool retVal = false;
osFilePath tempParamsFile;
GetTempActivityLoggerParamsFile(tempParamsFile);
osFile tempFile(tempParamsFile);
retVal = tempFile.open(osChannel::OS_ASCII_TEXT_CHANNEL);
if (retVal)
{
gtASCIIString line;
bool timeoutParamFound = false;
bool tempFileParamFound = false;
bool outputFileParamFound = false;
while (tempFile.readLine(line))
{
int equalPos = line.find("=");
gtASCIIString paramName = line.substr(0, equalPos);
gtASCIIString value = line.substr(equalPos + 1);
if (paramName == "TimeOut")
{
timeoutParamFound = true;
g_isTimeoutMode = value == "True";
}
else if (paramName == "PerfMarkerTempFileBaseName")
{
tempFileParamFound = true;
g_tempPerfMarkerFile = value.asCharArray();
}
else if (paramName == "PerfMarkerOutputFileName")
{
outputFileParamFound = true;
g_perfFileName = value.asCharArray();
}
}
tempFile.close();
retVal = timeoutParamFound && tempFileParamFound && outputFileParamFound;
}
return retVal;
}
/// Gets the current perf marker item
/// \param[out] ppItem the current perf marker item
/// \return the status code
int GetPerfMarkerItem(PerfMarkerItem** ppItem)
{
if (ppItem == NULL)
{
return AL_INTERNAL_ERROR;
}
ostream* os = NULL;
osThreadId tid = osGetUniqueCurrentThreadId();
map<osThreadId, PerfMarkerItem*>::const_iterator it;
it = g_perfMarkerItemMap.find(tid);
if (it != g_perfMarkerItemMap.end())
{
*ppItem = it->second;
return AL_SUCCESS;
}
else
{
if (g_isTimeoutMode)
{
stringstream ss;
// Timeout mode, create a tmp file
string path;
osProcessId pid = osGetCurrentProcessId();
path = g_tempPerfMarkerFile;
ss << path << pid << "_" << tid << "." << AL_PERFMARKER_EXT_NARROW;
os = new(nothrow) ofstream_with_filename(ss.str().c_str());
}
else
{
os = new(nothrow) stringstream();
}
if (os == NULL)
{
return AL_OUT_OF_MEMORY;
}
PerfMarkerItem* pItem = new(nothrow) PerfMarkerItem();
if (pItem == NULL)
{
delete os;
return AL_OUT_OF_MEMORY;
}
pItem->m_depth = 0;
pItem->m_pOstream = os;
g_perfMarkerItemMap.insert(pair<osThreadId, PerfMarkerItem*>(tid, pItem));
*ppItem = pItem;
return AL_SUCCESS;
}
}
extern "C"
int AL_API_CALL amdtInitializeActivityLogger()
{
std::lock_guard<std::mutex> lock(g_mtx);
if (g_bInit)
{
return AL_SUCCESS;
}
if (g_bFinalized)
{
return AL_FINALIZED_ACTIVITY_LOGGER;
}
gtString envVarValue;
bool envVarFound = osGetCurrentProcessEnvVariableValue(AL_CL_AGENT, envVarValue);
bool isTraceAgentFound = false;
if (envVarFound && !envVarValue.isEmpty())
{
isTraceAgentFound = -1 != envVarValue.find(AL_CL_TRACE_AGENT_DLL);
}
if (!isTraceAgentFound)
{
envVarFound = osGetCurrentProcessEnvVariableValue(AL_HSA_TOOLS_LIB, envVarValue);
if (envVarFound && !envVarValue.isEmpty())
{
isTraceAgentFound = -1 != envVarValue.find(AL_HSA_TRACE_AGENT_DLL);
}
}
if (!isTraceAgentFound)
{
return AL_GPU_PROFILER_NOT_DETECTED;
}
g_bInit = true;
if (!GetParametersFromFile())
{
return AL_GPU_PROFILER_MISMATCH;
}
return AL_SUCCESS;
}
const size_t s_DEFAULT_MARKER_NAME_WIDTH = 50; ///< default marker name width
extern "C"
int AL_API_CALL amdtBeginMarker(const char* szMarkerName, const char* szGroupName, const char* szUserString)
{
// TODO: szUserString is currently unused. Need to use it.
(void)(szUserString);
std::lock_guard<std::mutex> lock(g_mtx);
if (!g_bInit)
{
return AL_UNINITIALIZED_ACTIVITY_LOGGER;
}
if (g_bFinalized)
{
return AL_FINALIZED_ACTIVITY_LOGGER;
}
if (szMarkerName == NULL)
{
return AL_NULL_MARKER_NAME;
}
gtASCIIString strGroupName;
if (szGroupName == NULL)
{
strGroupName = DEFAULT_GROUP;
}
else
{
strGroupName = szGroupName;
}
if (strGroupName.isEmpty())
{
strGroupName = DEFAULT_GROUP;
}
gtASCIIString strMarkerName(szMarkerName);
PerfMarkerItem* pItem;
int ret = GetPerfMarkerItem(&pItem);
if (ret != AL_SUCCESS)
{
return ret;
}
strMarkerName.replace(" ", AL_SPACE);
strGroupName.replace(" ", AL_SPACE);
bool fit = static_cast<size_t>(strMarkerName.length()) < s_DEFAULT_MARKER_NAME_WIDTH;
if (fit)
{
(*pItem->m_pOstream) << left << setw(20) << "clBeginPerfMarker" << left << setw(s_DEFAULT_MARKER_NAME_WIDTH) << strMarkerName.asCharArray() << setw(20) << AMDTActivityLoggerTimeStamp::Instance()->GetTimeNanos() << " " << strGroupName.asCharArray() << endl;
}
else
{
// super long marker name
(*pItem->m_pOstream) << "clBeginPerfMarker " << strMarkerName.asCharArray() << " " << AMDTActivityLoggerTimeStamp::Instance()->GetTimeNanos() << " " << strGroupName.asCharArray() << endl;
}
pItem->m_depth++;
return AL_SUCCESS;
}
extern "C"
int AL_API_CALL amdtEndMarker()
{
return amdtEndMarkerEx("", "", "");
}
extern "C"
int AL_API_CALL amdtEndMarkerEx(const char* szMarkerName, const char* szGroupName, const char* szUserString)
{
// TODO: szUserString is currently unused. Need to use it.
(void)(szUserString);
std::lock_guard<std::mutex> lock(g_mtx);
if (!g_bInit)
{
return AL_UNINITIALIZED_ACTIVITY_LOGGER;
}
if (g_bFinalized)
{
return AL_FINALIZED_ACTIVITY_LOGGER;
}
if (szMarkerName == NULL)
{
return AL_NULL_MARKER_NAME;
}
string strGroupName(DEFAULT_GROUP);
if (szGroupName != NULL)
{
strGroupName = szGroupName;
if (strGroupName.empty())
{
strGroupName = DEFAULT_GROUP;
}
}
string strMarkerName(szMarkerName);
PerfMarkerItem* pItem;
int ret = GetPerfMarkerItem(&pItem);
if (ret != AL_SUCCESS)
{
return ret;
}
if (pItem->m_depth <= 0)
{
return AL_UNBALANCED_MARKER;
}
if (strMarkerName.empty() && strGroupName == DEFAULT_GROUP)
{
(*pItem->m_pOstream) << left << setw(20) << "clEndPerfMarker" << left << setw(20) << AMDTActivityLoggerTimeStamp::Instance()->GetTimeNanos() << endl;
}
else
{
/// the marker name must not be an empty string
if (strMarkerName.empty())
{
return AL_NULL_MARKER_NAME;
}
bool fit = strMarkerName.length() < s_DEFAULT_MARKER_NAME_WIDTH;
if (fit)
{
(*pItem->m_pOstream) << left << setw(20) << "clEndPerfMarkerEx" << setw(20) << AMDTActivityLoggerTimeStamp::Instance()->GetTimeNanos() << left << setw(s_DEFAULT_MARKER_NAME_WIDTH) << strMarkerName << " " << strGroupName << endl;
}
else
{
// super long marker name
(*pItem->m_pOstream) << "clEndPerfMarkerEx " << setw(20) << AMDTActivityLoggerTimeStamp::Instance()->GetTimeNanos() << " " << strMarkerName << " " << strGroupName << endl;
}
}
pItem->m_depth--;
return AL_SUCCESS;
}
/// Helper function to count the number of newlines in a string
/// \param str the input string
/// \return the number of newlines in the string
int GetNumLines(const string& str)
{
int ret = 0;
for (size_t i = 0; i < str.length(); i++)
{
if (str[i] == '\n')
{
ret++;
}
}
return ret;
}
extern "C"
int AL_API_CALL amdtFinalizeActivityLogger()
{
std::lock_guard<std::mutex> lock(g_mtx);
if (g_bFinalized)
{
return AL_SUCCESS;
}
if (g_bInit)
{
ofstream fout;
fout.open(g_perfFileName.c_str());
if (!fout.fail())
{
// write header
fout << "=====Perfmarker Output=====\n";
for (map<osThreadId, PerfMarkerItem*>::iterator it = g_perfMarkerItemMap.begin(); it != g_perfMarkerItemMap.end(); ++it)
{
string content;
// thread ID
fout << it->first << endl;
if (it->second->m_depth != 0)
{
cout << "[Thread " << it->first << "] Unbalanced PerfMarker detected.\n";
}
if (g_isTimeoutMode)
{
ofstream_with_filename* pOfstream = dynamic_cast<ofstream_with_filename*>(it->second->m_pOstream);
pOfstream->close();
gtString ofstreamFileName;
ofstreamFileName.fromASCIIString(pOfstream->m_fileName.c_str());
osFilePath markerFilePath;
markerFilePath.setFullPathFromString(ofstreamFileName);
osFile markerFile(markerFilePath);
markerFile.open(osChannel::OS_ASCII_TEXT_CHANNEL);
gtASCIIString fileContents;
markerFile.readIntoString(fileContents);
markerFile.close();
content.assign(fileContents.asCharArray());
remove(pOfstream->m_fileName.c_str());
}
else
{
content = dynamic_cast<stringstream*>(it->second->m_pOstream)->str();
}
// num of markers
fout << GetNumLines(content) << endl;
fout << content;
delete it->second;
}
g_perfMarkerItemMap.clear();
fout.close();
g_bFinalized = true;
return AL_SUCCESS;
}
else
{
return AL_FAILED_TO_OPEN_OUTPUT_FILE;
}
}
else
{
return AL_UNINITIALIZED_ACTIVITY_LOGGER;
}
}
extern "C"
int AL_API_CALL amdtStopProfiling(amdtProfilingControlMode profilingControlMode)
{
int result = AL_SUCCESS;
result = AMDTActivityLoggerProfileControl::Instance()->StopProfiling(profilingControlMode);
return result;
}
extern "C"
int AL_API_CALL amdtResumeProfiling(amdtProfilingControlMode profilingControlMode)
{
int result = AL_SUCCESS;
result = AMDTActivityLoggerProfileControl::Instance()->ResumeProfiling(profilingControlMode);
return result;
}
extern "C"
int AL_API_CALL amdtStopProfilingEx()
{
return amdtStopProfiling(AMDT_CPU_PROFILING);
}
extern "C"
int AL_API_CALL amdtResumeProfilingEx()
{
return amdtResumeProfiling(AMDT_CPU_PROFILING);
}