Skip to contentSkip to conversation

Test annotate with data type profiling

RFC PATCH Applied Dmitrii Dolgov 6 participants 27 messages Feb 8 – Apr 14

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover Test annotate with data type profiling

Add shell tests for data type profiling, including C and rust.

To support the rust test, a new workload with rust code was introduced. To build it only when rust is actually available, also add a feature test for rust compiler and guard everything behind it.

Changes in v2:
- Switched to use mem record.
- Fixed the build issue with a custom output.
- Separated patches for the workload and tests.
- Added new test for C code.
- Added a feature test for rust compiler.

Dmitrii Dolgov (4):
tools/build: Add a feature test for rust compiler perf test workload: Add code_with_type test workload perf tests: Test annotate with data type profiling and rust perf tests: Test annotate with data type profiling and C

 tools/build/Makefile.build                    | 14 ++++
 tools/build/Makefile.feature                  |  6 +-
 tools/build/feature/Makefile                  |  7 ++
 tools/build/feature/test-rust.rs              |  4 +
 tools/perf/Makefile.config                    | 11 +++
 tools/perf/Makefile.perf                      |  2 +-
 tools/perf/builtin-check.c                    |  1 +
 tools/perf/tests/builtin-test.c               |  4 +
 tools/perf/tests/shell/data_type_profiling.sh | 84 +++++++++++++++++++
 tools/perf/tests/tests.h                      |  4 +
 tools/perf/tests/workloads/Build              |  5 ++
 tools/perf/tests/workloads/code_with_type.c   | 46 ++++++++++
 tools/perf/tests/workloads/code_with_type.rs  | 23 +++++
 tools/scripts/Makefile.include                |  2 +
 14 files changed, 210 insertions(+), 3 deletions(-)
 create mode 100644 tools/build/feature/test-rust.rs
 create mode 100755 tools/perf/tests/shell/data_type_profiling.sh
 create mode 100644 tools/perf/tests/workloads/code_with_type.c
 create mode 100644 tools/perf/tests/workloads/code_with_type.rs

base-commit: 36a1b0061a584430277861fe5d8bd107aef26137

Signature · 1 line
2.52.0
#1 lore raw

RFC PATCH v2 1/4 tools/build: Add a feature test for rust compiler

Add a feature test to identify if the rust compiler is available, so that perf could build rust based worloads based on that.

---
 tools/build/Makefile.feature     |  6 ++++--
 tools/build/feature/Makefile     |  7 +++++++
 tools/build/feature/test-rust.rs |  4 ++++
 tools/perf/Makefile.config       | 11 +++++++++++
 tools/perf/builtin-check.c       |  1 +
 5 files changed, 27 insertions(+), 2 deletions(-)
 create mode 100644 tools/build/feature/test-rust.rs
tools/build/Makefile.feature +4 -2
index 7f119eafc7c..64d21152fc8 100644
@@ -100,7 +100,8 @@ FEATURE_TESTS_BASIC :=                  \
         disassembler-four-args		\
         disassembler-init-styled	\
         file-handle			\
-        libopenssl
+        libopenssl			\
+        rust
 
 # FEATURE_TESTS_BASIC + FEATURE_TESTS_EXTRA is the complete list
 # of all feature tests
@@ -149,7 +150,8 @@ FEATURE_DISPLAY ?=              \
          bpf			\
          libaio			\
          libzstd		\
-         libopenssl
+         libopenssl		\
+         rust
 
 #
 # Declare group members of a feature to display the logical OR of the detection
tools/build/feature/Makefile +7 -0
index 5c15572d505..9ae69d85716 100644
@@ -112,6 +112,9 @@ __BUILD = $(CC) $(CFLAGS) -MD -Wall -Werror -o $@ $(patsubst %.bin,%.c,$(@F)) $(
 __BUILDXX = $(CXX) $(CXXFLAGS) -MD -Wall -Werror -o $@ $(patsubst %.bin,%.cpp,$(@F)) $(LDFLAGS)
   BUILDXX = $(__BUILDXX) > $(@:.bin=.make.output) 2>&1
 
+__BUILDRS = $(RUSTC) $(RUSTC_FLAGS) -o $@ $(patsubst %.bin,%.rs,$(@F))
+  BUILDRS = $(__BUILDRS) > $(@:.bin=.make.output) 2>&1
+
 ###############################
 
 $(OUTPUT)test-all.bin:
@@ -388,6 +391,10 @@ $(OUTPUT)test-libopenssl.bin:
 $(OUTPUT)test-bpftool-skeletons.bin:
 	$(SYSTEM_BPFTOOL) version | grep '^features:.*skeletons' \
 		> $(@:.bin=.make.output) 2>&1
+
+$(OUTPUT)test-rust.bin:
+	$(BUILDRS) > $(@:.bin=.make.output) 2>&1
+
 ###############################
 
 clean:
tools/build/feature/test-rust.rs new file +4 -0
new file mode 100644
index 00000000000..f2fc91cc4f6
@@ -0,0 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0
+fn main() {
+    println!("hi")
+}
tools/perf/Makefile.config +11 -0
index b683aab3ab9..94aecfe38b9 100644
@@ -1153,6 +1153,17 @@ ifneq ($(NO_LIBTRACEEVENT),1)
   endif
 endif
 
+ifndef NO_RUST
+  ifneq ($(feature-rust), 1)
+    $(warning Rust is not found. Test workloads with rust are disabled.)
+    NO_RUST := 1
+  else
+    NO_RUST := 0
+    CFLAGS += -DHAVE_RUST_SUPPORT
+    $(call detected,CONFIG_RUST_SUPPORT)
+  endif
+endif
+
 # Among the variables below, these:
 #   perfexecdir
 #   libbpf_include_dir
tools/perf/builtin-check.c +1 -0
index d19769a8f68..27a41beeadd 100644
@@ -60,6 +60,7 @@ struct feature_status supported_features[] = {
 	FEATURE_STATUS("numa_num_possible_cpus", HAVE_LIBNUMA_SUPPORT),
 	FEATURE_STATUS("zlib", HAVE_ZLIB_SUPPORT),
 	FEATURE_STATUS("zstd", HAVE_ZSTD_SUPPORT),
+	FEATURE_STATUS("rust", HAVE_RUST_SUPPORT),
 
 	/* this should remain at end, to know the array end */
 	FEATURE_STATUS(NULL, _)
Signature · 1 line
2.52.0
#2 lore raw

RFC PATCH v2 2/4 perf test workload: Add code_with_type test workload

In reply to Dmitrii Dolgov

The purpose of the workload is to gather samples of rust runtime. To achiever that it has a dummy rust library linked with it. Per recommendations for such scenarios [1], the rust library is statically linked.

An example:

$ perf record perf test -w code_with_type
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.160 MB perf.data (4074 samples) ]
$ perf report --stdio --dso perf -s srcfile,srcline
    45.16%  ub_checks.rs       ub_checks.rs:72
     6.72%  code_with_type.rs  code_with_type.rs:15
     6.64%  range.rs           range.rs:767
     4.26%  code_with_type.rs  code_with_type.rs:21
     4.23%  range.rs           range.rs:0
     3.99%  code_with_type.rs  code_with_type.rs:16
    [...]

[1]: https://doc.rust-lang.org/reference/linkage.html#mixed-rust-and-foreign-codebases

---
 tools/build/Makefile.build                   | 14 ++++++
 tools/perf/Makefile.perf                     |  2 +-
 tools/perf/tests/builtin-test.c              |  4 ++
 tools/perf/tests/tests.h                     |  4 ++
 tools/perf/tests/workloads/Build             |  5 +++
 tools/perf/tests/workloads/code_with_type.c  | 46 ++++++++++++++++++++
 tools/perf/tests/workloads/code_with_type.rs | 23 ++++++++++
 tools/scripts/Makefile.include               |  2 +
 8 files changed, 99 insertions(+), 1 deletion(-)
 create mode 100644 tools/perf/tests/workloads/code_with_type.c
 create mode 100644 tools/perf/tests/workloads/code_with_type.rs
tools/build/Makefile.build +14 -0
index 3584ff30860..60e65870eae 100644
@@ -76,6 +76,14 @@ quiet_cmd_host_ld_multi = HOSTLD  $@
       cmd_host_ld_multi = $(if $(strip $(obj-y)),\
                           $(HOSTLD) -r -o $@  $(filter $(obj-y),$^),rm -f $@; $(HOSTAR) rcs $@)
 
+rust_common_cmd = \
+	$(RUSTC) $(rust_flags) \
+	--crate-type staticlib -L $(objtree)/rust/ \
+	--emit=dep-info=$(depfile),link
+
+quiet_cmd_rustc_a_rs = $(RUSTC) $(quiet_modtag) $@
+      cmd_rustc_a_rs = $(rust_common_cmd) -o $@ -g $< $(cmd_objtool)
+
 ifneq ($(filter $(obj),$(hostprogs)),)
   host = host_
 endif
@@ -105,6 +113,12 @@ $(OUTPUT)%.s: %.c FORCE
 	$(call rule_mkdir)
 	$(call if_changed_dep,cc_s_c)
 
+# it's recommended to build a static rust library, when a foreight (to rust)
+# linker is used.
+$(OUTPUT)%.a: %.rs FORCE
+	$(call rule_mkdir)
+	$(call if_changed_dep,rustc_a_rs)
+
 # bison and flex files are generated in the OUTPUT directory
 # so it needs a separate rule to depend on them properly
 $(OUTPUT)%-bison.o: $(OUTPUT)%-bison.c FORCE
tools/perf/Makefile.perf +1 -1
index 2a7e5814b15..a6d8ca3e923 100644
@@ -271,7 +271,7 @@ ifeq ($(PYLINT),1)
   PYLINT := $(shell which pylint 2> /dev/null)
 endif
 
-export srctree OUTPUT RM CC CXX LD AR CFLAGS CXXFLAGS V BISON FLEX AWK
+export srctree OUTPUT RM CC CXX RUSTC LD AR CFLAGS CXXFLAGS V BISON FLEX AWK
 export HOSTCC HOSTLD HOSTAR HOSTCFLAGS SHELLCHECK MYPY PYLINT
 
 include $(srctree)/tools/build/Makefile.include
tools/perf/tests/builtin-test.c +4 -0
index e2490652f03..06507066213 100644
@@ -154,6 +154,10 @@ static struct test_workload *workloads[] = {
 	&workload__landlock,
 	&workload__traploop,
 	&workload__inlineloop,
+
+#ifdef HAVE_RUST_SUPPORT
+	&workload__code_with_type,
+#endif
 };
 
 #define workloads__for_each(workload) \
tools/perf/tests/tests.h +4 -0
index 1f0f8b267fb..f5f1238d1f7 100644
@@ -242,6 +242,10 @@ DECLARE_WORKLOAD(landlock);
 DECLARE_WORKLOAD(traploop);
 DECLARE_WORKLOAD(inlineloop);
 
+#ifdef HAVE_RUST_SUPPORT
+DECLARE_WORKLOAD(code_with_type);
+#endif
+
 extern const char *dso_to_test;
 extern const char *test_objdump_path;
 
tools/perf/tests/workloads/Build +5 -0
index 866a00bd14a..2ef97f7affc 100644
@@ -10,6 +10,11 @@ perf-test-y += landlock.o
 perf-test-y += traploop.o
 perf-test-y += inlineloop.o
 
+ifeq ($(CONFIG_RUST_SUPPORT),y)
+    perf-test-y += code_with_type.o
+    perf-test-y += code_with_type.a
+endif
+
 CFLAGS_sqrtloop.o         = -g -O0 -fno-inline -U_FORTIFY_SOURCE
 CFLAGS_leafloop.o         = -g -O0 -fno-inline -fno-omit-frame-pointer -U_FORTIFY_SOURCE
 CFLAGS_brstack.o          = -g -O0 -fno-inline -U_FORTIFY_SOURCE
tools/perf/tests/workloads/code_with_type.c new file +46 -0

46 lines not on this page · raw message

tools/perf/tests/workloads/code_with_type.rs new file +23 -0
new file mode 100644
index 00000000000..3b91e51919d
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: GPL-2.0
+
+// We're going to look for this structure in the data type profiling report
+#[allow(dead_code)]
+struct Buf {
+    data1: u64,
+    data2: String,
+    data3: u64,
+}
+
+#[no_mangle]
+pub extern "C" fn test_rs(count: u32) {
+    let mut b =  Buf { data1: 0, data2: String::from("data"), data3: 0};
+
+    for _ in 1..count {
+        b.data1 += 1;
+        if b.data1 == 123 {
+            b.data1 += 1;
+        }
+
+        b.data3 += b.data1;
+    }
+}
tools/scripts/Makefile.include +2 -0
index ded48263dd5..b5ecf137feb 100644
@@ -94,6 +94,8 @@ LLVM_STRIP	?= llvm-strip
 # Some tools require bpftool
 SYSTEM_BPFTOOL	?= bpftool
 
+RUSTC		?= rustc
+
 ifeq ($(CC_NO_CLANG), 1)
 EXTRA_WARNINGS += -Wstrict-aliasing=3
 
Signature · 1 line
2.52.0
reviewer
#3 lore raw

Re: [RFC PATCH v2 2/4] perf test workload: Add code_with_type test workload

Quoted 42 lines · Dmitrii Dolgov

The purpose of the workload is to gather samples of rust runtime. To achiever that it has a dummy rust library linked with it. Per recommendations for such scenarios [1], the rust library is statically linked.

An example:

$ perf record perf test -w code_with_type
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.160 MB perf.data (4074 samples) ]
$ perf report --stdio --dso perf -s srcfile,srcline
    45.16%  ub_checks.rs       ub_checks.rs:72
     6.72%  code_with_type.rs  code_with_type.rs:15
     6.64%  range.rs           range.rs:767
     4.26%  code_with_type.rs  code_with_type.rs:21
     4.23%  range.rs           range.rs:0
     3.99%  code_with_type.rs  code_with_type.rs:16
    [...]

