From a1226d75ffc1467d7b8df1a37d8aa2e9e0447678 Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Thu, 30 May 2019 11:50:35 +0200 Subject: [PATCH] feat: add process.getBlinkMemoryInfo() (#17762) --- atom/common/api/electron_bindings.cc | 17 +++++++++++++++++ atom/common/api/electron_bindings.h | 1 + docs/api/process.md | 13 +++++++++++++ spec/api-browser-window-spec.js | 1 + spec/api-process-spec.js | 9 +++++++++ spec/fixtures/module/preload-sandbox.js | 1 + 6 files changed, 42 insertions(+) diff --git a/atom/common/api/electron_bindings.cc b/atom/common/api/electron_bindings.cc index 3002da6bf563..a76f16846cbd 100644 --- a/atom/common/api/electron_bindings.cc +++ b/atom/common/api/electron_bindings.cc @@ -29,6 +29,7 @@ #include "native_mate/dictionary.h" #include "services/resource_coordinator/public/cpp/memory_instrumentation/global_memory_dump.h" #include "services/resource_coordinator/public/cpp/memory_instrumentation/memory_instrumentation.h" +#include "third_party/blink/renderer/platform/heap/process_heap.h" // nogncheck namespace atom { @@ -68,6 +69,7 @@ void ElectronBindings::BindProcess(v8::Isolate* isolate, process->SetMethod("log", &Log); process->SetMethod("getCreationTime", &GetCreationTime); process->SetMethod("getHeapStatistics", &GetHeapStatistics); + process->SetMethod("getBlinkMemoryInfo", &GetBlinkMemoryInfo); process->SetMethod("getProcessMemoryInfo", &GetProcessMemoryInfo); process->SetMethod("getSystemMemoryInfo", &GetSystemMemoryInfo); process->SetMethod("getSystemVersion", @@ -252,6 +254,21 @@ v8::Local ElectronBindings::GetProcessMemoryInfo( return handle; } +// static +v8::Local ElectronBindings::GetBlinkMemoryInfo( + v8::Isolate* isolate) { + auto allocated = blink::ProcessHeap::TotalAllocatedObjectSize(); + auto marked = blink::ProcessHeap::TotalMarkedObjectSize(); + auto total = blink::ProcessHeap::TotalAllocatedSpace(); + + mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate); + dict.SetHidden("simple", true); + dict.Set("allocated", static_cast(allocated >> 10)); + dict.Set("marked", static_cast(marked >> 10)); + dict.Set("total", static_cast(total >> 10)); + return dict.GetHandle(); +} + // static void ElectronBindings::DidReceiveMemoryDump( v8::Global context, diff --git a/atom/common/api/electron_bindings.h b/atom/common/api/electron_bindings.h index 0c786dd79df3..10b401891662 100644 --- a/atom/common/api/electron_bindings.h +++ b/atom/common/api/electron_bindings.h @@ -58,6 +58,7 @@ class ElectronBindings { static v8::Local GetSystemMemoryInfo(v8::Isolate* isolate, mate::Arguments* args); static v8::Local GetProcessMemoryInfo(v8::Isolate* isolate); + static v8::Local GetBlinkMemoryInfo(v8::Isolate* isolate); static v8::Local GetCPUUsage(base::ProcessMetrics* metrics, v8::Isolate* isolate); static v8::Local GetIOCounters(v8::Isolate* isolate); diff --git a/docs/api/process.md b/docs/api/process.md index 9c745a5792ed..640393e74c2e 100644 --- a/docs/api/process.md +++ b/docs/api/process.md @@ -15,6 +15,7 @@ In sandboxed renderers the `process` object contains only a subset of the APIs: - `hang()` - `getCreationTime()` - `getHeapStatistics()` +- `getBlinkMemoryInfo()` - `getProcessMemoryInfo()` - `getSystemMemoryInfo()` - `getSystemVersion()` @@ -170,6 +171,18 @@ Returns `Object`: Returns an object with V8 heap statistics. Note that all statistics are reported in Kilobytes. +### `process.getBlinkMemoryInfo()` + +Returns `Object`: + +* `allocated` Integer - Size of all allocated objects in Kilobytes. +* `marked` Integer - Size of all marked objects in Kilobytes. +* `total` Integer - Total allocated space in Kilobytes. + +Returns an object with Blink memory information. +It can be useful for debugging rendering / DOM related memory issues. +Note that all values are reported in Kilobytes. + ### `process.getProcessMemoryInfo()` Returns `Promise` - Resolves with a [ProcessMemoryInfo](structures/process-memory-info.md) diff --git a/spec/api-browser-window-spec.js b/spec/api-browser-window-spec.js index ff4fc79edf83..09a80b20c5ba 100644 --- a/spec/api-browser-window-spec.js +++ b/spec/api-browser-window-spec.js @@ -1927,6 +1927,7 @@ describe('BrowserWindow module', () => { expect(test.hasCrash).to.be.true() expect(test.hasHang).to.be.true() expect(test.heapStatistics).to.be.an('object') + expect(test.blinkMemoryInfo).to.be.an('object') expect(test.processMemoryInfo).to.be.an('object') expect(test.systemVersion).to.be.a('string') expect(test.cpuUsage).to.be.an('object') diff --git a/spec/api-process-spec.js b/spec/api-process-spec.js index 9a4664db7407..e9f873354403 100644 --- a/spec/api-process-spec.js +++ b/spec/api-process-spec.js @@ -38,6 +38,15 @@ describe('process module', () => { }) }) + describe('process.getBlinkMemoryInfo()', () => { + it('returns blink memory information object', () => { + const heapStats = process.getBlinkMemoryInfo() + expect(heapStats.allocated).to.be.a('number') + expect(heapStats.marked).to.be.a('number') + expect(heapStats.total).to.be.a('number') + }) + }) + describe('process.getProcessMemoryInfo()', async () => { it('resolves promise successfully with valid data', async () => { const memoryInfo = await process.getProcessMemoryInfo() diff --git a/spec/fixtures/module/preload-sandbox.js b/spec/fixtures/module/preload-sandbox.js index 02da90f8c560..e1fb9fbd7df8 100644 --- a/spec/fixtures/module/preload-sandbox.js +++ b/spec/fixtures/module/preload-sandbox.js @@ -27,6 +27,7 @@ hasHang: typeof process.hang === 'function', creationTime: invoke(() => process.getCreationTime()), heapStatistics: invoke(() => process.getHeapStatistics()), + blinkMemoryInfo: invoke(() => process.getBlinkMemoryInfo()), processMemoryInfo: invoke(() => process.getProcessMemoryInfo()), systemMemoryInfo: invoke(() => process.getSystemMemoryInfo()), systemVersion: invoke(() => process.getSystemVersion()),