[OPTIMIZED, see post 6] KSysGuard NVIDIA GPU monitoring: Only 2 files!

Stumbled upon a few Github Gists that would be helpful if you use KSysGuard:

nvidia-gpu-sensor.pl: https://gist.github.com/Sporif/4ce63f7b6eea691bdbb18905a9589169

RAW, in case the user or script ever gets deleted:

#!/usr/bin/perl -w

# act as a KSysGuard sensor
# provides NVIDIA GPU info via `nvidia-smi`

# Usage:
# 1. Save this script, make it executable and move it to a directory in your $PATH
# 2. Save this ksysguard sensor file for Nvidia: https://gist.github.com/Sporif/31f0d8d9efc3315752aa4031f7080d79
# 2. In KSysGuard's menu, open "File > Import Tab From File option"
# 3. Open the sensor file (nvidia.srgd)

# See Also
# https://techbase.kde.org/Development/Tutorials/Sensors

$|=1;

print "ksysguardd 1.2.0\n";
print "ksysguardd> ";

while(<>){
    if(/monitors/){
        print "gpu_temp\tinteger\n";
        print "gpu_fan_speed\tinteger\n";
        print "gpu_core_usage\tinteger\n";
        print "gpu_core_clock\tinteger\n";
        print "gpu_mem_mib\tinteger\n";
        print "gpu_mem_clock\tinteger\n";
        print "gpu_video_decode\tinteger\n";
        print "gpu_video_encode\tinteger\n";
    }
    if(/gpu_temp/){
        if(/\?/){
            print "GPU Temp\t0\t100\t°C\n";
        }else{
            print `nvidia-smi --query-gpu=temperature.gpu --format=csv,noheader`;
        }
    }
    if(/gpu_fan_speed/){
        if(/\?/){
            print "GPU Fan Speed\t0\t100\t%\n";
        }else{
            print `nvidia-smi --query-gpu=fan.speed --format=csv,noheader,nounits`;
        }
    }
    if(/gpu_core_usage/){
        if(/\?/){
            print "GPU Core Usage\t0\t100\t%\n";
        }else{
            print `nvidia-smi --query-gpu=utilization.gpu --format=csv,noheader,nounits`;
        }
    }
    if(/gpu_core_clock/){
        if(/\?/){
            print "GPU Core Clock\t0\t2500\tMhz\n";
        }else{
            print `nvidia-smi --query-gpu=clocks.current.graphics --format=csv,noheader,nounits`;
        }
    }
    if(/gpu_mem_mib/){
        if(/\?/){
            print "GPU Memory Usage\t0\t".`nvidia-smi --query-gpu=memory.total --format=csv,noheader,nounits | perl -pe 'chomp'`."\tMiB\n";
        }else{
            print `nvidia-smi --query-gpu=memory.used --format=csv,noheader,nounits`;
        }
    }
    if(/gpu_mem_clock/){
        if(/\?/){
            print "GPU Memory Clock\t0\t8000\tMhz\n";
        }else{
            print `nvidia-smi --query-gpu=clocks.current.memory --format=csv,noheader,nounits`;
        }
    }
    if(/gpu_video_decode/){
        if(/\?/){
            print "GPU Decoding\t0\t100\t%\n";
        }else{
            print `nvidia-smi dmon -c 1 -s u | sed s/#// | awk '{print \$5}' | tail -n 1`;
        }
    }
    if(/gpu_video_encode/){
        if(/\?/){
            print "GPU Encoding\t0\t100\t%\n";
        }else{
            print `nvidia-smi dmon -c 1 -s u | sed s/#// | awk '{print \$4}' | tail -n 1`;
        }
    }
    print "ksysguardd> ";
}

There’s an error in the original script where a line break in the input wasn’t sanitized so this patch should correct it:

--- nvidia-gpu-sensor.pl	2019-02-22 14:25:25.337646000 -0800
+++ nvidia-gpu-sensor-fixed.pl	2019-02-22 21:46:22.872704984 -0800
@@ -13,6 +13,8 @@
 # https://techbase.kde.org/Development/Tutorials/Sensors
 
 $|=1;
+$MEM = "".`nvidia-smi -i 0 --query-gpu=memory.total --format=csv,noheader,nounits`."";
+chomp $MEM;
 
 print "ksysguardd 1.2.0\n";
 print "ksysguardd> ";
