fix: high cpu usage (#401)

This commit is contained in:
Louis 2023-10-19 17:42:32 +07:00 committed by GitHub
parent 81823f23f0
commit 1f3521822d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 30 deletions

View File

@ -2,6 +2,6 @@ import { atom } from "jotai";
export const systemBarVisibilityAtom = atom<boolean>(true); export const systemBarVisibilityAtom = atom<boolean>(true);
export const getSystemBarVisibilityAtom = atom((get) => export const getSystemBarVisibilityAtom = atom((get) => get(systemBarVisibilityAtom));
get(systemBarVisibilityAtom)
); export const totalRamAtom = atom<number>(0);

View File

@ -1,17 +1,13 @@
import { executeSerial } from "@/_services/pluginService";
import { SystemMonitoringService } from "@janhq/core";
import { ModelVersion } from "@/_models/ModelVersion"; import { ModelVersion } from "@/_models/ModelVersion";
import { useState } from "react"; import { useState } from "react";
import { useAtomValue } from "jotai";
import { totalRamAtom } from "@/_helpers/atoms/SystemBar.atom";
export default function useGetMostSuitableModelVersion() { export default function useGetMostSuitableModelVersion() {
const [suitableModel, setSuitableModel] = useState<ModelVersion | undefined>(); const [suitableModel, setSuitableModel] = useState<ModelVersion | undefined>();
const totalRam = useAtomValue(totalRamAtom);
const getMostSuitableModelVersion = async (modelVersions: ModelVersion[]) => { const getMostSuitableModelVersion = async (modelVersions: ModelVersion[]) => {
const resourceInfo = await executeSerial(
SystemMonitoringService.GetResourcesInfo
);
const totalRam = resourceInfo.mem.total;
// find the model version with the highest required RAM that is still below the user's RAM by 80% // find the model version with the highest required RAM that is still below the user's RAM by 80%
const modelVersion = modelVersions.reduce((prev, current) => { const modelVersion = modelVersions.reduce((prev, current) => {
if (current.maxRamRequired > prev.maxRamRequired) { if (current.maxRamRequired > prev.maxRamRequired) {

View File

@ -1,8 +1,8 @@
import { executeSerial } from "../../../electron/core/plugin-manager/execution/extension-manager";
import { SystemMonitoringService } from "@janhq/core";
import { useState } from "react"; import { useState } from "react";
import { ModelVersion } from "@/_models/ModelVersion"; import { ModelVersion } from "@/_models/ModelVersion";
import { ModelPerformance, TagType } from "@/_components/SimpleTag/TagType"; import { ModelPerformance, TagType } from "@/_components/SimpleTag/TagType";
import { useAtomValue } from "jotai";
import { totalRamAtom } from "@/_helpers/atoms/SystemBar.atom";
// Recommendation: // Recommendation:
// `Recommended (green)`: "Max RAM required" is 80% of users max RAM. // `Recommended (green)`: "Max RAM required" is 80% of users max RAM.
@ -11,12 +11,9 @@ import { ModelPerformance, TagType } from "@/_components/SimpleTag/TagType";
export default function useGetPerformanceTag() { export default function useGetPerformanceTag() {
const [performanceTag, setPerformanceTag] = useState<TagType | undefined>(); const [performanceTag, setPerformanceTag] = useState<TagType | undefined>();
const totalRam = useAtomValue(totalRamAtom);
const getPerformanceForModel = async (modelVersion: ModelVersion) => { const getPerformanceForModel = async (modelVersion: ModelVersion) => {
const resourceInfo = await executeSerial(
SystemMonitoringService.GetResourcesInfo
);
const totalRam = resourceInfo.mem.total;
const requiredRam = modelVersion.maxRamRequired; const requiredRam = modelVersion.maxRamRequired;
setPerformanceTag(calculateRamPerformance(requiredRam, totalRam)); setPerformanceTag(calculateRamPerformance(requiredRam, totalRam));
}; };
@ -37,10 +34,7 @@ export default function useGetPerformanceTag() {
return { performanceTag, title, getPerformanceForModel }; return { performanceTag, title, getPerformanceForModel };
} }
const calculateRamPerformance = ( const calculateRamPerformance = (requiredRamAmt: number, totalRamAmt: number) => {
requiredRamAmt: number,
totalRamAmt: number
) => {
const percentage = requiredRamAmt / totalRamAmt; const percentage = requiredRamAmt / totalRamAmt;
if (percentage < 0.8) { if (percentage < 0.8) {

View File

@ -1,20 +1,19 @@
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { executeSerial } from "../../../electron/core/plugin-manager/execution/extension-manager"; import { executeSerial } from "../../../electron/core/plugin-manager/execution/extension-manager";
import { SystemMonitoringService } from "@janhq/core"; import { SystemMonitoringService } from "@janhq/core";
import { useSetAtom } from "jotai";
import { totalRamAtom } from "@/_helpers/atoms/SystemBar.atom";
export default function useGetSystemResources() { export default function useGetSystemResources() {
const [ram, setRam] = useState<number>(0); const [ram, setRam] = useState<number>(0);
const [cpu, setCPU] = useState<number>(0); const [cpu, setCPU] = useState<number>(0);
const setTotalRam = useSetAtom(totalRamAtom);
const getSystemResources = async () => { const getSystemResources = async () => {
const resourceInfor = await executeSerial( const resourceInfor = await executeSerial(SystemMonitoringService.GetResourcesInfo);
SystemMonitoringService.GetResourcesInfo const currentLoadInfor = await executeSerial(SystemMonitoringService.GetCurrentLoad);
); const ram = (resourceInfor?.mem?.active ?? 0) / (resourceInfor?.mem?.total ?? 1);
const currentLoadInfor = await executeSerial( if (resourceInfor?.mem?.total) setTotalRam(resourceInfor.mem.total);
SystemMonitoringService.GetCurrentLoad
);
const ram =
(resourceInfor?.mem?.active ?? 0) / (resourceInfor?.mem?.total ?? 1);
setRam(Math.round(ram * 100)); setRam(Math.round(ram * 100));
setCPU(Math.round(currentLoadInfor?.currentLoad ?? 0)); setCPU(Math.round(currentLoadInfor?.currentLoad ?? 0));
}; };
@ -25,7 +24,7 @@ export default function useGetSystemResources() {
// Fetch interval - every 3s // Fetch interval - every 3s
const intervalId = setInterval(() => { const intervalId = setInterval(() => {
getSystemResources(); getSystemResources();
}, 3000); }, 5000);
// clean up // clean up
return () => clearInterval(intervalId); return () => clearInterval(intervalId);