Browse Source

Add process.getHeapStatistics() (#13183)

Milan Burda 6 years ago
parent
commit
6ad0a22602

+ 30 - 0
atom/common/api/atom_bindings.cc

@@ -52,6 +52,7 @@ void AtomBindings::BindTo(v8::Isolate* isolate, v8::Local<v8::Object> 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<v8::Value> 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<double>(v8_heap_stats.total_heap_size() >> 10));
+  dict.Set(
+      "totalHeapSizeExecutable",
+      static_cast<double>(v8_heap_stats.total_heap_size_executable() >> 10));
+  dict.Set("totalPhysicalSize",
+           static_cast<double>(v8_heap_stats.total_physical_size() >> 10));
+  dict.Set("totalAvailableSize",
+           static_cast<double>(v8_heap_stats.total_available_size() >> 10));
+  dict.Set("usedHeapSize",
+           static_cast<double>(v8_heap_stats.used_heap_size() >> 10));
+  dict.Set("heapSizeLimit",
+           static_cast<double>(v8_heap_stats.heap_size_limit() >> 10));
+  dict.Set("mallocedMemory",
+           static_cast<double>(v8_heap_stats.malloced_memory() >> 10));
+  dict.Set("peakMallocedMemory",
+           static_cast<double>(v8_heap_stats.peak_malloced_memory() >> 10));
+  dict.Set("doesZapGarbage",
+           static_cast<bool>(v8_heap_stats.does_zap_garbage()));
+
+  return dict.GetHandle();
+}
+
 // static
 v8::Local<v8::Value> AtomBindings::GetProcessMemoryInfo(v8::Isolate* isolate) {
   std::unique_ptr<base::ProcessMetrics> metrics(

+ 1 - 0
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<v8::Value> GetHeapStatistics(v8::Isolate* isolate);
   static v8::Local<v8::Value> GetProcessMemoryInfo(v8::Isolate* isolate);
   static v8::Local<v8::Value> GetSystemMemoryInfo(v8::Isolate* isolate,
                                                   mate::Arguments* args);

+ 1 - 0
atom/renderer/atom_sandboxed_renderer_client.cc

@@ -90,6 +90,7 @@ void InitializeBindings(v8::Local<v8::Object> 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);
 }

+ 16 - 0
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`:

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

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