Skip to content

Commit

Permalink
Switch from systeminformation to os-utils to resolve bitdefender anti…
Browse files Browse the repository at this point in the history
…virus on windows, and reduce memory leak for monitor extension (janhq#1282)

Co-authored-by: Hien To <[email protected]>
  • Loading branch information
hiento09 and hiento09 authored Jan 2, 2024
1 parent 843d1b4 commit 45fdadf
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 29 deletions.
4 changes: 2 additions & 2 deletions extensions/inference-nitro-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
"@janhq/core": "file:../../core",
"download-cli": "^1.1.1",
"fetch-retry": "^5.0.6",
"os-utils": "^0.0.14",
"path-browserify": "^1.0.1",
"rxjs": "^7.8.1",
"systeminformation": "^5.21.20",
"tcp-port-used": "^1.0.2",
"ts-loader": "^9.5.0",
"ulid": "^2.3.0"
Expand All @@ -50,6 +50,6 @@
"bundleDependencies": [
"tcp-port-used",
"fetch-retry",
"systeminformation"
"os-utils"
]
}
9 changes: 4 additions & 5 deletions extensions/inference-nitro-extension/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const path = require("path");
const { exec, spawn } = require("child_process");
const tcpPortUsed = require("tcp-port-used");
const fetchRetry = require("fetch-retry")(global.fetch);
const si = require("systeminformation");
const osUtils = require("os-utils");
const { readFileSync, writeFileSync, existsSync } = require("fs");

// The PORT to use for the Nitro subprocess
Expand Down Expand Up @@ -440,11 +440,10 @@ function spawnNitroProcess(nitroResourceProbe: any): Promise<any> {
*/
function getResourcesInfo(): Promise<ResourcesInfo> {
return new Promise(async (resolve) => {
const cpu = await si.cpu();
// const mem = await si.mem();

const cpu = await osUtils.cpuCount();
console.log("cpu: ", cpu);
const response: ResourcesInfo = {
numCpuPhysicalCore: cpu.physicalCores,
numCpuPhysicalCore: cpu,
memAvailable: 0,
};
resolve(response);
Expand Down
4 changes: 2 additions & 2 deletions extensions/monitoring-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"dependencies": {
"@janhq/core": "file:../../core",
"systeminformation": "^5.21.8",
"os-utils": "^0.0.14",
"ts-loader": "^9.5.0"
},
"files": [
Expand All @@ -26,6 +26,6 @@
"README.md"
],
"bundleDependencies": [
"systeminformation"
"os-utils"
]
}
36 changes: 23 additions & 13 deletions extensions/monitoring-extension/src/module.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
const si = require("systeminformation");
const os = require("os");
const osUtils = require("os-utils");

const getResourcesInfo = () =>
new Promise((resolve) => {
const totalMemory = os.totalmem();
const freeMemory = os.freemem();
const usedMemory = totalMemory - freeMemory;

const getResourcesInfo = async () =>
new Promise(async (resolve) => {
const cpu = await si.cpu();
const mem = await si.mem();
// const gpu = await si.graphics();
const response = {
cpu,
mem,
// gpu,
mem: {
totalMemory,
usedMemory,
},
};
resolve(response);
});

const getCurrentLoad = async () =>
new Promise(async (resolve) => {
const currentLoad = await si.currentLoad();
resolve(currentLoad);
const getCurrentLoad = () =>
new Promise((resolve) => {
osUtils.cpuUsage(function(v){
const cpuPercentage = v * 100;
const response = {
cpu: {
usage: cpuPercentage,
},
};
resolve(response);
});
});

module.exports = {
Expand Down
16 changes: 9 additions & 7 deletions web/hooks/useGetSystemResources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,26 @@ export default function useGetSystemResources() {
const currentLoadInfor = await monitoring?.getCurrentLoad()

const ram =
(resourceInfor?.mem?.active ?? 0) / (resourceInfor?.mem?.total ?? 1)
if (resourceInfor?.mem?.active) setUsedRam(resourceInfor.mem.active)
if (resourceInfor?.mem?.total) setTotalRam(resourceInfor.mem.total)
(resourceInfor?.mem?.usedMemory ?? 0) /
(resourceInfor?.mem?.totalMemory ?? 1)
if (resourceInfor?.mem?.usedMemory) setUsedRam(resourceInfor.mem.usedMemory)
if (resourceInfor?.mem?.totalMemory)
setTotalRam(resourceInfor.mem.totalMemory)

setRam(Math.round(ram * 100))
setCPU(Math.round(currentLoadInfor?.currentLoad ?? 0))
setCpuUsage(Math.round(currentLoadInfor?.currentLoad ?? 0))
setCPU(Math.round(currentLoadInfor?.cpu?.usage ?? 0))
setCpuUsage(Math.round(currentLoadInfor?.cpu?.usage ?? 0))
}

useEffect(() => {
getSystemResources()

// Fetch interval - every 5s
// Fetch interval - every 2s
// TODO: Will we really need this?
// There is a possibility that this will be removed and replaced by the process event hook?
const intervalId = setInterval(() => {
getSystemResources()
}, 5000)
}, 2000)

// clean up interval
return () => clearInterval(intervalId)
Expand Down

0 comments on commit 45fdadf

Please sign in to comment.