Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

MemoryMonitor: use /proc/*/oom_score_adj rather than /proc/*/oom_adj

This change is based on a change made originally to luna-sysmgr by Simon
Busch, at

openwebos/luna-sysmgr@fdf3775

On newer kernels /proc/*/oom_adj is deprecated and should not be used anymor
webOS requires at least a linux 3.3 kernel this changes the use to the new
/proc/*/oom_score_adj entry but still keeps backward compatiblity for older
kernels that provide /proc/*/oom_adj.

More information about the change are available on the following pages:
- torvalds/linux@a63d83f
- https://www.redhat.com/archives/lvm-devel/2011-July/msg00097.html

Open webOS DCO 1.0 Signed off by Simon Busch [morphis@gravedo.de]
Open-webOS-DCO-1.0-Signed-off-by: Eric Blade <eric.blade@palm.com>

Change-Id: I8fd3d4acd2ce71338391bad0e231fce1869160e6
  • Loading branch information...
commit 70b6bdfb07ce23fd143426b035e9bc82aefe2e1a 1 parent 1ce375c
@ericblade-owo ericblade-owo authored
Showing with 41 additions and 9 deletions.
  1. +39 −9 Src/webbase/MemoryWatcher.cpp
  2. +2 −0  Src/webbase/MemoryWatcher.h
View
48 Src/webbase/MemoryWatcher.cpp
@@ -22,6 +22,7 @@
#include "Common.h"
#include <sys/types.h>
+#include <sys/stat.h>
#include <unistd.h>
#include <fstream>
#include <strings.h>
@@ -33,6 +34,12 @@
#include "Time.h"
#include "WebAppManager.h"
+#define OOM_ADJ_PATH "/proc/self/oom_adj"
+#define OOM_SCORE_ADJ_PATH "/proc/self/oom_score_adj"
+
+#define OOM_ADJ_VALUE -17
+#define OOM_SCORE_ADJ_VALUE -1000
+
static const int kTimerMs = 5000;
static const int kLowMemExpensiveTimeoutMultiplier = 2;
static const uint32_t kMinIntervalBetweenLowMemActions = 30000;
@@ -57,15 +64,6 @@ MemoryWatcher::MemoryWatcher()
m_fileName[kFileNameLen - 1] = 0;
snprintf(m_fileName, kFileNameLen - 1, "/proc/%d/statm", getpid());
- char oom_adj[kFileNameLen];
- snprintf(oom_adj, kFileNameLen - 1, "/proc/%d/oom_adj", getpid());
- FILE* f = fopen(oom_adj, "wb");
- if (f) {
- size_t result = fwrite("-17\n", 4, 1, f);
- Q_UNUSED(result);
- fclose(f);
- }
-
m_timeAtLastLowMemAction = 0;
m_timeAtLastExpensiveLowMemAction = 0;
}
@@ -90,6 +88,7 @@ void MemoryWatcher::start()
g_warning("Failed to create MemchuteWatcher");
}
#endif
+ adjustOomScore();
}
static const char* nameForState(MemoryWatcher::MemState state)
@@ -195,6 +194,37 @@ void MemoryWatcher::doLowMemActions(bool allowExpensive)
g_warning("MemoryWatcher: RSS usage after low memory actions: %dMB\n", m_currRssUsage);
}
+void MemoryWatcher::adjustOomScore()
+{
+ struct stat st;
+ int score_value = 0;
+
+ /* Adjust OOM killer so we're considered the least likely candidate
+ * for killing in OOM conditions
+ */
+ char oom_adj_path[kFileNameLen];
+ snprintf(oom_adj_path, kFileNameLen - 1, OOM_SCORE_ADJ_PATH);
+ if (stat(oom_adj_path, &st) == -1) {
+ snprintf(oom_adj_path, kFileNameLen - 1, OOM_ADJ_PATH);
+ if (stat(oom_adj_path, &st) == -1) {
+ g_warning("MemoryWatcher: Failed to adjust OOM value");
+ return;
+ } else {
+ score_value = OOM_ADJ_VALUE;
+ }
+ } else {
+ score_value = OOM_SCORE_ADJ_VALUE;
+ }
+
+ FILE* f = fopen(oom_adj_path, "w");
+ if(f) {
+ fprintf(f, "%i", score_value);
+ fclose(f);
+ } else {
+ g_warning("MemoryWatcher: Failed to adjust OOM value, could not open file %s", oom_adj_path);
+ }
+}
+
#if defined(HAS_MEMCHUTE)
void MemoryWatcher::memchuteCallback(MemchuteThreshold threshold)
{
View
2  Src/webbase/MemoryWatcher.h
@@ -67,6 +67,8 @@ class MemoryWatcher
bool timerTicked();
int getCurrentRssUsage() const;
+ void adjustOomScore();
+
#if defined(HAS_MEMCHUTE)
static void memchuteCallback(MemchuteThreshold threshold);
#endif
Please sign in to comment.
Something went wrong with that request. Please try again.