[1]: https://doc.rust-lang.org/reference/linkage.html#mixed-rust-and-foreign-codebases

---
 tools/build/Makefile.build                   | 14 ++++++
 tools/perf/Makefile.perf                     |  2 +-
 tools/perf/tests/builtin-test.c              |  4 ++
 tools/perf/tests/tests.h                     |  4 ++
 tools/perf/tests/workloads/Build             |  5 +++
 tools/perf/tests/workloads/code_with_type.c  | 46 ++++++++++++++++++++
 tools/perf/tests/workloads/code_with_type.rs | 23 ++++++++++
 tools/scripts/Makefile.include               |  2 +
 8 files changed, 99 insertions(+), 1 deletion(-)
 create mode 100644 tools/perf/tests/workloads/code_with_type.c
 create mode 100644 tools/perf/tests/workloads/code_with_type.rs
tools/build/Makefile.build +0 -0
index 3584ff30860..60e65870eae 100644

tools/build/Makefile.build quiet_cmd_host_ld_multi = HOSTLD $@ · line 82

Quoted 7 lines
tools/build/Makefile.build +4 -0
@@ -76,6 +76,14 @@ quiet_cmd_host_ld_multi = HOSTLD  $@
       cmd_host_ld_multi = $(if $(strip $(obj-y)),\
                           $(HOSTLD) -r -o $@  $(filter $(obj-y),$^),rm -f $@; $(HOSTAR) rcs $@)
 
+rust_common_cmd = \
+       $(RUSTC) $(rust_flags) \
+       --crate-type staticlib -L $(objtree)/rust/ \
+       --emit=dep-info=$(depfile),link

Does this need to be cross compilation aware? I worry that we can detect local Rust support but not have cross compilation support and then perf tool cross compilation builds are broken.

Thanks,
Ian

Quoted 176 lines
tools/build/Makefile.build +10 -0
+
+quiet_cmd_rustc_a_rs = $(RUSTC) $(quiet_modtag) $@
+      cmd_rustc_a_rs = $(rust_common_cmd) -o $@ -g $< $(cmd_objtool)
+
 ifneq ($(filter $(obj),$(hostprogs)),)
   host = host_
 endif
@@ -105,6 +113,12 @@ $(OUTPUT)%.s: %.c FORCE
        $(call rule_mkdir)
        $(call if_changed_dep,cc_s_c)
 
+# it's recommended to build a static rust library, when a foreight (to rust)
+# linker is used.
+$(OUTPUT)%.a: %.rs FORCE
+       $(call rule_mkdir)
+       $(call if_changed_dep,rustc_a_rs)
+
 # bison and flex files are generated in the OUTPUT directory
 # so it needs a separate rule to depend on them properly
 $(OUTPUT)%-bison.o: $(OUTPUT)%-bison.c FORCE
tools/perf/Makefile.perf +1 -1
index 2a7e5814b15..a6d8ca3e923 100644
@@ -271,7 +271,7 @@ ifeq ($(PYLINT),1)
   PYLINT := $(shell which pylint 2> /dev/null)
 endif
 
-export srctree OUTPUT RM CC CXX LD AR CFLAGS CXXFLAGS V BISON FLEX AWK
+export srctree OUTPUT RM CC CXX RUSTC LD AR CFLAGS CXXFLAGS V BISON FLEX AWK
 export HOSTCC HOSTLD HOSTAR HOSTCFLAGS SHELLCHECK MYPY PYLINT
 
 include $(srctree)/tools/build/Makefile.include
tools/perf/tests/builtin-test.c +4 -0
index e2490652f03..06507066213 100644
@@ -154,6 +154,10 @@ static struct test_workload *workloads[] = {
        &workload__landlock,
        &workload__traploop,
        &workload__inlineloop,
+
+#ifdef HAVE_RUST_SUPPORT
+       &workload__code_with_type,
+#endif
 };
 
 #define workloads__for_each(workload) \
tools/perf/tests/tests.h +4 -0
index 1f0f8b267fb..f5f1238d1f7 100644
@@ -242,6 +242,10 @@ DECLARE_WORKLOAD(landlock);
 DECLARE_WORKLOAD(traploop);
 DECLARE_WORKLOAD(inlineloop);
 
