forked from carlosrisma/OpenCDA
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgpu_monitor.py
More file actions
65 lines (50 loc) · 2.36 KB
/
Copy pathgpu_monitor.py
File metadata and controls
65 lines (50 loc) · 2.36 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
import subprocess
import time
def get_gpu_usage():
# Command to get GPU usage
cmd = "nvidia-smi --query-compute-apps=pid,used_memory --format=csv,noheader"
# Execute the command
result = subprocess.run(cmd, shell=True, text=True, capture_output=True)
if result.returncode == 0:
return result.stdout.strip()
else:
raise Exception("Failed to execute nvidia-smi")
def get_user_by_pid(pid):
# Command to get user owning the PID
cmd = f"ps -o user= -p {pid}"
# Execute the command
result = subprocess.run(cmd, shell=True, text=True, capture_output=True)
if result.returncode == 0 and result.stdout.strip():
return result.stdout.strip()
else:
return "Unknown or No User"
def main():
users = ["alamin", "burrello", "carletti", "daghero", "edadmin", "jahier", "motetti", "ponzio", "tredese",
"benfenati", "cannizzaro", "carlucci", "depaoli", "fiorin", "lezzoche", "mascolini", "pollo", "risso"]
total_memory = 24564 # This should be dynamic or configured externally
with open("gpu_usage.csv", "w") as output_file:
output_file.write("time,GPU0 memory used,GPU1 memory used,GPU2 memory used,"
"GPU3 memory used,total memory used [%]," + ",".join(users) + "\n")
try:
while True:
measure = get_gpu_usage()
usage = [{"pid": m.split(",")[0], "memory": m.split(",")[1].replace(" MiB", "")}
for m in measure.split("\n") if m]
pid_user_map = {u['pid']: get_user_by_pid(u['pid']) for u in usage}
gpu_memory = [int(u['memory']) for u in usage[:4]]
new_line = f"{time.strftime('%Y-%m-%d %H:%M:%S')},{','.join(map(str, gpu_memory))},"
total = sum(gpu_memory)
new_line += f"{total / total_memory * 100},"
for user in users:
user_memory = sum(int(u['memory']) for u in usage if pid_user_map.get(u['pid']) == user)
new_line += f"{user_memory},"
output_file.write(new_line[:-1] + "\n")
output_file.flush()
print(new_line[:-1])
time.sleep(5)
except KeyboardInterrupt:
print("Monitoring stopped.")
except Exception as e:
print(e)
if __name__ == "__main__":
main()