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 2 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
33 changes: 20 additions & 13 deletions src/gpustat/usr/local/emhttp/plugins/gpustat/GPUStatSettings.page
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,23 @@ 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);
$gpustat_inventory = true;

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

Expand All @@ -90,18 +90,25 @@ Title="GPU Statistics"
include_once './plugins/gpustat/gpustatus.php';
}



$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) ;
}

// Restart nchan gpustat if not running when refresh setting set to Yes
$pid = exec('pgrep gpustat');
if (!$pid && $_POST['UIREFRESH'] == '1' && $gpustat_cfg['UIREFRESH'] != '1') {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be started automatically when you go back to the dashboard so not sure if this is needed. There is a file in var/run called nchan.pid which contains the running nchan processes but would need to confirm the correct way to stop a process.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about it too, but monitor_nchan doesn't restart it if path to gpustat is present in /var/run/nchan.pid.
I didn't find a proper way to stop a nchan process. The only way to stop a process is by adding :stop in .page header to nchan attribut, but it's only for a single run. Process killed after 30 seconds.

I made change to remove process path in nchan.pid and stop process itself if refresh set to No. Dashboard will restart it every time if necessary.
Removed code from GPUStatSettings.page and updated here:

// Stop nchan gpustat if refresh setting set to No and remove it from /var/run/nchan.pid
if ($gpustat_cfg['UIREFRESH'] != '1') {
exec('sed -i "/plugins\/gpustat\/nchan\/gpustat/d" /var/run/nchan.pid');
exit;
}

exec('/usr/local/emhttp/plugins/gpustat/nchan/gpustat &>/dev/null &');
}

$gpustat_cfg = parse_plugin_cfg("gpustat", true);
unset($_POST);
}
}

?>
Expand Down Expand Up @@ -142,8 +149,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 +280,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
4 changes: 0 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 @@ -31,10 +31,6 @@ span.gpu {
display: inline-block;
}

span.gpu-img-span {
display: none;
}

