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
1920namespace atom {
@@ -23,6 +24,38 @@ namespace {
2324// Dummy class type that used for crashing the program.
2425struct 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.
2861void 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
0 commit comments