CAPTCHA
Image CAPTCHA
Enter the characters shown in the image.
This question is for testing whether or not you are human.
  • Create new account
  • Reset your password

User account menu

Home
The Hyperlogos
Read Everything

Main navigation

  • Home
  • My Resumé
  • blog
  • Howtos
  • Pages
  • Contact
  • Search

nvstats.pl - pretty nvidia GPU stats for Unix

Breadcrumb

  • Home
  • nvstats.pl - pretty nvidia GPU stats for Unix

I wanted to have a nicer view of nvidia GPU statistics, so I made this script. I realize this is overwrought compared to just stuffing the output into a couple of arrays, but I had the code lying around to munge into this. FWIW Text::CSV is now a wrapper around Text::CSV_XS, so this is now considered to be the best and fastest way to parse CSV, which would matter if you had to do a lot of it.

#!/usr/bin/perl
#
# nvstats.pl - retrieve nvidia GPU utilization data using nvidia-smi
#
# run repeatedly for monitoring (e.g. watch -n 0.5 nvstats.pl)
#
# use nvidia-smi --help-query-gpu for a list of possible queries 
# in your version.
#
# supply an alternate query string on the command line
# (e.g. nvstats.pl timestamp,name,utilization.gpu,utilization.memory)

use Text::CSV_XS;
my $csv = Text::CSV_XS->new({binary => 1});

my $nvidia_smi_query = ($ARGV[0] || 'timestamp,name,utilization.gpu,utilization.memory,uuid,driver_version,vbios_version,inforom.img,inforom.oem,memory.total,memory.reserved,memory.used,memory.free,fan.speed,temperature.gpu,compute_mode,compute_cap,encoder.stats.sessionCount,encoder.stats.averageFps,encoder.stats.averageLatency,power.management,pstate,power.draw,power.limit,enforced.power.limit,power.default_limit,power.max_limit,clocks.current.graphics,clocks.max.graphics,clocks.current.sm,clocks.max.sm,clocks.current.video,clocks.current.memory,clocks.max.memory,clocks_throttle_reasons.gpu_idle,clocks_throttle_reasons.applications_clocks_setting,clocks_throttle_reasons.sw_power_cap,clocks_throttle_reasons.hw_slowdown,clocks_throttle_reasons.hw_thermal_slowdown,clocks_throttle_reasons.hw_power_brake_slowdown,clocks_throttle_reasons.sw_thermal_slowdown,clocks_throttle_reasons.sync_boost,pcie.link.gen.current,pcie.link.gen.gpucurrent,pcie.link.width.current');
my $nvidia_smi_command = "nvidia-smi --query-gpu=$nvidia_smi_query --format=csv";

open my $fh, "-|", $nvidia_smi_command or die "can't run nvidia-smi: $!";

my $line = 0;

while (my $row = $csv->getline ($fh)) {
    my $column = 0;
    my @fields = @$row;
    my $fieldcount = @fields;
    until ( $column >= $fieldcount ) {
        if ( $line == 0 ) {
            ( $columns[$column]{'description'} = ( $fields[$column] || '(none)') ) =~ s/^\ //;
        } else {
            ( $columns[$column]{'content'} = ( $fields[$column] || '(empty)') ) =~ s/^\ //;
        }
        $column++;
    }
    $line++;
}

foreach my $column ( keys(@columns) ) {
  print "$columns[$column]{'description'}: $columns[$column]{'content'}\n";
}

 

perl
nVidia
  • Log in or register to post comments

Footer menu

  • Contact
Powered by Drupal

Copyright © 2025 Martin Espinoza - All rights reserved