From eaae7624a7ab3b2559da3440692d3d33814b824d Mon Sep 17 00:00:00 2001 From: josc146 Date: Sun, 27 Aug 2023 22:53:18 +0800 Subject: [PATCH] add HardwareMonitor (Windows Only) --- .github/workflows/release.yml | 2 ++ backend-golang/app.go | 46 ++++++++++++++++++++++++- frontend/src/_locales/ja/main.json | 5 ++- frontend/src/_locales/zh-hans/main.json | 5 ++- frontend/src/startup.ts | 22 +++++++++++- main.go | 8 +++-- 6 files changed, 82 insertions(+), 6 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 157717c..eed02d3 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -57,6 +57,8 @@ jobs: with: args: install upx - run: | + Start-BitsTransfer https://github.com/josStorer/LibreHardwareMonitor.Console/releases/download/v0.1.0/LibreHardwareMonitor.Console.zip ./LibreHardwareMonitor.Console.zip + Expand-Archive ./LibreHardwareMonitor.Console.zip -DestinationPath ./components/LibreHardwareMonitor.Console Start-BitsTransfer https://www.python.org/ftp/python/3.10.11/python-3.10.11-embed-amd64.zip ./python-3.10.11-embed-amd64.zip Expand-Archive ./python-3.10.11-embed-amd64.zip -DestinationPath ./py310 $content=Get-Content "./py310/python310._pth"; $content | ForEach-Object {if ($_.ReadCount -eq 3) {"Lib\\site-packages"} else {$_}} | Set-Content ./py310/python310._pth diff --git a/backend-golang/app.go b/backend-golang/app.go index 34c7f73..f3e8521 100644 --- a/backend-golang/app.go +++ b/backend-golang/app.go @@ -1,6 +1,7 @@ package backend_golang import ( + "bufio" "context" "errors" "net/http" @@ -8,6 +9,7 @@ import ( "os/exec" "path/filepath" "runtime" + "syscall" "github.com/fsnotify/fsnotify" "github.com/minio/selfupdate" @@ -41,6 +43,7 @@ func (a *App) OnStartup(ctx context.Context) { a.cmdPrefix = "cd " + a.exDir + " && " } + os.Chmod("./backend-rust/webgpu_server", 0777) os.Mkdir(a.exDir+"models", os.ModePerm) os.Mkdir(a.exDir+"lora-models", os.ModePerm) os.Mkdir(a.exDir+"finetune/json2binidx_tool/data", os.ModePerm) @@ -50,7 +53,18 @@ func (a *App) OnStartup(ctx context.Context) { } a.downloadLoop() + a.watchFs() + a.monitorHardware() +} +func (a *App) OnBeforeClose(ctx context.Context) bool { + if monitor != nil { + monitor.Process.Kill() + } + return false +} + +func (a *App) watchFs() { watcher, err := fsnotify.NewWatcher() if err == nil { watcher.Add("./lora-models") @@ -62,7 +76,7 @@ func (a *App) OnStartup(ctx context.Context) { if !ok { return } - wruntime.EventsEmit(ctx, "fsnotify", event.Name) + wruntime.EventsEmit(a.ctx, "fsnotify", event.Name) case _, ok := <-watcher.Errors: if !ok { return @@ -73,6 +87,36 @@ func (a *App) OnStartup(ctx context.Context) { } } +var monitor *exec.Cmd + +func (a *App) monitorHardware() { + if runtime.GOOS != "windows" { + return + } + + monitor = exec.Command("./components/LibreHardwareMonitor.Console/LibreHardwareMonitor.Console.exe") + stdout, err := monitor.StdoutPipe() + if err != nil { + monitor = nil + return + } + + go func() { + reader := bufio.NewReader(stdout) + for { + line, _, err := reader.ReadLine() + if err != nil { + wruntime.EventsEmit(a.ctx, "monitorerr", err.Error()) + break + } + wruntime.EventsEmit(a.ctx, "monitor", string(line)) + } + }() + + monitor.SysProcAttr = &syscall.SysProcAttr{HideWindow: true} + monitor.Start() +} + func (a *App) UpdateApp(url string) (broken bool, err error) { resp, err := http.Get(url) if err != nil { diff --git a/frontend/src/_locales/ja/main.json b/frontend/src/_locales/ja/main.json index 38a2329..d7bdce9 100644 --- a/frontend/src/_locales/ja/main.json +++ b/frontend/src/_locales/ja/main.json @@ -245,5 +245,8 @@ "Please convert model to safe tensors format first": "モデルを安全なテンソル形式に変換してください", "Convert To Safe Tensors Format": "安全なテンソル形式に変換", "Please change Strategy to WebGPU to use safetensors format": "StrategyをWebGPUに変更して、安全なテンソル形式を使用してください", - "Preview Only": "プレビューのみ" + "Preview Only": "プレビューのみ", + "RAM": "RAM", + "VRAM": "VRAM", + "GPU Usage": "GPU使用率" } \ No newline at end of file diff --git a/frontend/src/_locales/zh-hans/main.json b/frontend/src/_locales/zh-hans/main.json index 13cf7c1..6d9266f 100644 --- a/frontend/src/_locales/zh-hans/main.json +++ b/frontend/src/_locales/zh-hans/main.json @@ -245,5 +245,8 @@ "Please convert model to safe tensors format first": "请先将模型转换为Safetensors格式", "Convert To Safe Tensors Format": "转换为Safetensors格式", "Please change Strategy to WebGPU to use safetensors format": "请将Strategy改为WebGPU以使用safetensors格式", - "Preview Only": "仅预览" + "Preview Only": "仅预览", + "RAM": "内存", + "VRAM": "显存", + "GPU Usage": "GPU占用" } \ No newline at end of file diff --git a/frontend/src/startup.ts b/frontend/src/startup.ts index 3b08697..f65091e 100644 --- a/frontend/src/startup.ts +++ b/frontend/src/startup.ts @@ -2,11 +2,12 @@ import commonStore, { Platform } from './stores/commonStore'; import { GetPlatform, ListDirFiles, ReadJson } from '../wailsjs/go/backend_golang/App'; import { Cache, checkUpdate, downloadProgramFiles, LocalConfig, refreshLocalModels, refreshModels } from './utils'; import { getStatus } from './apis'; -import { EventsOn } from '../wailsjs/runtime'; +import { EventsOn, WindowSetTitle } from '../wailsjs/runtime'; import manifest from '../../manifest.json'; import { defaultModelConfigs, defaultModelConfigsMac } from './pages/defaultConfigs'; import { Preset } from './pages/PresetsManager/PresetsButton'; import { wslHandler } from './pages/Train'; +import { t } from 'i18next'; export async function startup() { downloadProgramFiles(); @@ -23,6 +24,8 @@ export async function startup() { initPresets(); + initHardwareMonitor(); + await GetPlatform().then(p => commonStore.setPlatform(p as Platform)); await initConfig(); @@ -117,3 +120,20 @@ async function initLocalModelsNotify() { refreshLocalModels({ models: commonStore.modelSourceList }, false); //TODO fix bug that only add models }); } + +type monitorData = { + usedMemory: number; + totalMemory: number; + gpuUsage: number; + gpuPower: number; + usedVram: number; + totalVram: number; +} + +async function initHardwareMonitor() { + EventsOn('monitor', (data: string) => { + const results: monitorData = JSON.parse(data); + if (results) + WindowSetTitle(`RWKV-Runner (${t('RAM')}: ${results.usedMemory.toFixed(1)}/${results.totalMemory.toFixed(1)} GB, ${t('VRAM')}: ${(results.usedVram / 1024).toFixed(1)}/${(results.totalVram / 1024).toFixed(1)} GB, ${t('GPU Usage')}: ${results.gpuUsage}%)`); + }); +} diff --git a/main.go b/main.go index 30dc30b..d40409b 100644 --- a/main.go +++ b/main.go @@ -61,6 +61,9 @@ var midi embed.FS //go:embed assets/sound-font var midiAssets embed.FS +//go:embed components +var components embed.FS + func main() { if buildInfo, ok := debug.ReadBuildInfo(); !ok || strings.Contains(buildInfo.String(), "-ldflags") { backend.CopyEmbed(cyac) @@ -70,8 +73,8 @@ func main() { backend.CopyEmbed(finetune) backend.CopyEmbed(midi) backend.CopyEmbed(midiAssets) + backend.CopyEmbed(components) } - os.Chmod("./backend-rust/webgpu_server", 0777) // Create an instance of the app structure app := backend.NewApp() @@ -103,7 +106,8 @@ func main() { Assets: assets, Handler: NewFileLoader(), }, - OnStartup: app.OnStartup, + OnStartup: app.OnStartup, + OnBeforeClose: app.OnBeforeClose, Bind: []any{ app, },