when running under systemd, keep the scheduling parameters set by systemd, but report them to error.log
Costa Tsaousis (ktsaou) committed
Sep 9, 2018 at 23:59 UTC
8df87f4e1a2ce6130292d159c137a18a591f6f11
3 files changed
+67
-17
configure.ac
+1
-1
@@ -132,7 +132,7 @@ AX_GCC_FUNC_ATTRIBUTE([warn_unused_result])
132
AC_CHECK_TYPES([struct timespec, clockid_t], [], [], [[#include <time.h>]])
133
AC_SEARCH_LIBS([clock_gettime], [rt posix4])
134
AC_CHECK_FUNCS([clock_gettime])
135
-AC_CHECK_FUNCS([sched_setscheduler sched_get_priority_min sched_get_priority_max nice])
135
+AC_CHECK_FUNCS([sched_setscheduler sched_getscheduler sched_getparam sched_get_priority_min sched_get_priority_max getpriority setpriority nice])
136
AC_CHECK_FUNCS([recvmmsg])
137
138
AC_TYPE_INT8_T
src/daemon.c
+61
-7
@@ -152,6 +152,10 @@ static void oom_score_adj(void) {
152
s = config_get(CONFIG_SECTION_GLOBAL, "OOM score", s);
153
if(s && *s && (isdigit(*s) || *s == '-' || *s == '+'))
154
wanted_score = atoll(s);
155
+ else if(s && !strcmp(s, "keep")) {
156
+ info("Out-Of-Memory (OOM) kept as-is (running with %d)", (int) old_score);
157
+ return;
158
+ }
159
else {
160
info("Out-Of-Memory (OOM) score not changed due to non-numeric setting: '%s' (running with %d)", s, (int)old_score);
161
return;
@@ -203,8 +207,6 @@ static void process_nice_level(void) {
207
#endif // HAVE_NICE
208
};
209
206
-#ifdef HAVE_SCHED_SETSCHEDULER
207
-
210
#define SCHED_FLAG_NONE 0x00
211
#define SCHED_FLAG_PRIORITY_CONFIGURABLE 0x01 // the priority is user configurable
212
#define SCHED_FLAG_KEEP_AS_IS 0x04 // do not attempt to set policy, priority or nice()
@@ -228,8 +230,8 @@ struct sched_def {
230
#endif
231
232
#ifdef SCHED_OTHER
231
- { "nice", SCHED_OTHER, 0, SCHED_FLAG_USE_NICE },
233
{ "other", SCHED_OTHER, 0, SCHED_FLAG_USE_NICE },
234
+ { "nice", SCHED_OTHER, 0, SCHED_FLAG_USE_NICE },
235
#endif
236
237
#ifdef SCHED_RR
@@ -252,6 +254,55 @@ struct sched_def {
254
{ NULL, 0, 0, 0 }
255
};
256
257
+
258
+#ifdef HAVE_SCHED_GETSCHEDULER
259
+static void sched_getscheduler_report(void) {
260
+ int sched = sched_getscheduler(0);
261
+ if(sched == -1) {
262
+ error("Cannot get my current process scheduling policy.");
263
+ return;
264
+ }
265
+ else {
266
+ int i;
267
+ for(i = 0 ; scheduler_defaults[i].name ; i++) {
268
+ if(scheduler_defaults[i].policy == sched) {
269
+ if(scheduler_defaults[i].flags & SCHED_FLAG_PRIORITY_CONFIGURABLE) {
270
+ struct sched_param param;
271
+ if(sched_getparam(0, ¶m) == -1) {
272
+ error("Cannot get the process scheduling priority for my policy '%s'", scheduler_defaults[i].name);
273
+ return;
274
+ }
275
+ else {
276
+ info("Running with process scheduling policy '%s', priority %d", scheduler_defaults[i].name, param.sched_priority);
277
+ }
278
+ }
279
+ else if(scheduler_defaults[i].flags & SCHED_FLAG_USE_NICE) {
280
+ #ifdef HAVE_GETPRIORITY
281
+ int n = getpriority(PRIO_PROCESS, 0);
282
+ info("Running with process scheduling policy '%s', nice level %d", scheduler_defaults[i].name, n);
283
+ #else // !HAVE_GETPRIORITY
284
+ info("Running with process scheduling policy '%s'", scheduler_defaults[i].name);
285
+ #endif // !HAVE_GETPRIORITY
286
+ }
287
+ else {
288
+ info("Running with process scheduling policy '%s'", scheduler_defaults[i].name);
289
+ }
290
+
291
+ return;
292
+ }
293
+ }
294
+ }
295
+}
296
+#else // !HAVE_SCHED_GETSCHEDULER
297
+static void sched_getscheduler_report(void) {
298
+#ifdef HAVE_GETPRIORITY
299
+ info("Running with priority %d", getpriority(PRIO_PROCESS, 0));
300
+#endif // HAVE_GETPRIORITY
301
+}
302
+#endif // !HAVE_SCHED_GETSCHEDULER
303
+
304
+#ifdef HAVE_SCHED_SETSCHEDULER
305
+
306
static void sched_setscheduler_set(void) {
307
308
if(scheduler_defaults[0].name) {
@@ -271,7 +322,7 @@ static void sched_setscheduler_set(void) {
322
flags = scheduler_defaults[i].flags;
323
324
if(flags & SCHED_FLAG_KEEP_AS_IS)
274
- return;
325
+ goto report;
326
327
if(flags & SCHED_FLAG_PRIORITY_CONFIGURABLE)
328
priority = (int)config_get_number(CONFIG_SECTION_GLOBAL, "process scheduling priority", priority);
@@ -311,18 +362,21 @@ static void sched_setscheduler_set(void) {
362
else {
363
info("Adjusted netdata scheduling policy to %s (%d), with priority %d.", name, policy, priority);
364
if(!(flags & SCHED_FLAG_USE_NICE))
314
- return;
365
+ goto report;
366
}
367
}
368
369
fallback:
370
process_nice_level();
371
+
372
+report:
373
+ sched_getscheduler_report();
374
}
321
-#else
375
+#else // !HAVE_SCHED_SETSCHEDULER
376
static void sched_setscheduler_set(void) {
377
process_nice_level();
378
}
325
-#endif
379
+#endif // !HAVE_SCHED_SETSCHEDULER
380
381
int become_daemon(int dont_fork, const char *user)
382
{
system/netdata.service.in
+5
-9
@@ -9,7 +9,7 @@ User=netdata
9
Group=netdata
10
RuntimeDirectory=netdata
11
RuntimeDirectoryMode=0775
12
-ExecStart=@sbindir_POST@/netdata -P /run/netdata/netdata.pid -D
12
+ExecStart=@sbindir_POST@/netdata -P /run/netdata/netdata.pid -D -W set global 'process scheduling policy' 'keep' -W set global 'OOM score' 'keep'
13
ExecStartPre=/bin/mkdir -p @localstatedir_POST@/cache/netdata
14
ExecStartPre=/bin/chown -R netdata:netdata @localstatedir_POST@/cache/netdata
15
PermissionsStartOnly=true
@@ -25,20 +25,16 @@ RestartSec=30
25
# netdata (via [global].OOM score in netdata.conf) can only increase the value set here.
26
# To decrease it, set the minimum here and set the same or a higher value in netdata.conf.
27
# Valid values: -1000 (never kill netdata) to 1000 (always kill netdata).
28
-#OOMScoreAdjust=0
28
+OOMScoreAdjust=1000
29
30
-# By default netdata switches to scheduling policy idle, which makes it use CPU, only
31
-# when there is spare available.
30
# Valid policies: other (the system default) | batch | idle | fifo | rr
33
-#CPUSchedulingPolicy=other
31
+CPUSchedulingPolicy=idle
32
35
-# This sets the maximum scheduling priority netdata can set (for policies: rr and fifo).
36
-# netdata (via [global].process scheduling priority in netdata.conf) can only lower this value.
33
+# This sets the scheduling priority (for policies: rr and fifo).
34
# Priority gets values 1 (lowest) to 99 (highest).
35
#CPUSchedulingPriority=1
36
40
-# For scheduling policy 'other' and 'batch', this sets the lowest niceness of netdata.
41
-# netdata (via [global].process nice level in netdata.conf) can only increase the value set here.
37
+# For scheduling policy 'other' and 'batch', this sets the lowest niceness of netdata (-20 highest to 19 lowest).
38
#Nice=0
39
40
[Install]