-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdemo_reno.py
203 lines (169 loc) · 8.21 KB
/
demo_reno.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
"""
This demo aims to help player running system quickly by using the pypi library DTP-Emualtor https://pypi.org/project/DTP-Emulator/.
grade:554.24
"""
from simple_emulator import PccEmulator, CongestionControl
# We provided a simple algorithms about packet selection to help you being familiar with this competition.
# In this example, it will select the packet according to packet's created time first and radio of rest life time to deadline secondly.
from simple_emulator import Packet_selection
# We provided some simple algorithms about congestion control to help you being familiar with this competition.
# Like Reno and an example about reinforcement learning implemented by tensorflow
from simple_emulator import Reno
# Ensuring that you have installed tensorflow before you use it
# from simple_emulator import RL
# We provided some function of plotting to make you analyze result easily in utils.py
from simple_emulator import analyze_pcc_emulator, plot_rate
from simple_emulator import constant
from simple_emulator import cal_qoe
EVENT_TYPE_FINISHED='F'
EVENT_TYPE_DROP='D'
EVENT_TYPE_TEMP='T'
# Your solution should include packet selection and congestion control.
# So, we recommend you to achieve it by inherit the objects we provided and overwritten necessary method.
class MySolution(Packet_selection,Reno):
def __init__(self):
# base parameters in CongestionControl
self._input_list = []
self.cwnd = 1
self.send_rate = float("inf")
self.pacing_rate = float("inf")
self.call_nums = 0
self.rev_nums = 0
self.inflight = 0
# for reno
self.ssthresh = float("inf")
self.curr_state = "slow_start"
self.states = ["slow_start", "congestion_avoidance", "fast_recovery"]
self.drop_nums = 0
self.ack_nums = 0
self.cur_time = -1
self.last_cwnd = 0
self.instant_drop_nums = 0
def select_packet(self, cur_time, packet_queue):
"""
The algorithm to select which packet in 'packet_queue' should be sent at time 'cur_time'.
The following example is selecting packet by the create time firstly, and radio of rest life time to deadline secondly.
See more at https://github.com/Azson/DTP-emulator/tree/pcc-emulator#packet_selectionpy.
:param cur_time: float
:param packet_queue: the list of Packet.You can get more detail about Block in objects/packet.py
:return: int
"""
def is_better(packet):
best_block_create_time = best_packet.block_info["Create_time"]
packet_block_create_time = packet.block_info["Create_time"]
best_time_per = (cur_time-best_block_create_time)/best_packet.block_info["Deadline"]
packet_time_per = (cur_time-packet_block_create_time)/packet.block_info["Deadline"]
best_size = 1-best_packet.offset*1480/best_packet.block_info["Size"]
packet_size = 1-packet.offset*1480/packet.block_info["Size"]
# if packet is miss ddl
if (cur_time - packet_block_create_time) >= packet.block_info["Deadline"]:
return False
if (cur_time - best_block_create_time) >= best_packet.block_info["Deadline"]:
return True
#同一个block按序发送
if best_packet.block_info["Block_id"] == packet.block_info["Block_id"]:
return best_packet.offset > packet.offset
else:
if best_packet.block_info["Priority"] == packet.block_info["Priority"]:
return best_time_per * best_size > packet_time_per * packet_size
else:
return best_packet.block_info["Priority"] + best_packet.block_info[
"Deadline"] - cur_time + packet_block_create_time > \
packet.block_info["Priority"] + packet.block_info[
"Deadline"] - cur_time + packet_block_create_time
best_packet_idx = -1
best_packet = None
for idx, item in enumerate(packet_queue):
if best_packet is None or is_better(item):
best_packet_idx = idx
best_packet = item
return best_packet_idx
def make_decision(self, cur_time):
"""
The part of algorithm to make congestion control, which will be call when sender need to send pacekt.
See more at https://github.com/Azson/DTP-emulator/tree/pcc-emulator#congestion_control_algorithmpy.
"""
return super().make_decision(cur_time)
def cc_trigger(self, data):
event_type = data["event_type"]
event_time = data["event_time"]
# self.rev_nums+=1
# self.inflight=self.call_nums-self.rev_nums
if self.cur_time < event_time:
self.last_cwnd = 0
self.instant_drop_nums = 0
if event_type == EVENT_TYPE_DROP:
if self.instant_drop_nums > 0:
return
self.instant_drop_nums += 1
self.curr_state = self.states[2]
self.drop_nums += 1
self.ack_nums = 0
# Ref 1 : For ensuring the event type, drop or ack?
self.cur_time = event_time
if self.last_cwnd > 0 and self.last_cwnd != self.cwnd:
self.cwnd = self.last_cwnd
self.last_cwnd = 0
elif event_type == EVENT_TYPE_FINISHED:
# Ref 1
if event_time <= self.cur_time:
return
self.cur_time = event_time
self.last_cwnd = self.cwnd
self.ack_nums += 1
if self.curr_state == self.states[0]:
if self.ack_nums == self.cwnd:
self.cwnd *= 2
self.ack_nums = 0
if self.cwnd >= self.ssthresh:
self.curr_state = self.states[1]
elif self.curr_state == self.states[1]:
if self.ack_nums == self.cwnd:
self.cwnd += 1
self.ack_nums = 0
if self.curr_state == self.states[2]:
self.ssthresh = max(self.cwnd // 2, 1)
self.cwnd = self.ssthresh
self.curr_state = self.states[1]
def append_input(self, data):
"""
The part of algorithm to make congestion control, which will be call when sender get an event about acknowledge or lost from reciever.
See more at https://github.com/Azson/DTP-emulator/tree/pcc-emulator#congestion_control_algorithmpy.
"""
self._input_list.append(data)
if data["event_type"] != EVENT_TYPE_TEMP:
self.cc_trigger(data)
return {
"cwnd" : self.cwnd,
"send_rate" : self.send_rate
}
return None
if __name__ == '__main__':
# The file path of packets' log
log_packet_file = "output/packet_log/packet-0.log"
# Use the object you created above
my_solution = MySolution()
# Create the emulator using your solution
# Specify USE_CWND to decide whether or not use crowded windows. USE_CWND=True by default.
# Specify ENABLE_LOG to decide whether or not output the log of packets. ENABLE_LOG=True by default.
# You can get more information about parameters at https://github.com/Azson/DTP-emulator/tree/pcc-emulator#constant
emulator = PccEmulator(
block_file=["traces/data_video.csv", "traces/data_audio.csv"],
trace_file="traces/trace.txt",
solution=my_solution,
USE_CWND=True,
ENABLE_LOG=True
)
# Run the emulator and you can specify the time for the emualtor's running.
# It will run until there is no packet can sent by default.
emulator.run_for_dur(15)
# print the debug information of links and senders
emulator.print_debug()
# Output the picture of pcc_emulator-analysis.png
# You can get more information from https://github.com/Azson/DTP-emulator/tree/pcc-emulator#pcc_emulator-analysispng.
analyze_pcc_emulator(log_packet_file, file_range="all")
# Output the picture of rate_changing.png
# You can get more information from https://github.com/Azson/DTP-emulator/tree/pcc-emulator#cwnd_changingpng
plot_rate(log_packet_file, trace_file="traces/trace.txt", file_range="all", sender=[1])
#cal_qoe()
print(cal_qoe())