From nobody Wed Apr 8 12:01:27 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 1CE25C32771 for ; Fri, 19 Aug 2022 17:52:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351407AbiHSRwT (ORCPT ); Fri, 19 Aug 2022 13:52:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60270 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351972AbiHSRvr (ORCPT ); Fri, 19 Aug 2022 13:51:47 -0400 Received: from mail-pf1-x42b.google.com (mail-pf1-x42b.google.com [IPv6:2607:f8b0:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CF77611C31; Fri, 19 Aug 2022 10:25:25 -0700 (PDT) Received: by mail-pf1-x42b.google.com with SMTP id h28so4865557pfq.11; Fri, 19 Aug 2022 10:25:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc; bh=dSpM4iLTLrET7xFCs7v5LyLdzEl5fofMVCKIs5uE6gc=; b=Xurqqa14dBlMPL/YF/6uPBgMV5lilln5OAktYAZF8vR/WEosZY/ohC5vAOsF3naRYa DElxlAN2p30qR8FfMuqvY7ILYy4ek56VKAJ+kM4h+b/wLSVHIs7d/C+e/0jMe2VqrDhA PbzF/AcP8mb+/EfiZZCY8bkTeuMUg6wyqr6/SXmUx4OfKl1GhEZbeBgN7+17wNypdX+x duRvn/U/I5/Z/aXr1YzWROMeLpE4roSnqI7uLNCkqriRsv6tWns/Aap3nnlSf1/rlcuF 1bciYmM0OFjsPAOQlifVfS/upEs6utU2bM90TQ5oqca9Ht+g2jMVN20WIk/eoAqQDbZe 2i+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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; bh=dSpM4iLTLrET7xFCs7v5LyLdzEl5fofMVCKIs5uE6gc=; b=4m6vgZ/hXv6dkysf/twcWTtH+hefpKrcSKF/ydOXmFoQe915jZ4JiHQyEnLIMivdEp TjGR//bjOE47ki2gOLio8e43BvpIEg/A63JAKrqyMk4yjpzXFuPTkBkmH+aPyZqs8QBF LE5XaodLiNdugE7SMXMRNjISsBNN7vSaRy7REn1SLUV9e5PxU16iHbrhuDezQ9v265Md veRr8ENq+eyDU7HyLbzPgaeHQF0oDxKDxYM5ZadYhnSH6HLOiNO0mM0aU9za+Xu3fuhb acDNFMnthqJYE885NrKEL4s6mi3Dty2F+Pt24FE7n0BQqSf7xAGq0ZNrYjiuO9yEVLjx 99kg== X-Gm-Message-State: ACgBeo3Tylv/kTBai7XyGXlRt1FvQpH5OyZ3Z1h8NPVO3sUYCpyVnwjY GUPtge83/NlR3jgnt4mBnZE= X-Google-Smtp-Source: AA6agR5NwCrxAqaqcR90TzeNSEODBYEwOJad8eu7REQtnpR9U/+XeP6JiCBFT4Ct4Y4NyqdXb28ugg== X-Received: by 2002:a63:85c6:0:b0:42a:39c8:a95c with SMTP id u189-20020a6385c6000000b0042a39c8a95cmr3866138pgd.306.1660929925166; Fri, 19 Aug 2022 10:25:25 -0700 (PDT) Received: from youngsil.svl.corp.google.com ([2620:15c:2d4:203:93f3:4394:cc21:adca]) by smtp.gmail.com with ESMTPSA id e13-20020a631e0d000000b0041d5001f0ecsm3056459pge.43.2022.08.19.10.25.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Aug 2022 10:25:24 -0700 (PDT) Sender: Namhyung Kim From: Namhyung Kim To: Jiri Olsa , Arnaldo Carvalho de Melo Cc: Peter Zijlstra , Ingo Molnar , LKML , Ian Rogers , linux-perf-users@vger.kernel.org Subject: [PATCH v3.1] perf tools: Support reading PERF_FORMAT_LOST Date: Fri, 19 Aug 2022 10:25:13 -0700 Message-Id: <20220819172513.2543494-1-namhyung@kernel.org> X-Mailer: git-send-email 2.37.1.595.g718a3a8f04-goog In-Reply-To: References: 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" The recent kernel added lost count can be read from either read(2) or ring buffer data with PERF_SAMPLE_READ. As it's a variable length data we need to access it according to the format info. But for perf tools use cases, PERF_FORMAT_ID is always set. So we can only check PERF_FORMAT_LOST bit to determine the data format. Add sample_read_value_size() and next_sample_read_value() helpers to make it a bit easier to access. Use them in all places where it reads the struct sample_read_value. Acked-by: Jiri Olsa Signed-off-by: Namhyung Kim --- * remove an unnecessary cast change tools/perf/tests/sample-parsing.c | 14 +++++--- tools/perf/util/event.h | 21 ++++++++++- tools/perf/util/evsel.c | 26 +++++++++----- .../scripting-engines/trace-event-python.c | 19 +++++++--- tools/perf/util/session.c | 35 +++++++++++-------- tools/perf/util/synthetic-events.c | 32 +++++++++++++---- 6 files changed, 107 insertions(+), 40 deletions(-) diff --git a/tools/perf/tests/sample-parsing.c b/tools/perf/tests/sample-pa= rsing.c index 07f2411b0ad4..20930dd48ee0 100644 --- a/tools/perf/tests/sample-parsing.c +++ b/tools/perf/tests/sample-parsing.c @@ -86,10 +86,15 @@ static bool samples_same(const struct perf_sample *s1, COMP(read.time_running); /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */ if (read_format & PERF_FORMAT_GROUP) { - for (i =3D 0; i < s1->read.group.nr; i++) - MCOMP(read.group.values[i]); + for (i =3D 0; i < s1->read.group.nr; i++) { + /* FIXME: check values without LOST */ + if (read_format & PERF_FORMAT_LOST) + MCOMP(read.group.values[i]); + } } else { COMP(read.one.id); + if (read_format & PERF_FORMAT_LOST) + COMP(read.one.lost); } } =20 @@ -263,7 +268,7 @@ static int do_test(u64 sample_type, u64 sample_regs, u6= 4 read_format) .data =3D (void *)aux_data, }, }; - struct sample_read_value values[] =3D {{1, 5}, {9, 3}, {2, 7}, {6, 4},}; + struct sample_read_value values[] =3D {{1, 5, 0}, {9, 3, 0}, {2, 7, 0}, {= 6, 4, 1},}; struct perf_sample sample_out, sample_out_endian; size_t i, sz, bufsz; int err, ret =3D -1; @@ -286,6 +291,7 @@ static int do_test(u64 sample_type, u64 sample_regs, u6= 4 read_format) } else { sample.read.one.value =3D 0x08789faeb786aa87ULL; sample.read.one.id =3D 99; + sample.read.one.lost =3D 1; } =20 sz =3D perf_event__sample_event_size(&sample, sample_type, read_format); @@ -370,7 +376,7 @@ static int do_test(u64 sample_type, u64 sample_regs, u6= 4 read_format) */ static int test__sample_parsing(struct test_suite *test __maybe_unused, in= t subtest __maybe_unused) { - const u64 rf[] =3D {4, 5, 6, 7, 12, 13, 14, 15}; + const u64 rf[] =3D {4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 28, 29, 30, 3= 1}; u64 sample_type; u64 sample_regs; size_t i; diff --git a/tools/perf/util/event.h b/tools/perf/util/event.h index a7b0931d5137..058d07923f04 100644 --- a/tools/perf/util/event.h +++ b/tools/perf/util/event.h @@ -65,7 +65,8 @@ struct stack_dump { =20 struct sample_read_value { u64 value; - u64 id; + u64 id; /* only if PERF_FORMAT_ID */ + u64 lost; /* only if PERF_FORMAT_LOST */ }; =20 struct sample_read { @@ -80,6 +81,24 @@ struct sample_read { }; }; =20 +static inline size_t sample_read_value_size(u64 read_format) +{ + /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */ + if (read_format & PERF_FORMAT_LOST) + return sizeof(struct sample_read_value); + else + return offsetof(struct sample_read_value, lost); +} + +static inline struct sample_read_value * +next_sample_read_value(struct sample_read_value *v, u64 read_format) +{ + return (void *)v + sample_read_value_size(read_format); +} + +#define sample_read_group__for_each(v, nr, rf) \ + for (int __i =3D 0; __i < (int)nr; v =3D next_sample_read_value(v, rf), _= _i++) + struct ip_callchain { u64 nr; u64 ips[]; diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index 4852089e1d79..16383df26166 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -1541,7 +1541,7 @@ static int evsel__read_one(struct evsel *evsel, int c= pu_map_idx, int thread) } =20 static void evsel__set_count(struct evsel *counter, int cpu_map_idx, int t= hread, - u64 val, u64 ena, u64 run) + u64 val, u64 ena, u64 run, u64 lost) { struct perf_counts_values *count; =20 @@ -1550,6 +1550,7 @@ static void evsel__set_count(struct evsel *counter, i= nt cpu_map_idx, int thread, count->val =3D val; count->ena =3D ena; count->run =3D run; + count->lost =3D lost; =20 perf_counts__set_loaded(counter->counts, cpu_map_idx, thread, true); } @@ -1558,7 +1559,7 @@ static int evsel__process_group_data(struct evsel *le= ader, int cpu_map_idx, int { u64 read_format =3D leader->core.attr.read_format; struct sample_read_value *v; - u64 nr, ena =3D 0, run =3D 0, i; + u64 nr, ena =3D 0, run =3D 0, lost =3D 0; =20 nr =3D *data++; =20 @@ -1573,16 +1574,17 @@ static int evsel__process_group_data(struct evsel *= leader, int cpu_map_idx, int =20 v =3D (struct sample_read_value *) data; =20 - evsel__set_count(leader, cpu_map_idx, thread, v[0].value, ena, run); - - for (i =3D 1; i < nr; i++) { + sample_read_group__for_each(v, nr, read_format) { struct evsel *counter; =20 - counter =3D evlist__id2evsel(leader->evlist, v[i].id); + counter =3D evlist__id2evsel(leader->evlist, v->id); if (!counter) return -EINVAL; =20 - evsel__set_count(counter, cpu_map_idx, thread, v[i].value, ena, run); + if (read_format & PERF_FORMAT_LOST) + lost =3D v->lost; + + evsel__set_count(counter, cpu_map_idx, thread, v->value, ena, run, lost); } =20 return 0; @@ -2475,8 +2477,8 @@ int evsel__parse_sample(struct evsel *evsel, union pe= rf_event *event, =20 if (data->read.group.nr > max_group_nr) return -EFAULT; - sz =3D data->read.group.nr * - sizeof(struct sample_read_value); + + sz =3D data->read.group.nr * sample_read_value_size(read_format); OVERFLOW_CHECK(array, sz, max_size); data->read.group.values =3D (struct sample_read_value *)array; @@ -2485,6 +2487,12 @@ int evsel__parse_sample(struct evsel *evsel, union p= erf_event *event, OVERFLOW_CHECK_u64(array); data->read.one.id =3D *array; array++; + + if (read_format & PERF_FORMAT_LOST) { + OVERFLOW_CHECK_u64(array); + data->read.one.lost =3D *array; + array++; + } } } =20 diff --git a/tools/perf/util/scripting-engines/trace-event-python.c b/tools= /perf/util/scripting-engines/trace-event-python.c index 9ef2406e0ede..1f2040f36d4e 100644 --- a/tools/perf/util/scripting-engines/trace-event-python.c +++ b/tools/perf/util/scripting-engines/trace-event-python.c @@ -642,15 +642,19 @@ static PyObject *python_process_brstacksym(struct per= f_sample *sample, return pylist; } =20 -static PyObject *get_sample_value_as_tuple(struct sample_read_value *value) +static PyObject *get_sample_value_as_tuple(struct sample_read_value *value, + u64 read_format) { PyObject *t; =20 - t =3D PyTuple_New(2); + t =3D PyTuple_New(3); if (!t) Py_FatalError("couldn't create Python tuple"); PyTuple_SetItem(t, 0, PyLong_FromUnsignedLongLong(value->id)); PyTuple_SetItem(t, 1, PyLong_FromUnsignedLongLong(value->value)); + if (read_format & PERF_FORMAT_LOST) + PyTuple_SetItem(t, 2, PyLong_FromUnsignedLongLong(value->lost)); + return t; } =20 @@ -681,12 +685,17 @@ static void set_sample_read_in_dict(PyObject *dict_sa= mple, Py_FatalError("couldn't create Python list"); =20 if (read_format & PERF_FORMAT_GROUP) { - for (i =3D 0; i < sample->read.group.nr; i++) { - PyObject *t =3D get_sample_value_as_tuple(&sample->read.group.values[i]= ); + struct sample_read_value *v =3D sample->read.group.values; + + i =3D 0; + sample_read_group__for_each(v, sample->read.group.nr, read_format) { + PyObject *t =3D get_sample_value_as_tuple(v, read_format); PyList_SET_ITEM(values, i, t); + i++; } } else { - PyObject *t =3D get_sample_value_as_tuple(&sample->read.one); + PyObject *t =3D get_sample_value_as_tuple(&sample->read.one, + read_format); PyList_SET_ITEM(values, 0, t); } pydict_set_item_string_decref(dict_sample, "values", values); diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c index 98e16659a149..40a907d95639 100644 --- a/tools/perf/util/session.c +++ b/tools/perf/util/session.c @@ -1283,21 +1283,25 @@ static void sample_read__printf(struct perf_sample = *sample, u64 read_format) sample->read.time_running); =20 if (read_format & PERF_FORMAT_GROUP) { - u64 i; + struct sample_read_value *value =3D sample->read.group.values; =20 printf(".... group nr %" PRIu64 "\n", sample->read.group.nr); =20 - for (i =3D 0; i < sample->read.group.nr; i++) { - struct sample_read_value *value; - - value =3D &sample->read.group.values[i]; + sample_read_group__for_each(value, sample->read.group.nr, read_format) { printf("..... id %016" PRIx64 - ", value %016" PRIx64 "\n", + ", value %016" PRIx64, value->id, value->value); + if (read_format & PERF_FORMAT_LOST) + printf(", lost %" PRIu64, value->lost); + printf("\n"); } - } else - printf("..... id %016" PRIx64 ", value %016" PRIx64 "\n", + } else { + printf("..... id %016" PRIx64 ", value %016" PRIx64, sample->read.one.id, sample->read.one.value); + if (read_format & PERF_FORMAT_LOST) + printf(", lost %" PRIu64, sample->read.one.lost); + printf("\n"); + } } =20 static void dump_event(struct evlist *evlist, union perf_event *event, @@ -1411,6 +1415,9 @@ static void dump_read(struct evsel *evsel, union perf= _event *event) =20 if (read_format & PERF_FORMAT_ID) printf("... id : %" PRI_lu64 "\n", read_event->id); + + if (read_format & PERF_FORMAT_LOST) + printf("... lost : %" PRI_lu64 "\n", read_event->lost); } =20 static struct machine *machines__find_for_cpumode(struct machines *machine= s, @@ -1479,14 +1486,14 @@ static int deliver_sample_group(struct evlist *evli= st, struct perf_tool *tool, union perf_event *event, struct perf_sample *sample, - struct machine *machine) + struct machine *machine, + u64 read_format) { int ret =3D -EINVAL; - u64 i; + struct sample_read_value *v =3D sample->read.group.values; =20 - for (i =3D 0; i < sample->read.group.nr; i++) { - ret =3D deliver_sample_value(evlist, tool, event, sample, - &sample->read.group.values[i], + sample_read_group__for_each(v, sample->read.group.nr, read_format) { + ret =3D deliver_sample_value(evlist, tool, event, sample, v, machine); if (ret) break; @@ -1510,7 +1517,7 @@ static int evlist__deliver_sample(struct evlist *evli= st, struct perf_tool *tool, /* For PERF_SAMPLE_READ we have either single or group mode. */ if (read_format & PERF_FORMAT_GROUP) return deliver_sample_group(evlist, tool, event, sample, - machine); + machine, read_format); else return deliver_sample_value(evlist, tool, event, sample, &sample->read.one, machine); diff --git a/tools/perf/util/synthetic-events.c b/tools/perf/util/synthetic= -events.c index 2ae59c03ae77..24df0d9755e4 100644 --- a/tools/perf/util/synthetic-events.c +++ b/tools/perf/util/synthetic-events.c @@ -1432,11 +1432,12 @@ size_t perf_event__sample_event_size(const struct p= erf_sample *sample, u64 type, result +=3D sizeof(u64); /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */ if (read_format & PERF_FORMAT_GROUP) { - sz =3D sample->read.group.nr * - sizeof(struct sample_read_value); - result +=3D sz; + sz =3D sample_read_value_size(read_format); + result +=3D sz * sample->read.group.nr; } else { result +=3D sizeof(u64); + if (read_format & PERF_FORMAT_LOST) + result +=3D sizeof(u64); } } =20 @@ -1521,6 +1522,20 @@ void __weak arch_perf_synthesize_sample_weight(const= struct perf_sample *data, *array =3D data->weight; } =20 +static __u64 *copy_read_group_values(__u64 *array, __u64 read_format, + const struct perf_sample *sample) +{ + size_t sz =3D sample_read_value_size(read_format); + struct sample_read_value *v =3D sample->read.group.values; + + sample_read_group__for_each(v, sample->read.group.nr, read_format) { + /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */ + memcpy(array, v, sz); + array =3D (void *)array + sz; + } + return array; +} + int perf_event__synthesize_sample(union perf_event *event, u64 type, u64 r= ead_format, const struct perf_sample *sample) { @@ -1602,13 +1617,16 @@ int perf_event__synthesize_sample(union perf_event = *event, u64 type, u64 read_fo =20 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */ if (read_format & PERF_FORMAT_GROUP) { - sz =3D sample->read.group.nr * - sizeof(struct sample_read_value); - memcpy(array, sample->read.group.values, sz); - array =3D (void *)array + sz; + array =3D copy_read_group_values(array, read_format, + sample); } else { *array =3D sample->read.one.id; array++; + + if (read_format & PERF_FORMAT_LOST) { + *array =3D sample->read.one.lost; + array++; + } } } =20 --=20 2.37.1.595.g718a3a8f04-goog