From patchwork Fri Dec 8 00:05:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 175470 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp5143489vqy; Thu, 7 Dec 2023 16:06:05 -0800 (PST) X-Google-Smtp-Source: AGHT+IEpTHIDp0l0RV7GTpygM/6gKoXKcwl47KUPb/ABaClqcoJuPthc3cgqUpcSsEPzqaT1ZqbI X-Received: by 2002:a17:903:41ce:b0:1d0:6ffd:611c with SMTP id u14-20020a17090341ce00b001d06ffd611cmr48053ple.62.1701993964952; Thu, 07 Dec 2023 16:06:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701993964; cv=none; d=google.com; s=arc-20160816; b=uvEA6U7EXjzzUevssXl8+poIBxLdOEOWHCxwy4p4824hKlSAFoddBq8RCZbR2uh2Zi 1yHB3v5A1mqOmcLbsdCaAmgq30mf87lYx0fF48gDrCvYSzmkPG/QvdaST3cr5tJJyucY AGtTXw8751InQrBpk1zp0Swr47gKUDVIGszdjh0mvJ67YtjmUI8iMUXogugR3hefQeuc yqdZrR+kwGYn6A00t4+WdtryhGyzOYL+jejpJyMOfsokqRfM4N+z/iRMjRXY+UpWBsa/ XJkH4tqB8mcMy1lmqYbI7kHzY8ud51uCpKlTpeH1+35+DnHP8NaWwZs/GD65s/7K4ON5 xsCw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=Xf5gnEGQIoXazSeiXhfMb12CybARggvMyxZM26gKG4Q=; fh=TIE+Q456lum2w6CNS8hGW8/qeLzq5fdQr2L1Nx0bPN4=; b=wvWOIpHCQktILbC84CDNE4r2KKkez5TDGr+O4ukDdzeAp4RhRom2ZhWT2nak0pMMlS 8V2LUh0Ip3sQ5grQlTRAHk+GK/iOOTOLdYCfkLHgsHHEqn2bYOxyE+C0Am3LzH6EWZjN Z7DFsyUbfZzyLIb1nr8cnn3dtdbo+8NHAhzvQAlld+8r6H3hM7l7VQVUloS582t+8q2z 8wKukIWCcn88gq2OVilgLt7uO0asF0OCVU2SrrdRJX++M0Je2hI0YnNJ4jkrL7c8UE7e Uii85TTMAYJ2j0ik55TBgQuyKd8epggqOE1Pz97u/wBC/xo2U+Rp/Fb9Jlj2zBYKgrWt 0bfQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=kv6RQLIe; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id e15-20020a170902ed8f00b001cf5cb5ac88si396670plj.486.2023.12.07.16.06.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Dec 2023 16:06:04 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=kv6RQLIe; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 49CC5853A0E9; Thu, 7 Dec 2023 16:05:35 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235348AbjLHAFY (ORCPT + 99 others); Thu, 7 Dec 2023 19:05:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46600 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232782AbjLHAFW (ORCPT ); Thu, 7 Dec 2023 19:05:22 -0500 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CFE561716 for ; Thu, 7 Dec 2023 16:05:27 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-db542ec49d9so2002688276.0 for ; Thu, 07 Dec 2023 16:05:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701993927; x=1702598727; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=Xf5gnEGQIoXazSeiXhfMb12CybARggvMyxZM26gKG4Q=; b=kv6RQLIeqqQgCftrGdYe9u8RB45ZC9g4uHSOLndZz6pwpZtis87Dwxrlzebn+HLSa6 rLCM2SBKNEYVcdK2S5LyAOTmau7yeJMQDr10JtalD2nNnWyQlbDfbQT0k6GMyHwzeQXU fE6/BqMgPbRxR2JQyS5MNTaQQCQOg3Kq0aHRhIqtQLrsKtuhoXkuEscLbL64sd6FoLof Z5JK6IeBEZpNNYWqX89SFKqTpB//Gt1sGXfMh1QUzEbVQGATXLw7j/MKvKmD5Il5eDH2 sbih2vcRYFCYnyw8L+aKZjh2vN1w/3aP32P//vyASDXJqp7DXV0/6HYLfZ4oLUWwmfD4 OJ9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701993927; x=1702598727; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Xf5gnEGQIoXazSeiXhfMb12CybARggvMyxZM26gKG4Q=; b=JLKpWw1/QoEw64nSVR39Vbnc59c4ShzemG5uWcAZ4TIrBiQM1cCAQHRtxYfSHKnlED AvMRE72PEkex7GIkXPH/bQlSJHuOTNojqMTvnh9rcvcwUOZqp+H9x9P+16as+DRYXu3k Rpd9k+Mfx5yYEsx4Twupu0+brEaJU5SK4YjuHSB/79iCFGWfhmcnXQ2kbSdxvm6oW34X Ui+GPxYAfXlOzuymNLeiiwtFDzxQW2mmkIp5HelgNAtFFRGcK9KmyZwrZGMxCgFAxKz+ cvpSjLau1gks20k9QfmxPoV6N6/xEmj3K31fXcHg0mUW/f2j2CiMQbqJ0BFhZhOllu3+ s/Ow== X-Gm-Message-State: AOJu0YyTV63MYyqEcbBQ9smpYkjC40EUAuVYU0+ad18IR17VT6VgPr6P H0dKUSadnpELtUIm+QB5JZEs6xi9sVrY X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:aa27:6848:76cc:141c]) (user=irogers job=sendgmr) by 2002:a25:5509:0:b0:dbc:1b46:9aeb with SMTP id j9-20020a255509000000b00dbc1b469aebmr40703ybb.2.1701993926977; Thu, 07 Dec 2023 16:05:26 -0800 (PST) Date: Thu, 7 Dec 2023 16:05:14 -0800 In-Reply-To: <20231208000515.1693746-1-irogers@google.com> Message-Id: <20231208000515.1693746-2-irogers@google.com> Mime-Version: 1.0 References: <20231208000515.1693746-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Subject: [PATCH v1 2/3] perf: Suggest inbuilt commands for unknown command From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Chenyuan Mi , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Thu, 07 Dec 2023 16:05:35 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784670023390553116 X-GMAIL-MSGID: 1784670023390553116 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 --- 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; }