-
Notifications
You must be signed in to change notification settings - Fork 1
/
handover.py
177 lines (142 loc) · 4.74 KB
/
handover.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
import os,sys,argparse
import re
import logging
import json
from pycommons import pycommons
from pycommons import generic_logging
logger = logging.getLogger('handover')
import common
from common import Experiment, open_file
from common import *
KEY = 'handover'
class HandoverData(object):
RRC = 1
SERVING_CELL = 2
NEIGHBOR_CELL = 3
RRC_STATE_CHANGE = 4
def __init__(self, type, os_timestamp, diag_timestamp, **kwargs):
self.os_timestamp = os_timestamp
self.diag_timestamp = diag_timestamp
self.type = type
for key, value in kwargs.iteritems():
setattr(self, key, value)
@classmethod
def parse(cls, data):
start_line = data[0]
end_line = data[-1]
if cls.START_PATTERN.match(start_line) and \
cls.END_PATTERN.match(end_line):
timestamps_line = data[1]
try:
os_timestamp, diag_timestamp = [float(x) for x in timestamps_line.split(' ')]
data = data[2:-1]
d = {'data' : data}
return HandoverData(cls.TYPE, os_timestamp, diag_timestamp, **d)
except:
return None
class HandoverRRC(HandoverData):
TYPE = HandoverData.RRC
START_PATTERN = re.compile(r'\[start LTE RRC\]')
END_PATTERN = re.compile(r'\[end LTE RRC\]')
class HandoverServingCell(HandoverData):
TYPE = HandoverData.SERVING_CELL
START_PATTERN = re.compile(r'\[start Serving cell meas\]')
END_PATTERN = re.compile(r'\[end Serving cell meas\]')
class HandoverNeighborCell(HandoverData):
TYPE = HandoverData.NEIGHBOR_CELL
START_PATTERN = re.compile(r'\[start Neighbor cell meas\]')
END_PATTERN = re.compile(r'\[end Neighbor cell meas\]')
class HandoverRRCStateChange(HandoverData):
TYPE = HandoverData.RRC_STATE_CHANGE
START_PATTERN = re.compile(r'\[start LTE RRC state change\]')
END_PATTERN = re.compile(r'\[end LTE RRC state change\]')
class Handover(object):
START_PATTERN = re.compile(r'\[start.*\]')
END_PATTERN = re.compile(r'\[end.*\]')
def __init__(self, path):
self.path = path
self.data = []
self.process()
def process(self, start=0, stop=None):
assert os.path.exists(self.path), "'%s' does not exist!" % (file)
data = []
# cmdline = 'cat %s | diag_parser' % (file)
# ret, stdout, stderr = pycommons.run(cmdline, log=False)
# lines = stdout.split('\n')
lines = []
with open_file(self.path, 'rb') as f:
for line in f:
lines.append(line.strip())
for idx in range(len(lines)):
line = lines[idx]
m = self.START_PATTERN.match(line)
if m:
obj_data = []
while idx < len(lines):
line = lines[idx]
obj_data.append(lines[idx])
m = self.END_PATTERN.match(line)
if m:
break
idx += 1
obj = self.parse(obj_data)
if not obj:
#logger.debug("Could not convert: \n%s\n" % ('\n'.join(obj_data)))
pass
else:
if obj.os_timestamp > start:
if not stop or (stop and obj.os_timestamp < stop):
data.append(obj)
idx += 1
self.data = data
def get_data(self, start_idx, start=0, end=None):
data = []
last_idx = start_idx
for idx, obj in enumerate(self.data):
if obj.diag_timestamp > start:
if not end or (end and obj.diag_timestamp < end):
data.append(obj)
last_idx = start_idx + idx
return data, last_idx
def parse(self, data):
classes = [HandoverRRC, HandoverServingCell, HandoverNeighborCell, HandoverRRCStateChange]
for c in classes:
f = getattr(c, 'parse')
obj = f(data)
if obj:
return obj
return None
def process(file_path, experiments):
logger.info("Processing ...")
handover = Handover(file_path)
expt_with_no_handover = 0
num_cellular_expts = 0
last_idx = 0
for expt in experiments:
for measurement in expt.get_measurements():
if measurement.interface == Experiment.WIFI_IFACE:
continue
op = measurement.operation
start_time = measurement.results.get(op + 'Begin', 0)
end_time = measurement.results.get(op + 'End', 0)
handover_data, last_idx = handover.get_data(last_idx, start_time, end_time)
setattr(measurement, KEY, handover_data)
if len(handover_data) == 0:
expt_with_no_handover += 1
num_cellular_expts += 1
logger.info("%d/%d cellular experiments with no handover data" % (expt_with_no_handover, num_cellular_expts))
def main(argv):
generic_logging.init(level=logging.WARN)
global logger
logger = logging.getLogger()
config = json.loads(open('config').read())
battor_logfile_path = config['parse']['file_path']
sample_rate = get_sample_rate(battor_logfile_path)
down_sample = config['parse']['down_sample']
loglines = parse(**config)
start_edge = find_start_edge(loglines, sample_rate, down_sample, **config['edge'])
logcat_lines = logcat_generator(**config['logcat'])
logcat_time_offset, battor_first_edge_timestamp = get_edge_timestamps(loglines, start_edge, logcat_lines)
h = Handover(config['handover']['dir'], logcat_time_offset)
if __name__ == '__main__':
main(sys.argv)