-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_json.py
More file actions
72 lines (61 loc) · 2.1 KB
/
Copy pathparse_json.py
File metadata and controls
72 lines (61 loc) · 2.1 KB
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
import os
import sys
import csv
import json
HEADERS = [
"datetime",
"gpu_name",
"usage_percent",
"memory",
"memory_percent",
"energy",
"temperature",
"fan_speed",
"users",
"processes"
]
def parse(filename):
indices = {}
listfile = os.path.join("data", "list.json")
if os.path.exists(listfile):
with open(listfile, "r") as f:
indices = {gpu: idx for (idx, gpu) in enumerate(json.load(f)["gpus"])}
try:
with open(filename) as f:
data = json.load(f)
except FileNotFoundError as e:
print("WARNING: json file %s could not be found, skipping run..." % filename, file=sys.stderr)
exit(1)
listgpus = []
for gpu in data["gpus"]:
listgpus.append(gpu["uuid"])
row = {
"datetime": data["query_time"],
"gpu_name": "%s #%s" % (gpu["name"], indices[gpu["uuid"]] or gpu["index"]),
"usage_percent": gpu["utilization.gpu"],
"memory": gpu["memory.used"],
"memory_percent": round(100 * gpu["memory.used"] / gpu["memory.total"], 3),
"energy": gpu["power.draw"],
"temperature": gpu["temperature.gpu"],
"fan_speed": gpu["fan.speed"],
"users": "§".join([p["username"] for p in gpu["processes"] or []]),
"processes": "§".join([" ".join(p["full_command"]) for p in gpu["processes"] or []])
}
csvfilename = os.path.join("data", "%s_%s.csv" % (gpu["uuid"], row["datetime"][:7]))
if not os.path.exists(csvfilename):
with open(csvfilename, "w") as f:
writer = csv.writer(f)
writer.writerow(HEADERS)
with open(csvfilename, "a") as f:
writer = csv.writer(f)
writer.writerow([row[h] for h in HEADERS])
listfile = os.path.join("data", "list.json")
if not os.path.exists(listfile):
with open(listfile, "w") as f:
json.dump({
"gpus": listgpus,
"start": data["query_time"]
}, f)
if __name__ == "__main__":
FILE = sys.argv[1]
parse(FILE)