From patchwork Fri Mar 31 20:29:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 77968 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp825879vqo; Fri, 31 Mar 2023 13:40:31 -0700 (PDT) X-Google-Smtp-Source: AKy350bC/pSdHnPlowKSjZlIPoo+N1vOzwA0+X+ndv/9BebcvwrUcXF+Lf6mbYTVypyI3O2dG07K X-Received: by 2002:a17:906:1685:b0:930:660d:8f92 with SMTP id s5-20020a170906168500b00930660d8f92mr29523753ejd.52.1680295231297; Fri, 31 Mar 2023 13:40:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680295231; cv=none; d=google.com; s=arc-20160816; b=F6iTkwsbsWQuYASWU+bIiLdwYCDmJxNyQNTuVXIfIzm7kWv88MXhOEe1aT0qkeQ4Hz 91FiNTBku/uHF1alONhzfoYOuV9G5HsI1n1cBw23ofY5jJJe7PZk74yKYWGnJAjCEzTq tK4XdvFlMisgLvwykTLZHvm15oro/BSHClvMiH0ZPz7b27d3TiJV6CUYZxlF0J5hCPEt ZSDs9psEeJ3wvwfdkyV6sccU3on+RSSSABskMKabenRs4Gad7sysvgO3zApE4a61P1oF pDedp66sR6OSC8GD51dMghrcrZTdQNde89Flsqt2hUWALvrpfdGZAvjEDyfycXu5QMxr hXIA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from:sender :dkim-signature; bh=PLhh5lXY8mRzGTGozorwCPHUrnN37AS+DlWp1fZ57Ms=; b=NYJ4HsoKxuYK/DPhNwMYqvsPDQzOUp8rfJpTjSfcsw3oq4UAaxRRbRwmIHpJH/DG9X fsi9y5WqZf1IJuUlFjpaDaZHzl+QAB/WnknG6Bno7RZWr+JTMi9p2e8D72MBeagyIYYP t6rNj7EvDGxcXRHsVQTsFJW2LO4yi5p0sMbtpsIRVdQcqOsZUqZs1bnJXaSHgjBCDVbO 7b9j7Aby3WiGl80ZJLhRY0T1g5xiAAewGfbP7Ykk4XUN/RruYldz+yB6qMhytKZYU0Xk ThslkLc/GJIia5sLQ+Bh3fQJT01fD6fzx17P8/08AHzAxSe9JCSeX+GsDRHD6J463NnT MaBQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=PvobSiVq; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ci7-20020a170906c34700b0093dfe066be0si350948ejb.530.2023.03.31.13.39.58; Fri, 31 Mar 2023 13:40:31 -0700 (PDT) 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=@gmail.com header.s=20210112 header.b=PvobSiVq; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233170AbjCaU37 (ORCPT + 99 others); Fri, 31 Mar 2023 16:29:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44436 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230193AbjCaU3y (ORCPT ); Fri, 31 Mar 2023 16:29:54 -0400 Received: from mail-pl1-x631.google.com (mail-pl1-x631.google.com [IPv6:2607:f8b0:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B04151A96F; Fri, 31 Mar 2023 13:29:53 -0700 (PDT) Received: by mail-pl1-x631.google.com with SMTP id z19so22399376plo.2; Fri, 31 Mar 2023 13:29:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680294593; x=1682886593; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=PLhh5lXY8mRzGTGozorwCPHUrnN37AS+DlWp1fZ57Ms=; b=PvobSiVqjZzQJUcSIk/NI+SPYWuV2Nz3Hlt7g2jd0iCPqdnUnbU4PzRkNpdH8c+u+Y 1cF6Dx8r6hPGf3zgke+nPrA38Mdn94Fd/UIUv4HagZUVxHlYCm9tpty+nCLy234NXqOB Ig4bFULimXNrEzcgbLkHvZw1u0qZUEjVcFVcL2MOX0aYd8mqv/8gX9+8Fv1VGS3itjwn c7KuhQuUAo6wVAYBnby5flX3cJwP7Cj9u/oX8s8ifF2r5FSFcnBHTXb+37aRDxA92QUd nOCPk7W1PiEEetdSDpEnQWM1MABcTe3PPeY/jDoonJEWRThpWnvSqHDsfURWjzogKkCG c2SQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680294593; x=1682886593; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=PLhh5lXY8mRzGTGozorwCPHUrnN37AS+DlWp1fZ57Ms=; b=RPOFpK+UKSl6sHW0u2BF6jcrc9RSongPPkH/g77e7r6CLKnZe/59dtfktRe/5z1a3C nPOb57BqZtl0D6RW29wZD7zoqi5TuCv0jiN73ppjv1+B4qFdK+VfrAlStB6dAfB3Kwhh P6EeVzmve3aXj3maK21HOwAmow09IdEG1yhcb4dS8P5ey1HME7f3r9NsfmM6cRvojry1 P0+qIa3GNEmH2Ku77di5dWg0YsiXOVaNvbc1xis9IwKdWEHcZP/07uWCN1dfSjoQ0ov5 LbYZEzlr/atA000fJJRdzeQC2+RAQeYBHkRhnodkVQohVuNXnS7BydVsR/67FKKSP76c pcXw== X-Gm-Message-State: AAQBX9dcpHmGN0XMj+egaKuMz7tFugjQtteA83q6hXUcWPw+Z1Mfwldb YU9hKFJrbKo6P/p/TPlkGJc= X-Received: by 2002:a17:903:2290:b0:1a1:a727:a802 with SMTP id b16-20020a170903229000b001a1a727a802mr11880427plh.19.1680294593101; Fri, 31 Mar 2023 13:29:53 -0700 (PDT) Received: from bangji.hsd1.ca.comcast.net ([2601:647:6780:44b0:5cfb:ad2c:e6f5:d42f]) by smtp.gmail.com with ESMTPSA id t14-20020a1709028c8e00b001a1dc2be791sm1916803plo.259.2023.03.31.13.29.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 Mar 2023 13:29:52 -0700 (PDT) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Ian Rogers , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Kan Liang , Leo Yan Subject: [PATCH 1/9] perf list: Use relative path for tracepoint scan Date: Fri, 31 Mar 2023 13:29:41 -0700 Message-Id: <20230331202949.810326-2-namhyung@kernel.org> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog In-Reply-To: <20230331202949.810326-1-namhyung@kernel.org> References: <20230331202949.810326-1-namhyung@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=0.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE, SPF_PASS autolearn=no 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?1761917252319718589?= X-GMAIL-MSGID: =?utf-8?q?1761917252319718589?= Signed-off-by: Namhyung Kim Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/util/print-events.c | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/tools/perf/util/print-events.c b/tools/perf/util/print-events.c index 62e9ea7dcf40..26a7e017c928 100644 --- a/tools/perf/util/print-events.c +++ b/tools/perf/util/print-events.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -59,12 +60,20 @@ static const struct event_symbol event_symbols_tool[PERF_TOOL_MAX] = { void print_tracepoint_events(const struct print_callbacks *print_cb, void *print_state) { struct dirent **sys_namelist = NULL; + char *events_path = get_tracing_file("events"); int sys_items = tracing_events__scandir_alphasort(&sys_namelist); + int events_fd = open(events_path, O_PATH); + + put_tracing_file(events_path); + if (events_fd < 0) { + printf("Error: failed to open tracing events directory\n"); + return; + } for (int i = 0; i < sys_items; i++) { struct dirent *sys_dirent = sys_namelist[i]; struct dirent **evt_namelist = NULL; - char *dir_path; + int dir_fd; int evt_items; if (sys_dirent->d_type != DT_DIR || @@ -72,22 +81,26 @@ void print_tracepoint_events(const struct print_callbacks *print_cb, void *print !strcmp(sys_dirent->d_name, "..")) continue; - dir_path = get_events_file(sys_dirent->d_name); - if (!dir_path) + dir_fd = openat(events_fd, sys_dirent->d_name, O_PATH); + if (dir_fd < 0) continue; - evt_items = scandir(dir_path, &evt_namelist, NULL, alphasort); + evt_items = scandirat(events_fd, sys_dirent->d_name, &evt_namelist, NULL, alphasort); for (int j = 0; j < evt_items; j++) { struct dirent *evt_dirent = evt_namelist[j]; char evt_path[MAXPATHLEN]; + int evt_fd; if (evt_dirent->d_type != DT_DIR || !strcmp(evt_dirent->d_name, ".") || !strcmp(evt_dirent->d_name, "..")) continue; - if (tp_event_has_id(dir_path, evt_dirent) != 0) + snprintf(evt_path, sizeof(evt_path), "%s/id", evt_dirent->d_name); + evt_fd = openat(dir_fd, evt_path, O_RDONLY); + if (evt_fd < 0) continue; + close(evt_fd); snprintf(evt_path, MAXPATHLEN, "%s:%s", sys_dirent->d_name, evt_dirent->d_name); @@ -103,10 +116,11 @@ void print_tracepoint_events(const struct print_callbacks *print_cb, void *print /*long_desc=*/NULL, /*encoding_desc=*/NULL); } - free(dir_path); + close(dir_fd); free(evt_namelist); } free(sys_namelist); + close(events_fd); } void print_sdt_events(const struct print_callbacks *print_cb, void *print_state) From patchwork Fri Mar 31 20:29:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 77972 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp827888vqo; Fri, 31 Mar 2023 13:44:41 -0700 (PDT) X-Google-Smtp-Source: AKy350YSaRkhU97V2jB3zF35vIERPvRxJ39c9apzCRSMeVEvlfcl5gjzL2T8qOknBxyfN5/mFtq4 X-Received: by 2002:a17:902:e394:b0:1a2:7d:8a74 with SMTP id g20-20020a170902e39400b001a2007d8a74mr22009567ple.66.1680295480749; Fri, 31 Mar 2023 13:44:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680295480; cv=none; d=google.com; s=arc-20160816; b=vgKYvg9alnx/h/vVb7vHxo0w6+WY6yrNBUbDljdF8EmphnXvnOjnFtxEJzrauF5o25 KnI3iSkksOileYiN8c7Gv4N9o5/oEF4YvhctDx00MhEf1MZvYsqI6dJEpol19Wl9Gq3N 7EWIM9Tp7CnNHT4uAEQtxnHtjN4/PmbLTWisO31NjPsSV7VNtbpCaPk0cGR2Awz+zNSy TfTvld4oVuScrfkpuQFLGHOiHkW+POsrlLbStQ4nkbKyoD541tJKKWA0YvdNKDL9Ya7B dB3stxaCx4trASz8MnoqgARHBMZXI+YM7OWBH7RZ4GEjznWQInCuPAgGe83+HE/XIAcq CiJQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from:sender :dkim-signature; bh=ON3ePqj/wXwvqLAB1yIimizzqbLUeCNd0TWXqbkcGVM=; b=r7p8eM3afDIrI3VX6J6rB0nYjo3uEQ7YJGSkTurcoY1U/zHoLiDuA5MYW/evUbBGvs ojeHBCIiVZ3ss2tWhhIydANXZ7sn6FfkBnnrAf7sI7HgirBcw/EL1OWEnvu1Z8tCFtlG hohNnHV+oai0ej/rP7dW4IU/PlNVbFQexxm9UpXi0HNGqMAg+P5Mh2jbVEM6Yy8LQpy0 VC7/fgFktfhy6bof7kpVDZ6W2sfpGWTb7uAFLIh2Y/bzDX9GO2zFX27eVYiDM6NPeQjq /Ci/Pr/+CBxLlRxyD11CIuorLxbUyx12WsJ4inJTn/LuHfjBRHmucA9dPww73YHPo6MF Cp9A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="Y0/w0BpU"; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id li12-20020a170903294c00b0019b0b007994si2864873plb.163.2023.03.31.13.44.28; Fri, 31 Mar 2023 13:44:40 -0700 (PDT) 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=@gmail.com header.s=20210112 header.b="Y0/w0BpU"; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233168AbjCaUaD (ORCPT + 99 others); Fri, 31 Mar 2023 16:30:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44478 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229967AbjCaU34 (ORCPT ); Fri, 31 Mar 2023 16:29:56 -0400 Received: from mail-pj1-x102d.google.com (mail-pj1-x102d.google.com [IPv6:2607:f8b0:4864:20::102d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3D282B76E; Fri, 31 Mar 2023 13:29:55 -0700 (PDT) Received: by mail-pj1-x102d.google.com with SMTP id j13so21688465pjd.1; Fri, 31 Mar 2023 13:29:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680294595; x=1682886595; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=ON3ePqj/wXwvqLAB1yIimizzqbLUeCNd0TWXqbkcGVM=; b=Y0/w0BpUD/RjYQDJHQTZYdcHHj90Xry7pIWujDCnBcNgrI6ewhUvX+Za73JbTxs7vO XmqeUrQNZ6PSpjLbaQYTpQz3gC07EVRf+w2leb80++HMdS+OClW9auiVQ8alG7xRTTi8 tZ2gHwZ/XYcPdkKi2RPCa6RndYCZM6pknN+nu+q7TqtL17FJK/H5X4UIqzH6aj7ZZ53n yFHKsLXvWTYot/+pEncxNU8BkyX+wP802Vbk6wx6R+zco+dCywhomwW3xAURnHqzV2Ic rrIt7hs5P/5ce4SMST4pn7d6DbYGB2vj/9lS7cCTmYWluWtPFBw38qoEM8qRyngsvJoT pZpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680294595; x=1682886595; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=ON3ePqj/wXwvqLAB1yIimizzqbLUeCNd0TWXqbkcGVM=; b=yCYGkSdUH3z5SDjfKhNYMPUtY2RDNf3SaVC/t+c3AIQwNpkR6s34ei/30TG9p2XOdA K1cewOsXyrSxlMePsVP5Z6RZs1HOXGM6SI98DUGNBg6lpoXxqdyIw/1D6k9ErqqQCtIj 0e5xdzHcmDTbRk3qVQ+l5wVX0vkIvODCQPttTPh/S259KUr/MY0VBuEih+Htm3ATWciX 2W2QvleRpXEVT9mszXy1pzpLt/WzTD9c2Y/wvlpepYHGfOu1TeI4Jb/hzdvo7WDfxKDL 8KAkGzuglhlNdnTUFFK7yKd3gE3419w1TL8w+GnQIqlNR1AJbBxn0Mzl2JcMGQsgCscX Sjag== X-Gm-Message-State: AAQBX9fatTVXMES5DCrDPodmhExKEcuaHr7vzDqj8QVeZg8OPX9xkBZH IwmSDCXUpwXBETtK6A4bIoy8Ct8tn3Y= X-Received: by 2002:a17:903:27ce:b0:19c:da7f:a234 with SMTP id km14-20020a17090327ce00b0019cda7fa234mr22844841plb.67.1680294594606; Fri, 31 Mar 2023 13:29:54 -0700 (PDT) Received: from bangji.hsd1.ca.comcast.net ([2601:647:6780:44b0:5cfb:ad2c:e6f5:d42f]) by smtp.gmail.com with ESMTPSA id t14-20020a1709028c8e00b001a1dc2be791sm1916803plo.259.2023.03.31.13.29.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 Mar 2023 13:29:54 -0700 (PDT) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Ian Rogers , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Kan Liang , Leo Yan Subject: [PATCH 2/9] perf tools: Fix a asan issue in parse_events_multi_pmu_add() Date: Fri, 31 Mar 2023 13:29:42 -0700 Message-Id: <20230331202949.810326-3-namhyung@kernel.org> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog In-Reply-To: <20230331202949.810326-1-namhyung@kernel.org> References: <20230331202949.810326-1-namhyung@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=0.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE, SPF_PASS autolearn=no 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?1761917513754084567?= X-GMAIL-MSGID: =?utf-8?q?1761917513754084567?= In the parse_events_multi_pmu_add() it passes the 'config' variable twice to parse_events_term__num() - one for config and another for loc_term. I'm not sure about the second one as it's converted to YYLTYPE variable. Asan reports it like below: In function ‘parse_events_term__num’, inlined from ‘parse_events_multi_pmu_add’ at util/parse-events.c:1602:6: util/parse-events.c:2653:64: error: array subscript ‘YYLTYPE[0]’ is partly outside array bounds of ‘char[8]’ [-Werror=array-bounds] 2653 | .err_term = loc_term ? loc_term->first_column : 0, | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~ util/parse-events.c: In function ‘parse_events_multi_pmu_add’: util/parse-events.c:1587:15: note: object ‘config’ of size 8 1587 | char *config; | ^~~~~~ cc1: all warnings being treated as errors Signed-off-by: Namhyung Kim --- tools/perf/util/parse-events.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c index cc8e8766ca30..0010e5e0ee68 100644 --- a/tools/perf/util/parse-events.c +++ b/tools/perf/util/parse-events.c @@ -1601,7 +1601,7 @@ int parse_events_multi_pmu_add(struct parse_events_state *parse_state, if (parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER, - config, 1, false, &config, + config, 1, false, NULL, NULL) < 0) { free(config); goto out_err; From patchwork Fri Mar 31 20:29:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 77973 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp828503vqo; Fri, 31 Mar 2023 13:45:51 -0700 (PDT) X-Google-Smtp-Source: AKy350ZgqW6w/rA8FcufHFnnqKyxhSozaG6Mwn1/o2+X3hiUkohfe+mGnodEJFOr8kimvSODN8kt X-Received: by 2002:a62:18d5:0:b0:625:ff85:21ec with SMTP id 204-20020a6218d5000000b00625ff8521ecmr28380162pfy.26.1680295551217; Fri, 31 Mar 2023 13:45:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680295551; cv=none; d=google.com; s=arc-20160816; b=sCpvigNTc6n5m2ewoWFQw2OMvYnklvv/VD8NP+ilqjk/DZ/ix/uI31kf2KTO4aZaXi bsKXuq1Kts6ewYo7y1hgUCkg2EoUNqmK8ENNhjudTK4CusFIsmXOWa0U4Umg7T1e89xI g8f7CUxiEhbF7EdkR+v3JYd1PTzQ4TaQc4EAiTr0f7xoQ+MzGd+LGEYwwDv+/5/vj5je hf00EMyncwCRCOUcU4Exf/UzuJyo0TbDGhmDICPOY5T4cBQisTYiw139TwRW1z1oL+TK GoU1NqTrjAW4r4bLOUs0zFvm1+koRZqASOMy08npdvLTA3YvF5AzbLpUeG16BTBuO7Ep LidA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from:sender :dkim-signature; bh=RYLcbGcSpDsJ6OJqWj4Xn7XRp6fiJcNPcFrLyu2WCoE=; b=cgIgax9T7WLi0bhtksdsnhgH5P/Aty5za/X7MnXF74PfGYPZdEpA/G2HGKfht1EIUp 68DCaUZdc6rfS2ZbDUm5Xkv231q2IWv/R9KZ+AWHpuFKdS+lH0lhCk/bGzHDWfs8daJj 4nWV47Kwr3eoVxSH6pbglht6b7V/S2tN194GBMWKx6BuGfR9PF9WGJh88LJb5Hn04fC+ faF/CyIY6lb/9d6Uao0UxTFEclx0D9Xu9OT/SWwceAS/5THEO/guLcvdqyGcMAbgHuKL y9K8RoUBQathYZ+S9hNKtl+eH/mR/8z27X0zJjobo7mqIIRaYIZ1+nMr3h872m5fvssh m7pQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=oad0BYHU; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 194-20020a6305cb000000b004fba312c235si3316084pgf.411.2023.03.31.13.45.38; Fri, 31 Mar 2023 13:45:51 -0700 (PDT) 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=@gmail.com header.s=20210112 header.b=oad0BYHU; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233134AbjCaUaG (ORCPT + 99 others); Fri, 31 Mar 2023 16:30:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44510 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230064AbjCaU36 (ORCPT ); Fri, 31 Mar 2023 16:29:58 -0400 Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B3DB01A473; Fri, 31 Mar 2023 13:29:56 -0700 (PDT) Received: by mail-pl1-x629.google.com with SMTP id o2so22393318plg.4; Fri, 31 Mar 2023 13:29:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680294596; x=1682886596; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=RYLcbGcSpDsJ6OJqWj4Xn7XRp6fiJcNPcFrLyu2WCoE=; b=oad0BYHUDzSkNBpOlyfUkLXc2FssNytWVUcfWpzbnbu2DAVp2rl7MCpfe82iKxzwOy EeeG4Z5UbpoUrko0DG24q2CidqbuBlw4K2Mx7OpclYJmrtPPpZvqmxHhQgELexYh7A9+ 3kKX7VFC2BAAF6yDNQX8ZaFgs/iZGg9oaCOTLi2hNWnfsEG7zyGjlzLItSQ8DmNbWB9z raaeBa+5P0JtoqnaZwhi+Bp0TWYwUr/caa9erQ5nSNGJU8V39NZMO3OopiEAlI86gjyX pmI9cR+dy5/klfLnGdjMcyqMh/z9AUufvub+AFHO8v6DOOgDm4T1aGSzeJoNPZZ3mDJG uSXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680294596; x=1682886596; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=RYLcbGcSpDsJ6OJqWj4Xn7XRp6fiJcNPcFrLyu2WCoE=; b=1EdQs7LiVNS/GbbL3dUjBCPlIIA09/yRee5ismJ6LTW/PSjHPEXRtEW2j9fDmrSxwb hSuzLb5BB1egg9j7QBXI1Gj5g1DEw9IG5uAAAlq0IkakljjD9D07ZgzavJrWwP2KovZX XA4SSXdF/7nE/tgMBgbIZeqxnrcmXzSEMUIsIhZ1PrZSROlRT+LQJFTB0cNcymmA/vyX SQB1IYXHXNHVkLKNZ0xzMZ3Pk8nFXfJqJi82h/U47wJsaJsD1uXoBPWOs1BKAmqGh2TE cE5RgSUUHJ3HPaLpUIa0VwB2lIsAiCLK58bZO9hV/L4lfX+HOuc6PxQW93gvAZpDCb11 yCHg== X-Gm-Message-State: AAQBX9cBNe2YWl1Ib6lx/XNHR7scGiQX5SDLThiDCnIzQH66h8xfiEZ8 3nUaMg8LFLWrfvjMfhHkbFg= X-Received: by 2002:a17:902:db03:b0:1a1:b15a:d916 with SMTP id m3-20020a170902db0300b001a1b15ad916mr35911481plx.3.1680294596038; Fri, 31 Mar 2023 13:29:56 -0700 (PDT) Received: from bangji.hsd1.ca.comcast.net ([2601:647:6780:44b0:5cfb:ad2c:e6f5:d42f]) by smtp.gmail.com with ESMTPSA id t14-20020a1709028c8e00b001a1dc2be791sm1916803plo.259.2023.03.31.13.29.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 Mar 2023 13:29:55 -0700 (PDT) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Ian Rogers , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Kan Liang , Leo Yan Subject: [PATCH 3/9] perf pmu: Add perf_pmu__destroy() function Date: Fri, 31 Mar 2023 13:29:43 -0700 Message-Id: <20230331202949.810326-4-namhyung@kernel.org> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog In-Reply-To: <20230331202949.810326-1-namhyung@kernel.org> References: <20230331202949.810326-1-namhyung@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=0.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE, SPF_PASS autolearn=no 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?1761917588186556304?= X-GMAIL-MSGID: =?utf-8?q?1761917588186556304?= It seems there's no function to delete the perf pmu struct. Add the perf_pmu__destroy() to do the job. While at it, add some more helper functions to delete pmu aliases and caps. Signed-off-by: Namhyung Kim --- tools/perf/util/pmu.c | 50 +++++++++++++++++++++++++++++++++++++++++++ tools/perf/util/pmu.h | 2 ++ 2 files changed, 52 insertions(+) diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index e3aae731bd6f..b112606f36ec 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -300,6 +300,16 @@ void perf_pmu_free_alias(struct perf_pmu_alias *newalias) free(newalias); } +static void perf_pmu__del_aliases(struct perf_pmu *pmu) +{ + struct perf_pmu_alias *alias, *tmp; + + list_for_each_entry_safe(alias, tmp, &pmu->aliases, list) { + list_del(&alias->list); + perf_pmu_free_alias(alias); + } +} + /* Merge an alias, search in alias list. If this name is already * present merge both of them to combine all information. */ @@ -921,6 +931,8 @@ static struct perf_pmu *pmu_lookup(const char *lookup_name) if (is_hybrid) list_add_tail(&pmu->hybrid_list, &perf_pmu__hybrid_pmus); + else + INIT_LIST_HEAD(&pmu->hybrid_list); pmu->default_config = perf_pmu__get_default_config(pmu); @@ -1750,6 +1762,18 @@ static int perf_pmu__new_caps(struct list_head *list, char *name, char *value) return -ENOMEM; } +static void perf_pmu__del_caps(struct perf_pmu *pmu) +{ + struct perf_pmu_caps *caps, *tmp; + + list_for_each_entry_safe(caps, tmp, &pmu->caps, list) { + list_del(&caps->list); + free(caps->name); + free(caps->value); + free(caps); + } +} + /* * Reading/parsing the given pmu capabilities, which should be located at: * /sys/bus/event_source/devices//caps as sysfs group attributes. @@ -1932,3 +1956,29 @@ int perf_pmu__pathname_scnprintf(char *buf, size_t size, return 0; return scnprintf(buf, size, "%s%s/%s", base_path, pmu_name, filename); } + +static void perf_pmu__delete(struct perf_pmu *pmu) +{ + perf_pmu__del_formats(&pmu->format); + perf_pmu__del_aliases(pmu); + perf_pmu__del_caps(pmu); + + perf_cpu_map__put(pmu->cpus); + + free(pmu->default_config); + free(pmu->name); + free(pmu->alias_name); + free(pmu); +} + +void perf_pmu__destroy(void) +{ + struct perf_pmu *pmu, *tmp; + + list_for_each_entry_safe(pmu, tmp, &pmus, list) { + list_del(&pmu->list); + list_del(&pmu->hybrid_list); + + perf_pmu__delete(pmu); + } +} diff --git a/tools/perf/util/pmu.h b/tools/perf/util/pmu.h index 24cf69ab32cd..72fd5de334c0 100644 --- a/tools/perf/util/pmu.h +++ b/tools/perf/util/pmu.h @@ -259,4 +259,6 @@ int perf_pmu__pathname_scnprintf(char *buf, size_t size, const char *pmu_name, const char *filename); FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name); +void perf_pmu__destroy(void); + #endif /* __PMU_H */ From patchwork Fri Mar 31 20:29:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 77970 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp827143vqo; Fri, 31 Mar 2023 13:43:12 -0700 (PDT) X-Google-Smtp-Source: AKy350ZjUlBzBtNngmWaxZm59qnH5Z5RgvH9Stq/v9ybQ8JstRVo80l+TikOg4mxaJOIkzBg7P4F X-Received: by 2002:a62:17d5:0:b0:625:6d5b:c019 with SMTP id 204-20020a6217d5000000b006256d5bc019mr25577708pfx.11.1680295391794; Fri, 31 Mar 2023 13:43:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680295391; cv=none; d=google.com; s=arc-20160816; b=yAdEivSgIlSC1sAu/foX3Kz/ZJMkpdvGdvqA+8JabYz/c15j2CFHCdQ1mXthw2kW9o jkMXarLfdaY3VoUV882d6ltk6S9Esk62d74Z0Du0kIWbO8vjBJuoWCzsmRdp8BmbXH/h bttMqpoHbFoc5mzTXmxzlzVQ+tCin+FUsvnp2O7x6jcHT4kI5trOx4HzlbgZ0HpT5hyn CbTJAU+vhc6k6qkJdP4VXIA82xDK6EI8CU+BpNsJGluqzDvEB2xAdLKFv9P+JoUizeju 4Fean3+RLV9L5MG2qQeoRz1VFgYGxs7E7vP8q7FxmApp6qychnuJa5QPWf6EJAsmcm6Q w0jA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from:sender :dkim-signature; bh=fbGI83psWi01QhPjR2ttTrwM+cN287gT/1Q3Pzny4tc=; b=wRXzBmxW0Vgms1kvaoChq0RSqgOl1BRrZFTTw1weclz/7z7l1EeJNYEnCo+PBfIdvt C8qg+vIilklIGFX0+LozvsFlCkvqUMnpkaq4L3E7Sm90Lrov1nvFuV8MtQauzt8AiWuB 9wBqSEk7GRJ2TyhZujxhyApD9ziq0UYszXnllxQ/zHPmaM2EwEK+sXjX0DZxp8HaG/qK 7C2b1Iu/VnPoL8aNN3PQ2NtCJ2BdoYwzdCO6mv7IB0opXHfyEIjHT4rd7PjfkOJeoRYV JyyXdlVrNEcKysJw0cX/pJ9syFycIPjroWFESghp206WmuD+8Q3GHCNSc9zqubUEpIIM 0maw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=Ti80uQAx; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e187-20020a6369c4000000b0051352302c74si3259814pgc.7.2023.03.31.13.42.59; Fri, 31 Mar 2023 13:43:11 -0700 (PDT) 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=@gmail.com header.s=20210112 header.b=Ti80uQAx; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233231AbjCaUaK (ORCPT + 99 others); Fri, 31 Mar 2023 16:30:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44640 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233169AbjCaU37 (ORCPT ); Fri, 31 Mar 2023 16:29:59 -0400 Received: from mail-pj1-x1030.google.com (mail-pj1-x1030.google.com [IPv6:2607:f8b0:4864:20::1030]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1497F2221E; Fri, 31 Mar 2023 13:29:58 -0700 (PDT) Received: by mail-pj1-x1030.google.com with SMTP id x15so21681033pjk.2; Fri, 31 Mar 2023 13:29:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680294597; x=1682886597; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=fbGI83psWi01QhPjR2ttTrwM+cN287gT/1Q3Pzny4tc=; b=Ti80uQAx6OpqUQQn6abxy1r6kIrgrYp5ucXtvk9dt06p/1yVv32tGEh6g9F98fwx3X 4ZU2Pbe//dxggcX700aMTo5Buj7wwNVrYVDcgKgq+YaP5Ww555i6KSIjOOsZMmw0nfji 6WGO4+XKePfeeZxFXzP2MHvqnUHH14iXbQpfVtwoVEjHNGC8R2xpNa8HAtPqQclMk8Nw G8E7nYF68HrNV8hJIV4hXAff9O8MXr2xpgkO8QgIqm/cTpizEOz1J/iAMY8TQjtrLmuw tutwRo5a0SCtbL9JChQFctQ5IvH9dgkmbqLh3mQneOkri/z2K3NRcmhbZluknNz05H3I z1sA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680294597; x=1682886597; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=fbGI83psWi01QhPjR2ttTrwM+cN287gT/1Q3Pzny4tc=; b=LYF6zqTIdeYjLySzn7RNX8CBhQG9VgPNlfh3R+3Wifzr8RkzefWRbzRs6eLrvIXFaP nOKavbjawHefckgyjsEEg+Ox88kCvvztRZvYLFhYmHauYp4Czmhj1T1qLbKcicTdSmGk j/XJVQTcoukNq4t/iuSYYYwprC9/f4r97BqfAFScQmqZYgvXGz/2yH4SLGlscxPGfR1B 7lNTeAcyj/btjUzAjFB7GSAMN49hrPc9za27lziPhdK9OhD6t5oXFIxz2S6beFmc9Ovv x9BnsXUyvzqkCKPdZiJEVcbpxH7odoNM4HMpA+AHQPshm+fQIzGhRxducumAx2CE0qsM ARDQ== X-Gm-Message-State: AAQBX9ejTEVEaaHe5zwck5uYlezk8ErBTTiro9TiiWWXneI2sgJBhDgs Rv9ghip7uC/f0BgEMFuXKt4= X-Received: by 2002:a17:902:ceca:b0:1a1:956e:5417 with SMTP id d10-20020a170902ceca00b001a1956e5417mr35644440plg.22.1680294597444; Fri, 31 Mar 2023 13:29:57 -0700 (PDT) Received: from bangji.hsd1.ca.comcast.net ([2601:647:6780:44b0:5cfb:ad2c:e6f5:d42f]) by smtp.gmail.com with ESMTPSA id t14-20020a1709028c8e00b001a1dc2be791sm1916803plo.259.2023.03.31.13.29.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 Mar 2023 13:29:57 -0700 (PDT) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Ian Rogers , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Kan Liang , Leo Yan Subject: [PATCH 4/9] perf bench: Add pmu-scan benchmark Date: Fri, 31 Mar 2023 13:29:44 -0700 Message-Id: <20230331202949.810326-5-namhyung@kernel.org> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog In-Reply-To: <20230331202949.810326-1-namhyung@kernel.org> References: <20230331202949.810326-1-namhyung@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=0.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE, SPF_PASS autolearn=no 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?1761917421182760924?= X-GMAIL-MSGID: =?utf-8?q?1761917421182760924?= The pmu-scan benchmark will repeatedly scan the sysfs to get the available PMU information. $ ./perf bench internals pmu-scan # Running 'internals/pmu-scan' benchmark: Computing performance of sysfs PMU event scan for 100 times Average PMU scanning took: 6850.990 usec (+- 48.445 usec) Signed-off-by: Namhyung Kim --- tools/perf/bench/Build | 1 + tools/perf/bench/bench.h | 1 + tools/perf/bench/pmu-scan.c | 184 ++++++++++++++++++++++++++++++++++++ tools/perf/builtin-bench.c | 1 + 4 files changed, 187 insertions(+) create mode 100644 tools/perf/bench/pmu-scan.c diff --git a/tools/perf/bench/Build b/tools/perf/bench/Build index 6b6155a8ad09..0f158dc8139b 100644 --- a/tools/perf/bench/Build +++ b/tools/perf/bench/Build @@ -15,6 +15,7 @@ perf-y += find-bit-bench.o perf-y += inject-buildid.o perf-y += evlist-open-close.o perf-y += breakpoint.o +perf-y += pmu-scan.o perf-$(CONFIG_X86_64) += mem-memcpy-x86-64-asm.o perf-$(CONFIG_X86_64) += mem-memset-x86-64-asm.o diff --git a/tools/perf/bench/bench.h b/tools/perf/bench/bench.h index edfc25793cca..0d2b65976212 100644 --- a/tools/perf/bench/bench.h +++ b/tools/perf/bench/bench.h @@ -42,6 +42,7 @@ int bench_inject_build_id(int argc, const char **argv); int bench_evlist_open_close(int argc, const char **argv); int bench_breakpoint_thread(int argc, const char **argv); int bench_breakpoint_enable(int argc, const char **argv); +int bench_pmu_scan(int argc, const char **argv); #define BENCH_FORMAT_DEFAULT_STR "default" #define BENCH_FORMAT_DEFAULT 0 diff --git a/tools/perf/bench/pmu-scan.c b/tools/perf/bench/pmu-scan.c new file mode 100644 index 000000000000..f0f007843bb8 --- /dev/null +++ b/tools/perf/bench/pmu-scan.c @@ -0,0 +1,184 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Benchmark scanning sysfs files for PMU information. + * + * Copyright 2023 Google LLC. + */ +#include +#include "bench.h" +#include "util/debug.h" +#include "util/pmu.h" +#include "util/pmus.h" +#include "util/stat.h" +#include +#include +#include +#include + +static unsigned int iterations = 100; + +struct pmu_scan_result { + char *name; + int nr_aliases; + int nr_formats; + int nr_caps; +}; + +static const struct option options[] = { + OPT_UINTEGER('i', "iterations", &iterations, + "Number of iterations used to compute average"), + OPT_END() +}; + +static const char *const bench_usage[] = { + "perf bench internals pmu-scan ", + NULL +}; + +static int nr_pmus; +static struct pmu_scan_result *results; + +static int save_result(void) +{ + struct perf_pmu *pmu; + struct list_head *list; + struct pmu_scan_result *r; + + perf_pmu__scan(NULL); + + perf_pmus__for_each_pmu(pmu) { + r = realloc(results, (nr_pmus + 1) * sizeof(*r)); + if (r == NULL) + return -ENOMEM; + + results = r; + r = results + nr_pmus; + + r->name = strdup(pmu->name); + r->nr_caps = pmu->nr_caps; + + r->nr_aliases = 0; + list_for_each(list, &pmu->aliases) + r->nr_aliases++; + + r->nr_formats = 0; + list_for_each(list, &pmu->format) + r->nr_formats++; + + pr_debug("pmu[%d] name=%s, nr_caps=%d, nr_aliases=%d, nr_formats=%d\n", + nr_pmus, r->name, r->nr_caps, r->nr_aliases, r->nr_formats); + nr_pmus++; + } + + perf_pmu__destroy(); + return 0; +} + +static int check_result(void) +{ + struct pmu_scan_result *r; + struct perf_pmu *pmu; + struct list_head *list; + int nr; + + for (int i = 0; i < nr_pmus; i++) { + r = &results[i]; + pmu = perf_pmu__find(r->name); + if (pmu == NULL) { + pr_err("Cannot find PMU %s\n", r->name); + return -1; + } + + if (pmu->nr_caps != (u32)r->nr_caps) { + pr_err("Unmatched number of event caps in %s: expect %d vs got %d\n", + pmu->name, r->nr_caps, pmu->nr_caps); + return -1; + } + + nr = 0; + list_for_each(list, &pmu->aliases) + nr++; + if (nr != r->nr_aliases) { + pr_err("Unmatched number of event aliases in %s: expect %d vs got %d\n", + pmu->name, r->nr_aliases, nr); + return -1; + } + + nr = 0; + list_for_each(list, &pmu->format) + nr++; + if (nr != r->nr_formats) { + pr_err("Unmatched number of event formats in %s: expect %d vs got %d\n", + pmu->name, r->nr_formats, nr); + return -1; + } + } + return 0; +} + +static void delete_result(void) +{ + for (int i = 0; i < nr_pmus; i++) + free(results[i].name); + free(results); + + results = NULL; + nr_pmus = 0; +} + +static int run_pmu_scan(void) +{ + struct stats stats; + struct timeval start, end, diff; + double time_average, time_stddev; + u64 runtime_us; + unsigned int i; + int ret; + + init_stats(&stats); + pr_info("Computing performance of sysfs PMU event scan for %u times\n", + iterations); + + if (save_result() < 0) { + pr_err("Failed to initialize PMU scan result\n"); + return -1; + } + + for (i = 0; i < iterations; i++) { + gettimeofday(&start, NULL); + perf_pmu__scan(NULL); + gettimeofday(&end, NULL); + + timersub(&end, &start, &diff); + runtime_us = diff.tv_sec * USEC_PER_SEC + diff.tv_usec; + update_stats(&stats, runtime_us); + + ret = check_result(); + perf_pmu__destroy(); + if (ret < 0) + break; + } + + time_average = avg_stats(&stats); + time_stddev = stddev_stats(&stats); + pr_info(" Average PMU scanning took: %.3f usec (+- %.3f usec)\n", + time_average, time_stddev); + + delete_result(); + return 0; +} + +int bench_pmu_scan(int argc, const char **argv) +{ + int err = 0; + + argc = parse_options(argc, argv, options, bench_usage, 0); + if (argc) { + usage_with_options(bench_usage, options); + exit(EXIT_FAILURE); + } + + err = run_pmu_scan(); + + return err; +} diff --git a/tools/perf/builtin-bench.c b/tools/perf/builtin-bench.c index d0fcc3cdc185..58f1cfe1eb34 100644 --- a/tools/perf/builtin-bench.c +++ b/tools/perf/builtin-bench.c @@ -92,6 +92,7 @@ static struct bench internals_benchmarks[] = { { "kallsyms-parse", "Benchmark kallsyms parsing", bench_kallsyms_parse }, { "inject-build-id", "Benchmark build-id injection", bench_inject_build_id }, { "evlist-open-close", "Benchmark evlist open and close", bench_evlist_open_close }, + { "pmu-scan", "Benchmark sysfs PMU info scanning", bench_pmu_scan }, { NULL, NULL, NULL } }; From patchwork Fri Mar 31 20:29:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 77969 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp826352vqo; Fri, 31 Mar 2023 13:41:26 -0700 (PDT) X-Google-Smtp-Source: AKy350aXqiF3AOAjKpdMiVS7Kw9RgYSfklM2YWDcrE0NiC09p70RtiNmmlWsUSMniZG0BAzu5ZZs X-Received: by 2002:a17:907:c70c:b0:91e:acf4:b009 with SMTP id ty12-20020a170907c70c00b0091eacf4b009mr30535612ejc.22.1680295286419; Fri, 31 Mar 2023 13:41:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680295286; cv=none; d=google.com; s=arc-20160816; b=YnupdS97/xsoFMkhIKtE6mOs8Tp9sG084hqFUbqoiU+zOIHEqqksDfmNLwvy3a+ph8 C0Fmew0/qwen0jaAIX9mxJYCvGaqO5tWMWPzoDOMESK3ovM2YySCsnFuFG0ZltXS1+pA rRTLM5U8aWwrxi36fMiEqhLz798SbmgZBXWsUxVjPsEDT0Xt43Q79/kU3a+jjrxaB/Oz 6veaGe0qdz2DrvdyHJ3aldiC479uMNQKnwxeV0WNbEMsjdwuhBWVXxwWT79tTKJEZd8X ZsEM8hyX1sKNDt20T2TZo04V80rTK+o8ANSz9+jo5GBviMcVx0jsy3I4wg75SvSKxZny ZKbg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from:sender :dkim-signature; bh=GApJR9zLFQnP8aqhRF4/4bSrT+6uWVcNNs+zyY06OJY=; b=QBpQRzsLZ7RASAQlWd348qBAJd2HdvT/xgyTid64TeQXsFzL547FxszJb3xGUHszUE 71b+I8jdVXwXx0GFqc39v22mo3zaShFRWVFZlUIOSTfKguAVuox6CCS+RJwLTT/iInvO hFRXyEeQLAAKKjqp6LxnJqIbFTBIQ2J0M6ChJUqWIEQimajNKNRralJz1ZaZo3kO69ht AUX5OxnH1xFREM88LinosdIu0BIEi4BGnbwCuc0UFRuj0ULT0MJIKzN0fqD+XVJc9or1 tauU2vCwKA8OfQegEZtSW27dS/MRDqOckP0GM8KXjYBaMNEWbPfF0dUFJBc2IJUx5hbI j5qQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="REZtFi+/"; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ci7-20020a170906c34700b0093dfe066be0si350948ejb.530.2023.03.31.13.41.01; Fri, 31 Mar 2023 13:41:26 -0700 (PDT) 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=@gmail.com header.s=20210112 header.b="REZtFi+/"; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233236AbjCaUaN (ORCPT + 99 others); Fri, 31 Mar 2023 16:30:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44816 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233186AbjCaUaC (ORCPT ); Fri, 31 Mar 2023 16:30:02 -0400 Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0EDFF23B41; Fri, 31 Mar 2023 13:29:59 -0700 (PDT) Received: by mail-pj1-x102a.google.com with SMTP id q102so21666820pjq.3; Fri, 31 Mar 2023 13:29:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680294599; x=1682886599; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=GApJR9zLFQnP8aqhRF4/4bSrT+6uWVcNNs+zyY06OJY=; b=REZtFi+/crDI7AZhkYx5PzqWXMi9F2zeF8dZVXgzkQESNlUa/oXlk6pf5z8+Pkzn1J zVNfP17Y01enw8tF+Y0J+JVo1sHrUlDHnlc/OrGkalVZ0BuSe+MkFcq18InEO5ehk59n MFxIH7pvX4q7AR/dsN6wEA7OtxMWALRMBR6/19SHdUjfWtTp66lTCLa1EN2YhASxknHL LuH9GiSL6hgzAwISkxtQjhN2u3fMv1F0V2232RsGE6chBZJo7+SlHiQw6FLwuGO27ms4 t1iFc6qSnC2fhF3rPbey1piYQhRgLfY3I/QWi2kUFlRRuM5qabCuaZCe1jlDz3z9Qf91 VaZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680294599; x=1682886599; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=GApJR9zLFQnP8aqhRF4/4bSrT+6uWVcNNs+zyY06OJY=; b=XJB8ph198avLxiH869U2LHh95QpeG/+CF806zH/G+Nl1gbtjrUaU23T0cLam1OtYvo tCrgBbUW5CkZezXGSPanqWMfCQAq2hjFZyfOMd6Fo4jel7sYRNwgD+45IVUS4k28YnlY 4KDeml4eVPeqdUcomP+tKhDxurUc84alR0aZNEzoftsj/gu9BVwku/USkQJT+w+09xuY Fk2PAAZ0R0yLzUOhgGt5IwhjHw5TyQUa/kgBR4oW8FFCk85P8MYL01Xp+Av8LNrQ1Z12 DT3tjaXF7LZPRFDC4UgcdqkKWgjVW13pHbguFB+6kRK4ds2Ppi2VEpVISaUa3++huz7k tzLA== X-Gm-Message-State: AAQBX9dxyeTsFy/annVP07Ud2BWk6khG5JEiD/7HhTv0F79fqSdDuBsG /dPRBDNIu28PFIyYEOYBbTY= X-Received: by 2002:a17:902:ccd2:b0:1a2:7d:17f2 with SMTP id z18-20020a170902ccd200b001a2007d17f2mr31934863ple.69.1680294599136; Fri, 31 Mar 2023 13:29:59 -0700 (PDT) Received: from bangji.hsd1.ca.comcast.net ([2601:647:6780:44b0:5cfb:ad2c:e6f5:d42f]) by smtp.gmail.com with ESMTPSA id t14-20020a1709028c8e00b001a1dc2be791sm1916803plo.259.2023.03.31.13.29.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 Mar 2023 13:29:58 -0700 (PDT) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Ian Rogers , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Kan Liang , Leo Yan Subject: [PATCH 5/9] perf pmu: Use relative path for sysfs scan Date: Fri, 31 Mar 2023 13:29:45 -0700 Message-Id: <20230331202949.810326-6-namhyung@kernel.org> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog In-Reply-To: <20230331202949.810326-1-namhyung@kernel.org> References: <20230331202949.810326-1-namhyung@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=0.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE, SPF_PASS autolearn=no 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?1761917310347797632?= X-GMAIL-MSGID: =?utf-8?q?1761917310347797632?= The PMU information is in the kernel sysfs so it needs to scan the directory to get the whole information like event aliases, formats and so on. During the traversal, it opens a lot of files and directories like below: dir = opendir("/sys/bus/event_source/devices"); while (dentry = readdir(dir)) { char buf[PATH_MAX]; snprintf(buf, sizeof(buf), "%s/%s", "/sys/bus/event_source/devices", dentry->d_name); fd = open(buf, O_RDONLY); ... } But this is not good since it needs to copy the string to build the absolute pathname, and it makes redundant pathname walk (from the /sys) unnecessarily. We can use openat(2) to open the file in the given directory. While it's not a problem ususally, it can be a problem when the kernel has contentions on the sysfs. Add a couple of new helper to return the file descriptor of PMU directory so that it can use it with relative paths. * perf_pmu__event_source_devices_fd() - returns a fd for the PMU root ("/sys/bus/event_source/devices") * perf_pmu__pathname_fd() - returns a fd for "/" under the PMU root Now the above code can be converted something like below: dirfd = perf_pmu__event_source_devices_fd(); dir = fdopendir(dirfd); while (dentry = readdir(dir)) { fd = openat(dirfd, dentry->d_name, O_RDONLY); ... } Signed-off-by: Namhyung Kim --- tools/perf/tests/pmu.c | 9 ++- tools/perf/util/pmu.c | 161 +++++++++++++++++++++++++---------------- tools/perf/util/pmu.h | 4 +- 3 files changed, 111 insertions(+), 63 deletions(-) diff --git a/tools/perf/tests/pmu.c b/tools/perf/tests/pmu.c index 8507bd615e97..3cf25f883df7 100644 --- a/tools/perf/tests/pmu.c +++ b/tools/perf/tests/pmu.c @@ -3,6 +3,7 @@ #include "pmu.h" #include "tests.h" #include +#include #include #include #include @@ -149,10 +150,16 @@ static int test__pmu(struct test_suite *test __maybe_unused, int subtest __maybe do { struct perf_event_attr attr; + int fd; memset(&attr, 0, sizeof(attr)); - ret = perf_pmu__format_parse(format, &formats); + fd = open(format, O_DIRECTORY); + if (fd < 0) { + ret = fd; + break; + } + ret = perf_pmu__format_parse(fd, &formats); if (ret) break; diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index b112606f36ec..9fc6b8b5732b 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -62,38 +62,38 @@ extern FILE *perf_pmu_in; static bool hybrid_scanned; +static struct perf_pmu *perf_pmu__find2(int dirfd, const char *name); + /* * Parse & process all the sysfs attributes located under * the directory specified in 'dir' parameter. */ -int perf_pmu__format_parse(char *dir, struct list_head *head) +int perf_pmu__format_parse(int dirfd, struct list_head *head) { struct dirent *evt_ent; DIR *format_dir; int ret = 0; - format_dir = opendir(dir); + format_dir = fdopendir(dirfd); if (!format_dir) return -EINVAL; while (!ret && (evt_ent = readdir(format_dir))) { - char path[PATH_MAX]; char *name = evt_ent->d_name; - FILE *file; + int fd; if (!strcmp(name, ".") || !strcmp(name, "..")) continue; - snprintf(path, PATH_MAX, "%s/%s", dir, name); ret = -EINVAL; - file = fopen(path, "r"); - if (!file) + fd = openat(dirfd, name, O_RDONLY); + if (fd < 0) break; - perf_pmu_in = file; + perf_pmu_in = fdopen(fd, "r"); ret = perf_pmu_parse(head, name); - fclose(file); + fclose(perf_pmu_in); } closedir(format_dir); @@ -105,17 +105,16 @@ int perf_pmu__format_parse(char *dir, struct list_head *head) * located at: * /sys/bus/event_source/devices//format as sysfs group attributes. */ -static int pmu_format(const char *name, struct list_head *format) +static int pmu_format(int dirfd, const char *name, struct list_head *format) { - char path[PATH_MAX]; - - if (!perf_pmu__pathname_scnprintf(path, sizeof(path), name, "format")) - return -1; + int fd; - if (!file_available(path)) + fd = perf_pmu__pathname_fd(dirfd, name, "format", O_DIRECTORY); + if (fd < 0) return 0; - if (perf_pmu__format_parse(path, format)) + /* it'll close the fd */ + if (perf_pmu__format_parse(fd, format)) return -1; return 0; @@ -158,7 +157,7 @@ int perf_pmu__convert_scale(const char *scale, char **end, double *sval) return ret; } -static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name) +static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, int dirfd, char *name) { struct stat st; ssize_t sret; @@ -166,9 +165,9 @@ static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char * int fd, ret = -1; char path[PATH_MAX]; - scnprintf(path, PATH_MAX, "%s/%s.scale", dir, name); + scnprintf(path, PATH_MAX, "%s.scale", name); - fd = open(path, O_RDONLY); + fd = openat(dirfd, path, O_RDONLY); if (fd == -1) return -1; @@ -190,15 +189,15 @@ static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char * return ret; } -static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name) +static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, int dirfd, char *name) { char path[PATH_MAX]; ssize_t sret; int fd; - scnprintf(path, PATH_MAX, "%s/%s.unit", dir, name); + scnprintf(path, PATH_MAX, "%s.unit", name); - fd = open(path, O_RDONLY); + fd = openat(dirfd, path, O_RDONLY); if (fd == -1) return -1; @@ -221,14 +220,14 @@ static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *n } static int -perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name) +perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, int dirfd, char *name) { char path[PATH_MAX]; int fd; - scnprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name); + scnprintf(path, PATH_MAX, "%s.per-pkg", name); - fd = open(path, O_RDONLY); + fd = openat(dirfd, path, O_RDONLY); if (fd == -1) return -1; @@ -239,14 +238,14 @@ perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name) } static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias, - char *dir, char *name) + int dirfd, char *name) { char path[PATH_MAX]; int fd; - scnprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name); + scnprintf(path, PATH_MAX, "%s.snapshot", name); - fd = open(path, O_RDONLY); + fd = openat(dirfd, path, O_RDONLY); if (fd == -1) return -1; @@ -332,7 +331,7 @@ static bool perf_pmu_merge_alias(struct perf_pmu_alias *newalias, return false; } -static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name, +static int __perf_pmu__new_alias(struct list_head *list, int dirfd, char *name, char *desc, char *val, const struct pmu_event *pe) { struct parse_events_term *term; @@ -391,14 +390,14 @@ static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name, } alias->name = strdup(name); - if (dir) { + if (dirfd >= 0) { /* * load unit name and scale if available */ - perf_pmu__parse_unit(alias, dir, name); - perf_pmu__parse_scale(alias, dir, name); - perf_pmu__parse_per_pkg(alias, dir, name); - perf_pmu__parse_snapshot(alias, dir, name); + perf_pmu__parse_unit(alias, dirfd, name); + perf_pmu__parse_scale(alias, dirfd, name); + perf_pmu__parse_per_pkg(alias, dirfd, name); + perf_pmu__parse_snapshot(alias, dirfd, name); } alias->desc = desc ? strdup(desc) : NULL; @@ -419,7 +418,7 @@ static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name, return 0; } -static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file) +static int perf_pmu__new_alias(struct list_head *list, int dirfd, char *name, FILE *file) { char buf[256]; int ret; @@ -433,7 +432,7 @@ static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FI /* Remove trailing newline from sysfs file */ strim(buf); - return __perf_pmu__new_alias(list, dir, name, NULL, buf, NULL); + return __perf_pmu__new_alias(list, dirfd, name, NULL, buf, NULL); } static inline bool pmu_alias_info_file(char *name) @@ -457,17 +456,17 @@ static inline bool pmu_alias_info_file(char *name) * Process all the sysfs attributes located under the directory * specified in 'dir' parameter. */ -static int pmu_aliases_parse(char *dir, struct list_head *head) +static int pmu_aliases_parse(int dirfd, struct list_head *head) { struct dirent *evt_ent; DIR *event_dir; + int fd; - event_dir = opendir(dir); + event_dir = fdopendir(dirfd); if (!event_dir) return -EINVAL; while ((evt_ent = readdir(event_dir))) { - char path[PATH_MAX]; char *name = evt_ent->d_name; FILE *file; @@ -480,15 +479,14 @@ static int pmu_aliases_parse(char *dir, struct list_head *head) if (pmu_alias_info_file(name)) continue; - scnprintf(path, PATH_MAX, "%s/%s", dir, name); - - file = fopen(path, "r"); + fd = openat(dirfd, name, O_RDONLY); + file = fdopen(fd, "r"); if (!file) { - pr_debug("Cannot open %s\n", path); + pr_debug("Cannot open %s\n", name); continue; } - if (perf_pmu__new_alias(head, dir, name, file) < 0) + if (perf_pmu__new_alias(head, dirfd, name, file) < 0) pr_debug("Cannot set up %s\n", name); fclose(file); } @@ -501,17 +499,16 @@ static int pmu_aliases_parse(char *dir, struct list_head *head) * Reading the pmu event aliases definition, which should be located at: * /sys/bus/event_source/devices//events as sysfs group attributes. */ -static int pmu_aliases(const char *name, struct list_head *head) +static int pmu_aliases(int dirfd, const char *name, struct list_head *head) { - char path[PATH_MAX]; - - if (!perf_pmu__pathname_scnprintf(path, sizeof(path), name, "events")) - return -1; + int fd; - if (!file_available(path)) + fd = perf_pmu__pathname_fd(dirfd, name, "events", O_DIRECTORY); + if (fd < 0) return 0; - if (pmu_aliases_parse(path, head)) + /* it'll close the fd */ + if (pmu_aliases_parse(fd, head)) return -1; return 0; @@ -544,14 +541,15 @@ static int pmu_alias_terms(struct perf_pmu_alias *alias, /* Add all pmus in sysfs to pmu list: */ static void pmu_read_sysfs(void) { - char path[PATH_MAX]; + int fd; DIR *dir; struct dirent *dent; - if (!perf_pmu__event_source_devices_scnprintf(path, sizeof(path))) + fd = perf_pmu__event_source_devices_fd(); + if (fd < 0) return; - dir = opendir(path); + dir = fdopendir(fd); if (!dir) return; @@ -559,7 +557,7 @@ static void pmu_read_sysfs(void) if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) continue; /* add to static LIST_HEAD(pmus): */ - perf_pmu__find(dent->d_name); + perf_pmu__find2(fd, dent->d_name); } closedir(dir); @@ -763,7 +761,7 @@ static int pmu_add_cpu_aliases_map_callback(const struct pmu_event *pe, new_alias: /* need type casts to override 'const' */ - __perf_pmu__new_alias(data->head, NULL, (char *)pe->name, (char *)pe->desc, + __perf_pmu__new_alias(data->head, -1, (char *)pe->name, (char *)pe->desc, (char *)pe->event, pe); return 0; } @@ -814,7 +812,7 @@ static int pmu_add_sys_aliases_iter_fn(const struct pmu_event *pe, if (!strcmp(pmu->id, pe->compat) && pmu_uncore_alias_match(pe->pmu, pmu->name)) { - __perf_pmu__new_alias(idata->head, NULL, + __perf_pmu__new_alias(idata->head, -1, (char *)pe->name, (char *)pe->desc, (char *)pe->event, @@ -863,7 +861,7 @@ static int pmu_max_precise(struct perf_pmu *pmu) return max_precise; } -static struct perf_pmu *pmu_lookup(const char *lookup_name) +static struct perf_pmu *pmu_lookup(int dirfd, const char *lookup_name) { struct perf_pmu *pmu; LIST_HEAD(format); @@ -884,13 +882,13 @@ static struct perf_pmu *pmu_lookup(const char *lookup_name) * type value and format definitions. Load both right * now. */ - if (pmu_format(name, &format)) + if (pmu_format(dirfd, name, &format)) return NULL; /* * Check the aliases first to avoid unnecessary work. */ - if (pmu_aliases(name, &aliases)) + if (pmu_aliases(dirfd, name, &aliases)) return NULL; pmu = zalloc(sizeof(*pmu)); @@ -1024,6 +1022,27 @@ bool evsel__is_aux_event(const struct evsel *evsel) } struct perf_pmu *perf_pmu__find(const char *name) +{ + struct perf_pmu *pmu; + int dirfd; + + /* + * Once PMU is loaded it stays in the list, + * so we keep us from multiple reading/parsing + * the pmu format definitions. + */ + pmu = pmu_find(name); + if (pmu) + return pmu; + + dirfd = perf_pmu__event_source_devices_fd(); + pmu = pmu_lookup(dirfd, name); + close(dirfd); + + return pmu; +} + +static struct perf_pmu *perf_pmu__find2(int dirfd, const char *name) { struct perf_pmu *pmu; @@ -1036,7 +1055,7 @@ struct perf_pmu *perf_pmu__find(const char *name) if (pmu) return pmu; - return pmu_lookup(name); + return pmu_lookup(dirfd, name); } static struct perf_pmu_format * @@ -1938,6 +1957,18 @@ int perf_pmu__event_source_devices_scnprintf(char *pathname, size_t size) return scnprintf(pathname, size, "%s/bus/event_source/devices/", sysfs); } +int perf_pmu__event_source_devices_fd(void) +{ + char path[PATH_MAX]; + const char *sysfs = sysfs__mountpoint(); + + if (!sysfs) + return -1; + + scnprintf(path, sizeof(path), "%s/bus/event_source/devices/", sysfs); + return open(path, O_DIRECTORY); +} + /* * Fill 'buf' with the path to a file or folder in 'pmu_name' in * sysfs. For example if pmu_name = "cs_etm" and 'filename' = "format" @@ -1957,6 +1988,14 @@ int perf_pmu__pathname_scnprintf(char *buf, size_t size, return scnprintf(buf, size, "%s%s/%s", base_path, pmu_name, filename); } +int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *filename, int flags) +{ + char path[PATH_MAX]; + + scnprintf(path, sizeof(path), "%s/%s", pmu_name, filename); + return openat(dirfd, path, flags); +} + static void perf_pmu__delete(struct perf_pmu *pmu) { perf_pmu__del_formats(&pmu->format); diff --git a/tools/perf/util/pmu.h b/tools/perf/util/pmu.h index 72fd5de334c0..751c7016e7b6 100644 --- a/tools/perf/util/pmu.h +++ b/tools/perf/util/pmu.h @@ -211,7 +211,7 @@ void perf_pmu_error(struct list_head *list, char *name, char const *msg); int perf_pmu__new_format(struct list_head *list, char *name, int config, unsigned long *bits); void perf_pmu__set_format(unsigned long *bits, long from, long to); -int perf_pmu__format_parse(char *dir, struct list_head *head); +int perf_pmu__format_parse(int dirfd, struct list_head *head); void perf_pmu__del_formats(struct list_head *formats); struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu); @@ -257,6 +257,8 @@ double perf_pmu__cpu_slots_per_cycle(void); int perf_pmu__event_source_devices_scnprintf(char *pathname, size_t size); int perf_pmu__pathname_scnprintf(char *buf, size_t size, const char *pmu_name, const char *filename); +int perf_pmu__event_source_devices_fd(void); +int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *filename, int flags); FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name); void perf_pmu__destroy(void); From patchwork Fri Mar 31 20:29:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 77971 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp827815vqo; Fri, 31 Mar 2023 13:44:34 -0700 (PDT) X-Google-Smtp-Source: AK7set9DlmFdV0TkrG24VZxbFGu7qm3nZ6XA1Q8YmbZl2UUSRI5uavDLKmJrlbzqNFEF1Lt+nRxj X-Received: by 2002:a05:6a20:b70f:b0:da:318a:981f with SMTP id fg15-20020a056a20b70f00b000da318a981fmr25706619pzb.42.1680295474338; Fri, 31 Mar 2023 13:44:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680295474; cv=none; d=google.com; s=arc-20160816; b=aQvcvxOjLvqAol0ebNtQe9u11wEkIe2WhEWudmIXc5nWjSUtWie9zGCtEIbxaaEM6Y lbtLTxbTAkPrL0UTYb9NzjyEXrumQHtlY0C2++4R0FhW2+t+iLwiHbfmrS2xPl3o0iWS 9YpVam5EKS3lL9y9B+33UkaOorNXH5ni4U0eSgQNn8kehY1zSDzajZ/aJff2JhxtNkdX S9JjCBUWR0Ieh7k0r4obZmZ8VU6wM4Mvt29XdezfJVCmTMtxOyNk4kJq6PPG6DSKUMCa X8gY/nrWeyQNhLMzm3iX0vrBT++HP0QgGdM4i7YG6BDMQM+iq/f/G/sudOf+YeocHkl4 9PPw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from:sender :dkim-signature; bh=Kqxyb/rKt2Y/ABAu4ly+tta42zomTNbs/md/9tPbhmg=; b=w3N+IVtKn+orM9o7e1r9Bau0qVRAXwEEZ4neadU3p6qy/Chwkb40a1fQ0inMYGCIk8 Yuryps81kyZJWL0gf1h+19PpcOSTM9Forv2SfxbsMD0emrRGFG+zBXeQYSueOp/GJo5p J5AeauziA+H5/fqIhNioeKWC29X/bqeTx9jcbfJCOAgFk8uZU+vzVJ8i8j02AGGTdTzt h78CVagKBMZkOYrSMonQDPWebggvRzwoP2RgqmolJU+xi13cv2PufHd2W2SX11YwN7ex EJOF7AZcD6G+xbRBU+iE/9rC3LMKN14gjlsDStaKjoibnWALyHQ6mNoVCeerAJtPs3n5 gHzg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=ZmKzGxXQ; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x1-20020a63fe41000000b00513335a9cd7si2990562pgj.133.2023.03.31.13.44.21; Fri, 31 Mar 2023 13:44:34 -0700 (PDT) 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=@gmail.com header.s=20210112 header.b=ZmKzGxXQ; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233244AbjCaUaR (ORCPT + 99 others); Fri, 31 Mar 2023 16:30:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44598 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233190AbjCaUaD (ORCPT ); Fri, 31 Mar 2023 16:30:03 -0400 Received: from mail-pl1-x62b.google.com (mail-pl1-x62b.google.com [IPv6:2607:f8b0:4864:20::62b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5A25922916; Fri, 31 Mar 2023 13:30:01 -0700 (PDT) Received: by mail-pl1-x62b.google.com with SMTP id u10so22378180plz.7; Fri, 31 Mar 2023 13:30:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680294600; x=1682886600; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=Kqxyb/rKt2Y/ABAu4ly+tta42zomTNbs/md/9tPbhmg=; b=ZmKzGxXQhI5XpYfBW7NUYEcSzjPjkr6umwjV2WNVK9ly3t1fbi9ObRFjfCt9HHmXF0 Nr2dtH6jJBaE0Ale5lX7nhM79p17IMTkJMYM5z7Z59A7otgECj409oDF/+c4/20XRg6d IwbhoHm3UzLgH2V85QTxkztR/o6tfP92l/wU2FQpmF0UNRWY0dZ1nZEuy58AxhGHXCxW 3F/xzj0B1iWz6xuUMwLc98NSJHA08BT+29DZwsiPWuXFvcCxtXCmSgiN8rgvpd4vPzKs Wx+IKA0ytjEk0SbeFuEON4DSAhsb68N1azPNLJZll/tDFy+q2d8iG2n4EDnD53+2VSQe Rc1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680294600; x=1682886600; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=Kqxyb/rKt2Y/ABAu4ly+tta42zomTNbs/md/9tPbhmg=; b=WRHkeAmJJHFVjjtmh4ZkanrX5wzJ4G0gzf/ciXsDOuVzxrb4CFXb+hFhdQnutFp+z0 5eW+falKOr/9ERJFWUXbIMHmCyQuBavpoKjLiG9I1UuOZzg8wJkN/FjzUEPahTF0+X95 0p/uNvp2iqY3BeEBt84IwqqCjBt1oNGK68GaWVF06boqJbKdcababEjOQDuky7c79xrY 6i78kwW1EaC7fvaIyFDG0aGD+QnUGT7E0pLjx9bRkuzFKl+JoducyPQEPZOSwTX/jG4Q hkUEFSfNOjCZk//H+bVxavbT+lODe4yy0u0eDYTXFUVm9nHcmaYr2RYDpWe8cJigpL9E ARwQ== X-Gm-Message-State: AAQBX9f0/CdjhVeL2+Fc0yKq5/bXzOfy7z+Wzq2RvJQuyk21+ZOregOC C06udTf7Ahl8Y93fYBb2phU= X-Received: by 2002:a17:902:f315:b0:19e:9849:1767 with SMTP id c21-20020a170902f31500b0019e98491767mr22990632ple.42.1680294600644; Fri, 31 Mar 2023 13:30:00 -0700 (PDT) Received: from bangji.hsd1.ca.comcast.net ([2601:647:6780:44b0:5cfb:ad2c:e6f5:d42f]) by smtp.gmail.com with ESMTPSA id t14-20020a1709028c8e00b001a1dc2be791sm1916803plo.259.2023.03.31.13.29.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 Mar 2023 13:30:00 -0700 (PDT) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Ian Rogers , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Kan Liang , Leo Yan Subject: [PATCH 6/9] perf pmu: Use relative path in perf_pmu__caps_parse() Date: Fri, 31 Mar 2023 13:29:46 -0700 Message-Id: <20230331202949.810326-7-namhyung@kernel.org> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog In-Reply-To: <20230331202949.810326-1-namhyung@kernel.org> References: <20230331202949.810326-1-namhyung@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=0.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE, SPF_PASS autolearn=no 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?1761917507344096442?= X-GMAIL-MSGID: =?utf-8?q?1761917507344096442?= Likewise, it needs to traverse the pmu/caps directory, let's use openat() with the dirfd instead of open() using the absolute path. Signed-off-by: Namhyung Kim --- tools/perf/util/pmu.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index 9fc6b8b5732b..0c1d87f10b23 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -1804,6 +1804,7 @@ int perf_pmu__caps_parse(struct perf_pmu *pmu) char caps_path[PATH_MAX]; DIR *caps_dir; struct dirent *evt_ent; + int caps_fd; if (pmu->caps_initialized) return pmu->nr_caps; @@ -1822,18 +1823,19 @@ int perf_pmu__caps_parse(struct perf_pmu *pmu) if (!caps_dir) return -EINVAL; + caps_fd = dirfd(caps_dir); + while ((evt_ent = readdir(caps_dir)) != NULL) { - char path[PATH_MAX + NAME_MAX + 1]; char *name = evt_ent->d_name; char value[128]; FILE *file; + int fd; if (!strcmp(name, ".") || !strcmp(name, "..")) continue; - snprintf(path, sizeof(path), "%s/%s", caps_path, name); - - file = fopen(path, "r"); + fd = openat(caps_fd, name, O_RDONLY); + file = fdopen(fd, "r"); if (!file) continue; From patchwork Fri Mar 31 20:29:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 77966 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp821822vqo; Fri, 31 Mar 2023 13:33:01 -0700 (PDT) X-Google-Smtp-Source: AKy350YJRzy/pBdpJ2FKCYvk4yx+yRkTBptJC6y9MCfbxTtTrtAOj6HQeG55EeBku+5mM1/BhC6L X-Received: by 2002:a17:90b:3eca:b0:237:5a3c:e86c with SMTP id rm10-20020a17090b3eca00b002375a3ce86cmr31783296pjb.24.1680294781400; Fri, 31 Mar 2023 13:33:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680294781; cv=none; d=google.com; s=arc-20160816; b=t1KyaVvUE0hsXlqv5/1BhLbmUXaL0uT8LbgHkigcjef/GfyOHS89iWODgHFIDUUmYW Qs71PnDJsFliSWWEPdSb8TdQaixGhFwjUcWWgp+6xPrT8lT5JRJWcjva/30kmSs267QL qMWFw98vEqbio4pcnhsRoFupSsrLjENg0s/DSCzuBtMFRuLAuuInxz4jjfjG1w6vq2wP lqY+spBQK6eNPtouyqpqybvwJvhPBJ9mI9kKEzY8NhX0PZnVQwvFgGRx8AqCX0fk5+6o 0jTktUKkbDjphdRFcuPvtnQYxozbZsHbsGlTE3fvybMXrODDtasiP8kLrq3Pawg3SiSf 5n9Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from:sender :dkim-signature; bh=XJy9ywO65dgXdPExYWd/WvWg/5bb5i5vxOfS+9ONLpE=; b=Rn2x0rwJWhEed//ac2Q62UDYULuAHsk9sglC2rQzvQOmw354lm4SjIlXoFUTkpA2Sh qx5dPkSgwJZuQ9Jm0kYSUydRmKJMX2tYfRmZLXl2ZNmeRPTvLOhIgokQvB6H3oN7QwcD ruGmcUkhmh3WTLDTjQF2MueHjxrejKYeq9yrkrRH/CwJHVisYwXgKGW3yA8EzuQKk2WX 8mFET/HIRUxfsoY2qb36nxD9XxsRYkMnW0TzUrrooij6c/G2QkqTn1/ywBYnkYzka12v oWglDHHqmgAAy2QYDloxIMhjTc1p11h0vIdfuNn3A8HGJWtBVhoL1fWJMxUFK+2dCNU5 99kg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="Dq/ZgPQk"; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y6-20020a17090aa40600b00240ad93edfesi2912268pjp.5.2023.03.31.13.32.48; Fri, 31 Mar 2023 13:33:01 -0700 (PDT) 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=@gmail.com header.s=20210112 header.b="Dq/ZgPQk"; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231851AbjCaUa0 (ORCPT + 99 others); Fri, 31 Mar 2023 16:30:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44752 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233202AbjCaUaE (ORCPT ); Fri, 31 Mar 2023 16:30:04 -0400 Received: from mail-pj1-x102d.google.com (mail-pj1-x102d.google.com [IPv6:2607:f8b0:4864:20::102d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AC85922222; Fri, 31 Mar 2023 13:30:02 -0700 (PDT) Received: by mail-pj1-x102d.google.com with SMTP id j13so21688815pjd.1; Fri, 31 Mar 2023 13:30:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680294602; x=1682886602; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=XJy9ywO65dgXdPExYWd/WvWg/5bb5i5vxOfS+9ONLpE=; b=Dq/ZgPQkU5fblBTtWmiLNvUS3Ic9vsP2pqx4BomQixfxHnz8czK3NIW/AbhsvVtDxG PTzdoZB7mn5rHN567sdRjrqPRtDd73uvKeHNdnXjQMNpQ1uo8fT96PCbmExoZGHwdFI3 Pc8NeVJ+Sh9YuLTgS+KE31VtrcLJ+vrM/U4Xgkh3DS0v6LZptXkFn02JgUyKnQ/WO/Ey DG8u4Bj1hoZIlcd1HTr/grLCBUGE4NSVXcdqpN4gvfLmOj3gQtq+w1xPePRqnmuljpWU bWdYUY9PXrw6q4UXwpzCMWLDQH5Naj2rvlNqZ0D5pBzljp45K4Taxjl4vDB+bbAwXOla hyGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680294602; x=1682886602; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=XJy9ywO65dgXdPExYWd/WvWg/5bb5i5vxOfS+9ONLpE=; b=oathkcXPzMzrvWwIb7bEbfIG0QfMla9v6CTSJTeW/ML2r5k4f9JNwURwlpy5tU2K48 HQv5G/482yN8+r4o44cX+qfUwBU6/lt72esAvH8x+yTpn3LP2S3CxOKGYgvcoe9W8B45 CVnIkFT0CtE7LjY51ibjGdW0HUob7qrEHlVY+2JxvYHwZOh8r8QxwPJut7pxhXXtZAZr /U9gciVSIK1PqjCfN8C9ecPqaKMK/wMAhQ0xzd6m9bZz6EeZUmW2HlUNxWRUv5okN1Xu RDwtBPbPrde7JH5HXhbPikBVHGjpkOhyq3kJ0K8a6VUxY6X1B57HXTLLksq85NP2G+nU k09w== X-Gm-Message-State: AAQBX9dzWsmjBIweaxErhysL/TnC156Xdg7D0jBsJcA6M8D+7iu5LzBz 2u4fYmimFXFu3tL5+LdmjuM= X-Received: by 2002:a17:902:c951:b0:1a2:3108:5cc9 with SMTP id i17-20020a170902c95100b001a231085cc9mr28632579pla.40.1680294602145; Fri, 31 Mar 2023 13:30:02 -0700 (PDT) Received: from bangji.hsd1.ca.comcast.net ([2601:647:6780:44b0:5cfb:ad2c:e6f5:d42f]) by smtp.gmail.com with ESMTPSA id t14-20020a1709028c8e00b001a1dc2be791sm1916803plo.259.2023.03.31.13.30.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 Mar 2023 13:30:01 -0700 (PDT) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Ian Rogers , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Kan Liang , Leo Yan Subject: [PATCH 7/9] perf pmu: Use relative path in setup_pmu_alias_list() Date: Fri, 31 Mar 2023 13:29:47 -0700 Message-Id: <20230331202949.810326-8-namhyung@kernel.org> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog In-Reply-To: <20230331202949.810326-1-namhyung@kernel.org> References: <20230331202949.810326-1-namhyung@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=0.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE, SPF_PASS autolearn=no 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?1761916780400604511?= X-GMAIL-MSGID: =?utf-8?q?1761916780400604511?= Likewise, x86 needs to traverse the PMU list to build alias. Let's use the new helpers to use relative paths. Signed-off-by: Namhyung Kim --- tools/perf/arch/x86/util/pmu.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/tools/perf/arch/x86/util/pmu.c b/tools/perf/arch/x86/util/pmu.c index f73b80dcd8bd..3c0de3370d7e 100644 --- a/tools/perf/arch/x86/util/pmu.c +++ b/tools/perf/arch/x86/util/pmu.c @@ -71,7 +71,7 @@ static struct pmu_alias *pmu_alias__new(char *name, char *alias) static int setup_pmu_alias_list(void) { - char path[PATH_MAX]; + int fd, dirfd; DIR *dir; struct dirent *dent; struct pmu_alias *pmu_alias; @@ -79,10 +79,11 @@ static int setup_pmu_alias_list(void) FILE *file; int ret = -ENOMEM; - if (!perf_pmu__event_source_devices_scnprintf(path, sizeof(path))) + dirfd = perf_pmu__event_source_devices_fd(); + if (dirfd < 0) return -1; - dir = opendir(path); + dir = fdopendir(dirfd); if (!dir) return -errno; @@ -91,11 +92,11 @@ static int setup_pmu_alias_list(void) !strcmp(dent->d_name, "..")) continue; - perf_pmu__pathname_scnprintf(path, sizeof(path), dent->d_name, "alias"); - if (!file_available(path)) + fd = perf_pmu__pathname_fd(dirfd, dent->d_name, "alias", O_RDONLY); + if (fd < 0) continue; - file = fopen(path, "r"); + file = fdopen(fd, "r"); if (!file) continue; From patchwork Fri Mar 31 20:29:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 77965 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp821543vqo; Fri, 31 Mar 2023 13:32:37 -0700 (PDT) X-Google-Smtp-Source: AKy350bG5q89c2Y6Wlw7p2ve5pwxJYRmaH49Pq0IE0/4RpxLOA4w2DKnHACJ2UdpKKo4npmcQzpw X-Received: by 2002:a17:90b:390e:b0:23d:1aae:29e5 with SMTP id ob14-20020a17090b390e00b0023d1aae29e5mr31954333pjb.20.1680294757288; Fri, 31 Mar 2023 13:32:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680294757; cv=none; d=google.com; s=arc-20160816; b=jO6rm0HJDIpoM5gRQ7HFQD4+hzPc/cTYuAn6dybpFlcZt5H8Ux8XDBA5FsGOkpLtUR e8Gh1f8zAHiMPgalzaqOUjkS2PqiTIOg+ur8gKe79ZuT/rKSkHpirBOa+qpG2RrEfHjT rKtEnSOWXM7rZ5w1N4r8g83BCNYW6k1rFuAtrjmR36XHxz/YztxNNHS3jzuHDMOJnhdy b4MiMffMcweXnl4kX2Xwe0QxxrZpfA9+slgryVM0f1KDKpNXJtGtZXAEukUZkcy/oRXp ueY7HjjCDYtWYHyogIyZn9Z4NCWfN2XaarJOqQ/ua9A1un/ZsyvXi9/bADcZuGOlbJB/ foMA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from:sender :dkim-signature; bh=lEdFQ1e9O/nmgtLY4q6GhreBjz/AYmss+2MTbnjFDcU=; b=Eg+juoUC+UWpFcqTuO8J9DLiOoXqd+4/R1erHoP2OxUz/uwDCPaDcyT4yvv782hJnY 83ZwkNDwezRDSon9TjKEvtOodVw4uwF315m4VaZAG09cpEZ0hRa9wxaXLRCgi+rJOV0P ZFxYsYRaB0k6clyP5OzPAdMUYHHcRyOwEcuvym7ARVFb83ycTXSlxxmKJylGMYVFj0Rf 5OdivKuL7Yl9zt+Z8YgPLKnN/0uRhT1tm43PyTEHas7Ib5VhVMfJCI8Bq8S2W4H+W+o6 EQgNNBEPTx0fyCuUQX3OsuDS8wNmXiAT4KVNcg62Z99HF6Wtqh6hcNYCfaxYNdU+o5mc OZwg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=aycFzHBd; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m3-20020a632603000000b00513cd7f8ca7si182404pgm.345.2023.03.31.13.32.21; Fri, 31 Mar 2023 13:32:37 -0700 (PDT) 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=@gmail.com header.s=20210112 header.b=aycFzHBd; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233305AbjCaUa2 (ORCPT + 99 others); Fri, 31 Mar 2023 16:30:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45022 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233223AbjCaUaG (ORCPT ); Fri, 31 Mar 2023 16:30:06 -0400 Received: from mail-pj1-x102b.google.com (mail-pj1-x102b.google.com [IPv6:2607:f8b0:4864:20::102b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B2D8F24426; Fri, 31 Mar 2023 13:30:04 -0700 (PDT) Received: by mail-pj1-x102b.google.com with SMTP id h12-20020a17090aea8c00b0023d1311fab3so24637056pjz.1; Fri, 31 Mar 2023 13:30:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680294604; x=1682886604; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=lEdFQ1e9O/nmgtLY4q6GhreBjz/AYmss+2MTbnjFDcU=; b=aycFzHBdi0Sa6AEyDSKkTns8AJjhvaJozVBp/IYcun2wMMQmMwZ5oZV30btThiZEEl u7CKMhM0f0d+Ny+AChC4r87PKnCuWY0kOc2jFYfkT1k6KYP59BqYk9lJwOm0gnRgNE27 nKTh7PBOKHJ9PBBgJ3Wq7pFlH8oZVNn3N7NocMoe0PCRC+J6xZJCQpGavGDwQ+4AoQwq L9BurlyC52Lv6ZVtH3IcAo61KjsuvugZ6dhyXLkGe96R+gD5RluGFSlCRpwHF8eDMuLD ak16qZI2zV3i5ZXndX1om6aBL2O2qs3ol/HCgISa6g8s62HTkfWPeEfFPcRyKDFNyPOt aYzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680294604; x=1682886604; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=lEdFQ1e9O/nmgtLY4q6GhreBjz/AYmss+2MTbnjFDcU=; b=cKWgaty8hRLyQXdKl33J50BAhrEK2hadv92a7MsMhrAcV1k8ppO8A2fbz3YyflfJly ltcd/QI++U6DJn3VBSq/YUfw8EKnX0PQGefBjZTy/CAIAUyzcxhHAkT/juj8++qEeU8q 6SSj54+NcPhKnTIHmDHi1yAC0Twq+zCIpyxyBc0n7rnsyrVDA7dIbrcI+myf58MgdLuK nLpxoIgY9Le/6GR33xNh/AqMSOsNmM9tgTaSXPKs3NWRak/BpEHvNZr2+mdGS/tiYw+1 Ht1MgBVI8Mnx1UlANdeqlYU0uZEOcwbnAB4l0ALRgIfn06CHLY5t+YkE9yfppNWsSyWC PLnA== X-Gm-Message-State: AAQBX9dm0i28nJG76nR95o3hmLCoaC7EYpHppIqQr0QOcC27JRfZDEFD skncOjuUZRF+h+/g4X96oas= X-Received: by 2002:a17:902:e3c5:b0:19a:98c9:8cea with SMTP id r5-20020a170902e3c500b0019a98c98ceamr21379978ple.39.1680294603600; Fri, 31 Mar 2023 13:30:03 -0700 (PDT) Received: from bangji.hsd1.ca.comcast.net ([2601:647:6780:44b0:5cfb:ad2c:e6f5:d42f]) by smtp.gmail.com with ESMTPSA id t14-20020a1709028c8e00b001a1dc2be791sm1916803plo.259.2023.03.31.13.30.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 Mar 2023 13:30:03 -0700 (PDT) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Ian Rogers , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Kan Liang , Leo Yan Subject: [PATCH 8/9] perf pmu: Add perf_pmu__{open,scan}_file_at() Date: Fri, 31 Mar 2023 13:29:48 -0700 Message-Id: <20230331202949.810326-9-namhyung@kernel.org> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog In-Reply-To: <20230331202949.810326-1-namhyung@kernel.org> References: <20230331202949.810326-1-namhyung@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=0.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE, SPF_PASS autolearn=no 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?1761916755128123269?= X-GMAIL-MSGID: =?utf-8?q?1761916755128123269?= These two helpers will also use openat() to reduce the overhead with relative pathnames. Convert other functions in pmu_lookup() to use the new helpers. Signed-off-by: Namhyung Kim --- tools/perf/util/pmu.c | 57 ++++++++++++++++++++++++++++++++++--------- tools/perf/util/pmu.h | 6 ++++- 2 files changed, 50 insertions(+), 13 deletions(-) diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c index 0c1d87f10b23..78a407b42ad1 100644 --- a/tools/perf/util/pmu.c +++ b/tools/perf/util/pmu.c @@ -567,7 +567,7 @@ static void pmu_read_sysfs(void) * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64) * may have a "cpus" file. */ -static struct perf_cpu_map *pmu_cpumask(const char *name) +static struct perf_cpu_map *pmu_cpumask(int dirfd, const char *name) { struct perf_cpu_map *cpus; const char *templates[] = { @@ -582,10 +582,11 @@ static struct perf_cpu_map *pmu_cpumask(const char *name) strlcpy(pmu_name, name, sizeof(pmu_name)); for (template = templates; *template; template++) { - file = perf_pmu__open_file(&pmu, *template); + file = perf_pmu__open_file_at(&pmu, dirfd, *template); if (!file) continue; cpus = perf_cpu_map__read(file); + fclose(file); if (cpus) return cpus; } @@ -593,15 +594,19 @@ static struct perf_cpu_map *pmu_cpumask(const char *name) return NULL; } -static bool pmu_is_uncore(const char *name) +static bool pmu_is_uncore(int dirfd, const char *name) { - char path[PATH_MAX]; + int fd; if (perf_pmu__hybrid_mounted(name)) return false; - perf_pmu__pathname_scnprintf(path, sizeof(path), name, "cpumask"); - return file_available(path); + fd = perf_pmu__pathname_fd(dirfd, name, "cpumask", O_PATH); + if (fd < 0) + return false; + + close(fd); + return true; } static char *pmu_id(const char *name) @@ -853,11 +858,11 @@ pmu_find_alias_name(const char *name __maybe_unused) return NULL; } -static int pmu_max_precise(struct perf_pmu *pmu) +static int pmu_max_precise(int dirfd, struct perf_pmu *pmu) { int max_precise = -1; - perf_pmu__scan_file(pmu, "caps/max_precise", "%d", &max_precise); + perf_pmu__scan_file_at(pmu, dirfd, "caps/max_precise", "%d", &max_precise); return max_precise; } @@ -895,14 +900,14 @@ static struct perf_pmu *pmu_lookup(int dirfd, const char *lookup_name) if (!pmu) return NULL; - pmu->cpus = pmu_cpumask(name); + pmu->cpus = pmu_cpumask(dirfd, name); pmu->name = strdup(name); if (!pmu->name) goto err; /* Read type, and ensure that type value is successfully assigned (return 1) */ - if (perf_pmu__scan_file(pmu, "type", "%u", &type) != 1) + if (perf_pmu__scan_file_at(pmu, dirfd, "type", "%u", &type) != 1) goto err; alias_name = pmu_find_alias_name(name); @@ -913,10 +918,10 @@ static struct perf_pmu *pmu_lookup(int dirfd, const char *lookup_name) } pmu->type = type; - pmu->is_uncore = pmu_is_uncore(name); + pmu->is_uncore = pmu_is_uncore(dirfd, name); if (pmu->is_uncore) pmu->id = pmu_id(name); - pmu->max_precise = pmu_max_precise(pmu); + pmu->max_precise = pmu_max_precise(dirfd, pmu); pmu_add_cpu_aliases(&aliases, pmu); pmu_add_sys_aliases(&aliases, pmu); @@ -1730,6 +1735,17 @@ FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name) return fopen(path, "r"); } +FILE *perf_pmu__open_file_at(struct perf_pmu *pmu, int dirfd, const char *name) +{ + int fd; + + fd = perf_pmu__pathname_fd(dirfd, pmu->name, name, O_RDONLY); + if (fd < 0) + return NULL; + + return fdopen(fd, "r"); +} + int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt, ...) { @@ -1747,6 +1763,23 @@ int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt, return ret; } +int perf_pmu__scan_file_at(struct perf_pmu *pmu, int dirfd, const char *name, + const char *fmt, ...) +{ + va_list args; + FILE *file; + int ret = EOF; + + va_start(args, fmt); + file = perf_pmu__open_file_at(pmu, dirfd, name); + if (file) { + ret = vfscanf(file, fmt, args); + fclose(file); + } + va_end(args); + return ret; +} + bool perf_pmu__file_exists(struct perf_pmu *pmu, const char *name) { char path[PATH_MAX]; diff --git a/tools/perf/util/pmu.h b/tools/perf/util/pmu.h index 751c7016e7b6..32c3a75bca0e 100644 --- a/tools/perf/util/pmu.h +++ b/tools/perf/util/pmu.h @@ -220,7 +220,12 @@ bool is_pmu_core(const char *name); void print_pmu_events(const struct print_callbacks *print_cb, void *print_state); bool pmu_have_event(const char *pname, const char *name); +FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name); +FILE *perf_pmu__open_file_at(struct perf_pmu *pmu, int dirfd, const char *name); + int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt, ...) __scanf(3, 4); +int perf_pmu__scan_file_at(struct perf_pmu *pmu, int dirfd, const char *name, + const char *fmt, ...) __scanf(4, 5); bool perf_pmu__file_exists(struct perf_pmu *pmu, const char *name); @@ -259,7 +264,6 @@ int perf_pmu__pathname_scnprintf(char *buf, size_t size, const char *pmu_name, const char *filename); int perf_pmu__event_source_devices_fd(void); int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *filename, int flags); -FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name); void perf_pmu__destroy(void); From patchwork Fri Mar 31 20:29:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 77967 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp822494vqo; Fri, 31 Mar 2023 13:34:07 -0700 (PDT) X-Google-Smtp-Source: AK7set979WWRtAULLL25uL+2MOSrO35Z7ICxcNFCLHi1N162XoMrXGsyLKBaE4Y0PSAj2m2kpuN5 X-Received: by 2002:a05:6a20:4691:b0:d8:e59f:408c with SMTP id el17-20020a056a20469100b000d8e59f408cmr22915347pzb.33.1680294847696; Fri, 31 Mar 2023 13:34:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680294847; cv=none; d=google.com; s=arc-20160816; b=htJppkKc6vAfqOCKXOTA4Rtns/B8JtQZ3tFPeiikBoah0TT8LlN1kBdhCNtX9rA7qj tINCLTuNQZ/tOKtlg1z/1DMiBabLB1bBjRf6R15BL3H5hAi13qfo147bXtEZ5Q0Et8Ll pyrRTqvORWJlRgwHAAtHysfMb0imgRA+VaBNUYxcPP9S8B528/d2KepCyiRN9iBSDUSL PCWHzdvt0f5tj/Rqc7832CnuCuHDiAkzvKq7HQycHp8SV/6eA+OC3D+0DfL2wNU5JIjT njwmH2iK7m4mObtW+q/ms2VdT2CmpHTLTiigYeeyO+bJIFLH9YRhvrrHhZL1r6LTEY1I B/Gg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from:sender :dkim-signature; bh=VszdLpAL7WqW9IxkGHXAfoIlaDcuh12lplT2Sv91rgM=; b=y4ce8hniB+D+0qdOiy5J80Hft+QmiReU0P76YrTGaxBYG3BWQdjQ2xJrDTzU1LLrWh jEErU6hkcgaDBPe37HNrz77dBvIcSqkkK6GjOsVJyQQodEBgJRPIAokyOiRSJ4TPlJ1w FYSfclkXScDXJVdcch5/sSK4+3bszaAmX1GlmC5uI0KhLJTVCDiN14C/A37eyMWsqScr aOdMbuyNNEq3MRzw4UxeeV02MQmL7TcAJOzfT2XVZut3K+EO1kLAFbBmcGDJU84lDIZN N+Z5jatAY9VNHkbBmRg5fK85PaUGJImVQohVxGTZaJtHdOV6d9Lp5nMfP7HVwHkiGYZ/ dGLg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=fJTPcqNj; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q1-20020aa79601000000b00627eace679asi3244654pfg.8.2023.03.31.13.33.50; Fri, 31 Mar 2023 13:34:07 -0700 (PDT) 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=@gmail.com header.s=20210112 header.b=fJTPcqNj; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233333AbjCaUaj (ORCPT + 99 others); Fri, 31 Mar 2023 16:30:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46010 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233257AbjCaUaY (ORCPT ); Fri, 31 Mar 2023 16:30:24 -0400 Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 83D1C22928; Fri, 31 Mar 2023 13:30:05 -0700 (PDT) Received: by mail-pj1-x102e.google.com with SMTP id h12-20020a17090aea8c00b0023d1311fab3so24637135pjz.1; Fri, 31 Mar 2023 13:30:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680294605; x=1682886605; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=VszdLpAL7WqW9IxkGHXAfoIlaDcuh12lplT2Sv91rgM=; b=fJTPcqNjyxIKGGtqdvBiSEHrCMcmx8ZiItq9Z5trLCfiBkGu9Wm1Hbc18e7T+EJeiw LTe15kEhlp9pxxdNNl61QBW2DMakNHw307l9JIOqBuw9kje28RcSQ4i8qmiBF44zwioW OFRjZVuPkMuJBCPXgL1F2mT81a/nPDVnrfrCVPkfkOgyos8IaiBW4w4n3bU6R2WXkxgk 0dQFIDMek5uVuVONmkqeYpuSR3zGi7ziZngQOqB8p22myN0BgFQiekaWATJTKLHy+pqI 1s749Dlp50qjsCs0tVQJag9LNlhmeseuQd05FbxiIEvGMhoQMZ/+AeII2mGe2d3GS4GK 4d6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680294605; x=1682886605; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=VszdLpAL7WqW9IxkGHXAfoIlaDcuh12lplT2Sv91rgM=; b=aeN0HurK0b5OuiMiBJx7jptIpamnla58kYU6IVuWmhzgWyWD2/qEHKaJhQ8a/oHBo7 SGIwW+B/7KbghJbTpR2JIvHdL6rp07OqR4o9i88V0xKcP9VH0FZifo8I2S4hDm7QOWoM An9qjZg6zmJrw3N0SiMririv8Ju6XciLo4rLiQNAtdljXFdWI1ouri9uYF01gwKdwGnj PNUz6/eB7Q1uqAKS7q5Q+AOba9/9/dY9/Sh+jo7TjgHtCpG1iCqs6PsGZdmwJD5LHrLv MeLlkslFDGMF5DmQmpxe5IwIx0qBHmEs4SAAUXGt8Q518R5m6CG7Dub0GS1sKIM9o9Fm TSPQ== X-Gm-Message-State: AAQBX9fEAdRRlv+lxyk/0oNnkVWvsiwzuZ0/pNs/fPCceO02NAPhRW1v YAWfWCODRev0v/rmVmEyEE8= X-Received: by 2002:a17:903:32d0:b0:19c:dbce:dce8 with SMTP id i16-20020a17090332d000b0019cdbcedce8mr31103443plr.15.1680294605042; Fri, 31 Mar 2023 13:30:05 -0700 (PDT) Received: from bangji.hsd1.ca.comcast.net ([2601:647:6780:44b0:5cfb:ad2c:e6f5:d42f]) by smtp.gmail.com with ESMTPSA id t14-20020a1709028c8e00b001a1dc2be791sm1916803plo.259.2023.03.31.13.30.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 Mar 2023 13:30:04 -0700 (PDT) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Ian Rogers , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Kan Liang , Leo Yan Subject: [PATCH 9/9] perf intel-pt: Use perf_pmu__scan_file_at() if possible Date: Fri, 31 Mar 2023 13:29:49 -0700 Message-Id: <20230331202949.810326-10-namhyung@kernel.org> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog In-Reply-To: <20230331202949.810326-1-namhyung@kernel.org> References: <20230331202949.810326-1-namhyung@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=0.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE, SPF_PASS autolearn=no 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?1761916850019247147?= X-GMAIL-MSGID: =?utf-8?q?1761916850019247147?= Intel-PT calls perf_pmu__scan_file() a lot, let's use relative address when it accesses multiple files at one place. Signed-off-by: Namhyung Kim Acked-by: Adrian Hunter --- tools/perf/arch/x86/util/intel-pt.c | 52 ++++++++++++++++++----------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/tools/perf/arch/x86/util/intel-pt.c b/tools/perf/arch/x86/util/intel-pt.c index 1e39a034cee9..2cff11de9d8a 100644 --- a/tools/perf/arch/x86/util/intel-pt.c +++ b/tools/perf/arch/x86/util/intel-pt.c @@ -194,16 +194,19 @@ static u64 intel_pt_default_config(struct perf_pmu *intel_pt_pmu) int pos = 0; u64 config; char c; + int dirfd; + + dirfd = perf_pmu__event_source_devices_fd(); pos += scnprintf(buf + pos, sizeof(buf) - pos, "tsc"); - if (perf_pmu__scan_file(intel_pt_pmu, "caps/mtc", "%d", - &mtc) != 1) + if (perf_pmu__scan_file_at(intel_pt_pmu, dirfd, "caps/mtc", "%d", + &mtc) != 1) mtc = 1; if (mtc) { - if (perf_pmu__scan_file(intel_pt_pmu, "caps/mtc_periods", "%x", - &mtc_periods) != 1) + if (perf_pmu__scan_file_at(intel_pt_pmu, dirfd, "caps/mtc_periods", "%x", + &mtc_periods) != 1) mtc_periods = 0; if (mtc_periods) { mtc_period = intel_pt_pick_bit(mtc_periods, 3); @@ -212,13 +215,13 @@ static u64 intel_pt_default_config(struct perf_pmu *intel_pt_pmu) } } - if (perf_pmu__scan_file(intel_pt_pmu, "caps/psb_cyc", "%d", - &psb_cyc) != 1) + if (perf_pmu__scan_file_at(intel_pt_pmu, dirfd, "caps/psb_cyc", "%d", + &psb_cyc) != 1) psb_cyc = 1; if (psb_cyc && mtc_periods) { - if (perf_pmu__scan_file(intel_pt_pmu, "caps/psb_periods", "%x", - &psb_periods) != 1) + if (perf_pmu__scan_file_at(intel_pt_pmu, dirfd, "caps/psb_periods", "%x", + &psb_periods) != 1) psb_periods = 0; if (psb_periods) { psb_period = intel_pt_pick_bit(psb_periods, 3); @@ -227,8 +230,8 @@ static u64 intel_pt_default_config(struct perf_pmu *intel_pt_pmu) } } - if (perf_pmu__scan_file(intel_pt_pmu, "format/pt", "%c", &c) == 1 && - perf_pmu__scan_file(intel_pt_pmu, "format/branch", "%c", &c) == 1) + if (perf_pmu__scan_file_at(intel_pt_pmu, dirfd, "format/pt", "%c", &c) == 1 && + perf_pmu__scan_file_at(intel_pt_pmu, dirfd, "format/branch", "%c", &c) == 1) pos += scnprintf(buf + pos, sizeof(buf) - pos, ",pt,branch"); pr_debug2("%s default config: %s\n", intel_pt_pmu->name, buf); @@ -236,6 +239,7 @@ static u64 intel_pt_default_config(struct perf_pmu *intel_pt_pmu) intel_pt_parse_terms(intel_pt_pmu->name, &intel_pt_pmu->format, buf, &config); + close(dirfd); return config; } @@ -488,7 +492,7 @@ static void intel_pt_valid_str(char *str, size_t len, u64 valid) } } -static int intel_pt_val_config_term(struct perf_pmu *intel_pt_pmu, +static int intel_pt_val_config_term(struct perf_pmu *intel_pt_pmu, int dirfd, const char *caps, const char *name, const char *supported, u64 config) { @@ -498,11 +502,11 @@ static int intel_pt_val_config_term(struct perf_pmu *intel_pt_pmu, u64 bits; int ok; - if (perf_pmu__scan_file(intel_pt_pmu, caps, "%llx", &valid) != 1) + if (perf_pmu__scan_file_at(intel_pt_pmu, dirfd, caps, "%llx", &valid) != 1) valid = 0; if (supported && - perf_pmu__scan_file(intel_pt_pmu, supported, "%d", &ok) == 1 && !ok) + perf_pmu__scan_file_at(intel_pt_pmu, dirfd, supported, "%d", &ok) == 1 && !ok) valid = 0; valid |= 1; @@ -531,37 +535,45 @@ static int intel_pt_val_config_term(struct perf_pmu *intel_pt_pmu, static int intel_pt_validate_config(struct perf_pmu *intel_pt_pmu, struct evsel *evsel) { - int err; + int err, dirfd; char c; if (!evsel) return 0; + dirfd = perf_pmu__event_source_devices_fd(); + if (dirfd < 0) + return dirfd; + /* * If supported, force pass-through config term (pt=1) even if user * sets pt=0, which avoids senseless kernel errors. */ - if (perf_pmu__scan_file(intel_pt_pmu, "format/pt", "%c", &c) == 1 && + if (perf_pmu__scan_file_at(intel_pt_pmu, dirfd, "format/pt", "%c", &c) == 1 && !(evsel->core.attr.config & 1)) { pr_warning("pt=0 doesn't make sense, forcing pt=1\n"); evsel->core.attr.config |= 1; } - err = intel_pt_val_config_term(intel_pt_pmu, "caps/cycle_thresholds", + err = intel_pt_val_config_term(intel_pt_pmu, dirfd, "caps/cycle_thresholds", "cyc_thresh", "caps/psb_cyc", evsel->core.attr.config); if (err) - return err; + goto out; - err = intel_pt_val_config_term(intel_pt_pmu, "caps/mtc_periods", + err = intel_pt_val_config_term(intel_pt_pmu, dirfd, "caps/mtc_periods", "mtc_period", "caps/mtc", evsel->core.attr.config); if (err) - return err; + goto out; - return intel_pt_val_config_term(intel_pt_pmu, "caps/psb_periods", + err = intel_pt_val_config_term(intel_pt_pmu, dirfd, "caps/psb_periods", "psb_period", "caps/psb_cyc", evsel->core.attr.config); + +out: + close(dirfd); + return err; } static void intel_pt_config_sample_mode(struct perf_pmu *intel_pt_pmu,