linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] tools lib subcmd: Show parent options in help
@ 2023-05-25 22:09 Namhyung Kim
  2023-06-21  7:05 ` Ian Rogers
  0 siblings, 1 reply; 4+ messages in thread
From: Namhyung Kim @ 2023-05-25 22:09 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo, Jiri Olsa
  Cc: Ian Rogers, Adrian Hunter, Peter Zijlstra, Ingo Molnar, LKML,
	linux-perf-users

I've just realized that help message in a subcommand didn't show one
in the parent command.  Since the option parser understands the parent,
display code should do the same.  For example, `perf ftrace latency -h`
should show options in the `perf ftrace` command too.

Before:

  $ perf ftrace latency -h

   Usage: perf ftrace [<options>] [<command>]
      or: perf ftrace [<options>] -- [<command>] [<options>]
      or: perf ftrace {trace|latency} [<options>] [<command>]
      or: perf ftrace {trace|latency} [<options>] -- [<command>] [<options>]

      -b, --use-bpf         Use BPF to measure function latency
      -n, --use-nsec        Use nano-second histogram
      -T, --trace-funcs <func>
                            Show latency of given function

After:

  $ perf ftrace latency -h

   Usage: perf ftrace [<options>] [<command>]
      or: perf ftrace [<options>] -- [<command>] [<options>]
      or: perf ftrace {trace|latency} [<options>] [<command>]
      or: perf ftrace {trace|latency} [<options>] -- [<command>] [<options>]

      -a, --all-cpus        System-wide collection from all CPUs
      -b, --use-bpf         Use BPF to measure function latency
      -C, --cpu <cpu>       List of cpus to monitor
      -n, --use-nsec        Use nano-second histogram
      -p, --pid <pid>       Trace on existing process id
      -T, --trace-funcs <func>
                            Show latency of given function
      -v, --verbose         Be more verbose
          --tid <tid>       Trace on existing thread id (exclusive to --pid)

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/lib/subcmd/parse-options.c | 26 ++++++++++++++++++--------
 1 file changed, 18 insertions(+), 8 deletions(-)

diff --git a/tools/lib/subcmd/parse-options.c b/tools/lib/subcmd/parse-options.c
index 9fa75943f2ed..41de97671c72 100644
--- a/tools/lib/subcmd/parse-options.c
+++ b/tools/lib/subcmd/parse-options.c
@@ -806,18 +806,28 @@ static int option__cmp(const void *va, const void *vb)
 
 static struct option *options__order(const struct option *opts)
 {
-	int nr_opts = 0, nr_group = 0, len;
-	const struct option *o = opts;
-	struct option *opt, *ordered, *group;
+	int nr_opts = 0, nr_group = 0, nr_parent = 0, len;
+	const struct option *o, *p = opts;
+	struct option *opt, *ordered = NULL, *group;
 
-	for (o = opts; o->type != OPTION_END; o++)
+retry:
+	for (o = p; o->type != OPTION_END; o++)
 		++nr_opts;
 
-	len = sizeof(*o) * (nr_opts + 1);
-	ordered = malloc(len);
-	if (!ordered)
+	len = sizeof(*o) * (nr_opts + !o->parent);
+	group = realloc(ordered, len);
+	if (!group)
 		goto out;
-	memcpy(ordered, opts, len);
+	ordered = group;
+	memcpy(&ordered[nr_parent], p, sizeof(*o) * (nr_opts - nr_parent));
+
+	if (o->parent) {
+		p = o->parent;
+		nr_parent = nr_opts;
+		goto retry;
+	}
+	/* copy the last OPTION_END */
+	memcpy(&ordered[nr_opts], o, sizeof(*o));
 
 	/* sort each option group individually */
 	for (opt = group = ordered; opt->type != OPTION_END; opt++) {
-- 
2.41.0.rc0.172.g3f132b7071-goog


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] tools lib subcmd: Show parent options in help
  2023-05-25 22:09 [PATCH] tools lib subcmd: Show parent options in help Namhyung Kim
@ 2023-06-21  7:05 ` Ian Rogers
  2023-06-22 22:28   ` Namhyung Kim
  0 siblings, 1 reply; 4+ messages in thread
From: Ian Rogers @ 2023-06-21  7:05 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Arnaldo Carvalho de Melo, Jiri Olsa, Adrian Hunter,
	Peter Zijlstra, Ingo Molnar, LKML, linux-perf-users

On Thu, May 25, 2023 at 3:09 PM Namhyung Kim <namhyung@kernel.org> wrote:
>
> I've just realized that help message in a subcommand didn't show one
> in the parent command.  Since the option parser understands the parent,
> display code should do the same.  For example, `perf ftrace latency -h`
> should show options in the `perf ftrace` command too.
>
> Before:
>
>   $ perf ftrace latency -h
>
>    Usage: perf ftrace [<options>] [<command>]
>       or: perf ftrace [<options>] -- [<command>] [<options>]
>       or: perf ftrace {trace|latency} [<options>] [<command>]
>       or: perf ftrace {trace|latency} [<options>] -- [<command>] [<options>]
>
>       -b, --use-bpf         Use BPF to measure function latency
>       -n, --use-nsec        Use nano-second histogram
>       -T, --trace-funcs <func>
>                             Show latency of given function
>
> After:
>
>   $ perf ftrace latency -h
>
>    Usage: perf ftrace [<options>] [<command>]
>       or: perf ftrace [<options>] -- [<command>] [<options>]
>       or: perf ftrace {trace|latency} [<options>] [<command>]
>       or: perf ftrace {trace|latency} [<options>] -- [<command>] [<options>]
>
>       -a, --all-cpus        System-wide collection from all CPUs
>       -b, --use-bpf         Use BPF to measure function latency
>       -C, --cpu <cpu>       List of cpus to monitor
>       -n, --use-nsec        Use nano-second histogram
>       -p, --pid <pid>       Trace on existing process id
>       -T, --trace-funcs <func>
>                             Show latency of given function
>       -v, --verbose         Be more verbose
>           --tid <tid>       Trace on existing thread id (exclusive to --pid)
>
> Signed-off-by: Namhyung Kim <namhyung@kernel.org>
> ---
>  tools/lib/subcmd/parse-options.c | 26 ++++++++++++++++++--------
>  1 file changed, 18 insertions(+), 8 deletions(-)
>
> diff --git a/tools/lib/subcmd/parse-options.c b/tools/lib/subcmd/parse-options.c
> index 9fa75943f2ed..41de97671c72 100644
> --- a/tools/lib/subcmd/parse-options.c
> +++ b/tools/lib/subcmd/parse-options.c
> @@ -806,18 +806,28 @@ static int option__cmp(const void *va, const void *vb)
>
>  static struct option *options__order(const struct option *opts)
>  {
> -       int nr_opts = 0, nr_group = 0, len;
> -       const struct option *o = opts;
> -       struct option *opt, *ordered, *group;
> +       int nr_opts = 0, nr_group = 0, nr_parent = 0, len;
> +       const struct option *o, *p = opts;
> +       struct option *opt, *ordered = NULL, *group;
>
> -       for (o = opts; o->type != OPTION_END; o++)
> +retry:

Why use "goto retry" and not compute the size with the parent upfront?

> +       for (o = p; o->type != OPTION_END; o++)
>                 ++nr_opts;
>
> -       len = sizeof(*o) * (nr_opts + 1);
> -       ordered = malloc(len);
> -       if (!ordered)
> +       len = sizeof(*o) * (nr_opts + !o->parent);

It'd be nice to comment on why the "!o->parent" here.

Thanks,
Ian

> +       group = realloc(ordered, len);
> +       if (!group)
>                 goto out;
> -       memcpy(ordered, opts, len);
> +       ordered = group;
> +       memcpy(&ordered[nr_parent], p, sizeof(*o) * (nr_opts - nr_parent));
> +
> +       if (o->parent) {
> +               p = o->parent;
> +               nr_parent = nr_opts;
> +               goto retry;
> +       }
> +       /* copy the last OPTION_END */
> +       memcpy(&ordered[nr_opts], o, sizeof(*o));
>
>         /* sort each option group individually */
>         for (opt = group = ordered; opt->type != OPTION_END; opt++) {
> --
> 2.41.0.rc0.172.g3f132b7071-goog
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] tools lib subcmd: Show parent options in help
  2023-06-21  7:05 ` Ian Rogers
@ 2023-06-22 22:28   ` Namhyung Kim
  2023-06-23  0:56     ` Ian Rogers
  0 siblings, 1 reply; 4+ messages in thread
From: Namhyung Kim @ 2023-06-22 22:28 UTC (permalink / raw)
  To: Ian Rogers
  Cc: Arnaldo Carvalho de Melo, Jiri Olsa, Adrian Hunter,
	Peter Zijlstra, Ingo Molnar, LKML, linux-perf-users

On Wed, Jun 21, 2023 at 12:05 AM Ian Rogers <irogers@google.com> wrote:
>
> On Thu, May 25, 2023 at 3:09 PM Namhyung Kim <namhyung@kernel.org> wrote:
> >
> > I've just realized that help message in a subcommand didn't show one
> > in the parent command.  Since the option parser understands the parent,
> > display code should do the same.  For example, `perf ftrace latency -h`
> > should show options in the `perf ftrace` command too.
> >
> > Before:
> >
> >   $ perf ftrace latency -h
> >
> >    Usage: perf ftrace [<options>] [<command>]
> >       or: perf ftrace [<options>] -- [<command>] [<options>]
> >       or: perf ftrace {trace|latency} [<options>] [<command>]
> >       or: perf ftrace {trace|latency} [<options>] -- [<command>] [<options>]
> >
> >       -b, --use-bpf         Use BPF to measure function latency
> >       -n, --use-nsec        Use nano-second histogram
> >       -T, --trace-funcs <func>
> >                             Show latency of given function
> >
> > After:
> >
> >   $ perf ftrace latency -h
> >
> >    Usage: perf ftrace [<options>] [<command>]
> >       or: perf ftrace [<options>] -- [<command>] [<options>]
> >       or: perf ftrace {trace|latency} [<options>] [<command>]
> >       or: perf ftrace {trace|latency} [<options>] -- [<command>] [<options>]
> >
> >       -a, --all-cpus        System-wide collection from all CPUs
> >       -b, --use-bpf         Use BPF to measure function latency
> >       -C, --cpu <cpu>       List of cpus to monitor
> >       -n, --use-nsec        Use nano-second histogram
> >       -p, --pid <pid>       Trace on existing process id
> >       -T, --trace-funcs <func>
> >                             Show latency of given function
> >       -v, --verbose         Be more verbose
> >           --tid <tid>       Trace on existing thread id (exclusive to --pid)
> >
> > Signed-off-by: Namhyung Kim <namhyung@kernel.org>
> > ---
> >  tools/lib/subcmd/parse-options.c | 26 ++++++++++++++++++--------
> >  1 file changed, 18 insertions(+), 8 deletions(-)
> >
> > diff --git a/tools/lib/subcmd/parse-options.c b/tools/lib/subcmd/parse-options.c
> > index 9fa75943f2ed..41de97671c72 100644
> > --- a/tools/lib/subcmd/parse-options.c
> > +++ b/tools/lib/subcmd/parse-options.c
> > @@ -806,18 +806,28 @@ static int option__cmp(const void *va, const void *vb)
> >
> >  static struct option *options__order(const struct option *opts)
> >  {
> > -       int nr_opts = 0, nr_group = 0, len;
> > -       const struct option *o = opts;
> > -       struct option *opt, *ordered, *group;
> > +       int nr_opts = 0, nr_group = 0, nr_parent = 0, len;
> > +       const struct option *o, *p = opts;
> > +       struct option *opt, *ordered = NULL, *group;
> >
> > -       for (o = opts; o->type != OPTION_END; o++)
> > +retry:
>
> Why use "goto retry" and not compute the size with the parent upfront?

No specific reason, just followed the same pattern as in
parse_{short,long}_opt(). :)

>
> > +       for (o = p; o->type != OPTION_END; o++)
> >                 ++nr_opts;
> >
> > -       len = sizeof(*o) * (nr_opts + 1);
> > -       ordered = malloc(len);
> > -       if (!ordered)
> > +       len = sizeof(*o) * (nr_opts + !o->parent);
>
> It'd be nice to comment on why the "!o->parent" here.

What about this?

/* It needs a terminating NULL entry when there's no parent */

Thanks,
Namhyung


>
> Thanks,
> Ian
>
> > +       group = realloc(ordered, len);
> > +       if (!group)
> >                 goto out;
> > -       memcpy(ordered, opts, len);
> > +       ordered = group;
> > +       memcpy(&ordered[nr_parent], p, sizeof(*o) * (nr_opts - nr_parent));
> > +
> > +       if (o->parent) {
> > +               p = o->parent;
> > +               nr_parent = nr_opts;
> > +               goto retry;
> > +       }
> > +       /* copy the last OPTION_END */
> > +       memcpy(&ordered[nr_opts], o, sizeof(*o));
> >
> >         /* sort each option group individually */
> >         for (opt = group = ordered; opt->type != OPTION_END; opt++) {
> > --
> > 2.41.0.rc0.172.g3f132b7071-goog
> >

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] tools lib subcmd: Show parent options in help
  2023-06-22 22:28   ` Namhyung Kim
@ 2023-06-23  0:56     ` Ian Rogers
  0 siblings, 0 replies; 4+ messages in thread
