Merge pull request #132 from ente-io/log-high-memory-usage

Log high memory usage
This commit is contained in:
Abhinav Kumar 2023-01-12 11:11:40 +05:30 committed by GitHub
commit 3fb9318ca2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 273 additions and 14 deletions

View file

@ -1,7 +1,7 @@
const PROD_HOST_URL: string = 'ente://app';
const RENDERER_OUTPUT_DIR: string = './ui/out';
const LOG_FILENAME = 'ente.log';
const MAX_LOG_SIZE = 5 * 1024 * 1024; // 5MB
const MAX_LOG_SIZE = 50 * 1024 * 1024; // 50MB
const FILE_STREAM_CHUNK_SIZE: number = 4 * 1024 * 1024;

View file

@ -39,10 +39,18 @@ export async function runFFmpegCmd(
});
const escapedCmd = shellescape(cmd);
log.info('running ffmpeg command', escapedCmd);
const startTime = Date.now();
await execAsync(escapedCmd);
if (!existsSync(tempOutputFilePath)) {
throw new Error('ffmpeg output file not found');
}
log.info(
'ffmpeg command execution time ',
escapedCmd,
Date.now() - startTime,
'ms'
);
const outputFile = await readFile(tempOutputFilePath);
return new Uint8Array(outputFile);
} catch (e) {

View file

@ -1,27 +1,149 @@
import ElectronLog from 'electron-log';
import { webFrame } from 'electron/renderer';
const FIVE_MINUTES_IN_MICROSECONDS = 30 * 1000;
const LOGGING_INTERVAL_IN_MICROSECONDS = 30 * 1000; // 30 seconds
const SPIKE_DETECTION_INTERVAL_IN_MICROSECONDS = 1 * 1000; // 1 seconds
const MAIN_MEMORY_USAGE_DIFF_IN_KILOBYTES_CONSIDERED_AS_SPIKE = 10 * 1024; // 10 MB
const HIGH_MAIN_MEMORY_USAGE_THRESHOLD_IN_KILOBYTES = 100 * 1024; // 100 MB
const RENDERER_MEMORY_USAGE_DIFF_IN_KILOBYTES_CONSIDERED_AS_SPIKE = 200 * 1024; // 200 MB
const HIGH_RENDERER_MEMORY_USAGE_THRESHOLD_IN_KILOBYTES = 1024 * 1024; // 1 GB
async function logMainProcessStats() {
const systemMemoryInfo = process.getSystemMemoryInfo();
const processMemoryInfo = await getNormalizedProcessMemoryInfo(
await process.getProcessMemoryInfo()
);
const cpuUsage = process.getCPUUsage();
const processMemoryInfo = await process.getProcessMemoryInfo();
const heapStatistics = process.getHeapStatistics();
const heapStatistics = getNormalizedHeapStatistics(
process.getHeapStatistics()
);
ElectronLog.log('main process stats', {
systemMemoryInfo,
cpuUsage,
processMemoryInfo,
heapStatistics,
cpuUsage,
});
}
let previousMainProcessMemoryInfo: Electron.ProcessMemoryInfo = {
private: 0,
shared: 0,
residentSet: 0,
};
let mainProcessUsingHighMemory = false;
async function logSpikeMainMemoryUsage() {
const processMemoryInfo = await process.getProcessMemoryInfo();
const currentMemoryUsage = Math.max(
processMemoryInfo.residentSet,
processMemoryInfo.private
);
const previewMemoryUsage = Math.max(
previousMainProcessMemoryInfo.private,
previousMainProcessMemoryInfo.residentSet
);
const isSpiking =
currentMemoryUsage - previewMemoryUsage >=
MAIN_MEMORY_USAGE_DIFF_IN_KILOBYTES_CONSIDERED_AS_SPIKE;
const isHighMemoryUsage =
currentMemoryUsage >= HIGH_MAIN_MEMORY_USAGE_THRESHOLD_IN_KILOBYTES;
const shouldReport =
(isHighMemoryUsage && !mainProcessUsingHighMemory) ||
(!isHighMemoryUsage && mainProcessUsingHighMemory);
if (isSpiking || shouldReport) {
const normalizedCurrentProcessMemoryInfo =
await getNormalizedProcessMemoryInfo(processMemoryInfo);
const normalizedPreviousProcessMemoryInfo =
await getNormalizedProcessMemoryInfo(previousMainProcessMemoryInfo);
const cpuUsage = process.getCPUUsage();
const heapStatistics = getNormalizedHeapStatistics(
process.getHeapStatistics()
);
ElectronLog.log('reporting main memory usage spike', {
currentProcessMemoryInfo: normalizedCurrentProcessMemoryInfo,
previousProcessMemoryInfo: normalizedPreviousProcessMemoryInfo,
heapStatistics,
cpuUsage,
});
}
previousMainProcessMemoryInfo = processMemoryInfo;
if (shouldReport) {
mainProcessUsingHighMemory = !mainProcessUsingHighMemory;
}
}
let previousRendererProcessMemoryInfo: Electron.ProcessMemoryInfo = {
private: 0,
shared: 0,
residentSet: 0,
};
let rendererUsingHighMemory = false;
async function logSpikeRendererMemoryUsage() {
const processMemoryInfo = await process.getProcessMemoryInfo();
const currentMemoryUsage = Math.max(
processMemoryInfo.residentSet,
processMemoryInfo.private
);
const previewMemoryUsage = Math.max(
previousRendererProcessMemoryInfo.private,
previousRendererProcessMemoryInfo.residentSet
);
const isSpiking =
currentMemoryUsage - previewMemoryUsage >=
RENDERER_MEMORY_USAGE_DIFF_IN_KILOBYTES_CONSIDERED_AS_SPIKE;
const isHighMemoryUsage =
currentMemoryUsage >= HIGH_RENDERER_MEMORY_USAGE_THRESHOLD_IN_KILOBYTES;
const shouldReport =
(isHighMemoryUsage && !rendererUsingHighMemory) ||
(!isHighMemoryUsage && rendererUsingHighMemory);
if (isSpiking || shouldReport) {
const normalizedCurrentProcessMemoryInfo =
await getNormalizedProcessMemoryInfo(processMemoryInfo);
const normalizedPreviousProcessMemoryInfo =
await getNormalizedProcessMemoryInfo(
previousRendererProcessMemoryInfo
);
const cpuUsage = process.getCPUUsage();
const heapStatistics = getNormalizedHeapStatistics(
process.getHeapStatistics()
);
ElectronLog.log('reporting renderer memory usage spike', {
currentProcessMemoryInfo: normalizedCurrentProcessMemoryInfo,
previousProcessMemoryInfo: normalizedPreviousProcessMemoryInfo,
heapStatistics,
cpuUsage,
});
}
previousRendererProcessMemoryInfo = processMemoryInfo;
if (shouldReport) {
rendererUsingHighMemory = !rendererUsingHighMemory;
}
}
async function logRendererProcessStats() {
const blinkMemoryInfo = process.getBlinkMemoryInfo();
const heapStatistics = process.getHeapStatistics();
const processMemoryInfo = process.getProcessMemoryInfo();
const webFrameResourceUsage = webFrame.getResourceUsage();
const blinkMemoryInfo = getNormalizedBlinkMemoryInfo();
const heapStatistics = getNormalizedHeapStatistics(
process.getHeapStatistics()
);
const webFrameResourceUsage = getNormalizedWebFrameResourceUsage();
const processMemoryInfo = await getNormalizedProcessMemoryInfo(
await process.getProcessMemoryInfo()
);
ElectronLog.log('renderer process stats', {
blinkMemoryInfo,
heapStatistics,
@ -31,9 +153,138 @@ async function logRendererProcessStats() {
}
export function setupMainProcessStatsLogger() {
setInterval(logMainProcessStats, FIVE_MINUTES_IN_MICROSECONDS);
setInterval(
logSpikeMainMemoryUsage,
SPIKE_DETECTION_INTERVAL_IN_MICROSECONDS
);
setInterval(logMainProcessStats, LOGGING_INTERVAL_IN_MICROSECONDS);
}
export function setupRendererProcessStatsLogger() {
setInterval(logRendererProcessStats, FIVE_MINUTES_IN_MICROSECONDS);
setInterval(
logSpikeRendererMemoryUsage,
SPIKE_DETECTION_INTERVAL_IN_MICROSECONDS
);
setInterval(logRendererProcessStats, LOGGING_INTERVAL_IN_MICROSECONDS);
}
const getNormalizedProcessMemoryInfo = async (
processMemoryInfo: Electron.ProcessMemoryInfo
) => {
return {
residentSet: convertBytesToHumanReadable(
processMemoryInfo.residentSet * 1024
),
private: convertBytesToHumanReadable(processMemoryInfo.private * 1024),
shared: convertBytesToHumanReadable(processMemoryInfo.shared * 1024),
};
};
const getNormalizedBlinkMemoryInfo = () => {
const blinkMemoryInfo = process.getBlinkMemoryInfo();
return {
allocated: convertBytesToHumanReadable(
blinkMemoryInfo.allocated * 1024
),
total: convertBytesToHumanReadable(blinkMemoryInfo.total * 1024),
};
};
const getNormalizedHeapStatistics = (
heapStatistics: Electron.HeapStatistics
) => {
return {
totalHeapSize: convertBytesToHumanReadable(
heapStatistics.totalHeapSize * 1024
),
totalHeapSizeExecutable: convertBytesToHumanReadable(
heapStatistics.totalHeapSizeExecutable * 1024
),
totalPhysicalSize: convertBytesToHumanReadable(
heapStatistics.totalPhysicalSize * 1024
),
totalAvailableSize: convertBytesToHumanReadable(
heapStatistics.totalAvailableSize * 1024
),
usedHeapSize: convertBytesToHumanReadable(
heapStatistics.usedHeapSize * 1024
),
heapSizeLimit: convertBytesToHumanReadable(
heapStatistics.heapSizeLimit * 1024
),
mallocedMemory: convertBytesToHumanReadable(
heapStatistics.mallocedMemory * 1024
),
peakMallocedMemory: convertBytesToHumanReadable(
heapStatistics.peakMallocedMemory * 1024
),
doesZapGarbage: heapStatistics.doesZapGarbage,
};
};
const getNormalizedWebFrameResourceUsage = () => {
const webFrameResourceUsage = webFrame.getResourceUsage();
return {
images: {
count: webFrameResourceUsage.images.count,
size: convertBytesToHumanReadable(
webFrameResourceUsage.images.size
),
liveSize: convertBytesToHumanReadable(
webFrameResourceUsage.images.liveSize
),
},
scripts: {
count: webFrameResourceUsage.scripts.count,
size: convertBytesToHumanReadable(
webFrameResourceUsage.scripts.size
),
liveSize: convertBytesToHumanReadable(
webFrameResourceUsage.scripts.liveSize
),
},
cssStyleSheets: {
count: webFrameResourceUsage.cssStyleSheets.count,
size: convertBytesToHumanReadable(
webFrameResourceUsage.cssStyleSheets.size
),
liveSize: convertBytesToHumanReadable(
webFrameResourceUsage.cssStyleSheets.liveSize
),
},
xslStyleSheets: {
count: webFrameResourceUsage.xslStyleSheets.count,
size: convertBytesToHumanReadable(
webFrameResourceUsage.xslStyleSheets.size
),
liveSize: convertBytesToHumanReadable(
webFrameResourceUsage.xslStyleSheets.liveSize
),
},
fonts: {
count: webFrameResourceUsage.fonts.count,
size: convertBytesToHumanReadable(webFrameResourceUsage.fonts.size),
liveSize: convertBytesToHumanReadable(
webFrameResourceUsage.fonts.liveSize
),
},
other: {
count: webFrameResourceUsage.other.count,
size: convertBytesToHumanReadable(webFrameResourceUsage.other.size),
liveSize: convertBytesToHumanReadable(
webFrameResourceUsage.other.liveSize
),
},
};
};
function convertBytesToHumanReadable(bytes: number, precision = 2): string {
if (bytes === 0 || isNaN(bytes)) {
return '0 MB';
}
const i = Math.floor(Math.log(bytes) / Math.log(1024));
const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
return (bytes / Math.pow(1024, i)).toFixed(precision) + ' ' + sizes[i];
}

2
ui

@ -1 +1 @@
Subproject commit a955ae39fa97232131682ad72d866bf3155a0906
Subproject commit 86a7f1cdab5c3a9a2cfbd0daecb9f7e1dd06660e