Merge lp:~thomas-voss/unity-mir/less-aggressive-scores into lp:unity-mir

Proposed by Thomas Voß
Status: Merged
Approved by: Gerry Boland
Approved revision: 131
Merged at revision: 134
Proposed branch: lp:~thomas-voss/unity-mir/less-aggressive-scores
Merge into: lp:unity-mir
Diff against target: 139 lines (+64/-9)
1 file modified
src/modules/Unity/Application/taskcontroller.cpp (+64/-9)
To merge this branch: bzr merge lp:~thomas-voss/unity-mir/less-aggressive-scores
Reviewer Review Type Date Requested Status
Gerry Boland (community) Approve
PS Jenkins bot (community) continuous-integration Approve
Review via email: mp+191440@code.launchpad.net

Commit message

Ensure that app processes are not assigned an oom score smaller than the one of the shell.

Description of the change

Ensure that app processes are not assigned an oom score smaller than the one of the shell.

To post a comment you must log in.
Revision history for this message
Loïc Minier (lool) wrote :

I'd like to suggest two possible improvements:
1)
I think we want the active / front app to be scored less favorably than shell; shell is currently score -10, but I think we should score all apps positively; so I think all apps we track should e.g. start with 10, and get e.g. 800 when they get in the background, then back to 10 if they get in front etc.

-10 would be same score as current shell/session, and I think we prefer the front app to die rather than the shell.

2) could we perhaps use relative percentage scores in the code? e.g. set the mostLikelyToGetKilled to score 80% of range:
((maxValue() - minValue()) * 80 / 100) + minValue()

in fact this would ideally be computed against score of driving process (unity8) vs. max

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
130. By Thomas Voß

Ensure that apps can never gain a better oom score than the parent process.

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Gerry Boland (gerboland) wrote :

OomAdjuster::minValue() not used any more, remove?

Couple of magic numbers in OomScoreAdjuster::mostLikelyToBeKilled(), they have any particular meaning, or just work?

review: Needs Information
Revision history for this message
Gerry Boland (gerboland) wrote :

Works as described, please address my queries, then I can approve

131. By Thomas Voß

Replace magic numbers with meaningful constants and some documentation.

Revision history for this message
Thomas Voß (thomas-voss) wrote :

> OomAdjuster::minValue() not used any more, remove?
>
Not used, keeping it for the sake of consistency between OomAdjuster and OomScoreAdjuster.

> Couple of magic numbers in OomScoreAdjuster::mostLikelyToBeKilled(), they have
> any particular meaning, or just work?

Replaced magic numbers with meaningful constants and some documentation. Hope that helps in understanding and future refactoring.

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Gerry Boland (gerboland) wrote :

