forked from aceneun/sgminer-gm-x16r
-
Notifications
You must be signed in to change notification settings - Fork 13
/
sysfs-gpu-controls.c
339 lines (270 loc) · 9.28 KB
/
sysfs-gpu-controls.c
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
#include <stdint.h>
#include <stdbool.h>
#ifdef _MSC_VER
// well...
#else
#include <dirent.h>
#endif
#include "miner.h"
bool has_sysfs_hwcontrols = false;
bool opt_reorder = false;
int opt_hysteresis = 3;
int opt_targettemp = 80;
int opt_overheattemp = 90;
int sysfs_gpu_engineclock(int gpu) { return 0; }
int sysfs_gpu_memclock(int gpu) { return 0; }
float sysfs_gpu_vddc(int gpu) { return 0; }
int sysfs_gpu_activity(int gpu) { return 0; }
int sysfs_gpu_fanspeed(int gpu) { return(-1); }
int sysfs_set_vddc(int gpu, float fVddc) { return 0; }
int sysfs_set_engineclock(int gpu, int iEngineClock) { return 0; }
int sysfs_set_memoryclock(int gpu, int iMemoryClock) { return 0; }
int sysfs_set_powertune(int gpu, int iPercentage) { return 0; }
#ifndef __linux__
void GetGPUHWMonPath(char **HWMonPath, uint32_t GPUIdx)
{
*HWMonPath = NULL;
}
#else
// Must be freed by caller unless NULL
void GetGPUHWMonPath(char **HWMonPath, uint32_t GPUIdx)
{
DIR *hwmon;
char TempPath[256];
struct dirent *inner_hwmon;
// Set it to NULL (indicating failure) until we need to use it
*HWMonPath = NULL;
sprintf(TempPath, "/sys/class/drm/card%d/device/hwmon", GPUIdx);
hwmon = opendir(TempPath);
if(hwmon == NULL)
return;
for(;;)
{
inner_hwmon = readdir(hwmon);
if (inner_hwmon == NULL)
return;
if (inner_hwmon->d_type != DT_DIR)
continue;
if (!memcmp(inner_hwmon->d_name, "hwmon", 5))
break;
}
*HWMonPath = (char *)malloc(sizeof(char) * (256 + strlen(inner_hwmon->d_name)));
sprintf(*HWMonPath, "/sys/class/drm/card%d/device/hwmon/%s", GPUIdx, inner_hwmon->d_name);
return;
}
#endif
int ReadSysFSFile(uint8_t *Buffer, char *Filename, uint32_t BufSize)
{
FILE *fd;
int BytesRead;
fd = fopen(Filename, "rb");
if(!fd)
return -1;
BytesRead = fread(Buffer, sizeof(uint8_t), BufSize, fd);
fclose(fd);
return BytesRead;
}
int WriteSysFSFile(uint8_t *Buffer, char *Filename, uint32_t BufSize)
{
FILE *fd;
int BytesWritten;
fd = fopen(Filename, "wb");
if(!fd)
return -1;
BytesWritten = fwrite(Buffer, sizeof(uint8_t), BufSize, fd);
fclose(fd);
return BytesWritten;
}
float sysfs_gpu_temp(int gpu)
{
char TempFileName[513];
char TempBuf[33];
int BytesRead;
snprintf(TempFileName, 512, "%s/temp1_input", gpus[gpu].sysfs_info.HWMonPath);
BytesRead = ReadSysFSFile((uint8_t*)TempBuf, TempFileName, 32);
if (BytesRead <= 0)
return 0.f;
TempBuf[BytesRead] = 0x00;
return strtof(TempBuf, NULL) / 1000.f;
}
int fanpercent_to_speed(int gpu, float percent)
{
int min = gpus[gpu].sysfs_info.MinFanSpeed;
int max = gpus[gpu].sysfs_info.MaxFanSpeed;
float range = max - min;
float res = percent / 100.f * range + min + 0.5f;
return res;
}
float fanspeed_to_percent(int gpu, int speed)
{
int min = gpus[gpu].sysfs_info.MinFanSpeed;
int max = gpus[gpu].sysfs_info.MaxFanSpeed;
float range = max - min;
float res = (speed - min) / range * 100.f;
return res;
}
float sysfs_gpu_fanpercent(int gpu)
{
char PWM1FileName[513], SpeedStr[33];
int BytesRead, Speed;
snprintf(PWM1FileName, 512, "%s/pwm1", gpus[gpu].sysfs_info.HWMonPath);
BytesRead = ReadSysFSFile((uint8_t*)SpeedStr, PWM1FileName, 32);
if(BytesRead <= 0)
return -1;
SpeedStr[BytesRead] = 0x00;
Speed = strtoul(SpeedStr, NULL, 10);
return fanspeed_to_percent(gpu, Speed);
}
int sysfs_set_fanspeed(int gpu, float FanSpeed)
{
char PWM1FileName[513], Setting[33];
int speed = fanpercent_to_speed(gpu, FanSpeed);
gpus[gpu].sysfs_info.TgtFanSpeed = FanSpeed;
snprintf(PWM1FileName, 512, "%s/pwm1", gpus[gpu].sysfs_info.HWMonPath);
snprintf(Setting, 32, "%d", speed);
return (WriteSysFSFile((uint8_t*)Setting, PWM1FileName, strlen(Setting)) > 0 ? 0 : -1);
}
/* Returns whether the fanspeed is optimal already or not. The fan_window bool
* tells us whether the current fanspeed is in the target range for fanspeeds.
*/
static bool sysfs_fan_autotune(int gpu, int temp, float fanpercent, int lasttemp, bool *fan_window)
{
struct cgpu_info *cgpu = &gpus[gpu];
gpu_sysfs_info *SysFSInfo = &gpus[gpu].sysfs_info;
int tdiff = temp - lasttemp;
int top = gpus[gpu].gpu_fan;
int bot = gpus[gpu].min_fan;
float newpercent = SysFSInfo->TgtFanSpeed;//fanpercent;
float iMin = 0, iMax = 100;
if (temp > SysFSInfo->OverHeatTemp && fanpercent < iMax) {
applog(LOG_WARNING, "Overheat detected on GPU %d, increasing fan to 100%% (temp was %d, overtemp is %d)\n", gpu, temp, SysFSInfo->OverHeatTemp);
newpercent = iMax;
dev_error(cgpu, REASON_DEV_OVER_HEAT);
}
else if (temp > SysFSInfo->TargetTemp && fanpercent < top && tdiff >= 0) {
applog(LOG_DEBUG, "Temperature over target, increasing fanspeed");
if (temp > SysFSInfo->TargetTemp + opt_hysteresis)
newpercent = SysFSInfo->TgtFanSpeed + 10;
else
newpercent = SysFSInfo->TgtFanSpeed + 5;
if (newpercent > top)
newpercent = top;
}
else if (fanpercent > bot && temp < SysFSInfo->TargetTemp - opt_hysteresis) {
/* Detect large swings of 5 degrees or more and change fan by
* a proportion more */
if(tdiff <= 0) {
applog(LOG_DEBUG, "Temperature %d degrees below target, decreasing fanspeed", opt_hysteresis);
newpercent = SysFSInfo->TgtFanSpeed - 1 + tdiff / 5;
}
else if (tdiff >= 5) {
applog(LOG_DEBUG, "Temperature climbed %d while below target, increasing fanspeed", tdiff);
newpercent = SysFSInfo->TgtFanSpeed + tdiff / 5;
}
}
else {
/* We're in the optimal range, make minor adjustments if the
* temp is still drifting */
if (fanpercent > bot && tdiff < 0 && lasttemp < SysFSInfo->TargetTemp) {
applog(LOG_DEBUG, "Temperature dropping while in target range, decreasing fanspeed");
newpercent = SysFSInfo->TgtFanSpeed + tdiff;
}
else if (fanpercent < top && tdiff > 0 && temp > SysFSInfo->TargetTemp - opt_hysteresis) {
applog(LOG_DEBUG, "Temperature rising while in target range, increasing fanspeed");
newpercent = SysFSInfo->TgtFanSpeed + tdiff;
}
}
if (newpercent > iMax)
newpercent = iMax;
else if (newpercent < iMin)
newpercent = iMin;
if (newpercent <= top)
*fan_window = true;
else
*fan_window = false;
if (newpercent != fanpercent) {
applog(LOG_INFO, "Setting GPU %d fan percentage to %d", gpu, newpercent);
set_fanspeed(gpu, newpercent);
/* If the fanspeed is going down and we're below the top speed,
* consider the fan optimal to prevent minute changes in
* fanspeed delaying GPU engine speed changes */
if (newpercent < fanpercent && *fan_window)
return true;
return false;
}
return true;
}
void sysfs_gpu_autotune(int gpu, enum dev_enable *denable)
{
bool dummy;
int temp = sysfs_gpu_temp(gpu);
sysfs_fan_autotune(gpu, temp, gpus[gpu].sysfs_info.TgtFanSpeed, gpus[gpu].sysfs_info.LastTemp, &dummy);
gpu[gpus].sysfs_info.LastTemp = temp;
}
bool sysfs_gpu_stats(int gpu, float *temp, int *engineclock, int *memclock, float *vddc,
int *activity, int *fanspeed, int *fanpercent, int *powertune)
{
if (!gpus[gpu].has_sysfs_hwcontrols)
return false;
*temp = gpu_temp(gpu);
*fanspeed = 0;
*fanpercent = gpu_fanpercent(gpu);
*engineclock = 0;
*memclock = 0;
*vddc = 0;
*activity = 0;
return true;
}
bool init_sysfs_hwcontrols(int nDevs)
{
for(int i = 0; i < nDevs; ++i)
{
// Set gpus[i].sysfs_info.HWMonPath
GetGPUHWMonPath(&gpus[i].sysfs_info.HWMonPath, i);
if(gpus[i].sysfs_info.HWMonPath)
{
char PWMSettingBuf[33];
char FileNameBuf[513];
int BytesRead;
bool MinFanReadSuccess = false;
snprintf(FileNameBuf, 512, "%s/pwm1_min", gpus[i].sysfs_info.HWMonPath);
BytesRead = ReadSysFSFile((uint8_t*)PWMSettingBuf, FileNameBuf, 512);
if(BytesRead > 0)
{
PWMSettingBuf[BytesRead] = 0x00;
gpus[i].sysfs_info.MinFanSpeed = strtoul(PWMSettingBuf, NULL, 10);
MinFanReadSuccess = true;
}
snprintf(FileNameBuf, 512, "%s/pwm1_max", gpus[i].sysfs_info.HWMonPath);
BytesRead = ReadSysFSFile((uint8_t*)PWMSettingBuf, FileNameBuf, 512);
if((BytesRead > 0) && MinFanReadSuccess)
{
gpus[i].has_sysfs_hwcontrols = true;
PWMSettingBuf[BytesRead] = 0x00;
gpus[i].sysfs_info.MaxFanSpeed = strtoul(PWMSettingBuf, NULL, 10);
}
gpus[i].sysfs_info.LastTemp = sysfs_gpu_temp(i);
if(!gpus[i].sysfs_info.OverHeatTemp) gpus[i].sysfs_info.OverHeatTemp = opt_overheattemp;
if(!gpus[i].sysfs_info.TargetTemp) gpus[i].sysfs_info.TargetTemp = opt_targettemp;
}
if(gpus[i].has_sysfs_hwcontrols) has_sysfs_hwcontrols = true;
}
if(has_sysfs_hwcontrols)
{
gpu_temp = &sysfs_gpu_temp;
gpu_engineclock = &sysfs_gpu_engineclock;
gpu_memclock = &sysfs_gpu_memclock;
gpu_vddc = &sysfs_gpu_vddc;
gpu_activity = &sysfs_gpu_activity;
gpu_fanspeed = &sysfs_gpu_fanspeed;
gpu_fanpercent = &sysfs_gpu_fanpercent;
set_powertune = &sysfs_set_powertune;
set_vddc = &sysfs_set_vddc;
set_fanspeed = &sysfs_set_fanspeed;
set_engineclock = &sysfs_set_engineclock;
set_memoryclock = &sysfs_set_memoryclock;
gpu_stats = &sysfs_gpu_stats;
gpu_autotune = &sysfs_gpu_autotune;
}
return(has_sysfs_hwcontrols);
}