-
Notifications
You must be signed in to change notification settings - Fork 0
/
receiver.py
104 lines (89 loc) · 3.64 KB
/
receiver.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
"""
Module: Receiver
Desp: Basic receiver with buffer for stats collection
version: 0.0.2
requirements: NIL
Changelog: 0.0.1 - inital release
0.0.2 - added more print stat functions
"""
import numpy as np
class BaseReceiver:
def __init__(self, id):
self.id = id
self.number_of_packet_received = 0
def store(self, packet):
self.number_of_packet_received += 1
def print_stat(self, verbose, fout, print_output):
if self.number_of_packet_received > 0:
string = "--------------------Router %d--------------------\n" % self.id
string += "number_of_packet_received =%d\n" % self.number_of_packet_received
if print_output:
print(string, end="")
fout.write(string)
class PacketReceiver(BaseReceiver):
def __init__(self, id):
super().__init__(id)
self.local_storage = []
self.average_clock_taken = None
def store(self, packet):
super().store(packet)
self.local_storage.append(packet)
def is_empty(self):
return not self.local_storage
def print_stat(self, verbose, fout, print_output):
super().print_stat(verbose, fout, print_output)
self.print_packet_stat(verbose, fout, print_output)
def print_packet_stat(self, verbose, fout, print_output):
clocks_taken = []
clock_taken_by_source = {}
if self.local_storage:
for pkt in self.local_storage:
# for average calculations
clocks_taken.append(pkt.clock_cycle_taken)
try:
current_list = clock_taken_by_source[pkt.source_id]
current_list.append(pkt.clock_cycle_taken)
clock_taken_by_source.update({pkt.source_id: current_list})
except KeyError:
clock_taken_by_source.update(
{pkt.source_id: [pkt.clock_cycle_taken]}
)
if verbose >= 2:
pkt_string = "Pkt source: %2d, Dest: %s, clk:%4d, path:%s\n" % (
pkt.source_id,
str(pkt.dest_coordinates),
pkt.clock_cycle_taken,
str(pkt.path_trace),
)
fout.write(pkt_string)
if print_output:
print(pkt_string, end="")
if verbose >= 1:
avg_string = "avg pkt latency (cycles) = %.2f\n" % np.average(
clocks_taken
)
for source in clock_taken_by_source:
avg_string += (
"avg pkt latency from router %d = %.2f in %d packets\n"
% (
source,
np.average(clock_taken_by_source[source]),
len(clock_taken_by_source[source]),
)
)
if print_output:
print(avg_string, end="")
fout.write(avg_string)
def heatmap_collection(self):
heatmap = {}
if self.local_storage:
for pkt in self.local_storage: # for every pkt
pkt.path_trace.pop(0) # remove the source id
for router in pkt.path_trace: # for every place it been to
# increment heatmap
try:
current_val = heatmap[router]
heatmap.update({router: (current_val + 1)})
except KeyError:
heatmap.update({router: 1})
return heatmap