+#ifdef HAVE_RUST_SUPPORT
+DECLARE_WORKLOAD(code_with_type);
+#endif
+
 extern const char *dso_to_test;
 extern const char *test_objdump_path;
 
tools/perf/tests/workloads/Build +5 -0
index 866a00bd14a..2ef97f7affc 100644
@@ -10,6 +10,11 @@ perf-test-y += landlock.o
 perf-test-y += traploop.o
 perf-test-y += inlineloop.o
 
+ifeq ($(CONFIG_RUST_SUPPORT),y)
+    perf-test-y += code_with_type.o
+    perf-test-y += code_with_type.a
+endif
+
 CFLAGS_sqrtloop.o         = -g -O0 -fno-inline -U_FORTIFY_SOURCE
 CFLAGS_leafloop.o         = -g -O0 -fno-inline -fno-omit-frame-pointer -U_FORTIFY_SOURCE
 CFLAGS_brstack.o          = -g -O0 -fno-inline -U_FORTIFY_SOURCE
tools/perf/tests/workloads/code_with_type.c new file +46 -0
new file mode 100644
index 00000000000..65d7be7dac2
@@ -0,0 +1,46 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <pthread.h>
+#include <stdlib.h>
+#include <signal.h>
+#include <unistd.h>
+#include <linux/compiler.h>
+#include "../tests.h"
+
+extern void test_rs(uint count);
+
+static volatile sig_atomic_t done;
+
+static void sighandler(int sig __maybe_unused)
+{
+       done = 1;
+}
+
+static int code_with_type(int argc, const char **argv)
+{
+       int sec = 1, num_loops = 100;
+
+       pthread_setname_np(pthread_self(), "perf-code-with-type");
+       if (argc > 0)
+               sec = atoi(argv[0]);
+
+       if (argc > 1)
+               num_loops = atoi(argv[1]);
+
+       signal(SIGINT, sighandler);
+       signal(SIGALRM, sighandler);
+       alarm(sec);
+
+       /*
+        * Rust doesn't have signal management in the standard library. To
+        * not deal with any external crates, offload signal handling to the
+        * outside code.
+        */
+       while (!done) {
+               test_rs(num_loops);
+               continue;
+       }
+
+       return 0;
+}
+
+DEFINE_WORKLOAD(code_with_type);
tools/perf/tests/workloads/code_with_type.rs new file +23 -0
new file mode 100644
index 00000000000..3b91e51919d
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: GPL-2.0
+
+// We're going to look for this structure in the data type profiling report
+#[allow(dead_code)]
+struct Buf {
+    data1: u64,
+    data2: String,
+    data3: u64,
+}
+
+#[no_mangle]
+pub extern "C" fn test_rs(count: u32) {
+    let mut b =  Buf { data1: 0, data2: String::from("data"), data3: 0};
+
+    for _ in 1..count {
+        b.data1 += 1;
+        if b.data1 == 123 {
+            b.data1 += 1;
+        }
+
+        b.data3 += b.data1;
+    }
+}
tools/scripts/Makefile.include +2 -1
index ded48263dd5..b5ecf137feb 100644
@@ -94,6 +94,8 @@ LLVM_STRIP    ?= llvm-strip
 # Some tools require bpftool
 SYSTEM_BPFTOOL ?= bpftool
 
+RUSTC          ?= rustc
+
 ifeq ($(CC_NO_CLANG), 1)
 EXTRA_WARNINGS += -Wstrict-aliasing=3
 
--

2.52.0

#4 lore raw

Re: [RFC PATCH v2 2/4] perf test workload: Add code_with_type test workload

Quoted 10 lines · Ian Rogers

tools/build/Makefile.build quiet_cmd_host_ld_multi = HOSTLD $@ · line 82

Quoted 5 lines
tools/build/Makefile.build +4 -0
+rust_common_cmd = \
+       $(RUSTC) $(rust_flags) \
+       --crate-type staticlib -L $(objtree)/rust/ \
+       --emit=dep-info=$(depfile),link

Does this need to be cross compilation aware? I worry that we can detect local Rust support but not have cross compilation support and then perf tool cross compilation builds are broken.

Good point. It's supposed to be cross compilation aware, because rust_flags must contain the --target argument. But after checking it out, looks like this variable is not passed from Makefile.perf . I'll need to fix it, it should be handled similarly to CFLAGS/CXXFLAGS.

reviewer
#5 lore raw

Re: [RFC PATCH v2 2/4] perf test workload: Add code_with_type test workload

In reply to Dmitrii Dolgov
Quoted 117 lines · Dmitrii Dolgov

The purpose of the workload is to gather samples of rust runtime. To achiever that it has a dummy rust library linked with it. Per recommendations for such scenarios [1], the rust library is statically linked.

An example:

$ perf record perf test -w code_with_type
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.160 MB perf.data (4074 samples) ]
$ perf report --stdio --dso perf -s srcfile,srcline
    45.16%  ub_checks.rs       ub_checks.rs:72
     6.72%  code_with_type.rs  code_with_type.rs:15
     6.64%  range.rs           range.rs:767
     4.26%  code_with_type.rs  code_with_type.rs:21
     4.23%  range.rs           range.rs:0
     3.99%  code_with_type.rs  code_with_type.rs:16
    [...]

[1]: https://doc.rust-lang.org/reference/linkage.html#mixed-rust-and-foreign-codebases

---
 tools/build/Makefile.build                   | 14 ++++++
 tools/perf/Makefile.perf                     |  2 +-
 tools/perf/tests/builtin-test.c              |  4 ++
 tools/perf/tests/tests.h                     |  4 ++
 tools/perf/tests/workloads/Build             |  5 +++
 tools/perf/tests/workloads/code_with_type.c  | 46 ++++++++++++++++++++
 tools/perf/tests/workloads/code_with_type.rs | 23 ++++++++++
 tools/scripts/Makefile.include               |  2 +
 8 files changed, 99 insertions(+), 1 deletion(-)
 create mode 100644 tools/perf/tests/workloads/code_with_type.c
 create mode 100644 tools/perf/tests/workloads/code_with_type.rs
tools/build/Makefile.build +14 -0
index 3584ff30860..60e65870eae 100644
@@ -76,6 +76,14 @@ quiet_cmd_host_ld_multi = HOSTLD  $@
       cmd_host_ld_multi = $(if $(strip $(obj-y)),\
                           $(HOSTLD) -r -o $@  $(filter $(obj-y),$^),rm -f $@; $(HOSTAR) rcs $@)
 
+rust_common_cmd = \
+       $(RUSTC) $(rust_flags) \
+       --crate-type staticlib -L $(objtree)/rust/ \
+       --emit=dep-info=$(depfile),link
+
+quiet_cmd_rustc_a_rs = $(RUSTC) $(quiet_modtag) $@
+      cmd_rustc_a_rs = $(rust_common_cmd) -o $@ -g $< $(cmd_objtool)
+
 ifneq ($(filter $(obj),$(hostprogs)),)
   host = host_
 endif
@@ -105,6 +113,12 @@ $(OUTPUT)%.s: %.c FORCE
        $(call rule_mkdir)
        $(call if_changed_dep,cc_s_c)
 
+# it's recommended to build a static rust library, when a foreight (to rust)
+# linker is used.
+$(OUTPUT)%.a: %.rs FORCE
+       $(call rule_mkdir)
+       $(call if_changed_dep,rustc_a_rs)
+
 # bison and flex files are generated in the OUTPUT directory
 # so it needs a separate rule to depend on them properly
 $(OUTPUT)%-bison.o: $(OUTPUT)%-bison.c FORCE
tools/perf/Makefile.perf +1 -1
index 2a7e5814b15..a6d8ca3e923 100644
@@ -271,7 +271,7 @@ ifeq ($(PYLINT),1)
   PYLINT := $(shell which pylint 2> /dev/null)
 endif
 
-export srctree OUTPUT RM CC CXX LD AR CFLAGS CXXFLAGS V BISON FLEX AWK
+export srctree OUTPUT RM CC CXX RUSTC LD AR CFLAGS CXXFLAGS V BISON FLEX AWK
 export HOSTCC HOSTLD HOSTAR HOSTCFLAGS SHELLCHECK MYPY PYLINT
 
 include $(srctree)/tools/build/Makefile.include
tools/perf/tests/builtin-test.c +4 -0
index e2490652f03..06507066213 100644
@@ -154,6 +154,10 @@ static struct test_workload *workloads[] = {
        &workload__landlock,
        &workload__traploop,
        &workload__inlineloop,
+
+#ifdef HAVE_RUST_SUPPORT
+       &workload__code_with_type,
+#endif
 };
 
 #define workloads__for_each(workload) \
tools/perf/tests/tests.h +4 -0
index 1f0f8b267fb..f5f1238d1f7 100644
@@ -242,6 +242,10 @@ DECLARE_WORKLOAD(landlock);
 DECLARE_WORKLOAD(traploop);
 DECLARE_WORKLOAD(inlineloop);
 
+#ifdef HAVE_RUST_SUPPORT
+DECLARE_WORKLOAD(code_with_type);
+#endif
+
 extern const char *dso_to_test;
 extern const char *test_objdump_path;
 
