diff options
Diffstat (limited to 'base')
-rw-r--r-- | base/process_util.h | 7 | ||||
-rw-r--r-- | base/process_util_linux.cc | 18 |
2 files changed, 23 insertions, 2 deletions
diff --git a/base/process_util.h b/base/process_util.h index 41d1d17..1122de7 100644 --- a/base/process_util.h +++ b/base/process_util.h @@ -100,6 +100,13 @@ FilePath GetProcessExecutablePath(ProcessHandle process); // CPU-related ticks. Returns -1 on parse error. // Exposed for testing. int ParseProcStatCPU(const std::string& input); + +static const char kAdjustOOMScoreSwitch[] = "--adjust-oom-score"; + +// This adjusts /proc/process/oom_adj so the Linux OOM killer will prefer +// certain process types over others. The range for the adjustment is +// [-17,15], with [0,15] being user accessible. +bool AdjustOOMScore(ProcessId process, int score); #endif #if defined(OS_POSIX) diff --git a/base/process_util_linux.cc b/base/process_util_linux.cc index bd6bcf3..1a7e090 100644 --- a/base/process_util_linux.cc +++ b/base/process_util_linux.cc @@ -513,7 +513,6 @@ void OnNoMemory() { } // namespace extern "C" { - #if !defined(LINUX_USE_TCMALLOC) typedef void* (*malloc_type)(size_t size); @@ -600,7 +599,6 @@ int posix_memalign(void** ptr, size_t alignment, size_t size) { } #endif // !defined(LINUX_USE_TCMALLOC) - } // extern C void EnableTerminationOnOutOfMemory() { @@ -610,4 +608,20 @@ void EnableTerminationOnOutOfMemory() { // malloc and friends and make them die on out of memory. } +bool AdjustOOMScore(ProcessId process, int score) { + if (score < 0 || score > 15) + return false; + + FilePath oom_adj("/proc"); + oom_adj = oom_adj.Append(Int64ToString(process)); + oom_adj = oom_adj.AppendASCII("oom_adj"); + + if (!file_util::PathExists(oom_adj)) + return false; + + std::string score_str = IntToString(score); + return (static_cast<int>(score_str.length()) == + file_util::WriteFile(oom_adj, score_str.c_str(), score_str.length())); +} + } // namespace base |