Skip to content

Commit caba47e

Browse files
committed
Display NVIDIA driver version in the header (wookayin#53)
1 parent 4954cae commit caba47e

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

gpustat/core.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -250,19 +250,25 @@ def jsonify(self):
250250

251251
class GPUStatCollection(object):
252252

253-
def __init__(self, gpu_list):
253+
def __init__(self, gpu_list, driver_version=None):
254254
self.gpus = gpu_list
255255

256256
# attach additional system information
257257
self.hostname = platform.node()
258258
self.query_time = datetime.now()
259+
self.driver_version = driver_version
259260

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

264265
N.nvmlInit()
265266

267+
def _decode(b):
268+
if isinstance(b, bytes):
269+
return b.decode() # for python3, to unicode
270+
return b
271+
266272
def get_gpu_info(handle):
267273
"""Get one GPU information specified by nvml handle"""
268274

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

287-
def _decode(b):
288-
if isinstance(b, bytes):
289-
return b.decode() # for python3, to unicode
290-
return b
291-
292293
name = _decode(N.nvmlDeviceGetName(handle))
293294
uuid = _decode(N.nvmlDeviceGetUUID(handle))
294295

@@ -374,8 +375,14 @@ def _decode(b):
374375
gpu_stat = GPUStat(gpu_info)
375376
gpu_list.append(gpu_stat)
376377

378+
# 2. additional info (driver version, etc).
379+
try:
380+
driver_version = _decode(N.nvmlSystemGetDriverVersion())
381+
except N.NVMLError:
382+
driver_version = None # N/A
383+
377384
N.nvmlShutdown()
378-
return GPUStatCollection(gpu_list)
385+
return GPUStatCollection(gpu_list, driver_version=driver_version)
379386

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

427-
header_template = '{t.bold_white}{hostname:{width}}{t.normal} {timestr}' # noqa: E501
434+
header_template = '{t.bold_white}{hostname:{width}}{t.normal} '
435+
header_template += '{timestr} '
436+
header_template += '{t.bold_black}{driver_version}{t.normal}'
437+
428438
header_msg = header_template.format(
429439
hostname=self.hostname,
430440
width=gpuname_width + 3, # len("[?]")
431441
timestr=self.query_time.strftime(time_format),
442+
driver_version=self.driver_version,
432443
t=t_color,
433444
)
434445

435-
fp.write(header_msg)
446+
fp.write(header_msg.strip())
436447
fp.write(eol_char)
437448

438449
# body

gpustat/test_gpustat.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def _configure_mock(N, Process,
4444
N.nvmlInit = MagicMock()
4545
N.nvmlShutdown = MagicMock()
4646
N.nvmlDeviceGetCount.return_value = 3
47+
N.nvmlSystemGetDriverVersion.return_value = '415.27.mock'
4748

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

0 commit comments

Comments
 (0)