Commit 891c27ed authored by Chinglin Yu's avatar Chinglin Yu Committed by Commit Bot

Estimate freed memory in killing a process faster.

Use memory_instrumentation::OSMetrics::FillOSMemoryDump(), which
reads /proc/<pid>/statm, to get private bytes of a process, to avoid
contention with kswapd under heavy memory pressure.

BUG=chromium:872253
TEST=manual
R=cylee@chromium.org, sonnyrao@chromium.org, fdoray@chromium.org

Change-Id: I4d43933a39c3c89d8ebb81e3ccef20277cedb258
Reviewed-on: https://chromium-review.googlesource.com/1212246
Commit-Queue: Chinglin Yu <chinglinyu@chromium.org>
Reviewed-by: default avatarFrançois Doray <fdoray@chromium.org>
Reviewed-by: default avatarCheng-Yu Lee <cylee@chromium.org>
Cr-Commit-Position: refs/heads/master@{#592701}
parent bbda3b6f
......@@ -486,16 +486,7 @@ bool TabLifecycleUnitSource::TabLifecycleUnit::Load() {
int TabLifecycleUnitSource::TabLifecycleUnit::
GetEstimatedMemoryFreedOnDiscardKB() const {
#if defined(OS_CHROMEOS)
std::unique_ptr<base::ProcessMetrics> process_metrics(
base::ProcessMetrics::CreateProcessMetrics(GetProcessHandle()));
base::ProcessMetrics::TotalsSummary summary =
process_metrics->GetTotalsSummary();
return summary.private_clean_kb + summary.private_dirty_kb + summary.swap_kb;
#else
// TODO(fdoray): Implement this. https://crbug.com/775644
return 0;
#endif
return GetPrivateMemoryKB(GetProcessHandle());
}
bool TabLifecycleUnitSource::TabLifecycleUnit::CanPurge() const {
......
......@@ -31,6 +31,7 @@
#include "chrome/browser/memory/memory_kills_monitor.h"
#include "chrome/browser/resource_coordinator/tab_lifecycle_unit_external.h"
#include "chrome/browser/resource_coordinator/tab_manager_stats_collector.h"
#include "chrome/browser/resource_coordinator/utils.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_window.h"
......@@ -248,12 +249,8 @@ int TabManagerDelegate::MemoryStat::TargetMemoryToFreeKB() {
}
int TabManagerDelegate::MemoryStat::EstimatedMemoryFreedKB(
base::ProcessHandle pid) {
std::unique_ptr<base::ProcessMetrics> process_metrics(
base::ProcessMetrics::CreateProcessMetrics(pid));
base::ProcessMetrics::TotalsSummary summary =
process_metrics->GetTotalsSummary();
return summary.private_clean_kb + summary.private_dirty_kb + summary.swap_kb;
base::ProcessHandle handle) {
return GetPrivateMemoryKB(handle);
}
TabManagerDelegate::TabManagerDelegate(
......
......@@ -254,8 +254,8 @@ class TabManagerDelegate::MemoryStat {
// pre-configured low memory margin.
virtual int TargetMemoryToFreeKB();
// Returns estimated memory to be freed if the process |pid| is killed.
virtual int EstimatedMemoryFreedKB(base::ProcessHandle pid);
// Returns estimated memory to be freed if the process |handle| is killed.
virtual int EstimatedMemoryFreedKB(base::ProcessHandle handle);
private:
// Returns the low memory margin system config. Low memory condition is
......
......@@ -5,6 +5,8 @@
#include "chrome/browser/resource_coordinator/utils.h"
#include "base/md5.h"
#include "base/numerics/safe_conversions.h"
#include "services/resource_coordinator/public/cpp/memory_instrumentation/os_metrics.h"
namespace resource_coordinator {
......@@ -17,4 +19,26 @@ bool URLShouldBeStoredInLocalDatabase(const GURL& url) {
return url.SchemeIsHTTPOrHTTPS();
}
int GetPrivateMemoryKB(base::ProcessHandle handle) {
// Private memory footprint of a process is calculated using its private bytes
// and swap bytes. ProcessMetrics::GetTotalsSummary() is more precise in
// getting the private bytes but can be very slow under heavy memory pressure.
// Instead, use anonymous RSS as a faster estimation of private bytes for the
// process.
auto dump = memory_instrumentation::mojom::RawOSMemDump::New();
dump->platform_private_footprint =
memory_instrumentation::mojom::PlatformPrivateFootprint::New();
bool success = memory_instrumentation::OSMetrics::FillOSMemoryDump(
base::GetProcId(handle), dump.get());
// Failed to get private memory for the process, e.g. the process has died.
if (!success)
return 0;
uint64_t total_freed_bytes =
dump->platform_private_footprint->rss_anon_bytes +
dump->platform_private_footprint->vm_swap_bytes;
return base::saturated_cast<int>(total_freed_bytes / 1024);
}
} // namespace resource_coordinator
......@@ -7,6 +7,7 @@
#include <string>
#include "base/process/process_handle.h"
#include "url/gurl.h"
#include "url/origin.h"
......@@ -20,6 +21,9 @@ std::string SerializeOriginIntoDatabaseKey(const url::Origin& origin);
// database.
bool URLShouldBeStoredInLocalDatabase(const GURL& url);
// Get the private memory footprint (in KB) for the process.
int GetPrivateMemoryKB(base::ProcessHandle handle);
} // namespace resource_coordinator
#endif // CHROME_BROWSER_RESOURCE_COORDINATOR_UTILS_H_
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