-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_v1.py
325 lines (295 loc) · 11.4 KB
/
parse_v1.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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import sys
import os
import csv
import re
import json
from tqdm import tqdm
import utilities
HEAP_REGEX='(.*?)total(.*?), used(.[A-Za-z0-9_*-]*)'
CSV_COL = [
'gc_id',
'before_gc_live_objects',
'before_gc_dead_objects',
'before_gc_total_objects',
'before_gc_roots_walk_elapsed',
'allocation_size',
'young_gen_live_objects',
'young_gen_dead_objects',
'young_gen_total_objects',
'young_gen_roots_walk_elapsed',
'total_young_gen_heap',
'used_young_gen_heap',
'old_gen_live_objects',
'old_gen_dead_objects',
'old_gen_total_objects',
'old_gen_roots_walk_elapsed',
'total_old_gen_heap',
'used_old_gen_heap',
# 'phases',
'stringtable_size',
'stringtable_processed',
'stringtable_removed',
'stringtable_time',
'young_gen_gc_time',
'old_gen_gc_time',
'gc_time',
'gc_time_clean'
]
def skip_prestr(line: str, prestr: str):
first_index_of_prestr = line.find(prestr)
last_index_of_prestr = first_index_of_prestr + len(prestr)
return line[last_index_of_prestr:]
def convert_time(line: str):
time = 0.0
if 'ms' in line:
time = float(line[:-2]) * 1
elif 's' in line:
time = float(line[:-1]) * 1000
return round(time, 3)
def convert_size(line: str):
size = float(line[:-1])
unit = line[-1]
if unit == 'K':
size = size * 1.0
elif unit == 'B':
size = size / 1000.0
return round(size, 3)
def parse_line_summaries(line: str):
new_line = line.replace(',', '')
infos = new_line.split()[1:]
result = {}
for info in infos:
# only contains two elements if splitted
key, value, *rest = info.split('=')
assert(len(rest) == 0)
value = value.replace(']', '')
if (key == 'elapsed'):
result[key] = convert_time(value)
else:
try:
result[key] = float(value)
except:
result[key] = value
return result
def parse_heap_size(line: str):
total = 0.0
used = 0.0
result = re.search(HEAP_REGEX, line)
if result:
total = convert_size(result[2])
used = convert_size(result[3])
return (total, used)
def parse_gc_time(line: str, prestr = 'GC Time'):
gc_time_str = skip_prestr(line, prestr).rstrip(']\n')
if ('secs' in gc_time_str):
gc_time_str = gc_time_str.replace('secs', 's')
gc_time_str = gc_time_str.replace(' ', '')
gc_time_str = gc_time_str.split(',')
assert(len(gc_time_str) == 2)
gc_time = convert_time(gc_time_str[1])
return gc_time
def parse_allocation_size(line: str):
prestr = 'Mem allocate size'
alloc_size_str = skip_prestr(line, prestr).rstrip(']\n')
if 'bytes' in alloc_size_str:
alloc_size_str = alloc_size_str.replace('bytes', 'B')
alloc_size_str = alloc_size_str.replace(' ', '')
alloc_size = convert_size(alloc_size_str)
return alloc_size
def parse_phases(line: str):
prestr = '['
phases_str = skip_prestr(line, prestr).rstrip(']\n')
phases = ';'.join(phases_str.split(' ')[:-1])
return phases
def parse_stringtable_time(line: str, old_format: bool = False):
prestr = 'StringTableTime],' if old_format else 'StringTableTime,'
time_str = skip_prestr(line, prestr).rstrip(']\n')
if ('secs' in time_str):
time_str = time_str.replace('secs', 's')
time_str = time_str.replace(' ', '')
time = convert_time(time_str)
return time
def parse_stringtable_info(line: str):
prestr = 'StringTableInfo'
info_str = skip_prestr(line, prestr).rstrip(']\n')
info_str = info_str.replace(',', '')
infos = info_str.split()
result = {}
for info in infos:
temp = {}
# only contains two elements if splitted
key, value, *rest = info.split('=')
assert(len(rest) == 0)
value = value.replace(']', '')
if (key == 'elapsed'):
result[key] = convert_time(value)
else:
try:
result[key] = float(value)
except:
result[key] = value
return result
def parse(filename, output, old_format: bool = False):
with open(filename) as log_file:
with open(output, 'w', newline='') as csv_file:
writer = csv.writer(csv_file)
writer.writerow(CSV_COL)
line = log_file.readline()
write_summary = None
last_summary = None
summary = None
old_gen_summary = None
young_gen_summary = None
young_gen = None
old_gen = None
gc_time = 0.0
old_gen_gc_time = 0.0
young_gen_gc_time = 0.0
allocation_size = 0.0
start_of_gc = False
write_csv_row = False
stringtable_time = 0.0
stringtable_info = None
start = False
count = 0
while line:
if line.startswith(' summaries: context=OldGen'):
old_gen_summary = parse_line_summaries(line)
elif line.startswith(' summaries: context=YoungGen'):
young_gen_summary = parse_line_summaries(line)
elif line.startswith(' summaries:'):
summary = parse_line_summaries(line)
count += 1
else:
summary = None
if line.startswith(' PSYoungGen'):
young_gen = parse_heap_size(line)
elif line.startswith(' ParOldGen'):
old_gen = parse_heap_size(line)
elif 'GC Time' in line:
gc_time = parse_gc_time(line)
elif 'OldGenTime' in line and start:
old_gen_gc_time = parse_gc_time(line, 'OldGenTime')
elif 'YoungGenTime' in line and start:
young_gen_gc_time = parse_gc_time(line, 'YoungGenTime')
elif 'Mem allocate size' in line and start:
allocation_size = parse_allocation_size(line)
# elif 'phase gc_id' in line and start:
# phases = parse_phases(line)
elif 'StringTableTime' in line and start:
stringtable_time = parse_stringtable_time(line, old_format)
elif 'StringTableInfo' in line and start:
stringtable_info = parse_stringtable_info(line)
line = log_file.readline()
# if summary and summary['context'] == 'AfterGC':
# if last_summary:
# print('LAST_SUM', last_summary['gc_id'])
# print('SUM', summary['gc_id'])
if summary is not None:
if last_summary is not None:
if summary['context'] == 'AfterGC':
# if summary['gc_id'] != last_summary['gc_id']:
write_summary = last_summary
write_csv_row = True
start = False
if summary['context'] == 'BeforeGC':
start = True
last_summary = summary
write_csv_row = False
# write_csv_row = False
# if write_summary:
# print('WRITE', write_summary['gc_id'])
if write_csv_row and write_summary is not None:
write_csv_row = False
if young_gen_summary is None:
young_gen_summary = {
'live_objects': 0,
'dead_objects': 0,
'total_objects': 0,
'elapsed': 0.0,
}
if old_gen_summary is None:
old_gen_summary = {
'live_objects': 0.0,
'dead_objects': 0.0,
'total_objects': 0.0,
'elapsed': 0.0,
}
if stringtable_info is None:
stringtable_info = {
'table_size': 0.0,
'processed': 0.0,
'removed': 0.0,
}
writer.writerow([
write_summary['gc_id'],
write_summary['live_objects'],
write_summary['dead_objects'],
write_summary['total_objects'],
write_summary['elapsed'],
allocation_size,
young_gen_summary['live_objects'],
young_gen_summary['dead_objects'],
young_gen_summary['total_objects'],
young_gen_summary['elapsed'],
young_gen[0],
young_gen[1],
old_gen_summary['live_objects'],
old_gen_summary['dead_objects'],
old_gen_summary['total_objects'],
old_gen_summary['elapsed'],
old_gen[0],
old_gen[1],
# phases,
stringtable_info['table_size'],
stringtable_info['processed'],
stringtable_info['removed'],
stringtable_time,
young_gen_gc_time,
old_gen_gc_time,
gc_time,
gc_time - stringtable_time,
])
summary = None
write_summary = None
last_summary = None
write_summary = None
last_summary = None
summary = None
old_gen_summary = None
young_gen_summary = None
young_gen = None
old_gen = None
gc_time = 0.0
old_gen_gc_time = 0.0
young_gen_gc_time = 0.0
allocation_size = 0.0
start_of_gc = False
write_csv_row = False
# phases = None
stringtable_time = 0.0
stringtable_info = None
csv_file.close()
log_file.close()
def main(args):
print('Reading config')
config = utilities.read_json_config(args.config, utilities.Task.parse)
print('Starting parsing...')
output_dir = '{}/{}'.format(config['dir']['data'], config['name'])
print('Creating data directory {}'.format(output_dir))
utilities.create_dir(output_dir)
print('Reading raw data...')
threads = []
pbar = tqdm(range(len(config['data'])))
for index in pbar:
infile = config['data'][index]['file']
outfile = '{}/{}.csv'.format(output_dir, config['data'][index]['name'])
pbar.set_description('Processing raw_data in={} out={}'.format(infile, outfile))
parse(infile, outfile, config['data'][index]['old_format'] if 'old_format' in config['data'][index] else False)
if __name__ == '__main__':
import time
start_time = time.time()
main(utilities.get_args())
print("--- %s seconds ---" % (time.time() - start_time))
# Threads: --- 71.97676062583923 seconds ---
# No Thread : --- 64.11785078048706 seconds ---