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

This commit is contained in:
Milan Burda 2019-05-30 11:50:35 +02:00 committed by Alexey Kuzmin
parent 2dbd2c07e4
commit a1226d75ff
6 changed files with 42 additions and 0 deletions

View file

@ -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,

View file

@ -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);

View file

@ -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)

View file

@ -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')

View file

@ -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()

View file

@ -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()),