-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpu-statistics.py
49 lines (42 loc) · 1.24 KB
/
gpu-statistics.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
import os
import json
import time
from mysql_utils import auto_insert_database
host_name = 'hostname' # input your hostname
# input database config
database_config = {
'user': 'user',
'password': 'password',
'host': '0.0.0.0',
'database': 'gpustat',
'raise_on_warnings': True,
'auth_plugin': 'caching_sha2_password'
}
def read_stat():
cmd = 'gpustat -cu --no-color --json'
stat = os.popen(cmd).read()
stat_json = json.loads(stat)
return stat_json
def get_records(stats):
records = []
timestamp = time.time()
for gpu in stats['gpus']:
for process in gpu['processes']:
record = {
'timestamp':timestamp,
'hostname':host_name,
'username':process['username'],
'memory.usage':process['gpu_memory_usage'],
'gpu.index':gpu['index'],
'gpu.name':gpu['name'],
'gpu.memory.total':gpu['memory.total']
}
records.append(record)
return records
def main():
stats = read_stat()
stats_dict = get_records(stats)
for record in stats_dict:
auto_insert_database(database_config, record, table='gpustatistics')
if __name__ == '__main__':
main()