From 6ad0a226020d7a3b2cbac7df4f7cea81cf999752 Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Sun, 10 Jun 2018 14:00:36 +0200 Subject: [PATCH] Add process.getHeapStatistics() (#13183) --- atom/common/api/atom_bindings.cc | 30 +++++++++++++++++++ atom/common/api/atom_bindings.h | 1 + .../atom_sandboxed_renderer_client.cc | 1 + docs/api/process.md | 16 ++++++++++ lib/sandboxed_renderer/init.js | 1 + spec/api-process-spec.js | 15 ++++++++++ 6 files changed, 64 insertions(+) diff --git a/atom/common/api/atom_bindings.cc b/atom/common/api/atom_bindings.cc index ccd9ebd6780d..66211723a8fe 100644 --- a/atom/common/api/atom_bindings.cc +++ b/atom/common/api/atom_bindings.cc @@ -52,6 +52,7 @@ void AtomBindings::BindTo(v8::Isolate* isolate, v8::Local process) { dict.SetMethod("crash", &AtomBindings::Crash); dict.SetMethod("hang", &Hang); dict.SetMethod("log", &Log); + dict.SetMethod("getHeapStatistics", &GetHeapStatistics); dict.SetMethod("getProcessMemoryInfo", &GetProcessMemoryInfo); dict.SetMethod("getSystemMemoryInfo", &GetSystemMemoryInfo); dict.SetMethod("getCPUUsage", base::Bind(&AtomBindings::GetCPUUsage, @@ -125,6 +126,35 @@ void AtomBindings::Hang() { base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1)); } +// static +v8::Local AtomBindings::GetHeapStatistics(v8::Isolate* isolate) { + v8::HeapStatistics v8_heap_stats; + isolate->GetHeapStatistics(&v8_heap_stats); + + mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate); + dict.Set("totalHeapSize", + static_cast(v8_heap_stats.total_heap_size() >> 10)); + dict.Set( + "totalHeapSizeExecutable", + static_cast(v8_heap_stats.total_heap_size_executable() >> 10)); + dict.Set("totalPhysicalSize", + static_cast(v8_heap_stats.total_physical_size() >> 10)); + dict.Set("totalAvailableSize", + static_cast(v8_heap_stats.total_available_size() >> 10)); + dict.Set("usedHeapSize", + static_cast(v8_heap_stats.used_heap_size() >> 10)); + dict.Set("heapSizeLimit", + static_cast(v8_heap_stats.heap_size_limit() >> 10)); + dict.Set("mallocedMemory", + static_cast(v8_heap_stats.malloced_memory() >> 10)); + dict.Set("peakMallocedMemory", + static_cast(v8_heap_stats.peak_malloced_memory() >> 10)); + dict.Set("doesZapGarbage", + static_cast(v8_heap_stats.does_zap_garbage())); + + return dict.GetHandle(); +} + // static v8::Local AtomBindings::GetProcessMemoryInfo(v8::Isolate* isolate) { std::unique_ptr metrics( diff --git a/atom/common/api/atom_bindings.h b/atom/common/api/atom_bindings.h index 7a49d285fa9d..46a5f36bdcea 100644 --- a/atom/common/api/atom_bindings.h +++ b/atom/common/api/atom_bindings.h @@ -35,6 +35,7 @@ class AtomBindings { static void Log(const base::string16& message); static void Crash(); static void Hang(); + static v8::Local GetHeapStatistics(v8::Isolate* isolate); static v8::Local GetProcessMemoryInfo(v8::Isolate* isolate); static v8::Local GetSystemMemoryInfo(v8::Isolate* isolate, mate::Arguments* args); diff --git a/atom/renderer/atom_sandboxed_renderer_client.cc b/atom/renderer/atom_sandboxed_renderer_client.cc index 252121f18272..9fd8864b9b7b 100644 --- a/atom/renderer/atom_sandboxed_renderer_client.cc +++ b/atom/renderer/atom_sandboxed_renderer_client.cc @@ -90,6 +90,7 @@ void InitializeBindings(v8::Local binding, b.SetMethod("crash", AtomBindings::Crash); b.SetMethod("hang", AtomBindings::Hang); b.SetMethod("getArgv", GetArgv); + b.SetMethod("getHeapStatistics", &AtomBindings::GetHeapStatistics); b.SetMethod("getProcessMemoryInfo", &AtomBindings::GetProcessMemoryInfo); b.SetMethod("getSystemMemoryInfo", &AtomBindings::GetSystemMemoryInfo); } diff --git a/docs/api/process.md b/docs/api/process.md index f183f72c52d3..ada0554bd83f 100644 --- a/docs/api/process.md +++ b/docs/api/process.md @@ -106,6 +106,22 @@ Returns [`CPUUsage`](structures/cpu-usage.md) Returns [`IOCounters`](structures/io-counters.md) +### `process.getHeapStatistics()` + +Returns `Object`: + +* `totalHeapSize` Integer +* `totalHeapSizeExecutable` Integer +* `totalPhysicalSize` Integer +* `totalAvailableSize` Integer +* `usedHeapSize` Integer +* `heapSizeLimit` Integer +* `mallocedMemory` Integer +* `peakMallocedMemory` Integer +* `doesZapGarbage` Boolean + +Returns an object with V8 heap statistics. Note that all statistics are reported in Kilobytes. + ### `process.getProcessMemoryInfo()` Returns `Object`: diff --git a/lib/sandboxed_renderer/init.js b/lib/sandboxed_renderer/init.js index 55426db12252..dcb1357d30ee 100644 --- a/lib/sandboxed_renderer/init.js +++ b/lib/sandboxed_renderer/init.js @@ -52,6 +52,7 @@ require('../renderer/web-frame-init')() const preloadProcess = new events.EventEmitter() preloadProcess.crash = () => binding.crash() preloadProcess.hang = () => binding.hang() +preloadProcess.getHeapStatistics = () => binding.getHeapStatistics() preloadProcess.getProcessMemoryInfo = () => binding.getProcessMemoryInfo() preloadProcess.getSystemMemoryInfo = () => binding.getSystemMemoryInfo() preloadProcess.argv = binding.getArgv() diff --git a/spec/api-process-spec.js b/spec/api-process-spec.js index e76c31344505..1de5e1a19dc7 100644 --- a/spec/api-process-spec.js +++ b/spec/api-process-spec.js @@ -26,4 +26,19 @@ describe('process module', () => { assert.equal(typeof ioCounters.otherTransferCount, 'number') }) }) + + describe('process.getHeapStatistics()', () => { + it('returns heap statistics object', () => { + const heapStats = process.getHeapStatistics() + assert.equal(typeof heapStats.totalHeapSize, 'number') + assert.equal(typeof heapStats.totalHeapSizeExecutable, 'number') + assert.equal(typeof heapStats.totalPhysicalSize, 'number') + assert.equal(typeof heapStats.totalAvailableSize, 'number') + assert.equal(typeof heapStats.usedHeapSize, 'number') + assert.equal(typeof heapStats.heapSizeLimit, 'number') + assert.equal(typeof heapStats.mallocedMemory, 'number') + assert.equal(typeof heapStats.peakMallocedMemory, 'number') + assert.equal(typeof heapStats.doesZapGarbage, 'boolean') + }) + }) })