Skip to content

Commit

Permalink
MemoryMonitor: use /proc/*/oom_score_adj rather than /proc/*/oom_adj
Browse files Browse the repository at this point in the history
This change is based on a change made originally to luna-sysmgr by Simon
Busch, at

openwebos/luna-sysmgr@fdf37758e36da98268da95051cb6

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
ericblade-owo committed Mar 25, 2013
1 parent 1ce375c commit 70b6bdf
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
48 changes: 39 additions & 9 deletions Src/webbase/MemoryWatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "Common.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fstream>
#include <strings.h>
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -90,6 +88,7 @@ void MemoryWatcher::start()
g_warning("Failed to create MemchuteWatcher");
}
#endif
adjustOomScore();
}

static const char* nameForState(MemoryWatcher::MemState state)
Expand Down Expand Up @@ -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)
{
Expand Down
2 changes: 2 additions & 0 deletions Src/webbase/MemoryWatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class MemoryWatcher
bool timerTicked();
int getCurrentRssUsage() const;

void adjustOomScore();

#if defined(HAS_MEMCHUTE)
static void memchuteCallback(MemchuteThreshold threshold);
#endif
Expand Down

0 comments on commit 70b6bdf

Please sign in to comment.