tools/perf/tests/workloads/Build +0 -0
index 866a00bd14a..2ef97f7affc 100644

tools/perf/tests/workloads/Build perf-test-y += landlock.o · line 15

Quoted 6 lines
tools/perf/tests/workloads/Build +3 -0
@@ -10,6 +10,11 @@ perf-test-y += landlock.o
 perf-test-y += traploop.o
 perf-test-y += inlineloop.o
 
+ifeq ($(CONFIG_RUST_SUPPORT),y)
+    perf-test-y += code_with_type.o
+    perf-test-y += code_with_type.a

nit: It'd be more normal to write this as:
perf-test-$(CONFIG_RUST_SUPPORT) += code_with_type.o perf-test-$(CONFIG_RUST_SUPPORT) += code_with_type.a

I did an update this morning but found I needed to install rustc to get the build working. I'm not sure things work without rustc installed as advertised.

Thanks,
Ian

Quoted 102 lines
tools/perf/tests/workloads/Build +2 -0
+endif
+
 CFLAGS_sqrtloop.o         = -g -O0 -fno-inline -U_FORTIFY_SOURCE
 CFLAGS_leafloop.o         = -g -O0 -fno-inline -fno-omit-frame-pointer -U_FORTIFY_SOURCE
 CFLAGS_brstack.o          = -g -O0 -fno-inline -U_FORTIFY_SOURCE
tools/perf/tests/workloads/code_with_type.c new file +46 -0
new file mode 100644
index 00000000000..65d7be7dac2
@@ -0,0 +1,46 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <pthread.h>
+#include <stdlib.h>
+#include <signal.h>
+#include <unistd.h>
+#include <linux/compiler.h>
+#include "../tests.h"
+
+extern void test_rs(uint count);
+
+static volatile sig_atomic_t done;
+
+static void sighandler(int sig __maybe_unused)
+{
+       done = 1;
+}
+
+static int code_with_type(int argc, const char **argv)
+{
+       int sec = 1, num_loops = 100;
+
+       pthread_setname_np(pthread_self(), "perf-code-with-type");
+       if (argc > 0)
+               sec = atoi(argv[0]);
+
+       if (argc > 1)
+               num_loops = atoi(argv[1]);
+
+       signal(SIGINT, sighandler);
+       signal(SIGALRM, sighandler);
+       alarm(sec);
+
+       /*
+        * Rust doesn't have signal management in the standard library. To
+        * not deal with any external crates, offload signal handling to the
+        * outside code.
+        */
+       while (!done) {
+               test_rs(num_loops);
+               continue;
+       }
+
+       return 0;
+}
+
+DEFINE_WORKLOAD(code_with_type);
tools/perf/tests/workloads/code_with_type.rs new file +23 -0
new file mode 100644
index 00000000000..3b91e51919d
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: GPL-2.0
+
+// We're going to look for this structure in the data type profiling report
+#[allow(dead_code)]
+struct Buf {
+    data1: u64,
+    data2: String,
+    data3: u64,
+}
+
+#[no_mangle]
+pub extern "C" fn test_rs(count: u32) {
+    let mut b =  Buf { data1: 0, data2: String::from("data"), data3: 0};
+
+    for _ in 1..count {
+        b.data1 += 1;
+        if b.data1 == 123 {
+            b.data1 += 1;
+        }
+
+        b.data3 += b.data1;
+    }
+}
tools/scripts/Makefile.include +2 -1
index ded48263dd5..b5ecf137feb 100644
@@ -94,6 +94,8 @@ LLVM_STRIP    ?= llvm-strip
 # Some tools require bpftool
 SYSTEM_BPFTOOL ?= bpftool
 
+RUSTC          ?= rustc
+
 ifeq ($(CC_NO_CLANG), 1)
 EXTRA_WARNINGS += -Wstrict-aliasing=3
 
--

2.52.0

#6 lore raw

Re: [RFC PATCH v2 2/4] perf test workload: Add code_with_type test workload

Quoted 5 lines · Ian Rogers

I did an update this morning but found I needed to install rustc to get the build working. I'm not sure things work without rustc installed as advertised.

Yeah, sorry for that. There are few fixes starting from 3f5dfa472ea (tools build: Fix rust feature detection), which should address this.

#7 lore raw

RFC PATCH v2 3/4 perf tests: Test annotate with data type profiling and rust

In reply to Dmitrii Dolgov

Exercise the annotate command with data type profiling feature on the rust runtime. For that add a new shell test, which will profile the code_with_type workload, then annotate the result expecting to see some data structures from the rust code.

---
 tools/perf/tests/shell/data_type_profiling.sh | 69 +++++++++++++++++++
 1 file changed, 69 insertions(+)
 create mode 100755 tools/perf/tests/shell/data_type_profiling.sh
tools/perf/tests/shell/data_type_profiling.sh new file +69 -0
new file mode 100755
index 00000000000..cdc9adb7d70
@@ -0,0 +1,69 @@
+#!/bin/bash
+# perf data type profiling tests
+# SPDX-License-Identifier: GPL-2.0
+
+set -e
+
+# The logic below follows the same line as the annotate test, but looks for a
+# data type profiling manifestation
+testtype="# data-type: struct Buf"
+
+err=0
+perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
+perfout=$(mktemp /tmp/__perf_test.perf.out.XXXXX)
+testprog="perf test -w code_with_type"
+
+cleanup() {
+  rm -rf "${perfdata}" "${perfout}"
+  rm -rf "${perfdata}".old
+
+  trap - EXIT TERM INT
+}
+
+trap_cleanup() {
+  echo "Unexpected signal in ${FUNCNAME[1]}"
+  cleanup
+  exit 1
+}
+trap trap_cleanup EXIT TERM INT
+
+test_basic_annotate() {
+  mode=$1
+  echo "${mode} perf annotate test"
+  if [ "x${mode}" == "xBasic" ]
+  then
+    perf mem record -o "${perfdata}" ${testprog} 2> /dev/null
+  else
+    perf mem record -o - ${testprog} 2> /dev/null > "${perfdata}"
+  fi
+  if [ "x$?" != "x0" ]
+  then
+    echo "${mode} annotate [Failed: perf record]"
+    err=1
+    return
+  fi
+
+  # Generate the annotated output file
+  if [ "x${mode}" == "xBasic" ]
+  then
+    perf annotate --code-with-type -i "${perfdata}" --stdio --percent-limit 1 2> /dev/null > "${perfout}"
+  else
+    perf annotate --code-with-type -i - --stdio 2> /dev/null --percent-limit 1 < "${perfdata}" > "${perfout}"
+  fi
+
+  # check if it has the target data type
+  if ! grep -q "${testtype}" "${perfout}"
+  then
+    echo "${mode} annotate [Failed: missing target data type]"
+    cat "${perfout}"
+    err=1
+    return
+  fi
+  echo "${mode} annotate test [Success]"
+}
+
+test_basic_annotate Basic
+test_basic_annotate Pipe
+
+cleanup
+exit $err
Signature · 1 line
2.52.0
#8 lore raw

RFC PATCH v2 4/4 perf tests: Test annotate with data type profiling and C

In reply to Dmitrii Dolgov

Exercise the annotate command with data type profiling feature with C. For that extend the existing data type profiling shell test to profile the datasym workload, then annotate the result expecting to see some data structures from the C code.

---
 tools/perf/tests/shell/data_type_profiling.sh | 31 ++++++++++++++-----
 1 file changed, 23 insertions(+), 8 deletions(-)
tools/perf/tests/shell/data_type_profiling.sh +23 -8
index cdc9adb7d70..a230f5d4c42 100755
@@ -6,12 +6,14 @@ set -e
 
 # The logic below follows the same line as the annotate test, but looks for a
 # data type profiling manifestation
-testtype="# data-type: struct Buf"
+
+# Values in testtypes and testprogs should match
+testtypes=("# data-type: struct Buf" "# data-type: struct _buf")
+testprogs=("perf test -w code_with_type" "perf test -w datasym")
 
 err=0
 perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
 perfout=$(mktemp /tmp/__perf_test.perf.out.XXXXX)
-testprog="perf test -w code_with_type"
 
 cleanup() {
   rm -rf "${perfdata}" "${perfout}"
@@ -29,12 +31,23 @@ trap trap_cleanup EXIT TERM INT
 
 test_basic_annotate() {
   mode=$1
-  echo "${mode} perf annotate test"
+  runtime=$2
+
+  echo "${mode} ${runtime} perf annotate test"
+
+  case "x${runtime}" in
+    "xRust")
+    index=0 ;;
+
+    "xC")
+    index=1 ;;
+  esac
+
   if [ "x${mode}" == "xBasic" ]
   then
-    perf mem record -o "${perfdata}" ${testprog} 2> /dev/null
+    perf mem record -o "${perfdata}" ${testprogs[$index]} 2> /dev/null
   else
-    perf mem record -o - ${testprog} 2> /dev/null > "${perfdata}"
+    perf mem record -o - ${testprogs[$index]} 2> /dev/null > "${perfdata}"
   fi
   if [ "x$?" != "x0" ]
   then
