Implement process and system memory statistics
authorPaul Betts <paul@paulbetts.org>
Sat, 14 May 2016 00:50:58 +0000 (17:50 -0700)
committerPaul Betts <paul@paulbetts.org>
Tue, 17 May 2016 21:47:56 +0000 (14:47 -0700)
atom/common/api/atom_api_process_stats.cc

index 8023c5a..a06fb2e 100644 (file)
@@ -8,18 +8,54 @@
 #include <vector>
 
 #include "atom/common/node_includes.h"
+#include "base/process/process_metrics.h"
 #include "native_mate/dictionary.h"
 
 namespace {
 
-int32_t AddTwoNumbers(int32_t a, int32_t b) {
-  return a + b;
+v8::Local<v8::Value> GetProcessMetrics(v8::Isolate* isolate) {
+  mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
+  std::unique_ptr<base::ProcessMetrics> metrics(
+    base::ProcessMetrics::CreateCurrentProcessMetrics());
+
+  dict.Set("workingSetSize", (double)metrics->GetWorkingSetSize());
+  dict.Set("peakWorkingSetSize", (double)metrics->GetPeakWorkingSetSize());
+
+  size_t private_bytes, shared_bytes;
+  if (metrics->GetMemoryBytes(&private_bytes, &shared_bytes)) {
+    dict.set("privateBytes", (double)private_bytes);
+    dict.set("sharedBytes", (double)shared_bytes);
+  }
+
+  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();
 }
 
 void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
                 v8::Local<v8::Context> context, void* priv) {
   mate::Dictionary dict(context->GetIsolate(), exports);
-  dict.SetMethod("addTwoNumbers", &AddTwoNumbers);
+  dict.SetMethod("getProcessMetrics", &GetProcessMetrics);
+  dict.SetMethod("getSystemMemoryInfo", &GetSystemMemoryInfo);
 }
 
 }  // namespace