2016-05-14 00:33:47 +00:00
|
|
|
// 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 <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "atom/common/node_includes.h"
|
2016-05-14 00:50:58 +00:00
|
|
|
#include "base/process/process_metrics.h"
|
2016-05-14 00:33:47 +00:00
|
|
|
#include "native_mate/dictionary.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2016-05-14 00:57:35 +00:00
|
|
|
v8::Local<v8::Value> GetProcessMemoryInfo(v8::Isolate* isolate) {
|
2016-05-14 00:50:58 +00:00
|
|
|
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
|
|
|
|
std::unique_ptr<base::ProcessMetrics> metrics(
|
|
|
|
base::ProcessMetrics::CreateCurrentProcessMetrics());
|
|
|
|
|
2016-05-14 01:01:29 +00:00
|
|
|
dict.Set("workingSetSize",
|
|
|
|
static_cast<double>(metrics->GetWorkingSetSize() >> 10));
|
|
|
|
dict.Set("peakWorkingSetSize",
|
|
|
|
static_cast<double>(metrics->GetPeakWorkingSetSize() >> 10));
|
2016-05-14 00:50:58 +00:00
|
|
|
|
|
|
|
size_t private_bytes, shared_bytes;
|
|
|
|
if (metrics->GetMemoryBytes(&private_bytes, &shared_bytes)) {
|
2016-05-14 01:01:29 +00:00
|
|
|
dict.Set("privateBytes", static_cast<double>(private_bytes >> 10));
|
|
|
|
dict.Set("sharedBytes", static_cast<double>(shared_bytes >> 10));
|
2016-05-14 00:50:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return dict.GetHandle();
|
|
|
|
}
|
|
|
|
|
2016-05-14 01:01:29 +00:00
|
|
|
v8::Local<v8::Value> GetSystemMemoryInfo(
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
mate::Arguments* args) {
|
2016-05-14 00:50:58 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-05-14 00:56:40 +00:00
|
|
|
dict.Set("total", memInfo.total);
|
|
|
|
dict.Set("free", memInfo.free);
|
2016-05-14 00:50:58 +00:00
|
|
|
|
|
|
|
// NB: These return bogus values on OS X
|
2016-05-14 00:56:40 +00:00
|
|
|
#if !defined(OS_MACOSX)
|
|
|
|
dict.Set("swapTotal", memInfo.swap_total);
|
|
|
|
dict.Set("swapFree", memInfo.swap_free);
|
2016-05-14 00:50:58 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return dict.GetHandle();
|
2016-05-14 00:33:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2016-05-14 00:57:35 +00:00
|
|
|
dict.SetMethod("getProcessMemoryInfo", &GetProcessMemoryInfo);
|
2016-05-14 00:50:58 +00:00
|
|
|
dict.SetMethod("getSystemMemoryInfo", &GetSystemMemoryInfo);
|
2016-05-14 00:33:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_common_process_stats, Initialize)
|