Skip to content

Commit

Permalink
trace: add trace event iterator APIs
Browse files Browse the repository at this point in the history
Currently methods which want to iterate over trace events,
do so using the trace_event_count() and trace_event_id()
methods. This leaks the concept of a single ID enum to
the callers. There is an alternative trace_event_pattern()
method which can be used in an iteration context, but its
design is stateless, so is not easy to expand it in the
future.

This defines a formal iterator API will provide a future-
proof way of iterating over events.

The iterator is also able to apply a pattern match filter
to events, further removing the need for the pattern

Reviewed-by: Stefan Hajnoczi <[email protected]>
Reviewed-by: Lluís Vilanova <[email protected]>
Signed-off-by: Daniel P. Berrange <[email protected]>
Message-id: [email protected]
Signed-off-by: Stefan Hajnoczi <[email protected]>
  • Loading branch information
berrange authored and stefanhaRH committed Oct 12, 2016
1 parent 170f75a commit 6a1b0f3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
21 changes: 21 additions & 0 deletions trace/control.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,27 @@ TraceEvent *trace_event_pattern(const char *pat, TraceEvent *ev)
return NULL;
}

void trace_event_iter_init(TraceEventIter *iter, const char *pattern)
{
iter->event = 0;
iter->pattern = pattern;
}

TraceEvent *trace_event_iter_next(TraceEventIter *iter)
{
while (iter->event < TRACE_EVENT_COUNT) {
TraceEvent *ev = &(trace_events[iter->event]);
iter->event++;
if (!iter->pattern ||
pattern_glob(iter->pattern,
trace_event_get_name(ev))) {
return ev;
}
}

return NULL;
}

void trace_list_events(void)
{
int i;
Expand Down
27 changes: 27 additions & 0 deletions trace/control.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
#include "qemu-common.h"
#include "trace/generated-events.h"

typedef struct TraceEventIter {
size_t event;
const char *pattern;
} TraceEventIter;

/**
* TraceEventID:
Expand All @@ -25,6 +29,29 @@
*/
enum TraceEventID;


/**
* trace_event_iter_init:
* @iter: the event iterator struct
* @pattern: optional pattern to filter events on name
*
* Initialize the event iterator struct @iter,
* optionally using @pattern to filter out events
* with non-matching names.
*/
void trace_event_iter_init(TraceEventIter *iter, const char *pattern);

/**
* trace_event_iter_next:
* @iter: the event iterator struct
*
* Get the next event, if any. When this returns NULL,
* the iterator should no longer be used.
*
* Returns: the next event, or NULL if no more events exist
*/
TraceEvent *trace_event_iter_next(TraceEventIter *iter);

/**
* trace_event_id:
* @id: Event identifier.
Expand Down

0 comments on commit 6a1b0f3

Please sign in to comment.