From: Ian Rogers @ 2023-06-23  0:56 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Arnaldo Carvalho de Melo, Jiri Olsa, Adrian Hunter,
	Peter Zijlstra, Ingo Molnar, LKML, linux-perf-users

On Thu, Jun 22, 2023 at 3:28 PM Namhyung Kim <namhyung@kernel.org> wrote:
>
> On Wed, Jun 21, 2023 at 12:05 AM Ian Rogers <irogers@google.com> wrote:
> >
> > On Thu, May 25, 2023 at 3:09 PM Namhyung Kim <namhyung@kernel.org> wrote:
> > >
> > > I've just realized that help message in a subcommand didn't show one
> > > in the parent command.  Since the option parser understands the parent,
> > > display code should do the same.  For example, `perf ftrace latency -h`
> > > should show options in the `perf ftrace` command too.
> > >
> > > Before:
> > >
> > >   $ perf ftrace latency -h
> > >
> > >    Usage: perf ftrace [<options>] [<command>]
> > >       or: perf ftrace [<options>] -- [<command>] [<options>]
> > >       or: perf ftrace {trace|latency} [<options>] [<command>]
> > >       or: perf ftrace {trace|latency} [<options>] -- [<command>] [<options>]
> > >
> > >       -b, --use-bpf         Use BPF to measure function latency
> > >       -n, --use-nsec        Use nano-second histogram
> > >       -T, --trace-funcs <func>
> > >                             Show latency of given function
> > >
> > > After:
> > >
> > >   $ perf ftrace latency -h
> > >
> > >    Usage: perf ftrace [<options>] [<command>]
> > >       or: perf ftrace [<options>] -- [<command>] [<options>]
> > >       or: perf ftrace {trace|latency} [<options>] [<command>]
> > >       or: perf ftrace {trace|latency} [<options>] -- [<command>] [<options>]
> > >
> > >       -a, --all-cpus        System-wide collection from all CPUs
> > >       -b, --use-bpf         Use BPF to measure function latency
> > >       -C, --cpu <cpu>       List of cpus to monitor
> > >       -n, --use-nsec        Use nano-second histogram
> > >       -p, --pid <pid>       Trace on existing process id
> > >       -T, --trace-funcs <func>
> > >                             Show latency of given function
> > >       -v, --verbose         Be more verbose
> > >           --tid <tid>       Trace on existing thread id (exclusive to --pid)
> > >
> > > Signed-off-by: Namhyung Kim <namhyung@kernel.org>
> > > ---
> > >  tools/lib/subcmd/parse-options.c | 26 ++++++++++++++++++--------
> > >  1 file changed, 18 insertions(+), 8 deletions(-)
> > >
> > > diff --git a/tools/lib/subcmd/parse-options.c b/tools/lib/subcmd/parse-options.c
> > > index 9fa75943f2ed..41de97671c72 100644
> > > --- a/tools/lib/subcmd/parse-options.c
> > > +++ b/tools/lib/subcmd/parse-options.c
> > > @@ -806,18 +806,28 @@ static int option__cmp(const void *va, const void *vb)
> > >
> > >  static struct option *options__order(const struct option *opts)
> > >  {
> > > -       int nr_opts = 0, nr_group = 0, len;
> > > -       const struct option *o = opts;
> > > -       struct option *opt, *ordered, *group;
> > > +       int nr_opts = 0, nr_group = 0, nr_parent = 0, len;
> > > +       const struct option *o, *p = opts;
> > > +       struct option *opt, *ordered = NULL, *group;
> > >
> > > -       for (o = opts; o->type != OPTION_END; o++)
> > > +retry:
> >
> > Why use "goto retry" and not compute the size with the parent upfront?
>
> No specific reason, just followed the same pattern as in
> parse_{short,long}_opt(). :)

