Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp11605962rwd; Thu, 22 Jun 2023 16:06:38 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6GLxwHd1wWyyCn4+B28v0J+6Tyg8IjDI1y7C1hsp2HQcG2b6qZgHsRAXVMOYa58q8rPr+D X-Received: by 2002:a05:6808:1707:b0:39e:5892:8539 with SMTP id bc7-20020a056808170700b0039e58928539mr30636618oib.9.1687475198549; Thu, 22 Jun 2023 16:06:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687475198; cv=none; d=google.com; s=arc-20160816; b=aXNH7smPDpxUlkxRAi9eeq//fs0bcvmKM52TESKC+6YQxma9qOig4VGeH+Ip7nkjDQ OWrbbtfMKzktQ7SdwdqzUcciBiA+r0Kgd+YSkck8PoxG9+XlsroDEm3SVOj3ZWPwhU39 6bPOv5xSNLFiHxBsIhlt9VPhTgN0MThvlf6c29YEHAUhdHA4Y7a57BZUcnlnkSPbz/Mi diWnTOcD1ToFEYyOJwA0pDHboWuWqPs3dsWrkAta8Iq+hcukkYvBcAc7Q+YFiijwmGr7 lz8tDxcPULEBEeaO56kSQGe6AZOkPnrY99rp241xRxHUr3FRJ5NogCHfGGBhifL9fFr9 hMfw== 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; bh=z9nGQJSvsmUvsts0QG02dxzW0HpLeeaMkYEZ+8r4kkM=; b=qUuVVuGi48EjNN7jTve1Nz2DLKmIR5OlNRloJDMGNZtRcG2ABrnrAUXrLp6c6K9LjI Pa8MkecREbQ/vE3ItAmhebdDdssJHVG1tyN4lVLvFMAX4ie/5iwfYpTIQ2+/BsbHDM+q U3IaTjqzE5rnOevTZBbBHYXPeHZTcd9emL+QFe7B3tbmryzkOsfQdJkTrnYS5BuuTOR7 7n9N4xIu68IxFMm3tSV9rXTXlNnIfAIx0XJ6nvfY+Izt6hLMHlO/pV6egsMTPs2jxdH1 3lJoclpsCb/qSJt+9St2xmzfEA9X4XixVZfkSUWGexxsoO1BqK7Z7m6axIP1pi301Jow JBXg== ARC-Authentication-Results: i=1; mx.google.com; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t23-20020a63b257000000b0055339319e07si7670404pgo.755.2023.06.22.16.06.26; Thu, 22 Jun 2023 16:06:38 -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; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230243AbjFVW2q convert rfc822-to-8bit (ORCPT + 99 others); Thu, 22 Jun 2023 18:28:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51600 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229673AbjFVW2o (ORCPT ); Thu, 22 Jun 2023 18:28:44 -0400 Received: from mail-yb1-f174.google.com (mail-yb1-f174.google.com [209.85.219.174]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 28B9B195; Thu, 22 Jun 2023 15:28:43 -0700 (PDT) Received: by mail-yb1-f174.google.com with SMTP id 3f1490d57ef6-bc476bf5239so7519434276.2; Thu, 22 Jun 2023 15:28:43 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687472922; x=1690064922; 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=1cguTASJCALnnEHz7EV9F2VapKWryIEdNJ4Yyp61gRQ=; b=aa/aybTHtSDC94rPyiFQVVG4g63TieaKlAEqRK7mbTO7q7FjhxqERsB7Z7OSVaJiBE 0RKWISZcUy4HogggUjxV1rMq1b3BRVyZVOhkXZBFRmb2NfOsB49Odlegd+iuhRl3x1Rb XEpsIFxvegZs7CsR+noOqnBJLwpL1dhwIVKyDSkWracjyxFg5zGaH0y/ioJJaazcKNSF Uk5YCTIjZ3v1gz0muTmk/jM/sIoyX2/8d8hfQ4KNKO8y7CV+u4zLqxHx019/AHEJPYNR dayjOWNSqHSCzPFhAykKBfLgic2/6Ajs5NY0oSqfRXhCTq/LnDTsRLlTHkhaKaLpvnTv cuCQ== X-Gm-Message-State: AC+VfDwEhM+EHuRQellWmuxIkFiVD2EuTsCAtg8ufaBFyXI9S9rI5kes YW7R1Y/wBNDLmzx5Nq8tbVkmjbAZpRaEJkPYvJMQolyA X-Received: by 2002:a5b:906:0:b0:ba7:54ab:d676 with SMTP id a6-20020a5b0906000000b00ba754abd676mr15682550ybq.63.1687472922187; Thu, 22 Jun 2023 15:28:42 -0700 (PDT) MIME-Version: 1.0 References: <20230525220927.3544192-1-namhyung@kernel.org> In-Reply-To: From: Namhyung Kim Date: Thu, 22 Jun 2023 15:28:31 -0700 Message-ID: Subject: Re: [PATCH] tools lib subcmd: Show parent options in help To: Ian Rogers 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: 8BIT X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no 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 Wed, Jun 21, 2023 at 12:05 AM Ian Rogers wrote: > > On Thu, May 25, 2023 at 3:09 PM 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 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 [] [] > > 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/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 > >