-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathATER.py
More file actions
186 lines (161 loc) · 8.46 KB
/
Copy pathATER.py
File metadata and controls
186 lines (161 loc) · 8.46 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
from live_reader import LiveReader
from trace_parser import TraceParser
import frequency_sender
import calculator
from indicators import Indicators, REGULATION_TYPE
import time
import sys
url = sys.argv[1]
BASELINE = sys.argv[2]
if BASELINE == 'True':
print("==========================================================================")
print("================================ Baseline ================================")
BASELINE = True
else:
BASELINE = False
print("==========================================================================")
print("================================== ATER ==================================")
outfile1 = sys.argv[3]
file_saving_drops = open(outfile1, 'w')
outfile2 = sys.argv[4]
file_saving_cputime = open(outfile2, 'w')
outfile3 = sys.argv[5]
file_saving_throughput = open(outfile3, 'w')
outfile4 = sys.argv[6]
file_saving_execution_time = open(outfile4, 'w')
arch_file = './arch/architecture.yaml'
##############################
##### Parameters to be tuned
##############################
Delta_t = 8 # sampling period
varphi = 1 # expected sampling periods for regulation
sigma = 1
imdi_theta = 0.05 # theta
pmdi_w = 3 # w for PMDI, not used
ieri_lambda = 0.8 # lambda
buffer_sizes = [1,1,1,1]
##############################
live_reader = LiveReader(url)
live_reader.start_consume()
trace_data = []
epoch = 0
pre_position = None
pre_reduced_frequency = 0
pre_increased_frequency = float('inf')
pre_regulation_type = REGULATION_TYPE.NOACT
time.sleep(Delta_t) # the initial sleep time shold not be too small to prepare the trace data
trace_data = live_reader.get_trace_data()
if len(trace_data):
print("Warning: Trace data set is not empty (epoch: 1)!\n")
else:
print("Warning: Trace data set is empty (epoch: 1)!\n")
print("\n\nEpoch {}: analyzing ...".format(epoch+1))
trace_parser = TraceParser(arch_file, trace_data, buffer_sizes)
trace_parser.analyze_indicators()
Nd, Np, Ns, min_sub_period, avg_sub_period, Nb, throughput = trace_parser.get_message_indicators()
current_sub_period, pw_time, max_sub_period = trace_parser.get_callback_indicators()
print("==================================")
print("The number of drop messages: ", Nd)
print("The number of pub messages: ", Np)
print("The number of sub messages: ", Ns)
print("Number of messages in buffer: ", Nb)
print("Minimum subscription period: ", min_sub_period)
print("Maximum subscription period: ", max_sub_period)
print("Average subscription period: ", avg_sub_period)
print("Current subscription period: ", current_sub_period)
print("Executor PW workload: ", pw_time)
file_saving_drops.write("%d\n" % sum(Nd))
file_saving_throughput.write("%d\n" % Ns[3])
cputimes = [a * b for a, b in zip(Nd, pw_time)]
file_saving_cputime.write("%f\n" % sum(cputimes))
indicators = Indicators(Nd, Np, Ns, Nb, Delta_t, imdi_theta, ieri_lambda, current_sub_period, pw_time, pmdi_w)
regulation_type, regulation_position = indicators.check()
print("Regulation position: ", regulation_position)
regulation_flag = any(item == 1 for item in regulation_position)
if regulation_flag:
if regulation_type == REGULATION_TYPE.REDUCE:
modified_period, pre_position = calculator.calculate_reduced_frequency(max_sub_period, varphi, Delta_t, Nb, regulation_position)
print(f"--- Reduced timer frequency: {1000/modified_period} -> {modified_period}ms")
if BASELINE == False:
frequency_sender.publish_modified_cycle_time(modified_period)
pre_reduced_frequency = modified_period
pre_regulation_type = REGULATION_TYPE.REDUCE
elif regulation_type == REGULATION_TYPE.INCREASE:
modified_period = calculator.calculate_increased_frequency(avg_sub_period, sigma, Delta_t, Nb, regulation_position, pre_position)
if BASELINE == False:
if modified_period > 0.0:
print(f"+++ Increased timer frequency: {1000/modified_period} -> {modified_period}ms")
frequency_sender.publish_modified_cycle_time(modified_period)
pre_increased_frequency = modified_period
pre_regulation_type = REGULATION_TYPE.INCREASE
elapsed_time = 0
while True:
print("\n\nEpoch {}: analyzing ...".format(epoch+2))
time.sleep(max(0, Delta_t - elapsed_time))
start_time = time.time()
trace_data = live_reader.get_trace_data()
if len(trace_data):
trace_parser.add_new_trace_data(trace_data)
trace_parser.analyze_indicators()
Nd, Np, Ns, min_sub_period, avg_sub_period, Nb, throughput = trace_parser.get_message_indicators()
current_sub_period, pw_time, max_sub_period = trace_parser.get_callback_indicators()
print("==================================")
print("The number of drop messages: ", Nd)
print("The number of pub messages: ", Np)
print("The number of sub messages: ", Ns)
print("Number of messages in buffer: ", Nb)
print("Minimum subscription period: ", min_sub_period)
print("Maximum subscription period: ", max_sub_period)
print("Average subscription period: ", avg_sub_period)
print("Current subscription period: ", current_sub_period)
print("Executor PW workload: ", pw_time)
file_saving_drops.write("%d\n" % sum(Nd))
file_saving_throughput.write("%d\n" % Ns[3])
cputimes = [a * b for a, b in zip(Nd, pw_time)]
file_saving_cputime.write("%f\n" % sum(cputimes))
indicators.update(Nd, Np, Ns, Nb, current_sub_period, pw_time)
regulation_type, regulation_position = indicators.check()
print("Regulation position: ", regulation_position)
regulation_flag = any(item == 1 for item in regulation_position)
if regulation_flag:
if regulation_type == REGULATION_TYPE.REDUCE:
modified_period, pre_position = calculator.calculate_reduced_frequency(max_sub_period, varphi, Delta_t, Nb, regulation_position)
print(f"--- Reduced timer frequency: {1000/modified_period} -> {modified_period}ms")
if BASELINE == False:
if pre_regulation_type == REGULATION_TYPE.REDUCE or pre_regulation_type == REGULATION_TYPE.NOACT:
if modified_period > pre_reduced_frequency:
frequency_sender.publish_modified_cycle_time(modified_period)
pre_reduced_frequency = modified_period
pre_regulation_type = REGULATION_TYPE.REDUCE
elif pre_regulation_type == REGULATION_TYPE.INCREASE:
if modified_period > pre_increased_frequency:
frequency_sender.publish_modified_cycle_time(modified_period)
pre_reduced_frequency = modified_period
pre_regulation_type = REGULATION_TYPE.REDUCE
elif regulation_type == REGULATION_TYPE.INCREASE:
modified_period = calculator.calculate_increased_frequency(avg_sub_period, sigma, Delta_t, Nb, regulation_position, pre_position)
if BASELINE == False:
if pre_regulation_type == REGULATION_TYPE.INCREASE or pre_regulation_type == REGULATION_TYPE.NOACT:
if 0.0 < modified_period < pre_increased_frequency:
print(f"+++ Increased timer frequency: {1000/modified_period} -> {modified_period}ms")
# if modified_period < 80:
# modified_period = 80.0
frequency_sender.publish_modified_cycle_time(modified_period)
pre_increased_frequency = modified_period
pre_regulation_type = REGULATION_TYPE.INCREASE
elif pre_regulation_type == REGULATION_TYPE.REDUCE:
if 0.0 < modified_period < pre_reduced_frequency:
print(f"+++ Increased timer frequency: {1000/modified_period} -> {modified_period}ms")
frequency_sender.publish_modified_cycle_time(modified_period)
pre_increased_frequency = modified_period
pre_regulation_type = REGULATION_TYPE.INCREASE
else:
print("Warning: Trace data set is empty (epoch: {})!\n".format(epoch+2))
continue
end_time = time.time()
elapsed_time = end_time - start_time
file_saving_execution_time.write("%f\n" % elapsed_time)
if epoch == 3000:
live_reader.stop_consume()
exit()
epoch+=1