.gpu-image {
padding-left: 8px;
height: 32px;
Expand Down
254 changes: 116 additions & 138 deletions src/gpustat/usr/local/emhttp/plugins/gpustat/gpustatusmovablemulti.page

Large diffs are not rendered by default.

48 changes: 4 additions & 44 deletions src/gpustat/usr/local/emhttp/plugins/gpustat/gpustatusmulti.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@
include 'lib/Nvidia.php';
include 'lib/Intel.php';
include 'lib/AMD.php';
include 'lib/Error.php';

use gpustat\lib\AMD;
use gpustat\lib\Main;
use gpustat\lib\Nvidia;
use gpustat\lib\Intel;
use gpustat\lib\Error;

if (!isset($gpustat_cfg)) {
$gpustat_cfg = Main::getSettings();
Expand All @@ -45,50 +43,12 @@
// $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))->getInventorym(), (new Intel($gpustat_cfg))->getInventory(), (new AMD($gpustat_cfg))->getInventorym());
file_put_contents("/tmp/gpuinv",json_encode($gpustat_data)) ;
} else {


$array=json_decode($_GET['gpus'],true) ;

// Test data
// $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));

$data = array() ;
foreach ($array as $gpu) {
$gpustat_cfg["VENDOR"] = $gpu['vendor'] ;
$gpustat_cfg["GPUID"] = $gpu['guid'] ;
$gpustat_cfg["PCIID"] = $gpu['id'] ;

switch ($gpu['vendor']) {
case 'amd':
$return=(new AMD($gpustat_cfg))->getStatistics();
$decode = json_decode($return,true);
$decode["panel"] = $gpu['panel'] ;
$data[$gpu["id"]] = $decode;
break;
case 'intel':
$return=(new Intel($gpustat_cfg))->getStatistics();
$decode = json_decode($return,true);
$decode["panel"] = $gpu['panel'] ;
$data[$gpu["id"]] = $decode;
break;
case 'nvidia':
$return = (new Nvidia($gpustat_cfg))->getStatistics() ;
$decode = json_decode($return,true);
$decode["panel"] = $gpu['panel'] ;
$data[$gpu["id"]] = $decode;
break;
default:
print_r(Error::get(Error::CONFIG_SETTINGS_NOT_VALID));
}
}
$json=json_encode($data) ;
#Test data
#$json='{"00:02.0":{"clock":100,"fan":50,"memclock":500,"memutil":55,"memused":55,"power":"100W","powermax":500,"rxutil":50,"txutil":60,"temp":50,"tempmax":200,"util":"40%","vendor":"Intel","name":"AlderLake-S GT1","3drender":"50%","blitter":"50%","interrupts":100,"powerutil":"10%","video":"20%","videnh":"30%","panel":1},"03:00.0":{"clock":0,"fan":"N\/A","memclock":"N\/A","memutil":"N\/A","memused":"N\/A","power":"N\/A","powermax":"N\/A","rxutil":"N\/A","txutil":"N\/A","temp":"N\/A","tempmax":"N\/A","util":"0%","vendor":"Intel","name":"DG2 [Arc A770]","3drender":"0%","blitter":"0%","interrupts":0,"powerutil":"0%","video":"0%","videnh":"0%","panel":2},"08:00.0":{"clock":"810","fan":"30%","memclock":"2808","memutil":"50%","memused":"50","power":"28W","powermax":"87","rxutil":50,"txutil":60,"temp":"41 \u00b0C","tempmax":"101 \u00b0C","util":"77%","vendor":"NVIDIA","name":"Quadro K4000","clockmax":"810","memclockmax":"2808","memtotal":"3018","encutil":"50%","decutil":"50%","pciemax":500,"perfstate":"P0","throttled":"No","thrtlrsn":"","pciegen":2,"pciegenmax":2,"pciewidth":1,"pciewidthmax":16,"sessions":0,"uuid":"GPU-ef6c0299-f1bc-7b5c-5291-7cd1a012f8bd","plexusing":true,"plexmem":0,"plexcount":0,"jellyfinusing":true,"jellyfinmem":100,"jellyfincount":2,"handbrakeusing":false,"handbrakemem":0,"handbrakecount":0,"embyusing":false,"embymem":0,"embycount":0,"tdarrusing":false,"tdarrmem":0,"tdarrcount":0,"unmanicusing":true,"unmanicmem":0,"unmaniccount":0,"dizquetvusing":false,"dizquetvmem":0,"dizquetvcount":0,"ersatztvusing":false,"ersatztvmem":0,"ersatztvcount":0,"fileflowsusing":false,"fileflowsmem":0,"fileflowscount":0,"frigateusing":false,"frigatemem":0,"frigatecount":0,"deepstackusing":false,"deepstackmem":0,"deepstackcount":0,"nsfminerusing":false,"nsfminermem":0,"nsfminercount":0,"shinobiprousing":false,"shinobipromem":0,"shinobiprocount":0,"foldinghomeusing":false,"foldinghomemem":0,"foldinghomecount":0,"appssupp":["plex","jellyfin","handbrake","emby","tdarr","unmanic","dizquetv","ersatztv","fileflows","frigate","deepstack","nsfminer","shinobipro","foldinghome"],"panel":3},"0c:00.0":{"clock":2110.5,"fan":200,"memclock":2220.1,"memutil":"21.2%","memused":47.51,"power":50,"powermax":200,"rxutil":"N\/A","txutil":67,"temp":"38 \u00b0C","tempmax":105,"util":"90%","vendor":"AMD","name":"Radeon RX 6400\/6500 XT\/6500M","event":"80%","vertex":"70%","texture":"60%","shaderexp":"50%","sequencer":"40%","shaderinter":"30%","scancon":"30%","primassem":"30%","depthblk":"30%","colorblk":"30%","gfxtrans":"44.1%","transused":11.57,"memclockutil":"9.6%","clockutil":"21.6%","tempunit":"C","fanmax":5550,"voltage":77.7,"voltageunit":"V","panel":4}}' ;
header('Content-Type: application/json');
header('Content-Length:' . ES . strlen($json));
echo $json;
file_put_contents("/tmp/gpujson","Time = ".date(DATE_RFC2822)."\n") ;
file_put_contents("/tmp/gpujson",$json."\n",FILE_APPEND) ;
file_put_contents("/tmp/gpuinv",json_encode($gpustat_data));
}
36 changes: 20 additions & 16 deletions src/gpustat/usr/local/emhttp/plugins/gpustat/lib/Nvidia.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private function detectApplication (SimpleXMLElement $process)
foreach ($commands as $command) {
if (strpos($process->process_name, $command) !== false) {
// For Handbrake/ffmpeg: arguments tell us which application called it
if (in_array($command, ['ffmpeg', 'HandbrakeCLI', 'python3.8','python3'])) {
if (in_array($command, ['ffmpeg', 'HandbrakeCLI', 'python3.8', 'python3'])) {
if (isset($process->pid)) {
$pid_info = $this->getFullCommand((int) $process->pid);
if (!empty($pid_info) && strlen($pid_info) > 0) {
Expand All @@ -113,9 +113,19 @@ private function detectApplication (SimpleXMLElement $process)
}
}
}
$this->pageData[$app . 'using'] = true;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These would still be required for non multi i.e. pre 6.12 unless same changes are applied to the pages and js as this common for both.

Copy link
Author

@Peuuuur-Noel Peuuuur-Noel May 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also updated code for pre 6.12.
Both versions (multigpu and non multigpu) use websocket.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nchan can only be used if os is 6.10 or greater. Not sure if there will be users running pre 6.10 Non multy should use ajax updates for backwards compatibilty.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will revert it and use ajax polling for non multigpu. Sorry.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reverted to ajax polling for non multi.

$this->pageData[$app . 'mem'] += (int)$this->stripText(' MiB', $process->used_memory);
$this->pageData[$app . 'count']++;

$usedMemory = (int) $this->stripText(' MiB', $process->used_memory);
$index = array_search($app, array_column($this->pageData['active_apps'], 'name'));
if ($index === false) {
$this->pageData['active_apps'][] = [
'name' => $app,
'mem' => $usedMemory,
'count' => 1,
];
} else {
$this->pageData['active_apps'][$index]['mem'] += $usedMemory;
$this->pageData['active_apps'][$index]['count']++;
}
// If we match a more specific command/app to a process, continue on to the next process
break 2;
}
Expand Down Expand Up @@ -197,11 +207,11 @@ public function getInventorym(): array
}
foreach($result as $gpu) {
$cmd =self::CMD_UTILITY . ES . sprintf(self::INVENTORY_PARM_PCI, $gpu['guid']) ;
$cmdres = $this->stdout = shell_exec($cmd);
$cmdres = $this->stdout = shell_exec($cmd);
$pci = substr($cmdres,14,12);
$gpu['id'] = substr($pci,5) ;
$gpu['vendor'] = 'nvidia' ;
$result2[$pci] = $gpu ;
$result2[$pci] = $gpu ;
}
if (empty($result)) $result2=$this->getPCIInventory() ;
}
Expand Down Expand Up @@ -263,7 +273,7 @@ private function getProductName (string $name)
if (strlen($name) > 20 && str_word_count($name) > 2) {
$words = explode(" ", $name);
if ($words[0] == "GeForce") {
array_shift($words) ;
array_shift($words) ;
$words2 = implode(" ", $words) ;
if (strlen($words2) <= 20) $this->pageData['name'] = $words2;
} else $this->pageData['name'] = sprintf("%0s %1s", $words[0], $words[1]);
Expand Down Expand Up @@ -360,7 +370,7 @@ public function getStatistics()
if (!empty($this->stdout) && strlen($this->stdout) > 0) {
$this->parseStatistics();
} else {

$this->pageData['error'][] = Error::get(Error::VENDOR_DATA_NOT_RETURNED);
}
} else {
Expand All @@ -377,7 +387,7 @@ public function getStatistics()
$this->pageData["vfio"] = false ;
$this->pageData["vfiochk"] = $this->checkVFIO("0000:".$this->settings['PCIID']) ;
$this->pageData["vfiochkid"] = "0000:".$this->settings['PCIID'] ;

} else {
$this->pageData["vfio"] = true ;
$this->pageData["vendor"] = "Nvidia" ;
Expand Down Expand Up @@ -454,12 +464,7 @@ private function parseStatistics()
'uuid' => 'N/A',
];

// Set App HW Usage Defaults
foreach (self::SUPPORTED_APPS AS $app => $process) {
$this->pageData[$app . "using"] = false;
$this->pageData[$app . "mem"] = 0;
$this->pageData[$app . "count"] = 0;
}
$this->pageData['active_apps'] = [];
if (isset($data->product_name)) {
$this->getProductName($data->product_name);
}
Expand All @@ -484,7 +489,6 @@ private function parseStatistics()
}
// For some reason, encoder_sessions->session_count is not reliable on my install, better to count processes
if ($this->settings['DISPSESSIONS']) {
$this->pageData['appssupp'] = array_keys(self::SUPPORTED_APPS);
if (isset($data->processes->process_info)) {
$this->pageData['sessions'] = count($data->processes->process_info);
if ($this->pageData['sessions'] > 0) {
Expand Down
92 changes: 92 additions & 0 deletions src/gpustat/usr/local/emhttp/plugins/gpustat/nchan/gpustat
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/usr/bin/php -q
<?php
/*
MIT License

Copyright (c) 2020-2022 b3rs3rk

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
?>
<?php
$docroot = '/usr/local/emhttp';

const ES = ' ';

require_once "$docroot/webGui/include/publish.php";

require_once __DIR__ . '/../lib/Main.php';
require_once __DIR__ . '/../lib/Nvidia.php';
require_once __DIR__ . '/../lib/Intel.php';
require_once __DIR__ . '/../lib/AMD.php';
require_once __DIR__ . '/../lib/Error.php';

use gpustat\lib\AMD;
use gpustat\lib\Main;
use gpustat\lib\Nvidia;
use gpustat\lib\Intel;
use gpustat\lib\Error;

while (true) {
$gpustat_cfg = Main::getSettings();
$multi = urldecode($gpustat_cfg["MULTIGPUJSON"]);
$multigpus = json_decode($multi, true);
$data = array() ;

foreach ($multigpus as $gpu) {
$gpustat_cfg["VENDOR"] = $gpu['vendor'] ;
$gpustat_cfg["GPUID"] = $gpu['guid'] ;
$gpustat_cfg["PCIID"] = $gpu['id'] ;

switch ($gpu['vendor']) {
case 'amd':
$return = (new AMD($gpustat_cfg))->getStatistics();
$decode = json_decode($return, true);
$data[$gpu["id"]] = $decode;
break;
case 'intel':
$return = (new Intel($gpustat_cfg))->getStatistics();
$decode = json_decode($return, true);
$data[$gpu["id"]] = $decode;
break;
case 'nvidia':
$return = (new Nvidia($gpustat_cfg))->getStatistics() ;
$decode = json_decode($return, true);
$data[$gpu["id"]] = $decode;
break;
default:
print_r(Error::get(Error::CONFIG_SETTINGS_NOT_VALID));
}
}

// Test data
// $data = array_merge($data, json_decode('{"00:02.0": {"clock": 100, "fan": 50, "memclock": 500, "memutil": 55, "memused": 55, "power": "100W", "powermax": 500, "rxutil": 50, "txutil": 60, "temp": 50, "tempmax": 200, "util": "40%", "vendor": "Intel", "name": "AlderLake-S GT1", "3drender": "50%", "blitter": "50%", "interrupts": 100, "powerutil": "10%", "video": "20%", "videnh": "30%"}, "09:00.0": {"clock": 0, "fan": "N\/A", "memclock": "N\/A", "memutil": "N\/A", "memused": "N\/A", "power": "N\/A", "powermax": "N\/A", "rxutil": "N\/A", "txutil": "N\/A", "temp": "N\/A", "tempmax": "N\/A", "util": "0%", "vendor": "Intel", "name": "DG2 [Arc A770]", "3drender": "0%", "blitter": "0%", "interrupts": 0, "powerutil": "0%", "video": "0%", "videnh": "0%"}, "08:00.0": {"active_apps":[{"name": "plex", "mem": 214, "count": 2}], "clock": "810", "fan": "30%", "memclock": "2808", "memutil": "50%", "memused": "50", "power": "28W", "powermax": "87", "rxutil": 50, "txutil": 60, "temp": "41 \u00b0C", "tempmax": "101 \u00b0C", "util": "77%", "vendor": "NVIDIA", "name": "Quadro K4000", "clockmax": "810", "memclockmax": "2808", "memtotal": "3018", "encutil": "50%", "decutil": "50%", "pciemax": 500, "perfstate": "P0", "throttled": "No", "thrtlrsn": "", "pciegen": 2, "pciegenmax": 2, "pciewidth": 1, "pciewidthmax": 16, "sessions": 2, "uuid": "GPU-ef6c0299-f1bc-7b5c-5291-7cd1a012f8bd"}, "0c:00.0": {"clock": 2110.5, "fan": 200, "memclock": 2220.1, "memutil": "21.2%", "memused": 47.51, "power": 50, "powermax": 200, "rxutil": "N\/A", "txutil": 67, "temp": "38 \u00b0C", "tempmax": 105, "util": "90%", "vendor": "AMD", "name": "Radeon RX 6400\/6500 XT\/6500M", "event": "80%", "vertex": "70%", "texture": "60%", "shaderexp": "50%", "sequencer": "40%", "shaderinter": "30%", "scancon": "30%", "primassem": "30%", "depthblk": "30%", "colorblk": "30%", "gfxtrans": "44.1%", "transused": 11.57, "memclockutil": "9.6%", "clockutil": "21.6%", "tempunit": "C", "fanmax": 5550, "voltage": 77.7, "voltageunit": "V"}}', true));

$json = json_encode($data) ;

publish('gpustat', $json);

file_put_contents("/tmp/gpujson", "Time = " . date(DATE_RFC2822) . "\n" . $json . "\n") ;

if ($gpustat_cfg['UIREFRESH'] != '1') {
exit;
}

sleep(round($gpustat_cfg['UIREFRESHINT'] / 1000));
}
Loading