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
On newer kernels /proc/*/oom_adj is deprecated and should not be used anymore. As Open
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 which
onles provides /proc/*/oom_adj.

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

Open-webOS-DCO-1.0-Signed-off-by: Simon Busch <morphis@gravedo.de>
  • Loading branch information
morphis authored and Roger Stringer committed Dec 20, 2012
1 parent 6a0f153 commit fdf3775
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
49 changes: 39 additions & 10 deletions Src/base/MemoryMonitor.cpp
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 Down Expand Up @@ -51,6 +52,12 @@ static const std::string sMBLabel("mb");
static const std::string sProcRSS("VmRSS");
static const std::string sProcSwap("VmSwap");

#define OOM_ADJ_PATH "/proc/%d/oom_adj"
#define OOM_SCORE_ADJ_PATH "/proc/%d/oom_score_adj"

#define OOM_ADJ_VALUE -17
#define OOM_SCORE_ADJ_VALUE -1000

MemoryMonitor* MemoryMonitor::instance()
{
static MemoryMonitor* s_instance = 0;
Expand All @@ -68,19 +75,41 @@ MemoryMonitor::MemoryMonitor()
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);
(void)result;

fclose(f);
}
/* Adjust OOM killer so we're never killed for memory reasons */
adjustOomScore();
}

MemoryMonitor::~MemoryMonitor()
{
{
}

void MemoryMonitor::adjustOomScore()
{
struct stat st;
int score_value = 0;

/* Adjust OOM killer so we're never killed for memory reasons */
char oom_adj_path[kFileNameLen];
snprintf(oom_adj_path, kFileNameLen - 1, OOM_SCORE_ADJ_PATH, getpid());
if (stat(oom_adj_path, &st) == -1) {
snprintf(oom_adj_path, kFileNameLen - 1, OOM_ADJ_PATH, getpid());
if (stat(oom_adj_path, &st) == -1) {
g_warning("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, "wb");
if (f) {
fprintf(f, "%i\n", score_value);
fclose(f);
}
}

void MemoryMonitor::start()
Expand Down
4 changes: 3 additions & 1 deletion Src/base/MemoryMonitor.h
Expand Up @@ -75,7 +75,9 @@ class MemoryMonitor : public QObject
int getCurrentRssUsage() const;

int getProcessMemInfo(pid_t pid);


void adjustOomScore();

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

0 comments on commit fdf3775

Please sign in to comment.