Browse Source

feat: add process.getBlinkMemoryInfo() (#17762)

Milan Burda 5 years ago
parent
commit
a1226d75ff

+ 17 - 0
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<v8::Promise> ElectronBindings::GetProcessMemoryInfo(
   return handle;
 }
 
+// static
+v8::Local<v8::Value> 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<double>(allocated >> 10));
+  dict.Set("marked", static_cast<double>(marked >> 10));
+  dict.Set("total", static_cast<double>(total >> 10));
+  return dict.GetHandle();
+}
+
 // static
 void ElectronBindings::DidReceiveMemoryDump(
     v8::Global<v8::Context> context,

+ 1 - 0
atom/common/api/electron_bindings.h

@@ -58,6 +58,7 @@ class ElectronBindings {
   static v8::Local<v8::Value> GetSystemMemoryInfo(v8::Isolate* isolate,
                                                   mate::Arguments* args);
   static v8::Local<v8::Promise> GetProcessMemoryInfo(v8::Isolate* isolate);
+  static v8::Local<v8::Value> GetBlinkMemoryInfo(v8::Isolate* isolate);
   static v8::Local<v8::Value> GetCPUUsage(base::ProcessMetrics* metrics,
                                           v8::Isolate* isolate);
   static v8::Local<v8::Value> GetIOCounters(v8::Isolate* isolate);

+ 13 - 0
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<ProcessMemoryInfo>` - Resolves with a [ProcessMemoryInfo](structures/process-memory-info.md)

+ 1 - 0
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')

+ 9 - 0
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()

+ 1 - 0
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()),