-
Notifications
You must be signed in to change notification settings - Fork 0
/
vga-tools.c
251 lines (201 loc) · 5.04 KB
/
vga-tools.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
/*
* Copyright (C) 2016, CompuLab ltd.
* Author: Andrey Gelman <[email protected]>
* License: GNU GPLv2 or later, at your option
*/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <libgen.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/types.h>
#include <asm-generic/errno-base.h>
#include "common.h"
#include "nvml-tools.h"
#include "sensors.h"
int (*GPU_get_temperature)(int *temp);
#define SYS_PCI_DEVICES "/sys/bus/pci/devices"
#define DRIVER_BUF_SIZE 128
#define PCI_CLASS_VGA 0x030000
typedef struct PciDevice {
struct PciDevice *next;
unsigned int pci_class;
char driver[DRIVER_BUF_SIZE];
} PciDevice;
/*
* Implement visitor pattern on each directory under 'path'.
*/
static void scan_dirs(const char *path, int (*visitor)(DIR *dir, void *arg), void *varg)
{
DIR *root;
DIR *dir;
struct dirent *d;
char buffer[128];
bool keep_searching;
root = opendir(path);
if (root == NULL) {
sloge("%s: could not open directory: %m", path);
return;
}
keep_searching = true;
while (((d = readdir(root)) != NULL) && keep_searching) {
if (!strcmp(".", d->d_name) || !strcmp("..", d->d_name))
continue;
snprintf(buffer, sizeof(buffer), "%s/%s", path, d->d_name);
dir = opendir(buffer);
if (dir == NULL)
continue;
keep_searching = visitor(dir, varg);
closedir(dir);
}
closedir(root);
}
static PciDevice *new_pci_device(PciDevice *dev, const char *drv_name)
{
PciDevice *new_dev;
new_dev = (PciDevice *)malloc(sizeof(PciDevice));
new_dev->next = dev;
if (dev != NULL)
new_dev->pci_class = dev->pci_class;
strcpy(new_dev->driver, drv_name);
return new_dev;
}
/*
* Return:
* 'keep_searching' flag
*/
static int process_dir(DIR *dir, void *arg)
{
PciDevice **ptr_dev = (PciDevice **)arg;
PciDevice *dev = *ptr_dev;
struct dirent *d;
struct dirent *d_class = NULL;
struct dirent *d_driver = NULL;
char buffer[128];
int fd;
int n;
while ((d = readdir(dir)) != NULL) {
if (!strcmp("class", d->d_name))
d_class = d;
else if (!strcmp("driver", d->d_name))
d_driver = d;
}
/* CLASS */
if (d_class == NULL)
return 1;
fd = openat(dirfd(dir), d_class->d_name, O_RDONLY);
if (fd < 0)
return 1;
n = read(fd, buffer, sizeof(buffer));
close(fd);
if (strtol(buffer, NULL, 0) != dev->pci_class)
return 1;
/* DRIVER */
if (d_driver == NULL)
return 1;
n = readlinkat(dirfd(dir), d_driver->d_name, buffer, sizeof(buffer));
if (n < 0)
return 1;
buffer[n] = '\0';
*ptr_dev = new_pci_device(dev, basename(buffer));
return 1;
}
static char *name_list = NULL;
char *vga_driver_name_list(void)
{
PciDevice *vga_dev;
PciDevice *p;
int size;
int size_left;
if (name_list != NULL)
return name_list;
vga_dev = new_pci_device(NULL, "none");
vga_dev->pci_class = PCI_CLASS_VGA;
scan_dirs(SYS_PCI_DEVICES, process_dir, &vga_dev);
/* convert vga_dev linked list into space-separated list of VGA driver names */
size = 16;
size_left = size;
name_list = (char *)calloc(size, 1);
while (vga_dev != NULL) {
if (strlen(vga_dev->driver) > (size_left + 2)) {
size += 16;
size_left += 16;
name_list = realloc(name_list, size);
continue;
}
size_left -= sprintf((name_list + strlen(name_list)), "%s ", vga_dev->driver);
p = vga_dev;
vga_dev = p->next;
free(p);
}
return name_list;
}
static NvmlHandle *nvmlh;
static int nvidia_gpu_get_temperature(int *temp)
{
nvmlReturn_t err;
err = nvmlh->nvmlDeviceGetTemperature(nvmlh->device, NVML_TEMPERATURE_GPU/*no other options*/,
(unsigned int *)temp);
if (err != NVML_SUCCESS)
sloge("nvml: could not get device temperature: %d", err);
return err;
}
/*
* Intel's integrated GPU seemingly does not report temperature,
* however, being integrated on the chip allows us to evaluate
* its temperature as comparable to CPU core temperature.
*/
static int i915_gpu_get_temperature(int *temp)
{
int core_id;
int temp0;
int err;
*temp = 0;
for (core_id = 0; core_id >= 0;) {
err = sensors_coretemp_read(&core_id, &temp0);
if ( err )
break;
if (temp0 > *temp)
*temp = temp0;
}
return err;
}
static int undefined_gpu_get_temperature(int *temp)
{
*temp = 0;
return -EINVAL;
}
void gpu_sensors_init(void)
{
const char *name_list;
name_list = vga_driver_name_list();
/* ignore not identified GPU */
GPU_get_temperature = undefined_gpu_get_temperature;
/*
* Override 'Undefined GPU'.
*
* As name_list might contain more than one VGA driver name,
* the order of appearance below, prioritizes which device
* temperature will be reported to the front panel.
*/
if (strstr(name_list, "nvidia")) {
/* nvidia proprietary driver */
nvmlh = nvml_init();
if (nvmlh != NULL) {
GPU_get_temperature = nvidia_gpu_get_temperature;
on_exit(nvml_cleanup, nvmlh);
}
}
else if (strstr(name_list, "nouveau")) {
/* nouveau open source driver */
if ( !sensors_nouveau_init() )
GPU_get_temperature = sensors_nouveau_read;
}
else if (strstr(name_list, "i915")) {
/* i915 open source driver */
GPU_get_temperature = i915_gpu_get_temperature;
}
}