Received: by 2002:ab2:6991:0:b0:1f7:f6c3:9cb1 with SMTP id v17csp1246956lqo; Thu, 9 May 2024 08:33:02 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWDD6MlFCJkGcGkSvm6CRm0YvMOsKJNYQtnGNDDTDollur32/bgl/yiyrM8c+4BZGMtXNCJNKy7PPMuD4nvvq8rNkPOcKRcSQtlwdmUCg== X-Google-Smtp-Source: AGHT+IFPw5ZHo/qTskEkMh6JBkw5CdW3PBbnmsUzJF7QKBl0yQbPs2+vnJv1A17oHEzHf98ab9hd X-Received: by 2002:a05:6512:348a:b0:51f:2f5a:54ae with SMTP id 2adb3069b0e04-5217c374388mr4800232e87.7.1715268781839; Thu, 09 May 2024 08:33:01 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715268781; cv=pass; d=google.com; s=arc-20160816; b=CYgKdjPQkT4xFOgxUZqUMp9KeUzC4q8+wtIKuzsFqQkJ1bjqNOV/EG+yf/na8lxFqg 1FgirQ9aVBD605OpH4vlJMVxT/rKQN96Nhn3fH7maJLSXA2PT7yNwEVzRceu6WMwhXRB wN3gwe9dJJv4vREDiHVDTLj3W366CAYbS8JZmX6j9kccIvHWnrTXfnKkttr1/k3lVJY+ XaqgwpxKNWDZLRwNiFSlLaWwMiiSWVmpQtfhUcsMx0OeMBp3C2VZxHxB4n0e6k9aRj68 1mkaWlF0ZpuNr0hH5KPTivy77nc29pCP1Z/tpD45mRFMgZ2aNh6ptOTpwSc/ys7RPdY2 KMWg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=to:from:subject:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:message-id:date:dkim-signature; bh=+/cYEMUxODwz/noTI6gGdDYW4oBmyIjB0W+r47BpRI4=; fh=DaA6aTD+Z4u4+UmSS57SWQAxNgpHxuH9cjh8IUMcVHM=; b=yRtvas3WxFigvjQvXSioyYI4Cdt3sMODlR2hhvXqNuLHyarIMOkYrD5pN/pXbuG53R 2dKklQDSu5y4gmx0g01lknrc3rSChVvXDxqkTZdxiGZkJX4rHoc2QoiU7CytzAdxWbSI INTHjkRw6t4egQ6ZP02XP6/0ME7JjUaCC4MbkaHpxlTRZozXC1mj/QBsCY6ny8/J2MzX G67TmTd4OWEN+sTfB1fjRqfl0qWKSnwP1hf1uJJGYhMj6LoehCVQQ4KI9PYj6MjerTPB XCYP31RGh8XyA6PHq2Ro/WuKZU+xT/z1sfPPAnURxwQjOgtb/Nuw84KiBZWcuDTLkeTR ne4w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=NnFiAWv8; 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-174639-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-174639-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id a640c23a62f3a-a5a17c2b6f9si93597566b.910.2024.05.09.08.33.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 May 2024 08:33:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-174639-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=NnFiAWv8; 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-174639-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-174639-linux.lists.archive=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 am.mirrors.kernel.org (Postfix) with ESMTPS id 5FB7A1F22057 for ; Thu, 9 May 2024 15:33:01 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EF9F616F273; Thu, 9 May 2024 15:32:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="NnFiAWv8" 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 7D9C0383BD for ; Thu, 9 May 2024 15:32:50 +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=1715268772; cv=none; b=nV7LkH5LnVSGCheXQOcHxJocK3gAFqJzRMLy4LBOZ6cxj7gjKciX9kGDTBDGiLGQaNgltwArGswmjqUcCwhtbszSwS61hVWVZhJmBPvrBUmsZg2n6Qg3iYj99Gz0sHBlCf3s/6bAe3Btl+E6pIce/J7Gx2hfkT1Zm/TzKl5D/44= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715268772; c=relaxed/simple; bh=o7MEDhlQNTPIH9LLO9fztAGkTXiQChzvwN+jqtL5L9A=; h=Date:Message-Id:Mime-Version:Subject:From:To:Content-Type; b=BL90Ukp5LiayrcSxWExEjRXXiGuyO98tV9mwO2oo0C6Y2cRErHluCsc4xBW+zuaC0h6/3ETjOyUgOYX/gs6I/QdvWzHohDxlN7dtxk8R6haj4ikZRWvim4n+0Xu+Dau+aaC6SUq5zUHh3rUcdcufo1hiI9Y61KJ8pA+zaxmb4e0= 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=NnFiAWv8; 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-de54be7066bso1545796276.0 for ; Thu, 09 May 2024 08:32:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1715268769; x=1715873569; darn=vger.kernel.org; h=to:from:subject:mime-version:message-id:date:from:to:cc:subject :date:message-id:reply-to; bh=+/cYEMUxODwz/noTI6gGdDYW4oBmyIjB0W+r47BpRI4=; b=NnFiAWv8efV4fDFGVr84MAm5x86ZXekNSlanwIH0oqSqMjoAoSMPMrI9HDt8kZlX+T SwcKC/cQ+O2Wmciwtfk8oRryD9tUDmp8ydPEkoQFGndHSArtf7Lh7XxrE7g4pCny/UNJ tKP/qto8Wllx7IOvv/X067ZWCU2z+qnuXbucz2iH3Pyo8puO0K25GCVM1S5m0u2MKED4 3427Mo9x0tgK7h3d3Pt8PmkRNBd9HbF7R1v1b2f9v50Ba66+Kixds73VqYGI6hXPzQp1 JGcQYYsbgpvLsNhvbNOx+y2Cf2YbJKbfUUQClurkiFWdI8su9J0ty4UWg7+O/Q+CIfux 238Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715268769; x=1715873569; h=to:from:subject:mime-version:message-id:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=+/cYEMUxODwz/noTI6gGdDYW4oBmyIjB0W+r47BpRI4=; b=lupAyIaLMSwkhLsAyzsyRtToproCPj27t9HVQgzEVwd5zlqksNhuhZjRqXxj30JObF P0KPnK6K8j+CcE3Eise4ONiMwqX8Bf2lWNVpQ45Fde4Eg3SkjHkW/6DO/+bLn9aPyGo/ gTWRTziSL2oGWjFhDlqd0Nrdtfc03b7CoNn5sQUtdwr2xnhXxcuB+tIyo1lGPuHujI+2 8Be26Sm/031m8XIol4+89nwVA7IaMHM8s6DQsZeX5EQB3boF6YBRhtg3A/pxCV7sr1tI 5XUo8VkhKF7yhN37D54xwO6BQU2gm+Iltt+XNrsFuOMEFEje333Sc3AgjeqcPYViu8Ga Kr/w== X-Forwarded-Encrypted: i=1; AJvYcCU52jBpzeOMqVW9EqzzVaBVu4N4yT4jfrwYGymuKIsd1Y7VNzF4Cvue+3w/Al2LCyEtNWG53jCJ5p8a/ciMQWr2CiydPn7qGL2/o91m X-Gm-Message-State: AOJu0YwjMAZt5rE6xWoVabVipc0HJDiivEo9r9g8/5xC9BK4JXncddUj MI2BUPxvDbcfGTf9B3d9A2o7QzHJqsdwlXt0jfPXOgWw4SJzntlK8ez+rF/DKdJ3UdV7XkrTEeu F63DCBQ== X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:3887:270c:55a5:15aa]) (user=irogers job=sendgmr) by 2002:a05:6902:c05:b0:de5:2325:72ac with SMTP id 3f1490d57ef6-debb9d0ff72mr1731605276.6.1715268769503; Thu, 09 May 2024 08:32:49 -0700 (PDT) Date: Thu, 9 May 2024 08:32:45 -0700 Message-Id: <20240509153245.1990426-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.45.0.rc1.225.g2a3ae87e7f-goog Subject: [PATCH v2] perf tracepoint: Don't scan all tracepoints to test if one exists From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" In is_valid_tracepoint, rather than scanning "/sys/kernel/tracing/events/*/*" skipping any path where "/sys/kernel/tracing/events/*/*/id" doesn't exist, and then testing if "*:*" matches the tracepoint name, just use the given tracepoint name replace the ':' with '/' and see if the id file exists. This turns a nested directory search into a single file available test. Rather than return 1 for valid and 0 for invalid, return true and false. Signed-off-by: Ian Rogers --- v2. Fix potential uninitialized use on memory allocation failure path. --- tools/perf/util/tracepoint.c | 56 ++++++++++++++---------------------- tools/perf/util/tracepoint.h | 3 +- 2 files changed, 24 insertions(+), 35 deletions(-) diff --git a/tools/perf/util/tracepoint.c b/tools/perf/util/tracepoint.c index 92dd8b455b90..bb06d10fbe37 100644 --- a/tools/perf/util/tracepoint.c +++ b/tools/perf/util/tracepoint.c @@ -4,10 +4,12 @@ #include #include #include +#include #include #include #include +#include "fncache.h" int tp_event_has_id(const char *dir_path, struct dirent *evt_dir) { @@ -26,39 +28,25 @@ int tp_event_has_id(const char *dir_path, struct dirent *evt_dir) /* * Check whether event is in /tracing/events */ -int is_valid_tracepoint(const char *event_string) +bool is_valid_tracepoint(const char *event_string) { - DIR *sys_dir, *evt_dir; - struct dirent *sys_dirent, *evt_dirent; - char evt_path[MAXPATHLEN]; - char *dir_path; - - sys_dir = tracing_events__opendir(); - if (!sys_dir) - return 0; - - for_each_subsystem(sys_dir, sys_dirent) { - 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) { - snprintf(evt_path, MAXPATHLEN, "%s:%s", - sys_dirent->d_name, evt_dirent->d_name); - if (!strcmp(evt_path, event_string)) { - closedir(evt_dir); - put_events_file(dir_path); - closedir(sys_dir); - return 1; - } - } - closedir(evt_dir); -next: - put_events_file(dir_path); - } - closedir(sys_dir); - return 0; + char *dst, *path = malloc(strlen(event_string) + 4); /* Space for "/id\0". */ + const char *src; + bool have_file = false; /* Conservatively return false if memory allocation failed. */ + + if (!path) + return false; + + /* Copy event_string replacing the ':' with '/'. */ + for (src = event_string, dst = path; *src; src++, dst++) + *dst = (*src == ':') ? '/' : *src; + /* Add "/id\0". */ + memcpy(dst, "/id", 4); + + dst = get_events_file(path); + if (dst) + have_file = file_available(dst); + free(dst); + free(path); + return have_file; } diff --git a/tools/perf/util/tracepoint.h b/tools/perf/util/tracepoint.h index c4a110fe87d7..65ccb01fc312 100644 --- a/tools/perf/util/tracepoint.h +++ b/tools/perf/util/tracepoint.h @@ -4,6 +4,7 @@ #include #include +#include int tp_event_has_id(const char *dir_path, struct dirent *evt_dir); @@ -20,6 +21,6 @@ int tp_event_has_id(const char *dir_path, struct dirent *evt_dir); (strcmp(sys_dirent->d_name, ".")) && \ (strcmp(sys_dirent->d_name, ".."))) -int is_valid_tracepoint(const char *event_string); +bool is_valid_tracepoint(const char *event_string); #endif /* __PERF_TRACEPOINT_H */ -- 2.45.0.rc1.225.g2a3ae87e7f-goog