Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use websocket (nchan) instead of ajax polling to refresh data in dashboard #28

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 30 additions & 19 deletions src/gpustat/usr/local/emhttp/plugins/gpustat/GPUStatSettings.page
Original file line number Diff line number Diff line change
Expand Up @@ -64,44 +64,55 @@ Title="GPU Statistics"
$json_array[$pciid]['vendor'] = $gpus[$gpupciid]['vendor'] ;
$json_array[$pciid]['guid'] = $gpus[$gpupciid]['guid'] ;
}
} else {
} else {
$json_array[$pciid]['id'] = "None";
$json_array[$pciid]['model'] = "None";
$json_array[$pciid]['vendor'] = "None";
$json_array[$pciid]['guid'] = "None";
}
$val["MULTIGPUJSON"] = urlencode(json_encode($json_array)) ;
if (isset($val['MULTIGPU'])) {
if (isset($val['MULTIGPU'])) {
if($val['MULTIGPU'] != "") $val['MULTIGPU'] = implode("," , $val['MULTIGPU']) ;
} else $val['MULTIGPU'] = "" ;
}

save_ini_file("gpustat.cfg", $val) ;
}

$cfg = $gpustat_cfg = parse_plugin_cfg("gpustat", true);
$gpustat_inventory = true;
$cfg = $gpustat_cfg = parse_plugin_cfg("gpustat", true);
$multi_enable = version_compare(parse_ini_file('/etc/unraid-version')['version'], '6.12.0-beta5', '>');

const ES = ' ';

require_once $docroot . '/plugins/gpustat/lib/Main.php';
require_once $docroot . '/plugins/gpustat/lib/Nvidia.php';
require_once $docroot . '/plugins/gpustat/lib/Intel.php';
require_once $docroot . '/plugins/gpustat/lib/AMD.php';
require_once $docroot . '/plugins/gpustat/lib/Error.php';

$multi_enable=version_compare(parse_ini_file('/etc/unraid-version')['version'],'6.12.0-beta5', '>') ;
use gpustat\lib\AMD;
use gpustat\lib\Nvidia;
use gpustat\lib\Intel;
use gpustat\lib\Error;

// Settings page looks for $gpustat_data specifically -- inventory all supported GPU types
if ($multi_enable) {
include_once './plugins/gpustat/gpustatusmulti.php';
$gpustat_data = array_merge((new Nvidia($gpustat_cfg))->getInventorym(), (new Intel($gpustat_cfg))->getInventory(), (new AMD($gpustat_cfg))->getInventorym());

// Test data for multigpu
// $gpustat_data = array_merge($gpustat_data, json_decode('{"0000:00:02.0": {"id": "00:02.0","model": "AlderLake-S GT1","vendor": "intel","guid": "GPU-94118c02-132c-45bb-a114-df6f24902d5d"},"0000:09:00.0": {"id": "09:00.0","model": "DG2 [Arc A770]","vendor": "intel","guid": "GPU-b13f472d-a4a7-4914-a59e-9b73c4856259"},"0000:08:00.0": {"id": "08:00.0","model": "Quadro K4000","vendor": "nvidia","guid": "GPU-ef6c0299-f1bc-7b5c-5291-7cd1a012f8bd"},"0000:0c:00.0": {"id": "0c:00.0","model": "Radeon RX 6400\/6500 XT\/6500M","vendor": "amd","guid": "GPU-639cd727-f368-4fe0-aff3-947542489448"}}', true));
} else {
include_once './plugins/gpustat/gpustatus.php';
$gpustat_data = array_merge((new Nvidia($gpustat_cfg))->getInventory(), (new Intel($gpustat_cfg))->getInventory(), (new AMD($gpustat_cfg))->getInventory());
}



$multi_enable=version_compare(parse_ini_file('/etc/unraid-version')['version'],'6.12.0-beta5', '>') ;

if (count($_POST)) {
$cfg = NULL ;
if ($_POST['#apply'] == "_(Apply)_") {
array_map(function($k, $v) use (&$cfg) { if($k[0] != "#") $cfg[$k] = $v; }, array_keys($_POST), $_POST );
save_cfg($cfg,$gpustat_data) ;
$gpustat_cfg = parse_plugin_cfg("gpustat", true);
unset($_POST) ;
}
save_cfg($cfg,$gpustat_data);
$gpustat_cfg = parse_plugin_cfg("gpustat", true);
unset($_POST);
}
}

?>
Expand Down Expand Up @@ -142,8 +153,8 @@ Unit ID for Dashboard:
<?php if ($multi_enable) : ?>
Unit ID for Dashboard(Multiple):
: <select id="MULTIGPU" name="MULTIGPU[]" multiple="multiple" style="display:none">
<?php

<?php
if (isset($gpustat_data)) {
ksort($gpustat_data) ;
foreach ($gpustat_data AS $gpu) {
Expand Down Expand Up @@ -273,7 +284,7 @@ Power Draw Selection:
<?=mk_option($gpustat_cfg['DISPPWRDRWSEL'], "GPU", "GPU");?>
<?=mk_option($gpustat_cfg['DISPPWRDRWSEL'], "PACKAGE", "Package");?>
<?=mk_option($gpustat_cfg['DISPPWRDRWSEL'], "MAX", "Max of GPU or Package");?>
</select>
</select>


<h4><b>AMD Specific</b></h4>
Expand Down
12 changes: 8 additions & 4 deletions src/gpustat/usr/local/emhttp/plugins/gpustat/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,21 @@
margin-top: 0;
}

span.gpu {
.gpustat span.gpu {
width: 150px;
display: inline-block;
}

span.gpu-img-span {
display: none;
.gpustat span[class*="gpu-throttled"] {
margin-right: 5px;
}

.gpu-image {
.gpustat .gpu-image {
padding-left: 8px;
height: 32px;
width: 32px;
}

.gpustat .usage-disk > span {
transition: width 0.5s;
}
31 changes: 13 additions & 18 deletions src/gpustat/usr/local/emhttp/plugins/gpustat/gpustatus.page
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ Cond="version_compare(parse_ini_file('/etc/unraid-version')['version'],'6.12.0-b

$gpu_nv = $gpu_intel = $gpu_amd = $gpu_unknown = false;

$apps = [
'plex', 'jellyfin', 'handbrake', 'emby', 'tdarr', 'unmanic', 'dizquetv', 'ersatztv',
'fileflows', 'frigate', 'deepstack', 'nsfminer', 'shinobipro', 'foldinghome', 'compreface',
];

switch ($gpustat_cfg['VENDOR']) {
case "nvidia":
$gpu_nv = true;
Expand All @@ -59,7 +54,7 @@ Cond="version_compare(parse_ini_file('/etc/unraid-version')['version'],'6.12.0-b

<table id='db-box1' class='dash_gpustat dashboard box1' style='display:none'>
<thead sort='953'><tr class='hidden'><td></td><td colspan='3'></td><td></td></tr></thead>
<tbody sort='953' class='sortable'>
<tbody sort='953' class='gpustat sortable'>
<tr>
<td></td>
<td class='next' colspan='3'>
Expand Down Expand Up @@ -164,13 +159,7 @@ Cond="version_compare(parse_ini_file('/etc/unraid-version')['version'],'6.12.0-b
<tr class="dash_gpustat_toggle gpu-enviro">
<td></td>
<td>Active Apps</td>
<td colspan="2">
<?php foreach($apps as $app): ?>
<span id="gpu-img-span-<?= $app; ?>" class='gpu-img-span gpu-img-span-<?= $app; ?>'>
<img id='gpu-<?= $app; ?>' class='gpu-image' src="/plugins/gpustat/images/<?= $app; ?>.png">
</span>
<?php endforeach; ?>
</td>
<td colspan="2" class="gpu-active-apps"></td>
<td></td>
</tr>
<?php endif; ?>
Expand Down Expand Up @@ -354,9 +343,15 @@ Cond="version_compare(parse_ini_file('/etc/unraid-version')['version'],'6.12.0-b
<?php endif; ?>
<script type="text/javascript" src="/plugins/gpustat/scripts/gpustat.js"></script>
<script type="text/javascript">
$(gpustat_status);
if (<?=$gpustat_cfg['UIREFRESH'];?>) {
setInterval(gpustat_status, <?=max(abs($display['refresh']), $gpustat_cfg['UIREFRESHINT']);?>);
}
$(gpustat_dash);
$(gpustat_status);
if (<?=$gpustat_cfg['UIREFRESH'];?>) {
setInterval(gpustat_status, <?=max(abs($display['refresh']), $gpustat_cfg['UIREFRESHINT']);?>);
}
// Add Nchan="gpustat" to this .page header to enable Nchan
// const gpustat_ws = new NchanSubscriber('/sub/gpustat');
// gpustat_ws.on('message', (data) => {
// parseStats(JSON.parse(data));
// });
// gpustat_ws.start();
$(gpustat_dash);
</script>
48 changes: 18 additions & 30 deletions src/gpustat/usr/local/emhttp/plugins/gpustat/gpustatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,35 +38,23 @@
use gpustat\lib\Intel;
use gpustat\lib\Error;

if (!isset($gpustat_cfg)) {
$gpustat_cfg = Main::getSettings();
$gpustat_cfg = Main::getSettings();

switch ($gpustat_cfg['VENDOR']) {
case 'amd':
$json = (new AMD($gpustat_cfg))->getStatistics();
break;
case 'intel':
$json = (new Intel($gpustat_cfg))->getStatistics();
break;
case 'nvidia':
$json = (new Nvidia($gpustat_cfg))->getStatistics();
break;
default:
print_r(Error::get(Error::CONFIG_SETTINGS_NOT_VALID));
}

// $gpustat_inventory should be set if called from settings page code
if (isset($gpustat_inventory) && $gpustat_inventory) {
$gpustat_cfg['inventory'] = true;
// Settings page looks for $gpustat_data specifically -- inventory all supported GPU types
$gpustat_data = array_merge((new Nvidia($gpustat_cfg))->getInventory(), (new Intel($gpustat_cfg))->getInventory(), (new AMD($gpustat_cfg))->getInventory());
} else {

switch ($gpustat_cfg['VENDOR']) {
case 'amd':
$data = (new AMD($gpustat_cfg))->getStatistics();
break;
case 'intel':
$data = (new Intel($gpustat_cfg))->getStatistics();
break;
case 'nvidia':
$data = (new Nvidia($gpustat_cfg))->getStatistics();
break;
default:
print_r(Error::get(Error::CONFIG_SETTINGS_NOT_VALID));
}
$json = $data ;
header('Content-Type: application/json');
header('Content-Length:' . ES . strlen($json));
echo $json;
file_put_contents("/tmp/gpujson2","Time = ".date(DATE_RFC2822)."\n") ;
file_put_contents("/tmp/gpujson2",$json."\n",FILE_APPEND) ;

}
header('Content-Type: application/json');
header('Content-Length:' . ES . strlen($json));
echo $json;
file_put_contents("/tmp/gpujson2", "Time = " . date(DATE_RFC2822) . PHP_EOL . $json . PHP_EOL);
Loading