@@ -52,7 +65,7 @@ test_basic_annotate() {
   fi
 
   # check if it has the target data type
-  if ! grep -q "${testtype}" "${perfout}"
+  if ! grep -q "${testtypes[$index]}" "${perfout}"
   then
     echo "${mode} annotate [Failed: missing target data type]"
     cat "${perfout}"
@@ -62,8 +75,10 @@ test_basic_annotate() {
   echo "${mode} annotate test [Success]"
 }
 
-test_basic_annotate Basic
-test_basic_annotate Pipe
+test_basic_annotate Basic Rust
+test_basic_annotate Pipe Rust
+test_basic_annotate Basic C
+test_basic_annotate Pipe C
 
 cleanup
 exit $err
Signature · 1 line
2.52.0
reviewer
#9 lore raw

Re: [RFC PATCH v2 4/4] perf tests: Test annotate with data type profiling and C

Quoted 16 lines · Dmitrii Dolgov

Exercise the annotate command with data type profiling feature with C. For that extend the existing data type profiling shell test to profile the datasym workload, then annotate the result expecting to see some data structures from the C code.

---
 tools/perf/tests/shell/data_type_profiling.sh | 31 ++++++++++++++-----
 1 file changed, 23 insertions(+), 8 deletions(-)
tools/perf/tests/shell/data_type_profiling.sh +0 -0
index cdc9adb7d70..a230f5d4c42 100755

tools/perf/tests/shell/data_type_profiling.sh set -e · line 11

Quoted 7 lines
tools/perf/tests/shell/data_type_profiling.sh +3 -1
@@ -6,12 +6,14 @@ set -e
 
 # The logic below follows the same line as the annotate test, but looks for a
 # data type profiling manifestation
-testtype="# data-type: struct Buf"
+
+# Values in testtypes and testprogs should match
+testtypes=("# data-type: struct Buf" "# data-type: struct _buf")

So in the datasym workload the struct _buf is a typedef and the value declared to be of the type of the typedef:
https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/tests/workloads/datasym.c?h=perf-tools-next#n14

```
typedef struct _buf {
char data1;
char reserved[55];
char data2;
} buf __attribute__((aligned(64)));

/* volatile to try to avoid the compiler seeing reserved as unused. */
static volatile buf workload_datasym_buf1 = {
/* to have this in the data section */
.reserved[0] = 1,
};
```
This test is failing for me as I see "data-type: buf" rather than
"data-type: struct _buf" (ie the typedef rather than the struct):
```
    0.00 :   201290:        movzbl  0xbb5869(%rip), %eax  # 0xdb6b00
<workload_datasym_buf1>               # data-type: buf +0 (data1)
    0.00 :   201297:        addl    $0x1, %eax
   47.66 :   20129a:        movb    %al, 0xbb5860(%rip)  # 0xdb6b00
<workload_datasym_buf1>                # data-type: buf +0 (data1)
    0.00 :   2012a0:        movzbl  0xbb5859(%rip), %eax  # 0xdb6b00
<workload_datasym_buf1>               # data-type: buf +0 (data1)
    0.00 :   2012a7:        cmpb    $0x7b, %al
    0.00 :   2012a9:        jne     0x2012bb <datasym+0x9c>
    0.00 :   2012ab:        movzbl  0xbb584e(%rip), %eax  # 0xdb6b00
<workload_datasym_buf1>               # data-type: buf +0 (data1)
    0.00 :   2012b2:        addl    $0x1, %eax
    0.25 :   2012b5:        movb    %al, 0xbb5845(%rip)  # 0xdb6b00
<workload_datasym_buf1>                # data-type: buf +0 (data1)
    0.00 :   2012bb:        movzbl  0xbb583e(%rip), %eax  # 0xdb6b00
<workload_datasym_buf1>               # data-type: buf +0 (data1)
    0.00 :   2012c2:        movzbl  0xbb586f(%rip), %edx  # 0xdb6b38
<workload_datasym_buf1+0x38>                  # data-type: buf +0x38
(data2)
    0.00 :   2012c9:        addl    %edx, %eax
   52.10 :   2012cb:        movb    %al, 0xbb5867(%rip)  # 0xdb6b38
<workload_datasym_buf1+0x38>                   # data-type: buf +0x38
(data2)
    0.00 :   2012d1:        movl    0xbc7861(%rip), %eax  # 0xdc8b38
<done>             # data-type: sig_atomic_t +0
```

It seems the typedef is a more accurate type name, so I think the test is wrong here.

Thanks,
Ian

Quoted 61 lines
tools/perf/tests/shell/data_type_profiling.sh +20 -8
+testprogs=("perf test -w code_with_type" "perf test -w datasym")
 
 err=0
 perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
 perfout=$(mktemp /tmp/__perf_test.perf.out.XXXXX)
-testprog="perf test -w code_with_type"
 
 cleanup() {
   rm -rf "${perfdata}" "${perfout}"
@@ -29,12 +31,23 @@ trap trap_cleanup EXIT TERM INT
 
 test_basic_annotate() {
   mode=$1
-  echo "${mode} perf annotate test"
+  runtime=$2
+
+  echo "${mode} ${runtime} perf annotate test"
+
+  case "x${runtime}" in
+    "xRust")
+    index=0 ;;
+
+    "xC")
+    index=1 ;;
+  esac
+
   if [ "x${mode}" == "xBasic" ]
   then
-    perf mem record -o "${perfdata}" ${testprog} 2> /dev/null
+    perf mem record -o "${perfdata}" ${testprogs[$index]} 2> /dev/null
   else
-    perf mem record -o - ${testprog} 2> /dev/null > "${perfdata}"
+    perf mem record -o - ${testprogs[$index]} 2> /dev/null > "${perfdata}"
   fi
   if [ "x$?" != "x0" ]
   then
@@ -52,7 +65,7 @@ test_basic_annotate() {
   fi
 
   # check if it has the target data type
-  if ! grep -q "${testtype}" "${perfout}"
+  if ! grep -q "${testtypes[$index]}" "${perfout}"
   then
     echo "${mode} annotate [Failed: missing target data type]"
     cat "${perfout}"
@@ -62,8 +75,10 @@ test_basic_annotate() {
   echo "${mode} annotate test [Success]"
 }
 
-test_basic_annotate Basic
-test_basic_annotate Pipe
+test_basic_annotate Basic Rust
+test_basic_annotate Pipe Rust
+test_basic_annotate Basic C
+test_basic_annotate Pipe C
 
 cleanup
 exit $err
--

2.52.0

#10 lore raw

Re: [RFC PATCH v2 4/4] perf tests: Test annotate with data type profiling and C

Quoted 53 lines · Ian Rogers

tools/perf/tests/shell/data_type_profiling.sh set -e · line 11

Quoted 6 lines

# The logic below follows the same line as the annotate test, but looks for a

 # data type profiling manifestation
tools/perf/tests/shell/data_type_profiling.sh +3 -1
-testtype="# data-type: struct Buf"
+
+# Values in testtypes and testprogs should match
+testtypes=("# data-type: struct Buf" "# data-type: struct _buf")

So in the datasym workload the struct _buf is a typedef and the value declared to be of the type of the typedef:
https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/tests/workloads/datasym.c?h=perf-tools-next#n14

```
typedef struct _buf {
char data1;
char reserved[55];
char data2;
} buf __attribute__((aligned(64)));

/* volatile to try to avoid the compiler seeing reserved as unused. */
static volatile buf workload_datasym_buf1 = {
/* to have this in the data section */
.reserved[0] = 1,
};
```
This test is failing for me as I see "data-type: buf" rather than
"data-type: struct _buf" (ie the typedef rather than the struct):
```
    0.00 :   201290:        movzbl  0xbb5869(%rip), %eax  # 0xdb6b00
<workload_datasym_buf1>               # data-type: buf +0 (data1)
    0.00 :   201297:        addl    $0x1, %eax
   47.66 :   20129a:        movb    %al, 0xbb5860(%rip)  # 0xdb6b00
<workload_datasym_buf1>                # data-type: buf +0 (data1)
    0.00 :   2012a0:        movzbl  0xbb5859(%rip), %eax  # 0xdb6b00
<workload_datasym_buf1>               # data-type: buf +0 (data1)
    0.00 :   2012a7:        cmpb    $0x7b, %al
    0.00 :   2012a9:        jne     0x2012bb <datasym+0x9c>
    0.00 :   2012ab:        movzbl  0xbb584e(%rip), %eax  # 0xdb6b00
<workload_datasym_buf1>               # data-type: buf +0 (data1)
    0.00 :   2012b2:        addl    $0x1, %eax
    0.25 :   2012b5:        movb    %al, 0xbb5845(%rip)  # 0xdb6b00
<workload_datasym_buf1>                # data-type: buf +0 (data1)
    0.00 :   2012bb:        movzbl  0xbb583e(%rip), %eax  # 0xdb6b00
<workload_datasym_buf1>               # data-type: buf +0 (data1)
    0.00 :   2012c2:        movzbl  0xbb586f(%rip), %edx  # 0xdb6b38
<workload_datasym_buf1+0x38>                  # data-type: buf +0x38
(data2)
    0.00 :   2012c9:        addl    %edx, %eax
   52.10 :   2012cb:        movb    %al, 0xbb5867(%rip)  # 0xdb6b38
<workload_datasym_buf1+0x38>                   # data-type: buf +0x38
(data2)
    0.00 :   2012d1:        movl    0xbc7861(%rip), %eax  # 0xdc8b38
<done>             # data-type: sig_atomic_t +0
```
Quoted 2 lines

It seems the typedef is a more accurate type name, so I think the test is wrong here.

That's interesting, I don't see "data-type: buf" for this particular test, but I've noticed that "perf annotate" reports different data types (buf vs _buf) in certain cases. E.g. if as in the test, the data is collected via

perf mem record

then I get "data-type: struct _buf". But if we do

perf record

then I get "data-type: buf". Interestingly enough, all the addresses looks the same, except the final type_die in the end:

	# perf record
	find data type for 0xa00e69(PC) at datasym+0x71
	CU for tests/workloads/datasym.c (die:0x1ddfcf)
	found by addr=0xf7e000 type_offset=0
	 type='buf' size=0x39 (die:0x1de132)
	final result:  type='buf' size=0x39 (die:0x1de132)
	# perf mem record
	find data type for 0xa00e69(PC) at datasym+0x71
	CU for tests/workloads/datasym.c (die:0x1ddfcf)
	found by addr=0xf7e000 type_offset=0
	 type='struct _buf' size=0x39 (die:0x1de0ed)
	final result:  type='struct _buf' size=0x39 (die:0x1de0ed)

Not sure what to make of it, still trying to figure out where it's coming from.

reviewer
#11 lore raw

PATCH 1/1 perf test type profiling: Remote typedef on struct

The typedef creates an issue where the struct or the typedef may appear in the output and cause the "perf data type profiling tests" to fail. Let's remove the typedef to keep the test passing.

Fixes 335047109d7d ("perf tests: Test annotate with data type profiling and C")
---
 tools/perf/tests/shell/data_type_profiling.sh | 2 +-
 tools/perf/tests/workloads/datasym.c          | 6 +++---
 2 files changed, 4 insertions(+), 4 deletions(-)
tools/perf/tests/shell/data_type_profiling.sh +1 -1
index 2a7f8f7c42d0..fb47b7213b33 100755
@@ -8,7 +8,7 @@ set -e
 # data type profiling manifestation
 
 # Values in testtypes and testprogs should match
-testtypes=("# data-type: struct Buf" "# data-type: struct _buf")
+testtypes=("# data-type: struct Buf" "# data-type: struct buf")
 testprogs=("perf test -w code_with_type" "perf test -w datasym")
 
 err=0
tools/perf/tests/workloads/datasym.c +3 -3
index 1d0b7d64e1ba..19242c7255c0 100644
@@ -4,14 +4,14 @@
 #include <linux/compiler.h>
 #include "../tests.h"
 
-typedef struct _buf {
+struct buf {
 	char data1;
 	char reserved[55];
 	char data2;
-} buf __attribute__((aligned(64)));
+} __attribute__((aligned(64)));
 
 /* volatile to try to avoid the compiler seeing reserved as unused. */
-static volatile buf workload_datasym_buf1 = {
+static volatile struct buf workload_datasym_buf1 = {
 	/* to have this in the data section */
 	.reserved[0] = 1,
 };
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
author Reviewed-by on v1 1/1
#12 lore raw

Re: [PATCH v1] perf test type profiling: Remote typedef on struct

Quoted 4 lines · Ian Rogers

The typedef creates an issue where the struct or the typedef may appear in the output and cause the "perf data type profiling tests" to fail. Let's remove the typedef to keep the test passing.

Yes, makes sense to me, thanks. As mentioned in the previous message, it sounds fishy to me that perf record and perf mem record capture different data type -- I'll try to get to the bottom of it (I was sort of hoping to finish the cross compilation topic first, but since it's not moving that fast, why not do some debugging here).

maintainer Applied
#13 lore raw

Re: [PATCH v1] perf test type profiling: Remote typedef on struct

Quoted 5 lines · Dmitry Dolgov
Quoted 4 lines · Ian Rogers

The typedef creates an issue where the struct or the typedef may appear in the output and cause the "perf data type profiling tests" to fail. Let's remove the typedef to keep the test passing.

Quoted 7 lines

Yes, makes sense to me, thanks. As mentioned in the previous message, it sounds fishy to me that perf record and perf mem record capture different data type -- I'll try to get to the bottom of it (I was sort of hoping to finish the cross compilation topic first, but since it's not moving that fast, why not do some debugging here).

Thanks, applied to perf-tools, for v7.0.

- Arnaldo

#14 lore raw

Re: [PATCH v1] perf test type profiling: Remote typedef on struct

In reply to Dmitry Dolgov
Quoted 9 lines · Dmitry Dolgov
Quoted 4 lines · Ian Rogers

The typedef creates an issue where the struct or the typedef may appear in the output and cause the "perf data type profiling tests" to fail. Let's remove the typedef to keep the test passing.

Yes, makes sense to me, thanks. As mentioned in the previous message, it sounds fishy to me that perf record and perf mem record capture different data type -- I'll try to get to the bottom of it.

I think I figured it out. To reiterate, the problem was that in my environment this:

    $ perf record ...
    $ perf annotate --code-with-type ...

was showing a different data structure for workload_datasym_buf1 (buf vs struct _buf) than:

    $ perf mem record ...
    $ perf annotate --code-with-type ...

It turns out that the type_die for this variable was derived differently: for "record" it was going through check_variable, for "mem record" through global_var__collect. If the type tag is DW_TAG_typedef, check_variable tries to figure out a real data type via die_get_real_type, which says this:

/**
* [...]
* If the type is qualifiers (e.g. const) or typedef, this skips it
* and tries to find real type (structure or basic types, e.g. int). */

But the underlying implementation doesn't actually check for DW_TAG_typedef for some reason:

	while (tag == DW_TAG_const_type ||
		 tag == DW_TAG_restrict_type ||
		 tag == DW_TAG_volatile_type ||
		 tag == DW_TAG_shared_type);

By itself it doesn't look problematic, but for some reason datasym test was producing such a code, that had a chain of two DW_TAG_typedef in a row, so that die_get_real_type was returning a DW_TAG_typedef again.

It looks to me like a deficiency of die_get_real_type, and the following code change fixes the problem for me:

tools/perf/util/dwarf-aux.c +1 -0
index 9267af204c7..de152fae1d9 100644
@@ -279,6 +279,7 @@ Dwarf_Die *__die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
        } while (tag == DW_TAG_const_type ||
                 tag == DW_TAG_restrict_type ||
                 tag == DW_TAG_volatile_type ||
+                tag == DW_TAG_typedef ||
                 tag == DW_TAG_shared_type);
 
        return vr_die;

