feat: add memory to app.getAppMetrics() (#18831)

This commit is contained in:
Milan Burda 2019-07-23 22:41:59 +02:00 committed by Shelley Vohr
parent 2c383b51c1
commit 103b38650f
8 changed files with 149 additions and 1 deletions

View file

@ -1,3 +1,4 @@
import * as fs from 'fs'
import * as path from 'path'
import { deprecate, Menu } from 'electron'
@ -66,6 +67,43 @@ if (process.platform === 'darwin') {
app.dock!.getMenu = () => dockMenu
}
if (process.platform === 'linux') {
const patternVmRSS = /^VmRSS:\s*(\d+) kB$/m
const patternVmHWM = /^VmHWM:\s*(\d+) kB$/m
const getStatus = (pid: number) => {
try {
return fs.readFileSync(`/proc/${pid}/status`, 'utf8')
} catch {
return ''
}
}
const getEntry = (file: string, pattern: RegExp) => {
const match = file.match(pattern)
return match ? parseInt(match[1], 10) : 0
}
const getProcessMemoryInfo = (pid: number) => {
const file = getStatus(pid)
return {
workingSetSize: getEntry(file, patternVmRSS),
peakWorkingSetSize: getEntry(file, patternVmHWM)
}
}
const nativeFn = app.getAppMetrics
app.getAppMetrics = () => {
const metrics = nativeFn.call(app)
for (const metric of metrics) {
metric.memory = getProcessMemoryInfo(metric.pid)
}
return metrics
}
}
// Routes the events to webContents.
const events = ['login', 'certificate-error', 'select-client-certificate']
for (const name of events) {