API to get memory of all processes of the app
This commit is contained in:
parent
dafd0b6c3c
commit
bef7d5a520
3 changed files with 65 additions and 1 deletions
|
@ -912,6 +912,38 @@ void App::GetFileIcon(const base::FilePath& path,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
v8::Local<v8::Value> App::GetAppMemoryInfo(v8::Isolate* isolate) {
|
||||||
|
AppIdProcessIterator processIterator;
|
||||||
|
auto processEntry = processIterator.NextProcessEntry();
|
||||||
|
mate::Dictionary result = mate::Dictionary::CreateEmpty(isolate);
|
||||||
|
|
||||||
|
while(processEntry != nullptr) {
|
||||||
|
int64_t pid = processEntry->pid();
|
||||||
|
auto process = base::Process::OpenWithExtraPrivileges(pid);
|
||||||
|
|
||||||
|
std::unique_ptr<base::ProcessMetrics> metrics(
|
||||||
|
base::ProcessMetrics::CreateProcessMetrics(process.Handle()));
|
||||||
|
|
||||||
|
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
|
||||||
|
|
||||||
|
dict.Set("workingSetSize",
|
||||||
|
static_cast<double>(metrics->GetWorkingSetSize() >> 10));
|
||||||
|
dict.Set("peakWorkingSetSize",
|
||||||
|
static_cast<double>(metrics->GetPeakWorkingSetSize() >> 10));
|
||||||
|
|
||||||
|
size_t private_bytes, shared_bytes;
|
||||||
|
if (metrics->GetMemoryBytes(&private_bytes, &shared_bytes)) {
|
||||||
|
dict.Set("privateBytes", static_cast<double>(private_bytes >> 10));
|
||||||
|
dict.Set("sharedBytes", static_cast<double>(shared_bytes >> 10));
|
||||||
|
}
|
||||||
|
|
||||||
|
result.Set(std::to_string(pid).c_str(), dict);
|
||||||
|
processEntry = processIterator.NextProcessEntry();
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.GetHandle();
|
||||||
|
}
|
||||||
|
|
||||||
// static
|
// static
|
||||||
mate::Handle<App> App::Create(v8::Isolate* isolate) {
|
mate::Handle<App> App::Create(v8::Isolate* isolate) {
|
||||||
return mate::CreateHandle(isolate, new App(isolate));
|
return mate::CreateHandle(isolate, new App(isolate));
|
||||||
|
@ -983,7 +1015,8 @@ void App::BuildPrototype(
|
||||||
&App::IsAccessibilitySupportEnabled)
|
&App::IsAccessibilitySupportEnabled)
|
||||||
.SetMethod("disableHardwareAcceleration",
|
.SetMethod("disableHardwareAcceleration",
|
||||||
&App::DisableHardwareAcceleration)
|
&App::DisableHardwareAcceleration)
|
||||||
.SetMethod("getFileIcon", &App::GetFileIcon);
|
.SetMethod("getFileIcon", &App::GetFileIcon)
|
||||||
|
.SetMethod("getAppMemoryInfo", &App::GetAppMemoryInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace api
|
} // namespace api
|
||||||
|
|
|
@ -13,6 +13,7 @@
|
||||||
#include "atom/browser/browser.h"
|
#include "atom/browser/browser.h"
|
||||||
#include "atom/browser/browser_observer.h"
|
#include "atom/browser/browser_observer.h"
|
||||||
#include "atom/common/native_mate_converters/callback.h"
|
#include "atom/common/native_mate_converters/callback.h"
|
||||||
|
#include "base/process/process_iterator.h"
|
||||||
#include "base/task/cancelable_task_tracker.h"
|
#include "base/task/cancelable_task_tracker.h"
|
||||||
#include "chrome/browser/icon_manager.h"
|
#include "chrome/browser/icon_manager.h"
|
||||||
#include "chrome/browser/process_singleton.h"
|
#include "chrome/browser/process_singleton.h"
|
||||||
|
@ -141,6 +142,8 @@ class App : public AtomBrowserClient::Delegate,
|
||||||
void GetFileIcon(const base::FilePath& path,
|
void GetFileIcon(const base::FilePath& path,
|
||||||
mate::Arguments* args);
|
mate::Arguments* args);
|
||||||
|
|
||||||
|
v8::Local<v8::Value> GetAppMemoryInfo(v8::Isolate* isolate);
|
||||||
|
|
||||||
#if defined(OS_WIN)
|
#if defined(OS_WIN)
|
||||||
// Get the current Jump List settings.
|
// Get the current Jump List settings.
|
||||||
v8::Local<v8::Value> GetJumpListSettings();
|
v8::Local<v8::Value> GetJumpListSettings();
|
||||||
|
@ -163,6 +166,17 @@ class App : public AtomBrowserClient::Delegate,
|
||||||
DISALLOW_COPY_AND_ASSIGN(App);
|
DISALLOW_COPY_AND_ASSIGN(App);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class AppIdProcessIterator : public base::ProcessIterator {
|
||||||
|
public:
|
||||||
|
AppIdProcessIterator() : base::ProcessIterator(NULL) {}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
bool IncludeEntry() override {
|
||||||
|
return (entry().parent_pid() == base::GetCurrentProcId() ||
|
||||||
|
entry().pid() == base::GetCurrentProcId());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace api
|
} // namespace api
|
||||||
|
|
||||||
} // namespace atom
|
} // namespace atom
|
||||||
|
|
|
@ -760,6 +760,23 @@ Disables hardware acceleration for current app.
|
||||||
|
|
||||||
This method can only be called before app is ready.
|
This method can only be called before app is ready.
|
||||||
|
|
||||||
|
### `app.getAppMemoryInfo()`
|
||||||
|
|
||||||
|
Returns `Object[]`:
|
||||||
|
|
||||||
|
* `pid` Integer - The process id for which memory info is collected for
|
||||||
|
* `workingSetSize` Integer - The amount of memory currently pinned to actual physical
|
||||||
|
RAM.
|
||||||
|
* `peakWorkingSetSize` Integer - The maximum amount of memory that has ever been pinned
|
||||||
|
to actual physical RAM.
|
||||||
|
* `privateBytes` Integer - The amount of memory not shared by other processes, such as
|
||||||
|
JS heap or HTML content.
|
||||||
|
* `sharedBytes` Integer - The amount of memory shared between processes, typically
|
||||||
|
memory consumed by the Electron code itself
|
||||||
|
|
||||||
|
Returns an object giving memory usage statistics about all the processes associated with
|
||||||
|
the app. Note that all statistics are reported in Kilobytes.
|
||||||
|
|
||||||
### `app.setBadgeCount(count)` _Linux_ _macOS_
|
### `app.setBadgeCount(count)` _Linux_ _macOS_
|
||||||
|
|
||||||
* `count` Integer
|
* `count` Integer
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue