From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795231290; b=GQEJMIgpu5vgF5FUpNsMrjyB0OCyMRHkiz493mAjgt6CeZaFVN35Kk9yCf1OVOq7TQbp LWN6zIIaAHWix00Xx5tMBgjqbcAWtRHZoLnIy+SOfgLN1oaVGZ975OUFznGUlu6gcxwbK vnFYUPRFj+7hXKjLRRXJ6HCvLflOy6xq7nnxftJLCv/24RneKBNa1YMiu77QdaRBtuPWR v098LztR9ns0LtAVK6z0+A4+x6kq0VhhWD3UCDQBU94uD7zA93jyXz+JeO4MEk2/Lgq76 CTMjIg0rNoTaxNbHlk0kqqw+zYAnBlbnr7JFxVG4iwTZYDC0FpBsARK470Ha3Zb/tJg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795231290; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=55wLPBt9gzjsx4UzgpGFGR16PwS45f/a7NOgglhMcLk=; b=Rwa9iZbzXMjvb/BNd0qCpkObdCbAXQ38ljEoWhB/4hxCvzKo8nnTh+nRI5lsDH/bIPXy WuSY4ap30PfGevKhTsbQBHp5MlJZarjZsWdkCBqzewkLHFnhFlhIxgw68+amRGFVYxxJ2 9JzW7a8SGWMUIE3CI0ST7TnfEppOOLY5tOMdhb7DT6u1JpGU4U7ypQB/KsvXKt0xJCt0r Vxk/Jwx9JSD4o98+xTPq53PaJRMn7CuuFLLG8MGYnVT22TuU/eFizkca3HNCLjpkTUY9E QFJ9XNv2Sev7TgrK48HhQRUn1+eLcPHJYCIghm7i2TjKmkwhRWqARfwMSnn/aHJbh9Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=ErBIkzuS; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6D058606A680; Sat, 21 Nov 2026 03:21:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=ErBIkzuS Received: by smtp.kernel.org (Postfix) with ESMTPSA id 580101F000C0; Sat, 21 Nov 2026 03:21:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795231290; bh=55wLPBt9gzjsx4UzgpGFGR16PwS45f/a7NOgglhMcLk=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=ErBIkzuS9y2A/na2Bk47jY7Abk1m/3Au5ZdBW5JYBmBDKHvzNbT+v0gyprRd19ix2 tP5is0A3MSSrVcFOFKTsH3ouSCP8++xRF3t2x1ZbDfYyRRfCQXMHT09Gv9UqMgzLjP LdRmwtdJQxxdaQsqlclPXX7A58/1MOC6mBYsczEnJWqsQQxc5bD6xeK3xnTohWVn2G aheo8Y7RO3EB0Uv4zFnniPao9liDDOr6MgOKVK6EMaf4JZUGFqNcZFfrECCMo5ZdF8 2u4mo3g6pHcVW1biBRaIVbEesBPhN3KgbjD6yKb6CxA5UDmRr3bkEK54nTmBnkljVM pOzCbf+6FrB5Q== From: Arnaldo Carvalho de Melo To: Namhyung Kim Cc: Ingo Molnar , Thomas Gleixner , James Clark , Jiri Olsa , Ian Rogers , Adrian Hunter , Clark Williams , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Arnaldo Carvalho de Melo Subject: [PATCH v10 09/16] perf tests: Exercise the flexible array member path Date: Sat, 21 Nov 2026 00:20:45 -0300 Message-ID: <20261121032052.1392234-10-acme@kernel.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261121032052.1392234-1-acme@kernel.org> References: <20261121032052.1392234-1-acme@kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Arnaldo Carvalho de Melo No test covered find_flex_array() and get_flex_array_member(). Add a flex_array workload that keeps a struct with a trailing data[] behind a global pointer, and a test that checks the accesses past the struct size are resolved and named after the flexible array member; find_data_type() folds offsets past the struct modulo the element size back to the member's offset (+0x4 for char data[]). The test also checks that the accesses count for the struct in the percent, samples and period columns. It skips on 32-bit PIE builds, which reach static globals relative to the GOT base register. Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/tests/builtin-test.c | 1 + .../perf/tests/shell/data_type_flex_array.sh | 111 ++++++++++++++++++ tools/perf/tests/tests.h | 1 + tools/perf/tests/workloads/Build | 2 + tools/perf/tests/workloads/flex_array.c | 55 +++++++++ 5 files changed, 170 insertions(+) create mode 100755 tools/perf/tests/shell/data_type_flex_array.sh create mode 100644 tools/perf/tests/workloads/flex_array.c diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c index 42e85d1b1420f0a7..d761758201a4b2f2 100644 --- a/tools/perf/tests/builtin-test.c +++ b/tools/perf/tests/builtin-test.c @@ -215,6 +215,7 @@ static struct test_workload *workloads[] = { &workload__callchain, &workload__false_sharing, &workload__global_data, + &workload__flex_array, #ifdef HAVE_RUST_SUPPORT &workload__code_with_type, diff --git a/tools/perf/tests/shell/data_type_flex_array.sh b/tools/perf/tests/shell/data_type_flex_array.sh new file mode 100755 index 0000000000000000..6adbb556f0e48921 --- /dev/null +++ b/tools/perf/tests/shell/data_type_flex_array.sh @@ -0,0 +1,111 @@ +#!/bin/bash +# perf data type profiling tests for flexible array members +# SPDX-License-Identifier: GPL-2.0 + +set -e + +perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX) +perfout=$(mktemp /tmp/__perf_test.perf.out.XXXXX) +perferr=$(mktemp /tmp/__perf_test.perf.err.XXXXX) + +cleanup() +{ + rm -f "$perfdata" "$perfout" "$perferr" + trap - EXIT TERM INT +} + +trap_cleanup() +{ + echo "Unexpected signal in ${FUNCNAME[1]}" + cleanup + exit 1 +} +trap trap_cleanup EXIT TERM INT + +if ! perf check feature -q dwarf; then + echo "Skip: no DWARF (libdw) support" + cleanup + exit 2 +fi + +# 32-bit x86 PIE reaches static globals relative to the GOT base register, +# which the resolver does not track; e_type's low byte is at 16 on LE x86. +perf_bin=$(command -v perf) +if [ "$(od -An -tx1 -j4 -N1 "$perf_bin" | tr -d ' ')" = 01 ] && + [ "$(od -An -tx1 -j16 -N1 "$perf_bin" | tr -d ' ')" = 03 ]; then + echo "Skip: 32-bit PIE uses GOT-relative global accesses" + cleanup + exit 2 +fi + +# No point in recording if the PMU cannot do memory events. +if ! perf mem record -o /dev/null -- true 2>/dev/null; then + echo "Skip: cannot record memory events on this PMU" + cleanup + exit 2 +fi + +if ! perf mem record -o "$perfdata" -- perf test -w flex_array 1 \ + > /dev/null 2> "$perferr"; then + echo "FAIL: perf mem record" + cat "$perferr" + cleanup + exit 1 +fi + +if ! perf annotate --code-with-type -i "$perfdata" --stdio \ + --percent-limit 1 > "$perfout"; then + echo "FAIL: perf annotate" + cleanup + exit 1 +fi + +if ! grep -Fq "# data-type: struct flex +0x4 (data)" "$perfout"; then + echo "FAIL: flexible array member not resolved" + tail -n 30 "$perfout" + cleanup + exit 1 +fi + +# The accesses to the flexible array are resolved past the end of the +# struct: they must count for the struct, not only for the member. +if ! perf annotate --data-type -i "$perfdata" --stdio > "$perfout"; then + echo "FAIL: perf annotate --data-type" + cleanup + exit 1 +fi + +if ! awk ' + $1 + 0 == 100.00 && /struct flex/ { struct_ok = 1 } + $1 + 0 > 0 && /char\[\]/ && /data;/ { data_ok = 1 } + END { exit !(struct_ok && data_ok) } +' <(sed -n "/^Annotate type: 'struct flex' in/,/^Annotate type: /p" "$perfout"); then + echo "FAIL: --data-type does not count the flexible array accesses" + tail -n 30 "$perfout" + cleanup + exit 1 +fi + +# Same with the samples and period columns, the other two display modes. +for mode in --show-nr-samples --show-total-period; do + if ! perf annotate --data-type -i "$perfdata" --stdio "$mode" > "$perfout"; then + echo "FAIL: perf annotate --data-type $mode" + cleanup + exit 1 + fi + + if ! awk ' + $1 + 0 > 0 && /struct flex/ { total = $1 + 0 } + /int/ && /len;/ { len = $1 + 0 } + /char\[\]/ && /data;/ { data = $1 + 0 } + END { exit !(total > 0 && data > 0 && total == data + len) } + ' <(sed -n "/^Annotate type: 'struct flex' in/,/^Annotate type: /p" "$perfout"); then + echo "FAIL: --data-type $mode does not count the flexible array accesses" + tail -n 30 "$perfout" + cleanup + exit 1 + fi +done + +echo "flexible array member resolved and counted" +cleanup diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h index 63d49a960eab8f3c..8621ea345b5f06ff 100644 --- a/tools/perf/tests/tests.h +++ b/tools/perf/tests/tests.h @@ -275,6 +275,7 @@ DECLARE_WORKLOAD(deterministic); DECLARE_WORKLOAD(callchain); DECLARE_WORKLOAD(false_sharing); DECLARE_WORKLOAD(global_data); +DECLARE_WORKLOAD(flex_array); #ifdef HAVE_RUST_SUPPORT DECLARE_WORKLOAD(code_with_type); diff --git a/tools/perf/tests/workloads/Build b/tools/perf/tests/workloads/Build index 03017aaaaa0bec32..74a0cbe49dc93533 100644 --- a/tools/perf/tests/workloads/Build +++ b/tools/perf/tests/workloads/Build @@ -18,6 +18,7 @@ perf-test-y += deterministic.o perf-test-y += callchain.o perf-test-y += false_sharing.o perf-test-y += global_data.o +perf-test-y += flex_array.o ifeq ($(CONFIG_RUST_SUPPORT),y) perf-test-y += code_with_type.o @@ -37,3 +38,4 @@ CFLAGS_named_threads.o = -g -O0 -fno-inline -U_FORTIFY_SOURCE CFLAGS_callchain.o = -g -O0 -fno-inline -U_FORTIFY_SOURCE CFLAGS_false_sharing.o = -g -O0 -fno-inline -U_FORTIFY_SOURCE CFLAGS_global_data.o = -g -O0 -fno-inline -U_FORTIFY_SOURCE +CFLAGS_flex_array.o = -g -O0 -fno-inline -U_FORTIFY_SOURCE diff --git a/tools/perf/tests/workloads/flex_array.c b/tools/perf/tests/workloads/flex_array.c new file mode 100644 index 0000000000000000..141f9d510816cb05 --- /dev/null +++ b/tools/perf/tests/workloads/flex_array.c @@ -0,0 +1,55 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Flexible array member workload for data type profiling: the accesses to + * the array are past the end of the struct, where the resolver has to find + * the flexible array member. + * + * See tests/shell/data_type_flex_array.sh. + */ +#include +#include +#include +#include +#include "../tests.h" + +struct flex { + int len; + char data[]; +}; + +static struct flex *gp; +static volatile unsigned long flex_sink; +static volatile sig_atomic_t done; + +static void sighandler(int sig __maybe_unused) +{ + done = 1; +} + +static int flex_array(int argc, const char **argv) +{ + unsigned long sum = 0; + int sec = 1; + + if (argc > 0) + sec = atoi(argv[0]); + + gp = calloc(1, sizeof(*gp) + 4096); + if (gp == NULL) + return 1; + gp->len = 4096; + + signal(SIGINT, sighandler); + signal(SIGALRM, sighandler); + alarm(sec); + + while (!done) { + sum += gp->data[3]; + gp->data[5] = 1; + } + flex_sink = sum; + free(gp); + return 0; +} + +DEFINE_WORKLOAD(flex_array); -- 2.56.0