From patchwork Thu Feb 1 00:14:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 195108 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:106:209c:c626 with SMTP id mn5csp122810dyc; Wed, 31 Jan 2024 16:47:15 -0800 (PST) X-Google-Smtp-Source: AGHT+IF/5Q6eYW9AnF8g4Pb6lCW6nPo8MLamU14QwUWEX+3et7Ra2z1o9ponYaJ6a+k/aKQLAthK X-Received: by 2002:a17:90a:e502:b0:292:679c:1ace with SMTP id t2-20020a17090ae50200b00292679c1acemr631578pjy.14.1706748435790; Wed, 31 Jan 2024 16:47:15 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706748435; cv=pass; d=google.com; s=arc-20160816; b=Z+JePvC7Pb5IaetWWmjukjsFiXPyOOdlCucsJnoc4Cq7EmFKbq2HUzdeYUTmRQAXwu 8nRjPq+PlQYRhefaj7tboLNUJz0qZqyMUjS1BnzcGKETeCXL7t3KwUU6XF2bboj+Q/Rg HlZfIsZpH5t2EsqmzQFRY031atOXVqCL/EKd7Eie6oXF6lJgHgD+MplhqTmUkXO0RdEc sagcrNBB89d2n2Bk4GkDMvwaVPjWBfyi1vbghmku9hFBJwkWETjH1rCFjm05Gq9JSGHZ wOesjyZGttf0NVAt88wW33ZK19Wj/Ja+g87joEKB9Oqh0zQxAJyyrCLH8W/SEizGpvhe bQ4A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=to:from:subject:references:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:in-reply-to:date :dkim-signature; bh=aRy9RhZw5CJlB7gMEzQb2ZjhFzzNCUTS7Rkwtu2S9w4=; fh=nEGJUAw+bIqIgJOFVZiOI9Pb8s8ReKqqemhB0+PAeQs=; b=MQ62Xpb73XO7nniBznH7auG08GpcAxlaMpJXVJLclKqEjU4CypNGkKhMKU248xt+PW nP1WoXajhGVw9cHtijQIo5pVjaA7fk5mqYb5n6zXrA4xnZy6MHMgJrosF48vDuadCDVk /Tupi+Cd41S4nKFJRzOrncNNoUNNN6y8q115LVX5Kjz0FARMb9/mEtqrDMjBlSVmvLqT AaSNo/2WkR7KoFYY2swtb1lRWxOWBrK2EGygoo5eAD2LlcOyVdK3hrDREQhiXbSEKygZ oJy13sRe1lakEXyeVeFZDyRyo4ln4B0XUNmwUNTFRliAfgu3aAs8wfJ4BmZFCNQOD/m6 A/Fw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=L1rZ6veU; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-47414-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-47414-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=1; AJvYcCXCZwDaU2lNfIDYVkC2m2CsmTfSb8bZVi9/H2Km+NATAcT49A9IwVsHddJo2oep5Dw3/DuG+/PO+NLa/Nn03At1u++5QA== Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id o14-20020a17090ac70e00b00295b3ecc46bsi2385807pjt.168.2024.01.31.16.47.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jan 2024 16:47:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-47414-ouuuleilei=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=@google.com header.s=20230601 header.b=L1rZ6veU; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-47414-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-47414-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com 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 6104DB21D16 for ; Thu, 1 Feb 2024 00:16:02 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6C16A3D7B; Thu, 1 Feb 2024 00:15:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="L1rZ6veU" Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8560A626 for ; Thu, 1 Feb 2024 00:15:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706746526; cv=none; b=rHtCGjdUb5N9VPbmoE16LRs7v1iam8MMWDBDXvx42THvsfVftXMu5awngm80itjMVTxkmdwVXbt7ey69rVKIVyN1CHelAXzSNrHRp0GgYYZt6NtSboiPY9nh1VPFrg2SNsCtwA5N45tr0ewyKYiPSLpGERlcHp8K99tPMWMzzKs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706746526; c=relaxed/simple; bh=6ZpLBuInMEOZtrqrEBa7yzNG4a6Xjz6IF3cCBjCdIOc=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=YvVhHtd7001IUPNkye4kAWE5nYBNDgfHz2ZcNEZ0PTodC6eWYkRf2Fbta6SEuR05ILQNr0O1NqT6namwCICi3De3dx+Rcw7/oy9ydzIe5J7bbKn7i9Lz2Pn1SXsMR+50w8wcm7onyaKoqv8uLeBb6v42enpmTNexAdHN6M5jBHo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=L1rZ6veU; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dc6b267bf11so448336276.2 for ; Wed, 31 Jan 2024 16:15:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706746523; x=1707351323; 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=aRy9RhZw5CJlB7gMEzQb2ZjhFzzNCUTS7Rkwtu2S9w4=; b=L1rZ6veUvnXWJRI2FWcx+SAfWPKkDVDt9JSN/YO/brQ393xU7S3nDLFeWhSoaTWeXx 4C9jVLHXfwRFRcx/A4L8MIDTdN5Kl0OAP4aHXiJgiJhwEh47RRx56Myo/BFPR+jS92eD tUcm7QveSzHh96ijk1Rch+pLO0kmMDxueBRKs6LAgI/+HWWJowu7frLA/l6tplSiDOeC B0SfNAdprbTnmGMNVeGeT7Vy4zLgPBuIpqlmkKvzQUftNvI3nyWKs9bwwfigfk/LAAN6 0+dpiVB6Nk0anTuLrm/nqp/FvgqPgrVh9SG1WVgos/mVtc50sKQhRdzelhrLCLUGYYYa TdHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706746523; x=1707351323; 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=aRy9RhZw5CJlB7gMEzQb2ZjhFzzNCUTS7Rkwtu2S9w4=; b=Q1cm0fVhStohZYmX8CHtZNo5tCXy1c5++bCGvA3VZ2z6isXFT3g5iR0L5pOlkGPJEh WHrKdcf29HMTW4MyGFKjjZ8HYm1X7dWDw/mvViGleH4RC1EKMBWD8ryLG1A4laNI6TZL tN5KSGKr6oqvMm0TXpF3ezcnLAmq5V5omQvXhCu0wk3v/7MVKrEF4XzGgl7qF3flLLom R8Tb4UxSrqn6mbZun0djwQfSZno+d5RGrz0/nMU0F4cUSSCGUNHDDq4akro2gIoiwD9K l0GjVnhzHXfBdVBojhB55yYybRHq5rD9teTBcghqpY/ejVbeOJkf9w2mLfUh44Dm5aKV 1IxA== X-Gm-Message-State: AOJu0Yz1m0NnkAIJ27K7gcBLbIUUFuOT0j9aymfbVRalyqJFEl2JJbXq CBpvBhuGetKtg2GxgMmXaBenViwFqJrskvqcx0X7810My2Yb9Yyol9W+MDS1flA6gGjbAE9h7mc dprqUOg== X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:16c5:1feb:bf99:a5d1]) (user=irogers job=sendgmr) by 2002:a05:6902:2702:b0:dc6:c623:ce6f with SMTP id dz2-20020a056902270200b00dc6c623ce6fmr27336ybb.13.1706746523485; Wed, 31 Jan 2024 16:15:23 -0800 (PST) Date: Wed, 31 Jan 2024 16:14:56 -0800 In-Reply-To: <20240201001504.1348511-1-irogers@google.com> Message-Id: <20240201001504.1348511-2-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240201001504.1348511-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.429.g432eaa2c6b-goog Subject: [PATCH v2 2/9] perf list: Add scandirat compatibility function From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nathan Chancellor , Nick Desaulniers , Tom Rix , Ravi Bangoria , James Clark , Kan Liang , John Garry , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, llvm@lists.linux.dev X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789655447547007416 X-GMAIL-MSGID: 1789655447547007416 scandirat is used during the printing of tracepoint events but may be missing from certain libcs. Add a compatibility implementation that uses the symlink of an fd in /proc as a path for the reliably present scandir. Signed-off-by: Ian Rogers --- tools/perf/util/print-events.c | 12 +++--------- tools/perf/util/util.c | 19 +++++++++++++++++++ tools/perf/util/util.h | 8 ++++++++ 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/tools/perf/util/print-events.c b/tools/perf/util/print-events.c index b0fc48be623f..15ec55b07bfd 100644 --- a/tools/perf/util/print-events.c +++ b/tools/perf/util/print-events.c @@ -63,6 +63,8 @@ void print_tracepoint_events(const struct print_callbacks *print_cb __maybe_unus { char *events_path = get_tracing_file("events"); int events_fd = open(events_path, O_PATH); + struct dirent **sys_namelist = NULL; + int sys_items; put_tracing_file(events_path); if (events_fd < 0) { @@ -70,10 +72,7 @@ void print_tracepoint_events(const struct print_callbacks *print_cb __maybe_unus return; } -#ifdef HAVE_SCANDIRAT_SUPPORT -{ - struct dirent **sys_namelist = NULL; - int sys_items = tracing_events__scandir_alphasort(&sys_namelist); + sys_items = tracing_events__scandir_alphasort(&sys_namelist); for (int i = 0; i < sys_items; i++) { struct dirent *sys_dirent = sys_namelist[i]; @@ -130,11 +129,6 @@ void print_tracepoint_events(const struct print_callbacks *print_cb __maybe_unus } free(sys_namelist); -} -#else - printf("\nWARNING: Your libc doesn't have the scandirat function, please ask its maintainers to implement it.\n" - " As a rough fallback, please do 'ls %s' to see the available tracepoint events.\n", events_path); -#endif close(events_fd); } diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c index c1fd9ba6d697..4f561e5e4162 100644 --- a/tools/perf/util/util.c +++ b/tools/perf/util/util.c @@ -552,3 +552,22 @@ int sched_getcpu(void) return -1; } #endif + +#ifndef HAVE_SCANDIRAT_SUPPORT +int scandirat(int dirfd, const char *dirp, + struct dirent ***namelist, + int (*filter)(const struct dirent *), + int (*compar)(const struct dirent **, const struct dirent **)) +{ + char path[PATH_MAX]; + int err, fd = openat(dirfd, dirp, O_PATH); + + if (fd < 0) + return fd; + + snprintf(path, sizeof(path), "/proc/%d/fd/%d", getpid(), fd); + err = scandir(path, namelist, filter, compar); + close(fd); + return err; +} +#endif diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h index 7c8915d92dca..9966c21aaf04 100644 --- a/tools/perf/util/util.h +++ b/tools/perf/util/util.h @@ -6,6 +6,7 @@ /* glibc 2.20 deprecates _BSD_SOURCE in favour of _DEFAULT_SOURCE */ #define _DEFAULT_SOURCE 1 +#include #include #include #include @@ -56,6 +57,13 @@ int perf_tip(char **strp, const char *dirpath); int sched_getcpu(void); #endif +#ifndef HAVE_SCANDIRAT_SUPPORT +int scandirat(int dirfd, const char *dirp, + struct dirent ***namelist, + int (*filter)(const struct dirent *), + int (*compar)(const struct dirent **, const struct dirent **)); +#endif + extern bool perf_singlethreaded; void perf_set_singlethreaded(void);