netdata now adjusts its scheduling priority to IDLE and its Out-Of-Memory score to 1000
Costa Tsaousis committed
Aug 12, 2016 at 14:05 UTC
30ce32f31ea23ef2166cd24e4a9a00aac34a9946
2 files changed
+45
-10
src/daemon.c
+45
-7
@@ -1,4 +1,5 @@
1
#include "common.h"
2
+#include <sched.h>
3
4
char pidfile[FILENAME_MAX + 1] = "";
5
@@ -102,6 +103,36 @@ int become_user(const char *username, int access_fd, int output_fd, int error_fd
103
return(0);
104
}
105
106
+void oom_score_adj(int score) {
107
+ int done = 0;
108
+ int fd = open("/proc/self/oom_score_adj", O_WRONLY);
109
+ if(fd != -1) {
110
+ char buf[10 + 1];
111
+ ssize_t len = snprintfz(buf, 10, "%d", score);
112
+ if(write(fd, buf, len) == len) done = 1;
113
+ close(fd);
114
+ }
115
+
116
+ if(!done)
117
+ error("Cannot adjust my Out-Of-Memory score to %d.", score);
118
+ else
119
+ info("Adjusted my Out-Of-Memory score to %d.", score);
120
+}
121
+
122
+int sched_setscheduler_idle(void) {
123
+ const struct sched_param param = {
124
+ .sched_priority = 0
125
+ };
126
+
127
+ int i = sched_setscheduler(0, SCHED_IDLE, ¶m);
128
+ if(i != 0)
129
+ error("Cannot adjust my scheduling priority to IDLE.");
130
+ else
131
+ info("Adjusted my scheduling priority to IDLE.");
132
+
133
+ return i;
134
+}
135
+
136
int become_daemon(int dont_fork, int close_all_files, const char *user, const char *input, const char *output, const char *error, const char *access, int *access_fd, FILE **access_fp)
137
{
138
fflush(NULL);
@@ -192,11 +223,9 @@ int become_daemon(int dont_fork, int close_all_files, const char *user, const ch
223
perror("Cannot become session leader.");
224
exit(2);
225
}
195
- }
226
197
- // fork() again
198
- if(!dont_fork) {
199
- int i = fork();
227
+ // fork() again
228
+ i = fork();
229
if(i == -1) {
230
perror("cannot fork");
231
exit(1);
@@ -206,9 +235,6 @@ int become_daemon(int dont_fork, int close_all_files, const char *user, const ch
235
}
236
}
237
209
- // Set new file permissions
210
- umask(0);
211
-
238
// close all files
239
if(close_all_files) {
240
int i;
@@ -286,6 +312,18 @@ int become_daemon(int dont_fork, int close_all_files, const char *user, const ch
312
else error("Failed to open pidfile '%s'.", pidfile);
313
}
314
315
+ // Set new file permissions
316
+ umask(0002);
317
+
318
+ // adjust my Out-Of-Memory score
319
+ oom_score_adj(1000);
320
+
321
+ // never become a problem
322
+ if(sched_setscheduler_idle() != 0) {
323
+ if(nice(19) == -1) error("Cannot lower my CPU priority.");
324
+ else info("Set my nice value to 19.");
325
+ }
326
+
327
if(user && *user) {
328
if(become_user(user, (access_fd)?*access_fd:-1, output_fd, error_fd, pidfd) != 0) {
329
error("Cannot become user '%s'. Continuing as we are.", user);
src/main.c
-3
@@ -621,9 +621,6 @@ int main(int argc, char **argv)
621
create_listen_sockets();
622
}
623
624
- // never become a problem
625
- if(nice(20) == -1) error("Cannot lower my CPU priority.");
626
-
624
if(become_daemon(dont_fork, 0, user, input_log_file, output_log_file, error_log_file, access_log_file, &access_fd, &stdaccess) == -1)
625
fatal("Cannot demonize myself.");
626