Received: by 2002:ab2:6816:0:b0:1f9:5764:f03e with SMTP id t22csp3082334lqo; Tue, 21 May 2024 06:30:55 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXHvk/f9te901PvD/Xx+jHlvGrZ7xCsuHA8SUKj6OxpPvUXUhUuhvWo50pLOZhjyiVZeVf9uQ3BuypecnbAHXdJbDTChQva8IFB2JThmg== X-Google-Smtp-Source: AGHT+IHQzTbUFEy+5SBwarx67dW83mGpfapkGsXV11yanlfaBQeLNEVBI/5VO6hh+nCiB/52XiY1 X-Received: by 2002:a05:6a00:4f86:b0:6f3:e6ac:1a3f with SMTP id d2e1a72fcca58-6f4e02ad970mr37667187b3a.11.1716298254826; Tue, 21 May 2024 06:30:54 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716298254; cv=pass; d=google.com; s=arc-20160816; b=JB297x/1d3ophCTY8ILa/7OpyEE7wd9o4o899Gqxj7xnwwQp/qvBKNNXF8hdB1AWGT YcqOFY7Hj4wvdr5Jhy0RWa9NbIvvZS5laJEeLPqZTV5yeSYyRwZCGTPDKnIHUitvbAJo S1AJMJ8qRgE3sjUntgq4Y88r21TJ8my9yvJnWaZdR5EXWq1ZCgoMVGqXtdfY+NRu/k9e ye4TotFylFOfezy8vuTbIMTAgR2Mwm05IC2kooAaW48igaJPlm+/zV37x0G5EmWbdT8S 6LiSMeLEHzYMP8NVSfTOpz1loL+MVk/ygqzBJ7jrU+k6muaReq8cWXTn8j6GGkVGuT/t bYCw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from; bh=7sfqGUqfxyrouM90N3NU+gtQyd9kriOrW0BtWillHxo=; fh=qmyFNe8t+IUXUkrxfdZt+VGwiwY4q6x1nzoI5t8Y/1U=; b=bTM6SU23nq0UlG6gIjnwrQ8MB47Xa7YpvkK/E90JBdpMJl+zThQY4mhGLTl66MxeoB JnpxzwtHmgNLgrCRFXSnK6zlIUpqLBJaWoo87bgAXCw/uGcnti0OLMle8cj+yvp9R4FF MBOXznR5bImtMF33e/vrJJ9aMMWAIsr/zuy1WznVBOVXJZag07YfDAzPn7sJ4ZPM/4tw vV6Svb6Gkv0eopa/e1BWktNX3BokS8AkpKrSvAU6lR+o7+3BK7yhdQErxH1TkPrDHZ+n yJvSF7sXCC1syzBGDD92M3B3Nu3E4b8XIvuE6FXCc8gdvgouCh5Z2g/ZMwRw0q7/tIuB M7lA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-185013-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-185013-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id d2e1a72fcca58-6f4d2a971f3si8558490b3a.138.2024.05.21.06.30.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 May 2024 06:30:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-185013-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-185013-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-185013-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 79488B21DE6 for ; Tue, 21 May 2024 13:30:48 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1F9E07EF02; Tue, 21 May 2024 13:30:40 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D3B4D5FBB1; Tue, 21 May 2024 13:30:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716298239; cv=none; b=dmu2GfB4rqGGhMlmZ/v4Xd2sQJ5CM6ZBFmxoCZ7pJQxb/p5g3Ch4YDLqSXHUADUxqppw4yCuxfv7uDeVwYlRuLApV1Brfe8CStv9n3c5vSm+7eeehRhoGfEy86JU2Oy62na41mcL7hEX7hY5mSjahVwuV9XED1weKoQXLMxTieg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716298239; c=relaxed/simple; bh=aOBxTb0SIctrnxguRhCJKdv1YkI8dBF3Vz8QHuFmXhI=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=CTvIt4liP9LSjQGbk23eJrkzecI6Aon7MF2IZdmuk7ksaV77S8bxEsa7dLJpFkkBc8hcYGmR63fMvj1oSfCUtnEP9sCsrHF79qxwFqAchovxlNlePP8YGwmmokOM3aPnDmEZhLVPijc8mDq49unlNgrDPUDiapayv4ljsdYGn9s= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 2F575DA7; Tue, 21 May 2024 06:31:01 -0700 (PDT) Received: from e126817.cambridge.arm.com (e126817.cambridge.arm.com [10.2.3.5]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 7108D3F641; Tue, 21 May 2024 06:30:35 -0700 (PDT) From: Ben Gainey To: peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org Cc: james.clark@arm.com, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Ben Gainey Subject: [PATCH v6 0/4] perf: Support PERF_SAMPLE_READ with inherit Date: Tue, 21 May 2024 14:30:25 +0100 Message-ID: <20240521133029.83654-1-ben.gainey@arm.com> X-Mailer: git-send-email 2.45.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit This change allows events to use PERF_SAMPLE READ with inherit so long as PERF_SAMPLE_TID is also set. Currently it is not possible to use PERF_SAMPLE_READ with inherit. This restriction assumes the user is interested in collecting aggregate statistics as per `perf stat`. It prevents a user from collecting per-thread samples using counter groups from a multi-threaded or multi-process application, as with `perf record -e '{....}:S'`. Instead users must use system-wide mode, or forgo the ability to sample counter groups. System-wide mode is often problematic as it requires specific permissions (no CAP_PERFMON / root access), or may lead to capture of significant amounts of extra data from other processes running on the system. This patch changes `perf_event_alloc` relaxing the restriction against combining `inherit` with `PERF_SAMPLE_READ` so that the combination will be allowed so long as `PERF_SAMPLE_TID` is enabled. It modifies sampling so that only the count associated with the active thread is recorded into the buffer. It modifies the context switch handling so that perf contexts are always switched out if they have this kind of event so that the correct per-thread state is maintained. Finally, the tools are updated to allow perf record to specify this combination and to correctly decode the sample data. In this configuration stream ids (such as may appear in the read_format field of a PERF_RECORD_SAMPLE) are no longer globally unique, rather the pair of (stream id, tid) uniquely identify each event. Tools that rely on this, for example to calculate a delta between samples, would need updating to take this into account. Previously valid event configurations (system-wide, no-inherit and so on) where each stream id is the identifier are unaffected. Changes since v5: - Rebase on v6.9 - Cleanup feedback from Namhyung Kim Changes since v4: - Rebase on v6.9-rc1 - Removed the dependency on inherit_stat that was previously assumed necessary as per feedback from Namhyung Kim. - Fixed an incorrect use of zfree instead of free in the tools leading to an abort on tool shutdown. - Additional test coverage improvements added to perf test. - Cleaned up the remaining bit of irrelevant change missed between v3 and v4. Changes since v3: - Cleaned up perf test data changes incorrectly included into this series from elsewhere. Changes since v2: - Rebase on v6.8 - Respond to James Clarke's feedback; fixup some typos and move some repeated checks into a helper macro. - Cleaned up checkpatch lints. - Updated perf test; fixed evsel handling so that existing tests pass and added new tests to cover the new behaviour. Changes since v1: - Rebase on v6.8-rc1 - Fixed value written into sample after child exists. - Modified handling of switch-out so that context with these events take the slow path, so that the per-event/per-thread PMU state is correctly switched. - Modified perf tools to support this mode of operation. Ben Gainey (4): perf: Support PERF_SAMPLE_READ with inherit tools/perf: Track where perf_sample_ids need per-thread periods tools/perf: Correctly calculate sample period for inherited SAMPLE_READ values tools/perf: Allow inherit + PERF_SAMPLE_READ when opening events include/linux/perf_event.h | 1 + kernel/events/core.c | 78 ++++++++++++++----- tools/lib/perf/evlist.c | 1 + tools/lib/perf/evsel.c | 48 ++++++++++++ tools/lib/perf/include/internal/evsel.h | 54 ++++++++++++- tools/perf/tests/attr/README | 2 + .../tests/attr/test-record-group-sampling | 39 ---------- .../tests/attr/test-record-group-sampling1 | 50 ++++++++++++ .../tests/attr/test-record-group-sampling2 | 60 ++++++++++++++ tools/perf/tests/attr/test-record-group2 | 9 ++- tools/perf/util/evsel.c | 19 ++++- tools/perf/util/evsel.h | 1 + tools/perf/util/session.c | 11 ++- 13 files changed, 302 insertions(+), 71 deletions(-) delete mode 100644 tools/perf/tests/attr/test-record-group-sampling create mode 100644 tools/perf/tests/attr/test-record-group-sampling1 create mode 100644 tools/perf/tests/attr/test-record-group-sampling2 -- 2.45.1