mirror of
https://github.com/Motorhead1991/qemu.git
synced 2025-07-29 13:23:54 -06:00
simpletrace: move logic of process into internal function
To avoid duplicate code depending on input types and to better handle open/close of log with a context-manager, we move the logic of process into _process. Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com> Signed-off-by: Mads Ynddal <m.ynddal@samsung.com> Message-id: 20230926103436.25700-11-mads@ynddal.dk Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
This commit is contained in:
parent
6f53641a98
commit
b78234e65c
1 changed files with 18 additions and 8 deletions
|
@ -201,13 +201,26 @@ def process(events, log, analyzer, read_header=True):
|
||||||
# Treat as an already opened file-object
|
# Treat as an already opened file-object
|
||||||
events_list = read_events(events, events.name)
|
events_list = read_events(events, events.name)
|
||||||
|
|
||||||
close_log = False
|
|
||||||
if isinstance(log, str):
|
if isinstance(log, str):
|
||||||
log = open(log, 'rb')
|
with open(log, 'rb') as log_fobj:
|
||||||
close_log = True
|
_process(events_list, log_fobj, analyzer, read_header)
|
||||||
|
else:
|
||||||
|
# Treat `log` as an already opened file-object. We will not close it,
|
||||||
|
# as we do not own it.
|
||||||
|
_process(events_list, log, analyzer, read_header)
|
||||||
|
|
||||||
|
def _process(events, log_fobj, analyzer, read_header=True):
|
||||||
|
"""Internal function for processing
|
||||||
|
|
||||||
|
Args:
|
||||||
|
events (list): list of events already produced by tracetool.read_events
|
||||||
|
log_fobj (file): file-object to read log data from
|
||||||
|
analyzer (Analyzer): the Analyzer to interpret the event data
|
||||||
|
read_header (bool, optional): Whether to read header data from the log data. Defaults to True.
|
||||||
|
"""
|
||||||
|
|
||||||
if read_header:
|
if read_header:
|
||||||
read_trace_header(log)
|
read_trace_header(log_fobj)
|
||||||
|
|
||||||
def build_fn(analyzer, event):
|
def build_fn(analyzer, event):
|
||||||
if isinstance(event, str):
|
if isinstance(event, str):
|
||||||
|
@ -231,14 +244,11 @@ def process(events, log, analyzer, read_header=True):
|
||||||
|
|
||||||
with analyzer:
|
with analyzer:
|
||||||
fn_cache = {}
|
fn_cache = {}
|
||||||
for event, event_id, timestamp_ns, record_pid, *rec_args in read_trace_records(events, log, read_header):
|
for event, event_id, timestamp_ns, record_pid, *rec_args in read_trace_records(events, log_fobj, read_header):
|
||||||
if event_id not in fn_cache:
|
if event_id not in fn_cache:
|
||||||
fn_cache[event_id] = build_fn(analyzer, event)
|
fn_cache[event_id] = build_fn(analyzer, event)
|
||||||
fn_cache[event_id](event, (event_id, timestamp_ns, record_pid, *rec_args))
|
fn_cache[event_id](event, (event_id, timestamp_ns, record_pid, *rec_args))
|
||||||
|
|
||||||
if close_log:
|
|
||||||
log.close()
|
|
||||||
|
|
||||||
def run(analyzer):
|
def run(analyzer):
|
||||||
"""Execute an analyzer on a trace file given on the command-line.
|
"""Execute an analyzer on a trace file given on the command-line.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue