-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreport.py
More file actions
190 lines (126 loc) · 5.3 KB
/
report.py
File metadata and controls
190 lines (126 loc) · 5.3 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import threading
import heapq
import time
import os
import json
import requests
import psutil
class Reporter(threading.Thread):
def __init__(self):
threading.Thread.__init__(
self, daemon=True
)
self._manager = None
self._collect_period = 0.5
self._report_period = 5
self._schedule = []
self._sys_info = []
self._proc_info = []
self._packet_info = []
self._target_addr = None
self._target_port = None
self._terminating = threading.Event()
def set_manager(self, manager):
self._manager = manager
def add_collector(self, addr, port):
self._target_addr = addr
self._target_port = port
def set_collect_period(self, period):
self._collect_period = period
def set_report_period(self, period):
self._report_period = period
def collect_info(self):
self.collect_sys_info()
self.collect_proc_info()
self.collect_packet_info()
schedule_time = time.time() + self._collect_period
heapq.heappush(self._schedule, (schedule_time, self.collect_info))
def collect_sys_info(self):
report = {}
report['timestamp'] = time.time()
report['cpu_util'] = psutil.cpu_percent()
mem_info = psutil.virtual_memory()
report['mem_total'] = mem_info[0] >> 20
report['mem_util'] = mem_info[2]
report['gpu_info'] = [
{
'gpu_id': int(info[0]) if info[0].isdigit() else None,
'gpu_power': int(info[1]) if info[1].isdigit() else None,
'gpu_util': int(info[4]) if info[4].isdigit() else None,
'gpu_mem_util': int(info[5]) if info[5].isdigit() else None,
'gpu_clock': int(info[9]) if info[9].isdigit() else None
} for info in [line.split() for line in os.popen(
'nvidia-smi dmon -c 1'
).read().strip().split('\n')[2:] if not len(line) == 0]
]
self._sys_info.append(report)
def collect_proc_info(self):
assert self._manager is not None
gpu_info = [
{
'pid': int(info[1]) if info[1].isdigit() else None,
'gpu_id': int(info[0]) if info[0].isdigit() else None,
'gpu_util': int(info[3]) if info[3].isdigit() else None,
'gpu_mem_util': int(info[4]) if info[4].isdigit() else None
} for info in [line.split() for line in os.popen(
'nvidia-smi pmon -c 1'
).read().strip().split('\n')[2:] if not len(line) == 0]
]
for s in self._manager.get_sessions():
for h in s.get_handlers():
report = {}
report['timestamp'] = time.time()
report['session_id'] = s.id
report['handler_id'] = h.id
p = psutil.Process(h.pid)
# TODO: handle handlers with subprocesses
assert not p.children()
report['cpu_util'] = p.cpu_percent()
report['mem_util'] = p.memory_percent()
report['gpu_info'] = [
{
'gpu_id': info['gpu_id'],
'gpu_util': info['gpu_util'],
'gpu_mem_util': info['gpu_mem_util']
} for info in gpu_info if info['pid'] == h.pid
]
self._proc_info.append(report)
def collect_packet_info(self):
for s in self._manager.get_sessions():
sink = s.get_report_sink()
while not sink.empty():
report = sink.get()
report['session_id'] = s.id
self._packet_info.append(report)
def send_report(self):
assert self._target_addr is not None
assert self._target_port is not None
url = 'http://' + self._target_addr + ':' + str(self._target_port)
payload = {'sys_info': self._sys_info,
'proc_info': self._proc_info,
'packet_info': self._packet_info}
try:
response = requests.post(
url, data=json.dumps(payload), timeout=0.5
).json()
if not response['result'] == 'Succeeded':
print('Warning: report post failed (%s)' % response['reason'])
except requests.exceptions.RequestException:
print('Warning: timeout sending report. skipped.')
else:
self._sys_info, self._proc_info, self._packet_info = [], [], []
schedule_time = time.time() + self._report_period
heapq.heappush(self._schedule, (schedule_time, self.send_report))
def run(self):
schedule_time = time.time() + self._collect_period
heapq.heappush(self._schedule, (schedule_time, self.collect_info))
schedule_time = time.time() + self._report_period
heapq.heappush(self._schedule, (schedule_time, self.send_report))
while not self._terminating.is_set():
schedule_time, task = self._schedule[0]
if time.time() >= schedule_time:
task()
heapq.heappop(self._schedule)
time.sleep(0.01)
def stop(self):
self._terminating.set()