Skip to content

Commit

Permalink
Display NVIDIA driver version in the header (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
wookayin committed Feb 2, 2019
1 parent 4954cae commit caba47e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
29 changes: 20 additions & 9 deletions gpustat/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,19 +250,25 @@ def jsonify(self):

class GPUStatCollection(object):

def __init__(self, gpu_list):
def __init__(self, gpu_list, driver_version=None):
self.gpus = gpu_list

# attach additional system information
self.hostname = platform.node()
self.query_time = datetime.now()
self.driver_version = driver_version

@staticmethod
def new_query():
"""Query the information of all the GPUs on local machine"""

N.nvmlInit()

def _decode(b):
if isinstance(b, bytes):
return b.decode() # for python3, to unicode
return b

def get_gpu_info(handle):
"""Get one GPU information specified by nvml handle"""

Expand All @@ -284,11 +290,6 @@ def get_process_info(nv_process):
process['pid'] = nv_process.pid
return process

def _decode(b):
if isinstance(b, bytes):
return b.decode() # for python3, to unicode
return b

name = _decode(N.nvmlDeviceGetName(handle))
uuid = _decode(N.nvmlDeviceGetUUID(handle))

Expand Down Expand Up @@ -374,8 +375,14 @@ def _decode(b):
gpu_stat = GPUStat(gpu_info)
gpu_list.append(gpu_stat)

# 2. additional info (driver version, etc).
try:
driver_version = _decode(N.nvmlSystemGetDriverVersion())
except N.NVMLError:
driver_version = None # N/A

N.nvmlShutdown()
return GPUStatCollection(gpu_list)
return GPUStatCollection(gpu_list, driver_version=driver_version)

def __len__(self):
return len(self.gpus)
Expand Down Expand Up @@ -424,15 +431,19 @@ def print_formatted(self, fp=sys.stdout, force_color=False, no_color=False,
if show_header:
time_format = locale.nl_langinfo(locale.D_T_FMT)

header_template = '{t.bold_white}{hostname:{width}}{t.normal} {timestr}' # noqa: E501
header_template = '{t.bold_white}{hostname:{width}}{t.normal} '
header_template += '{timestr} '
header_template += '{t.bold_black}{driver_version}{t.normal}'

header_msg = header_template.format(
hostname=self.hostname,
width=gpuname_width + 3, # len("[?]")
timestr=self.query_time.strftime(time_format),
driver_version=self.driver_version,
t=t_color,
)

fp.write(header_msg)
fp.write(header_msg.strip())
fp.write(eol_char)

# body
Expand Down
1 change: 1 addition & 0 deletions gpustat/test_gpustat.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def _configure_mock(N, Process,
N.nvmlInit = MagicMock()
N.nvmlShutdown = MagicMock()
N.nvmlDeviceGetCount.return_value = 3
N.nvmlSystemGetDriverVersion.return_value = '415.27.mock'

mock_handles = ['mock-handle-%d' % i for i in range(3)]

Expand Down

0 comments on commit caba47e

Please sign in to comment.