From bef7d5a520c5eedfae38bd1237e0d3526f2db56e Mon Sep 17 00:00:00 2001 From: Hari Krishna Reddy Juturu Date: Mon, 17 Apr 2017 04:59:16 -0700 Subject: [PATCH] API to get memory of all processes of the app --- atom/browser/api/atom_api_app.cc | 35 +++++++++++++++++++++++++++++++- atom/browser/api/atom_api_app.h | 14 +++++++++++++ docs/api/app.md | 17 ++++++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) diff --git a/atom/browser/api/atom_api_app.cc b/atom/browser/api/atom_api_app.cc index 115041d0082d..a990b5c1bd30 100644 --- a/atom/browser/api/atom_api_app.cc +++ b/atom/browser/api/atom_api_app.cc @@ -912,6 +912,38 @@ void App::GetFileIcon(const base::FilePath& path, } } +v8::Local App::GetAppMemoryInfo(v8::Isolate* isolate) { + AppIdProcessIterator processIterator; + auto processEntry = processIterator.NextProcessEntry(); + mate::Dictionary result = mate::Dictionary::CreateEmpty(isolate); + + while(processEntry != nullptr) { + int64_t pid = processEntry->pid(); + auto process = base::Process::OpenWithExtraPrivileges(pid); + + std::unique_ptr metrics( + base::ProcessMetrics::CreateProcessMetrics(process.Handle())); + + mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate); + + dict.Set("workingSetSize", + static_cast(metrics->GetWorkingSetSize() >> 10)); + dict.Set("peakWorkingSetSize", + static_cast(metrics->GetPeakWorkingSetSize() >> 10)); + + size_t private_bytes, shared_bytes; + if (metrics->GetMemoryBytes(&private_bytes, &shared_bytes)) { + dict.Set("privateBytes", static_cast(private_bytes >> 10)); + dict.Set("sharedBytes", static_cast(shared_bytes >> 10)); + } + + result.Set(std::to_string(pid).c_str(), dict); + processEntry = processIterator.NextProcessEntry(); + } + + return result.GetHandle(); +} + // static mate::Handle App::Create(v8::Isolate* isolate) { return mate::CreateHandle(isolate, new App(isolate)); @@ -983,7 +1015,8 @@ void App::BuildPrototype( &App::IsAccessibilitySupportEnabled) .SetMethod("disableHardwareAcceleration", &App::DisableHardwareAcceleration) - .SetMethod("getFileIcon", &App::GetFileIcon); + .SetMethod("getFileIcon", &App::GetFileIcon) + .SetMethod("getAppMemoryInfo", &App::GetAppMemoryInfo); } } // namespace api diff --git a/atom/browser/api/atom_api_app.h b/atom/browser/api/atom_api_app.h index a87b88bc4642..9c35d91af720 100644 --- a/atom/browser/api/atom_api_app.h +++ b/atom/browser/api/atom_api_app.h @@ -13,6 +13,7 @@ #include "atom/browser/browser.h" #include "atom/browser/browser_observer.h" #include "atom/common/native_mate_converters/callback.h" +#include "base/process/process_iterator.h" #include "base/task/cancelable_task_tracker.h" #include "chrome/browser/icon_manager.h" #include "chrome/browser/process_singleton.h" @@ -141,6 +142,8 @@ class App : public AtomBrowserClient::Delegate, void GetFileIcon(const base::FilePath& path, mate::Arguments* args); + v8::Local GetAppMemoryInfo(v8::Isolate* isolate); + #if defined(OS_WIN) // Get the current Jump List settings. v8::Local GetJumpListSettings(); @@ -163,6 +166,17 @@ class App : public AtomBrowserClient::Delegate, DISALLOW_COPY_AND_ASSIGN(App); }; +class AppIdProcessIterator : public base::ProcessIterator { + public: + AppIdProcessIterator() : base::ProcessIterator(NULL) {} + + protected: + bool IncludeEntry() override { + return (entry().parent_pid() == base::GetCurrentProcId() || + entry().pid() == base::GetCurrentProcId()); + } +}; + } // namespace api } // namespace atom diff --git a/docs/api/app.md b/docs/api/app.md index e3160e9053f8..facc18ec389e 100644 --- a/docs/api/app.md +++ b/docs/api/app.md @@ -760,6 +760,23 @@ Disables hardware acceleration for current app. This method can only be called before app is ready. +### `app.getAppMemoryInfo()` + +Returns `Object[]`: + +* `pid` Integer - The process id for which memory info is collected for + * `workingSetSize` Integer - The amount of memory currently pinned to actual physical + RAM. + * `peakWorkingSetSize` Integer - The maximum amount of memory that has ever been pinned + to actual physical RAM. + * `privateBytes` Integer - The amount of memory not shared by other processes, such as + JS heap or HTML content. + * `sharedBytes` Integer - The amount of memory shared between processes, typically + memory consumed by the Electron code itself + +Returns an object giving memory usage statistics about all the processes associated with +the app. Note that all statistics are reported in Kilobytes. + ### `app.setBadgeCount(count)` _Linux_ _macOS_ * `count` Integer