Merge pull request #9373 from electron/process-metrics

Adding CPU and I/O metrics to process module
This commit is contained in:
Kevin Sawicki 2017-05-04 15:30:19 -07:00 committed by GitHub
commit 70033009eb
6 changed files with 90 additions and 0 deletions

View file

@ -14,6 +14,7 @@
#include "atom/common/node_includes.h"
#include "base/logging.h"
#include "base/process/process_metrics.h"
#include "base/sys_info.h"
#include "native_mate/dictionary.h"
namespace atom {
@ -52,6 +53,8 @@ void AtomBindings::BindTo(v8::Isolate* isolate,
dict.SetMethod("log", &Log);
dict.SetMethod("getProcessMemoryInfo", &GetProcessMemoryInfo);
dict.SetMethod("getSystemMemoryInfo", &GetSystemMemoryInfo);
dict.SetMethod("getCPUUsage", &GetCPUUsage);
dict.SetMethod("getIOCounters", &GetIOCounters);
#if defined(OS_POSIX)
dict.SetMethod("setFdLimit", &base::SetFdLimit);
#endif
@ -171,4 +174,37 @@ v8::Local<v8::Value> AtomBindings::GetSystemMemoryInfo(v8::Isolate* isolate,
return dict.GetHandle();
}
// static
v8::Local<v8::Value> AtomBindings::GetCPUUsage(v8::Isolate* isolate) {
std::unique_ptr<base::ProcessMetrics> metrics(
base::ProcessMetrics::CreateCurrentProcessMetrics());
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
int processor_count = base::SysInfo::NumberOfProcessors();
dict.Set("percentCPUUsage",
metrics->GetPlatformIndependentCPUUsage() / processor_count);
dict.Set("idleWakeupsPerSecond", metrics->GetIdleWakeupsPerSecond());
return dict.GetHandle();
}
// static
v8::Local<v8::Value> AtomBindings::GetIOCounters(v8::Isolate* isolate) {
std::unique_ptr<base::ProcessMetrics> metrics(
base::ProcessMetrics::CreateCurrentProcessMetrics());
base::IoCounters io_counters;
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
if (metrics->GetIOCounters(&io_counters)) {
dict.Set("readOperationCount", io_counters.ReadOperationCount);
dict.Set("writeOperationCount", io_counters.WriteOperationCount);
dict.Set("otherOperationCount", io_counters.OtherOperationCount);
dict.Set("readTransferCount", io_counters.ReadTransferCount);
dict.Set("writeTransferCount", io_counters.WriteTransferCount);
dict.Set("otherTransferCount", io_counters.OtherTransferCount);
}
return dict.GetHandle();
}
} // namespace atom

View file

@ -37,6 +37,8 @@ class AtomBindings {
static v8::Local<v8::Value> GetProcessMemoryInfo(v8::Isolate* isolate);
static v8::Local<v8::Value> GetSystemMemoryInfo(v8::Isolate* isolate,
mate::Arguments* args);
static v8::Local<v8::Value> GetCPUUsage(v8::Isolate* isolate);
static v8::Local<v8::Value> GetIOCounters(v8::Isolate* isolate);
private:
void ActivateUVLoop(v8::Isolate* isolate);

View file

@ -116,3 +116,15 @@ Returns `Object`:
Returns an object giving memory usage statistics about the entire system. Note
that all statistics are reported in Kilobytes.
### `process.getCPUUsage()`
Returns:
* `CPUUsage` [CPUUsage](structures/cpu-usage.md)
### `process.getIOCounters()` _Windows_ _Linux_
Returns:
* `IOCounters` [IOCounters](structures/io-counters.md)

View file

@ -0,0 +1,6 @@
# CPUUsage Object
* `percentCPUUsage` Number - Percentage of CPU used since the last call to getCPUUsage.
First call returns 0.
* `idleWakeupsPerSecond` Number - The number of average idle cpu wakeups per second
since the last call to getCPUUsage. First call returns 0.

View file

@ -0,0 +1,8 @@
# IOCounters Object
* `readOperationCount` Number - The number of I/O read operations.
* `writeOperationCount` Number - The number of I/O write operations.
* `otherOperationCount` Number - Then number of I/O other operations.
* `readTransferCount` Number - The number of I/O read transfers.
* `writeTransferCount` Number - The number of I/O write transfers.
* `otherTransferCount` Number - Then number of I/O other transfers.

26
spec/api-process-spec.js Normal file
View file

@ -0,0 +1,26 @@
const assert = require('assert')
describe('process module', function () {
describe('process.getCPUUsage()', function () {
it('returns a cpu usage object', function () {
const cpuUsage = process.getCPUUsage()
assert.equal(typeof cpuUsage.percentCPUUsage, 'number')
assert.equal(typeof cpuUsage.idleWakeupsPerSecond, 'number')
})
})
describe('process.getIOCounters()', function () {
it('returns an io counters object', function () {
if (process.platform === 'darwin') {
return
}
const ioCounters = process.getIOCounters()
assert.equal(typeof ioCounters.readOperationCount, 'number')
assert.equal(typeof ioCounters.writeOperationCount, 'number')
assert.equal(typeof ioCounters.otherOperationCount, 'number')
assert.equal(typeof ioCounters.readTransferCount, 'number')
assert.equal(typeof ioCounters.writeTransferCount, 'number')
assert.equal(typeof ioCounters.otherTransferCount, 'number')
})
})
})