Thanks for addressing that

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'src/modules/Unity/Application/taskcontroller.cpp'
--- src/modules/Unity/Application/taskcontroller.cpp 2013-10-15 16:17:37 +0000
+++ src/modules/Unity/Application/taskcontroller.cpp 2013-10-22 05:49:30 +0000
@@ -25,6 +25,9 @@
25// Qt25// Qt
26#include <QStringList>26#include <QStringList>
2727
28// STL
29#include <mutex>
30
28// glib31// glib
29#include <glib.h>32#include <glib.h>
3033
@@ -52,7 +55,7 @@
52 * The default value for this file is 0; a new process inherits its55 * The default value for this file is 0; a new process inherits its
53 * parent's oom_adj setting. A process must be privileged56 * parent's oom_adj setting. A process must be privileged
54 * (CAP_SYS_RESOURCE) to update this file.57 * (CAP_SYS_RESOURCE) to update this file.
55 58
56 * Since Linux 2.6.36, use of this file is deprecated in favor of59 * Since Linux 2.6.36, use of this file is deprecated in favor of
57 * /proc/[pid]/oom_score_adj.60 * /proc/[pid]/oom_score_adj.
58 */61 */
@@ -75,7 +78,12 @@
7578
76 static OomAdjuster leastLikelyToBeKilled()79 static OomAdjuster leastLikelyToBeKilled()
77 {80 {
78 return OomAdjuster(minValue());81 // By system default, we set the oom_score_adj of Unity8 to -10 (via lightdm).
82 // As we want to avoid that any app's oom_score_adj or oom_adj is <= Unity8's oom_score_adj,
83 // we choose a default value of -9 for oom_score_adj and 0 for oom_adj.
84 static const int default_value = 0;
85
86 return OomAdjuster(default_value);
79 }87 }
8088
81 static OomAdjuster mostLikelyToBeKilled()89 static OomAdjuster mostLikelyToBeKilled()
@@ -96,7 +104,7 @@
96 {104 {
97 auto fn = QString("/proc/%1/oom_adj").arg(pid);105 auto fn = QString("/proc/%1/oom_adj").arg(pid);
98 QFile file(fn);106 QFile file(fn);
99 107
100 if (!file.open(QIODevice::WriteOnly | QIODevice::Text))108 if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
101 return false;109 return false;
102110
@@ -185,15 +193,62 @@
185193
186 static OomScoreAdjuster leastLikelyToBeKilled()194 static OomScoreAdjuster leastLikelyToBeKilled()
187 {195 {
196 // By system default, we set the oom_score_adj of Unity8 to -10 (via lightdm).
197 // As we want to avoid that any app's oom_score_adj is <= Unity8's oom_score_adj,
198 // we choose a default value of -9, and a default increase of +1.
199 static const int default_value = -9;
200 static const int default_increase = 1;
201
202
188 // We could be way more clever here if we knew the distribution203 // We could be way more clever here if we knew the distribution
189 // of oom_score_adj values of all app processes. However, we just204 // 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.205 // make sure that the process is not ignored by the oom killer for now.
191 return OomScoreAdjuster(minValue()+200);206 return OomScoreAdjuster(
207 OomScoreAdjuster::thisProcess().isValid() ?
208 OomScoreAdjuster::thisProcess().value + default_increase :
209 default_value);
192 }210 }
193211
194 static OomScoreAdjuster mostLikelyToBeKilled()212 static OomScoreAdjuster mostLikelyToBeKilled()
195 {213 {
196 return OomScoreAdjuster(maxValue());214 // We avoid extremal values for oom_score_adj. For that, we
215 // set it to 80% of the total available range. If we cannot
216 // determine the oom_score_adj of the current process, i.e.,
217 // Unity8, we substract -200 by default.
218 static const float default_percentage = 0.8;
219 static const int default_decrease = -200;
220
221 return OomScoreAdjuster(
222 OomScoreAdjuster::thisProcess().isValid() ?
223 (maxValue() - OomScoreAdjuster::thisProcess().value) * default_percentage + OomScoreAdjuster::thisProcess().value :
224 maxValue() - default_decrease);
225 }
226
227 static const OomScoreAdjuster& thisProcess()
228 {
229 // Initialize as invalid.
230 static OomScoreAdjuster adjusterForThisProcess(minValue()-1);
231
232 static std::once_flag query_once;
233 std::call_once(
234 query_once,
235 []()
236 {
237 pid_t pid = getpid();
238
239 auto fn = QString("/proc/%1/oom_score_adj").arg(pid);
240 QFile file(fn);
241
242 if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
243 return;
244
245 QTextStream in(&file);
246 int value; in >> value;
247
248 adjusterForThisProcess.value = value;
249 });
250
251 return adjusterForThisProcess;
197 }252 }
198253
199 OomScoreAdjuster(int value) : value(value)254 OomScoreAdjuster(int value) : value(value)
@@ -209,7 +264,7 @@
209 {264 {
210 auto fn = QString("/proc/%1/oom_score_adj").arg(pid);265 auto fn = QString("/proc/%1/oom_score_adj").arg(pid);
211 QFile file(fn);266 QFile file(fn);
212 267
213 if (!file.open(QIODevice::WriteOnly | QIODevice::Text))268 if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
214 return false;269 return false;
215270
@@ -223,17 +278,17 @@
223};278};
224279
225void ensureProcessIsUnlikelyToBeKilled(pid_t pid)280void ensureProcessIsUnlikelyToBeKilled(pid_t pid)
226{ 281{
227 if (!OomScoreAdjuster::leastLikelyToBeKilled().applyForPid(pid))282 if (!OomScoreAdjuster::leastLikelyToBeKilled().applyForPid(pid))
228 if (!OomAdjuster::leastLikelyToBeKilled().applyForPid(pid))283 if (!OomAdjuster::leastLikelyToBeKilled().applyForPid(pid))
229 DLOG("ensureProcessIsUnlikelyToBeKilled failed");284 LOG("ensureProcessIsUnlikelyToBeKilled failed");
230}285}
231286
232void ensureProcessIsLikelyToBeKilled(pid_t pid)287void ensureProcessIsLikelyToBeKilled(pid_t pid)
233{288{
234 if (!OomScoreAdjuster::mostLikelyToBeKilled().applyForPid(pid))289 if (!OomScoreAdjuster::mostLikelyToBeKilled().applyForPid(pid))
235 if (!OomAdjuster::mostLikelyToBeKilled().applyForPid(pid))290 if (!OomAdjuster::mostLikelyToBeKilled().applyForPid(pid))
236 DLOG("ensureProcessIsLikelyToBeKilled failed");291 LOG("ensureProcessIsLikelyToBeKilled failed");
237}292}
238}293}
239294

Subscribers

People subscribed via source and target branches