Skip to content

Commit 82d70f2

Browse files
committed
Add GPU VRAM widget to row mode
1 parent b686d60 commit 82d70f2

File tree

17 files changed

+256
-2
lines changed

17 files changed

+256
-2
lines changed

src/ts/backend/config/default.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ const DEFAULT_CONFIG: Config = {
5959
updateInterval: 3000,
6060
}
6161
},
62+
gpu: {
63+
enabled: false,
64+
timing: {
65+
updateInterval: 4000,
66+
},
67+
},
6268
color: {
6369
background: "#242424",
6470
featureBackground: "#333333",

src/ts/backend/config/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {Config} from "./interface";
33
import * as electron from "electron";
44
import * as path from "path";
55
import * as fs from "fs";
6+
import {gpuPresent} from "../gpu";
67

78
const CONFIG_FILE_NAME = "config.json";
89
let config: Config | undefined = undefined;
@@ -20,6 +21,13 @@ export async function loadConfig() {
2021
console.log("Unable to find or parse config file, using default");
2122
config = DEFAULT_CONFIG;
2223
}
24+
25+
if (config) {
26+
config.gpu = {
27+
...config.gpu,
28+
enabled: await gpuPresent(),
29+
}
30+
}
2331
}
2432

2533
export function getConfig(): Config {

src/ts/backend/config/interface.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ export interface Config {
5252
}
5353
}
5454
},
55+
gpu: {
56+
enabled: boolean,
57+
timing: {
58+
updateInterval: number,
59+
},
60+
}
5561
process: {
5662
timing: {
5763
updateInterval: number,

src/ts/backend/gpu.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import * as si from "systeminformation";
2+
import {GpuUsageUpdate} from "../data/gpu";
3+
import {ONE_MEBIBYTE} from "../ui/constants/data";
4+
5+
export async function gpuUsageUpdate(): Promise<GpuUsageUpdate> {
6+
const siGpuUsage = await si.graphics();
7+
const nvidiaGpus = siGpuUsage.controllers
8+
.filter(controller => controller.memoryUsed !== undefined);
9+
if (nvidiaGpus.length === 0) {
10+
return {
11+
vramUsageBytes: 0,
12+
vramCapacityBytes: 1,
13+
gpuUsage: 0,
14+
}
15+
}
16+
const gpu = nvidiaGpus[0];
17+
return {
18+
vramUsageBytes: (gpu.memoryUsed ?? 0) * ONE_MEBIBYTE,
19+
vramCapacityBytes: (gpu.memoryTotal ?? 1) * ONE_MEBIBYTE,
20+
gpuUsage: gpu.utilizationGpu ?? 0,
21+
}
22+
}
23+
24+
export async function gpuPresent(): Promise<boolean> {
25+
const siGpuUsage = await si.graphics();
26+
const nvidiaGpus = siGpuUsage.controllers
27+
.filter(controller => controller.memoryUsed !== undefined);
28+
return nvidiaGpus.length > 0;
29+
}

src/ts/backend/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {networkAdapter, networkTransferUpdate, pingUpdate, webUpdate} from "./ne
55
import {partitionInfo} from "./disk";
66
import {getConfig, updateConfig} from "./config";
77
import {getAllProcessInfo} from "./process/process";
8+
import {gpuUsageUpdate} from "./gpu";
89

910
export default class NodeBackend implements Backend {
1011

@@ -25,6 +26,8 @@ export default class NodeBackend implements Backend {
2526

2627
processInfo = getAllProcessInfo;
2728

29+
gpuUsageUpdate = gpuUsageUpdate;
30+
2831
getConfig = async () => getConfig();
2932
updateConfig = updateConfig;
3033
}

src/ts/backend/server/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@ export default function registerBackend(
2727

2828
addRoute(ROUTES.DISK_PARTITION_INFO, backend.partitionInfo);
2929

30+
addRoute(ROUTES.GPU_USAGE, backend.gpuUsageUpdate);
31+
3032
}

src/ts/data/backend.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {NetworkTransferUpdate, PingUpdate, WebUpdate} from "./network";
44
import {MemoryInfo, MemoryUsageUpdate} from "./memory";
55
import {Config} from "../backend/config/interface";
66
import {ProcessUsageInfo} from "./process";
7+
import {GpuUsageUpdate} from "./gpu";
78

89
export interface Backend {
910

@@ -24,6 +25,8 @@ export interface Backend {
2425

2526
partitionInfo(): Promise<PartitionInfo[]>;
2627

28+
gpuUsageUpdate(): Promise<GpuUsageUpdate>;
29+
2730
getConfig(): Promise<Config>;
2831
updateConfig(config: Config): Promise<void>;
2932

src/ts/data/gpu.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export interface GpuUsageUpdate {
2+
vramUsageBytes: number;
3+
vramCapacityBytes: number;
4+
gpuUsage: number;
5+
}

src/ts/data/routes.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ const ROUTES = {
1212
NETWORK_PING: "/network/ping",
1313
NETWORK_WEB: "/network/web",
1414
PROCESS_INFO: "/process/info",
15-
DISK_PARTITION_INFO: "/disk/partition/info"
15+
DISK_PARTITION_INFO: "/disk/partition/info",
16+
GPU_USAGE: "/gpu/usage",
1617
}
1718

1819
export default ROUTES;

src/ts/ui/gpu/index.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import Widget from "../layout/widget";
2+
import {Size} from "../../util/vec2";
3+
import * as React from "react";
4+
import {GpuVRamBars} from "./vrambars";
5+
import {GpuGraphLabel} from "./label";
6+
7+
export class GpuUsageWidget extends Widget {
8+
renderContent(pixelSize: Size): React.ReactNode {
9+
return (
10+
<div className={"memory-usage"}>
11+
<GpuGraphLabel />
12+
<svg
13+
viewBox={`0 0 ${pixelSize.width} ${pixelSize.height}`}
14+
className={'memory-usage-graph full'}
15+
preserveAspectRatio="xMidYMid meet"
16+
>
17+
<GpuVRamBars position={{x: 0, y: 10}} size={{width: pixelSize.width, height: pixelSize.height - 20}} barSize={2} barSpacing={10} />
18+
</svg>
19+
</div>
20+
);
21+
}
22+
}

0 commit comments

Comments
 (0)