allow users to set process scheduling priority and nice level; fixes #1712
Costa Tsaousis (ktsaou) committed
Feb 4, 2017 at 00:58 UTC
4209bf5c9c6119cf48c286f839aa346de6859460
5 files changed
+131
-25
autogen.sh
+2
-1
@@ -1,2 +1,3 @@
1
-#!/bin/sh
1
+#!/usr/bin/env sh
2
+
3
autoreconf -ivf
configure.ac
+1
@@ -47,6 +47,7 @@ AC_CHECK_FUNCS_ONCE(accept4)
47
AC_CHECK_TYPES([struct timespec, clockid_t], [], [], [[#include <time.h>]])
48
AC_SEARCH_LIBS([clock_gettime], [rt posix4])
49
AC_CHECK_FUNCS([clock_gettime])
50
+AC_CHECK_FUNCS([sched_setscheduler sched_get_priority_min sched_get_priority_max nice])
51
52
# Check system type
53
case "$host_os" in
src/daemon.c
+126
-20
@@ -155,13 +155,15 @@ int become_user(const char *username, int pid_fd)
155
return(0);
156
}
157
158
-void oom_score_adj(int score) {
158
+static void oom_score_adj(void) {
159
+ int score = (int)config_get_number("global", "OOM score", 1000);
160
+
161
int done = 0;
162
int fd = open("/proc/self/oom_score_adj", O_WRONLY);
163
if(fd != -1) {
164
char buf[10 + 1];
165
ssize_t len = snprintfz(buf, 10, "%d", score);
164
- if(write(fd, buf, len) == len) done = 1;
166
+ if(len > 0 && write(fd, buf, (size_t)len) == len) done = 1;
167
close(fd);
168
}
169
@@ -171,25 +173,132 @@ void oom_score_adj(int score) {
173
debug(D_SYSTEM, "Adjusted my Out-Of-Memory score to %d.", score);
174
}
175
174
-int sched_setscheduler_idle(void) {
176
+static void process_nice_level(void) {
177
+#ifdef HAVE_NICE
178
+ int nice_level = (int)config_get_number("global", "process nice level", 19);
179
+ if(nice(nice_level) == -1) error("Cannot set netdata CPU nice level to %d.", nice_level);
180
+ else debug(D_SYSTEM, "Set netdata nice level to %d.", nice_level);
181
+#endif // HAVE_NICE
182
+};
183
+
184
+#ifdef HAVE_SCHED_SETSCHEDULER
185
+
186
+#define SCHED_FLAG_NONE 0x00
187
+#define SCHED_FLAG_PRIORITY_CONFIGURABLE 0x01 // the priority is user configurable
188
+#define SCHED_FLAG_KEEP_AS_IS 0x04 // do not attempt to set policy, priority or nice()
189
+#define SCHED_FLAG_USE_NICE 0x08 // use nice() after setting this policy
190
+
191
+struct sched_def {
192
+ char *name;
193
+ int policy;
194
+ int priority;
195
+ uint8_t flags;
196
+} scheduler_defaults[] = {
197
+
198
+ // the order of array members is important!
199
+ // the first defined is the default used by netdata
200
+
201
+ // the available members are important too!
202
+ // these are all the possible scheduling policies supported by netdata
203
+
204
#ifdef SCHED_IDLE
176
- const struct sched_param param = {
177
- .sched_priority = 0
178
- };
205
+ { "idle", SCHED_IDLE, 0, SCHED_FLAG_NONE },
206
+#endif
207
180
- int i = sched_setscheduler(0, SCHED_IDLE, ¶m);
181
- if(i != 0)
182
- error("Cannot adjust my scheduling priority to IDLE.");
183
- else
184
- debug(D_SYSTEM, "Adjusted my scheduling priority to IDLE.");
208
+#ifdef SCHED_OTHER
209
+ { "nice", SCHED_OTHER, 0, SCHED_FLAG_USE_NICE },
210
+ { "other", SCHED_OTHER, 0, SCHED_FLAG_USE_NICE },
211
+#endif
212
186
- return i;
187
-#else
188
- return -1;
213
+#ifdef SCHED_RR
214
+ { "rr", SCHED_RR, 99, SCHED_FLAG_PRIORITY_CONFIGURABLE },
215
+#endif
216
+
217
+#ifdef SCHED_FIFO
218
+ { "rr", SCHED_FIFO, 99, SCHED_FLAG_PRIORITY_CONFIGURABLE },
219
+#endif
220
+
221
+#ifdef SCHED_BATCH
222
+ { "rr", SCHED_BATCH, 99, SCHED_FLAG_PRIORITY_CONFIGURABLE },
223
+#endif
224
+
225
+ // do not change the scheduling priority
226
+ { "keep", 0, 0, SCHED_FLAG_KEEP_AS_IS },
227
+ { "none", 0, 0, SCHED_FLAG_KEEP_AS_IS },
228
+
229
+ // array termination
230
+ { NULL, 0, 0, 0 }
231
+};
232
+
233
+static void sched_setscheduler_set(void) {
234
+
235
+ if(scheduler_defaults[0].name) {
236
+ const char *name = scheduler_defaults[0].name;
237
+ int policy = scheduler_defaults[0].policy, priority = scheduler_defaults[0].priority;
238
+ uint8_t flags = scheduler_defaults[0].flags;
239
+ int found = 0;
240
+
241
+ // read the configuration
242
+ name = config_get("global", "process scheduling policy", name);
243
+ int i;
244
+ for(i = 0 ; scheduler_defaults[i].name ; i++) {
245
+ if(!strcmp(name, scheduler_defaults[i].name)) {
246
+ found = 1;
247
+ priority = scheduler_defaults[i].priority;
248
+ flags = scheduler_defaults[i].flags;
249
+
250
+ if(flags & SCHED_FLAG_KEEP_AS_IS)
251
+ return;
252
+
253
+ if(flags & SCHED_FLAG_PRIORITY_CONFIGURABLE)
254
+ priority = (int)config_get_number("global", "process scheduling priority", priority);
255
+
256
+#ifdef HAVE_SCHED_GET_PRIORITY_MIN
257
+ if(priority < sched_get_priority_min(policy)) {
258
+ error("scheduler %s priority %d is below the minimum %d. Using the minimum.", name, priority, sched_get_priority_min(policy));
259
+ priority = sched_get_priority_min(policy);
260
+ }
261
+#endif
262
+#ifdef HAVE_SCHED_GET_PRIORITY_MAX
263
+ if(priority > sched_get_priority_max(policy)) {
264
+ error("scheduler %s priority %d is above the maximum %d. Using the maximum.", name, priority, sched_get_priority_max(policy));
265
+ priority = sched_get_priority_max(policy);
266
+ }
267
#endif
268
+ break;
269
+ }
270
+ }
271
+
272
+ if(!found) {
273
+ error("Unknown scheduling policy %s - falling back to nice()", name);
274
+ goto fallback;
275
+ }
276
+
277
+ const struct sched_param param = {
278
+ .sched_priority = priority
279
+ };
280
+
281
+ i = sched_setscheduler(0, policy, ¶m);
282
+ if(i != 0) {
283
+ error("Cannot adjust netdata scheduling policy to %s (%d), with priority %d. Falling back to nice", name, policy, priority);
284
+ }
285
+ else {
286
+ debug(D_SYSTEM, "Adjusted netdata scheduling policy to %s (%d), with priority %d.", name, policy, priority);
287
+ if(!(flags & SCHED_FLAG_USE_NICE))
288
+ return;
289
+ }
290
+ }
291
+
292
+fallback:
293
+ process_nice_level();
294
}
295
+#else
296
+static void sched_setscheduler_set(void) {
297
+ process_nice_level();
298
+}
299
+#endif
300
192
-int become_daemon(int dont_fork, const char *user, int oom_score)
301
+int become_daemon(int dont_fork, const char *user)
302
{
303
if(!dont_fork) {
304
int i = fork();
@@ -239,13 +348,10 @@ int become_daemon(int dont_fork, const char *user, int oom_score)
348
umask(0007);
349
350
// adjust my Out-Of-Memory score
242
- oom_score_adj(oom_score);
351
+ oom_score_adj();
352
353
// never become a problem
245
- if(sched_setscheduler_idle() != 0) {
246
- if(nice(19) == -1) error("Cannot lower my CPU priority.");
247
- else debug(D_SYSTEM, "Set my nice value to 19.");
248
- }
354
+ sched_setscheduler_set();
355
356
if(user && *user) {
357
if(become_user(user, pidfd) != 0) {
src/daemon.h
+1
-1
@@ -8,7 +8,7 @@ extern void sig_handler_reload_health(int signo);
8
9
extern int become_user(const char *username, int pid_fd);
10
11
-extern int become_daemon(int dont_fork, const char *user, int oom_score);
11
+extern int become_daemon(int dont_fork, const char *user);
12
13
extern void netdata_cleanup_and_exit(int i);
14
src/main.c
+1
-3
@@ -338,7 +338,6 @@ int main(int argc, char **argv)
338
int i, check_config = 0;
339
int config_loaded = 0;
340
int dont_fork = 0;
341
- int oom_score = 1000;
341
size_t wanted_stacksize = 0, stacksize = 0;
342
pthread_attr_t attr;
343
@@ -617,7 +616,6 @@ int main(int argc, char **argv)
616
// --------------------------------------------------------------------
617
618
rrd_memory_mode = rrd_memory_mode_id(config_get("global", "memory mode", rrd_memory_mode_name(rrd_memory_mode)));
620
- oom_score = (int)config_get_number("global", "OOM score", oom_score);
619
620
// --------------------------------------------------------------------
621
@@ -757,7 +755,7 @@ int main(int argc, char **argv)
755
#endif /* NETDATA_INTERNAL_CHECKS */
756
757
// fork, switch user, create pid file, set process priority
760
- if(become_daemon(dont_fork, user, oom_score) == -1)
758
+ if(become_daemon(dont_fork, user) == -1)
759
fatal("Cannot daemonize myself.");
760
761
info("netdata started on pid %d.", getpid());