Add basic conversion entrypoint

This commit is contained in:
Christophe Bedard 2019-06-06 09:18:41 +02:00
parent b9e4ce7bfb
commit c500876b47
2 changed files with 72 additions and 5 deletions

View file

@ -1,3 +1,60 @@
# CTF to pickle conversion
def ctf_to_pickle():
print('ctf_to_pickle')
import babeltrace
from pickle import Pickler
import time
# TODO
_IGNORED_FIELDS = []
_DISCARD = "events_discarded"
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()
print(f'Importing {trace_directory}')
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)
print(f'dumping pod: {str(pod)}')
target.dump(pod)
count_written += 1
print(f'{count_written} events in {time.time() - start_time}')
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

View file

@ -2,8 +2,18 @@
# Entrypoint/script to convert CTF trace data to a pickle file
# TODO
import sys
from pickle import Pickler
from tracetools_analysis.conversion.ctf import *
def main():
print('main called')
ctf_to_pickle()
def main(argv=sys.argv):
if len(argv) != 3:
print("usage: /trace/directory pickle_target_file")
exit(1)
trace_directory = sys.argv[1]
pickle_target_file = sys.argv[2]
with open(pickle_target_file, 'wb') as f:
p = Pickler(f, protocol=4)
ctf_to_pickle(trace_directory, p)