Received: by 2002:ab2:620c:0:b0:1ef:ffd0:ce49 with SMTP id o12csp1569712lqt; Wed, 20 Mar 2024 07:54:31 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVyt+1egXS4fiqelzC5J6xU2cxBxYQIT+NSKrTOJ9p3J1eMOtS47LPLNjrOsjSgctaANAWXJ80W3iNd/C8j/YFLSHJd8N9uFVOT+fq82A== X-Google-Smtp-Source: AGHT+IElywHQpPcO4yH9CCxAVW8hUr2VXC8lUrADSo5bWNoRHM5Ur4kMo5DK3DNkQUNntXomUEOV X-Received: by 2002:a17:903:4281:b0:1dd:e115:336c with SMTP id ju1-20020a170903428100b001dde115336cmr16740646plb.69.1710946471160; Wed, 20 Mar 2024 07:54:31 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710946471; cv=pass; d=google.com; s=arc-20160816; b=pCyPtj7KjO7bRp/r5Y7rXZXZrDe7zZa5eqUjJmyeB8PAhHLCwhX8SGjFajjCCtV89j UY1qypLrldo8SAZCVgjqoDdb9Ph0Ylv2HMh5ccEUJjpQzXKK/jaSLi3ZgOdid9zPtG0U c2ywKC0r1ViaJcAC4Si+heDO/NS9THjUTIs0/QnvLY1IFILn8m26a7Sdr9jxVXzxOHeu 8ygwBCJ4LgCELFBAXHKtdRDeAdpjvGW7kEP6w2RNGArCPDaGgDgKZdHjwr9/fwNviydG JdmCVC8pkbXQIFgRyNU3pgbvVBX1afncL1mROOrXoz2qSNPsWLhqeJjkQqOrIE7Az+Nd l/jA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:message-id:subject:cc:to:from:date:dkim-signature; bh=fWIubYuKOqAzmX70XM9Eajik4+T6tzkFqAkd5RGl8qs=; fh=+nf3xsUObfH9Yp5NDiwu6oI02KIu+MFZ4MEudg/3pt8=; b=qyK+sIwZREfTBmfoBsN62bWI/8ZvFhbdd/dC7cXRomzA6VpziDOR9Sk2UqMaEPJB/C R12GSoLSWT+401wh28uQhNU2pwgZBzRZHFDt77aqoG6LiyFv0y8/c+m3Wd/Sa50iGi1R mV/G6mKh0eb+LiyXQDZmGsEgEa3SK1o947vB/Ywff+0+dnlsOzYeg/+zp1FdAHieowuB lAM3YjbpT9yGc8FJN5Ks4H8r/vf2cVH0c5uVQwUws68J3x82Ji0DIy6G+48XigfcexEv TJWTXp1byhfo1ruSZ5a8lUjYIWxOXMPdXTibyJd1TOdBE20TBA+jsEf0nTXz2ltXJYES WNMw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=utU2qiEf; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-109033-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-109033-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id q13-20020a170902dacd00b001dba722cf4csi13674687plx.40.2024.03.20.07.54.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Mar 2024 07:54:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-109033-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=utU2qiEf; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-109033-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-109033-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 0FDCCB20CD8 for ; Wed, 20 Mar 2024 14:54:25 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D6BDE3D54C; Wed, 20 Mar 2024 14:54:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="utU2qiEf" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CC84B4AECF; Wed, 20 Mar 2024 14:54:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710946448; cv=none; b=mbg67HbYoTzRtmuvGobZeHmAIDVi0f4ioohRpJyYjJgvaA0Oeix+mD9vhXu5u+P2E+4GHyy9QM/6qeaxDwVmBVaMibdnDP4rCDoAjUxrWUuxGHyq+xunRZ4JDSzg69ATfdmqelKY6QOytzOdRxayyAWRROw1708knnBAn7/BpmU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710946448; c=relaxed/simple; bh=6xGklQNGbFkNZFhkj91Z7jnuhVtnP2riuP9QNjlSbko=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=cSbRqivmA0f/I8U51qa32fHS4eXaOfdJ+pczH8b3gajHmlvq++lEG3iuPpSVrcmQsHmzupv+6QQMw9QU31o9CW9W2QIl5ZjDgvgJuPINcDEm3w3FwWtTC4Pkn+eFZA58DQ7VNoSfHgEdKFflk71xeMtTeLH0yK8n1FunYLT00Uk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=utU2qiEf; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 552A6C433C7; Wed, 20 Mar 2024 14:54:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1710946448; bh=6xGklQNGbFkNZFhkj91Z7jnuhVtnP2riuP9QNjlSbko=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=utU2qiEfaChUDz67ams9qMH9uEXZKmngT+A7+9ymNRyj6cPi2QRs061blBCfEdkWC CiNDbhQgLNp5nPe/q+kL3w/X3Dwf8cZm9SIggQgy4bQiSmCgICyQHMxqfCaykDzP5Q aykqNFdjuaUGAp1LzOiHvgKViFVHKg9VB75/86WkxriAiKek/RVHh2kOagPfOTIcel Vt3B878OsnxJ5xco+zPf68DpFjBaEjFEXF2JfpxAQA2BP15gtemuE0UaaPu7+24XwL fXsleQCwQH6mEOO0MwbISJhT/0r8n4O0sihOhptXFH6xUJ1/UO7y3W5y/1fSawTBh1 9n2neKq/ztrXQ== Date: Wed, 20 Mar 2024 11:54:05 -0300 From: Arnaldo Carvalho de Melo To: Ian Rogers Cc: Peter Zijlstra , Ingo Molnar , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Adrian Hunter , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2 1/2] perf: Suggest inbuilt commands for unknown command Message-ID: References: <20240301201306.2680986-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20240301201306.2680986-1-irogers@google.com> On Fri, Mar 01, 2024 at 12:13:05PM -0800, Ian Rogers wrote: > The existing unknown command code looks for perf scripts like > perf-archive.sh and perf-iostat.sh, however, inbuilt commands aren't > suggested. Add the inbuilt commands so they may be suggested too. > > Before: > ``` > $ perf reccord > perf: 'reccord' is not a perf-command. See 'perf --help'. > ``` > > After: > ``` > $ perf reccord > perf: 'reccord' is not a perf-command. See 'perf --help'. > > Did you mean this? > record > ``` > > Signed-off-by: Ian Rogers > --- > v2. Drops a merged patch and rebases. No functional change. Arnaldo > reported the patch not working for him, but I've not found a Not working: root@number:~# perf reccord Failed to run command 'reccord': No such file or directory root@number:~# ⬢[acme@toolbox perf-tools-next]$ git log --oneline -1 a65ef8052854ba75 (HEAD) perf: Suggest inbuilt commands for unknown command ⬢[acme@toolbox perf-tools-next]$ I use O= with install-bin, trying: ⬢[acme@toolbox perf-tools-next]$ make -C tools/perf install-bin ⬢[acme@toolbox perf-tools-next]$ perf raccord Failed to run command 'raccord': No such file or directory ⬢[acme@toolbox perf-tools-next]$ Also didn't work Trying to figure this out... - Arnaldo > reproduction and it works for me: > https://lore.kernel.org/lkml/ZZcdDyyADG8dP8LM@kernel.org/ > --- > tools/perf/builtin.h | 4 ++- > tools/perf/perf.c | 21 +++++++++++--- > tools/perf/util/help-unknown-cmd.c | 45 ++++++++++++++---------------- > 3 files changed, 41 insertions(+), 29 deletions(-) > > diff --git a/tools/perf/builtin.h b/tools/perf/builtin.h > index f2ab5bae2150..f4375deabfa3 100644 > --- a/tools/perf/builtin.h > +++ b/tools/perf/builtin.h > @@ -2,8 +2,10 @@ > #ifndef BUILTIN_H > #define BUILTIN_H > > +struct cmdnames; > + > void list_common_cmds_help(void); > -const char *help_unknown_cmd(const char *cmd); > +const char *help_unknown_cmd(const char *cmd, struct cmdnames *main_cmds); > > int cmd_annotate(int argc, const char **argv); > int cmd_bench(int argc, const char **argv); > diff --git a/tools/perf/perf.c b/tools/perf/perf.c > index 921bee0a6437..c719e6ccd9e2 100644 > --- a/tools/perf/perf.c > +++ b/tools/perf/perf.c > @@ -18,6 +18,7 @@ > #include > #include "util/parse-events.h" > #include > +#include > #include "util/debug.h" > #include "util/event.h" > #include "util/util.h" // usage() > @@ -557,7 +558,7 @@ int main(int argc, const char **argv) > pthread__block_sigwinch(); > > while (1) { > - static int done_help; > + int done_help; > > run_argv(&argc, &argv); > > @@ -565,14 +566,26 @@ int main(int argc, const char **argv) > break; > > if (!done_help) { > - cmd = argv[0] = help_unknown_cmd(cmd); > + struct cmdnames main_cmds; > + > + for (unsigned int i = 0; i < ARRAY_SIZE(commands); i++) { > + add_cmdname(&main_cmds, > + commands[i].cmd, > + strlen(commands[i].cmd)); > + } > + cmd = argv[0] = help_unknown_cmd(cmd, &main_cmds); > + clean_cmdnames(&main_cmds); > done_help = 1; > + if (!cmd) > + break; > } else > break; > } > > - fprintf(stderr, "Failed to run command '%s': %s\n", > - cmd, str_error_r(errno, sbuf, sizeof(sbuf))); > + if (cmd) { > + fprintf(stderr, "Failed to run command '%s': %s\n", > + cmd, str_error_r(errno, sbuf, sizeof(sbuf))); > + } > out: > if (debug_fp) > fclose(debug_fp); > diff --git a/tools/perf/util/help-unknown-cmd.c b/tools/perf/util/help-unknown-cmd.c > index eab99ea6ac01..2ba3369f1620 100644 > --- a/tools/perf/util/help-unknown-cmd.c > +++ b/tools/perf/util/help-unknown-cmd.c > @@ -52,46 +52,44 @@ static int add_cmd_list(struct cmdnames *cmds, struct cmdnames *old) > return 0; > } > > -const char *help_unknown_cmd(const char *cmd) > +const char *help_unknown_cmd(const char *cmd, struct cmdnames *main_cmds) > { > unsigned int i, n = 0, best_similarity = 0; > - struct cmdnames main_cmds, other_cmds; > + struct cmdnames other_cmds; > > - memset(&main_cmds, 0, sizeof(main_cmds)); > - memset(&other_cmds, 0, sizeof(main_cmds)); > + memset(&other_cmds, 0, sizeof(other_cmds)); > > perf_config(perf_unknown_cmd_config, NULL); > > - load_command_list("perf-", &main_cmds, &other_cmds); > + load_command_list("perf-", main_cmds, &other_cmds); > > - if (add_cmd_list(&main_cmds, &other_cmds) < 0) { > + if (add_cmd_list(main_cmds, &other_cmds) < 0) { > fprintf(stderr, "ERROR: Failed to allocate command list for unknown command.\n"); > goto end; > } > - qsort(main_cmds.names, main_cmds.cnt, > - sizeof(main_cmds.names), cmdname_compare); > - uniq(&main_cmds); > + qsort(main_cmds->names, main_cmds->cnt, > + sizeof(main_cmds->names), cmdname_compare); > + uniq(main_cmds); > > - if (main_cmds.cnt) { > + if (main_cmds->cnt) { > /* This reuses cmdname->len for similarity index */ > - for (i = 0; i < main_cmds.cnt; ++i) > - main_cmds.names[i]->len = > - levenshtein(cmd, main_cmds.names[i]->name, 0, 2, 1, 4); > + for (i = 0; i < main_cmds->cnt; ++i) > + main_cmds->names[i]->len = > + levenshtein(cmd, main_cmds->names[i]->name, 0, 2, 1, 4); > > - qsort(main_cmds.names, main_cmds.cnt, > - sizeof(*main_cmds.names), levenshtein_compare); > + qsort(main_cmds->names, main_cmds->cnt, > + sizeof(*main_cmds->names), levenshtein_compare); > > - best_similarity = main_cmds.names[0]->len; > + best_similarity = main_cmds->names[0]->len; > n = 1; > - while (n < main_cmds.cnt && best_similarity == main_cmds.names[n]->len) > + while (n < main_cmds->cnt && best_similarity == main_cmds->names[n]->len) > ++n; > } > > if (autocorrect && n == 1) { > - const char *assumed = main_cmds.names[0]->name; > + const char *assumed = main_cmds->names[0]->name; > > - main_cmds.names[0] = NULL; > - clean_cmdnames(&main_cmds); > + main_cmds->names[0] = NULL; > clean_cmdnames(&other_cmds); > fprintf(stderr, "WARNING: You called a perf program named '%s', " > "which does not exist.\n" > @@ -107,15 +105,14 @@ const char *help_unknown_cmd(const char *cmd) > > fprintf(stderr, "perf: '%s' is not a perf-command. See 'perf --help'.\n", cmd); > > - if (main_cmds.cnt && best_similarity < 6) { > + if (main_cmds->cnt && best_similarity < 6) { > fprintf(stderr, "\nDid you mean %s?\n", > n < 2 ? "this": "one of these"); > > for (i = 0; i < n; i++) > - fprintf(stderr, "\t%s\n", main_cmds.names[i]->name); > + fprintf(stderr, "\t%s\n", main_cmds->names[i]->name); > } > end: > - clean_cmdnames(&main_cmds); > clean_cmdnames(&other_cmds); > - exit(1); > + return NULL; > } > -- > 2.44.0.278.ge034bb2e1d-goog >