-
Notifications
You must be signed in to change notification settings - Fork 0
/
vegachiller.c
220 lines (174 loc) · 4.58 KB
/
vegachiller.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
#include <getopt.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "amd.h"
#include "control.h"
#include "controls/curve/curve.h"
#define MAX_DEVICES 1
static volatile int terminate_req = 0;
void setTerminateReq(int sig)
{
terminate_req = 1;
}
struct Args {
int verbose; // bool
int interval; // ms
const char *dev[MAX_DEVICES];
const char *name[MAX_DEVICES];
const char *parameters[MAX_DEVICES];
const char *type[MAX_DEVICES];
};
struct Args defaultArgs()
{
struct Args args = {.verbose = 0, .interval = 6000};
return args;
}
int measureAmd(const char *path, struct Measurements *m)
{
int (*todo[])(const char *, int *) = {amdGetTemp, amdGetBusyPercent, amdGetPowerAvg,
amdGetFanMinRPM, amdGetFanMaxRPM};
int *dsts[] = {&m->temp, &m->busy, &m->power_avg, &m->fan_min, &m->fan_max};
for (int i = 0; i != sizeof(dsts) / sizeof(*dsts); i++) {
int err = todo[i](path, dsts[i]);
if (err != 0)
return err;
}
return 0;
}
struct Args parseArgs(int argc, char **argv)
{
struct Args args = defaultArgs();
int devices = -1;
for (;;) {
int option_index = 0;
static struct option long_options[] = {{"device", required_argument, 0, 'd'},
{"name", required_argument, 0, 'l'},
{"interval", required_argument, 0, 'i'},
{"verbose", no_argument, 0, 'v'},
{"parameters", required_argument, 0, 'p'},
{"type", required_argument, 0, 't'},
{0, 0, 0, 0}};
int c = getopt_long(argc, argv, "d:i:l:p:t:v", long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 'd':
++devices;
args.dev[devices] = optarg;
args.name[devices] = "<unnamed>";
args.parameters[devices] = "";
args.type[devices] = NULL;
break;
case 'i':
args.interval = atoi(optarg);
break;
case 'l':
if (devices < 0) {
fprintf(stderr, "error: provide device path first");
exit(1);
}
args.name[devices] = optarg;
break;
case 'p':
if (devices < 0) {
fprintf(stderr, "error: provide device path first");
exit(1);
}
args.parameters[devices] = optarg;
break;
case 't':
if (devices < 0) {
fprintf(stderr, "error: provide device path first");
exit(1);
}
args.type[devices] = optarg;
break;
case 'v':
args.verbose = 1;
break;
default:
fprintf(stderr, "error: unknown parameter %d. Exiting", c);
exit(1);
}
}
return args;
}
int run(struct Control *const ctrl)
{
const useconds_t usec = ctrl->interval * 1000;
int err = 0;
if (ctrl->init(&ctrl->state) != 0) {
fprintf(stderr, "error: failed to initialize control\n");
goto fail;
}
if (ctrl->parseParameters(ctrl->state, ctrl->parameters) != 0) {
fprintf(stderr, "error: failed to parse parameters\n");
goto fail;
}
int lastPWM = -1;
fprintf(stderr, "Switching to manual control for '%s' (%s)\n", ctrl->card.name,
ctrl->card.path);
amdSetControlMode(ctrl->card.path, MANUAL);
while (!terminate_req) {
struct Measurements m;
struct Action action;
if(measureAmd(ctrl->card.path, &m) != 0) {
fprintf(stderr, "error: cannot measure parameters of card");
goto fail;
}
if (ctrl->verbose)
fprintf(stderr, "info: temp: %d °C pwm: %d (range 0-255)\n", m.temp / 1000,
lastPWM);
if (ctrl->control(ctrl->state, &m, &action) != 0) {
fprintf(stderr, "error: control function failed\n");
goto fail;
}
if (action.pwm < 0 || action.pwm > 255) {
fprintf(stderr, "error: returned out-of-range value\n");
goto fail;
}
if (lastPWM != action.pwm) {
if (ctrl->verbose)
fprintf(stderr, "info: adjusting Fan PWM to %d\n", action.pwm);
amdSetFanPWM(ctrl->card.path, lastPWM = action.pwm);
}
usleep(usec);
}
goto good;
fail:
err = 1;
good:
fprintf(stderr, "Switching to automatic control for '%s' (%s)\n", ctrl->card.name,
ctrl->card.path);
amdSetControlMode(ctrl->card.path, AUTOMATIC);
ctrl->finalize(ctrl->state);
return err;
}
int main(int argc, char **argv)
{
struct Args args = parseArgs(argc, argv);
if (args.type[0] == NULL) {
fprintf(stderr, "error: select a control type.\n");
return 1;
}
struct Control ctrl;
if (strcmp(args.type[0], "curve") == 0) {
newCurveControl(&ctrl);
} else {
fprintf(stderr, "error: no such control type.\n");
return 1;
}
newCard(&ctrl.card, args.name[0], args.dev[0]);
ctrl.interval = args.interval;
ctrl.verbose = args.verbose;
ctrl.parameters = args.parameters[0];
signal(SIGINT, setTerminateReq);
signal(SIGTERM, setTerminateReq);
signal(SIGSEGV, setTerminateReq);
signal(SIGQUIT, setTerminateReq);
run(&ctrl);
return 0;
}