Does this change sound reasonable?

maintainer
#15 lore raw

Re: [PATCH v1] perf test type profiling: Remote typedef on struct

Hello,

Quoted 61 lines · Dmitry Dolgov
Quoted 9 lines · Dmitry Dolgov
Quoted 4 lines · Ian Rogers

The typedef creates an issue where the struct or the typedef may appear in the output and cause the "perf data type profiling tests" to fail. Let's remove the typedef to keep the test passing.

Yes, makes sense to me, thanks. As mentioned in the previous message, it sounds fishy to me that perf record and perf mem record capture different data type -- I'll try to get to the bottom of it.

I think I figured it out. To reiterate, the problem was that in my environment this:

    $ perf record ...
    $ perf annotate --code-with-type ...

was showing a different data structure for workload_datasym_buf1 (buf vs struct _buf) than:

    $ perf mem record ...
    $ perf annotate --code-with-type ...

It turns out that the type_die for this variable was derived differently: for "record" it was going through check_variable, for "mem record" through global_var__collect. If the type tag is DW_TAG_typedef, check_variable tries to figure out a real data type via die_get_real_type, which says this:

/**
* [...]
* If the type is qualifiers (e.g. const) or typedef, this skips it
* and tries to find real type (structure or basic types, e.g. int). */

But the underlying implementation doesn't actually check for DW_TAG_typedef for some reason:

	while (tag == DW_TAG_const_type ||
		 tag == DW_TAG_restrict_type ||
		 tag == DW_TAG_volatile_type ||
		 tag == DW_TAG_shared_type);

By itself it doesn't look problematic, but for some reason datasym test was producing such a code, that had a chain of two DW_TAG_typedef in a row, so that die_get_real_type was returning a DW_TAG_typedef again.

It looks to me like a deficiency of die_get_real_type, and the following code change fixes the problem for me:

tools/perf/util/dwarf-aux.c +1 -0
index 9267af204c7..de152fae1d9 100644
@@ -279,6 +279,7 @@ Dwarf_Die *__die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
        } while (tag == DW_TAG_const_type ||
                 tag == DW_TAG_restrict_type ||
                 tag == DW_TAG_volatile_type ||
+                tag == DW_TAG_typedef ||
                 tag == DW_TAG_shared_type);
 
        return vr_die;
Quoted 1 line

Does this change sound reasonable?