@@ -58,7 +60,7 @@
     }
     if(/gpu_mem_mib/){
         if(/\?/){
-            print "GPU Memory Usage\t0\t".`nvidia-smi --query-gpu=memory.total --format=csv,noheader,nounits | perl -pe 'chomp'`."\tMiB\n";
+            print "GPU Memory Usage\t0\t$MEM\tMiB\n";
         }else{
             print `nvidia-smi --query-gpu=memory.used --format=csv,noheader,nounits`;
         }

And the 2nd file is this nvidia.sgml you import into KSysGuard: https://gist.github.com/Sporif/31f0d8d9efc3315752aa4031f7080d79

RAW, in case the user or script ever gets deleted:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE KSysGuardWorkSheet>
<WorkSheet title="Nvidia" columns="2" rows="4" locked="0" interval="1">
 <host name="127.0.0.1" port="-1" shell="" command="nvidia-gpu-sensor.pl"/>
 <display labels="1" svgBackground="" vScroll="1" version="1" class="FancyPlotter" vLines="1" unit="" row="0" hScale="3" autoRange="1" fontSize="8" manualRange="0" rowSpan="1" columnSpan="1" showUnit="0" vDistance="30" column="0" stacked="0" title="GPU Temp" hLines="1">
  <beam sensorName="gpu_temp" sensorType="integer" color="0xff0057ae" hostName="127.0.0.1"/>
 </display>
 <display labels="1" svgBackground="" vScroll="1" version="1" class="FancyPlotter" vLines="1" unit="" row="0" hScale="3" autoRange="1" fontSize="8" manualRange="0" rowSpan="1" columnSpan="1" showUnit="0" vDistance="30" column="1" stacked="0" title="GPU Fan Speed" hLines="1">
  <beam sensorName="gpu_fan_speed" sensorType="integer" color="0xff0057ae" hostName="127.0.0.1"/>
 </display>
 <display labels="1" svgBackground="" vScroll="1" version="1" class="FancyPlotter" vLines="1" unit="" row="1" hScale="3" autoRange="1" fontSize="8" manualRange="0" rowSpan="1" columnSpan="1" showUnit="0" vDistance="30" column="0" stacked="0" title="GPU Core Usage" hLines="1">
  <beam sensorName="gpu_core_usage" sensorType="integer" color="0xff0057ae" hostName="127.0.0.1"/>
 </display>
 <display labels="1" svgBackground="" vScroll="1" version="1" class="FancyPlotter" vLines="1" unit="" row="1" hScale="3" autoRange="1" fontSize="8" manualRange="0" rowSpan="1" columnSpan="1" showUnit="0" vDistance="30" column="1" stacked="0" title="GPU Core Clock" hLines="1">
  <beam sensorName="gpu_core_clock" sensorType="integer" color="0xff0057ae" hostName="127.0.0.1"/>
 </display>
 <display labels="1" svgBackground="" vScroll="0" version="1" class="FancyPlotter" vLines="0" unit="" row="2" hScale="3" autoRange="1" fontSize="8" manualRange="0" rowSpan="1" columnSpan="1" showUnit="0" vDistance="30" column="0" stacked="0" title="GPU Memory Usage" hLines="1">
  <beam sensorName="gpu_mem_mib" sensorType="integer" color="0xff0057ae" hostName="127.0.0.1"/>
 </display>
 <display labels="1" svgBackground="" vScroll="0" version="1" class="FancyPlotter" vLines="0" unit="" row="2" hScale="3" autoRange="1" fontSize="8" manualRange="0" rowSpan="1" columnSpan="1" showUnit="0" vDistance="30" column="1" stacked="0" title="GPU Memory Clock" hLines="1">
  <beam sensorName="gpu_mem_clock" sensorType="integer" color="0xff0057ae" hostName="127.0.0.1"/>
 </display>
 <display labels="1" svgBackground="" vScroll="1" version="1" class="FancyPlotter" vLines="1" unit="" row="3" hScale="3" autoRange="1" fontSize="8" manualRange="0" rowSpan="1" columnSpan="1" showUnit="0" vDistance="30" column="0" stacked="0" title="GPU Video Decode" hLines="1">
  <beam sensorName="gpu_video_decode" sensorType="integer" color="0xff0057ae" hostName="127.0.0.1"/>
 </display>
 <display labels="1" svgBackground="" vScroll="1" version="1" class="FancyPlotter" vLines="1" unit="" row="3" hScale="3" autoRange="1" fontSize="8" manualRange="0" rowSpan="1" columnSpan="1" showUnit="0" vDistance="30" column="1" stacked="0" title="GPU Video Encode" hLines="1">
  <beam sensorName="gpu_video_encode" sensorType="integer" color="0xff0057ae" hostName="127.0.0.1"/>
 </display>
</WorkSheet>

Usage:

Put nvidia-gpu-sensor.pl in $HOME/.local/bin (Ubuntu. For other distros, it’s where $PATH is)

Make it executable: chmod +x $HOME/.local/bin/nvidia-gpu-sensor.pl

Import the .sgrd from the Gist into KSysGuard. Without importing the file, the Perl script won’t load.

DONE!

Gotta say, this is pretty elegant for those wanting GPU monitoring out of KSysGuard using nvidia-smi

1 Like

Added my own patch for PCIe Bandwidth Utilization and Fan RPM: (Patch with the original file from Gist)

pcie-fanrpm.patch:

--- nvidia-gpu-sensor.pl	2019-02-22 14:25:25.337646000 -0800
+++ nvidia-gpu-sensor-new.pl	2019-02-22 22:02:18.181801958 -0800
@@ -13,6 +13,8 @@
 # https://techbase.kde.org/Development/Tutorials/Sensors
 
 $|=1;
+$MEM = "".`nvidia-smi -i 0 --query-gpu=memory.total --format=csv,noheader,nounits`."";
+chomp $MEM;
 
 print "ksysguardd 1.2.0\n";
 print "ksysguardd> ";
@@ -21,12 +23,14 @@
     if(/monitors/){
         print "gpu_temp\tinteger\n";
         print "gpu_fan_speed\tinteger\n";
+        print "gpu_fan_RPM\tinteger\n";
         print "gpu_core_usage\tinteger\n";
         print "gpu_core_clock\tinteger\n";
         print "gpu_mem_mib\tinteger\n";
         print "gpu_mem_clock\tinteger\n";
         print "gpu_video_decode\tinteger\n";
         print "gpu_video_encode\tinteger\n";
+        print "gpu_pcie_utilization\tinteger\n";
     }
     if(/gpu_temp/){
         if(/\?/){
@@ -42,6 +46,13 @@
             print `nvidia-smi --query-gpu=fan.speed --format=csv,noheader,nounits`;
         }
     }
+        if(/gpu_fan_RPM/){
+        if(/\?/){
+            print "GPU Fan RPM\t0\t\tRPM\n";
+        }else{
+            print `nvidia-settings -query=[fan:0]/GPUCurrentFanSpeedRPM -t`;
+        }
+    }
     if(/gpu_core_usage/){
         if(/\?/){
             print "GPU Core Usage\t0\t100\t%\n";
@@ -58,31 +69,38 @@
     }
     if(/gpu_mem_mib/){
         if(/\?/){
-            print "GPU Memory Usage\t0\t".`nvidia-smi --query-gpu=memory.total --format=csv,noheader,nounits | perl -pe 'chomp'`."\tMiB\n";
+            print "GPU Memory Usage\t0\t$MEM\tMiB\n";
         }else{
             print `nvidia-smi --query-gpu=memory.used --format=csv,noheader,nounits`;
         }
     }
     if(/gpu_mem_clock/){
         if(/\?/){
-            print "GPU Memory Clock\t0\t8000\tMhz\n";
+            print "GPU Memory Clock\t0\t6000\tMhz\n";
         }else{
             print `nvidia-smi --query-gpu=clocks.current.memory --format=csv,noheader,nounits`;
         }
     }
     if(/gpu_video_decode/){
         if(/\?/){
-            print "GPU Decoding\t0\t100\t%\n";
+            print "GPU Video Decode\t0\t100\t%\n";
         }else{
             print `nvidia-smi dmon -c 1 -s u | sed s/#// | awk '{print \$5}' | tail -n 1`;
         }
     }
     if(/gpu_video_encode/){
         if(/\?/){
-            print "GPU Encoding\t0\t100\t%\n";
+            print "GPU Video Encode\t0\t100\t%\n";
         }else{
             print `nvidia-smi dmon -c 1 -s u | sed s/#// | awk '{print \$4}' | tail -n 1`;
         }
     }
+    if(/gpu_pcie_utilization/){
+        if(/\?/){
+            print "PCIe Bandwidth Utilization\t0\t100\t%\n";
+        }else{
+            print `nvidia-settings -query=[gpu:0]/GPUUtilization -t | awk '{print $4}' | grep -Eo '[[:alpha:]]+|[0-9]+' | tail -n 1`;
+        }
+    }
     print "ksysguardd> ";
 }

pcie-fanrpm-sgrd.patch:

--- nvidia.sgrd	2019-02-22 14:25:47.092943627 -0800
+++ nvidia-new.sgrd	2019-02-22 22:10:07.344730435 -0800
@@ -1,29 +1,35 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE KSysGuardWorkSheet>
-<WorkSheet title="Nvidia" columns="2" rows="4" locked="0" interval="1">
+<WorkSheet title="Nvidia" columns="2" rows="5" locked="0" interval="1">
  <host name="127.0.0.1" port="-1" shell="" command="nvidia-gpu-sensor.pl"/>
  <display labels="1" svgBackground="" vScroll="1" version="1" class="FancyPlotter" vLines="1" unit="" row="0" hScale="3" autoRange="1" fontSize="8" manualRange="0" rowSpan="1" columnSpan="1" showUnit="0" vDistance="30" column="0" stacked="0" title="GPU Temp" hLines="1">
   <beam sensorName="gpu_temp" sensorType="integer" color="0xff0057ae" hostName="127.0.0.1"/>
  </display>
- <display labels="1" svgBackground="" vScroll="1" version="1" class="FancyPlotter" vLines="1" unit="" row="0" hScale="3" autoRange="1" fontSize="8" manualRange="0" rowSpan="1" columnSpan="1" showUnit="0" vDistance="30" column="1" stacked="0" title="GPU Fan Speed" hLines="1">
-  <beam sensorName="gpu_fan_speed" sensorType="integer" color="0xff0057ae" hostName="127.0.0.1"/>
- </display>
- <display labels="1" svgBackground="" vScroll="1" version="1" class="FancyPlotter" vLines="1" unit="" row="1" hScale="3" autoRange="1" fontSize="8" manualRange="0" rowSpan="1" columnSpan="1" showUnit="0" vDistance="30" column="0" stacked="0" title="GPU Core Usage" hLines="1">
+  <display labels="1" svgBackground="" vScroll="1" version="1" class="FancyPlotter" vLines="1" unit="" row="0" hScale="3" autoRange="1" fontSize="8" manualRange="0" rowSpan="1" columnSpan="1" showUnit="0" vDistance="30" column="1" stacked="0" title="GPU Core Usage" hLines="1">
   <beam sensorName="gpu_core_usage" sensorType="integer" color="0xff0057ae" hostName="127.0.0.1"/>
  </display>
- <display labels="1" svgBackground="" vScroll="1" version="1" class="FancyPlotter" vLines="1" unit="" row="1" hScale="3" autoRange="1" fontSize="8" manualRange="0" rowSpan="1" columnSpan="1" showUnit="0" vDistance="30" column="1" stacked="0" title="GPU Core Clock" hLines="1">
+ <display labels="1" svgBackground="" vScroll="1" version="1" class="FancyPlotter" vLines="1" unit="" row="1" hScale="3" autoRange="1" fontSize="8" manualRange="0" rowSpan="1" columnSpan="1" showUnit="0" vDistance="30" column="0" stacked="0" title="GPU Core Clock" hLines="1">
   <beam sensorName="gpu_core_clock" sensorType="integer" color="0xff0057ae" hostName="127.0.0.1"/>
  </display>
+  <display labels="1" svgBackground="" vScroll="0" version="1" class="FancyPlotter" vLines="0" unit="" row="1" hScale="3" autoRange="1" fontSize="8" manualRange="0" rowSpan="1" columnSpan="1" showUnit="0" vDistance="30" column="1" stacked="0" title="GPU Memory Clock" hLines="1">
+  <beam sensorName="gpu_mem_clock" sensorType="integer" color="0xff0057ae" hostName="127.0.0.1"/>
+ </display>
  <display labels="1" svgBackground="" vScroll="0" version="1" class="FancyPlotter" vLines="0" unit="" row="2" hScale="3" autoRange="1" fontSize="8" manualRange="0" rowSpan="1" columnSpan="1" showUnit="0" vDistance="30" column="0" stacked="0" title="GPU Memory Usage" hLines="1">
   <beam sensorName="gpu_mem_mib" sensorType="integer" color="0xff0057ae" hostName="127.0.0.1"/>
  </display>
- <display labels="1" svgBackground="" vScroll="0" version="1" class="FancyPlotter" vLines="0" unit="" row="2" hScale="3" autoRange="1" fontSize="8" manualRange="0" rowSpan="1" columnSpan="1" showUnit="0" vDistance="30" column="1" stacked="0" title="GPU Memory Clock" hLines="1">
-  <beam sensorName="gpu_mem_clock" sensorType="integer" color="0xff0057ae" hostName="127.0.0.1"/>
+ <display labels="1" svgBackground="" vScroll="1" version="1" class="FancyPlotter" vLines="1" unit="" row="2" hScale="3" autoRange="1" fontSize="8" manualRange="0" rowSpan="1" columnSpan="1" showUnit="0" vDistance="30" column="1" stacked="0" title="GPU Fan Speed" hLines="1">
+  <beam sensorName="gpu_fan_speed" sensorType="integer" color="0xff0057ae" hostName="127.0.0.1"/>
+ </display>
+    <display labels="1" svgBackground="" vScroll="1" version="1" class="FancyPlotter" vLines="1" unit="" row="3" hScale="3" autoRange="1" fontSize="8" manualRange="0" rowSpan="1" columnSpan="1" showUnit="0" vDistance="30" column="0" stacked="0" title="GPU Fan RPM" hLines="1">
+  <beam sensorName="gpu_fan_RPM" sensorType="integer" color="0xff0057ae" hostName="127.0.0.1"/>
+ </display>
+   <display labels="1" svgBackground="" vScroll="1" version="1" class="FancyPlotter" vLines="1" unit="" row="3" hScale="3" autoRange="1" fontSize="8" manualRange="0" rowSpan="1" columnSpan="1" showUnit="0" vDistance="30" column="1" stacked="0" title="PCIe Bandwidth Utilization" hLines="1">
+  <beam sensorName="gpu_pcie_utilization" sensorType="integer" color="0xff0057ae" hostName="127.0.0.1"/>
  </display>
- <display labels="1" svgBackground="" vScroll="1" version="1" class="FancyPlotter" vLines="1" unit="" row="3" hScale="3" autoRange="1" fontSize="8" manualRange="0" rowSpan="1" columnSpan="1" showUnit="0" vDistance="30" column="0" stacked="0" title="GPU Video Decode" hLines="1">
+ <display labels="1" svgBackground="" vScroll="1" version="1" class="FancyPlotter" vLines="1" unit="" row="4" hScale="3" autoRange="1" fontSize="8" manualRange="0" rowSpan="1" columnSpan="1" showUnit="0" vDistance="30" column="0" stacked="0" title="GPU Video Decode" hLines="1">
   <beam sensorName="gpu_video_decode" sensorType="integer" color="0xff0057ae" hostName="127.0.0.1"/>
  </display>
- <display labels="1" svgBackground="" vScroll="1" version="1" class="FancyPlotter" vLines="1" unit="" row="3" hScale="3" autoRange="1" fontSize="8" manualRange="0" rowSpan="1" columnSpan="1" showUnit="0" vDistance="30" column="1" stacked="0" title="GPU Video Encode" hLines="1">
+ <display labels="1" svgBackground="" vScroll="1" version="1" class="FancyPlotter" vLines="1" unit="" row="4" hScale="3" autoRange="1" fontSize="8" manualRange="0" rowSpan="1" columnSpan="1" showUnit="0" vDistance="30" column="1" stacked="0" title="GPU Video Encode" hLines="1">
   <beam sensorName="gpu_video_encode" sensorType="integer" color="0xff0057ae" hostName="127.0.0.1"/>
  </display>
 </WorkSheet>

I added PCIe Bandwidth Utilization and Fan RPM monitoring using nvidia-settings. Patch using patch < pcie-fanrpm.patch and patch < pcie-fanrpm-sgrd.patch.

WARNING: May or may not cause performance issues since it’s calling upon nvidia-settings. YMMV. Use at your own risk.

How do you apply the .patch files?

Put the .pl and .sgrd files and .patch files in the same folder, run a terminal from that folder, and use patch < (filename).patch

I’m providing the contents of the file in line, so copy and paste the code into a text editor, and save as the filename I’m providing.

MAJOR UPDATE:

nvidia-settings calls were a resource hog. Rewrote it: (Patch using the original Gists again)

dmon.patch:

--- nvidia-gpu-sensor.pl	2019-02-22 14:25:25.337646000 -0800
+++ nvidia-gpu-sensor-new.pl	2019-02-23 00:41:01.302245590 -0800
@@ -8,11 +8,16 @@
 # 2. Save this ksysguard sensor file for Nvidia: https://gist.github.com/Sporif/31f0d8d9efc3315752aa4031f7080d79
 # 2. In KSysGuard's menu, open "File > Import Tab From File option"
 # 3. Open the sensor file (nvidia.srgd)
+# 4. To change which GPU is monitored, change $GPU
 
 # See Also
 # https://techbase.kde.org/Development/Tutorials/Sensors
 
 $|=1;
+$GPU = 0;
+chomp $GPU;
+$MEM = "".`nvidia-smi -i $GPU --query-gpu=memory.total --format=csv,noheader,nounits`."";
+chomp $MEM;
 
 print "ksysguardd 1.2.0\n";
 print "ksysguardd> ";
@@ -21,67 +26,99 @@
     if(/monitors/){
         print "gpu_temp\tinteger\n";
         print "gpu_fan_speed\tinteger\n";
+        print "gpu_pwr_usage\tinteger\n";
         print "gpu_core_usage\tinteger\n";
         print "gpu_core_clock\tinteger\n";
         print "gpu_mem_mib\tinteger\n";
         print "gpu_mem_clock\tinteger\n";
+        print "gpu_mem_load\tinteger\n";
         print "gpu_video_decode\tinteger\n";
         print "gpu_video_encode\tinteger\n";
+        print "gpu_pcie_rx\tinteger\n";
+        print "gpu_pcie_tx\tinteger\n";
     }
     if(/gpu_temp/){
         if(/\?/){
             print "GPU Temp\t0\t100\t°C\n";
         }else{
-            print `nvidia-smi --query-gpu=temperature.gpu --format=csv,noheader`;
+            print `nvidia-smi dmon -i $GPU -c 1 -s p | sed s/#// | awk '{print \$3}' | tail -n 1`;
         }
     }
     if(/gpu_fan_speed/){
         if(/\?/){
             print "GPU Fan Speed\t0\t100\t%\n";
         }else{
-            print `nvidia-smi --query-gpu=fan.speed --format=csv,noheader,nounits`;
+            print `nvidia-smi -i $GPU --query-gpu=fan.speed --format=csv,noheader,nounits`;
+        }
+    }
+        if(/gpu_pwr_usage/){
+        if(/\?/){
+            print "GPU Power Usage\t0\t\tW\n";
+        }else{
+            print `nvidia-smi dmon -i $GPU -c 1 -s p | sed s/#// | awk '{print \$2}' | tail -n 1`;
         }
     }
     if(/gpu_core_usage/){
         if(/\?/){
             print "GPU Core Usage\t0\t100\t%\n";
         }else{
-            print `nvidia-smi --query-gpu=utilization.gpu --format=csv,noheader,nounits`;
+            print `nvidia-smi dmon -i $GPU -c 1 -s u | sed s/#// | awk '{print \$2}' | tail -n 1`;
         }
     }
     if(/gpu_core_clock/){
         if(/\?/){
-            print "GPU Core Clock\t0\t2500\tMhz\n";
+            print "GPU Core Clock\t0\t\tMhz\n";
         }else{
-            print `nvidia-smi --query-gpu=clocks.current.graphics --format=csv,noheader,nounits`;
+            print `nvidia-smi dmon -i $GPU -c 1 -s c | sed s/#// | awk '{print \$3}' | tail -n 1`;
         }
     }
     if(/gpu_mem_mib/){
         if(/\?/){
-            print "GPU Memory Usage\t0\t".`nvidia-smi --query-gpu=memory.total --format=csv,noheader,nounits | perl -pe 'chomp'`."\tMiB\n";
+            print "GPU Memory Usage\t0\t$MEM\tMiB\n";
         }else{
-            print `nvidia-smi --query-gpu=memory.used --format=csv,noheader,nounits`;
+            print `nvidia-smi dmon -i $GPU -c 1 -s m | sed s/#// | awk '{print \$2}' | tail -n 1`;
         }
     }
     if(/gpu_mem_clock/){
         if(/\?/){
-            print "GPU Memory Clock\t0\t8000\tMhz\n";
+            print "GPU Memory Clock\t0\t\tMhz\n";
+        }else{
+            print `nvidia-smi dmon -i $GPU -c 1 -s c | sed s/#// | awk '{print \$2}' | tail -n 1`;
+        }
+    }
+    if(/gpu_mem_load/){
+        if(/\?/){
+            print "GPU Memory Load\t0\t100\t%\n";
         }else{
-            print `nvidia-smi --query-gpu=clocks.current.memory --format=csv,noheader,nounits`;
+            print `nvidia-smi dmon -i $GPU -c 1 -s u | sed s/#// | awk '{print \$3}' | tail -n 1`;
         }
     }
     if(/gpu_video_decode/){
         if(/\?/){
-            print "GPU Decoding\t0\t100\t%\n";
+            print "GPU Video Decode\t0\t100\t%\n";
         }else{
-            print `nvidia-smi dmon -c 1 -s u | sed s/#// | awk '{print \$5}' | tail -n 1`;
+            print `nvidia-smi dmon -i $GPU -c 1 -s u | sed s/#// | awk '{print \$5}' | tail -n 1`;
         }
     }
     if(/gpu_video_encode/){
         if(/\?/){
-            print "GPU Encoding\t0\t100\t%\n";
+            print "GPU Video Encode\t0\t100\t%\n";
+        }else{
+            print `nvidia-smi dmon -i $GPU -c 1 -s u | sed s/#// | awk '{print \$4}' | tail -n 1`;
+        }
+    }
+    if(/gpu_pcie_rx/){
+        if(/\?/){
+            print "PCIe RX Bandwidth\t0\t\tMB/s\n";
+        }else{
+            print `nvidia-smi dmon -i $GPU -c 1 -s t | sed s/#// | awk '{print \$2}' | tail -n 1`;
+        }
+    }
+    if(/gpu_pcie_tx/){
+        if(/\?/){
+            print "PCIe TX Bandwidth\t0\t\tMB/s\n";
         }else{
-            print `nvidia-smi dmon -c 1 -s u | sed s/#// | awk '{print \$4}' | tail -n 1`;
+            print `nvidia-smi dmon -i $GPU -c 1 -s t | sed s/#// | awk '{print \$3}' | tail -n 1`;
         }
     }
     print "ksysguardd> ";

dmon-sgrd.patch:

--- nvidia.sgrd	2019-02-22 14:25:47.092943627 -0800
+++ nvidia-new.sgrd	2019-02-23 01:11:12.586051851 -0800
@@ -1,29 +1,37 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE KSysGuardWorkSheet>
-<WorkSheet title="Nvidia" columns="2" rows="4" locked="0" interval="1">
- <host name="127.0.0.1" port="-1" shell="" command="nvidia-gpu-sensor.pl"/>
- <display labels="1" svgBackground="" vScroll="1" version="1" class="FancyPlotter" vLines="1" unit="" row="0" hScale="3" autoRange="1" fontSize="8" manualRange="0" rowSpan="1" columnSpan="1" showUnit="0" vDistance="30" column="0" stacked="0" title="GPU Temp" hLines="1">
-  <beam sensorName="gpu_temp" sensorType="integer" color="0xff0057ae" hostName="127.0.0.1"/>
+<WorkSheet columns="2" interval="1" rows="5" locked="0" title="NVIDIA">
+ <host name="127.0.0.1" port="-1" command="nvidia-gpu-sensor.pl" shell=""/>
+ <display column="0" version="1" labels="1" columnSpan="1" fontSize="8" stacked="0" rowSpan="1" class="FancyPlotter" unit="" svgBackground="" autoRange="1" row="0" hLines="1" title="GPU Temp" vScroll="1" vLines="1" manualRange="0" vDistance="30" hScale="3" showUnit="0">
+  <beam color="0xff0057ae" hostName="127.0.0.1" sensorName="gpu_temp" sensorType="integer"/>
  </display>
- <display labels="1" svgBackground="" vScroll="1" version="1" class="FancyPlotter" vLines="1" unit="" row="0" hScale="3" autoRange="1" fontSize="8" manualRange="0" rowSpan="1" columnSpan="1" showUnit="0" vDistance="30" column="1" stacked="0" title="GPU Fan Speed" hLines="1">
-  <beam sensorName="gpu_fan_speed" sensorType="integer" color="0xff0057ae" hostName="127.0.0.1"/>
+ <display column="1" version="1" labels="1" columnSpan="1" fontSize="8" stacked="0" rowSpan="1" class="FancyPlotter" unit="" svgBackground="" autoRange="1" row="0" hLines="1" title="GPU Core Usage" vScroll="0" vLines="0" manualRange="0" vDistance="30" hScale="6" showUnit="0">
+  <beam color="0xff0057ae" hostName="127.0.0.1" sensorName="gpu_core_usage" sensorType="integer"/>
  </display>
- <display labels="1" svgBackground="" vScroll="1" version="1" class="FancyPlotter" vLines="1" unit="" row="1" hScale="3" autoRange="1" fontSize="8" manualRange="0" rowSpan="1" columnSpan="1" showUnit="0" vDistance="30" column="0" stacked="0" title="GPU Core Usage" hLines="1">
-  <beam sensorName="gpu_core_usage" sensorType="integer" color="0xff0057ae" hostName="127.0.0.1"/>
+ <display column="0" version="1" labels="1" columnSpan="1" fontSize="8" stacked="0" rowSpan="1" class="FancyPlotter" unit="" svgBackground="" autoRange="1" row="1" hLines="1" title="GPU Core Clock" vScroll="0" vLines="0" manualRange="0" vDistance="30" hScale="6" showUnit="0">
+  <beam color="0xff0057ae" hostName="127.0.0.1" sensorName="gpu_core_clock" sensorType="integer"/>
  </display>
- <display labels="1" svgBackground="" vScroll="1" version="1" class="FancyPlotter" vLines="1" unit="" row="1" hScale="3" autoRange="1" fontSize="8" manualRange="0" rowSpan="1" columnSpan="1" showUnit="0" vDistance="30" column="1" stacked="0" title="GPU Core Clock" hLines="1">
-  <beam sensorName="gpu_core_clock" sensorType="integer" color="0xff0057ae" hostName="127.0.0.1"/>
+ <display column="1" version="1" labels="1" columnSpan="1" fontSize="8" stacked="0" rowSpan="1" class="FancyPlotter" unit="" svgBackground="" autoRange="1" row="1" hLines="1" title="GPU Memory Clock" vScroll="0" vLines="0" manualRange="0" vDistance="30" hScale="6" showUnit="0">
+  <beam color="0xff0057ae" hostName="127.0.0.1" sensorName="gpu_mem_clock" sensorType="integer"/>
  </display>
- <display labels="1" svgBackground="" vScroll="0" version="1" class="FancyPlotter" vLines="0" unit="" row="2" hScale="3" autoRange="1" fontSize="8" manualRange="0" rowSpan="1" columnSpan="1" showUnit="0" vDistance="30" column="0" stacked="0" title="GPU Memory Usage" hLines="1">
-  <beam sensorName="gpu_mem_mib" sensorType="integer" color="0xff0057ae" hostName="127.0.0.1"/>
+ <display column="0" version="1" labels="1" columnSpan="1" fontSize="8" stacked="0" rowSpan="1" class="FancyPlotter" unit="" svgBackground="" autoRange="1" row="2" hLines="1" title="GPU Memory Load" vScroll="0" vLines="0" manualRange="0" vDistance="30" hScale="6" showUnit="0">
+  <beam color="0xff0057ae" hostName="127.0.0.1" sensorName="gpu_mem_load" sensorType="integer"/>
  </display>
- <display labels="1" svgBackground="" vScroll="0" version="1" class="FancyPlotter" vLines="0" unit="" row="2" hScale="3" autoRange="1" fontSize="8" manualRange="0" rowSpan="1" columnSpan="1" showUnit="0" vDistance="30" column="1" stacked="0" title="GPU Memory Clock" hLines="1">
-  <beam sensorName="gpu_mem_clock" sensorType="integer" color="0xff0057ae" hostName="127.0.0.1"/>
+ <display column="1" version="1" labels="1" columnSpan="1" fontSize="8" stacked="0" rowSpan="1" class="FancyPlotter" unit="" svgBackground="" autoRange="1" row="2" hLines="1" title="GPU Memory Usage" vScroll="0" vLines="0" manualRange="0" vDistance="30" hScale="6" showUnit="0">
+  <beam color="0xff0057ae" hostName="127.0.0.1" sensorName="gpu_mem_mib" sensorType="integer"/>
  </display>
- <display labels="1" svgBackground="" vScroll="1" version="1" class="FancyPlotter" vLines="1" unit="" row="3" hScale="3" autoRange="1" fontSize="8" manualRange="0" rowSpan="1" columnSpan="1" showUnit="0" vDistance="30" column="0" stacked="0" title="GPU Video Decode" hLines="1">
-  <beam sensorName="gpu_video_decode" sensorType="integer" color="0xff0057ae" hostName="127.0.0.1"/>
+ <display column="0" version="1" labels="1" columnSpan="1" fontSize="8" stacked="0" rowSpan="1" class="FancyPlotter" unit="" svgBackground="" autoRange="1" row="3" hLines="1" title="GPU Power Usage" vScroll="0" vLines="0" manualRange="0" vDistance="30" hScale="6" showUnit="0">
+  <beam color="0xff0057ae" hostName="127.0.0.1" sensorName="gpu_pwr_usage" sensorType="integer"/>
  </display>
- <display labels="1" svgBackground="" vScroll="1" version="1" class="FancyPlotter" vLines="1" unit="" row="3" hScale="3" autoRange="1" fontSize="8" manualRange="0" rowSpan="1" columnSpan="1" showUnit="0" vDistance="30" column="1" stacked="0" title="GPU Video Encode" hLines="1">
-  <beam sensorName="gpu_video_encode" sensorType="integer" color="0xff0057ae" hostName="127.0.0.1"/>
+ <display column="1" version="1" labels="1" columnSpan="1" fontSize="8" stacked="0" rowSpan="1" class="FancyPlotter" unit="" svgBackground="" autoRange="1" row="3" hLines="1" title="GPU Fan Speed" vScroll="0" vLines="0" manualRange="0" vDistance="30" hScale="6" showUnit="0">
+  <beam color="0xff0057ae" hostName="127.0.0.1" sensorName="gpu_fan_speed" sensorType="integer"/>
+ </display>
+ <display column="0" version="1" labels="1" columnSpan="1" fontSize="8" stacked="0" rowSpan="1" class="FancyPlotter" unit="" svgBackground="" autoRange="1" row="4" hLines="1" title="GPU Video Load" vScroll="0" vLines="0" manualRange="0" vDistance="30" hScale="6" showUnit="0">
+  <beam color="0xff0057ae" hostName="127.0.0.1" sensorName="gpu_video_decode" sensorType="integer"/>
+  <beam color="0xffe20800" hostName="127.0.0.1" sensorName="gpu_video_encode" sensorType="integer"/>
+ </display>
+ <display column="1" version="1" labels="1" columnSpan="1" fontSize="8" stacked="0" rowSpan="1" class="FancyPlotter" unit="" svgBackground="" autoRange="1" row="4" hLines="1" title="GPU PCIe Usage" vScroll="0" vLines="0" manualRange="0" vDistance="30" hScale="6" showUnit="0">
+  <beam color="0xff0057ae" hostName="127.0.0.1" sensorName="gpu_pcie_rx" sensorType="integer"/>
+  <beam color="0xffe20800" hostName="127.0.0.1" sensorName="gpu_pcie_tx" sensorType="integer"/>
  </display>
 </WorkSheet>

Major rework using the dmon function. Added PCIe RX and TX rates, and Power Usage in watts. Also added GPU selection in the .pl file using a variable.

Patch with patch < dmon.patch and patch < dmon-sgrd.patch

These patches come with NO WARRANTY! Excessive resource use could still be a possibility. You have been warned.

Edit: Found the first problem with this method. The Perl script doesn’t terminate when the program is “shut down.” Therefore KSysGuard remains active until nvidia-gpu-sens is manually terminated… And this is why direct NVML access is better.

Optimized out all dmon calls. Filtering queries was way too resource intensive.

optimized.patch:

--- nvidia-gpu-sensor.pl	2019-02-22 14:25:25.337646000 -0800
+++ nvidia-gpu-sensor-new.pl	2019-02-23 03:20:31.078913041 -0800
@@ -8,11 +8,16 @@
 # 2. Save this ksysguard sensor file for Nvidia: https://gist.github.com/Sporif/31f0d8d9efc3315752aa4031f7080d79
 # 2. In KSysGuard's menu, open "File > Import Tab From File option"
 # 3. Open the sensor file (nvidia.srgd)
+# 4. To change which GPU is monitored, change $GPU
 
 # See Also
 # https://techbase.kde.org/Development/Tutorials/Sensors
 
 $|=1;
+$GPU = 0;
+chomp $GPU;
+$MEM = "".`nvidia-smi -i $GPU --query-gpu=memory.total --format=csv,noheader,nounits`."";
+chomp $MEM;
 
 print "ksysguardd 1.2.0\n";
 print "ksysguardd> ";
@@ -22,66 +27,66 @@
         print "gpu_temp\tinteger\n";
         print "gpu_fan_speed\tinteger\n";
         print "gpu_core_usage\tinteger\n";
+        print "gpu_mem_load\tinteger\n";
         print "gpu_core_clock\tinteger\n";
         print "gpu_mem_mib\tinteger\n";
         print "gpu_mem_clock\tinteger\n";
-        print "gpu_video_decode\tinteger\n";
-        print "gpu_video_encode\tinteger\n";
+        print "gpu_pwr_usage\tinteger\n";
     }
     if(/gpu_temp/){
         if(/\?/){
             print "GPU Temp\t0\t100\t°C\n";
         }else{
-            print `nvidia-smi --query-gpu=temperature.gpu --format=csv,noheader`;
+            print `nvidia-smi -i $GPU --query-gpu=temperature.gpu --format=csv,noheader`;
         }
     }
     if(/gpu_fan_speed/){
         if(/\?/){
             print "GPU Fan Speed\t0\t100\t%\n";
         }else{
-            print `nvidia-smi --query-gpu=fan.speed --format=csv,noheader,nounits`;
+            print `nvidia-smi -i $GPU --query-gpu=fan.speed --format=csv,noheader,nounits`;
         }
     }
     if(/gpu_core_usage/){
         if(/\?/){
             print "GPU Core Usage\t0\t100\t%\n";
         }else{
-            print `nvidia-smi --query-gpu=utilization.gpu --format=csv,noheader,nounits`;
+            print `nvidia-smi -i $GPU --query-gpu=utilization.gpu --format=csv,noheader,nounits`;
         }
     }
