Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp11719691rwd; Thu, 22 Jun 2023 18:04:16 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6PyM/JXzpBARZxRvwT1JKzhOpiJ2x/1HLURQmCwTtPCqmivbqH/A3g+ycLUSZwYrBR3OVU X-Received: by 2002:a17:902:7206:b0:1b5:532e:33b8 with SMTP id ba6-20020a170902720600b001b5532e33b8mr8226849plb.35.1687482255777; Thu, 22 Jun 2023 18:04:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687482255; cv=none; d=google.com; s=arc-20160816; b=BhxZB1ybQOB12c6V8LuWWNPPVSL6zbiagTnzhzlqscQ3y9uT3XOwRZLtx/ZxM5Kbye 8mq9ipeEIQRMk0Wpw5jWIk1BcneqA6Au75D0l9XggkU5eAWtgttOFEd1jD5mxJxGxW7i 58Hc64dKlwN3LExKbsAvLIOHrougYVYED8q67/gf6wK3sNDKr52aMTO28+iVUYM/s9/K BzNaOP9mWJT+9i4CCITBs05lWcJXyV4+RQRtblDRrrGr6+o5reEiF0nxq+Au0Divkorv Zr+5kZ+aa0q28oANkc5IpnCyJLxqyg/oyMR822Evd4TOGzTIMQUSrJ+yYk7iTeBhoEtb SNAw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=Ikq5b9kgTceT+X+WpMJVM1coIwpvRfiKLd0ATl6S7OQ=; b=JVeAwZvucU4P1E5VKnQpJXpBbaSOnZD52Pt6olhvbh+WgKFJQN/Yt8F/y6Be+Hx6R9 ALeUF9U/yXkwI8QaAU0xGi9MRpEhYqNi2HmLexgdHqRqbe9uZwCSa3Nbtn5uvcL/knPs LHQYLAEAgDboD9TmFBrkF72RYadYgJPIXwA2Jc3uA2GiO8K/Tk20OFwrsScw7jO4QPXs XYujVPe8huY1z0wJo2XQlSKt/+qYtm6FZXsOZRk5SMKKb5eIWyo41t+51MkGsSCYJK/Z FxMmWENmXKvtx7GJYhrf7wjLkZ1/61vzWoSWiDcaK9fZSOc3M4CtuNrpScTwm+JrpKkT q3rw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=JUAI62Bo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j18-20020a170902da9200b001a1bbc5bea5si3383644plx.537.2023.06.22.18.03.54; Thu, 22 Jun 2023 18:04:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=JUAI62Bo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231137AbjFWA4t (ORCPT + 99 others); Thu, 22 Jun 2023 20:56:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55402 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230506AbjFWA4r (ORCPT ); Thu, 22 Jun 2023 20:56:47 -0400 Received: from mail-qt1-x82a.google.com (mail-qt1-x82a.google.com [IPv6:2607:f8b0:4864:20::82a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 43853268A for ; Thu, 22 Jun 2023 17:56:43 -0700 (PDT) Received: by mail-qt1-x82a.google.com with SMTP id d75a77b69052e-4007b5bafceso56541cf.1 for ; Thu, 22 Jun 2023 17:56:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1687481802; x=1690073802; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=Ikq5b9kgTceT+X+WpMJVM1coIwpvRfiKLd0ATl6S7OQ=; b=JUAI62BobRApovyUny4rN/CIgEhRU64qfCjg7BoeX/mTynRJsLfPpsZSOElY1GBU6b 4Cg2dAlChBeV5KCkC1a2zCVf7i+EpSe0nPoFHNcb3TiTgkrR2ywCeCbEH+GakIAfs0d4 +eIEAF3ufXOyt2GXc2o6Ne07aX5VIvKmPKtyWk2lMv6JkcaylwzxKPZ6TMNAEhe6Dt13 dMOl6AyO/JCbkK21Rs2lY79wQkWqiLX18brhTk6/iXRvw45RCAuSZkRdXbrYEiPLfGrm 160tU0FbtVi9xuDEZ0rG0QJ2JY3dqL7EXukQAGdMDWrgvnw/H0exCAStJcaMyN9jmfCY VVAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687481802; x=1690073802; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Ikq5b9kgTceT+X+WpMJVM1coIwpvRfiKLd0ATl6S7OQ=; b=AP3AErO7xHRyVCXAf8XRzt8droKzq/Z4kUJ4Rxqf9R1HEvUOh9H9vkYOciz2uj7JGf 19iQUAD5pjddUCTeOPpvvbHaRodxFayAh8dLLDfZg0mZdPujckA9rrNY/EElAgAhxIg7 yAd/eKVGcn0l3cBTglhiH0/GEYCAOS6KBKsYCVByHqSoWmiISaVbblPralOt65fJqjPw z/e9HF7HoqpiIWefNQ5lONmF4SS9474RElXc4vveLZGhGgIPLn7N/6T2Zpp+X9Sy2aQy VsLZafOgvVKMmIaPBWVr72exyoAi0NWRGVzU86yMymqWc5C5dD06pqgO3wzAhbQH60Ft H2kw== X-Gm-Message-State: AC+VfDx7gEOJtJez4cB7bzNU+29u4hKTEyPSAfNKQtYAXOX38H6gDdYS O1x8Cq4m2lhfxkVJpO5v7p3gORUa0PrUCUvpE8dCiQ== X-Received: by 2002:a05:622a:138f:b0:3fd:aaef:3807 with SMTP id o15-20020a05622a138f00b003fdaaef3807mr21214qtk.16.1687481802209; Thu, 22 Jun 2023 17:56:42 -0700 (PDT) MIME-Version: 1.0 References: <20230525220927.3544192-1-namhyung@kernel.org> In-Reply-To: From: Ian Rogers Date: Thu, 22 Jun 2023 17:56:30 -0700 Message-ID: Subject: Re: [PATCH] tools lib subcmd: Show parent options in help To: Namhyung Kim Cc: Arnaldo Carvalho de Melo , Jiri Olsa , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jun 22, 2023 at 3:28=E2=80=AFPM Namhyung Kim = wrote: > > On Wed, Jun 21, 2023 at 12:05=E2=80=AFAM Ian Rogers = wrote: > > > > On Thu, May 25, 2023 at 3:09=E2=80=AFPM Namhyung Kim 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 paren= t, > > > 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 [] [] > > > or: perf ftrace [] -- [] [] > > > or: perf ftrace {trace|latency} [] [] > > > or: perf ftrace {trace|latency} [] -- [] [] > > > > > > -b, --use-bpf Use BPF to measure function latency > > > -n, --use-nsec Use nano-second histogram > > > -T, --trace-funcs > > > Show latency of given function > > > > > > After: > > > > > > $ perf ftrace latency -h > > > > > > Usage: perf ftrace [] [] > > > or: perf ftrace [] -- [] [] > > > or: perf ftrace {trace|latency} [] [] > > > or: perf ftrace {trace|latency} [] -- [] [] > > > > > > -a, --all-cpus System-wide collection from all CPUs > > > -b, --use-bpf Use BPF to measure function latency > > > -C, --cpu List of cpus to monitor > > > -n, --use-nsec Use nano-second histogram > > > -p, --pid Trace on existing process id > > > -T, --trace-funcs > > > Show latency of given function > > > -v, --verbose Be more verbose > > > --tid Trace on existing thread id (exclusive to= --pid) > > > > > > Signed-off-by: Namhyung Kim > > > --- > > > 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/pars= e-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 vo= id *vb) > > > > > > static struct option *options__order(const struct option *opts) > > > { > > > - int nr_opts =3D 0, nr_group =3D 0, len; > > > - const struct option *o =3D opts; > > > - struct option *opt, *ordered, *group; > > > + int nr_opts =3D 0, nr_group =3D 0, nr_parent =3D 0, len; > > > + const struct option *o, *p =3D opts; > > > + struct option *opt, *ordered =3D NULL, *group; > > > > > > - for (o =3D opts; o->type !=3D 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 =3D options->parent) { ... } rather than using a goto? > > > > > + for (o =3D p; o->type !=3D OPTION_END; o++) > > > ++nr_opts; > > > > > > - len =3D sizeof(*o) * (nr_opts + 1); > > > - ordered =3D malloc(len); > > > - if (!ordered) > > > + len =3D 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 =3D .. ; p; p =3D p->parent) for (o =3D p; o->type !=3D OPTION_END; o++) ++nr_opts; len =3D sizeof(*o) * (nr_opts + 1); /* +1 for terminator */ Thanks, Ian > Thanks, > Namhyung > > > > > > Thanks, > > Ian > > > > > + group =3D realloc(ordered, len); > > > + if (!group) > > > goto out; > > > - memcpy(ordered, opts, len); > > > + ordered =3D group; > > > + memcpy(&ordered[nr_parent], p, sizeof(*o) * (nr_opts - nr_par= ent)); > > > + > > > + if (o->parent) { > > > + p =3D o->parent; > > > + nr_parent =3D nr_opts; > > > + goto retry; > > > + } > > > + /* copy the last OPTION_END */ > > > + memcpy(&ordered[nr_opts], o, sizeof(*o)); > > > > > > /* sort each option group individually */ > > > for (opt =3D group =3D ordered; opt->type !=3D OPTION_END; op= t++) { > > > -- > > > 2.41.0.rc0.172.g3f132b7071-goog > > >