forked from ifdefelse/ProgPOW
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapamdsysfs.cpp
346 lines (300 loc) · 8.86 KB
/
wrapamdsysfs.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
/*
* Wrapper for AMD SysFS on linux, using adapted code from amdcovc by matszpk
*
* By Philipp Andreas - [email protected]
*/
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string>
#include <cstring>
#include <climits>
#include <limits>
#include <cstdint>
#include <algorithm>
#include <fstream>
#include <sys/types.h>
#include <regex>
#if defined(__linux)
#include <dirent.h>
#endif
#include "wraphelper.h"
#include "wrapamdsysfs.h"
static bool getFileContentValue(const char* filename, unsigned int& value)
{
value = 0;
std::ifstream ifs(filename, std::ios::binary);
std::string line;
std::getline(ifs, line);
char* p = (char*)line.c_str();
char* p2;
errno = 0;
value = strtoul(p, &p2, 0);
if (errno != 0)
return false;
return (p != p2);
}
wrap_amdsysfs_handle * wrap_amdsysfs_create()
{
wrap_amdsysfs_handle *sysfsh = NULL;
#if defined(__linux)
sysfsh = (wrap_amdsysfs_handle *)calloc(1, sizeof(wrap_amdsysfs_handle));
DIR* dirp = opendir("/sys/class/drm");
if (dirp == nullptr)
return NULL;
unsigned int gpucount = 0;
struct dirent* dire;
errno = 0;
while ((dire = readdir(dirp)) != nullptr)
{
if (::strncmp(dire->d_name, "card", 4) != 0)
continue; // is not card directory
const char* p;
for (p = dire->d_name + 4; ::isdigit(*p); p++);
if (*p != 0)
continue; // is not card directory
unsigned int v = ::strtoul(dire->d_name + 4, nullptr, 10);
gpucount = std::max(gpucount, v + 1);
}
if (errno != 0)
{
closedir(dirp);
return NULL;
}
closedir(dirp);
sysfsh->card_sysfs_device_id = (int*)calloc(gpucount, sizeof(int));
sysfsh->sysfs_hwmon_id = (int*)calloc(gpucount, sizeof(int));
// filter AMD GPU cards and create mappings
char dbuf[120];
int cardIndex = 0;
for (unsigned int i = 0; i < gpucount; i++)
{
sysfsh->card_sysfs_device_id[cardIndex] = -1;
sysfsh->sysfs_hwmon_id[cardIndex] = -1;
snprintf(dbuf, 120, "/sys/class/drm/card%u/device/vendor", i);
unsigned int vendorId = 0;
if (!getFileContentValue(dbuf, vendorId))
continue;
if (vendorId != 4098) // if not AMD
continue;
sysfsh->card_sysfs_device_id[cardIndex] = i;
cardIndex++;
}
// Number of AMD cards found we do not care about non AMD cards
sysfsh->sysfs_gpucount = cardIndex;
// Get hwmon directory index
for (int i = 0; i < sysfsh->sysfs_gpucount; i++)
{
int sysfsIdx = sysfsh->card_sysfs_device_id[i];
// Should not happen
if (sysfsIdx < 0) {
free(sysfsh);
return NULL;
}
// search hwmon
errno = 0;
snprintf(dbuf, 120, "/sys/class/drm/card%u/device/hwmon", sysfsIdx);
DIR* dirp = opendir(dbuf);
if (dirp == nullptr) {
free(sysfsh);
return NULL;
}
errno = 0;
struct dirent* dire;
unsigned int hwmonIndex = UINT_MAX;
while ((dire = readdir(dirp)) != nullptr)
{
if (::strncmp(dire->d_name, "hwmon", 5) != 0)
continue; // is not hwmon directory
const char* p;
for (p = dire->d_name + 5; ::isdigit(*p); p++);
if (*p != 0)
continue; // is not hwmon directory
errno = 0;
unsigned int v = ::strtoul(dire->d_name + 5, nullptr, 10);
hwmonIndex = std::min(hwmonIndex, v);
}
if (errno != 0)
{
closedir(dirp);
free(sysfsh);
return NULL;
}
closedir(dirp);
if (hwmonIndex == UINT_MAX) {
free(sysfsh);
return NULL;
}
sysfsh->sysfs_hwmon_id[i] = hwmonIndex;
}
sysfsh->opencl_gpucount = 0;
sysfsh->sysfs_opencl_device_id = (int*)calloc(sysfsh->sysfs_gpucount, sizeof(int));
#if ETH_ETHASHCL
if (sysfsh->sysfs_gpucount > 0) {
//Get and count OpenCL devices.
std::vector<cl::Platform> platforms;
cl::Platform::get(&platforms);
std::vector<cl::Device> platdevs;
for (unsigned p = 0; p < platforms.size(); p++) {
std::string platformName = platforms[p].getInfo<CL_PLATFORM_NAME>();
if (platformName == "AMD Accelerated Parallel Processing") {
platforms[p].getDevices(
CL_DEVICE_TYPE_GPU | CL_DEVICE_TYPE_ACCELERATOR,
&platdevs
);
sysfsh->opencl_gpucount = platdevs.size();
break;
}
}
sysfsh->opencl_sysfs_device_id = (int*)calloc(sysfsh->opencl_gpucount, sizeof(int));
//Map SysFs to opencl devices
for (int i = 0; i < sysfsh->sysfs_gpucount; i++) {
for (unsigned j = 0; j < platdevs.size(); j++) {
cl::Device cldev = platdevs[j];
cl_device_topology_amd topology;
int status = clGetDeviceInfo(cldev(), CL_DEVICE_TOPOLOGY_AMD,
sizeof(cl_device_topology_amd), &topology, NULL);
if (status == CL_SUCCESS) {
if (topology.raw.type == CL_DEVICE_TOPOLOGY_TYPE_PCIE_AMD) {
int gpuindex = sysfsh->card_sysfs_device_id[i];
char dbuf[120];
snprintf(dbuf, 120, "/sys/class/drm/card%u/device/uevent", gpuindex);
std::ifstream ifs(dbuf, std::ios::binary);
std::string line;
int iBus = 0, iDevice = 0, iFunction = 0;
while (std::getline(ifs, line)) {
if (line.length() > 24 && line.substr(0, 13) == "PCI_SLOT_NAME") {
std::string pciId = line.substr(14, 12);
std::vector<std::string> pciParts;
std::string part;
std::size_t prev = 0, pos;
while ((pos = pciId.find_first_of(":.", prev)) != std::string::npos)
{
if (pos > prev)
pciParts.push_back(pciId.substr(prev, pos - prev));
prev = pos + 1;
}
if (prev < pciId.length())
pciParts.push_back(pciId.substr(prev, std::string::npos));
//Format -- DDDD:BB:dd.FF
iBus = atoi(pciParts[1].c_str());
iDevice = atoi(pciParts[2].c_str());
iFunction = atoi(pciParts[3].c_str());
break;
}
}
if (iBus == (int)topology.pcie.bus
&& iDevice == (int)topology.pcie.device
&& iFunction == (int)topology.pcie.function) {
#if 0
printf("[DEBUG] - SYSFS GPU[%d]%d,%d,%d matches OpenCL GPU[%d]%d,%d,%d\n",
i,
iBus,
iDevice,
iFunction,
j, (int)topology.pcie.bus, (int)topology.pcie.device, (int)topology.pcie.function);
#endif
sysfsh->sysfs_opencl_device_id[i] = j;
sysfsh->opencl_sysfs_device_id[j] = i;
}
}
}
}
}
}
#endif
#endif
return sysfsh;
}
int wrap_amdsysfs_destroy(wrap_amdsysfs_handle *sysfsh)
{
free(sysfsh);
return 0;
}
int wrap_amdsysfs_get_gpucount(wrap_amdsysfs_handle *sysfsh, int *gpucount)
{
*gpucount = sysfsh->sysfs_gpucount;
return 0;
}
int wrap_amdsysfs_get_gpu_pci_id(wrap_amdsysfs_handle *sysfsh, int index, char *idbuf, int bufsize)
{
int gpuindex = sysfsh->card_sysfs_device_id[index];
if (gpuindex < 0 || index >= sysfsh->sysfs_gpucount)
return -1;
char dbuf[120];
snprintf(dbuf, 120, "/sys/class/drm/card%u/device/uevent", gpuindex);
std::ifstream ifs(dbuf, std::ios::binary);
std::string line;
while (std::getline(ifs, line))
{
if (line.length() > 24 && line.substr(0, 13) == "PCI_SLOT_NAME") {
memcpy(idbuf, line.substr(14, 12).c_str(), bufsize);
return 0;
}
}
//memcpy(idbuf, "0000:00:00.0", bufsize);//?
return -1;
}
int wrap_amdsysfs_get_tempC(wrap_amdsysfs_handle *sysfsh, int index, unsigned int *tempC)
{
int gpuindex = sysfsh->card_sysfs_device_id[index];
if (gpuindex < 0 || index >= sysfsh->sysfs_gpucount)
return -1;
int hwmonindex = sysfsh->sysfs_hwmon_id[index];
if (hwmonindex < 0)
return -1;
char dbuf[120];
snprintf(dbuf, 120, "/sys/class/drm/card%u/device/hwmon/hwmon%u/temp1_input",
gpuindex, hwmonindex);
unsigned int temp = 0;
getFileContentValue(dbuf, temp);
if (temp > 0)
*tempC = temp / 1000;
return 0;
}
int wrap_amdsysfs_get_fanpcnt(wrap_amdsysfs_handle *sysfsh, int index, unsigned int *fanpcnt)
{
int gpuindex = sysfsh->card_sysfs_device_id[index];
if (gpuindex < 0 || index >= sysfsh->sysfs_gpucount)
return -1;
int hwmonindex = sysfsh->sysfs_hwmon_id[index];
if (hwmonindex < 0)
return -1;
unsigned int pwm = 0, pwmMax = 255, pwmMin = 0;
char dbuf[120];
snprintf(dbuf, 120, "/sys/class/drm/card%u/device/hwmon/hwmon%u/pwm1",
gpuindex, hwmonindex);
getFileContentValue(dbuf, pwm);
snprintf(dbuf, 120, "/sys/class/drm/card%u/device/hwmon/hwmon%u/pwm1_max",
gpuindex, hwmonindex);
getFileContentValue(dbuf, pwmMax);
snprintf(dbuf, 120, "/sys/class/drm/card%u/device/hwmon/hwmon%u/pwm1_min",
gpuindex, hwmonindex);
getFileContentValue(dbuf, pwmMin);
*fanpcnt = (unsigned int)(double(pwm - pwmMin) / double(pwmMax - pwmMin) * 100.0);
return 0;
}
int wrap_amdsysfs_get_power_usage(wrap_amdsysfs_handle *sysfsh, int index, unsigned int *milliwatts)
{
int gpuindex = sysfsh->card_sysfs_device_id[index];
if (gpuindex < 0 || index >= sysfsh->sysfs_gpucount)
return -1;
char dbuf[120];
snprintf(dbuf, 120, "/sys/kernel/debug/dri/%u/amdgpu_pm_info", gpuindex);
std::ifstream ifs(dbuf, std::ios::binary);
std::string line;
while (std::getline(ifs, line))
{
std::smatch sm;
std::regex regex(R"(([\d|\.]+) W \(average GPU\))");
if (std::regex_search(line, sm, regex)) {
if (sm.size() == 2) {
double watt = atof(sm.str(1).c_str());
*milliwatts = (unsigned int)(watt * 1000);
return 0;
}
}
}
return -1;
}