From patchwork Mon Nov 14 07:51:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 19570 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2022414wru; Mon, 14 Nov 2022 00:00:46 -0800 (PST) X-Google-Smtp-Source: AA0mqf71BD85dks15czGzOR7mTKYm2yRkJMBh9MP0gx5YwBdu7uaEzzM8vIYIpX9rxkxc5DBnjJr X-Received: by 2002:a17:902:ec8a:b0:186:deec:7ca2 with SMTP id x10-20020a170902ec8a00b00186deec7ca2mr12515537plg.121.1668412845981; Mon, 14 Nov 2022 00:00:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668412845; cv=none; d=google.com; s=arc-20160816; b=r1eNYTLsKXV60aK/TBzhYYEHB1d+77GA5zP8XC/W4SyQ3aNHz2NFfr0pI6VW6tN7Ux MPDq/6ikH7S/sloCpPLifCxw+I/Dw+QjA2VACZhtcPK1JhZdeG2sK4jTLrc9Eq/lvzZS Kk5Vw6+7MpK9u9W8pSAQ5NxRSQBxIyOrRhiT389VB7cR8NG49RZcGj/K0jGaNV1vwQSg cELktxCpsVdyYqAV8x2nzxSyoL25JUt7867cTqNWBtHFZ9O/fcjJFq3vO9cPQYsIRdcI kcYUvEdyGSd7N6cJCe+uO1eYJZ4ct15/2T6aH6uV6sYAszJyS0waAkw1Xh20CCCnPXAw PAjw== 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=FAjkIdGz/j9kJ1xDL5dYREJTueL8AT+RXQYxwRGY3ak=; b=sM9fJ623V1Yjs//dyBGptio8aiyFd0XpMcsjy4cNUNSP506KFufUwF6Q62xRS+x3ai s/fBxDTEEbNyzO1hbNIZvd3agtYG/LR3kA3XR0nN6oSCz6nsZ3CEnFA9ObwLWW/+sR95 z9ceh4jArPKed7XNP1kYRIeajCE3WGQBt/5ICnc5rYi1lqJvpt6phqrl7Uk/vJro8w+n 13+jKj26m9UNmv9pRd7/o3NTE4CpQ6rEjlZ66S6zK067/RUq6ouOUh8fCOv5Gwar85IH +H6GJLOIj3i3kyM0mbJYd8yopm1re9YSjfVjSi2zNX/o1ZDnpF+EHKYQoQLdIHgDo9Fe r6ow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b="CSUl/cD/"; 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 dc8-20020a056a0035c800b00562bb120d21si8442166pfb.165.2022.11.14.00.00.30; Mon, 14 Nov 2022 00:00:45 -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="CSUl/cD/"; 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 S236106AbiKNHw1 (ORCPT + 99 others); Mon, 14 Nov 2022 02:52:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45264 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236080AbiKNHwU (ORCPT ); Mon, 14 Nov 2022 02:52:20 -0500 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 714E519297 for ; Sun, 13 Nov 2022 23:52:16 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id h67-20020a252146000000b006ccc4702068so9855587ybh.12 for ; Sun, 13 Nov 2022 23:52:16 -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=FAjkIdGz/j9kJ1xDL5dYREJTueL8AT+RXQYxwRGY3ak=; b=CSUl/cD/WDzvyy7/gVUJxkLFomwzeS2nfA2va4W4NOvQegNB7iMnKzJhKFfJpEj0XS Yi3vAR5g5YbGcWE3BONv9feINe2WcX8c2NFXbZNEMPrx+45SI/gJp6X4t6vh//8PnT6n dE5Q6xxF1+8mEwQ370g22QepR6tznIJLx5XI8y2pLhkGx+t33L0CB+H8QXI0vu6DKsC0 DPzogliilSztWZGhb7C88kERACUOPi8sr9nM/q++dJ+iPOhx3b65FNgRjZto52bU8P9K F2J7g25KWkIcP+YMEiSZJGWvDqxXDMsYIAGhIgKNCzEWMfKqjgpg9sENyPY0L+7/nT+d sCmQ== 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=FAjkIdGz/j9kJ1xDL5dYREJTueL8AT+RXQYxwRGY3ak=; b=lIL+4fZg73nHoCzLEou3PFezb0pCoLZlOrCTqC7jgZ8LlOAOodn7/KeVTk2opTMpSM KYDRan7Jl61O6cRGtvaKvdM+7tuoGIk8GSpOLnd4q9cSIgDM+spxdZigk5CSLjdfKJrY o4G7w/jgBVy2ZS/IlndiRe2q8awdb+EZmP+KPzbNm1kpC6Wnq9XVRm8SExsqkfN67Po8 a0JwzDrcEAi9hvOj7+D3SB7gOUrIOHG+fl3D4JCBOoUxxlswPhn6WgGjcqNHfYaaDWji 6AA+nDRxIQ+2DD5c2m1yCN6OJ1NjSnMjQj7JyTJhiG/ppD+7QXnaqjdVPoDoGKEWWlwh oEeA== X-Gm-Message-State: ACrzQf3q993mzNO2dFKgkONHhwvLBXPKVQr+4TZ8dLgtebWCT937cEEU tG0XFLS2DASWHZ0Z+R29Mn1V0nevFBC1 X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:cba6:8279:98e6:3d6e]) (user=irogers job=sendgmr) by 2002:a0d:eac3:0:b0:36a:dfbf:3ee0 with SMTP id t186-20020a0deac3000000b0036adfbf3ee0mr64843865ywe.451.1668412334840; Sun, 13 Nov 2022 23:52:14 -0800 (PST) Date: Sun, 13 Nov 2022 23:51:21 -0800 In-Reply-To: <20221114075127.2650315-1-irogers@google.com> Message-Id: <20221114075127.2650315-4-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 3/9] perf tracepoint: Sort events in iterator 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?1749457668276085287?= X-GMAIL-MSGID: =?utf-8?q?1749457668276085287?= In print_tracepoint_events use tracing_events__scandir_alphasort and scandir alphasort so that the subsystem and events are sorted and don't need a secondary qsort. Locally this results in the following change: ... ext4:ext4_zero_range [Tracepoint event] - fib6:fib6_table_lookup [Tracepoint event] fib:fib_table_lookup [Tracepoint event] + fib6:fib6_table_lookup [Tracepoint event] filelock:break_lease_block [Tracepoint event] ... ie fib6 now is after fib and not before it. This is more consistent with how numbers are more generally sorted, such as: ... syscalls:sys_enter_renameat [Tracepoint event] syscalls:sys_enter_renameat2 [Tracepoint event] ... and so an improvement over the qsort approach. Signed-off-by: Ian Rogers --- tools/perf/util/print-events.c | 108 +++++++++++---------------------- 1 file changed, 37 insertions(+), 71 deletions(-) diff --git a/tools/perf/util/print-events.c b/tools/perf/util/print-events.c index c4d5d87fae2f..fefc025bc259 100644 --- a/tools/perf/util/print-events.c +++ b/tools/perf/util/print-events.c @@ -66,26 +66,21 @@ static int cmp_string(const void *a, const void *b) void print_tracepoint_events(const char *subsys_glob, const char *event_glob, bool name_only) { - DIR *sys_dir, *evt_dir; - struct dirent *sys_dirent, *evt_dirent; - char evt_path[MAXPATHLEN]; - char *dir_path; - char **evt_list = NULL; - unsigned int evt_i = 0, evt_num = 0; - bool evt_num_known = false; - -restart: - sys_dir = tracing_events__opendir(); - if (!sys_dir) - return; - - if (evt_num_known) { - evt_list = zalloc(sizeof(char *) * evt_num); - if (!evt_list) - goto out_close_sys_dir; - } + struct dirent **sys_namelist = NULL; + bool printed = false; + int sys_items = tracing_events__scandir_alphasort(&sys_namelist); + + for (int i = 0; i < sys_items; i++) { + struct dirent *sys_dirent = sys_namelist[i]; + struct dirent **evt_namelist = NULL; + char *dir_path; + int evt_items; + + if (sys_dirent->d_type != DT_DIR || + !strcmp(sys_dirent->d_name, ".") || + !strcmp(sys_dirent->d_name, "..")) + continue; - for_each_subsystem(sys_dir, sys_dirent) { if (subsys_glob != NULL && !strglobmatch(sys_dirent->d_name, subsys_glob)) continue; @@ -93,69 +88,40 @@ void print_tracepoint_events(const char *subsys_glob, dir_path = get_events_file(sys_dirent->d_name); if (!dir_path) continue; - evt_dir = opendir(dir_path); - if (!evt_dir) - goto next; - for_each_event(dir_path, evt_dir, evt_dirent) { - if (event_glob != NULL && - !strglobmatch(evt_dirent->d_name, event_glob)) + evt_items = scandir(dir_path, &evt_namelist, NULL, alphasort); + for (int j = 0; j < evt_items; j++) { + struct dirent *evt_dirent = evt_namelist[j]; + char evt_path[MAXPATHLEN]; + + if (evt_dirent->d_type != DT_DIR || + !strcmp(evt_dirent->d_name, ".") || + !strcmp(evt_dirent->d_name, "..")) continue; - if (!evt_num_known) { - evt_num++; + if (tp_event_has_id(dir_path, evt_dirent) != 0) + continue; + + if (event_glob != NULL && + !strglobmatch(evt_dirent->d_name, event_glob)) continue; - } snprintf(evt_path, MAXPATHLEN, "%s:%s", sys_dirent->d_name, evt_dirent->d_name); - - evt_list[evt_i] = strdup(evt_path); - if (evt_list[evt_i] == NULL) { - put_events_file(dir_path); - goto out_close_evt_dir; + if (name_only) + printf("%s ", evt_path); + else { + printf(" %-50s [%s]\n", evt_path, + event_type_descriptors[PERF_TYPE_TRACEPOINT]); } - evt_i++; + printed = true; } - closedir(evt_dir); -next: - put_events_file(dir_path); + free(dir_path); + free(evt_namelist); } - closedir(sys_dir); - - 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) { - if (name_only) { - printf("%s ", evt_list[evt_i++]); - continue; - } - printf(" %-50s [%s]\n", evt_list[evt_i++], - event_type_descriptors[PERF_TYPE_TRACEPOINT]); - } - if (evt_num && pager_in_use()) + free(sys_namelist); + if (printed && 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_close_evt_dir: - closedir(evt_dir); -out_close_sys_dir: - closedir(sys_dir); - - printf("FATAL: not enough memory to print %s\n", - event_type_descriptors[PERF_TYPE_TRACEPOINT]); - if (evt_list) - goto out_free; } void print_sdt_events(const char *subsys_glob, const char *event_glob,