32
32
#include <signal.h>
33
33
#include <unistd.h>
36
#include <linux/oom.h>
43
* This file can be used to adjust the score used to select which
44
* process should be killed in an out-of-memory (OOM) situation. The
45
* kernel uses this value for a bit-shift opera‐ tion of the process's
46
* oom_score value: valid values are in the range -16 to +15, plus the
47
* special value -17, which disables OOM-killing altogether for this
48
* process. A posi‐ tive score increases the likelihood of this process
49
* being killed by the OOM-killer; a negative score decreases the
52
* The default value for this file is 0; a new process inherits its
53
* parent's oom_adj setting. A process must be privileged
54
* (CAP_SYS_RESOURCE) to update this file.
56
* Since Linux 2.6.36, use of this file is deprecated in favor of
57
* /proc/[pid]/oom_score_adj.
61
static int disableOomKillerValue()
68
return OOM_ADJUST_MIN;
73
return OOM_ADJUST_MAX;
76
static OomAdjuster leastLikelyToBeKilled()
78
return OomAdjuster(minValue());
81
static OomAdjuster mostLikelyToBeKilled()
83
return OomAdjuster(maxValue());
86
OomAdjuster(int value) : value(value)
92
return !(value < disableOomKillerValue() || value > maxValue());
95
bool applyForPid(pid_t pid) const
97
auto fn = QString("/proc/%1/oom_adj").arg(pid);
100
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
103
QTextStream out(&file);
115
* This file can be used to adjust the badness heuristic used to
116
* select which process gets killed in out-of-memory conditions.
118
* The badness heuristic assigns a value to each candidate
119
* task ranging from 0 (never kill) to 1000 (always kill)
120
* to determine which process is targeted. The units are
121
* roughly a proportion along that range of allowed memory
122
* the process may allocate from, based on an estimation of
123
* its current memory and swap use. For example, if a task
124
* is using all allowed memory, its badness score will be
125
* 1000. If it is using half of its allowed memory, its
128
* There is an additional factor included in the badness score: root
129
* processes are given 3% extra memory over other tasks.
131
* The amount of "allowed" memory depends on the context in
132
* which the OOM-killer was called. If it is due to the
133
* memory assigned to the allocating task's cpuset being
134
* exhausted, the allowed memory represents the set of mems
135
* assigned to that cpuset (see cpuset(7)). If it is due
136
* to a mempolicy's node(s) being exhausted, the allowed
137
* memory represents the set of mempolicy nodes. If it is
138
* due to a memory limit (or swap limit) being reached, the
139
* allowed memory is that configured limit. Finally, if it
140
* is due to the entire system being out of memory, the
141
* allowed memory represents all allocatable resources.
143
* The value of oom_score_adj is added to the badness score before it
144
* is used to determine which task to kill. Acceptable values range
145
* from -1000 (OOM_SCORE_ADJ_MIN) to +1000 (OOM_SCORE_ADJ_MAX). This
146
* allows user space to control the preference for OOM-killing,
147
* ranging from always preferring a certain task or completely
148
* disabling it from OOM- killing. The lowest possible value, -1000,
149
* is equivalent to disabling OOM-killing entirely for that task,
150
* since it will always report a badness score of 0.
152
* Consequently, it is very simple for user space to define the amount
153
* of memory to consider for each task. Setting a oom_score_adj value
154
* of +500, for example, is roughly equiv‐ alent to allowing the
155
* remainder of tasks sharing the same system, cpuset, mempolicy, or
156
* memory controller resources to use at least 50% more memory. A
157
* value of -500, on the other hand, would be roughly equivalent to
158
* discounting 50% of the task's allowed memory from being considered
159
* as scoring against the task.
161
* For backward compatibility with previous kernels,
162
* /proc/[pid]/oom_adj can still be used to tune the badness score.
163
* Its value is scaled linearly with oom_score_adj.
165
* Writing to /proc/[pid]/oom_score_adj or
166
* /proc/[pid]/oom_adj will change the other with its
169
struct OomScoreAdjuster
171
static int disableOomKillerValue()
173
return OOM_SCORE_ADJ_MIN;
176
static int minValue()
178
return OOM_SCORE_ADJ_MIN;
181
static int maxValue()
183
return OOM_SCORE_ADJ_MAX;
186
static OomScoreAdjuster leastLikelyToBeKilled()
188
// We could be way more clever here if we knew the distribution
189
// of oom_score_adj values of all app processes. However, we just
190
// make sure that the process is not ignored by the oom killer for now.
191
return OomScoreAdjuster(minValue()+200);
194
static OomScoreAdjuster mostLikelyToBeKilled()
196
return OomScoreAdjuster(maxValue());
199
OomScoreAdjuster(int value) : value(value)
205
return !(value < disableOomKillerValue() || value > maxValue());
208
bool applyForPid(pid_t pid) const
210
auto fn = QString("/proc/%1/oom_score_adj").arg(pid);
213
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
216
QTextStream out(&file);
225
void ensureProcessIsUnlikelyToBeKilled(pid_t pid)
227
if (!OomScoreAdjuster::leastLikelyToBeKilled().applyForPid(pid))
228
if (!OomAdjuster::leastLikelyToBeKilled().applyForPid(pid))
229
DLOG("ensureProcessIsUnlikelyToBeKilled failed");
232
void ensureProcessIsLikelyToBeKilled(pid_t pid)
234
if (!OomScoreAdjuster::mostLikelyToBeKilled().applyForPid(pid))
235
if (!OomAdjuster::mostLikelyToBeKilled().applyForPid(pid))
236
DLOG("ensureProcessIsLikelyToBeKilled failed");
36
240
TaskController* TaskController::m_theTaskController = nullptr;