Add process.getHeapStatistics() (#13183)

This commit is contained in:
Milan Burda 2018-06-10 14:00:36 +02:00 committed by Samuel Attard
parent 1b8790aeb2
commit 6ad0a22602
6 changed files with 64 additions and 0 deletions

View file

@ -52,6 +52,7 @@ void AtomBindings::BindTo(v8::Isolate* isolate, v8::Local<v8::Object> process) {
dict.SetMethod("crash", &AtomBindings::Crash); dict.SetMethod("crash", &AtomBindings::Crash);
dict.SetMethod("hang", &Hang); dict.SetMethod("hang", &Hang);
dict.SetMethod("log", &Log); dict.SetMethod("log", &Log);
dict.SetMethod("getHeapStatistics", &GetHeapStatistics);
dict.SetMethod("getProcessMemoryInfo", &GetProcessMemoryInfo); dict.SetMethod("getProcessMemoryInfo", &GetProcessMemoryInfo);
dict.SetMethod("getSystemMemoryInfo", &GetSystemMemoryInfo); dict.SetMethod("getSystemMemoryInfo", &GetSystemMemoryInfo);
dict.SetMethod("getCPUUsage", base::Bind(&AtomBindings::GetCPUUsage, dict.SetMethod("getCPUUsage", base::Bind(&AtomBindings::GetCPUUsage,
@ -125,6 +126,35 @@ void AtomBindings::Hang() {
base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1)); 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 // static
v8::Local<v8::Value> AtomBindings::GetProcessMemoryInfo(v8::Isolate* isolate) { v8::Local<v8::Value> AtomBindings::GetProcessMemoryInfo(v8::Isolate* isolate) {
std::unique_ptr<base::ProcessMetrics> metrics( std::unique_ptr<base::ProcessMetrics> metrics(

View file

@ -35,6 +35,7 @@ class AtomBindings {
static void Log(const base::string16& message); static void Log(const base::string16& message);
static void Crash(); static void Crash();
static void Hang(); 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> GetProcessMemoryInfo(v8::Isolate* isolate);
static v8::Local<v8::Value> GetSystemMemoryInfo(v8::Isolate* isolate, static v8::Local<v8::Value> GetSystemMemoryInfo(v8::Isolate* isolate,
mate::Arguments* args); mate::Arguments* args);

View file

@ -90,6 +90,7 @@ void InitializeBindings(v8::Local<v8::Object> binding,
b.SetMethod("crash", AtomBindings::Crash); b.SetMethod("crash", AtomBindings::Crash);
b.SetMethod("hang", AtomBindings::Hang); b.SetMethod("hang", AtomBindings::Hang);
b.SetMethod("getArgv", GetArgv); b.SetMethod("getArgv", GetArgv);
b.SetMethod("getHeapStatistics", &AtomBindings::GetHeapStatistics);
b.SetMethod("getProcessMemoryInfo", &AtomBindings::GetProcessMemoryInfo); b.SetMethod("getProcessMemoryInfo", &AtomBindings::GetProcessMemoryInfo);
b.SetMethod("getSystemMemoryInfo", &AtomBindings::GetSystemMemoryInfo); b.SetMethod("getSystemMemoryInfo", &AtomBindings::GetSystemMemoryInfo);
} }

View file

@ -106,6 +106,22 @@ Returns [`CPUUsage`](structures/cpu-usage.md)
Returns [`IOCounters`](structures/io-counters.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()` ### `process.getProcessMemoryInfo()`
Returns `Object`: Returns `Object`:

View file

@ -52,6 +52,7 @@ require('../renderer/web-frame-init')()
const preloadProcess = new events.EventEmitter() const preloadProcess = new events.EventEmitter()
preloadProcess.crash = () => binding.crash() preloadProcess.crash = () => binding.crash()
preloadProcess.hang = () => binding.hang() preloadProcess.hang = () => binding.hang()
preloadProcess.getHeapStatistics = () => binding.getHeapStatistics()
preloadProcess.getProcessMemoryInfo = () => binding.getProcessMemoryInfo() preloadProcess.getProcessMemoryInfo = () => binding.getProcessMemoryInfo()
preloadProcess.getSystemMemoryInfo = () => binding.getSystemMemoryInfo() preloadProcess.getSystemMemoryInfo = () => binding.getSystemMemoryInfo()
preloadProcess.argv = binding.getArgv() preloadProcess.argv = binding.getArgv()

View file

@ -26,4 +26,19 @@ describe('process module', () => {
assert.equal(typeof ioCounters.otherTransferCount, 'number') 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')
})
})
}) })