Skip to content

Commit

Permalink
new: gpu status (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
lollipopkit committed Nov 27, 2023
1 parent ac94b33 commit 05de760
Show file tree
Hide file tree
Showing 11 changed files with 1,126 additions and 65 deletions.
14 changes: 10 additions & 4 deletions lib/data/model/app/shell_func.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ enum ShellFunc {

/// Issue #168
/// Use `sh` for compatibility
static final installShellCmd = """
static final installShellCmd =
"""
mkdir -p $_homeVar/$_srvBoxDir
cat << 'EOF' > $_installShellPath
${ShellFunc.allScript}
Expand Down Expand Up @@ -134,7 +135,8 @@ fi''';

static final String allScript = () {
final sb = StringBuffer();
sb.write('''
sb.write(
'''
#!/bin/sh
# Script for ServerBox app v1.0.${BuildData.build}
# DO NOT delete this file while app is running
Expand All @@ -153,7 +155,8 @@ userId=\$(id -u)
''');
// Write each func
for (final func in values) {
sb.write('''
sb.write(
'''
${func.name}() {
${func._cmd.split('\n').map((e) => '\t$e').join('\n')}
}
Expand Down Expand Up @@ -199,7 +202,9 @@ enum StatusCmdType {
tempType,
tempVal,
host,
diskio;
diskio,
nvdia,
;
}

/// Cmds for linux server
Expand All @@ -217,6 +222,7 @@ const _statusCmds = [
'cat /sys/class/thermal/thermal_zone*/temp',
'hostname',
'cat /proc/diskstats',
'nvidia-smi -q -x',
];

enum DockerCmdType {
Expand Down
132 changes: 132 additions & 0 deletions lib/data/model/server/nvdia.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import 'package:xml/xml.dart';

/// [
/// {
/// "name": "GeForce RTX 3090",
/// "temp": 40,
/// "power": "30W / 350W",
/// "memory": {
/// "total": 24268,
/// "used": 240,
/// "unit": "MiB",
/// "processes": [
/// {
/// "pid": 1456,
/// "name": "/usr/lib/xorg/Xorg",
/// "memory": 40
/// },
/// ]
/// },
/// }
/// ]
///
class NvdiaSmi {
static List<NvdiaSmiItem> fromXml(String raw) {
final xmlData = XmlDocument.parse(raw);
final gpus = xmlData.findAllElements('gpu');
final result = List<NvdiaSmiItem?>.generate(gpus.length, (index) {
final gpu = gpus.elementAt(index);
final name = gpu.findElements('product_name').firstOrNull?.innerText;
final temp = gpu
.findElements('temperature')
.firstOrNull
?.findElements('gpu_temp')
.firstOrNull
?.innerText;
final power = gpu.findElements('gpu_power_readings').firstOrNull;
final powerDraw =
power?.findElements('power_draw').firstOrNull?.innerText;
final powerLimit =
power?.findElements('current_power_limit').firstOrNull?.innerText;
final memory = gpu.findElements('fb_memory_usage').firstOrNull;
final memoryUsed = memory?.findElements('used').firstOrNull?.innerText;
final memoryTotal = memory?.findElements('total').firstOrNull?.innerText;
final processes = gpu
.findElements('processes')
.firstOrNull
?.findElements('process_info');
final memoryProcesses =
List<NvdiaSmiMemProcess?>.generate(processes?.length ?? 0, (index) {
final process = processes?.elementAt(index);
final pid = process?.findElements('pid').firstOrNull?.innerText;
final name =
process?.findElements('process_name').firstOrNull?.innerText;
final memory =
process?.findElements('used_memory').firstOrNull?.innerText;
if (pid != null && name != null && memory != null) {
return NvdiaSmiMemProcess(
int.parse(pid),
name,
int.parse(
memory.split(' ').firstOrNull ?? '0',
),
);
}
return null;
});
memoryProcesses.removeWhere((element) => element == null);
if (name != null &&
temp != null &&
powerDraw != null &&
powerLimit != null &&
memory != null) {
return NvdiaSmiItem(
name,
int.parse(temp.split(' ').firstOrNull ?? '0'),
'$powerDraw / $powerLimit',
NvdiaSmiMem(
int.parse(memoryTotal?.split(' ').firstOrNull ?? '0'),
int.parse(memoryUsed?.split(' ').firstOrNull ?? '0'),
'MiB',
List.from(memoryProcesses),
),
);
}
return null;
});
result.removeWhere((element) => element == null);
return List.from(result);
}
}

class NvdiaSmiItem {
final String name;
final int temp;
final String power;
final NvdiaSmiMem memory;

const NvdiaSmiItem(this.name, this.temp, this.power, this.memory);

@override
String toString() {
return 'NvdiaSmiItem{name: $name, temp: $temp, power: $power, memory: $memory}';
}
}

class NvdiaSmiMem {
final int total;
final int used;
final String unit;
final List<NvdiaSmiMemProcess> processes;

const NvdiaSmiMem(this.total, this.used, this.unit, this.processes);

@override
String toString() {
return 'NvdiaSmiMem{total: $total, used: $used, unit: $unit, processes: $processes}';
}
}

class NvdiaSmiMemProcess {
final int pid;
final String name;
final int memory;

const NvdiaSmiMemProcess(this.pid, this.name, this.memory);

@override
String toString() {
return 'NvdiaSmiMemProcess{pid: $pid, name: $name, memory: $memory}';
}
}
3 changes: 3 additions & 0 deletions lib/data/model/server/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:toolbox/data/model/server/cpu.dart';
import 'package:toolbox/data/model/server/disk.dart';
import 'package:toolbox/data/model/server/memory.dart';
import 'package:toolbox/data/model/server/net_speed.dart';
import 'package:toolbox/data/model/server/nvdia.dart';
import 'package:toolbox/data/model/server/server_private_info.dart';
import 'package:toolbox/data/model/server/system.dart';
import 'package:toolbox/data/model/server/temp.dart';
Expand Down Expand Up @@ -55,6 +56,7 @@ class ServerStatus {
SystemType system;
String? err;
DiskIO diskIO;
List<NvdiaSmiItem>? nvdia;

/// Whether is connectting, parsing and etc.
bool _isBusy = false;
Expand All @@ -72,6 +74,7 @@ class ServerStatus {
required this.system,
required this.diskIO,
this.err,
this.nvdia,
});
}

Expand Down
8 changes: 8 additions & 0 deletions lib/data/model/server/server_status_update_req.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:toolbox/data/model/server/nvdia.dart';
import 'package:toolbox/data/model/server/server.dart';
import 'package:toolbox/data/model/server/system.dart';
import 'package:toolbox/data/res/logger.dart';
Expand Down Expand Up @@ -110,6 +111,13 @@ Future<ServerStatus> _getLinuxStatus(ServerStatusUpdateReq req) async {
} catch (e, s) {
Loggers.parse.warning(e, s);
}

try {
final nvdia = NvdiaSmi.fromXml(StatusCmdType.nvdia.find(segments));
req.ss.nvdia = nvdia;
} catch (e, s) {
Loggers.parse.warning(e, s);
}
return req.ss;
}

Expand Down
1 change: 1 addition & 0 deletions lib/data/res/default.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Defaults {
'cpu',
'mem',
'swap',
'gpu',
'disk',
'net',
'temp'
Expand Down
111 changes: 111 additions & 0 deletions lib/view/page/server/detail.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:toolbox/core/extension/context/common.dart';
import 'package:toolbox/core/extension/context/dialog.dart';
import 'package:toolbox/core/extension/context/locale.dart';
import 'package:toolbox/core/extension/order.dart';
import 'package:toolbox/data/model/server/cpu.dart';
import 'package:toolbox/data/model/server/disk.dart';
import 'package:toolbox/data/model/server/net_speed.dart';
import 'package:toolbox/data/model/server/nvdia.dart';
import 'package:toolbox/data/model/server/server_private_info.dart';
import 'package:toolbox/data/model/server/system.dart';
import 'package:toolbox/data/res/store.dart';
Expand Down Expand Up @@ -45,6 +47,7 @@ class _ServerDetailPageState extends State<ServerDetailPage>
_buildCPUView,
_buildMemView,
_buildSwapView,
_buildGpuView,
_buildDiskView,
_buildNetView,
_buildTemperature,
Expand Down Expand Up @@ -302,6 +305,114 @@ class _ServerDetailPageState extends State<ServerDetailPage>
);
}

Widget _buildGpuView(ServerStatus ss) {
if (ss.nvdia == null) return UIs.placeholder;
final children = ss.nvdia!.map((e) => _buildGpuItem(e)).toList();
return CardX(
ExpandTile(
title: const Text('GPU'),
leading: const Icon(Icons.memory, size: 17),
initiallyExpanded: children.length <= 3,
children: children,
),
);
}

Widget _buildGpuItem(NvdiaSmiItem item) {
final mem = item.memory;
final processes = mem.processes;
final children = <Widget>[];
if (processes.isNotEmpty) {
children.addAll(processes.map((e) => _buildGpuProcessItem(e)));
}
return CardX(
ListTile(
title: Text(item.name, style: UIs.textSize13),
leading: Text(
'${item.temp}°C',
style: UIs.textSize11Grey,
textScaler: _textFactor,
),
subtitle: Text(
'${item.power} - ${mem.used} / ${mem.total} ${mem.unit}',
style: UIs.textSize11Grey,
textScaler: _textFactor,
),
trailing: InkWell(
onTap: () {
final height = () {
if (processes.length > 5) {
return 5 * 47.0;
}
return processes.length * 47.0;
}();
context.showRoundDialog(
title: Text(item.name),
child: SizedBox(
width: double.maxFinite,
height: height,
child: ListView.builder(
itemCount: processes.length,
itemBuilder: (_, idx) => _buildGpuProcessItem(processes[idx]),
),
),
actions: [
TextButton(
onPressed: () => context.pop(),
child: Text(l10n.close),
)
],
);
},
child: const Icon(Icons.info_outline, size: 17),
),
),
);
}

Widget _buildGpuProcessItem(NvdiaSmiMemProcess process) {
return ListTile(
title: Text(
process.name,
style: UIs.textSize11,
maxLines: 1,
overflow: TextOverflow.ellipsis,
textScaler: _textFactor,
),
subtitle: Text(
'PID: ${process.pid} - ${process.memory} MiB',
style: UIs.textSize11Grey,
textScaler: _textFactor,
),
trailing: InkWell(
onTap: () {
context.showRoundDialog(
title: SizedBox(
width: 377,
child: Text('${process.pid}', maxLines: 1),
),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Memory: ${process.memory} MiB'),
UIs.height13,
Text('Process: ${process.name}')
],
),
actions: [
TextButton(
onPressed: () => context.pop(),
child: Text(l10n.close),
)
],
);
},
child: const Icon(Icons.info_outline, size: 17),
),
);
}

