feat: add memory to app.getAppMetrics() (#18831)

This commit is contained in:
Milan Burda 2019-07-23 22:41:59 +02:00 committed by Shelley Vohr
parent 2c383b51c1
commit 103b38650f
8 changed files with 149 additions and 1 deletions

View file

@ -7,13 +7,46 @@
#include <memory>
#include <utility>
#include "base/optional.h"
#if defined(OS_WIN)
#include <windows.h>
#include <psapi.h>
#include "base/win/win_util.h"
#endif
#if defined(OS_MACOSX)
#include <mach/mach.h>
#include "base/process/port_provider_mac.h"
#include "content/public/browser/browser_child_process_host.h"
extern "C" int sandbox_check(pid_t pid, const char* operation, int type, ...);
#endif
namespace {
mach_port_t TaskForPid(pid_t pid) {
mach_port_t task = MACH_PORT_NULL;
if (auto* port_provider = content::BrowserChildProcessHost::GetPortProvider())
task = port_provider->TaskForPid(pid);
if (task == MACH_PORT_NULL && pid == getpid())
task = mach_task_self();
return task;
}
base::Optional<mach_task_basic_info_data_t> GetTaskInfo(mach_port_t task) {
if (task == MACH_PORT_NULL)
return base::nullopt;
mach_task_basic_info_data_t info = {};
mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT;
kern_return_t kr = task_info(task, MACH_TASK_BASIC_INFO,
reinterpret_cast<task_info_t>(&info), &count);
return (kr == KERN_SUCCESS) ? base::make_optional(info) : base::nullopt;
}
} // namespace
#endif // defined(OS_MACOSX)
namespace electron {
@ -37,6 +70,21 @@ ProcessMetric::~ProcessMetric() = default;
#if defined(OS_WIN)
ProcessMemoryInfo ProcessMetric::GetMemoryInfo() const {
ProcessMemoryInfo result;
PROCESS_MEMORY_COUNTERS_EX info = {};
if (::GetProcessMemoryInfo(process.Handle(),
reinterpret_cast<PROCESS_MEMORY_COUNTERS*>(&info),
sizeof(info))) {
result.working_set_size = info.WorkingSetSize;
result.peak_working_set_size = info.PeakWorkingSetSize;
result.private_bytes = info.PrivateUsage;
}
return result;
}
ProcessIntegrityLevel ProcessMetric::GetIntegrityLevel() const {
HANDLE token = nullptr;
if (!::OpenProcessToken(process.Handle(), TOKEN_QUERY, &token)) {
@ -96,6 +144,17 @@ bool ProcessMetric::IsSandboxed(ProcessIntegrityLevel integrity_level) {
#elif defined(OS_MACOSX)
ProcessMemoryInfo ProcessMetric::GetMemoryInfo() const {
ProcessMemoryInfo result;
if (auto info = GetTaskInfo(TaskForPid(process.Pid()))) {
result.working_set_size = info->resident_size;
result.peak_working_set_size = info->resident_size_max;
}
return result;
}
bool ProcessMetric::IsSandboxed() const {
#if defined(MAS_BUILD)
return true;