forked from rfjakob/earlyoom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kill.c
251 lines (215 loc) · 5.86 KB
/
kill.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
/* Kill the most memory-hungy process */
#include <dirent.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <ctype.h>
#include <limits.h> // for PATH_MAX
#include <unistd.h>
#include "kill.h"
extern int enable_debug;
struct procinfo {
int oom_score;
int oom_score_adj;
unsigned long vm_rss;
int exited;
};
static int isnumeric(char* str)
{
int i=0;
// Empty string is not numeric
if(str[0]==0)
return 0;
while(1)
{
if(str[i]==0) // End of string
return 1;
if(isdigit(str[i])==0)
return 0;
i++;
}
}
static void maybe_notify(char* notif_command, char* notif_args)
{
if(!notif_command)
return;
char notif[600];
snprintf(notif, 600, "%s %s", notif_command, notif_args);
system(notif);
}
const char * const fopen_msg = "fopen %s failed: %s\n";
/* Read /proc/pid/{oom_score, oom_score_adj, statm}
* Caller must ensure that we are already in the /proc/ directory
*/
static struct procinfo get_process_stats(int pid)
{
char buf[256];
FILE * f;
struct procinfo p = {0, 0, 0, 0};
// Read /proc/[pid]/oom_score
snprintf(buf, sizeof(buf), "%d/oom_score", pid);
f = fopen(buf, "r");
if(f == NULL) {
printf(fopen_msg, buf, strerror(errno));
p.exited = 1;
return p;
}
fscanf(f, "%d", &(p.oom_score));
fclose(f);
// Read /proc/[pid]/oom_score_adj
snprintf(buf, sizeof(buf), "%d/oom_score_adj", pid);
f = fopen(buf, "r");
if(f == NULL) {
printf(fopen_msg, buf, strerror(errno));
p.exited = 1;
return p;
}
fscanf(f, "%d", &(p.oom_score_adj));
fclose(f);
// Read VmRss from /proc/[pid]/statm
snprintf(buf, sizeof(buf), "%d/statm", pid);
f = fopen(buf, "r");
if(f == NULL)
{
printf(fopen_msg, buf, strerror(errno));
p.exited = 1;
return p;
}
fscanf(f, "%*u %lu", &(p.vm_rss));
fclose(f);
return p;
}
/*
* Find the process with the largest oom_score and kill it.
* See trigger_kernel_oom() for the reason why this is done in userspace.
*/
static void userspace_kill(DIR *procdir, int sig, int ignore_oom_score_adj, char *notif_command)
{
struct dirent * d;
char buf[256];
int pid;
int victim_pid = 0;
int victim_badness = 0;
unsigned long victim_vm_rss = 0;
char name[PATH_MAX];
struct procinfo p;
int badness;
rewinddir(procdir);
while(1)
{
errno = 0;
d = readdir(procdir);
if(d == NULL)
{
if(errno != 0)
perror("readdir returned error");
break;
}
// proc contains lots of directories not related to processes,
// skip them
if(!isnumeric(d->d_name))
continue;
pid = strtoul(d->d_name, NULL, 10);
if(pid == 1)
// Let's not kill init.
continue;
p = get_process_stats(pid);
if(p.exited == 1)
// Process may have died in the meantime
continue;
badness = p.oom_score;
if(ignore_oom_score_adj && p.oom_score_adj > 0)
badness -= p.oom_score_adj;
if(enable_debug)
printf("pid %5d: badness %3d vm_rss %6lu\n", pid, badness, p.vm_rss);
if(badness > victim_badness)
{
victim_pid = pid;
victim_badness = badness;
victim_vm_rss = p.vm_rss;
if(enable_debug)
printf(" ^ new victim (higher badness)\n");
} else if(badness == victim_badness && p.vm_rss > victim_vm_rss) {
victim_pid = pid;
victim_vm_rss = p.vm_rss;
if(enable_debug)
printf(" ^ new victim (higher vm_rss)\n");
}
}
if(victim_pid == 0)
{
fprintf(stderr, "Error: Could not find a process to kill. Sleeping 10 seconds.\n");
maybe_notify(notif_command, "-i dialog-error 'earlyoom' 'Error: Could not find a process to kill'");
sleep(10);
return;
}
name[0]=0;
snprintf(buf, sizeof(buf), "%d/stat", victim_pid);
FILE * stat = fopen(buf, "r");
fscanf(stat, "%*d %s", name);
fclose(stat);
if(sig != 0)
{
fprintf(stderr, "Killing process %d %s\n", victim_pid, name);
char notif_args[200];
snprintf(notif_args, 200, "-i dialog-warning 'earlyoom' 'Killing process %d %s'", victim_pid, name);
maybe_notify(notif_command, notif_args);
}
if(kill(victim_pid, sig) != 0)
{
perror("Could not kill process");
// Killing the process may have failed because we are not running as root.
// In that case, trying again in 100ms will just yield the same error.
// Throttle ourselves to not spam the log.
fprintf(stderr, "Sleeping 10 seconds\n");
maybe_notify(notif_command, "-i dialog-error 'earlyoom' 'Error: Failed to kill process'");
sleep(10);
}
}
/*
* Invoke the kernel oom killer by writing "f" into /proc/sysrq-trigger
*
* This approach has a few problems:
* 1) It is disallowed by default (even for root) on Fedora 20.
* You have to first write "1" into /proc/sys/kernel/sysrq to enable the "f"
* trigger.
* 2) The Chrome web browser assigns a penalty of 300 onto its own tab renderer
* processes. On an 8GB RAM machine, this means 2400MB, and will lead to every
* tab being killed before the actual memory hog
* See https://code.google.com/p/chromium/issues/detail?id=333617 for more info
* 3) It is broken in 4.0.5 - see
* https://github.com/rfjakob/earlyoom/commit/f7e2cedce8e9605c688d0c6d7dc26b7e81817f02
* Because of these issues, kill_by_rss() is used instead by default.
*/
void trigger_kernel_oom(int sig, char *notif_command)
{
FILE * trig_fd;
trig_fd = fopen("sysrq-trigger", "w");
if(trig_fd == NULL)
{
perror("Could not open /proc/sysrq-trigger");
exit(7);
}
if(sig == 9)
{
fprintf(stderr, "Invoking oom killer: ");
maybe_notify(notif_command, "-i dialog-warning 'earlyoom' 'Invoking OOM killer'");
if(fprintf(trig_fd, "f\n") != 2)
{
perror("failed");
maybe_notify(notif_command, "-i dialog-error 'earlyoom' 'Error: Failed to invoke OOM killer'");
}
else
fprintf(stderr, "done\n");
}
fclose(trig_fd);
}
void handle_oom(DIR * procdir, int sig, int kernel_oom_killer, int ignore_oom_score_adj, char *notif_command)
{
if(kernel_oom_killer)
trigger_kernel_oom(sig, notif_command);
else
userspace_kill(procdir, sig, ignore_oom_score_adj, notif_command);
}