2019-06-06 09:18:41 +02:00
|
|
|
# CTF to pickle conversion
|
2019-06-06 09:17:36 +02:00
|
|
|
|
2019-06-06 09:18:41 +02:00
|
|
|
import babeltrace
|
|
|
|
from pickle import Pickler
|
|
|
|
import time
|
|
|
|
|
2019-06-06 09:20:10 +02:00
|
|
|
# List of ignored CTF fields
|
2019-06-06 09:19:49 +02:00
|
|
|
_IGNORED_FIELDS = [
|
|
|
|
'content_size', 'cpu_id', 'events_discarded', 'id', 'packet_size', 'packet_seq_num',
|
|
|
|
'stream_id', 'stream_instance_id', 'timestamp_end', 'timestamp_begin', 'magic', 'uuid', 'v'
|
|
|
|
]
|
|
|
|
_DISCARD = 'events_discarded'
|
2019-06-06 09:18:41 +02:00
|
|
|
|
|
|
|
def ctf_to_pickle(trace_directory, target):
|
|
|
|
"""
|
|
|
|
Load CTF trace and convert to a pickle file
|
|
|
|
:param trace_directory (str): the main/top trace directory
|
|
|
|
:param target (Pickler): the target pickle file to write to
|
|
|
|
"""
|
|
|
|
# add traces
|
|
|
|
tc = babeltrace.TraceCollection()
|
2019-06-06 09:23:24 +02:00
|
|
|
print(f'Importing trace directory: {trace_directory}')
|
2019-06-06 09:18:41 +02:00
|
|
|
tc.add_traces_recursive(trace_directory, 'ctf')
|
|
|
|
|
|
|
|
count = 0
|
|
|
|
count_written = 0
|
|
|
|
# count_pid_matched = 0
|
|
|
|
# traced = set()
|
|
|
|
|
|
|
|
start_time = time.time()
|
|
|
|
|
|
|
|
# PID_KEYS = ['vpid', 'pid']
|
|
|
|
for event in tc.events:
|
|
|
|
count += 1
|
|
|
|
# pid = None
|
|
|
|
# for key in PID_KEYS:
|
|
|
|
# if key in event.keys():
|
|
|
|
# pid = event[key]
|
|
|
|
# break
|
|
|
|
|
|
|
|
# Write all for now
|
|
|
|
pod = _ctf_event_to_pod(event)
|
|
|
|
target.dump(pod)
|
|
|
|
count_written += 1
|
|
|
|
|
2019-06-06 09:23:24 +02:00
|
|
|
time_diff = time.time() - start_time
|
|
|
|
print(f'{count_written} events in {time_diff * 1000:.2f} ms')
|
2019-06-06 09:18:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
def _ctf_event_to_pod(ctf_event):
|
|
|
|
"""
|
|
|
|
Convert name, timestamp, and all other keys except those in IGNORED_FIELDS into a dictionary.
|
|
|
|
:param ctf_element: The element to convert
|
|
|
|
:type ctf_element: babeltrace.Element
|
|
|
|
:return:
|
|
|
|
:return type: dict
|
|
|
|
"""
|
|
|
|
pod = {'_name': ctf_event.name, '_timestamp': ctf_event.timestamp}
|
|
|
|
if hasattr(ctf_event, _DISCARD) and ctf_event[_DISCARD] > 0:
|
|
|
|
print(ctf_event[_DISCARD])
|
|
|
|
for key in [key for key in ctf_event.keys() if key not in _IGNORED_FIELDS]:
|
|
|
|
pod[key] = ctf_event[key]
|
|
|
|
return pod
|