-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnl-calc
executable file
·283 lines (230 loc) · 8.87 KB
/
nl-calc
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
#!/usr/bin/env python3
# Copyright (c) 2018, Kontron Europe GmbH
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
from __future__ import print_function
import argparse
import copy
import dateutil.parser
import json
import numpy
import sys
def update_histogram_timestamp(timestamp, histogram):
tx_timestamp = numpy.datetime64(timestamp)
if not histogram['start-timestamp']:
histogram['start-timestamp'] = timestamp
if not histogram['end-timestamp']:
histogram['end-timestamp'] = timestamp
if numpy.datetime64(timestamp) > \
numpy.datetime64(histogram['end-timestamp']):
histogram['end-timestamp'] = timestamp
def update_histogram_general(value, histogram):
histogram['count'] += 1
if value > histogram['max'] or histogram['max'] == 0:
histogram['max'] = value
if value < histogram['min'] or histogram['min'] == 0:
histogram['min'] = value
def update_histogram_modulo(timestamp, value, histogram):
update_histogram_general(value, histogram)
if value < 0:
histogram['time_error'] += 1
elif value > len(histogram['histogram']):
histogram['outliers'] += 1
else:
value = value % 1000
histogram['histogram'][value] += 1
update_histogram_timestamp(timestamp, histogram)
def update_histogram(timestamp, value, histogram):
update_histogram_general(value, histogram)
if value < 0:
histogram['time_error'] += 1
elif value < len(histogram['histogram']):
try:
histogram['histogram'][value] += 1
except IndexError as e:
print ((e, value))
else:
histogram['outliers'] += 1
update_histogram_timestamp(timestamp, histogram)
def update_histogram_jitter(timestamp, value, offset, histogram):
update_histogram_general(value, histogram)
value += offset
if value < 0 or value >= len(histogram['histogram']):
histogram['outliers'] += 1
else:
try:
histogram['histogram'][value] += 1
except IndexError as e:
print ((e, value))
update_histogram_timestamp(timestamp, histogram)
def calc_latency(pkt, ts):
result = {}
interval_start = numpy.datetime64(ts['interval-start'])
# t0
#tx_user_target = numpy.datetime64(ts['tx-wakeup'])
# t1
tx_user = numpy.datetime64(ts['tx-program'])
# t4
rx_hw = numpy.datetime64(ts['rx-hardware'])
# rt-application latency: (t1 - t0) % interval
diff_rt_app = tx_user - interval_start
diff_interval_start_hw_rx = rx_hw - interval_start
result['type'] = 'latency-calc'
result['object'] = {
'latency-program': int(diff_rt_app)/1000 % pkt['interval-usec'],
#'latency-scheduled-times': int(diff_interval_start_hw_rx)/1000 % pkt['interval-usec'],
'latency-scheduled-times': int(diff_interval_start_hw_rx)/1000,
'sequence-number': pkt['sequence-number'],
'tx-program': ts['tx-program'],
'stream-id': pkt['stream-id'],
}
return result
mean_latency = 0
count_pkt = 0
jitter_min = 0
jitter_max = 0
def calc_jitter(pkt, ts):
global mean_latency
global count_pkt
global jitter_min
global jitter_max
result = {}
interval_start = numpy.datetime64(ts['interval-start'])
rx_hw = numpy.datetime64(ts['rx-hardware'])
diff_interval_start_hw_rx = rx_hw - interval_start
val = int(diff_interval_start_hw_rx) % (pkt['interval-usec'] * 1000)
mean_latency = (count_pkt * mean_latency + val) / (count_pkt + 1)
count_pkt += 1
#print(mean_latency)
jitter = mean_latency - val
if jitter_min > jitter:
jitter_min = jitter
if jitter_max < jitter:
jitter_max = jitter
return jitter
def dump_json_str(val):
print(json.dumps(val), file=sys.stdout)
sys.stdout.flush()
def main(args=None):
parser = argparse.ArgumentParser(
description='latency')
parser.add_argument('-c', '--count', type=int, dest='count',
help='Count until histogram output', default=0)
parser.add_argument('infile', nargs='?', type=argparse.FileType('r'),
help='Input file (default is STDIN)', default=sys.stdin)
args = parser.parse_args(args)
output = None
histogram_program_latency_empty = {
'type': 'histogram-program-latency',
'object': {
'stream-id': 0,
'count': 0,
'min': 0,
'max': 0,
'outliers': 0,
'time_error': 0,
'histogram': [0] * 50,
'start-timestamp': None,
'end-timestamp': None,
}
}
histogram_scheduled_times_empty = {
'type': 'histogram-scheduled-times',
'object': {
'stream-id': 0,
'count': 0,
'min': 0,
'max': 0,
'outliers': 0,
'time_error': 0,
'histogram': [0] * 1000,
'start-timestamp': None,
'end-timestamp': None,
}
}
histogram_jitter_empty= {
'type': 'histogram-jitter',
'object': {
'stream-id': 0,
'count': 0,
'min': 0,
'max': 0,
'outliers': 0,
'time_error': 0,
'offset': 1000,
'histogram': [0] * 2000,
'start-timestamp': None,
'end-timestamp': None,
}
}
hist_program_latency = copy.deepcopy(histogram_program_latency_empty)
hist_scheduled_times = copy.deepcopy(histogram_scheduled_times_empty)
hist_jitter = copy.deepcopy(histogram_jitter_empty)
count = 0
try:
for line in args.infile:
line = line.strip()
if not line:
continue
try:
j = json.loads(line)
if j['type'] == 'rx-error':
print(line, file=sys.stdout)
elif j['type'] == 'rx-packet':
count += 1
ts = j['object']['timestamps']
ts = dict(zip(ts['names'], ts['values']))
result = calc_latency(j['object'], ts)
timestamp = result['object']['tx-program']
value = result['object']['latency-program']
update_histogram(timestamp, int(value),
hist_program_latency['object'])
value = result['object']['latency-scheduled-times']
update_histogram_modulo(timestamp, int(value),
hist_scheduled_times['object'])
jitter = calc_jitter(j['object'], ts)
update_histogram_jitter(timestamp, int(jitter), hist_jitter['object']['offset'],
hist_jitter['object'])
if args.count != 0:
if count == args.count:
dump_json_str(hist_program_latency)
dump_json_str(hist_scheduled_times)
dump_json_str(hist_jitter)
count = 0
hist_program_latency= copy.deepcopy(
histogram_program_latency_empty)
hist_scheduled_times = copy.deepcopy(
histogram_scheduled_times_empty)
hist_jitter = copy.deepcopy(
histogram_jitter_empty)
sys.stdout.flush()
except ValueError as e:
print(e, file=sys.stderr)
pass
except KeyboardInterrupt as e:
pass
dump_json_str(hist_program_latency)
dump_json_str(hist_scheduled_times)
dump_json_str(hist_jitter)
if __name__ == '__main__':
main()