Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 714b9e9e4d | |||
| 64cc2c57cf |
@ -6,7 +6,7 @@ A naive tool for observing gpu status and auto set visible gpu in python code.
|
|||||||
|
|
||||||
1. install the package.
|
1. install the package.
|
||||||
```shell
|
```shell
|
||||||
pip install https://git.zmy.pub/zmyme/gpuutil/archive/v0.0.4.tar.gz
|
pip install https://git.zmy.pub/zmyme/gpuutil/archive/v0.0.5.tar.gz
|
||||||
```
|
```
|
||||||
|
|
||||||
2. for observing gpu status, just input
|
2. for observing gpu status, just input
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
import curses
|
||||||
from gpuutil import GPUStat, loaddict, savedict
|
from gpuutil import GPUStat, loaddict, savedict
|
||||||
import sys
|
import sys
|
||||||
import argparse
|
import argparse
|
||||||
@ -58,12 +59,13 @@ if __name__ == '__main__':
|
|||||||
recommended_cols = ['ID', 'Fan', 'Temp', 'Pwr', 'Freq', 'Util', 'Vmem']
|
recommended_cols = ['ID', 'Fan', 'Temp', 'Pwr', 'Freq', 'Util', 'Vmem']
|
||||||
|
|
||||||
parser = argparse.ArgumentParser()
|
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('--profile', '-p', default='default', type=str, help='profile keyword, corresponding configuration are saved in ~/.gpuutil.conf')
|
||||||
parser.add_argument('--cols', '-c', type=csv2list, help='colums to show.(Availabel cols: {0}'.format(avaliable_cols))
|
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('--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('--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('--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')
|
parser.add_argument('--save', default=False, action="store_true", help='save config to profile')
|
||||||
|
parser.add_argument('--watch', '-w', default=-1, type=float, help='save config to profile')
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
cols = args.cols if args.cols is not None else recommended_cols
|
cols = args.cols if args.cols is not None else recommended_cols
|
||||||
show_process = args.show_process
|
show_process = args.show_process
|
||||||
@ -90,6 +92,14 @@ if __name__ == '__main__':
|
|||||||
save_config(config)
|
save_config(config)
|
||||||
elif args.profile is not None:
|
elif args.profile is not None:
|
||||||
config = load_config()
|
config = load_config()
|
||||||
|
if 'default' not in config:
|
||||||
|
config['default'] = {
|
||||||
|
"cols": cols,
|
||||||
|
"style": style,
|
||||||
|
"limit": limit,
|
||||||
|
"show-process": show_process,
|
||||||
|
"vertical": vertical
|
||||||
|
}
|
||||||
if args.profile in config:
|
if args.profile in config:
|
||||||
params = config[args.profile]
|
params = config[args.profile]
|
||||||
cols = params["cols"]
|
cols = params["cols"]
|
||||||
@ -103,7 +113,35 @@ if __name__ == '__main__':
|
|||||||
limit = params["limit"]
|
limit = params["limit"]
|
||||||
if "vertical" in params:
|
if "vertical" in params:
|
||||||
vertical = params["vertical"]
|
vertical = params["vertical"]
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise ValueError('Profile do not exist.\nAvaliable Profiles:{0}'.format(','.join(list(config.keys()))))
|
raise ValueError('Profile do not exist.\nAvaliable Profiles:{0}'.format(','.join(list(config.keys()))))
|
||||||
stat.show(enabled_cols = cols, colsty=style, colsz=limit, vertical=vertical, show_command=show_process)
|
info = stat.show(enabled_cols = cols, colsty=style, colsz=limit, vertical=vertical, show_command=show_process, tostdout=False)
|
||||||
|
if args.watch < 0:
|
||||||
|
print(info)
|
||||||
|
else:
|
||||||
|
from curses import wrapper
|
||||||
|
import time
|
||||||
|
def continuous_watch(stdscr, info):
|
||||||
|
curses.curs_set(0)
|
||||||
|
stdscr.clear()
|
||||||
|
stdscr.nodelay(True)
|
||||||
|
lasttime = time.time()
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
c = stdscr.getch()
|
||||||
|
if c in [ord('q'), ord('Q')]:
|
||||||
|
break
|
||||||
|
curses.flushinp()
|
||||||
|
hint = "Interval: {0} S | CurrentTime: {1}".format(args.watch, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
|
||||||
|
stdscr.erase()
|
||||||
|
stdscr.addstr(0, 0, hint + '\n' + info)
|
||||||
|
stdscr.refresh()
|
||||||
|
passed_time = time.time() - lasttime
|
||||||
|
if passed_time < args.watch:
|
||||||
|
time.sleep(args.watch - passed_time)
|
||||||
|
lasttime = time.time()
|
||||||
|
info = stat.show(enabled_cols = cols, colsty=style, colsz=limit, vertical=vertical, show_command=show_process, tostdout=False)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
curses.flushinp()
|
||||||
|
pass
|
||||||
|
wrapper(continuous_watch, info)
|
||||||
@ -378,7 +378,7 @@ class GPUStat():
|
|||||||
gpu['id'] = i
|
gpu['id'] = i
|
||||||
self.gpus.append(gpu)
|
self.gpus.append(gpu)
|
||||||
|
|
||||||
def show(self, enabled_cols = ['ID', 'Fan', 'Temp', 'Pwr', 'Freq', 'Util', 'Vmem', 'Users'], colsty=None, colsz=None, show_command=True, vertical=False):
|
def show(self, enabled_cols = ['ID', 'Fan', 'Temp', 'Pwr', 'Freq', 'Util', 'Vmem', 'Users'], colsty=None, colsz=None, show_command=True, vertical=False, tostdout=True):
|
||||||
self.parse()
|
self.parse()
|
||||||
gpu_infos = []
|
gpu_infos = []
|
||||||
# stats = {
|
# stats = {
|
||||||
@ -450,7 +450,10 @@ class GPUStat():
|
|||||||
pid = proc['pid']
|
pid = proc['pid']
|
||||||
proc['gpu'] = [str(gpu['id'])]
|
proc['gpu'] = [str(gpu['id'])]
|
||||||
if type(proc['vmem']) is str:
|
if type(proc['vmem']) is str:
|
||||||
proc['vmem'] = int(proc['vmem'].split(' ')[0])
|
try:
|
||||||
|
proc['vmem'] = int(proc['vmem'].split(' ')[0])
|
||||||
|
except:
|
||||||
|
proc['vmem'] = 0
|
||||||
if pid not in procs:
|
if pid not in procs:
|
||||||
procs[pid] = proc
|
procs[pid] = proc
|
||||||
else:
|
else:
|
||||||
@ -471,7 +474,9 @@ class GPUStat():
|
|||||||
table_width = info.find('\n')
|
table_width = info.find('\n')
|
||||||
proc_info = draw_table([['Process Info'.center(table_width-4)], [proc_info]], rowsty="c|c|", colsty="|l|", colsz=[table_width-4])
|
proc_info = draw_table([['Process Info'.center(table_width-4)], [proc_info]], rowsty="c|c|", colsty="|l|", colsz=[table_width-4])
|
||||||
info += proc_info
|
info += proc_info
|
||||||
print(info)
|
if tostdout:
|
||||||
|
print(info)
|
||||||
|
return info
|
||||||
|
|
||||||
class MoreGPUNeededError(Exception):
|
class MoreGPUNeededError(Exception):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
|||||||
2
setup.py
2
setup.py
@ -2,7 +2,7 @@ from setuptools import setup, find_packages
|
|||||||
|
|
||||||
setup(
|
setup(
|
||||||
name = 'gpuutil',
|
name = 'gpuutil',
|
||||||
version = '0.0.4',
|
version = '0.0.5',
|
||||||
keywords='gpu utils',
|
keywords='gpu utils',
|
||||||
description = 'A tool for observing gpu stat and auto set visible gpu in python code.',
|
description = 'A tool for observing gpu stat and auto set visible gpu in python code.',
|
||||||
license = 'MIT License',
|
license = 'MIT License',
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user