Merge lp:~ted/ubuntu-app-launch/oom-adjust-file into lp:ubuntu-app-launch/14.10

Proposed by Ted Gould
Status: Merged
Approved by: Charles Kerr
Approved revision: 179
Merged at revision: 173
Proposed branch: lp:~ted/ubuntu-app-launch/oom-adjust-file
Merge into: lp:ubuntu-app-launch/14.10
Diff against target: 193 lines (+72/-19)
4 files modified
libubuntu-app-launch/ubuntu-app-launch.c (+40/-17)
tests/libual-test.cc (+26/-2)
upstart-jobs/application-click.conf.in (+3/-0)
upstart-jobs/application-legacy.conf.in (+3/-0)
To merge this branch: bzr merge lp:~ted/ubuntu-app-launch/oom-adjust-file
Reviewer Review Type Date Requested Status
Charles Kerr (community) Approve
PS Jenkins bot (community) continuous-integration Approve
Review via email: mp+235992@code.launchpad.net

Commit message

Set OOM adjustment without using temporary files.

Description of the change

Fixes the OOM adjustment code which was using set_file_contents, which should work in theory, but internally it works by creating a temp file and renaming it. Which doesn't really work in /proc. So now going with the good ol' fwrite.

Also, figured out how to test this code. So now we're checking values.

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Charles Kerr (charlesk) wrote :

LGTM.

review: Approve
180. By Ted Gould

Optimize the compiler's choice of branching

181. By Ted Gould

