All of lore.kernel.org
 help / color / mirror / Atom feed
From: Arnaldo Carvalho de Melo <acme@kernel.org>
To: Willy Tarreau <w@1wt.eu>
Cc: dwarves@vger.kernel.org, Alan Maguire <alan.maguire@oracle.com>,
	Jiri Olsa <jolsa@kernel.org>,
	Clark Williams <williams@redhat.com>,
	Kate Carcia <kcarcia@redhat.com>,
	Arnaldo Carvalho de Melo <acme@redhat.com>,
	"Gustavo A. R. Silva" <gustavoars@kernel.org>
Subject: [PATCH 3/3] tests: Add a test for the accounting of flexible arrays
Date: Tue,  8 Oct 2024 16:52:09 -0300	[thread overview]
Message-ID: <20241008195209.1094299-4-acme@kernel.org> (raw)
In-Reply-To: <20241008195209.1094299-1-acme@kernel.org>

From: Arnaldo Carvalho de Melo <acme@redhat.com>

  acme@x1:~/git/pahole$ tests/flexible_arrays.sh
  Flexible arrays accounting: Ok
  acme@x1:~/git/pahole$ VERBOSE=1 tests/flexible_arrays.sh
  Flexible arrays accounting: end: mem_cgroup: 2 2
  middle: mem_cgroup: 3 3
  end: pglist_data: 2 2
  middle: pglist_data: 0 0
  end: zone: 3 3
  middle: zone: 0 0
  end: cgroup: 1 1
  middle: cgroup: 0 0
  end: cgroup_root: 1 1
  middle: cgroup_root: 1 1
  end: page_counter: 2 2
  middle: page_counter: 0 0
  end: cpu_hw_events: 1 1
  middle: cpu_hw_events: 0 0
  end: crypto_shash: 1 1
  middle: crypto_shash: 0 0
  end: crypto_aead: 1 1
  middle: crypto_aead: 0 0
  end: crypto_ahash: 1 1
  middle: crypto_ahash: 0 0
  end: crypto_skcipher: 1 1
  middle: crypto_skcipher: 0 0
  end: crypto_sync_skcipher: 0 0
  middle: crypto_sync_skcipher: 1 1
  <SNIP>
  end: bpf_ctx_convert: 2 2
  middle: bpf_ctx_convert: 0 0
  Ok
  acme@x1:~/git/pahole$

Cc: "Gustavo A. R. Silva" <gustavoars@kernel.org>
Cc: Willy Tarreau <w@1wt.eu>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tests/flexible_arrays.sh | 77 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 77 insertions(+)
 create mode 100755 tests/flexible_arrays.sh

