From nobody Mon Apr 27 04:46:47 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 70BACC43334 for ; Thu, 16 Jun 2022 18:06:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1357234AbiFPSGd (ORCPT ); Thu, 16 Jun 2022 14:06:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41082 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236071AbiFPSG0 (ORCPT ); Thu, 16 Jun 2022 14:06:26 -0400 Received: from mail-pj1-x1032.google.com (mail-pj1-x1032.google.com [IPv6:2607:f8b0:4864:20::1032]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 05DCC49928 for ; Thu, 16 Jun 2022 11:06:26 -0700 (PDT) Received: by mail-pj1-x1032.google.com with SMTP id a10so2028530pju.3 for ; Thu, 16 Jun 2022 11:06:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=1mvSlf4cEf+F/1o/ks4xLLLQW9NOb2I7CDCyXMdcNxg=; b=IfGyaRQbkPh3+pqSOzZKZjQ2qu2v2gHs4JU7U4slIlor1Jp8wzpRx2duXPwu3e4Odz y6BkXNJpy8eeIB1n8FAAIHtc8FwRjGGAzyxJnvCxw3sjq3fD7NBaFahiDWRc2HzSyB25 Nk7U2c2cnCSV5NzpUhUIMgwhlk6kTNj+fn05GxmkM+QlJtDEUk/Ut0L7TPUDjPqkUk3j AEUPRiDhlOEL3xNIiAMC+jzatEpfd0d6+NAkBPQfEYrvKNdqQggNcghAnHqo4VIRM0WH rSfIdNqXcN0omKVCKlxsIX7OvyW+vpY1yyw7pUNtjtAwRujXX3hHmyWLyFSqEmXUSIlO YjYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :mime-version:content-transfer-encoding; bh=1mvSlf4cEf+F/1o/ks4xLLLQW9NOb2I7CDCyXMdcNxg=; b=hspvsH5enMrG5I0ocJPAgq846BkJtRX0usFxedpmXQc7mWMvb4858ABLL9/eZkxih7 uXLQIo0pzGPPjWRAMwrozOJMM6upSEnVLolPIbWy1S7PMj6jocxwuSwkzyYF5Hl1/UyI bTKh57zomz7Et3tdL04yapojFoKqeigLjcsDGQDDRZfzD4LGLz1jaZ9n2PF9DbsI8iqH yvDI20IXl2pgAbTO0UZi01d84E6rAjsnlMcbKMuUfcKdnioZzgkZrwSGmLZYJG93WeJj XHgzIhXwZcE0q/jKra7hNki5PsLmbQnORHNG+/3f92fgdUDceKQgYcCLYTYPLm48TjW+ Z+KA== X-Gm-Message-State: AJIora8Ps69bwY7AFRQspGkrCc3ZRMXgkO7xwcPIdaC3wgYuNHWzcjSb NxeKNH7WA9Hp5kGX9gi+CFE= X-Google-Smtp-Source: AGRyM1vy3blQ0TmnvYGNFsHnjFFKtM1A7f8C5LdHX81iXPYcTvOthiAQKyGcAfbe6NwOYtEt32e77A== X-Received: by 2002:a17:902:d903:b0:169:764:fca1 with SMTP id c3-20020a170902d90300b001690764fca1mr3652073plz.28.1655402785386; Thu, 16 Jun 2022 11:06:25 -0700 (PDT) Received: from balhae.corp.google.com ([2620:15c:2c1:200:2ccb:4745:1a90:629f]) by smtp.gmail.com with ESMTPSA id d80-20020a621d53000000b0050dc7628191sm2071649pfd.107.2022.06.16.11.06.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Jun 2022 11:06:24 -0700 (PDT) Sender: Namhyung Kim From: Namhyung Kim To: Peter Zijlstra Cc: Ingo Molnar , Arnaldo Carvalho de Melo , Jiri Olsa , Mark Rutland , Alexander Shishkin , LKML , Stephane Eranian , Andi Kleen , Ian Rogers , Michael Petlan , Jiri Olsa Subject: [PATCH v4] perf/core: Add a new read format to get a number of lost samples Date: Thu, 16 Jun 2022 11:06:23 -0700 Message-Id: <20220616180623.1358843-1-namhyung@kernel.org> X-Mailer: git-send-email 2.36.1.476.g0c4daa206d-goog MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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 --- * rebased to tip/perf/core 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 da759560eec5..ee8b9ecdc03b 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 d37629dbad72..0474ee362151 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 80782cddb1da..4d8c335a07db 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 fb35b926024c..726132039c38 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(); --=20 2.36.1.476.g0c4daa206d-goog