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 4
/
GPUPerfAPIUtil.cpp
executable file
·141 lines (115 loc) · 3.73 KB
/
GPUPerfAPIUtil.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
//==============================================================================
// Copyright (c) 2016 Advanced Micro Devices, Inc. All rights reserved.
/// \author AMD Developer Tools Team
/// \file
/// \brief Defines the entrypoints to the GPUPerfAPIUtil library.
//==============================================================================
#include "GPUPerfAPI.h"
#include "GPUPerfAPIUtil.h"
#ifdef _WIN32
#include <windows.h>
#include <atlbase.h>
#pragma warning( disable : 4996 )
#endif
#ifdef _LINUX
#include <string.h>
typedef int errno_t;
extern errno_t fopen_s(FILE** pFile, const char* pFilename, const char* pMode);
#endif // _LINUX
GPA_Status GPA_EnableCountersFromFile(GPUPerfAPILoader* pLoader, const char* pFileName, gpa_uint32* pCountersRead, const char** ppErrorText, const char* pActiveSectionLabel)
{
*ppErrorText = NULL;
if (!pLoader || !pFileName || !pCountersRead)
{
*ppErrorText = "null pointer";
return GPA_STATUS_ERROR_NULL_POINTER;
}
if (!pLoader->Loaded())
{
*ppErrorText = "loader not loaded";
return GPA_STATUS_ERROR_FAILED;
}
(*pCountersRead) = 0;
FILE* pFile;
fopen_s(&pFile, pFileName, "r");
if (!pFile)
{
*ppErrorText = "could not open file for reading";
return GPA_STATUS_ERROR_NOT_FOUND;
}
bool usingSectionLabel;
if (pActiveSectionLabel && strlen(pActiveSectionLabel))
{
usingSectionLabel = true;
}
else
{
usingSectionLabel = false;
}
char readBuf[2048];
int scanResult = fscanf(pFile, "%s", readBuf);
bool skippingSection = false;
while (scanResult > 0)
{
if (usingSectionLabel && readBuf[0] == '[')
{
// handle [label] section
char* closingBracket = strchr(readBuf, ']');
if (closingBracket)
{
if (strncmp(readBuf + 1, pActiveSectionLabel, strlen(pActiveSectionLabel)) == 0)
{
// matches, so read next string
if (nullptr != fgets(readBuf, sizeof(readBuf), pFile))
{
scanResult = fscanf(pFile, "%s", &readBuf[0]);
continue;
}
else
{
*ppErrorText = "uanble to read a string from the file";
fclose(pFile);
return GPA_STATUS_ERROR_FAILED;
}
}
else
{
// skip all text until next []
skippingSection = true;
}
}
else
{
*ppErrorText = "did not find closing ] in a section label";
fclose(pFile);
return GPA_STATUS_ERROR_FAILED;
}
}
if (readBuf[0] == ';' || skippingSection)
{
if (nullptr != fgets(readBuf, sizeof(readBuf), pFile))
{
scanResult = fscanf(pFile, "%s", &readBuf[0]);
continue;
}
else
{
*ppErrorText = "uanble to read a string from the file";
fclose(pFile);
return GPA_STATUS_ERROR_FAILED;
}
}
// read a valid counter name, enable it
(*pCountersRead)++;
GPA_Status s = pLoader->GPA_EnableCounterStr(readBuf);
if (s != GPA_STATUS_OK)
{
*ppErrorText = "could not enable counter";
fclose(pFile);
return GPA_STATUS_ERROR_FAILED;
}
scanResult = fscanf(pFile, "%s", &readBuf[0]);
}
fclose(pFile);
return GPA_STATUS_OK;
}