-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.py
70 lines (64 loc) · 2.19 KB
/
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
# Copyright 2024 The ScheduleSimPy Authors. All rights reserved.
# Use of this source code is governed by a Apache2.0-style
# license that can be found in the LICENSE file.
#
# Time : 2024/6/13 15:49
# Author : linyf49@qq.com
# File : monitor.py
import json
import numpy as np
import matplotlib.pyplot as plt
class Monitor(object):
def __init__(self, env, scheduler_name: str):
self.env = env
self.simulator = None
self.cluster = None
self.event_file = scheduler_name + "_event.txt"
self.events = []
def attach(self, simulator):
self.simulator = simulator
self.cluster = simulator.cluster
def run(self):
cpus = []
mems = []
gpus = []
while not self.simulator.finished:
state = {
"timestamp": self.env.now,
"cluster_state": self.cluster.state,
}
cpus.append(self.cluster.cpu_utilization)
mems.append(self.cluster.mem_utilization)
gpus.append(self.cluster.gpu_utilization)
self.events.append(state)
yield self.env.timeout(0.2)
# final state
state = {
"timestamp": self.env.now,
"cluster_state": self.cluster.state,
}
self.events.append(state)
avg_utilization = {
"avg_cpu_utilization:": "{:.2f}%".format(sum(cpus) / len(cpus)),
"avg_mem_utilization:": "{:.2f}%".format(sum(mems) / len(mems)),
"avg_gpu_utilization:": "{:.2f}%".format(sum(gpus) / len(gpus)),
}
self.events.append(avg_utilization)
self.write_to_file()
# self.draw(cpus, mems)
def write_to_file(self):
with open(self.event_file, "w") as f:
json.dump(self.events, f, indent=4)
@staticmethod
def draw(cpus, mems):
final_time = len(cpus) - 1
tm = [i for i in range(final_time + 1)]
x = np.arange(20, 350)
l = plt.plot(tm, cpus, "g--", label="cpu")
plt.plot(tm, cpus, "g-")
plt.title("CPU Utilization")
plt.xlabel("timestamp")
plt.ylabel("cluster_cpu_utilization")
plt.ylim((0, 100))
plt.legend()
plt.show()