:-) Could those loops not be:

for (; options; options = options->parent) {
...
}

rather than using a goto?

> >
> > > +       for (o = p; o->type != OPTION_END; o++)
> > >                 ++nr_opts;
> > >
> > > -       len = sizeof(*o) * (nr_opts + 1);
> > > -       ordered = malloc(len);
> > > -       if (!ordered)
> > > +       len = sizeof(*o) * (nr_opts + !o->parent);
> >
> > It'd be nice to comment on why the "!o->parent" here.
>
> What about this?
>
> /* It needs a terminating NULL entry when there's no parent */

Perhaps:
the length is given by the number of options plus a null terminator
for the last loop iteration.

It may be cleaner to just compute this after nr_opts settles. perhaps
something along the lines of:

for (p = .. ; p; p = p->parent)
   for (o = p; o->type != OPTION_END; o++)
                 ++nr_opts;
len = sizeof(*o) * (nr_opts + 1); /* +1 for terminator */

Thanks,
Ian

> Thanks,
> Namhyung
>
>
> >
> > Thanks,
> > Ian
> >
> > > +       group = realloc(ordered, len);
> > > +       if (!group)
> > >                 goto out;
> > > -       memcpy(ordered, opts, len);
> > > +       ordered = group;
> > > +       memcpy(&ordered[nr_parent], p, sizeof(*o) * (nr_opts - nr_parent));
> > > +
> > > +       if (o->parent) {
> > > +               p = o->parent;
> > > +               nr_parent = nr_opts;
> > > +               goto retry;
> > > +       }
> > > +       /* copy the last OPTION_END */
> > > +       memcpy(&ordered[nr_opts], o, sizeof(*o));
> > >
> > >         /* sort each option group individually */
> > >         for (opt = group = ordered; opt->type != OPTION_END; opt++) {
> > > --
> > > 2.41.0.rc0.172.g3f132b7071-goog
> > >

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-06-23  0:56 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-25 22:09 [PATCH] tools lib subcmd: Show parent options in help Namhyung Kim
2023-06-21  7:05 ` Ian Rogers
2023-06-22 22:28   ` Namhyung Kim
2023-06-23  0:56     ` Ian Rogers

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).