We have this:

  Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
  {
      do {
          vr_die = __die_get_real_type(vr_die, die_mem);
      } while (vr_die && dwarf_tag(vr_die) == DW_TAG_typedef);
      return vr_die;

}

Thanks,
Namhyung

#16 lore raw

Re: [PATCH v1] perf test type profiling: Remote typedef on struct

Quoted 12 lines · Namhyung Kim

We have this:

  Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
  {
      do {
          vr_die = __die_get_real_type(vr_die, die_mem);
      } while (vr_die && dwarf_tag(vr_die) == DW_TAG_typedef);
      return vr_die;

}

Indeed, I didn't finish the analysis, fooled that the diff was solving the issue. Adding DW_TAG_typedef into __die_get_real_type still fixes the problem, but due to different reasons:

* check_variable calls __die_get_real_type directly at the beginning and stores the result in type_die, which is the result of the function.

* if it doesn't resolve DW_TAG_typedef, the flow goes into the die_get_real_type, which stores the result in the local variable sized_type. But it never copied into the type_die, and never returned as a result of the check_variable.

Hence we either need to add DW_TAG_typedef into __die_get_real_type, or copy sized_type into type_die. After some testing both seems to be achieving the goal.

#17 lore raw

Re: [PATCH v1] perf test type profiling: Remote typedef on struct

Quoted 21 lines · Dmitry Dolgov
Quoted 12 lines · Namhyung Kim

We have this:

  Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
  {
      do {
          vr_die = __die_get_real_type(vr_die, die_mem);
      } while (vr_die && dwarf_tag(vr_die) == DW_TAG_typedef);
      return vr_die;

}

Indeed, I didn't finish the analysis, fooled that the diff was solving the issue. Adding DW_TAG_typedef into __die_get_real_type still fixes the problem, but due to different reasons:

* check_variable calls __die_get_real_type directly at the beginning and stores the result in type_die, which is the result of the function.

Quoted 8 lines

* if it doesn't resolve DW_TAG_typedef, the flow goes into the die_get_real_type, which stores the result in the local variable sized_type. But it never copied into the type_die, and never returned as a result of the check_variable.

Hence we either need to add DW_TAG_typedef into __die_get_real_type, or copy sized_type into type_die. After some testing both seems to be achieving the goal.

Any thoughts on this ^ ?

reviewer
#18 lore raw

Re: [PATCH v1] perf test type profiling: Remote typedef on struct

In reply to Dmitry Dolgov
Quoted 20 lines · Dmitry Dolgov
Quoted 12 lines · Namhyung Kim

We have this:

  Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
  {
      do {
          vr_die = __die_get_real_type(vr_die, die_mem);
      } while (vr_die && dwarf_tag(vr_die) == DW_TAG_typedef);
      return vr_die;

}

Indeed, I didn't finish the analysis, fooled that the diff was solving the issue. Adding DW_TAG_typedef into __die_get_real_type still fixes the problem, but due to different reasons:

* check_variable calls __die_get_real_type directly at the beginning and stores the result in type_die, which is the result of the function.

Quoted 8 lines

* if it doesn't resolve DW_TAG_typedef, the flow goes into the die_get_real_type, which stores the result in the local variable sized_type. But it never copied into the type_die, and never returned as a result of the check_variable.

Hence we either need to add DW_TAG_typedef into __die_get_real_type, or copy sized_type into type_die. After some testing both seems to be achieving the goal.

Why is the code in check_variable resolving typedefs itself instead of using die_get_real_type instead?

Best,
Gary

#19 lore raw

Re: [PATCH v1] perf test type profiling: Remote typedef on struct

Quoted 13 lines · Gary Guo
Quoted 12 lines

Indeed, I didn't finish the analysis, fooled that the diff was solving the issue. Adding DW_TAG_typedef into __die_get_real_type still fixes the problem, but due to different reasons:

* check_variable calls __die_get_real_type directly at the beginning and stores the result in type_die, which is the result of the function.

* if it doesn't resolve DW_TAG_typedef, the flow goes into the die_get_real_type, which stores the result in the local variable sized_type. But it never copied into the type_die, and never returned as a result of the check_variable.

Quoted 6 lines
Quoted 3 lines

Hence we either need to add DW_TAG_typedef into __die_get_real_type, or copy sized_type into type_die. After some testing both seems to be achieving the goal.

Why is the code in check_variable resolving typedefs itself instead of using die_get_real_type instead?

Looks like it was done in 90d78e7b8e5 ("perf annotate-data: Show typedef names properly") for the purposes of resolving structs without a name slightly better.

maintainer Applied
#20 lore raw

Re: [PATCH v1] perf test type profiling: Remote typedef on struct

In reply to Ian Rogers
Quoted 5 lines · Ian Rogers

The typedef creates an issue where the struct or the typedef may appear in the output and cause the "perf data type profiling tests" to fail. Let's remove the typedef to keep the test passing.

Applied to perf-tools-next, thanks!

Best regards,
Namhyung

#21 lore raw

Re: [PATCH v1] perf test type profiling: Remote typedef on struct

Quoted 7 lines · Namhyung Kim
Quoted 5 lines · Ian Rogers

The typedef creates an issue where the struct or the typedef may appear in the output and cause the "perf data type profiling tests" to fail. Let's remove the typedef to keep the test passing.

Applied to perf-tools-next, thanks!

I applied this to perf-tools as well, for now just at tmp.perf-tools tho.

- Arnaldo

maintainer Applied
#22 lore raw
In reply to Dmitrii Dolgov
Quoted 6 lines · Dmitrii Dolgov

Add shell tests for data type profiling, including C and rust.

To support the rust test, a new workload with rust code was introduced. To build it only when rust is actually available, also add a feature test for rust compiler and guard everything behind it.

Quoted 6 lines

Changes in v2:
- Switched to use mem record.
- Fixed the build issue with a custom output.
- Separated patches for the workload and tests.
- Added new test for C code.
- Added a feature test for rust compiler.

Thanks, tested it, everything working as advertised:

⬢ [acme@toolbx perf-tools-next]$ m
make: Entering directory '/home/acme/git/linux/tools/perf'
BUILD: Doing 'make -j32' parallel build
Warning: Kernel ABI header differences:
diff -u tools/arch/arm64/include/asm/cputype.h arch/arm64/include/asm/cputype.h diff -u tools/perf/arch/s390/entry/syscalls/syscall.tbl arch/s390/kernel/syscalls/syscall.tbl

Auto-detecting system features:

...                  libdw: [ on  ]
...                  glibc: [ on  ]
...                 libelf: [ on  ]
...                libnuma: [ on  ]
... numa_num_possible_cpus: [ on  ]
...              libpython: [ on  ]
...            libcapstone: [ on  ]
...              llvm-perf: [ on  ]
...                   zlib: [ on  ]
...                   lzma: [ on  ]
...                    bpf: [ on  ]
...                 libaio: [ on  ]
...                libzstd: [ on  ]
...             libopenssl: [ on  ]
...                   rust: [ on  ] <-------------------------

INSTALL libsubcmd_headers
<SNIP>

⬢ [acme@toolbx perf-tools-next]$ perf record perf test -w code_with_type
[ perf record: Woken up 2 times to write data ]
[ perf record: Captured and wrote 0.162 MB perf.data (4036 samples) ]
⬢ [acme@toolbx perf-tools-next]$ perf report --stdio --dso perf -s srcfile,srcline | head -20 # To display the perf.data header info, please use --header/--header-only options. #
#
# Total Lost Samples: 0
#
# Samples: 4K of event 'cpu/cycles/Pu'
# Event count (approx.): 5337677712
#

# Overhead  Source File        Source:Line
# ........  .................  ..............................................................
#
    25.53%  cmp.rs             cmp.rs:1903
    16.66%  code_with_type.rs  code_with_type.rs:15
    14.48%  ub_checks.rs       ub_checks.rs:68
    11.16%  range.rs           range.rs:204
     6.39%  range.rs           range.rs:0
     4.61%  range.rs           range.rs:849
     3.78%  code_with_type.rs  code_with_type.rs:13
     3.51%  code_with_type.rs  code_with_type.rs:21
     2.89%  range.rs           range.rs:764

⬢ [acme@toolbx perf-tools-next]$ grep -m1 'model name' /proc/cpuinfo
model name : AMD Ryzen 9 9950X3D 16-Core Processor
⬢ [acme@toolbx perf-tools-next]$ uname -a
Linux toolbx 6.18.8-200.fc43.x86_64 #1 SMP PREEMPT_DYNAMIC Fri Jan 30 20:23:28 UTC 2026 x86_64 GNU/Linux ⬢ [acme@toolbx perf-tools-next]$

  root@number:~# perf test 'perf data type profiling tests'
   83: perf data type profiling tests                                  : Ok
  root@number:~# perf test -vv 'perf data type profiling tests'
   83: perf data type profiling tests:
  --- start ---
  test child forked, pid 125028
  Basic Rust perf annotate test
  Basic annotate test [Success]
  Pipe Rust perf annotate test
  Pipe annotate test [Success]
  Basic C perf annotate test
  Basic annotate test [Success]
  Pipe C perf annotate test
  Pipe annotate test [Success]
  ---- end(0) ----
   83: perf data type profiling tests                                  : Ok
  root@number:~#

