qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Daniel P. Berrange" <berrange@redhat.com>
To: qemu-devel@nongnu.org, Stefan Hajnoczi <stefanha@redhat.com>
Subject: Re: [Qemu-devel] [PATCH v3 02/18] trace: convert code to use event iterators
Date: Tue, 20 Sep 2016 14:13:38 +0100	[thread overview]
Message-ID: <20160920131337.GI25490@redhat.com> (raw)
In-Reply-To: <87r38fakre.fsf@fimbulvetr.bsc.es>

On Mon, Sep 19, 2016 at 06:59:17PM +0200, Lluís Vilanova wrote:
> Daniel P Berrange writes:
> 
> > This converts the HMP/QMP monitor API implementations
> > and some internal trace control methods to use the new
> > trace event iterator APIs.
> 
> > Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
> > Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
> > ---
> >  monitor.c       | 26 ++++++++--------
> >  trace/control.c | 92 +++++++++++++++++++++++++++++++++------------------------
> >  trace/qmp.c     | 16 ++++++----
> >  3 files changed, 78 insertions(+), 56 deletions(-)
> 
> > diff --git a/monitor.c b/monitor.c
> > index 5c00373..ae70157 100644
> > --- a/monitor.c
> > +++ b/monitor.c
> > @@ -3335,13 +3335,14 @@ void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *st
> >      len = strlen(str);
> >      readline_set_completion_index(rs, len);
> >      if (nb_args == 2) {
> > -        TraceEventID id;
> > -        for (id = 0; id < trace_event_count(); id++) {
> > -            const char *event_name = trace_event_get_name(trace_event_id(id));
> > -            if (!strncmp(str, event_name, len)) {
> > -                readline_add_completion(rs, event_name);
> > -            }
> > +        TraceEventIter iter;
> > +        TraceEvent *ev;
> > +        char *pattern = g_strdup_printf("%s*", str);
> > +        trace_event_iter_init(&iter, pattern);
> > +        while ((ev = trace_event_iter_next(&iter)) != NULL) {
> > +            readline_add_completion(rs, trace_event_get_name(ev));
> >          }
> > +        g_free(pattern);
> >      }
> >  }
>  
> > @@ -3352,13 +3353,14 @@ void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
> >      len = strlen(str);
> >      readline_set_completion_index(rs, len);
> >      if (nb_args == 2) {
> > -        TraceEventID id;
> > -        for (id = 0; id < trace_event_count(); id++) {
> > -            const char *event_name = trace_event_get_name(trace_event_id(id));
> > -            if (!strncmp(str, event_name, len)) {
> > -                readline_add_completion(rs, event_name);
> > -            }
> > +        TraceEventIter iter;
> > +        TraceEvent *ev;
> > +        char *pattern = g_strdup_printf("%s*", str);
> > +        trace_event_iter_init(&iter, pattern);
> > +        while ((ev = trace_event_iter_next(&iter)) != NULL) {
> > +            readline_add_completion(rs, trace_event_get_name(ev));
> >          }
> > +        g_free(pattern);
> >      } else if (nb_args == 3) {
> >          add_completion_option(rs, str, "on");
> >          add_completion_option(rs, str, "off");
> > diff --git a/trace/control.c b/trace/control.c
> > index 1a96049..b471146 100644
> > --- a/trace/control.c
> > +++ b/trace/control.c
> > @@ -60,9 +60,10 @@ TraceEvent *trace_event_name(const char *name)
> >  {
> >      assert(name != NULL);
>  
> > -    TraceEventID i;
> > -    for (i = 0; i < trace_event_count(); i++) {
> > -        TraceEvent *ev = trace_event_id(i);
> > +    TraceEventIter iter;
> > +    TraceEvent *ev;
> > +    trace_event_iter_init(&iter, NULL);
> > +    while ((ev = trace_event_iter_next(&iter)) != NULL) {
> >          if (strcmp(trace_event_get_name(ev), name) == 0) {
> >              return ev;
> >          }
> > @@ -105,21 +106,18 @@ TraceEvent *trace_event_pattern(const char *pat, TraceEvent *ev)
> >  {
> >      assert(pat != NULL);
>  
> > -    TraceEventID i;
> > -
> > -    if (ev == NULL) {
> > -        i = -1;
> > -    } else {
> > -        i = trace_event_get_id(ev);
> > -    }
> > -    i++;
> > -
> > -    while (i < trace_event_count()) {
> > -        TraceEvent *res = trace_event_id(i);
> > -        if (pattern_glob(pat, trace_event_get_name(res))) {
> > -            return res;
> > +    bool matched = ev ? false : true;
> > +    TraceEventIter iter;
> > +    TraceEvent *thisev;
> > +    trace_event_iter_init(&iter, pat);
> > +    while ((thisev = trace_event_iter_next(&iter)) != NULL) {
> > +        if (matched) {
> > +            return thisev;
> > +        } else {
> > +            if (ev == thisev) {
> > +                matched = true;
> > +            }
> >          }
> > -        i++;
> >      }
>  
> >      return NULL;
> > @@ -148,10 +146,11 @@ TraceEvent *trace_event_iter_next(TraceEventIter *iter)
>  
> >  void trace_list_events(void)
> >  {
> > -    int i;
> > -    for (i = 0; i < trace_event_count(); i++) {
> > -        TraceEvent *res = trace_event_id(i);
> > -        fprintf(stderr, "%s\n", trace_event_get_name(res));
> > +    TraceEventIter iter;
> > +    TraceEvent *ev;
> > +    trace_event_iter_init(&iter, NULL);
> > +    while ((ev = trace_event_iter_next(&iter)) != NULL) {
> > +        fprintf(stderr, "%s\n", trace_event_get_name(ev));
> >      }
> >  }
>  
> > @@ -159,25 +158,40 @@ static void do_trace_enable_events(const char *line_buf)
> >  {
> >      const bool enable = ('-' != line_buf[0]);
> >      const char *line_ptr = enable ? line_buf : line_buf + 1;
> > -
> > -    if (trace_event_is_pattern(line_ptr)) {
> > -        TraceEvent *ev = NULL;
> > -        while ((ev = trace_event_pattern(line_ptr, ev)) != NULL) {
> > +    TraceEventIter iter;
> > +    TraceEvent *ev;
> > +    bool is_pattern = trace_event_is_pattern(line_ptr);
> > +
> > +    trace_event_iter_init(&iter, is_pattern ? line_ptr : NULL);
> > +    while ((ev = trace_event_iter_next(&iter)) != NULL) {
> > +        bool match = false;
> > +        if (is_pattern) {
> >              if (trace_event_get_state_static(ev)) {
> > -                trace_event_set_state_dynamic_init(ev, enable);
> > +                match = true;
> >              }
> > -        }
> > -    } else {
> > -        TraceEvent *ev = trace_event_name(line_ptr);
> > -        if (ev == NULL) {
> > -            error_report("WARNING: trace event '%s' does not exist",
> > -                         line_ptr);
> > -        } else if (!trace_event_get_state_static(ev)) {
> > -            error_report("WARNING: trace event '%s' is not traceable",
> > -                         line_ptr);
> >          } else {
> > -            trace_event_set_state_dynamic_init(ev, enable);
> > +            if (g_str_equal(trace_event_get_name(ev),
> > +                            line_ptr)) {
> 
> Why do some places use strcmp() and others g_str_equal() (the former are only on
> previously existing lines).

g_str_equal is clearer to follow, as you can see what the
intended semantics are, without having to look to the end of
the expression for a == 0 or != 0 and then remember which of
them means eq vs not-eq.

> If you maintain calls to trace_event_name() instead of iterating on both paths,
> the function used to compare names would be "unique". And would also make the
> warning logic at the end easier to follow (even if you call
> _set_state_dynamic_init() from two places instead of one).

We can actually simplify in a different way - if pattern_glob is
passed a pattern without any wildcards, it'll do an exact match,
so we can just rely on that in both cases.



Regards,
Daniel
-- 
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|

  reply	other threads:[~2016-09-20 13:13 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-19 14:48 [Qemu-devel] [PATCH v3 00/18] Refactor trace to allow modular build Daniel P. Berrange
2016-09-19 14:48 ` [Qemu-devel] [PATCH v3 01/18] trace: add trace event iterator APIs Daniel P. Berrange
2016-09-19 16:39   ` Lluís Vilanova
2016-09-19 14:48 ` [Qemu-devel] [PATCH v3 02/18] trace: convert code to use event iterators Daniel P. Berrange
2016-09-19 16:59   ` Lluís Vilanova
2016-09-20 13:13     ` Daniel P. Berrange [this message]
2016-09-20 13:36       ` Lluís Vilanova
2016-09-19 14:48 ` [Qemu-devel] [PATCH v3 03/18] trace: remove some now unused functions Daniel P. Berrange
2016-09-19 17:00   ` Lluís Vilanova
2016-09-19 17:24     ` Daniel P. Berrange
2016-09-19 14:48 ` [Qemu-devel] [PATCH v3 04/18] trace: remove global 'uint16 dstate[]' array Daniel P. Berrange
2016-09-19 15:55   ` Eric Blake
2016-09-19 17:05   ` Lluís Vilanova
2016-09-19 14:48 ` [Qemu-devel] [PATCH v3 05/18] trace: remove duplicate control.h includes in generated-tracers.h Daniel P. Berrange
2016-09-19 17:06   ` Lluís Vilanova
2016-09-19 14:48 ` [Qemu-devel] [PATCH v3 06/18] trace: break circular dependancy in event-internal.h Daniel P. Berrange
2016-09-19 17:08   ` Lluís Vilanova
2016-09-20 13:24     ` Daniel P. Berrange
2016-09-19 14:48 ` [Qemu-devel] [PATCH v3 07/18] trace: give each trace event a named TraceEvent struct Daniel P. Berrange
2016-09-19 17:17   ` Lluís Vilanova
2016-09-19 14:48 ` [Qemu-devel] [PATCH v3 08/18] trace: remove the TraceEventID and TraceEventVCPUID enums Daniel P. Berrange
2016-09-19 17:48   ` Lluís Vilanova
2016-09-20 13:29     ` Daniel P. Berrange
2016-09-19 14:49 ` [Qemu-devel] [PATCH v3 09/18] trace: emit name <-> ID mapping in simpletrace header Daniel P. Berrange
2016-09-19 14:49 ` [Qemu-devel] [PATCH v3 10/18] trace: don't abort qemu if ftrace can't be initialized Daniel P. Berrange
2016-09-19 14:49 ` [Qemu-devel] [PATCH v3 11/18] trace: provide mechanism for registering trace events Daniel P. Berrange
2016-09-19 14:49 ` [Qemu-devel] [PATCH v3 12/18] trace: dynamically allocate trace_dstate in CPUState Daniel P. Berrange
2016-09-19 17:58   ` Lluís Vilanova
2016-09-19 14:49 ` [Qemu-devel] [PATCH v3 13/18] trace: dynamically allocate event IDs at runtime Daniel P. Berrange
2016-09-19 18:05   ` Lluís Vilanova
2016-09-20 13:50     ` Daniel P. Berrange
2016-09-20 17:45       ` Lluís Vilanova
2016-09-19 14:49 ` [Qemu-devel] [PATCH v3 14/18] trace: get rid of generated-events.h/generated-events.c Daniel P. Berrange
2016-09-19 16:02   ` Eric Blake
2016-09-19 18:12   ` Lluís Vilanova
2016-09-19 14:49 ` [Qemu-devel] [PATCH v3 15/18] trace: rename _read_events to read_events Daniel P. Berrange
2016-09-19 18:15   ` Lluís Vilanova
2016-09-19 14:49 ` [Qemu-devel] [PATCH v3 16/18] trace: push reading of events up a level to tracetool main Daniel P. Berrange
2016-09-19 18:16   ` Lluís Vilanova
2016-09-19 14:49 ` [Qemu-devel] [PATCH v3 17/18] trace: pass trace-events to tracetool as a positional param Daniel P. Berrange
2016-09-19 18:18   ` Lluís Vilanova
2016-09-20 13:55     ` Daniel P. Berrange
2016-09-20 17:46       ` Lluís Vilanova
2016-09-19 14:49 ` [Qemu-devel] [PATCH v3 18/18] trace: introduce a formal group name for trace events Daniel P. Berrange
2016-09-19 18:27   ` Lluís Vilanova
2016-09-19 15:54 ` [Qemu-devel] [PATCH v3 00/18] Refactor trace to allow modular build no-reply
2016-09-19 16:00   ` Daniel P. Berrange
2016-09-19 17:13 ` no-reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160920131337.GI25490@redhat.com \
    --to=berrange@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).