Files
yenai-plugin/model/State/GPU.js
2024-04-20 00:51:25 +08:00

48 lines
1.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { Circle, si, initDependence } from "./utils.js"
let isGPU = false;
(async function initGetIsGPU() {
if (!await initDependence()) return
const { controllers } = await si.graphics()
// 初始化GPU获取
if (controllers?.find(item =>
item.memoryUsed && item.memoryFree && item.utilizationGpu)
) {
isGPU = true
}
})()
/** 获取GPU占用 */
export default async function getGPU() {
if (!isGPU) return false
try {
const { controllers } = await si.graphics()
let graphics = controllers?.find(item =>
item.memoryUsed && item.memoryFree && item.utilizationGpu
)
if (!graphics) {
logger.warn("[Yenai-plugin][state]状态GPU数据异常\n", controllers)
return false
}
let {
vendor, temperatureGpu, utilizationGpu,
memoryTotal, memoryUsed /* powerDraw */
} = graphics
temperatureGpu && (temperatureGpu = temperatureGpu + "℃")
// powerDraw && (powerDraw = powerDraw + "W")
return {
...Circle(utilizationGpu / 100),
inner: Math.round(utilizationGpu) + "%",
title: "GPU",
info: [
`${(memoryUsed / 1024).toFixed(2)} GB / ${(memoryTotal / 1024).toFixed(2)} GB`,
`${vendor} ${temperatureGpu}`
]
}
} catch (e) {
logger.warn("[Yenai-Plugin][State] 获取GPU失败")
return false
}
}