fix: High CPU due to WMI queries (#5924)

* fix: High CPU due to WMI queries
This commit is contained in:
naman-bruno
2025-10-30 15:32:21 +05:30
committed by GitHub
parent 73caaef42b
commit b69db7b44b
7 changed files with 328 additions and 127 deletions

View File

@@ -1,4 +1,4 @@
import React from 'react';
import React, { useEffect } from 'react';
import { useSelector } from 'react-redux';
import StyledWrapper from './StyledWrapper';
import {
@@ -12,6 +12,37 @@ import {
const Performance = () => {
const { systemResources } = useSelector((state) => state.performance);
useEffect(() => {
const { ipcRenderer } = window;
if (!ipcRenderer) {
console.warn('IPC Renderer not available');
return;
}
const startMonitoring = async () => {
try {
await ipcRenderer.invoke('renderer:start-system-monitoring', 2000);
} catch (error) {
console.error('Failed to start system monitoring:', error);
}
};
const stopMonitoring = async () => {
try {
await ipcRenderer.invoke('renderer:stop-system-monitoring');
} catch (error) {
console.error('Failed to stop system monitoring:', error);
}
};
startMonitoring();
return () => {
stopMonitoring();
};
}, []);
const formatBytes = (bytes) => {
if (bytes === 0) return '0 Bytes';
const k = 1024;

View File

@@ -66,7 +66,6 @@
"lodash": "^4.17.21",
"mime-types": "^2.1.35",
"nanoid": "3.3.8",
"pidusage": "^4.0.1",
"qs": "^6.11.0",
"socks-proxy-agent": "^8.0.2",
"tough-cookie": "^6.0.0",

View File

@@ -1,4 +1,4 @@
const pidusage = require('pidusage');
const { app } = require('electron');
class SystemMonitor {
constructor() {
@@ -19,38 +19,59 @@ class SystemMonitor {
this.emitSystemStats(win);
// Set up periodic monitoring
this.intervalId = setInterval(() => {
// Use setTimeout pattern instead of setInterval to avoid overlapping calls
this.scheduleNextEmit(win, intervalMs);
}
scheduleNextEmit(win, intervalMs) {
if (!this.isMonitoring) {
return;
}
this.intervalId = setTimeout(() => {
this.emitSystemStats(win);
this.scheduleNextEmit(win, intervalMs);
}, intervalMs);
}
stop() {
if (this.intervalId) {
clearInterval(this.intervalId);
clearTimeout(this.intervalId);
this.intervalId = null;
}
this.isMonitoring = false;
}
async emitSystemStats(win) {
emitSystemStats(win) {
try {
const pid = process.pid;
const stats = await pidusage(pid);
const uptime = (Date.now() - this.startTime) / 1000;
const metrics = app.getAppMetrics();
const currentTime = Date.now();
let totalCPU = 0;
let totalMemory = 0;
for (const metric of metrics) {
totalCPU += metric.cpu.percentCPUUsage;
totalMemory += metric.memory.workingSetSize;
}
const uptime = (currentTime - this.startTime) / 1000;
const systemResources = {
cpu: stats.cpu || 0,
memory: stats.memory || 0,
pid: pid,
cpu: totalCPU,
memory: totalMemory,
pid: process.pid,
uptime: uptime,
timestamp: new Date().toISOString()
};
win.webContents.send('main:filesync-system-resources', systemResources);
if (win && !win.isDestroyed()) {
win.webContents.send('main:filesync-system-resources', systemResources);
}
} catch (error) {
console.error('Error getting system stats:', error);
// Fallback stats if pidusage fails
// Fallback stats using process.memoryUsage()
const fallbackStats = {
cpu: 0,
memory: process.memoryUsage().rss,
@@ -59,7 +80,9 @@ class SystemMonitor {
timestamp: new Date().toISOString()
};
win.webContents.send('main:filesync-system-resources', fallbackStats);
if (win && !win.isDestroyed()) {
win.webContents.send('main:filesync-system-resources', fallbackStats);
}
}
}

View File

@@ -37,6 +37,7 @@ const registerNetworkIpc = require('./ipc/network');
const registerCollectionsIpc = require('./ipc/collection');
const registerFilesystemIpc = require('./ipc/filesystem');
const registerPreferencesIpc = require('./ipc/preferences');
const registerSystemMonitorIpc = require('./ipc/system-monitor');
const collectionWatcher = require('./app/collection-watcher');
const { loadWindowState, saveBounds, saveMaximized } = require('./utils/window');
const registerNotificationsIpc = require('./ipc/notifications');
@@ -207,9 +208,6 @@ app.on('ready', async () => {
mainWindow.webContents.send('main:app-loaded', {
isRunningInRosetta: getIsRunningInRosetta()
});
// Start system monitoring for FileSync
systemMonitor.start(mainWindow);
});
// register all ipc handlers
@@ -219,6 +217,7 @@ app.on('ready', async () => {
registerPreferencesIpc(mainWindow, collectionWatcher, lastOpenedCollections);
registerNotificationsIpc(mainWindow, collectionWatcher);
registerFilesystemIpc(mainWindow);
registerSystemMonitorIpc(mainWindow, systemMonitor);
});
// Quit the app once all windows are closed

View File

@@ -0,0 +1,35 @@
const { ipcMain } = require('electron');
const registerSystemMonitorIpc = (mainWindow, systemMonitor) => {
ipcMain.handle('renderer:start-system-monitoring', (event, intervalMs = 2000) => {
try {
systemMonitor.start(mainWindow, intervalMs);
return { success: true };
} catch (error) {
console.error('Error starting system monitoring:', error);
return { success: false, error: error.message };
}
});
ipcMain.handle('renderer:stop-system-monitoring', (event) => {
try {
systemMonitor.stop();
return { success: true };
} catch (error) {
console.error('Error stopping system monitoring:', error);
return { success: false, error: error.message };
}
});
ipcMain.handle('renderer:is-system-monitoring-active', (event) => {
try {
const isActive = systemMonitor.isRunning();
return { success: true, isActive };
} catch (error) {
console.error('Error checking system monitoring status:', error);
return { success: false, error: error.message, isActive: false };
}
});
};
module.exports = registerSystemMonitorIpc;