Bug 785171 - Adopt semantics of /proc/<pid>/oom_score_adjust in preferences. r=dhylands
authorThomas Zimmermann <tdz@users.sourceforge.net>
Fri, 14 Sep 2012 11:52:07 -0700
changeset 107213 f746375dc95c7e9bc55ff6eedbb7b981342e55a9
parent 107212 8e5fe9ad79505f0d171ee838c90d1958e0b00b97
child 107214 54f940cf6d6514b03416890d4f5216dcfde12509
push id74
push usershu@rfrn.org
push dateTue, 18 Sep 2012 19:23:47 +0000
reviewersdhylands
bugs785171
milestone18.0a1
Bug 785171 - Adopt semantics of /proc/<pid>/oom_score_adjust in preferences. r=dhylands The preference settings hal.processPriorityManager.gonk.backgroundOomAdjust, hal.processPriorityManager.gonk.foregroundOomAdjust, and hal.processPriorityManager.gonk.masterOomAdjust have been replaced by hal.processPriorityManager.gonk.backgroundOomScoreAdjust, hal.processPriorityManager.gonk.foregroundOomScoreAdjust, and hal.processPriorityManager.gonk.masterOomScoreAdjust. The new settings adopt the semantics of /proc/<pid>/oom_score_adj and the default values have been changed accordingly. The interface to the OOM killer interface is still /proc/<pid>/oom_adj, and its input values are computed from the settings.
b2g/app/b2g.js
hal/gonk/GonkHal.cpp
--- a/b2g/app/b2g.js
+++ b/b2g/app/b2g.js
@@ -508,19 +508,19 @@ pref("javascript.options.mem.high_water_
 // Show/Hide scrollbars when active/inactive
 pref("ui.showHideScrollbars", 1);
 
 // Enable the ProcessPriorityManager, and give processes with no visible
 // documents a 1s grace period before they're eligible to be marked as
 // background.
 pref("dom.ipc.processPriorityManager.enabled", true);
 pref("dom.ipc.processPriorityManager.gracePeriodMS", 1000);
-pref("hal.processPriorityManager.gonk.masterOomAdjust", 0);
-pref("hal.processPriorityManager.gonk.foregroundOomAdjust", 1);
-pref("hal.processPriorityManager.gonk.backgroundOomAdjust", 6);
+pref("hal.processPriorityManager.gonk.masterOomScoreAdjust", 0);
+pref("hal.processPriorityManager.gonk.foregroundOomScoreAdjust", 67);
+pref("hal.processPriorityManager.gonk.backgroundOomScoreAdjust", 400);
 pref("hal.processPriorityManager.gonk.masterNice", -1);
 pref("hal.processPriorityManager.gonk.foregroundNice", 0);
 pref("hal.processPriorityManager.gonk.backgroundNice", 10);
 
 #ifndef DEBUG
 // Enable pre-launching content processes for improved startup time
 // (hiding latency).
 pref("dom.ipc.processPrelauch.enabled", true);
--- a/hal/gonk/GonkHal.cpp
+++ b/hal/gonk/GonkHal.cpp
@@ -54,16 +54,38 @@
 #include "nsXULAppAPI.h"
 #include "OrientationObserver.h"
 #include "UeventPoller.h"
 
 #define LOG(args...)  __android_log_print(ANDROID_LOG_INFO, "Gonk", args)
 #define NsecPerMsec  1000000
 #define NsecPerSec   1000000000
 
+// The header linux/oom.h is not available in bionic libc. We
+// redefine some of its constants here.
+
+#ifndef OOM_DISABLE
+#define OOM_DISABLE  (-17)
+#endif
+
+#ifndef OOM_ADJUST_MIN
+#define OOM_ADJUST_MIN  (-16)
+#endif
+
+#ifndef OOM_ADJUST_MAX
+#define OOM_ADJUST_MAX  15
+#endif
+
+#ifndef OOM_SCORE_ADJ_MIN
+#define OOM_SCORE_ADJ_MIN  (-1000)
+#endif
+
+#ifndef OOM_SCORE_ADJ_MAX
+#define OOM_SCORE_ADJ_MAX  1000
+#endif
 
 using namespace mozilla;
 using namespace mozilla::hal;
 
 namespace mozilla {
 namespace hal_impl {
 
 namespace {
@@ -823,16 +845,33 @@ SetAlarm(int32_t aSeconds, int32_t aNano
   if (result < 0) {
     HAL_LOG(("Unable to set alarm: %s.", strerror(errno)));
     return false;
   }
 
   return true;
 }
 
+static int
+oomAdjOfOomScoreAdj(int aOomScoreAdj)
+{
+  // Convert OOM adjustment from the domain of /proc/<pid>/oom_score_adj
+  // to thew domain of /proc/<pid>/oom_adj.
+
+  int adj;
+
+  if (aOomScoreAdj < 0) {
+    adj = (OOM_DISABLE * aOomScoreAdj) / OOM_SCORE_ADJ_MIN;
+  } else {
+    adj = (OOM_ADJUST_MAX * aOomScoreAdj) / OOM_SCORE_ADJ_MAX;
+  }
+
+  return adj;
+}
+
 void
 SetProcessPriority(int aPid, ProcessPriority aPriority)
 {
   HAL_LOG(("SetProcessPriority(pid=%d, priority=%d)", aPid, aPriority));
 
   const char* priorityStr = NULL;
   switch (aPriority) {
   case PROCESS_PRIORITY_BACKGROUND:
@@ -846,20 +885,25 @@ SetProcessPriority(int aPid, ProcessPrio
     break;
   default:
     MOZ_NOT_REACHED();
   }
 
   // Notice that you can disable oom_adj and renice by deleting the prefs
   // hal.processPriorityManager{foreground,background,master}{OomAdjust,Nice}.
 
-  int32_t oomAdj = 0;
+  int32_t oomScoreAdj = 0;
   nsresult rv = Preferences::GetInt(nsPrintfCString(
-    "hal.processPriorityManager.gonk.%sOomAdjust", priorityStr).get(), &oomAdj);
+    "hal.processPriorityManager.gonk.%sOomScoreAdjust",
+    priorityStr).get(), &oomScoreAdj);
+
   if (NS_SUCCEEDED(rv)) {
+
+    int oomAdj = oomAdjOfOomScoreAdj(oomScoreAdj);
+
     HAL_LOG(("Setting oom_adj for pid %d to %d", aPid, oomAdj));
     WriteToFile(nsPrintfCString("/proc/%d/oom_adj", aPid).get(),
                 nsPrintfCString("%d", oomAdj).get());
   }
 
   int32_t nice = 0;
   rv = Preferences::GetInt(nsPrintfCString(
     "hal.processPriorityManager.gonk.%sNice", priorityStr).get(), &nice);