fix a bug which do not show summed vmen in process section and some slight modifications

This commit is contained in:
mingyang 2020-12-14 18:19:49 +08:00
parent 5f57991308
commit 8ae743e166
3 changed files with 30 additions and 14 deletions

1
.gitignore vendored
View File

@ -4,6 +4,7 @@ __pycache__/
*.py[cod]
*$py.class
try.py
.vscode/
# C extensions
*.so

View File

@ -28,6 +28,8 @@ def save_config(config):
# style format: |c|l:15|r|c:14rl:13|
def parse_style(style):
if style is None:
return None, None
components = []
limits = []
while len(style) > 0:
@ -60,14 +62,16 @@ if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--profile', '-p', default=None, type=str, help='profile keyword, corresponding configuration are saved in ~/.gpuutil.conf')
parser.add_argument('--cols', '-c', type=csv2list, help='colums to show')
parser.add_argument('--style', '-sty', type=parse_style, help='column style')
parser.add_argument('--cols', '-c', type=csv2list, help='colums to show.(Availabel cols: {0}'.format(avaliable_cols))
parser.add_argument('--style', '-sty', type=str, default=None, help='column style, format: |c|l:15|r|c:14rl:13|, c,l,r are align methods, | is line and :(int) are width limit.')
parser.add_argument('--show-process', '-sp', default=True, type=str2bool, help='whether show process or not')
parser.add_argument('--vertical', '-v', default=False, type=str2bool, help='whether show each user in different lines. (show user vertically)')
parser.add_argument('--save', default=False, action="store_true", help='save config to profile')
args = parser.parse_args()
cols = args.cols if args.cols is not None else recommended_cols
show_process = args.show_process
style, limit = args.style
style, limit = parse_style(args.style)
vertical = args.vertical
unexpected_cols = []
for col in cols:
if col not in avaliable_cols:
@ -78,9 +82,10 @@ if __name__ == '__main__':
if args.save:
params = {
"cols": cols,
"show-process": show_process,
"style": style,
"limit": limit
"limit": limit,
"show-process": show_process,
"vertical": vertical
}
profile = args.profile if args.profile is not None else input('Please input your profile name:\n>>> ')
config = load_config()
@ -94,11 +99,14 @@ if __name__ == '__main__':
show_process = params["show-process"]
style = None
limit = None
vertical = False
if "style" in params:
style = params["style"]
if "limit" in params:
limit = params["limit"]
if "vertical" in params:
vertical = params["vertical"]
else:
raise ValueError('Profile do not exist.\nAvaliable Profiles:{0}'.format(','.join(list(config.keys()))))
stat.show(enabled_cols = cols, colsty=style, colsz=limit, show_command=show_process)
stat.show(enabled_cols = cols, colsty=style, colsz=limit, vertical=vertical, show_command=show_process)

View File

@ -195,7 +195,7 @@ def draw_table(table, rowsty=None, colsty=None, colsz = None):
for row in table:
for i, col in enumerate(row):
col = str(col)
width = len(col)
width = max([len(c) for c in col.split('\n')])
if colsz[i] is not None and colsz[i] < width:
width = colsz[i]
if width > col_width[i]:
@ -204,7 +204,8 @@ def draw_table(table, rowsty=None, colsty=None, colsz = None):
vline = []
colaligns = []
col_pos = 0
delemeter = ' '
line_delemeter = '-'
content_delemeter = ' '
for ch in colsty:
if ch == '|':
vline.append('+')
@ -212,7 +213,7 @@ def draw_table(table, rowsty=None, colsty=None, colsz = None):
colaligns.append(ch)
vline.append('-' * col_width[col_pos])
col_pos += 1
vline = delemeter.join(vline)
vline = line_delemeter.join(vline)
table_to_draw = []
row_pos = 0
for ch in rowsty:
@ -253,7 +254,7 @@ def draw_table(table, rowsty=None, colsty=None, colsz = None):
elif ch in ['c', 'r', 'l']:
cols_to_drawn.append(row[col_pos])
col_pos += 1
strings.append(delemeter.join(cols_to_drawn))
strings.append(content_delemeter.join(cols_to_drawn))
return '\n'.join(strings)
class GPUStat():
@ -298,7 +299,7 @@ class GPUStat():
gpu['id'] = i
self.gpus.append(gpu)
def show(self, enabled_cols = ['ID', 'Fan', 'Temp', 'Pwr', 'Freq', 'Util', 'Vmem', 'Users'], colsty=None, colsz=None, show_command=True):
def show(self, enabled_cols = ['ID', 'Fan', 'Temp', 'Pwr', 'Freq', 'Util', 'Vmem', 'Users'], colsty=None, colsz=None, show_command=True, vertical=False):
self.parse()
gpu_infos = []
# stats = {
@ -329,7 +330,10 @@ class GPUStat():
if user not in users_process:
users_process[user] = []
users_process[user].append(pid)
process_info = ','.join(process_fmt.format(user=user, pids = '|'.join(users_process[user])) for user in users_process)
delemeter = ','
if vertical:
delemeter = '\n'
process_info = delemeter.join(process_fmt.format(user=user, pids = '|'.join(users_process[user])) for user in users_process)
info_gpu = {
'ID': '{0}'.format(str(gpu['id'])),
'Fan': '{0} %'.format(gpu['fan_speed'].split(' ')[0].strip()),
@ -359,23 +363,26 @@ class GPUStat():
for info in gpu_infos:
this_row = [info[key] for key in enabled_cols]
info_table.append(this_row)
info = draw_table(info_table, rowsty='|c|{0}|'.format('c'*(len(info_table)-1)), colsty=colsty, colsz=colsz)
info = draw_table(info_table, rowsty='|c|{0}|'.format('c'*(len(info_table)-1)), colsty=colsty, colsz=colsz) + '\n'
if show_command:
procs = {}
for gpu in self.gpus:
for proc in gpu['processes']:
pid = proc['pid']
proc['gpu'] = [str(gpu['id'])]
if type(proc['vmem']) is str:
proc['vmem'] = int(proc['vmem'].split(' ')[0])
if pid not in procs:
procs[pid] = proc
else:
procs[pid]['gpu'].append(str(gpu['id']))
procs[pid]['vmem'] += proc['vmem']
proc_fmt = '[{pid}|{gpus}] {user}({vmem} MiB) {cmd}'
proc_strs = []
for pid in procs:
this_proc_str = proc_fmt.format(
user = procs[pid]['user'],
vmem = procs[pid]['vmem'].split(' ')[0],
vmem = procs[pid]['vmem'],
pid = procs[pid]['pid'].rjust(5),
cmd = procs[pid]['command'],
gpus = ','.join(procs[pid]['gpu'])