diff --git a/tests/flexible_arrays.sh b/tests/flexible_arrays.sh
new file mode 100755
index 0000000000000000..7c21253a7e08c9d3
--- /dev/null
+++ b/tests/flexible_arrays.sh
@@ -0,0 +1,77 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# Match flexible array member info with the per struct final stats.
+#
+# Arnaldo Carvalho de Melo <acme@redhat.com> (C) 2024-
+
+vmlinux=${vmlinux:-$1}
+
+if [ -z "$vmlinux" ] ; then
+	vmlinux=$(pahole --running_kernel_vmlinux)
+fi
+
+if [ ! -f "$vmlinux" ] ; then
+	echo "$vmlinux file not available, please specify another"
+	exit 2
+fi
+
+pretty=$(mktemp /tmp/flexible_arrays.data.sh.XXXXXX.c)
+
+echo -n "Flexible arrays accounting: "
+
+for struct in $(pahole -F btf --sizes --with_embedded_flexible_array $vmlinux | cut -f1) ; do
+	pahole $struct > $pretty
+
+	# We need to check for just one tab before the comment as when expanding unnamed
+	# structs with members with flexible arrays inside another struct we would mess
+	# up the accounting, see 'pahole fanotify_fid_event' for instance, circa October 2024:
+	# $ pahole fanotify_fid_event
+	# struct fanotify_fid_event {
+	#	struct fanotify_event      fae;                  /*     0    48 */
+	#	__kernel_fsid_t            fsid;                 /*    48     8 */
+	#	struct {
+	#		struct fanotify_fh object_fh;            /*    56     4 */
+	#		/* XXX last struct has a flexible array */
+	#		unsigned char      _inline_fh_buf[12];   /*    60    12 */
+	#	};                                               /*    56    16 */
+
+	#	/* XXX last struct has embedded flexible array(s) */
+	#	/* size: 72, cachelines: 2, members: 3 */
+	#	/* flexible array members: middle: 1 */
+	#	/* last cacheline: 8 bytes */
+	# }
+
+	nr_flexible_arrays=$(grep $'^\t/\* XXX last struct has a flexible array' $pretty | wc -l)
+	nr_embedded_flexible_arrays=$(grep $'^\t/\* XXX last struct.*embedded flexible array' $pretty | wc -l)
+	stat_nr_flexible_arrays=$(grep "flexible array members:.*end:" $pretty | sed -r 's/.*end: *([[:digit:]]+).*/\1/g')
+	[ -z "$stat_nr_flexible_arrays" ] && stat_nr_flexible_arrays=0
+	stat_nr_embedded_flexible_arrays=$(grep "flexible array members:.*middle:" $pretty | sed -r 's/.*middle: *([[:digit:]]+).*/\1/g')
+	[ -z "$stat_nr_embedded_flexible_arrays" ] && stat_nr_embedded_flexible_arrays=0
+	test -n "$VERBOSE" && echo "end: $struct: $nr_flexible_arrays $stat_nr_flexible_arrays"
+	test -n "$VERBOSE" && echo "middle: $struct: $nr_embedded_flexible_arrays $stat_nr_embedded_flexible_arrays"
+
+	if [ "$nr_embedded_flexible_arrays" != "$stat_nr_embedded_flexible_arrays" ] ; then
+		test -n "$VERBOSE" && printf "struct %s: The number of embedded flexible arrays (%s) doesn't match the number of members marked as such (%s)\n" \
+			"$struct" "$stat_nr_embedded_flexible_arrays" "$nr_embedded_flexible_arrays"
+		test -n "$VERBOSE" && pahole $struct
+		FAILED=1
+	fi
+
+	if [ "$nr_flexible_arrays" != "$stat_nr_flexible_arrays" ] ; then
+		test -n "$VERBOSE" && printf "struct %s: The number of flexible arrays (%s) doesn't match the number of members marked as such (%s)\n" \
+			"$struct" "$stat_nr_flexible_arrays" "$nr_flexible_arrays"
+		test -n "$VERBOSE" && pahole $struct
+		FAILED=1
+	fi
+
+	rm -f $pretty
+done
+
+if [ -n "$FAILED" ] ; then
+	echo "FAILED"
+	exit 1
+fi
+
+echo "Ok"
+exit 0
-- 
2.46.0


  parent reply	other threads:[~2024-10-08 19:52 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-08 19:52 [PATCH 0/3] Differentiate embedded flexible arrays from flexible ones Arnaldo Carvalho de Melo
2024-10-08 19:52 ` [PATCH 1/3] fprintf: Differentiate embedded flexible arrays from flexible arrays Arnaldo Carvalho de Melo
2024-10-08 19:52 ` [PATCH 2/3] fprintf: Show statistics about members with " Arnaldo Carvalho de Melo
2024-10-08 19:52 ` Arnaldo Carvalho de Melo [this message]
2024-10-13  9:51 ` [PATCH 0/3] Differentiate embedded flexible arrays from flexible ones Willy Tarreau

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241008195209.1094299-4-acme@kernel.org \
    --to=acme@kernel.org \
    --cc=acme@redhat.com \
    --cc=alan.maguire@oracle.com \
    --cc=dwarves@vger.kernel.org \
    --cc=gustavoars@kernel.org \
    --cc=jolsa@kernel.org \
    --cc=kcarcia@redhat.com \
    --cc=w@1wt.eu \
    --cc=williams@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.