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
1=== modified file 'src/modules/Unity/Application/taskcontroller.cpp'
2--- src/modules/Unity/Application/taskcontroller.cpp 2013-10-15 16:17:37 +0000
3+++ src/modules/Unity/Application/taskcontroller.cpp 2013-10-22 05:49:30 +0000
4@@ -25,6 +25,9 @@
5 // Qt
6 #include <QStringList>
7
8+// STL
9+#include <mutex>
10+
11 // glib
12 #include <glib.h>
13
14@@ -52,7 +55,7 @@
15 * The default value for this file is 0; a new process inherits its
16 * parent's oom_adj setting. A process must be privileged
17 * (CAP_SYS_RESOURCE) to update this file.
18-
19+
20 * Since Linux 2.6.36, use of this file is deprecated in favor of
21 * /proc/[pid]/oom_score_adj.
22 */
23@@ -75,7 +78,12 @@
24
25 static OomAdjuster leastLikelyToBeKilled()
26 {
27- return OomAdjuster(minValue());
28+ // By system default, we set the oom_score_adj of Unity8 to -10 (via lightdm).
29+ // As we want to avoid that any app's oom_score_adj or oom_adj is <= Unity8's oom_score_adj,
30+ // we choose a default value of -9 for oom_score_adj and 0 for oom_adj.
31+ static const int default_value = 0;
32+
33+ return OomAdjuster(default_value);
34 }
35
36 static OomAdjuster mostLikelyToBeKilled()
37@@ -96,7 +104,7 @@
38 {
39 auto fn = QString("/proc/%1/oom_adj").arg(pid);
40 QFile file(fn);
41-
42+
43 if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
44 return false;
45
46@@ -185,15 +193,62 @@
47
48 static OomScoreAdjuster leastLikelyToBeKilled()
49 {
50+ // By system default, we set the oom_score_adj of Unity8 to -10 (via lightdm).
51+ // As we want to avoid that any app's oom_score_adj is <= Unity8's oom_score_adj,
52+ // we choose a default value of -9, and a default increase of +1.
53+ static const int default_value = -9;
54+ static const int default_increase = 1;
55+
56+
57 // We could be way more clever here if we knew the distribution
58 // of oom_score_adj values of all app processes. However, we just
59 // make sure that the process is not ignored by the oom killer for now.
60- return OomScoreAdjuster(minValue()+200);
61+ return OomScoreAdjuster(
62+ OomScoreAdjuster::thisProcess().isValid() ?
63+ OomScoreAdjuster::thisProcess().value + default_increase :
64+ default_value);
65 }
66
67 static OomScoreAdjuster mostLikelyToBeKilled()
68 {
69- return OomScoreAdjuster(maxValue());
70+ // We avoid extremal values for oom_score_adj. For that, we
71+ // set it to 80% of the total available range. If we cannot
72+ // determine the oom_score_adj of the current process, i.e.,
73+ // Unity8, we substract -200 by default.
74+ static const float default_percentage = 0.8;
75+ static const int default_decrease = -200;
76+
77+ return OomScoreAdjuster(
78+ OomScoreAdjuster::thisProcess().isValid() ?
79+ (maxValue() - OomScoreAdjuster::thisProcess().value) * default_percentage + OomScoreAdjuster::thisProcess().value :
80+ maxValue() - default_decrease);
81+ }
82+
83+ static const OomScoreAdjuster& thisProcess()
84+ {
85+ // Initialize as invalid.
86+ static OomScoreAdjuster adjusterForThisProcess(minValue()-1);
87+
88+ static std::once_flag query_once;
89+ std::call_once(
90+ query_once,
91+ []()
92+ {
93+ pid_t pid = getpid();
94+
95+ auto fn = QString("/proc/%1/oom_score_adj").arg(pid);
96+ QFile file(fn);
97+
98+ if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
99+ return;
100+
101+ QTextStream in(&file);
102+ int value; in >> value;
103+
104+ adjusterForThisProcess.value = value;
105+ });
106+
107+ return adjusterForThisProcess;
108 }
109
110 OomScoreAdjuster(int value) : value(value)
111@@ -209,7 +264,7 @@
112 {
113 auto fn = QString("/proc/%1/oom_score_adj").arg(pid);
114 QFile file(fn);
115-
116+
117 if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
118 return false;
119
120@@ -223,17 +278,17 @@
121 };
122
123 void ensureProcessIsUnlikelyToBeKilled(pid_t pid)
124-{
125+{
126 if (!OomScoreAdjuster::leastLikelyToBeKilled().applyForPid(pid))
127 if (!OomAdjuster::leastLikelyToBeKilled().applyForPid(pid))
128- DLOG("ensureProcessIsUnlikelyToBeKilled failed");
129+ LOG("ensureProcessIsUnlikelyToBeKilled failed");
130 }
131
132 void ensureProcessIsLikelyToBeKilled(pid_t pid)
133 {
134 if (!OomScoreAdjuster::mostLikelyToBeKilled().applyForPid(pid))
135 if (!OomAdjuster::mostLikelyToBeKilled().applyForPid(pid))
136- DLOG("ensureProcessIsLikelyToBeKilled failed");
137+ LOG("ensureProcessIsLikelyToBeKilled failed");
138 }
139 }
140

Subscribers

People subscribed via source and target branches