// Copyright (c) 2015 GitHub, Inc. // Use of this source code is governed by the MIT license that can be // found in the LICENSE file. #include "atom/common/api/atom_api_native_image.h" #include #include #include "atom/common/node_includes.h" #include "base/process/process_metrics.h" #include "native_mate/dictionary.h" namespace { v8::Local GetProcessMetrics(v8::Isolate* isolate) { mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate); std::unique_ptr metrics( base::ProcessMetrics::CreateCurrentProcessMetrics()); dict.Set("workingSetSize", (double)(metrics->GetWorkingSetSize() >> 10)); dict.Set("peakWorkingSetSize", (double)(metrics->GetPeakWorkingSetSize() >> 10)); size_t private_bytes, shared_bytes; if (metrics->GetMemoryBytes(&private_bytes, &shared_bytes)) { dict.Set("privateBytes", (double)(private_bytes >> 10)); dict.Set("sharedBytes", (double)(shared_bytes >> 10)); } return dict.GetHandle(); } v8::Local 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 exports, v8::Local unused, v8::Local context, void* priv) { mate::Dictionary dict(context->GetIsolate(), exports); dict.SetMethod("getProcessMetrics", &GetProcessMetrics); dict.SetMethod("getSystemMemoryInfo", &GetSystemMemoryInfo); } } // namespace NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_common_process_stats, Initialize)