Thanks, applied to perf-tools-next,

- Arnaldo

- Arnaldo

Quoted 31 lines

Dmitrii Dolgov (4):
tools/build: Add a feature test for rust compiler perf test workload: Add code_with_type test workload perf tests: Test annotate with data type profiling and rust perf tests: Test annotate with data type profiling and C

 tools/build/Makefile.build                    | 14 ++++
 tools/build/Makefile.feature                  |  6 +-
 tools/build/feature/Makefile                  |  7 ++
 tools/build/feature/test-rust.rs              |  4 +
 tools/perf/Makefile.config                    | 11 +++
 tools/perf/Makefile.perf                      |  2 +-
 tools/perf/builtin-check.c                    |  1 +
 tools/perf/tests/builtin-test.c               |  4 +
 tools/perf/tests/shell/data_type_profiling.sh | 84 +++++++++++++++++++
 tools/perf/tests/tests.h                      |  4 +
 tools/perf/tests/workloads/Build              |  5 ++
 tools/perf/tests/workloads/code_with_type.c   | 46 ++++++++++
 tools/perf/tests/workloads/code_with_type.rs  | 23 +++++
 tools/scripts/Makefile.include                |  2 +
 14 files changed, 210 insertions(+), 3 deletions(-)
 create mode 100644 tools/build/feature/test-rust.rs
 create mode 100755 tools/perf/tests/shell/data_type_profiling.sh
 create mode 100644 tools/perf/tests/workloads/code_with_type.c
 create mode 100644 tools/perf/tests/workloads/code_with_type.rs

base-commit: 36a1b0061a584430277861fe5d8bd107aef26137

Signature · 1 line
2.52.0
#23 lore raw

Rust data-type profiling working in perf was: Re: [RFC PATCH v2 0/4] Test annotate with data type profiling

Quoted 7 lines · Arnaldo Carvalho de Melo
Quoted 5 lines · Dmitrii Dolgov

Add shell tests for data type profiling, including C and rust.

To support the rust test, a new workload with rust code was introduced. To build it only when rust is actually available, also add a feature test for rust compiler and guard everything behind it.

Quoted 8 lines
Quoted 6 lines

Changes in v2:
- Switched to use mem record.
- Fixed the build issue with a custom output.
- Separated patches for the workload and tests.
- Added new test for C code.
- Added a feature test for rust compiler.

Thanks, tested it, everything working as advertised:

This is an interesting piece of news, so CCing to lkml and Miguel as well, please CC that list in the future.

Also please consider adding examples of output of such new features, so that people can quickly see it in action, data-type profiling for Rust seems interesting, right? :-)

- Arnaldo

Quoted 109 lines

⬢ [acme@toolbx perf-tools-next]$ m
make: Entering directory '/home/acme/git/linux/tools/perf'
BUILD: Doing 'make -j32' parallel build
Warning: Kernel ABI header differences:
diff -u tools/arch/arm64/include/asm/cputype.h arch/arm64/include/asm/cputype.h diff -u tools/perf/arch/s390/entry/syscalls/syscall.tbl arch/s390/kernel/syscalls/syscall.tbl

Auto-detecting system features:

...                  libdw: [ on  ]
...                  glibc: [ on  ]
...                 libelf: [ on  ]
...                libnuma: [ on  ]
... numa_num_possible_cpus: [ on  ]
...              libpython: [ on  ]
...            libcapstone: [ on  ]
...              llvm-perf: [ on  ]
...                   zlib: [ on  ]
...                   lzma: [ on  ]
...                    bpf: [ on  ]
...                 libaio: [ on  ]
...                libzstd: [ on  ]
...             libopenssl: [ on  ]
...                   rust: [ on  ] <-------------------------

INSTALL libsubcmd_headers
<SNIP>

⬢ [acme@toolbx perf-tools-next]$ perf record perf test -w code_with_type
[ perf record: Woken up 2 times to write data ]
[ perf record: Captured and wrote 0.162 MB perf.data (4036 samples) ]
⬢ [acme@toolbx perf-tools-next]$ perf report --stdio --dso perf -s srcfile,srcline | head -20 # To display the perf.data header info, please use --header/--header-only options. #
#
# Total Lost Samples: 0
#
# Samples: 4K of event 'cpu/cycles/Pu'
# Event count (approx.): 5337677712
#

# Overhead  Source File        Source:Line
# ........  .................  ..............................................................
#
    25.53%  cmp.rs             cmp.rs:1903
    16.66%  code_with_type.rs  code_with_type.rs:15
    14.48%  ub_checks.rs       ub_checks.rs:68
    11.16%  range.rs           range.rs:204
     6.39%  range.rs           range.rs:0
     4.61%  range.rs           range.rs:849
     3.78%  code_with_type.rs  code_with_type.rs:13
     3.51%  code_with_type.rs  code_with_type.rs:21
     2.89%  range.rs           range.rs:764

⬢ [acme@toolbx perf-tools-next]$ grep -m1 'model name' /proc/cpuinfo
model name : AMD Ryzen 9 9950X3D 16-Core Processor
⬢ [acme@toolbx perf-tools-next]$ uname -a
Linux toolbx 6.18.8-200.fc43.x86_64 #1 SMP PREEMPT_DYNAMIC Fri Jan 30 20:23:28 UTC 2026 x86_64 GNU/Linux ⬢ [acme@toolbx perf-tools-next]$

  root@number:~# perf test 'perf data type profiling tests'
   83: perf data type profiling tests                                  : Ok
  root@number:~# perf test -vv 'perf data type profiling tests'
   83: perf data type profiling tests:
  --- start ---
  test child forked, pid 125028
  Basic Rust perf annotate test
  Basic annotate test [Success]
  Pipe Rust perf annotate test
  Pipe annotate test [Success]
  Basic C perf annotate test
  Basic annotate test [Success]
  Pipe C perf annotate test
  Pipe annotate test [Success]
  ---- end(0) ----
   83: perf data type profiling tests                                  : Ok
  root@number:~#

Thanks, applied to perf-tools-next,

- Arnaldo

Quoted 30 lines

Dmitrii Dolgov (4):
tools/build: Add a feature test for rust compiler perf test workload: Add code_with_type test workload perf tests: Test annotate with data type profiling and rust perf tests: Test annotate with data type profiling and C

 tools/build/Makefile.build                    | 14 ++++
 tools/build/Makefile.feature                  |  6 +-
 tools/build/feature/Makefile                  |  7 ++
 tools/build/feature/test-rust.rs              |  4 +
 tools/perf/Makefile.config                    | 11 +++
 tools/perf/Makefile.perf                      |  2 +-
 tools/perf/builtin-check.c                    |  1 +
 tools/perf/tests/builtin-test.c               |  4 +
 tools/perf/tests/shell/data_type_profiling.sh | 84 +++++++++++++++++++
 tools/perf/tests/tests.h                      |  4 +
 tools/perf/tests/workloads/Build              |  5 ++
 tools/perf/tests/workloads/code_with_type.c   | 46 ++++++++++
 tools/perf/tests/workloads/code_with_type.rs  | 23 +++++
 tools/scripts/Makefile.include                |  2 +
 14 files changed, 210 insertions(+), 3 deletions(-)
 create mode 100644 tools/build/feature/test-rust.rs
 create mode 100755 tools/perf/tests/shell/data_type_profiling.sh
 create mode 100644 tools/perf/tests/workloads/code_with_type.c
 create mode 100644 tools/perf/tests/workloads/code_with_type.rs

base-commit: 36a1b0061a584430277861fe5d8bd107aef26137

Signature · 1 line
2.52.0
#24 lore raw

Re: Rust data-type profiling working in perf was: Re: [RFC PATCH v2 0/4] Test annotate with data type profiling

Quoted 7 lines · Arnaldo Carvalho de Melo

This is an interesting piece of news, so CCing to lkml and Miguel as well, please CC that list in the future.

Also please consider adding examples of output of such new features, so that people can quickly see it in action, data-type profiling for Rust seems interesting, right? :-)

Thanks Arnaldo for the ping!

Cc'ing rust-for-linux as well.

Thread at https://lore.kernel.org/all/[email protected]/

Cheers,
Miguel

#25 lore raw

Re: Rust data-type profiling working in perf was: Re: [RFC PATCH v2 0/4] Test annotate with data type profiling

In reply to Arnaldo Carvalho de Melo
Quoted 5 lines · Arnaldo Carvalho de Melo

Also please consider adding examples of output of such new features, so that people can quickly see it in action, data-type profiling for Rust seems interesting, right? :-)

Agree :) What would be the right place to put such examples, somewhere in the documentation?

maintainer
#26 lore raw

Re: Rust data-type profiling working in perf was: Re: [RFC PATCH v2 0/4] Test annotate with data type profiling

Quoted 8 lines · Dmitry Dolgov
Quoted 5 lines · Arnaldo Carvalho de Melo

Also please consider adding examples of output of such new features, so that people can quickly see it in action, data-type profiling for Rust seems interesting, right? :-)

Agree :) What would be the right place to put such examples, somewhere in the documentation?

Yep, please add it to the documentation.

Thanks,
Namhyung