-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpu_monitor.py
executable file
·109 lines (84 loc) · 2.59 KB
/
gpu_monitor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#! /usr/bin/env python3
import sys
import argparse
import logging as lg
import fileinput as Fi
from pathlib import Path
from time import time
class Output(object) :
def __init__(self, filename, mode='w') :
if str(filename) == '-' :
self.outstream = sys.stdout
else :
self.outstream = Path(filename).open(mode)
lg.debug('Output: self.outstream set.')
def __enter__(self) :
return self.outstream
def __exit__(self, exc_type, exc_value, traceback) :
self.outstream.close()
def cli_args() :
parser = argparse.ArgumentParser(
description="Parse `nvidia-smi' and write"
" a line of log for each gpu.",
formatter_class=(
argparse.ArgumentDefaultsHelpFormatter
),
)
parser.add_argument("-v", "--verbose", action="store_true",
help="Verbose logging.")
parser.add_argument("-i", "--input", default='-',
metavar='PATH',
help="Input Filename")
parser.add_argument("-o", "--output", default='-',
metavar='PATH',
help="Output Filename")
return parser.parse_args()
def main() :
args = cli_args()
if args.verbose : lg.getLogger().setLevel(lg.DEBUG)
lg.debug('Args: %s', args)
L = [l.rstrip('\n') for l in Fi.input(args.input)]
lg.debug('L: \n%s', '\n'.join(L))
data, datum = [], []
for l in L :
line_data = [d for d in l.split(' ') if d]
if len(line_data) < 2 :
if datum : data.append(datum)
datum = []
continue
lg.debug('line_data: %s', line_data)
if not line_data[1].endswith('%') :
lg.debug('not line_data[1].endswith("%%"): %s',
line_data)
try :
gpu_id = int (line_data[1])
except :
continue
lg.debug('gpu_id: %s', gpu_id)
datum.extend((time(), gpu_id))
continue
lg.debug('line_data[1].endswith("%%"): %s',
line_data)
try :
fan = float(line_data[1].rstrip('%'))
temp = float(line_data[2].rstrip('C'))
pwr = float(line_data[4].rstrip('W'))
mem = float(line_data[8].rstrip('MiB'))
gpu_util = float(line_data[12].rstrip('%'))
except :
raise
lg.debug('fan:%s tmp:%s pwr:%s mem:%s gpu:%s',
fan, temp,pwr,mem,gpu_util)
datum.extend((fan, temp,pwr,mem,gpu_util))
data = '\n'.join(
' '.join(
('%s'% v) for v in d
) for d in data
)
lg.debug('data: \n%s', data)
with Output(args.output, 'a') as F :
F.write(data)
F.write('\n')
if __name__ == '__main__' :
lg.basicConfig(level=lg.INFO, format='%(levelname)-8s: %(message)s')
main()