Merge pull request #5526 from electron/process-stats

Process and System memory statistics API
This commit is contained in:
Cheng Zhao 2016-05-19 10:34:01 +00:00
commit 0bd3e28a05
2 changed files with 79 additions and 4 deletions

View file

@ -33,6 +33,48 @@ void Hang() {
base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1));
}
v8::Local<v8::Value> GetProcessMemoryInfo(v8::Isolate* isolate) {
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
std::unique_ptr<base::ProcessMetrics> metrics(
base::ProcessMetrics::CreateCurrentProcessMetrics());
dict.Set("workingSetSize",
static_cast<double>(metrics->GetWorkingSetSize() >> 10));
dict.Set("peakWorkingSetSize",
static_cast<double>(metrics->GetPeakWorkingSetSize() >> 10));
size_t private_bytes, shared_bytes;
if (metrics->GetMemoryBytes(&private_bytes, &shared_bytes)) {
dict.Set("privateBytes", static_cast<double>(private_bytes >> 10));
dict.Set("sharedBytes", static_cast<double>(shared_bytes >> 10));
}
return dict.GetHandle();
}
v8::Local<v8::Value> GetSystemMemoryInfo(
v8::Isolate* isolate,
mate::Arguments* args) {
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
base::SystemMemoryInfoKB memInfo;
if (!base::GetSystemMemoryInfo(&memInfo)) {
args->ThrowError("Unable to retrieve system memory information");
return v8::Undefined(isolate);
}
dict.Set("total", memInfo.total);
dict.Set("free", memInfo.free);
// NB: These return bogus values on OS X
#if !defined(OS_MACOSX)
dict.Set("swapTotal", memInfo.swap_total);
dict.Set("swapFree", memInfo.swap_free);
#endif
return dict.GetHandle();
}
// Called when there is a fatal error in V8, we just crash the process here so
// we can get the stack trace.
void FatalErrorCallback(const char* location, const char* message) {
@ -63,6 +105,8 @@ void AtomBindings::BindTo(v8::Isolate* isolate,
dict.SetMethod("crash", &Crash);
dict.SetMethod("hang", &Hang);
dict.SetMethod("log", &Log);
dict.SetMethod("getProcessMemoryInfo", &GetProcessMemoryInfo);
dict.SetMethod("getSystemMemoryInfo", &GetSystemMemoryInfo);
#if defined(OS_POSIX)
dict.SetMethod("setFdLimit", &base::SetFdLimit);
#endif

View file

@ -14,10 +14,10 @@ upstream node:
other builds it is `undefined`.
* `process.windowsStore` Boolean - If the app is running as a Windows Store app
(appx), this value is `true`, for other builds it is `undefined`.
* `process.defaultApp` Boolean - When app is started by being passed as parameter
to the default app, this value is `true` in the main process, otherwise it is
`undefined`.
* `process.defaultApp` Boolean - When app is started by being passed as
parameter to the default app, this value is `true` in the main process,
otherwise it is `undefined`.
## Events
### Event: 'loaded'
@ -63,3 +63,34 @@ Causes the main thread of the current process hang.
Sets the file descriptor soft limit to `maxDescriptors` or the OS hard
limit, whichever is lower for the current process.
### getProcessMemoryInfo()
Return an object giving memory usage statistics about the current process. Note
that all statistics are reported in Kilobytes.
* `workingSetSize` - The amount of memory currently pinned to actual physical
RAM
* `peakWorkingSetSize` - The maximum amount of memory that has ever been pinned
to actual physical RAM
* `privateBytes` - The amount of memory not shared by other processes, such as
JS heap or HTML content.
* `sharedBytes` - The amount of memory shared between processes, typically
memory consumed by the Electron code itself
### getSystemMemoryInfo()
Return an object giving memory usage statistics about the entire system. Note
that all statistics are reported in Kilobytes.
* `total` - The total amount of physical memory in Kilobytes available to the
system
* `free` - The total amount of memory not being used by applications or disk
cache
On Windows / Linux:
* `swapTotal` - The total amount of swap memory in Kilobytes available to the
system
* `swapFree` - The free amount of swap memory in Kilobytes available to the
system