-    if(/gpu_core_clock/){
+    if(/gpu_mem_load/){
         if(/\?/){
-            print "GPU Core Clock\t0\t2500\tMhz\n";
+            print "GPU Memory Load\t0\t100\t%\n";
         }else{
-            print `nvidia-smi --query-gpu=clocks.current.graphics --format=csv,noheader,nounits`;
+            print `nvidia-smi -i $GPU --query-gpu=utilization.memory --format=csv,noheader,nounits`;
         }
     }
-    if(/gpu_mem_mib/){
+    if(/gpu_core_clock/){
         if(/\?/){
-            print "GPU Memory Usage\t0\t".`nvidia-smi --query-gpu=memory.total --format=csv,noheader,nounits | perl -pe 'chomp'`."\tMiB\n";
+            print "GPU Core Clock\t0\t\tMhz\n";
         }else{
-            print `nvidia-smi --query-gpu=memory.used --format=csv,noheader,nounits`;
+            print `nvidia-smi -i $GPU --query-gpu=clocks.current.graphics --format=csv,noheader,nounits`;
         }
     }
-    if(/gpu_mem_clock/){
+    if(/gpu_mem_mib/){
         if(/\?/){
-            print "GPU Memory Clock\t0\t8000\tMhz\n";
+            print "GPU Memory Usage\t0\t$MEM\tMiB\n";
         }else{
-            print `nvidia-smi --query-gpu=clocks.current.memory --format=csv,noheader,nounits`;
+            print `nvidia-smi -i $GPU --query-gpu=memory.used --format=csv,noheader,nounits`;
         }
     }
-    if(/gpu_video_decode/){
+    if(/gpu_mem_clock/){
         if(/\?/){
-            print "GPU Decoding\t0\t100\t%\n";
+            print "GPU Memory Clock\t0\t\tMhz\n";
         }else{
-            print `nvidia-smi dmon -c 1 -s u | sed s/#// | awk '{print \$5}' | tail -n 1`;
+            print `nvidia-smi -i $GPU --query-gpu=clocks.current.memory --format=csv,noheader,nounits`;
         }
     }
-    if(/gpu_video_encode/){
+    if(/gpu_pwr_usage/){
         if(/\?/){
-            print "GPU Encoding\t0\t100\t%\n";
+            print "GPU Power Draw\t0\t\tW\n";
         }else{
-            print `nvidia-smi dmon -c 1 -s u | sed s/#// | awk '{print \$4}' | tail -n 1`;
+            print `nvidia-smi -i $GPU --query-gpu=power.draw --format=csv,noheader,nounits`;
         }
     }
     print "ksysguardd> ";

optimized-sgrd.patch:

--- nvidia.sgrd	2019-02-22 14:25:47.092943627 -0800
+++ nvidia-new.sgrd	2019-02-23 03:21:27.104552031 -0800
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE KSysGuardWorkSheet>
-<WorkSheet title="Nvidia" columns="2" rows="4" locked="0" interval="1">
+<WorkSheet title="NVIDIA" columns="2" rows="4" locked="0" interval="1">
  <host name="127.0.0.1" port="-1" shell="" command="nvidia-gpu-sensor.pl"/>
  <display labels="1" svgBackground="" vScroll="1" version="1" class="FancyPlotter" vLines="1" unit="" row="0" hScale="3" autoRange="1" fontSize="8" manualRange="0" rowSpan="1" columnSpan="1" showUnit="0" vDistance="30" column="0" stacked="0" title="GPU Temp" hLines="1">
   <beam sensorName="gpu_temp" sensorType="integer" color="0xff0057ae" hostName="127.0.0.1"/>
@@ -11,8 +11,8 @@
  <display labels="1" svgBackground="" vScroll="1" version="1" class="FancyPlotter" vLines="1" unit="" row="1" hScale="3" autoRange="1" fontSize="8" manualRange="0" rowSpan="1" columnSpan="1" showUnit="0" vDistance="30" column="0" stacked="0" title="GPU Core Usage" hLines="1">
   <beam sensorName="gpu_core_usage" sensorType="integer" color="0xff0057ae" hostName="127.0.0.1"/>
  </display>
- <display labels="1" svgBackground="" vScroll="1" version="1" class="FancyPlotter" vLines="1" unit="" row="1" hScale="3" autoRange="1" fontSize="8" manualRange="0" rowSpan="1" columnSpan="1" showUnit="0" vDistance="30" column="1" stacked="0" title="GPU Core Clock" hLines="1">
-  <beam sensorName="gpu_core_clock" sensorType="integer" color="0xff0057ae" hostName="127.0.0.1"/>
+ <display labels="1" svgBackground="" vScroll="1" version="1" class="FancyPlotter" vLines="1" unit="" row="1" hScale="3" autoRange="1" fontSize="8" manualRange="0" rowSpan="1" columnSpan="1" showUnit="0" vDistance="30" column="1" stacked="0" title="GPU Memory Load" hLines="1">
+  <beam sensorName="gpu_mem_load" sensorType="integer" color="0xff0057ae" hostName="127.0.0.1"/>
  </display>
  <display labels="1" svgBackground="" vScroll="0" version="1" class="FancyPlotter" vLines="0" unit="" row="2" hScale="3" autoRange="1" fontSize="8" manualRange="0" rowSpan="1" columnSpan="1" showUnit="0" vDistance="30" column="0" stacked="0" title="GPU Memory Usage" hLines="1">
   <beam sensorName="gpu_mem_mib" sensorType="integer" color="0xff0057ae" hostName="127.0.0.1"/>
@@ -20,10 +20,10 @@
  <display labels="1" svgBackground="" vScroll="0" version="1" class="FancyPlotter" vLines="0" unit="" row="2" hScale="3" autoRange="1" fontSize="8" manualRange="0" rowSpan="1" columnSpan="1" showUnit="0" vDistance="30" column="1" stacked="0" title="GPU Memory Clock" hLines="1">
   <beam sensorName="gpu_mem_clock" sensorType="integer" color="0xff0057ae" hostName="127.0.0.1"/>
  </display>
- <display labels="1" svgBackground="" vScroll="1" version="1" class="FancyPlotter" vLines="1" unit="" row="3" hScale="3" autoRange="1" fontSize="8" manualRange="0" rowSpan="1" columnSpan="1" showUnit="0" vDistance="30" column="0" stacked="0" title="GPU Video Decode" hLines="1">
-  <beam sensorName="gpu_video_decode" sensorType="integer" color="0xff0057ae" hostName="127.0.0.1"/>
+ <display labels="1" svgBackground="" vScroll="1" version="1" class="FancyPlotter" vLines="1" unit="" row="3" hScale="3" autoRange="1" fontSize="8" manualRange="0" rowSpan="1" columnSpan="1" showUnit="0" vDistance="30" column="0" stacked="0" title="GPU Core Clock" hLines="1">
+  <beam sensorName="gpu_core_clock" sensorType="integer" color="0xff0057ae" hostName="127.0.0.1"/>
  </display>
- <display labels="1" svgBackground="" vScroll="1" version="1" class="FancyPlotter" vLines="1" unit="" row="3" hScale="3" autoRange="1" fontSize="8" manualRange="0" rowSpan="1" columnSpan="1" showUnit="0" vDistance="30" column="1" stacked="0" title="GPU Video Encode" hLines="1">
-  <beam sensorName="gpu_video_encode" sensorType="integer" color="0xff0057ae" hostName="127.0.0.1"/>
+ <display labels="1" svgBackground="" vScroll="1" version="1" class="FancyPlotter" vLines="1" unit="" row="3" hScale="3" autoRange="1" fontSize="8" manualRange="0" rowSpan="1" columnSpan="1" showUnit="0" vDistance="30" column="1" stacked="0" title="GPU Power Usage" hLines="1">
+  <beam sensorName="gpu_pwr_usage" sensorType="integer" color="0xff0057ae" hostName="127.0.0.1"/>
  </display>
 </WorkSheet>

This SHOULD be more optimized… I hope.

Again, Patch with patch < optimized.patch and patch < optimized-sgrd.patch

You should put this in the KDE Store