Merge pull request #9214 from electron/app-memoryinfo

API to get memory of all processes of the app
This commit is contained in:
Kevin Sawicki 2017-05-04 14:00:30 -07:00 committed by GitHub
commit 59511354fd
6 changed files with 92 additions and 2 deletions

View file

@ -34,12 +34,12 @@
#include "chrome/browser/icon_manager.h" #include "chrome/browser/icon_manager.h"
#include "chrome/common/chrome_paths.h" #include "chrome/common/chrome_paths.h"
#include "content/public/browser/browser_accessibility_state.h" #include "content/public/browser/browser_accessibility_state.h"
#include "content/public/browser/browser_child_process_host.h"
#include "content/public/browser/client_certificate_delegate.h" #include "content/public/browser/client_certificate_delegate.h"
#include "content/public/browser/gpu_data_manager.h" #include "content/public/browser/gpu_data_manager.h"
#include "content/public/browser/render_frame_host.h" #include "content/public/browser/render_frame_host.h"
#include "content/public/common/content_switches.h" #include "content/public/common/content_switches.h"
#include "media/audio/audio_manager.h" #include "media/audio/audio_manager.h"
#include "native_mate/dictionary.h"
#include "native_mate/object_template_builder.h" #include "native_mate/object_template_builder.h"
#include "net/ssl/ssl_cert_request_info.h" #include "net/ssl/ssl_cert_request_info.h"
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
@ -337,6 +337,17 @@ namespace api {
namespace { namespace {
class AppIdProcessIterator : public base::ProcessIterator {
public:
AppIdProcessIterator() : base::ProcessIterator(nullptr) {}
protected:
bool IncludeEntry() override {
return (entry().parent_pid() == base::GetCurrentProcId() ||
entry().pid() == base::GetCurrentProcId());
}
};
IconLoader::IconSize GetIconSizeByString(const std::string& size) { IconLoader::IconSize GetIconSizeByString(const std::string& size) {
if (size == "small") { if (size == "small") {
return IconLoader::IconSize::SMALL; return IconLoader::IconSize::SMALL;
@ -912,6 +923,47 @@ void App::GetFileIcon(const base::FilePath& path,
} }
} }
std::vector<mate::Dictionary> App::GetAppMemoryInfo(v8::Isolate* isolate) {
AppIdProcessIterator process_iterator;
auto process_entry = process_iterator.NextProcessEntry();
std::vector<mate::Dictionary> result;
while (process_entry != nullptr) {
int64_t pid = process_entry->pid();
auto process = base::Process::OpenWithExtraPrivileges(pid);
#if defined(OS_MACOSX)
std::unique_ptr<base::ProcessMetrics> metrics(
base::ProcessMetrics::CreateProcessMetrics(
process.Handle(), content::BrowserChildProcessHost::GetPortProvider()));
#else
std::unique_ptr<base::ProcessMetrics> metrics(
base::ProcessMetrics::CreateProcessMetrics(process.Handle()));
#endif
mate::Dictionary pid_dict = mate::Dictionary::CreateEmpty(isolate);
mate::Dictionary memory_dict = mate::Dictionary::CreateEmpty(isolate);
memory_dict.Set("workingSetSize",
static_cast<double>(metrics->GetWorkingSetSize() >> 10));
memory_dict.Set("peakWorkingSetSize",
static_cast<double>(metrics->GetPeakWorkingSetSize() >> 10));
size_t private_bytes, shared_bytes;
if (metrics->GetMemoryBytes(&private_bytes, &shared_bytes)) {
memory_dict.Set("privateBytes", static_cast<double>(private_bytes >> 10));
memory_dict.Set("sharedBytes", static_cast<double>(shared_bytes >> 10));
}
pid_dict.Set("memory", memory_dict);
pid_dict.Set("pid", pid);
result.push_back(pid_dict);
process_entry = process_iterator.NextProcessEntry();
}
return result;
}
// static // static
mate::Handle<App> App::Create(v8::Isolate* isolate) { mate::Handle<App> App::Create(v8::Isolate* isolate) {
return mate::CreateHandle(isolate, new App(isolate)); return mate::CreateHandle(isolate, new App(isolate));
@ -983,7 +1035,8 @@ void App::BuildPrototype(
&App::IsAccessibilitySupportEnabled) &App::IsAccessibilitySupportEnabled)
.SetMethod("disableHardwareAcceleration", .SetMethod("disableHardwareAcceleration",
&App::DisableHardwareAcceleration) &App::DisableHardwareAcceleration)
.SetMethod("getFileIcon", &App::GetFileIcon); .SetMethod("getFileIcon", &App::GetFileIcon)
.SetMethod("getAppMemoryInfo", &App::GetAppMemoryInfo);
} }
} // namespace api } // namespace api

View file

@ -13,10 +13,12 @@
#include "atom/browser/browser.h" #include "atom/browser/browser.h"
#include "atom/browser/browser_observer.h" #include "atom/browser/browser_observer.h"
#include "atom/common/native_mate_converters/callback.h" #include "atom/common/native_mate_converters/callback.h"
#include "base/process/process_iterator.h"
#include "base/task/cancelable_task_tracker.h" #include "base/task/cancelable_task_tracker.h"
#include "chrome/browser/icon_manager.h" #include "chrome/browser/icon_manager.h"
#include "chrome/browser/process_singleton.h" #include "chrome/browser/process_singleton.h"
#include "content/public/browser/gpu_data_manager_observer.h" #include "content/public/browser/gpu_data_manager_observer.h"
#include "native_mate/dictionary.h"
#include "native_mate/handle.h" #include "native_mate/handle.h"
#include "net/base/completion_callback.h" #include "net/base/completion_callback.h"
@ -141,6 +143,8 @@ class App : public AtomBrowserClient::Delegate,
void GetFileIcon(const base::FilePath& path, void GetFileIcon(const base::FilePath& path,
mate::Arguments* args); mate::Arguments* args);
std::vector<mate::Dictionary> GetAppMemoryInfo(v8::Isolate* isolate);
#if defined(OS_WIN) #if defined(OS_WIN)
// Get the current Jump List settings. // Get the current Jump List settings.
v8::Local<v8::Value> GetJumpListSettings(); v8::Local<v8::Value> GetJumpListSettings();

View file

@ -760,6 +760,10 @@ Disables hardware acceleration for current app.
This method can only be called before app is ready. This method can only be called before app is ready.
### `app.getAppMemoryInfo()`
Returns [ProcessMemoryInfo[]](structures/process-memory-info.md): Array of `ProcessMemoryInfo` objects that correspond to memory usage statistics of all the processes associated with the app.
### `app.setBadgeCount(count)` _Linux_ _macOS_ ### `app.setBadgeCount(count)` _Linux_ _macOS_
* `count` Integer * `count` Integer

View file

@ -0,0 +1,12 @@
# MemoryInfo Object
* `workingSetSize` Integer - Process id of the process.
* `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
Note that all statistics are reported in Kilobytes.

View file

@ -0,0 +1,4 @@
# ProcessMemoryInfo Object
* `pid` Integer - Process id of the process.
* `memory` [MemoryInfo](memory-info.md) - Memory information of the process.

View file

@ -533,4 +533,17 @@ describe('app module', function () {
}) })
}) })
}) })
describe('getAppMemoryInfo() API', function () {
it('returns the process memory of all running electron processes', function () {
const appMemoryInfo = app.getAppMemoryInfo()
assert.ok(appMemoryInfo.length > 0, 'App memory info object is not > 0')
for (const {memory, pid} of appMemoryInfo) {
assert.ok(memory.workingSetSize > 0, 'working set size is not > 0')
assert.ok(memory.privateBytes > 0, 'private bytes is not > 0')
assert.ok(memory.sharedBytes > 0, 'shared bytes is not > 0')
assert.ok(pid > 0, 'pid is not > 0')
}
})
})
}) })