@@ -3,7 +3,7 @@ from __future__ import print_function
3
3
from __future__ import division
4
4
from __future__ import absolute_import
5
5
6
- from py3nvml .py3nvml import *
6
+ from py3nvml .py3nvml import *
7
7
from datetime import datetime
8
8
import re
9
9
import os
@@ -12,20 +12,20 @@ from subprocess import Popen, PIPE
12
12
import argparse
13
13
from time import sleep
14
14
import sys
15
+ from contextlib import contextmanager
15
16
16
17
parser = argparse .ArgumentParser (description = 'Print GPU stats' )
17
- parser .add_argument ('-l' , '--loop' , action = 'store' , type = int ,
18
- default = 0 , help = 'Loop period' )
19
- parser .add_argument ('-f' , '--full' , action = 'store_true' ,
20
- help = 'Print extended version' )
21
- parser .add_argument ('-w' , '--width' , type = int , default = 77 ,
22
- help = 'Print width' )
18
+ parser .add_argument ('-l' , '--loop' , action = 'store' , type = int , default = 0 , help = 'Loop period' )
19
+ parser .add_argument ('-f' , '--full' , action = 'store_true' , help = 'Print extended version' )
20
+ parser .add_argument ('-w' , '--width' , type = int , default = 77 , help = 'Print width' )
21
+ parser .add_argument ('--left' , action = 'store_true' , help = 'Prints left part of process name' )
23
22
24
23
COL1_WIDTH = 33
25
24
COL2_WIDTH = 21
26
25
COL3_WIDTH = 21
27
26
WIDTH = 77
28
27
LONG_FORMAT = False
28
+ LEN_PROCESS_LESS_NAME = 51
29
29
30
30
gpu_format_col1 = '| {:>3} {:3} {:>5} {:>4} {:>11}|'
31
31
gpu_format_col2 = ' {:>19} |'
@@ -76,6 +76,7 @@ def print_proc_header():
76
76
print ('+' + '=' * args .width + '+' )
77
77
return 6
78
78
79
+
79
80
def enabled_str (x ):
80
81
if x == 'Enabled' :
81
82
return 'On'
@@ -123,7 +124,6 @@ def print_gpu_info(index, long_format=False):
123
124
print (gpu_format_col3 .format ('' , '' ))
124
125
return 1
125
126
126
-
127
127
min_number = try_get_info (nvmlDeviceGetMinorNumber , h )
128
128
prod_name = try_get_info (nvmlDeviceGetName , h )
129
129
pers_mode = try_get_info (nvmlDeviceGetPersistenceMode , h , 0 )
@@ -199,10 +199,13 @@ def print_gpu_info(index, long_format=False):
199
199
return n
200
200
201
201
202
- def cut_proc_name (name , maxlen ):
202
+ def cut_proc_name (name , maxlen , left = False ):
203
203
if len (name ) > maxlen :
204
204
# return '...' + name[-maxlen+3:]
205
- return name [:maxlen - 3 ] + '...'
205
+ if left :
206
+ return name [:maxlen - 2 ] + '..'
207
+ else :
208
+ return '..' + name [- maxlen + 2 :]
206
209
else :
207
210
return name
208
211
@@ -240,7 +243,7 @@ def get_uptime(pid):
240
243
return time
241
244
242
245
243
- def main (full = False ):
246
+ def main (full = False , left = False ):
244
247
num_lines = 0
245
248
driver_version = nvmlSystemGetDriverVersion ()
246
249
header_lines = print_header (driver_version , full )
@@ -276,7 +279,7 @@ def main(full=False):
276
279
uptime = get_uptime (p .pid )
277
280
print (proc_format .format (
278
281
min_number , uname , p .pid , uptime ,
279
- cut_proc_name (procname , args .width - 50 ),
282
+ cut_proc_name (procname , args .width - LEN_PROCESS_LESS_NAME , left ),
280
283
p .usedGpuMemory >> 20 , 'MiB' ))
281
284
proc_lines += 1
282
285
print ('+' + '-' * args .width + '+' )
@@ -290,22 +293,20 @@ def main(full=False):
290
293
291
294
if __name__ == '__main__' :
292
295
args = parser .parse_args ()
293
- proc_format = '| {:>3} {:>11} {:>5 } {:>11 } {: <' + str (args .width - 50 ) + '} {:>5}{:3<} |'
296
+ proc_format = '| {:>3} {:>11} {:>7 } {:>10 } {: <' + str (args .width - LEN_PROCESS_LESS_NAME ) + '} {:>5}{:3<} |'
294
297
nvmlInit ()
295
- print_lines = main (args .full )
298
+ print_lines = main (args .full , args . left )
296
299
297
300
if args .loop > 0 :
298
301
try :
299
302
while True :
300
303
sleep (args .loop )
301
304
sys .stdout .write ("\033 [F" * print_lines )
302
- print_lines_new = main (args .full )
305
+ print_lines_new = main (args .full , args . left )
303
306
if print_lines_new < print_lines :
304
307
sys .stdout .write ((' ' * (args .width + 2 )+ '\n ' )* (print_lines - print_lines_new ))
305
308
sys .stdout .write ("\033 [F" * (print_lines - print_lines_new ))
306
309
print_lines = print_lines_new
307
310
except KeyboardInterrupt :
308
311
pass
309
312
nvmlShutdown ()
310
-
311
-
0 commit comments