From nobody Sun Apr 19 18:49:09 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 05D75C433EF for ; Tue, 28 Jun 2022 07:16:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240438AbiF1HQi (ORCPT ); Tue, 28 Jun 2022 03:16:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48884 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238901AbiF1HQg (ORCPT ); Tue, 28 Jun 2022 03:16:36 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5253B2C648; Tue, 28 Jun 2022 00:16:35 -0700 (PDT) Date: Tue, 28 Jun 2022 07:16:32 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1656400593; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=UH6GiLvl6EPgMRDFC8w8CCwZJcIkbtjTfEHNzAI9Mmg=; b=ggYxpFGEAW/jHJdWoDJOod6bQQNpEFu5rDrgfXYCEJELT/vo+dcj5UhOAOfOgAj4ge71ox rHCgVHvd4QfXfbYrvh2umZKhFTZ8+AaH35v7y6zZTFvWd4eZBIHCVA++9sqJzdOA63Bv2w 9bn0Jw8D+KfqjElEYX5M46O5w7HHT1EgALt/xqZYgC7/zK5wIwt+g9haqUa4nRLP1qOV19 cNmgBgH/VGRI2S1aMu7IpTN16FcVwEi7AWPBiIpKYHO7Fu/hcVQ/tD/iNPzYrgm8l4uVDo 9tB+6b+9boe5Zg8qmw8FiNAYF6o+oo6SMeKW65yKO1/pN5pmSBaooEbO6HGNBQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1656400593; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=UH6GiLvl6EPgMRDFC8w8CCwZJcIkbtjTfEHNzAI9Mmg=; b=U5hrbqDJwd/Gzb2FDuHXZCeCD544bSgfkTiG18o33iIry/rJHBxuMiQ+thqz7xs/8b+gr9 0bRDNH+6+PPYqbAg== From: "tip-bot2 for Namhyung Kim" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: perf/core] perf/core: Add a new read format to get a number of lost samples Cc: Namhyung Kim , "Peter Zijlstra (Intel)" , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20220616180623.1358843-1-namhyung@kernel.org> References: <20220616180623.1358843-1-namhyung@kernel.org> MIME-Version: 1.0 Message-ID: <165640059260.4207.6165560170010032582.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The following commit has been merged into the perf/core branch of tip: Commit-ID: 119a784c81270eb88e573174ed2209225d646656 Gitweb: https://git.kernel.org/tip/119a784c81270eb88e573174ed2209225= d646656 Author: Namhyung Kim AuthorDate: Thu, 16 Jun 2022 11:06:23 -07:00 Committer: Peter Zijlstra CommitterDate: Tue, 28 Jun 2022 09:08:31 +02:00 perf/core: Add a new read format to get a number of lost samples Sometimes we want to know an accurate number of samples even if it's lost. Currenlty PERF_RECORD_LOST is generated for a ring-buffer which might be shared with other events. So it's hard to know per-event lost count. Add event->lost_samples field and PERF_FORMAT_LOST to retrieve it from userspace. Original-patch-by: Jiri Olsa Signed-off-by: Namhyung Kim Signed-off-by: Peter Zijlstra (Intel) Link: https://lkml.kernel.org/r/20220616180623.1358843-1-namhyung@kernel.org --- include/linux/perf_event.h | 2 ++ include/uapi/linux/perf_event.h | 5 ++++- kernel/events/core.c | 21 ++++++++++++++++++--- kernel/events/ring_buffer.c | 5 ++++- 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index da75956..ee8b9ec 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -759,6 +759,8 @@ struct perf_event { struct pid_namespace *ns; u64 id; =20 + atomic64_t lost_samples; + u64 (*clock)(void); perf_overflow_handler_t overflow_handler; void *overflow_handler_context; diff --git a/include/uapi/linux/perf_event.h b/include/uapi/linux/perf_even= t.h index d37629d..0474ee3 100644 --- a/include/uapi/linux/perf_event.h +++ b/include/uapi/linux/perf_event.h @@ -301,6 +301,7 @@ enum { * { u64 time_enabled; } && PERF_FORMAT_TOTAL_TIME_ENABLED * { u64 time_running; } && PERF_FORMAT_TOTAL_TIME_RUNNING * { u64 id; } && PERF_FORMAT_ID + * { u64 lost; } && PERF_FORMAT_LOST * } && !PERF_FORMAT_GROUP * * { u64 nr; @@ -308,6 +309,7 @@ enum { * { u64 time_running; } && PERF_FORMAT_TOTAL_TIME_RUNNING * { u64 value; * { u64 id; } && PERF_FORMAT_ID + * { u64 lost; } && PERF_FORMAT_LOST * } cntr[nr]; * } && PERF_FORMAT_GROUP * }; @@ -317,8 +319,9 @@ enum perf_event_read_format { PERF_FORMAT_TOTAL_TIME_RUNNING =3D 1U << 1, PERF_FORMAT_ID =3D 1U << 2, PERF_FORMAT_GROUP =3D 1U << 3, + PERF_FORMAT_LOST =3D 1U << 4, =20 - PERF_FORMAT_MAX =3D 1U << 4, /* non-ABI */ + PERF_FORMAT_MAX =3D 1U << 5, /* non-ABI */ }; =20 #define PERF_ATTR_SIZE_VER0 64 /* sizeof first published struct */ diff --git a/kernel/events/core.c b/kernel/events/core.c index 80782cd..4d8c335 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -1819,6 +1819,9 @@ static void __perf_event_read_size(struct perf_event = *event, int nr_siblings) if (event->attr.read_format & PERF_FORMAT_ID) entry +=3D sizeof(u64); =20 + if (event->attr.read_format & PERF_FORMAT_LOST) + entry +=3D sizeof(u64); + if (event->attr.read_format & PERF_FORMAT_GROUP) { nr +=3D nr_siblings; size +=3D sizeof(u64); @@ -5260,11 +5263,15 @@ static int __perf_read_group_add(struct perf_event = *leader, values[n++] +=3D perf_event_count(leader); if (read_format & PERF_FORMAT_ID) values[n++] =3D primary_event_id(leader); + if (read_format & PERF_FORMAT_LOST) + values[n++] =3D atomic64_read(&leader->lost_samples); =20 for_each_sibling_event(sub, leader) { values[n++] +=3D perf_event_count(sub); if (read_format & PERF_FORMAT_ID) values[n++] =3D primary_event_id(sub); + if (read_format & PERF_FORMAT_LOST) + values[n++] =3D atomic64_read(&sub->lost_samples); } =20 raw_spin_unlock_irqrestore(&ctx->lock, flags); @@ -5321,7 +5328,7 @@ static int perf_read_one(struct perf_event *event, u64 read_format, char __user *buf) { u64 enabled, running; - u64 values[4]; + u64 values[5]; int n =3D 0; =20 values[n++] =3D __perf_event_read_value(event, &enabled, &running); @@ -5331,6 +5338,8 @@ static int perf_read_one(struct perf_event *event, values[n++] =3D running; if (read_format & PERF_FORMAT_ID) values[n++] =3D primary_event_id(event); + if (read_format & PERF_FORMAT_LOST) + values[n++] =3D atomic64_read(&event->lost_samples); =20 if (copy_to_user(buf, values, n * sizeof(u64))) return -EFAULT; @@ -6858,7 +6867,7 @@ static void perf_output_read_one(struct perf_output_h= andle *handle, u64 enabled, u64 running) { u64 read_format =3D event->attr.read_format; - u64 values[4]; + u64 values[5]; int n =3D 0; =20 values[n++] =3D perf_event_count(event); @@ -6872,6 +6881,8 @@ static void perf_output_read_one(struct perf_output_h= andle *handle, } if (read_format & PERF_FORMAT_ID) values[n++] =3D primary_event_id(event); + if (read_format & PERF_FORMAT_LOST) + values[n++] =3D atomic64_read(&event->lost_samples); =20 __output_copy(handle, values, n * sizeof(u64)); } @@ -6882,7 +6893,7 @@ static void perf_output_read_group(struct perf_output= _handle *handle, { struct perf_event *leader =3D event->group_leader, *sub; u64 read_format =3D event->attr.read_format; - u64 values[5]; + u64 values[6]; int n =3D 0; =20 values[n++] =3D 1 + leader->nr_siblings; @@ -6900,6 +6911,8 @@ static void perf_output_read_group(struct perf_output= _handle *handle, values[n++] =3D perf_event_count(leader); if (read_format & PERF_FORMAT_ID) values[n++] =3D primary_event_id(leader); + if (read_format & PERF_FORMAT_LOST) + values[n++] =3D atomic64_read(&leader->lost_samples); =20 __output_copy(handle, values, n * sizeof(u64)); =20 @@ -6913,6 +6926,8 @@ static void perf_output_read_group(struct perf_output= _handle *handle, values[n++] =3D perf_event_count(sub); if (read_format & PERF_FORMAT_ID) values[n++] =3D primary_event_id(sub); + if (read_format & PERF_FORMAT_LOST) + values[n++] =3D atomic64_read(&sub->lost_samples); =20 __output_copy(handle, values, n * sizeof(u64)); } diff --git a/kernel/events/ring_buffer.c b/kernel/events/ring_buffer.c index fb35b92..7261320 100644 --- a/kernel/events/ring_buffer.c +++ b/kernel/events/ring_buffer.c @@ -172,8 +172,10 @@ __perf_output_begin(struct perf_output_handle *handle, goto out; =20 if (unlikely(rb->paused)) { - if (rb->nr_pages) + if (rb->nr_pages) { local_inc(&rb->lost); + atomic64_inc(&event->lost_samples); + } goto out; } =20 @@ -254,6 +256,7 @@ __perf_output_begin(struct perf_output_handle *handle, =20 fail: local_inc(&rb->lost); + atomic64_inc(&event->lost_samples); perf_output_put_handle(handle); out: rcu_read_unlock();