-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathriddler_node.py
377 lines (305 loc) · 11.5 KB
/
riddler_node.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import threading
import socket
import time
import copy
import re
import riddler_interface as interface
nodes = []
class node(threading.Thread):
def __init__(self, name, host, port=8899):
super(node, self).__init__(None)
self.name = name
self.host = host
self.port = port
self.mesh_host = ""
self.mesh_port = 8877
self.dests = []
self.enable_ratio = False
self.sources = []
self.samples = []
self.store_samples = False
self.total_cpu = None
self.total_idle = None
self.run_result = None
self.run_error = False
nodes.append(self)
self.end = threading.Event()
self.reply = threading.Event()
# Tell the main loop to stop
def stop(self):
# Stop any loops
self.end.set()
# Free waiters
self.reply.set()
# Close socket for faster quit
if self.socket:
self.socket.close()
# Free waiters
def pause(self):
# Tell the controller that this run should be discarded
self.run_error = True
# Free the controller from waiting
self.reply.set()
# Thread main function
def run(self):
# Thread main loop
while not self.end.is_set():
try:
# Try to connect this node
self.connect()
# Read data from node
self.recv()
except KeyboardInterrupt:
# Someone pressed Ctrl-C
self.stop()
return
except socket.timeout:
# Timed out during connect. Try again
continue
except socket.error as e:
# Something happened to the socket
if e.errno != 0:
# Error number 0 is self made, so don't print it
print("{0}: {1}".format(self.name.title(), e))
if e.errno in (1, 113, 111):
# Connection refused. Wait a bit
time.sleep(5)
# Tell controller that something went wrong
self.run_error = True
self.socket = None
self.reply.set()
# Connect to configured node and start main loop
def connect(self):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Set timeout to 5 seconds to avoid too many reconnects.
# (See exception handler in self.run()
self.socket.settimeout(5)
# Do the connect
self.socket.connect((self.host, self.port))
# Reduce timeout to avoid hanging in socket.read() while quitting
self.socket.settimeout(.1)
# Reconnect to node
def reconnect(self):
# Free waiting processes
self.reply.set()
if self.socket:
# Closing the socket causes exception and reconnect in self.recv()
try:
self.socket.shutdown(socket.SHUT_RDWR)
except socket.error as e:
if e.errno not in (107,9):
# We tried to close a non-connected socket
# Don't be too noisy
raise e
# Keep receiving objects from the node until connection is closed
def recv(self):
# Loop while we are not told otherwise
while not self.end.is_set():
try:
# Wait for data from the node
obj = interface.recv(self.socket)
if obj:
# Pass received object to the handler
self.handle(obj)
else:
# Connection was closed.
# Raise an exception to cause reconnect in self.run()
raise socket.error(0, "Connection closed")
except socket.timeout:
# This happens all the time. Just try again
continue
# Handle objects received from the node
def handle(self, obj):
if obj.cmd is interface.NODE_INFO:
self.handle_node_info(obj)
elif obj.cmd is interface.PREPARE_DONE:
self.handle_prepare_done(obj)
elif obj.cmd is interface.PREPARE_ERROR:
self.handle_prepare_error(obj)
elif obj.cmd is interface.RUN_RESULT:
self.handle_run_result(obj)
elif obj.cmd is interface.RUN_ERROR:
self.handle_run_error(obj)
elif obj.cmd is interface.FINISH_DONE:
self.handle_finish_done(obj)
elif obj.cmd is interface.SAMPLE:
self.handle_sample(obj)
elif obj.cmd is interface.SAMPLE_ERROR:
self.handle_sample_error(obj)
else:
print("Received unknown command from {0}: {1}".format(self.name, obj.cmd))
# Configure a destination node for tests
def add_dest(self, node):
self.dests.append(node)
def add_source(self, node):
self.sources.append(node)
# Return nicely formatted dictionary with destinations of this node
def get_dests(self):
return map(lambda n: {'name': n.name, 'host': n.mesh_host, 'port': n.mesh_port}, self.dests)
# Set if this node should apply the ratio given by a run_info
def set_enable_ratio(self, b):
self.enable_ratio = b
# Return result of current run
def get_result(self):
return self.run_result
# Return received samples from current run
def get_samples(self):
if self.run_error:
print("Sample error from {}".format(self.name))
return None
return self.samples
def get_samples_diff(self):
if self.run_error:
print("Sample error from {}".format(self.name))
return None
samples = {}
for key,val in self.samples[1].items():
samples[key] = val - self.samples[0][key]
return samples
# Wait for node to answer last command
def wait(self):
while not self.end.is_set():
if self.reply.wait(.1):
break
if self.run_error:
print("Wait error from {}".format(self.name))
return self.run_error
# Save information received from node
def handle_node_info(self, obj):
self.mesh_host = obj.mesh_host
self.mesh_port = obj.mesh_port
self.mesh_mac = obj.mesh_mac
self.reply.set()
# Tell the node to prepare a new run
def prepare_run(self, run_info):
# Adjust rate according to ratio, if enabled
run_info = copy.deepcopy(run_info)
if self.enable_ratio and run_info['ratio']:
run_info['rate'] = run_info['rate'] * run_info['ratio']/100
if not self.sources and not self.dests:
# Tell helpers that they are helpers
run_info['role'] = 'helper'
elif not self.sources and self.dests:
run_info['role'] = 'source'
elif self.sources and not self.dests:
run_info['role'] = 'destination'
self.samples = []
self.run_info = run_info
self.run_error = False
self.run_result = None
self.total_cpu = None
self.total_idle = None
self.reply.clear()
interface.send_node(self.socket, interface.PREPARE_RUN, dests=self.get_dests(), run_info=run_info)
# Set event to inform waiting callers
def handle_prepare_done(self, obj):
self.run_error = False
self.reply.set()
# Be verbose about errors occurring during while configuring the node
def handle_prepare_error(self, obj):
self.run_error = True
print("Setup failed at {0}: {1}".format(self.name, obj.error))
# Tell node to start a test run
def start_run(self):
self.reply.clear()
self.store_samples = True
interface.send_node(self.socket, interface.START_RUN)
# Save received result and set event to inform waiting callers
def handle_run_result(self, obj):
if obj.result:
self.run_result = obj.result
# Send result to connected clients
self.client.export_result(self.name, self.run_info, obj.result)
self.reply.set()
# Save received error and set event to inform waiting callers
def handle_run_error(self, obj):
self.run_error = True
print("Run failed on {0}: {1}".format(self.name, obj.error))
self.reply.set()
# Tell node to clean up after test run
def finish_run(self):
self.reply.clear()
interface.send_node(self.socket, interface.FINISH_RUN)
# Set event to inform waiting callers
def handle_finish_done(self, obj):
self.store_samples = False
self.reply.set()
# Save received measurement sample for later extraction
def handle_sample(self, obj):
# Add name to sample
#obj.sample['node'] = self.name
obj = self.parse_nc(obj)
obj = self.parse_iw(obj)
obj = self.parse_cpu(obj)
obj = self.parse_fox(obj)
# Only store sample if a test is running
if self.store_samples:
self.samples.append(obj.sample)
# Send sample to connected clients
self.client.export_sample(self.name, obj.sample)
# Be verbose about sampling errors that not necessarily ruins the run result
def handle_sample_error(self, obj):
self.run_error = True
print("Sampling failed at {0}: {1}".format(self.name, obj.error))
def parse_nc(self, obj):
if not hasattr(obj, 'nc'):
return obj
for line in obj.nc.splitlines():
t = line.split(": ")
key = "bat_" + t[0].lstrip()
val = int(t[1])
obj.sample[key] = val
return obj
def parse_iw(self, obj):
if not hasattr(obj, 'iw'):
return obj
for line in obj.iw.splitlines():
match = re.findall("\s+(.+):\s+(.+)", line)
if not match:
continue
# We want integers to be integers
try:
# Try to convert
val = int(match[0][1])
# Okay, the convert did not fail, so compose the key
key = "iw " + match[0][0]
# Update or set the value for this counter
if key in obj.sample:
obj.sample[key] += val
else:
obj.sample[key] = val
except ValueError:
# The convert failed, so just use the string version
pass
return obj
def parse_cpu(self, obj):
if not hasattr(obj, 'cpu'):
return obj
# Save temporary values for later calculations
last_cpu = self.total_cpu
last_idle = self.total_idle
# Parse the output
line = obj.cpu.split("\n")[0]
self.total_cpu = sum(map(lambda x: float(x), line.split()[1:]))
self.total_idle = float(line.split()[4])
if not last_cpu:
obj.sample['cpu'] = 0
return obj
# Calculate the utilization since last sample
total = self.total_cpu - last_cpu
idle = self.total_idle - last_idle
obj.sample['cpu'] = int(100*(total - idle)/total)
return obj
def parse_fox(self, obj):
if not hasattr(obj, 'fox'):
return obj
for line in obj.fox.splitlines():
t = line.split(": ")
key = "rlnc " + t[0]
val = int(t[1])
if "recoder" in key:
self.run_error = True
print("Sample error on {}: Started acting as recoder".format(self.name))
break
obj.sample[key] = val
return obj