Widget _buildDiskView(ServerStatus ss) {
final disks = ss.disk;
disks.removeWhere((e) {
Expand Down
10 changes: 5 additions & 5 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -698,10 +698,10 @@ packages:
dependency: transitive
description:
name: petitparser
sha256: cb3798bef7fc021ac45b308f4b51208a152792445cce0448c9a4ba5879dd8750
sha256: eeb2d1428ee7f4170e2bd498827296a18d4e7fc462b71727d111c0ac7707cfa6
url: "https://pub.dev"
source: hosted
version: "5.4.0"
version: "6.0.1"
pinenacl:
dependency: transitive
description:
Expand Down Expand Up @@ -1125,13 +1125,13 @@ packages:
source: hosted
version: "1.0.3"
xml:
dependency: transitive
dependency: "direct main"
description:
name: xml
sha256: "5bc72e1e45e941d825fd7468b9b4cc3b9327942649aeb6fc5cdbf135f0a86e84"
sha256: af5e77e9b83f2f4adc5d3f0a4ece1c7f45a2467b695c2540381bac793e34e556
url: "https://pub.dev"
source: hosted
version: "6.3.0"
version: "6.4.2"
xterm:
dependency: "direct main"
description:
Expand Down
Loading

0 comments on commit 05de760

Please sign in to comment.