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
=== modified file 'libubuntu-app-launch/ubuntu-app-launch.c'
--- libubuntu-app-launch/ubuntu-app-launch.c 2014-09-15 19:40:02 +0000
+++ libubuntu-app-launch/ubuntu-app-launch.c 2014-09-29 15:38:54 +0000
@@ -23,6 +23,8 @@
23#include <upstart.h>23#include <upstart.h>
24#include <gio/gio.h>24#include <gio/gio.h>
25#include <string.h>25#include <string.h>
26#include <fcntl.h>
27#include <errno.h>
26#include <zeitgeist.h>28#include <zeitgeist.h>
2729
28#include "ubuntu-app-launch-trace.h"30#include "ubuntu-app-launch-trace.h"
@@ -414,17 +416,45 @@
414static gboolean416static gboolean
415set_oom_value (GPid pid, const gchar * oomscore)417set_oom_value (GPid pid, const gchar * oomscore)
416{418{
417 if (oomscore == NULL) {419 static const gchar * procpath = NULL;
420 if (G_UNLIKELY(procpath == NULL)) {
421 /* Set by the test suite, probably not anyone else */
422 procpath = g_getenv("UBUNTU_APP_LAUNCH_OOM_PROC_PATH");
423 if (G_LIKELY(procpath == NULL)) {
424 procpath = "/proc";
425 }
426 }
427
428 gchar * path = g_strdup_printf("%s/%d/oom_score_adj", procpath, pid);
429 FILE * adj = fopen(path, "w");
430 int openerr = errno;
431 g_free(path);
432
433 if (adj == NULL) {
434 if (openerr != ENOENT) {
435 /* ENOENT happens a fair amount because of races, so it's not
436 worth printing a warning about */
437 g_warning("Unable to set OOM value for '%d' to '%s': %s", pid, oomscore, strerror(openerr));
438 return FALSE;
439 } else {
440 return TRUE;
441 }
442 }
443
444 size_t writesize = fwrite(oomscore, 1, strlen(oomscore), adj);
445 int writeerr = errno;
446 fclose(adj);
447
448 if (writesize == strlen(oomscore))
418 return TRUE;449 return TRUE;
419 }450
420451 if (writeerr != 0)
421 gchar * path = g_strdup_printf("/proc/%d/oom_score_adj", pid);452 g_warning("Unable to set OOM value for '%d' to '%s': %s", pid, oomscore, strerror(writeerr));
422453 else
423 gboolean res = g_file_set_contents(path, oomscore, -1, NULL);454 /* No error, but yet, wrong size. Not sure, what could cause this. */
424455 g_debug("Unable to set OOM value for '%d' to '%s': Wrote %d bytes", pid, oomscore, (int)writesize);
425 g_free(path);456
426457 return FALSE;
427 return res;
428}458}
429459
430/* Gets all the pids for an appid and sends a signal to all of them. This also460/* Gets all the pids for an appid and sends a signal to all of them. This also
@@ -436,12 +466,6 @@
436 guint hash_table_size = 0;466 guint hash_table_size = 0;
437 gboolean retval = TRUE;467 gboolean retval = TRUE;
438468
439 /* In the test suite we can't set this becuase we don't have permissions,
440 which sucks, but it's the reality of testing at package build time */
441 if (g_getenv("UBUNTU_APP_LAUNCH_NO_SET_OOM")) {
442 oomscore = NULL;
443 }
444
445 do {469 do {
446 hash_table_size = g_hash_table_size(pidssignaled);470 hash_table_size = g_hash_table_size(pidssignaled);
447 GList * pidlist = pids_for_appid(appid);471 GList * pidlist = pids_for_appid(appid);
@@ -467,7 +491,6 @@
467 }491 }
468492
469 if (!set_oom_value(pid, oomscore)) {493 if (!set_oom_value(pid, oomscore)) {
470 g_warning("Unable to set OOM score '%s' on pid %d", oomscore, pid);
471 retval = FALSE;494 retval = FALSE;
472 }495 }
473496
474497
=== modified file 'tests/libual-test.cc'
--- tests/libual-test.cc 2014-09-15 19:40:02 +0000
+++ tests/libual-test.cc 2014-09-29 15:38:54 +0000
@@ -1274,7 +1274,7 @@
12741274
1275TEST_F(LibUAL, PauseResume)1275TEST_F(LibUAL, PauseResume)
1276{1276{
1277 g_setenv("UBUNTU_APP_LAUNCH_NO_SET_OOM", "TRUE", 1);1277 g_setenv("UBUNTU_APP_LAUNCH_OOM_PROC_PATH", CMAKE_BINARY_DIR "/libual-proc" , 1);
12781278
1279 /* Setup some spew */1279 /* Setup some spew */
1280 GPid spewpid = 0;1280 GPid spewpid = 0;
@@ -1296,6 +1296,13 @@
1296 g_io_channel_set_flags(spewoutchan, G_IO_FLAG_NONBLOCK, NULL);1296 g_io_channel_set_flags(spewoutchan, G_IO_FLAG_NONBLOCK, NULL);
1297 g_io_add_watch(spewoutchan, G_IO_IN, datain, &datacnt);1297 g_io_add_watch(spewoutchan, G_IO_IN, datain, &datacnt);
12981298
1299 /* Setup our OOM adjust file */
1300 gchar * procdir = g_strdup_printf(CMAKE_BINARY_DIR "/libual-proc/%d", spewpid);
1301 ASSERT_EQ(0, g_mkdir_with_parents(procdir, 0700));
1302 gchar * oomadjfile = g_strdup_printf("%s/oom_score_adj", procdir);
1303 g_free(procdir);
1304 ASSERT_TRUE(g_file_set_contents(oomadjfile, "0", -1, NULL));
1305
1299 /* Setup the cgroup */1306 /* Setup the cgroup */
1300 g_setenv("UBUNTU_APP_LAUNCH_CG_MANAGER_NAME", "org.test.cgmock2", TRUE);1307 g_setenv("UBUNTU_APP_LAUNCH_CG_MANAGER_NAME", "org.test.cgmock2", TRUE);
1301 DbusTestDbusMock * cgmock2 = dbus_test_dbus_mock_new("org.test.cgmock2");1308 DbusTestDbusMock * cgmock2 = dbus_test_dbus_mock_new("org.test.cgmock2");
@@ -1346,6 +1353,7 @@
13461353
1347 pause(200);1354 pause(200);
13481355
1356 /* Check data coming out */
1349 EXPECT_EQ(0, datacnt);1357 EXPECT_EQ(0, datacnt);
13501358
1351 /* Check to make sure we sent the event to ZG */1359 /* Check to make sure we sent the event to ZG */
@@ -1356,8 +1364,14 @@
1356 EXPECT_EQ(1, numcalls);1364 EXPECT_EQ(1, numcalls);
13571365
1358 dbus_test_dbus_mock_object_clear_method_calls(zgmock, zgobj, NULL);1366 dbus_test_dbus_mock_object_clear_method_calls(zgmock, zgobj, NULL);
1367
1368 /* Check to ensure we set the OOM score */
1369 gchar * pauseoomscore = NULL;
1370 ASSERT_TRUE(g_file_get_contents(oomadjfile, &pauseoomscore, NULL, NULL));
1371 EXPECT_STREQ("900", pauseoomscore);
1372 g_free(pauseoomscore);
13591373
1360 /* No Resume the App */1374 /* Now Resume the App */
1361 EXPECT_TRUE(ubuntu_app_launch_resume_application("com.test.good_application_1.2.3"));1375 EXPECT_TRUE(ubuntu_app_launch_resume_application("com.test.good_application_1.2.3"));
13621376
1363 pause(200);1377 pause(200);
@@ -1371,6 +1385,12 @@
1371 EXPECT_NE(nullptr, calls);1385 EXPECT_NE(nullptr, calls);
1372 EXPECT_EQ(1, numcalls);1386 EXPECT_EQ(1, numcalls);
13731387
1388 /* Check to ensure we set the OOM score */
1389 gchar * resumeoomscore = NULL;
1390 ASSERT_TRUE(g_file_get_contents(oomadjfile, &resumeoomscore, NULL, NULL));
1391 EXPECT_STREQ("100", resumeoomscore);
1392 g_free(resumeoomscore);
1393
1374 /* Clean up */1394 /* Clean up */
1375 gchar * killstr = g_strdup_printf("kill -9 %d", spewpid);1395 gchar * killstr = g_strdup_printf("kill -9 %d", spewpid);
1376 ASSERT_TRUE(g_spawn_command_line_sync(killstr, NULL, NULL, NULL, NULL));1396 ASSERT_TRUE(g_spawn_command_line_sync(killstr, NULL, NULL, NULL, NULL));
@@ -1378,8 +1398,12 @@
13781398
1379 g_io_channel_unref(spewoutchan);1399 g_io_channel_unref(spewoutchan);
13801400
1401 g_spawn_command_line_sync("rm -rf " CMAKE_BINARY_DIR "/libual-proc", NULL, NULL, NULL, NULL);
1402
1381 /* Kill ZG default instance :-( */1403 /* Kill ZG default instance :-( */
1382 ZeitgeistLog * log = zeitgeist_log_get_default();1404 ZeitgeistLog * log = zeitgeist_log_get_default();
1383 g_object_unref(log);1405 g_object_unref(log);
1384 g_object_unref(log);1406 g_object_unref(log);
1407
1408 g_free(oomadjfile);
1385}1409}
13861410
=== modified file 'upstart-jobs/application-click.conf.in'
--- upstart-jobs/application-click.conf.in 2014-08-22 03:28:19 +0000
+++ upstart-jobs/application-click.conf.in 2014-09-29 15:38:54 +0000
@@ -18,6 +18,9 @@
18apparmor switch ${APP_ID}18apparmor switch ${APP_ID}
19cgroup freezer19cgroup freezer
2020
21# Initial OOM Score
22oom score 100
23
21# Remember, this is confined24# Remember, this is confined
22exec @pkglibexecdir@/exec-line-exec25exec @pkglibexecdir@/exec-line-exec
2326
2427
=== modified file 'upstart-jobs/application-legacy.conf.in'
--- upstart-jobs/application-legacy.conf.in 2014-08-22 03:28:19 +0000
+++ upstart-jobs/application-legacy.conf.in 2014-09-29 15:38:54 +0000
@@ -16,6 +16,9 @@
16apparmor switch $APP_EXEC_POLICY16apparmor switch $APP_EXEC_POLICY
17cgroup freezer17cgroup freezer
1818
19# Initial OOM Score
20oom score 110
21
19# This could be confined22# This could be confined
20exec @pkglibexecdir@/exec-line-exec23exec @pkglibexecdir@/exec-line-exec
2124

Subscribers

People subscribed via source and target branches