Return true if it does not exist

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'libubuntu-app-launch/ubuntu-app-launch.c'
2--- libubuntu-app-launch/ubuntu-app-launch.c 2014-09-15 19:40:02 +0000
3+++ libubuntu-app-launch/ubuntu-app-launch.c 2014-09-29 15:38:54 +0000
4@@ -23,6 +23,8 @@
5 #include <upstart.h>
6 #include <gio/gio.h>
7 #include <string.h>
8+#include <fcntl.h>
9+#include <errno.h>
10 #include <zeitgeist.h>
11
12 #include "ubuntu-app-launch-trace.h"
13@@ -414,17 +416,45 @@
14 static gboolean
15 set_oom_value (GPid pid, const gchar * oomscore)
16 {
17- if (oomscore == NULL) {
18+ static const gchar * procpath = NULL;
19+ if (G_UNLIKELY(procpath == NULL)) {
20+ /* Set by the test suite, probably not anyone else */
21+ procpath = g_getenv("UBUNTU_APP_LAUNCH_OOM_PROC_PATH");
22+ if (G_LIKELY(procpath == NULL)) {
23+ procpath = "/proc";
24+ }
25+ }
26+
27+ gchar * path = g_strdup_printf("%s/%d/oom_score_adj", procpath, pid);
28+ FILE * adj = fopen(path, "w");
29+ int openerr = errno;
30+ g_free(path);
31+
32+ if (adj == NULL) {
33+ if (openerr != ENOENT) {
34+ /* ENOENT happens a fair amount because of races, so it's not
35+ worth printing a warning about */
36+ g_warning("Unable to set OOM value for '%d' to '%s': %s", pid, oomscore, strerror(openerr));
37+ return FALSE;
38+ } else {
39+ return TRUE;
40+ }
41+ }
42+
43+ size_t writesize = fwrite(oomscore, 1, strlen(oomscore), adj);
44+ int writeerr = errno;
45+ fclose(adj);
46+
47+ if (writesize == strlen(oomscore))
48 return TRUE;
49- }
50-
51- gchar * path = g_strdup_printf("/proc/%d/oom_score_adj", pid);
52-
53- gboolean res = g_file_set_contents(path, oomscore, -1, NULL);
54-
55- g_free(path);
56-
57- return res;
58+
59+ if (writeerr != 0)
60+ g_warning("Unable to set OOM value for '%d' to '%s': %s", pid, oomscore, strerror(writeerr));
61+ else
62+ /* No error, but yet, wrong size. Not sure, what could cause this. */
63+ g_debug("Unable to set OOM value for '%d' to '%s': Wrote %d bytes", pid, oomscore, (int)writesize);
64+
65+ return FALSE;
66 }
67
68 /* Gets all the pids for an appid and sends a signal to all of them. This also
69@@ -436,12 +466,6 @@
70 guint hash_table_size = 0;
71 gboolean retval = TRUE;
72
73- /* In the test suite we can't set this becuase we don't have permissions,
74- which sucks, but it's the reality of testing at package build time */
75- if (g_getenv("UBUNTU_APP_LAUNCH_NO_SET_OOM")) {
76- oomscore = NULL;
77- }
78-
79 do {
80 hash_table_size = g_hash_table_size(pidssignaled);
81 GList * pidlist = pids_for_appid(appid);
82@@ -467,7 +491,6 @@
83 }
84
85 if (!set_oom_value(pid, oomscore)) {
86- g_warning("Unable to set OOM score '%s' on pid %d", oomscore, pid);
87 retval = FALSE;
88 }
89
90
91=== modified file 'tests/libual-test.cc'
92--- tests/libual-test.cc 2014-09-15 19:40:02 +0000
93+++ tests/libual-test.cc 2014-09-29 15:38:54 +0000
94@@ -1274,7 +1274,7 @@
95
96 TEST_F(LibUAL, PauseResume)
97 {
98- g_setenv("UBUNTU_APP_LAUNCH_NO_SET_OOM", "TRUE", 1);
99+ g_setenv("UBUNTU_APP_LAUNCH_OOM_PROC_PATH", CMAKE_BINARY_DIR "/libual-proc" , 1);
100
101 /* Setup some spew */
102 GPid spewpid = 0;
103@@ -1296,6 +1296,13 @@
104 g_io_channel_set_flags(spewoutchan, G_IO_FLAG_NONBLOCK, NULL);
105 g_io_add_watch(spewoutchan, G_IO_IN, datain, &datacnt);
106
107+ /* Setup our OOM adjust file */
108+ gchar * procdir = g_strdup_printf(CMAKE_BINARY_DIR "/libual-proc/%d", spewpid);
109+ ASSERT_EQ(0, g_mkdir_with_parents(procdir, 0700));
110+ gchar * oomadjfile = g_strdup_printf("%s/oom_score_adj", procdir);
111+ g_free(procdir);
112+ ASSERT_TRUE(g_file_set_contents(oomadjfile, "0", -1, NULL));
113+
114 /* Setup the cgroup */
115 g_setenv("UBUNTU_APP_LAUNCH_CG_MANAGER_NAME", "org.test.cgmock2", TRUE);
116 DbusTestDbusMock * cgmock2 = dbus_test_dbus_mock_new("org.test.cgmock2");
117@@ -1346,6 +1353,7 @@
118
119 pause(200);
120
121+ /* Check data coming out */
122 EXPECT_EQ(0, datacnt);
123
124 /* Check to make sure we sent the event to ZG */
125@@ -1356,8 +1364,14 @@
126 EXPECT_EQ(1, numcalls);
127
128 dbus_test_dbus_mock_object_clear_method_calls(zgmock, zgobj, NULL);
129+
130+ /* Check to ensure we set the OOM score */
131+ gchar * pauseoomscore = NULL;
132+ ASSERT_TRUE(g_file_get_contents(oomadjfile, &pauseoomscore, NULL, NULL));
133+ EXPECT_STREQ("900", pauseoomscore);
134+ g_free(pauseoomscore);
135
136- /* No Resume the App */
137+ /* Now Resume the App */
138 EXPECT_TRUE(ubuntu_app_launch_resume_application("com.test.good_application_1.2.3"));
139
140 pause(200);
141@@ -1371,6 +1385,12 @@
142 EXPECT_NE(nullptr, calls);
143 EXPECT_EQ(1, numcalls);
144
145+ /* Check to ensure we set the OOM score */
146+ gchar * resumeoomscore = NULL;
147+ ASSERT_TRUE(g_file_get_contents(oomadjfile, &resumeoomscore, NULL, NULL));
148+ EXPECT_STREQ("100", resumeoomscore);
149+ g_free(resumeoomscore);
150+
151 /* Clean up */
152 gchar * killstr = g_strdup_printf("kill -9 %d", spewpid);
153 ASSERT_TRUE(g_spawn_command_line_sync(killstr, NULL, NULL, NULL, NULL));
154@@ -1378,8 +1398,12 @@
155
156 g_io_channel_unref(spewoutchan);
157
158+ g_spawn_command_line_sync("rm -rf " CMAKE_BINARY_DIR "/libual-proc", NULL, NULL, NULL, NULL);
159+
160 /* Kill ZG default instance :-( */
161 ZeitgeistLog * log = zeitgeist_log_get_default();
162 g_object_unref(log);
163 g_object_unref(log);
164+
165+ g_free(oomadjfile);
166 }
167
168=== modified file 'upstart-jobs/application-click.conf.in'
169--- upstart-jobs/application-click.conf.in 2014-08-22 03:28:19 +0000
170+++ upstart-jobs/application-click.conf.in 2014-09-29 15:38:54 +0000
171@@ -18,6 +18,9 @@
172 apparmor switch ${APP_ID}
173 cgroup freezer
174
175+# Initial OOM Score
176+oom score 100
177+
178 # Remember, this is confined
179 exec @pkglibexecdir@/exec-line-exec
180
181
182=== modified file 'upstart-jobs/application-legacy.conf.in'
183--- upstart-jobs/application-legacy.conf.in 2014-08-22 03:28:19 +0000
184+++ upstart-jobs/application-legacy.conf.in 2014-09-29 15:38:54 +0000
185@@ -16,6 +16,9 @@
186 apparmor switch $APP_EXEC_POLICY
187 cgroup freezer
188
189+# Initial OOM Score
190+oom score 110
191+
192 # This could be confined
193 exec @pkglibexecdir@/exec-line-exec
194

Subscribers

People subscribed via source and target branches