Commit b1728927 authored by chengx's avatar chengx Committed by Commit bot

Add function to compute proportional set size for OS_WIN

Memory-Infra on Windows doesn't use memory usage reported by the OS (proportional set size, private dirty, etc.) yet.

This CL added functionality to calculate proportional set size for a process.

BUG=623499

Review-Url: https://codereview.chromium.org/2549803003
Cr-Commit-Position: refs/heads/master@{#437827}
parent 4537fa80
...@@ -145,6 +145,9 @@ class BASE_EXPORT ProcessMetrics { ...@@ -145,6 +145,9 @@ class BASE_EXPORT ProcessMetrics {
// usage in bytes, as per definition of WorkingSetBytes. Note that this // usage in bytes, as per definition of WorkingSetBytes. Note that this
// function is somewhat expensive on Windows (a few ms per process). // function is somewhat expensive on Windows (a few ms per process).
bool GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const; bool GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const;
// Computes pss (proportional set size) of a process. Note that this
// function is somewhat expensive on Windows (a few ms per process).
bool GetProportionalSetSizeBytes(uint64_t* pss_bytes) const;
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
// Fills both CommitedKBytes and WorkingSetKBytes in a single operation. This // Fills both CommitedKBytes and WorkingSetKBytes in a single operation. This
......
...@@ -155,9 +155,58 @@ class WorkingSetInformationBuffer { ...@@ -155,9 +155,58 @@ class WorkingSetInformationBuffer {
return UncheckedMalloc(size, reinterpret_cast<void**>(&buffer_)); return UncheckedMalloc(size, reinterpret_cast<void**>(&buffer_));
} }
PSAPI_WORKING_SET_INFORMATION* get() { return buffer_; }
const PSAPI_WORKING_SET_INFORMATION* operator ->() const { return buffer_; } const PSAPI_WORKING_SET_INFORMATION* operator ->() const { return buffer_; }
size_t GetPageEntryCount() const { return number_of_entries; }
// This function is used to get page entries for a process.
bool QueryPageEntries(const ProcessHandle& process_) {
int retries = 5;
number_of_entries = 4096; // Just a guess.
for (;;) {
size_t buffer_size =
sizeof(PSAPI_WORKING_SET_INFORMATION) +
(number_of_entries * sizeof(PSAPI_WORKING_SET_BLOCK));
if (!Reserve(buffer_size))
return false;
// On success, |buffer_| is populated with info about the working set of
// |process_|. On ERROR_BAD_LENGTH failure, increase the size of the
// buffer and try again.
if (QueryWorkingSet(process_, buffer_, buffer_size))
break; // Success
if (GetLastError() != ERROR_BAD_LENGTH)
return false;
number_of_entries = buffer_->NumberOfEntries;
// Maybe some entries are being added right now. Increase the buffer to
// take that into account. Increasing by 10% should generally be enough,
// especially considering the potentially low memory condition during the
// call (when called from OomMemoryDetails) and the potentially high
// number of entries (300K was observed in crash dumps).
number_of_entries *= 1.1;
if (--retries == 0) {
// If we're looping, eventually fail.
return false;
}
}
// TODO(chengx): Remove the comment and the logic below. It is no longer
// needed since we don't have Win2000 support.
// On windows 2000 the function returns 1 even when the buffer is too small.
// The number of entries that we are going to parse is the minimum between
// the size we allocated and the real number of entries.
number_of_entries = std::min(number_of_entries,
static_cast<size_t>(buffer_->NumberOfEntries));
return true;
}
private: private:
void Clear() { void Clear() {
free(buffer_); free(buffer_);
...@@ -166,6 +215,9 @@ class WorkingSetInformationBuffer { ...@@ -166,6 +215,9 @@ class WorkingSetInformationBuffer {
PSAPI_WORKING_SET_INFORMATION* buffer_ = nullptr; PSAPI_WORKING_SET_INFORMATION* buffer_ = nullptr;
// Number of page entries.
size_t number_of_entries = 0;
DISALLOW_COPY_AND_ASSIGN(WorkingSetInformationBuffer); DISALLOW_COPY_AND_ASSIGN(WorkingSetInformationBuffer);
}; };
...@@ -179,44 +231,12 @@ bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const { ...@@ -179,44 +231,12 @@ bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const {
DCHECK(ws_usage); DCHECK(ws_usage);
memset(ws_usage, 0, sizeof(*ws_usage)); memset(ws_usage, 0, sizeof(*ws_usage));
DWORD number_of_entries = 4096; // Just a guess.
WorkingSetInformationBuffer buffer; WorkingSetInformationBuffer buffer;
int retries = 5; if (!buffer.QueryPageEntries(process_))
for (;;) { return false;
DWORD buffer_size = sizeof(PSAPI_WORKING_SET_INFORMATION) +
(number_of_entries * sizeof(PSAPI_WORKING_SET_BLOCK));
if (!buffer.Reserve(buffer_size))
return false;
// Call the function once to get number of items
if (QueryWorkingSet(process_, buffer.get(), buffer_size))
break; // Success
if (GetLastError() != ERROR_BAD_LENGTH)
return false;
number_of_entries = static_cast<DWORD>(buffer->NumberOfEntries);
// Maybe some entries are being added right now. Increase the buffer to
// take that into account. Increasing by 10% should generally be enough,
// especially considering the potentially low memory condition during the
// call (when called from OomMemoryDetails) and the potentially high
// number of entries (300K was observed in crash dumps).
number_of_entries = static_cast<DWORD>(number_of_entries * 1.1);
if (--retries == 0) {
// If we're looping, eventually fail.
return false;
}
}
// On windows 2000 the function returns 1 even when the buffer is too small. size_t num_page_entries = buffer.GetPageEntryCount();
// The number of entries that we are going to parse is the minimum between the for (size_t i = 0; i < num_page_entries; i++) {
// size we allocated and the real number of entries.
number_of_entries =
std::min(number_of_entries, static_cast<DWORD>(buffer->NumberOfEntries));
for (unsigned int i = 0; i < number_of_entries; i++) {
if (buffer->WorkingSetInfo[i].Shared) { if (buffer->WorkingSetInfo[i].Shared) {
ws_shareable++; ws_shareable++;
if (buffer->WorkingSetInfo[i].ShareCount > 1) if (buffer->WorkingSetInfo[i].ShareCount > 1)
...@@ -229,6 +249,28 @@ bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const { ...@@ -229,6 +249,28 @@ bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const {
ws_usage->priv = ws_private * PAGESIZE_KB; ws_usage->priv = ws_private * PAGESIZE_KB;
ws_usage->shareable = ws_shareable * PAGESIZE_KB; ws_usage->shareable = ws_shareable * PAGESIZE_KB;
ws_usage->shared = ws_shared * PAGESIZE_KB; ws_usage->shared = ws_shared * PAGESIZE_KB;
return true;
}
// This function calculates the proportional set size for a process.
bool ProcessMetrics::GetProportionalSetSizeBytes(uint64_t* pss_bytes) const {
double ws_pss = 0.0;
WorkingSetInformationBuffer buffer;
if (!buffer.QueryPageEntries(process_))
return false;
size_t num_page_entries = buffer.GetPageEntryCount();
for (size_t i = 0; i < num_page_entries; i++) {
if (buffer->WorkingSetInfo[i].Shared &&
buffer->WorkingSetInfo[i].ShareCount > 0)
ws_pss += 1.0 / buffer->WorkingSetInfo[i].ShareCount;
else
ws_pss += 1.0;
}
*pss_bytes = static_cast<uint64_t>(ws_pss * GetPageSize());
return true; return true;
} }
......
...@@ -298,7 +298,20 @@ bool ProcessMetricsMemoryDumpProvider::DumpProcessTotals( ...@@ -298,7 +298,20 @@ bool ProcessMetricsMemoryDumpProvider::DumpProcessTotals(
nullptr /* shared_bytes */); nullptr /* shared_bytes */);
if (res) if (res)
pmd->process_totals()->SetExtraFieldInBytes("private_bytes", private_bytes); pmd->process_totals()->SetExtraFieldInBytes("private_bytes", private_bytes);
#endif // defined(OS_LINUX) || defined(OS_ANDROID) #elif defined(OS_WIN)
if (args.level_of_detail ==
base::trace_event::MemoryDumpLevelOfDetail::DETAILED) {
uint64_t pss_bytes = 0;
bool res = process_metrics_->GetProportionalSetSizeBytes(&pss_bytes);
if (res) {
base::trace_event::ProcessMemoryMaps::VMRegion region;
region.byte_stats_proportional_resident = pss_bytes;
pmd->process_mmaps()->AddVMRegion(region);
pmd->set_has_process_mmaps();
}
}
#endif
#endif // !defined(OS_IOS) #endif // !defined(OS_IOS)
pmd->process_totals()->set_resident_set_bytes(rss_bytes); pmd->process_totals()->set_resident_set_bytes(rss_bytes);
......
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