-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnvpowermizerd.c
256 lines (209 loc) · 6.15 KB
/
nvpowermizerd.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
/*
* nvpowermizerd - a daemon to improve nVidia PowerMizer mode behavior
* Copyright (C) 2014 Mark R. Pariente
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#include <X11/extensions/scrnsaver.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <argp.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
// Argument parser / documentation
const char *argp_program_version = "nvpowermizerd DEVELOPMENT";
const char *argp_program_bug_address = "[email protected]";
static char doc[] = "nvpowermizerd - a daemon to improve nVidia PowerMizer "
"mode behavior";
static struct argp_option options[] = {
{"verbose", 'v', 0, 0, "Show debugging logs" },
{"gpuid", 'g', "GPU-ID", 0, "GPU ID as shown by 'nvidia-settings -q gpus'"},
{"idle-timeout", 'i', "SECONDS", 0, "Time to spend in idle mode before switching to low power mode"},
{0}
};
// How long should the system be idle before we switch to lower power mode
#define SWITCH_TO_LOW_POWER_AFTER_IDLE_MS (20000)
/*
* When in power saving mode we need to react very quickly to user input
* so we poll frequently
*/
#define IDLE_POLL_FREQ_LOW_POWER_MS (10)
/*
* When in high power mode we take it easy when polling to save CPU - this is
* the common case.
*/
#define IDLE_POLL_FREQ_HIGH_POWER_MS (5000)
// Command strings for switching to modes
#define LOW_POWER_CMD_FMT_STR "nvidia-settings -a [gpu:%d]/GPUPowerMizerMode=0"
#define HIGH_POWER_CMD_FMT_STR "nvidia-settings -a [gpu:%d]/GPUPowerMizerMode=1"
static char *lowPowerCmd;
static char *highPowerCmd;
// Whether we're driving the card in high power or low power mode.
typedef enum Mode {
LOW_POWER,
HIGH_POWER,
} Mode;
// Global state
static Mode currentMode = LOW_POWER;
static int gpuID = 0;
static int idleTimeoutMS = SWITCH_TO_LOW_POWER_AFTER_IDLE_MS;
static int highPowerPollFreqMS = IDLE_POLL_FREQ_HIGH_POWER_MS;
// verbose = 0 is LOG only, verbose = 1 also shows LOGDEBUG
static int verbose = 0;
// Debug / logging macros
#define LOGDEBUG(...) \
if (verbose) { \
printf(__VA_ARGS__);\
}
#define LOG(...) \
printf(__VA_ARGS__);
// Global variables for X access
static XScreenSaverInfo *SSInfo;
static Display *display;
static int screen;
// Returns the idle time in milliseconds as reported by X
static inline time_t
GetIdleTimeMS()
{
XScreenSaverQueryInfo(display, RootWindow(display,screen), SSInfo);
return SSInfo->idle;
}
void
SwitchToLowPower()
{
int retVal;
retVal = system(lowPowerCmd);
LOGDEBUG("nvidia-settings returned %d\n", retVal);
LOG("Switched to low power - polling for action every %dms\n",
IDLE_POLL_FREQ_LOW_POWER_MS);
currentMode = LOW_POWER;
}
void
SwitchToHighPower()
{
int retVal;
retVal = system(highPowerCmd);
LOGDEBUG("nvidia-settings returned %d\n", retVal);
LOG("Switched to high power - polling for idle every %dms\n",
highPowerPollFreqMS);
currentMode = HIGH_POWER;
}
void
Cleanup()
{
free(lowPowerCmd);
free(highPowerCmd);
XFree(SSInfo);
XCloseDisplay(display);
}
void
HandleSignal(int signum)
{
LOGDEBUG("Signal %d received.\n", signum);
SwitchToLowPower();
Cleanup();
LOG("Exiting program.\n");
exit(0);
}
static error_t
ParseOption(int key, char *arg, struct argp_state *state)
{
switch (key) {
case 'v':
verbose = 1;
break;
case 'g':
gpuID = atoi(arg);
LOGDEBUG("GPU ID set to %d\n", gpuID);
break;
case 'i':
idleTimeoutMS = atoi(arg) * 1000;
highPowerPollFreqMS = idleTimeoutMS / 4;
LOGDEBUG("Idle timeout set to %d seconds\n", atoi(arg));
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
void
Init()
{
struct sigaction action;
// Install signal handlers
memset(&action, 0, sizeof(action));
action.sa_handler = HandleSignal;
sigaction(SIGTERM, &action, NULL);
sigaction(SIGINT, &action, NULL);
// Initialize X state
SSInfo = XScreenSaverAllocInfo();
if ((display = XOpenDisplay(NULL)) == NULL) {
LOG("Couldn't open X display!\n");
exit(1);
}
screen = DefaultScreen(display);
}
int
PrepareCommands()
{
int retVal;
retVal = asprintf(&lowPowerCmd, LOW_POWER_CMD_FMT_STR, gpuID);
if (retVal < 0) {
return retVal;
}
retVal = asprintf(&highPowerCmd, HIGH_POWER_CMD_FMT_STR, gpuID);
if (retVal < 0) {
return retVal;
}
return 0;
}
// Parameters for the argument parser
static struct argp argpParams = {options, ParseOption, NULL, doc};
int
main(int argc, char *argv[])
{
int idleMS;
Init();
// Parse arguments
argp_parse(&argpParams, argc, argv, 0, 0, NULL);
if (PrepareCommands() != 0) {
LOG("Failed to allocate memory for command strings");
return 1;
}
while (1) {
idleMS = GetIdleTimeMS();
LOGDEBUG("Poll - idle time: %dms Mode: %s\n", idleMS,
currentMode == LOW_POWER ? "Low power" : "High power");
if (currentMode == LOW_POWER) {
if (idleMS < idleTimeoutMS) {
SwitchToHighPower();
// We don't need to poll again until the idle timeout
LOGDEBUG("Delaying first poll to %dms\n", idleTimeoutMS - idleMS + 1);
usleep((idleTimeoutMS - idleMS + 1) * 1000);
} else {
usleep(IDLE_POLL_FREQ_LOW_POWER_MS * 1000);
}
} else { // currentMode == HIGH_POWER
if (idleMS >= idleTimeoutMS) {
SwitchToLowPower();
}
usleep(highPowerPollFreqMS * 1000);
}
}
}