X Tutup
Skip to content

Commit 8a5aa04

Browse files
committed
Adding CPU and I/O metrics to process module
1 parent 70e199e commit 8a5aa04

File tree

5 files changed

+84
-0
lines changed

5 files changed

+84
-0
lines changed

atom/common/api/atom_bindings.cc

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "atom/common/node_includes.h"
1515
#include "base/logging.h"
1616
#include "base/process/process_metrics.h"
17+
#include "base/sys_info.h"
1718
#include "native_mate/dictionary.h"
1819

1920
namespace atom {
@@ -23,6 +24,38 @@ namespace {
2324
// Dummy class type that used for crashing the program.
2425
struct DummyClass { bool crash; };
2526

27+
v8::Local<v8::Value> GetCPUUsage(v8::Isolate* isolate) {
28+
std::unique_ptr<base::ProcessMetrics> metrics(
29+
base::ProcessMetrics::CreateCurrentProcessMetrics());
30+
31+
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
32+
int processor_count = base::SysInfo::NumberOfProcessors();
33+
dict.Set("percentCPUUsage",
34+
metrics->GetPlatformIndependentCPUUsage() / processor_count);
35+
dict.Set("idleWakeupsPerSecond", metrics->GetIdleWakeupsPerSecond());
36+
37+
return dict.GetHandle();
38+
}
39+
40+
v8::Local<v8::Value> GetIOCounters(v8::Isolate* isolate) {
41+
std::unique_ptr<base::ProcessMetrics> metrics(
42+
base::ProcessMetrics::CreateCurrentProcessMetrics());
43+
base::IoCounters io_counters;
44+
const bool got_counters = metrics->GetIOCounters(&io_counters);
45+
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
46+
47+
if (got_counters) {
48+
dict.Set("readOperationCount", io_counters.ReadOperationCount);
49+
dict.Set("writeOperationCount", io_counters.WriteOperationCount);
50+
dict.Set("otherOperationCount", io_counters.OtherOperationCount);
51+
dict.Set("readTransferCount", io_counters.ReadTransferCount);
52+
dict.Set("writeTransferCount", io_counters.WriteTransferCount);
53+
dict.Set("otherTransferCount", io_counters.OtherTransferCount);
54+
}
55+
56+
return dict.GetHandle();
57+
}
58+
2659
// Called when there is a fatal error in V8, we just crash the process here so
2760
// we can get the stack trace.
2861
void FatalErrorCallback(const char* location, const char* message) {
@@ -52,6 +85,8 @@ void AtomBindings::BindTo(v8::Isolate* isolate,
5285
dict.SetMethod("log", &Log);
5386
dict.SetMethod("getProcessMemoryInfo", &GetProcessMemoryInfo);
5487
dict.SetMethod("getSystemMemoryInfo", &GetSystemMemoryInfo);
88+
dict.SetMethod("getCPUUsage", &GetCPUUsage);
89+
dict.SetMethod("getIOCounters", &GetIOCounters);
5590
#if defined(OS_POSIX)
5691
dict.SetMethod("setFdLimit", &base::SetFdLimit);
5792
#endif

docs/api/process.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,15 @@ Returns `Object`:
116116

117117
Returns an object giving memory usage statistics about the entire system. Note
118118
that all statistics are reported in Kilobytes.
119+
120+
### `process.getCPUUsage()`
121+
122+
Returns:
123+
124+
* `CPUUsage` [CPUUsage](structures/cpu-usage.md)
125+
126+
### `process.getIOCounters()`
127+
128+
Returns:
129+
130+
* `IOCounters` [IOCounters](structures/io-counters.md)

docs/api/structures/cpu-usage.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# CPUUsage Object
2+
3+
* `percentCPUUsage` number - Percentage of CPU used since the last call to getCPUUsage.
4+
First call returns 0.
5+
* `idleWakeupsPerSecond` number - The number of average idle cpu wakeups per second
6+
since the last call to getCPUUsage. First call returns 0.

docs/api/structures/io-counters.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# IOCounters Object
2+
3+
* `readOperationCount` Number - The number of I/O read operations.
4+
* `writeOperationCount` Number - The number of I/O write operations.
5+
* `otherOperationCount` Number - Then number of I/O other operations.
6+
* `readTransferCount` Number - The number of I/O read transfers.
7+
* `writeTransferCount` Number - The number of I/O write transfers.
8+
* `otherTransferCount` Number - Then number of I/O other transfers.

spec/api-process-spec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const assert = require('assert')
2+
3+
describe('process module', function () {
4+
describe('process.getCPUUsage()', function () {
5+
it('returns a cpu usage object', function () {
6+
var cpuUsage = process.getCPUUsage()
7+
assert.equal(typeof cpuUsage.percentCPUUsage, 'number')
8+
assert.equal(typeof cpuUsage.idleWakeupsPerSecond, 'number')
9+
})
10+
})
11+
12+
describe('process.getIOCounters()', function () {
13+
it('returns an io counters object', function () {
14+
var ioCounters = process.getIOCounters()
15+
assert.ok(ioCounters.readOperationCount > 0, 'read operation count not > 0')
16+
assert.ok(ioCounters.writeOperationCount > 0, 'write operation count not > 0')
17+
assert.ok(ioCounters.otherOperationCount > 0, 'other operation count not > 0')
18+
assert.ok(ioCounters.readTransferCount > 0, 'read transfer count not > 0')
19+
assert.ok(ioCounters.writeTransferCount > 0, 'write transfer count not > 0')
20+
assert.ok(ioCounters.otherTransferCount > 0, 'other transfer count not > 0')
21+
})
22+
})
23+
})

0 commit comments

Comments
 (0)
X Tutup