Received: by 2002:ac0:e350:0:0:0:0:0 with SMTP id g16csp2365651imn; Mon, 1 Aug 2022 23:14:27 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tUFLPG1LtaKZfWIFBxeU5MoLiaVMwD5lm4vNdLY6QJ/iUYSbOH2TxSuSUuL9ILLluHSMkv X-Received: by 2002:a63:5653:0:b0:419:ffb6:3ec6 with SMTP id g19-20020a635653000000b00419ffb63ec6mr15858877pgm.149.1659420866918; Mon, 01 Aug 2022 23:14:26 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1659420866; cv=pass; d=google.com; s=arc-20160816; b=hbIvEvPHz+t2ZVuMJ1jBiCihszi5jp4+w4fABT4qaOtmaIzUSiqtOyteyGvq07kV4S DUZjZvbE7d5SRq0HRS0MWUqgNrbKWmnwC5X+FEGc0EkmpsCK5WchkgLUIkkIdy6KyfZJ 2CgrH0r5wdRBud2F+BTo3z3KAI0wn4/1hhqcfaTVVcAs9hVSievYcozGQnYr7C8b4xHd LUYXXdpQUtchaI2sMjVu1awKbB6WJlO4E++P4e2NJNPIo/36RDpxuKFVFzvUdISgRC8O m7nEqTepGhr6WTkdZOPdhRzXZruujx5nIN3OZ4lkTKVcEx/qd7FHjxVRot/Q+B4+/xwT uUJw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:content-transfer-encoding :in-reply-to:content-language:references:cc:to:subject:from :user-agent:date:message-id:dkim-signature; bh=nc/34UIQVFBImIo5M8hbAFTVF/3Sdbxm5PKwkP4HICo=; b=Jv/vBRlAX0HVNWd3ffPPQzWuVeQCzvm+r/AYvHRhmshcbgsMQxNX4nqvG+28rVduiN yHmndi0Mj1wJ8B68zMWJ4FK4NDMHHizAzlXKbmDVO9S6Sxdbts8SAOQFqQFaGkoj2PWm eJwbiN/ZOMs/YLf0Ziaqu3UG0xoP/g8Hy6sDPJ9uQ0lqU89bCJKhRf6Vw2wIBWPGw7iz RWYIKunwr2vzH0DWhSZTY+vdl/3K82dSe01wXmZcmx92tCL8St5YkRci4iqZ+yzi3wtF dcwrvIHwVliw1FN4wkQugTRkjoijJ2p8ZUu9OcMS86y9F5ZwYz5YdkULi4sBPdzTxLAw tXmQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=fUHbyoW9; arc=pass (i=1 spf=pass spfdomain=amd.com dkim=pass dkdomain=amd.com dmarc=pass fromdomain=amd.com); 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a11-20020a170902eccb00b00166449db813si15562009plh.464.2022.08.01.23.14.12; Mon, 01 Aug 2022 23:14: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=@amd.com header.s=selector1 header.b=fUHbyoW9; arc=pass (i=1 spf=pass spfdomain=amd.com dkim=pass dkdomain=amd.com dmarc=pass fromdomain=amd.com); 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235114AbiHBGKy (ORCPT + 99 others); Tue, 2 Aug 2022 02:10:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43086 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232759AbiHBGKx (ORCPT ); Tue, 2 Aug 2022 02:10:53 -0400 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2046.outbound.protection.outlook.com [40.107.93.46]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 349A26321 for ; Mon, 1 Aug 2022 23:10:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Df7OOeRf2E5iLYYa4Axtn6GvHhL9IfuMO++shrFO5QQ1+lEP2XXGTm9wNWMKcTEbs+bkU42bWvcdp8AztlCcghI5qXNG3G8skRd2L1lcUCPW2q4+FlWueK6lQFsHvZGBnZ6UHkjoBi7HL3IT9v7zuitIHpxpCXQl521owWJXzwyqkUabuxPCJHKAqIzRVmKwNbY9GFhDhdiivo/T4KbMCegWQroEzZatg/ErcG8OENVL6HaJbSFmo3XZzbZrpV53LvBAmAuZ3d/Tn4IB6PgIJZRsCmEXUk5yaUMzmqEMyi2W4VE3KbCmizRS8/gNCkjDU2oDfWTpYNt5014THoDCaQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=nc/34UIQVFBImIo5M8hbAFTVF/3Sdbxm5PKwkP4HICo=; b=FWOjQXxK0JKbSvzrJWoWmhkf/GsM98ohDb8GNumQCV1qPTYiXK3Utr1smyp184zBHiPfmj3l8ra3j9uVKPYgLt7xtThQEUvBvCmxYaneipdJ50kUs1gXfubwJlhODVrrXuzoYOujAiluVelbJmRMTMv5vyvnHyIyVO5rUXOeKZvdkh2t4nMbhinIQTLV2FljHpZoqvOD4LzojhMGeiQtjmmXoq6iK0e4g8awMGNVXjOEh2vDGXC1WWPUg+GnobQ5DVS/PYr3/Ksfj1v29l0Ra7H0c6eXoQKJePTsuGd3pTdA+NAISAx49us152FN/7ELUIViRMyyC/O62lV9+9bGOQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=nc/34UIQVFBImIo5M8hbAFTVF/3Sdbxm5PKwkP4HICo=; b=fUHbyoW9AlSmk+rnKlgLM5zoywN4Kd0Z8uRXhBcIBOrjhkxpsO2oJ0q3N2PHr0hTKYXeLlCP89iWS1ri0kGAf++ArevOrIm8ulW3nsdezoPJSU1Vwj72QkRQlKGZQyBDRs3B0ugA5Z0ng6hEnzYAyJJ3gs1U/Ttl7yCsXUSo2v8= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=amd.com; Received: from PH7PR12MB6588.namprd12.prod.outlook.com (2603:10b6:510:210::10) by MN2PR12MB3293.namprd12.prod.outlook.com (2603:10b6:208:106::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5482.11; Tue, 2 Aug 2022 06:10:49 +0000 Received: from PH7PR12MB6588.namprd12.prod.outlook.com ([fe80::1c48:55fc:da99:87c9]) by PH7PR12MB6588.namprd12.prod.outlook.com ([fe80::1c48:55fc:da99:87c9%3]) with mapi id 15.20.5482.011; Tue, 2 Aug 2022 06:10:49 +0000 Message-ID: <35394cb7-a490-5aeb-b3a8-0f46e3c8ca28@amd.com> Date: Tue, 2 Aug 2022 11:40:34 +0530 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1 From: Ravi Bangoria Subject: Re: [RFC v2] perf: Rewrite core context handling To: Peter Zijlstra Cc: acme@kernel.org, alexander.shishkin@linux.intel.com, jolsa@redhat.com, namhyung@kernel.org, songliubraving@fb.com, eranian@google.com, alexey.budankov@linux.intel.com, ak@linux.intel.com, mark.rutland@arm.com, megha.dey@intel.com, frederic@kernel.org, maddy@linux.ibm.com, irogers@google.com, kim.phillips@amd.com, linux-kernel@vger.kernel.org, santosh.shukla@amd.com, ravi.bangoria@amd.com References: <20220113134743.1292-1-ravi.bangoria@amd.com> Content-Language: en-US In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-ClientProxiedBy: PN3PR01CA0072.INDPRD01.PROD.OUTLOOK.COM (2603:1096:c01:99::19) To PH7PR12MB6588.namprd12.prod.outlook.com (2603:10b6:510:210::10) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: c2c57023-ebb3-4a4a-b37c-08da744dbeef X-MS-TrafficTypeDiagnostic: MN2PR12MB3293:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: FWh7Tq7glQMkk6LpoaZfr8hFGLinslpsNNlimF7zVH7LEt5jkTnRwLpfgdw6la63bqkDssX8eCBBBm7QeY745/PSr9yqeQyN3vWGcxHWLfwARk5XdJtZK7pzhbpsS7r18mxPBD6Dvmgv/VY4Yvx6XUKPi3uZC2AxY5hoV/uKUzS2F3CYbbEwEJ3VLmV6Q5zhtc3rXO6KyHGVjk9c3JJKKl0mf7yhIlK+qKS3m0ukiXSlv+5uFq91lCavnCuQkDSD09aq6h4zpPrQdNxtdNoICvbtgdi5qYEs63lAqW0oMNXL1Uw0YrIWvmAWk/DibKD8Qm+lBwWCMw00SDpoBEbz6wtRMn0LjEZBhfDX3gKHa2puGCYUEXocKa3xaWR/KTeOPzxuQxbHagonjTOSloJAssZTHFj/wNqdnAuhDx0bhQng7ImnZsasmk8hsYYWF+0MVIc4OdCGN31BI3n494OguC/UOtwI6AFuFp3dux49l8MnbjFY9Ijy49ilzlSeCTgfDMFMBj2PAyNr39Bt5SgEPOJbiF3k4CF/obrcGSfM6MEZyClhvGQrACOYdhbOgjaLSRsPROa1aFFSg17bA3vMrqubVqsVClOnDHZ/BQQsfYT29ml0lrann+0HHCuC5kpUWesRnZoYWbiDtcyrBGOtJmSp7TcK62YckzeR+3nDar0XzSfmKFhFWXw2AG0m1XyLjg4yrhjQin53xI+XaeGTC2UDs9xpr01bbMfHXd+Q+wZf4xFnVh9aana0ttgOfsmIMSastZDZaDsp2WrgRneXMKGaduNY74IwnTyc6roKDeuhbbEp+jOkklW0vHnCZUDQqirDDMnjvbMSxqxEJW4drjlzVe7Du6fXWxQhqy/3CoAlccRK9jOF6RRZJ9kHdCjz X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:PH7PR12MB6588.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230016)(4636009)(376002)(136003)(39860400002)(396003)(346002)(366004)(53546011)(6512007)(26005)(6506007)(31696002)(6666004)(41300700001)(86362001)(6916009)(316002)(6486002)(966005)(478600001)(38100700002)(2616005)(186003)(83380400001)(2906002)(66946007)(66556008)(8676002)(4326008)(66476007)(8936002)(44832011)(31686004)(5660300002)(7416002)(36756003)(21314003)(45980500001)(43740500002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?YlJQWVVGTDBiRjhLQ2cxMlVKdGErcUVDTzl0TC9NQkI3UDVFN3FHZy8yU0hJ?= =?utf-8?B?d1owMEp2NTJlb0VpeTFvS0NwSUk2SitpL0pnMGtRYys0Sktzd0NsVkU5U3hw?= =?utf-8?B?UFUvZno3LzJCOXJBUXRjS2IwOFVJTHBBa0hDOFBZaWtxT0ZoU0FCVDU5Mmkx?= =?utf-8?B?NXcyMWhLVmdCaHY5V1ZNanRKQ1lIVW5ZTEF4dWhIaFVwOElRdUpkeXp6MkRV?= =?utf-8?B?L1lITDFYTnp0TUJSRC9NUFhZdmZ2c2ZKN3djVnp3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VzBhaDBFOXJsSVV5S2pSdllNbU0rS2xFZDM2UmFnMkx4S1M3?= =?utf-8?Q?sC/YT+oyor2xzBaNBaqO7ZQG4?= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: c2c57023-ebb3-4a4a-b37c-08da744dbeef X-MS-Exchange-CrossTenant-AuthSource: PH7PR12MB6588.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Aug 2022 06:10:49.4623 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: SFJXIi8fT1IsZckgxW3Y7SDhVOu5fElDdJDu2SGBNv93z7ggHZherlE9YTS8UDj0OowbWlgwRhc618d6fo3TQQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR12MB3293 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 13-Jun-22 8:25 PM, Peter Zijlstra wrote: > On Mon, Jun 13, 2022 at 04:35:11PM +0200, Peter Zijlstra wrote: >> @@ -12125,6 +12232,8 @@ SYSCALL_DEFINE5(perf_event_open, >> goto err_task; >> } >> >> + // XXX premature; what if this is allowed, but we get moved to a PMU >> + // that doesn't have this. >> if (is_sampling_event(event)) { >> if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) { >> err = -EOPNOTSUPP; > > No; this really should be against the event's native PMU. If the event > can't natively sample, it can't sample when placed in another group > either. Right. But IIUC, the question was, would there be any issue if we allow grouping of perf_sw_context sampling event as group leader and perf_{hw|invalid}_context counting event as group member. I think no. It should just work fine. And, there could be real usecases of it as you described in one old thread[1]. TL;DR Although I can't find any such pmu combination on AMD(not considering real sw pmus), I just tried opposite scenario: Group leader: msr/tsc/ as counting event (perf_sw_context) Group member: ibs_op/cnt_ctl=1/ as sampling event (perf_invalid_context) And a simple test program seems to work fine: #include #include #include #include #include #include #include #include #include #include #include #include #define PAGE_SIZE sysconf(_SC_PAGESIZE) #define PERF_MMAP_DATA_PAGES 256 #define PERF_MMAP_DATA_SIZE (PERF_MMAP_DATA_PAGES * PAGE_SIZE) #define PERF_MMAP_DATA_MASK (PERF_MMAP_DATA_SIZE - 1) #define PERF_MMAP_TOTAL_PAGES (PERF_MMAP_DATA_PAGES + 1) #define PERF_MMAP_TOTAL_SIZE (PERF_MMAP_TOTAL_PAGES * PAGE_SIZE) #define rmb() asm volatile("lfence":::"memory") struct perf_event { int fd; void *p; }; static int perf_event_open(struct perf_event_attr *attr, pid_t pid, int cpu, int group_fd, unsigned long flags) { int fd = syscall(__NR_perf_event_open, attr, pid, cpu, group_fd, flags); if (fd < 0) perror("perf_event_open() failed."); return fd; } static void *perf_event_mmap(int fd) { void *p = mmap(NULL, PERF_MMAP_TOTAL_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (p == MAP_FAILED) perror("mmap() failed."); return p; } static void copy_event_data(void *src, unsigned long offset, void *dest, size_t size) { size_t chunk1_size, chunk2_size; if ((offset + size) < PERF_MMAP_DATA_SIZE) { memcpy(dest, src + offset, size); } else { chunk1_size = PERF_MMAP_DATA_SIZE - offset; chunk2_size = size - chunk1_size; memcpy(dest, src + offset, chunk1_size); memcpy(dest + chunk1_size, src, chunk2_size); } } static int mmap_read(struct perf_event_mmap_page *p, void *dest, size_t size) { void *base; unsigned long data_tail, data_head; /* Casting to (void *) is needed. */ base = (void *)p + PAGE_SIZE; data_head = p->data_head; rmb(); data_tail = p->data_tail; if ((data_head - data_tail) < size) return -1; data_tail &= PERF_MMAP_DATA_MASK; copy_event_data(base, data_tail, dest, size); p->data_tail += size; return 0; } static void mmap_skip(struct perf_event_mmap_page *p, size_t size) { int data_head = p->data_head; rmb(); if ((p->data_tail + size) > data_head) p->data_tail = data_head; else p->data_tail += size; } static void perf_read_event_details(struct perf_event_mmap_page *p) { struct perf_event_header hdr; unsigned int pid, tid; /* * PERF_RECORD_SAMPLE: * struct { * struct perf_event_header hdr; * u32 pid; // PERF_SAMPLE_TID * u32 tid; // PERF_SAMPLE_TID * }; */ while(1) { if (mmap_read(p, &hdr, sizeof(hdr))) return; if (hdr.type == PERF_RECORD_SAMPLE) { if (mmap_read(p, &pid, sizeof(pid))) perror("Error reading pid."); if (mmap_read(p, &tid, sizeof(tid))) perror("Error reading tid."); printf("pid: %d, tid: %d\n", pid, tid); } else { mmap_skip(p, hdr.size - sizeof(hdr)); } } } int main(int argc, char *argv[]) { struct perf_event_attr attr; struct perf_event events[2]; int i; long long count1, count2; memset(&attr, 0, sizeof(struct perf_event_attr)); attr.size = sizeof(struct perf_event_attr); attr.type = 16; /* /sys/bus/event_source/devices/msr/type */ attr.config = 0x0; /* /sys/bus/event_source/devices/msr/events/tsc */ attr.disabled = 1; events[0].fd = perf_event_open(&attr, -1, 0, -1, 0); attr.type = 9; /* /sys/bus/event_source/devices/ibs_op/type */ attr.config = (0x1 << 19); /* /sys/bus/event_source/devices/ibs_op/format/cnt_ctl */ attr.disabled = 1; /* perf_read_event_details() can parse PERF_SAMPLE_TID only */ attr.sample_type = PERF_SAMPLE_TID; attr.sample_period = 10000000; events[1].fd = perf_event_open(&attr, -1, 0, events[0].fd, 0); events[1].p = perf_event_mmap(events[1].fd); ioctl(events[0].fd, PERF_EVENT_IOC_RESET, 0); ioctl(events[1].fd, PERF_EVENT_IOC_RESET, 0); ioctl(events[0].fd, PERF_EVENT_IOC_ENABLE, 0); ioctl(events[1].fd, PERF_EVENT_IOC_ENABLE, 0); i = 5; while(i--) { sleep(1); read(events[0].fd, &count1, sizeof(long long)); read(events[1].fd, &count2, sizeof(long long)); perf_read_event_details(events[1].p); ioctl(events[0].fd, PERF_EVENT_IOC_RESET, 0); ioctl(events[1].fd, PERF_EVENT_IOC_RESET, 0); printf("%lld, %lld\n", count1, count2); } close(events[1].fd); close(events[0].fd); } Example run: [term1~]$ taskset -c 0 top [term2~]$ pgrep top 85747 [term2~]$ sudo ./perf-group-sample-count 1996319080, 0 pid: 85747, tid: 85747 pid: 85747, tid: 85747 pid: 85747, tid: 85747 pid: 85747, tid: 85747 pid: 85747, tid: 85747 pid: 85747, tid: 85747 pid: 85747, tid: 85747 pid: 85747, tid: 85747 pid: 85747, tid: 85747 pid: 85747, tid: 85747 pid: 85747, tid: 85747 pid: 85747, tid: 85747 pid: 85747, tid: 85747 pid: 85747, tid: 85747 pid: 0, tid: 0 1996510960, 150000000 1996325400, 0 1996348600, 0 pid: 85747, tid: 85747 pid: 85747, tid: 85747 pid: 85747, tid: 85747 pid: 85747, tid: 85747 pid: 85747, tid: 85747 pid: 85747, tid: 85747 pid: 85747, tid: 85747 pid: 85747, tid: 85747 pid: 85747, tid: 85747 pid: 85747, tid: 85747 pid: 85747, tid: 85747 pid: 85747, tid: 85747 pid: 0, tid: 0 1996341420, 130000000 Thanks, Ravi [1] https://lore.kernel.org/all/20150204125954.GL21418@twins.programming.kicks-ass.net