From patchwork Mon Nov 14 07:51:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 19573 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2023058wru; Mon, 14 Nov 2022 00:02:04 -0800 (PST) X-Google-Smtp-Source: AA0mqf4kDzU/Zt6CSowOtExhZVkXOQU3Ftb255bktqbxSwf73F7dcXQqsvLittNtMD8oCp6Ozdfo X-Received: by 2002:a17:907:9144:b0:7a8:3597:34a8 with SMTP id l4-20020a170907914400b007a8359734a8mr9382630ejs.628.1668412924741; Mon, 14 Nov 2022 00:02:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668412924; cv=none; d=google.com; s=arc-20160816; b=LCwG0busXU7ZgsXAqOVOSWjNBmeKZeoeOuXVhIbZy693yp++65rPo2Tjc/CxBREExz ENPyt/wauxmjFFVYseBQqpIDqT7agRK88Bs5dbDpHpSeqJyhSz3hisX/xoUvwkwOhoqE D09rXDL/occNeE9Ejpp3lO1qkbVxxBT5C4Jba2TOoQUQFq2+blfaoxWz6nrdaXK8v3m5 ywvxKAZcP4YY3kmYE0kre7FW3Efy78v7Z9S6DqNix1O3YoDT7/DVoaHuoYeIo17B1DOf RNC5ORyLNFZDySqiYMvuC/9eu3Slu04RWCfcvLQq3ziUl+TcMQhGJ8OXSt32bRRpYnTY ON8g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=HIf5Y9gptGCDfzfHbnNjnA19mp6qRpBDngBttcXhem0=; b=syQrgxApplXE9NV3ebdcj59hQch8TshSgXpvvee7xDGMfAhaM5Qdvfjxrnrv85o51W UAMcE2CmHiKlDGKAqLqiIH/TzT1G5DM1dtO1pOlDp6/sKHC7umj9ROKv6B5lfRoCmyxA dJUy2pDv+IMpBespzRYzLHy5KBE/+Y3syUSnNKSC9Tcopl/q7lU+xoVJrXPrXwzzx5u5 312dTrV4JyFx9umDdhiPjX+2a3AuHpKUCf932ZmywvUsKtd/lxE77e4eJdaxfhThA6PC s5LKyDoQm2GIdQjOOu64YQM+3fwwj6WNuAYm1bjWsyabeKoyqWUaRcXgw6AHCZpmEK7h tyZw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=lZg8XR9r; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i6-20020aa7dd06000000b0045878eab247si8149597edv.193.2022.11.14.00.01.40; Mon, 14 Nov 2022 00:02:04 -0800 (PST) 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=20210112 header.b=lZg8XR9r; 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 S236079AbiKNHxJ (ORCPT + 99 others); Mon, 14 Nov 2022 02:53:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45570 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236099AbiKNHwt (ORCPT ); Mon, 14 Nov 2022 02:52:49 -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 3CA9719C28 for ; Sun, 13 Nov 2022 23:52:45 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id e8-20020a5b0cc8000000b006bca0fa3ab6so9790169ybr.0 for ; Sun, 13 Nov 2022 23:52:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=HIf5Y9gptGCDfzfHbnNjnA19mp6qRpBDngBttcXhem0=; b=lZg8XR9rikMPVUaFqH/pduhN9NiqpmRBLJZPpTe2SkaCZZo9Qs/Hp/RFLCAp+mFkx9 lGuvUrwFTHlGZVfm2+G3HqL9/POx1pN3vv90zCejS2/nyihHFSaGyNfTMFnDKGQScODi IXWwCxxlDC6sVqn5lXwCYbIoBKwiLJbZt4FgxUpXjfo4ez5sZG/uI919jUZBoYP6tlTh 1QJm1kAw8xF8wg4+9qQE29f1a6EemftrmcfUv8v0xwGRUsijsRo8XcCXX8IhYuqTsp+f rNKtmYdkvUc/zYmSkJenNFWeVbykk/cWU5DjSmGtkoH2ECbul3q+2Ojn5X4f8P3Bl7Fz kcow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc: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=HIf5Y9gptGCDfzfHbnNjnA19mp6qRpBDngBttcXhem0=; b=pyVEfNJBIGywp6U6b4dzErQ7DA263qU27gqpcAAlFlksO7Vb2Hy8bOUSk78k5TgcCk VP9l1XqSznPe7kotmYXT9LupgS8e+LytdaULaggWtakwgua1pQ3GYFzLqnLLrtflNlnw QqodXM7qs6FO1BwHMlaFDhV53/gT1ank1lyJmH63dPpLomY6iltQOFVK3MuYxlzaI1LA 4uyYP8hrNDEOoJjmu73tiHa06GFddHOf6RDFhbk3YocknNjIuSiklUZH6XTLfbNsEqEL 1FNy1lB/mTQNoz4oD4EWJ5AwSXAkr2hutyCDw6oxLEDCjIen0KnDXGf1IfwCtpjtOJDI 1jvg== X-Gm-Message-State: ANoB5pmrMlrAjVRUriDLCPEjX7fODCyR424fqqI3XnloPLJBMDCi7eia DhZd5G9vQw77L/06FwppBs2e+m15OJD+ X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:cba6:8279:98e6:3d6e]) (user=irogers job=sendgmr) by 2002:a25:320e:0:b0:6d1:a39d:637 with SMTP id y14-20020a25320e000000b006d1a39d0637mr11858110yby.335.1668412364529; Sun, 13 Nov 2022 23:52:44 -0800 (PST) Date: Sun, 13 Nov 2022 23:51:24 -0800 In-Reply-To: <20221114075127.2650315-1-irogers@google.com> Message-Id: <20221114075127.2650315-7-irogers@google.com> Mime-Version: 1.0 References: <20221114075127.2650315-1-irogers@google.com> X-Mailer: git-send-email 2.38.1.431.g37b22c650d-goog Subject: [PATCH v1 6/9] perf list: Simplify symbol event printing From: Ian Rogers To: Weilin Wang , Perry Taylor , Caleb Biggers , Leo Yan , Adrian Hunter , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Sandipan Das , Kajol Jain , Zhengjun Xing , Kan Liang , Ravi Bangoria , Xin Gao , Rob Herring , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Cc: Stephane Eranian , Ian Rogers X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749457751126862735?= X-GMAIL-MSGID: =?utf-8?q?1749457751126862735?= The current code computes an array of symbol names then sorts and prints them. Use a strlist to create a list of names that is sorted and then print it. Signed-off-by: Ian Rogers --- tools/perf/util/print-events.c | 79 +++++++++------------------------- 1 file changed, 21 insertions(+), 58 deletions(-) diff --git a/tools/perf/util/print-events.c b/tools/perf/util/print-events.c index ff7793944246..d53dba033597 100644 --- a/tools/perf/util/print-events.c +++ b/tools/perf/util/print-events.c @@ -52,14 +52,6 @@ static const struct event_symbol event_symbols_tool[PERF_TOOL_MAX] = { }, }; -static int cmp_string(const void *a, const void *b) -{ - const char * const *as = a; - const char * const *bs = b; - - return strcmp(*as, *bs); -} - /* * Print the events from /tracing/events */ @@ -298,77 +290,48 @@ void print_symbol_events(const char *event_glob, unsigned int type, struct event_symbol *syms, unsigned int max, bool name_only) { - unsigned int i, evt_i = 0, evt_num = 0; - char name[MAX_NAME_LEN]; - char **evt_list = NULL; - bool evt_num_known = false; - -restart: - if (evt_num_known) { - evt_list = zalloc(sizeof(char *) * evt_num); - if (!evt_list) - goto out_enomem; - syms -= max; - } + struct strlist *evt_name_list = strlist__new(NULL, NULL); + struct str_node *nd; - for (i = 0; i < max; i++, syms++) { + if (!evt_name_list) { + pr_debug("Failed to allocate new strlist for symbol events\n"); + return; + } + for (unsigned int i = 0; i < max; i++) { /* * New attr.config still not supported here, the latest * example was PERF_COUNT_SW_CGROUP_SWITCHES */ - if (syms->symbol == NULL) + if (syms[i].symbol == NULL) continue; - if (event_glob != NULL && !(strglobmatch(syms->symbol, event_glob) || - (syms->alias && strglobmatch(syms->alias, event_glob)))) + if (event_glob != NULL && !(strglobmatch(syms[i].symbol, event_glob) || + (syms[i].alias && strglobmatch(syms[i].alias, event_glob)))) continue; if (!is_event_supported(type, i)) continue; - if (!evt_num_known) { - evt_num++; - continue; - } - - if (!name_only && strlen(syms->alias)) - snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias); - else - strlcpy(name, syms->symbol, MAX_NAME_LEN); + if (strlen(syms[i].alias)) { + char name[MAX_NAME_LEN]; - evt_list[evt_i] = strdup(name); - if (evt_list[evt_i] == NULL) - goto out_enomem; - evt_i++; + snprintf(name, MAX_NAME_LEN, "%s OR %s", syms[i].symbol, syms[i].alias); + strlist__add(evt_name_list, name); + } else + strlist__add(evt_name_list, syms[i].symbol); } - if (!evt_num_known) { - evt_num_known = true; - goto restart; - } - qsort(evt_list, evt_num, sizeof(char *), cmp_string); - evt_i = 0; - while (evt_i < evt_num) { + strlist__for_each_entry(nd, evt_name_list) { if (name_only) { - printf("%s ", evt_list[evt_i++]); + printf("%s ", nd->s); continue; } - printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]); + printf(" %-50s [%s]\n", nd->s, event_type_descriptors[type]); } - if (evt_num && pager_in_use()) + if (!strlist__empty(evt_name_list) && pager_in_use()) printf("\n"); -out_free: - evt_num = evt_i; - for (evt_i = 0; evt_i < evt_num; evt_i++) - zfree(&evt_list[evt_i]); - zfree(&evt_list); - return; - -out_enomem: - printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]); - if (evt_list) - goto out_free; + strlist__delete(evt_name_list); } /*