Commit acd58b3a authored by Cheng-Yu Lee's avatar Cheng-Yu Lee Committed by Commit Bot

Setting oom_score_adj directly is faster and doesn't have to involve debugd when ARC++ is disabled.

Bug: 868249
Change-Id: Ie99e4c4ac7738c369a9b54bf2621a2c16dc8bc1d
Reviewed-on: https://chromium-review.googlesource.com/1168222Reviewed-by: default avatarCheng-Yu Lee <cylee@chromium.org>
Reviewed-by: default avatarGabriel Charette <gab@chromium.org>
Commit-Queue: Cheng-Yu Lee <cylee@chromium.org>
Cr-Commit-Position: refs/heads/master@{#582431}
parent 3a303576
......@@ -15,6 +15,7 @@
#include "base/logging.h"
#include "base/process/internal_linux.h"
#include "base/strings/string_number_conversions.h"
#include "base/threading/thread_restrictions.h"
#include "build/build_config.h"
#if defined(USE_TCMALLOC)
......@@ -65,15 +66,30 @@ void EnableTerminationOnOutOfMemory() {
#endif
}
// NOTE: This is not the only version of this function in the source:
// the setuid sandbox (in process_util_linux.c, in the sandbox source)
// also has its own C version.
bool AdjustOOMScore(ProcessId process, int score) {
// ScopedAllowBlocking() has private constructor and it can only be used in
// friend classes/functions. Declaring a class is easier in this situation to
// avoid adding more dependency to thread_restrictions.h because of the
// parameter used in AdjustOOMScore(). Specifically, ProcessId is a typedef
// and we'll need to include another header file in thread_restrictions.h
// without the class.
class AdjustOOMScoreHelper {
public:
static bool AdjustOOMScore(ProcessId process, int score);
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(AdjustOOMScoreHelper);
};
// static.
bool AdjustOOMScoreHelper::AdjustOOMScore(ProcessId process, int score) {
if (score < 0 || score > kMaxOomScore)
return false;
FilePath oom_path(internal::GetProcPidDir(process));
// Temporarily allowing blocking since oom paths are pseudo-filesystem paths.
base::ScopedAllowBlocking allow_blocking;
// Attempt to write the newer oom_score_adj file first.
FilePath oom_file = oom_path.AppendASCII("oom_score_adj");
if (PathExists(oom_file)) {
......@@ -102,6 +118,13 @@ bool AdjustOOMScore(ProcessId process, int score) {
return false;
}
// NOTE: This is not the only version of this function in the source:
// the setuid sandbox (in process_util_linux.c, in the sandbox source)
// also has its own C version.
bool AdjustOOMScore(ProcessId process, int score) {
return AdjustOOMScoreHelper::AdjustOOMScore(process, score);
}
bool UncheckedMalloc(size_t size, void** result) {
#if BUILDFLAG(USE_ALLOCATOR_SHIM)
*result = allocator::UncheckedAlloc(size);
......
......@@ -149,6 +149,7 @@ namespace internal {
class TaskTracker;
}
class AdjustOOMScoreHelper;
class GetAppOutputScopedAllowBaseSyncPrimitives;
class SimpleThread;
class StackSamplingProfiler;
......@@ -250,6 +251,7 @@ class BASE_EXPORT ScopedAllowBlocking {
// This can only be instantiated by friends. Use ScopedAllowBlockingForTesting
// in unit tests to avoid the friend requirement.
FRIEND_TEST_ALL_PREFIXES(ThreadRestrictionsTest, ScopedAllowBlocking);
friend class AdjustOOMScoreHelper;
friend class android_webview::ScopedAllowInitGLBindings;
friend class audio::OutputDevice;
friend class content::BrowserProcessSubThread;
......
......@@ -18,6 +18,7 @@
#include "base/files/file_util.h"
#include "base/memory/memory_pressure_monitor_chromeos.h"
#include "base/metrics/histogram_macros.h"
#include "base/process/memory.h"
#include "base/process/process_handle.h" // kNullProcessHandle.
#include "base/process/process_metrics.h"
#include "base/strings/string16.h"
......@@ -380,10 +381,10 @@ void TabManagerDelegate::OnFocusTabScoreAdjustmentTimeout() {
// Sets OOM score.
VLOG(3) << "Set OOM score " << chrome::kLowestRendererOomScore
<< " for focused tab " << pid;
std::map<int, int> dict;
dict[pid] = chrome::kLowestRendererOomScore;
DCHECK(GetDebugDaemonClient());
GetDebugDaemonClient()->SetOomScoreAdj(dict, base::Bind(&OnSetOomScoreAdj));
if (!base::AdjustOOMScore(pid, chrome::kLowestRendererOomScore))
LOG(ERROR) << "Failed to set oom_score_adj to "
<< chrome::kLowestRendererOomScore
<< " for focused tab, pid: " << pid;
}
void TabManagerDelegate::AdjustFocusedTabScore(base::ProcessHandle pid) {
......@@ -760,7 +761,13 @@ void TabManagerDelegate::DistributeOomScoreInRange(
// current cached score.
if (oom_score_map_[pid] != score) {
VLOG(3) << "Update OOM score " << score << " for " << *cur;
oom_scores_to_change[pid] = static_cast<int32_t>(score);
if (cur->app()) {
oom_scores_to_change[pid] = static_cast<int32_t>(score);
} else {
if (!base::AdjustOOMScore(pid, score))
LOG(ERROR) << "Failed to set oom_score_adj to " << score
<< " for process " << pid;
}
}
priority += priority_increment;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment