Skip to contentSkip to conversation

perf beauty: Introduce scraper script for 'clone' syscall 'flags' argument

PATCH In review Arnaldo Carvalho de Melo 2 participants 3 messages Mar 19, 2024 perf events

lore
Share & tools
lore

Arnaldo Carvalho de Melo

author maintainer
#0 lore raw

PATCH 1/1 perf beauty: Introduce scraper script for 'clone' syscall 'flags' argument

It was using the first variation on producing a string representation for a binary flag, one that used the copy of uapi/linux/sched.h with preprocessor tricks that had to be updated everytime a new flag was introduced.

Use the more recent scraper script + strarray + strarray__scnprintf_flags() combo.

  $ tools/perf/trace/beauty/clone.sh | head -5
  static const char *clone_flags[] = {
  	[ilog2(0x00000100) + 1] = "VM",
  	[ilog2(0x00000200) + 1] = "FS",
  	[ilog2(0x00000400) + 1] = "FILES",
  	[ilog2(0x00000800) + 1] = "SIGHAND",
  $

Now we can move uapi/linux/sched.h from tools/include/, that is used for building perf to the scrap only directory tools/perf/trace/beauty/include.

Cc Adrian Hunter <[email protected]>
Cc Ian Rogers <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
---
 tools/perf/Makefile.perf                      | 14 ++++--
 tools/perf/check-headers.sh                   |  2 +-
 tools/perf/trace/beauty/clone.c               | 46 ++-----------------
 tools/perf/trace/beauty/clone.sh              | 17 +++++++
 .../trace/beauty}/include/uapi/linux/sched.h  |  0
 5 files changed, 34 insertions(+), 45 deletions(-)
 create mode 100755 tools/perf/trace/beauty/clone.sh
 rename tools/{ => perf/trace/beauty}/include/uapi/linux/sched.h (100%)
tools/perf/Makefile.perf +11 -3
index f5654d06e31385b7..ccd2dcbc64f720d2 100644
@@ -485,13 +485,20 @@ x86_arch_asm_dir := $(srctree)/tools/arch/x86/include/asm/
 
 beauty_outdir := $(OUTPUT)trace/beauty/generated
 beauty_ioctl_outdir := $(beauty_outdir)/ioctl
-drm_ioctl_array := $(beauty_ioctl_outdir)/drm_ioctl_array.c
-drm_hdr_dir := $(srctree)/tools/include/uapi/drm
-drm_ioctl_tbl := $(srctree)/tools/perf/trace/beauty/drm_ioctl.sh
 
 # Create output directory if not already present
 $(shell [ -d '$(beauty_ioctl_outdir)' ] || mkdir -p '$(beauty_ioctl_outdir)')
 
+clone_flags_array := $(beauty_outdir)/clone_flags_array.c
+clone_flags_tbl := $(srctree)/tools/perf/trace/beauty/clone.sh
+
+$(clone_flags_array): $(beauty_uapi_linux_dir)/sched.h $(clone_flags_tbl)
+	$(Q)$(SHELL) '$(clone_flags_tbl)' $(beauty_uapi_linux_dir) > $@
+
+drm_ioctl_array := $(beauty_ioctl_outdir)/drm_ioctl_array.c
+drm_hdr_dir := $(srctree)/tools/include/uapi/drm
+drm_ioctl_tbl := $(srctree)/tools/perf/trace/beauty/drm_ioctl.sh
+
 $(drm_ioctl_array): $(drm_hdr_dir)/drm.h $(drm_hdr_dir)/i915_drm.h $(drm_ioctl_tbl)
 	$(Q)$(SHELL) '$(drm_ioctl_tbl)' $(drm_hdr_dir) > $@
 
@@ -765,6 +772,7 @@ build-dir   = $(or $(__build-dir),.)
 
 prepare: $(OUTPUT)PERF-VERSION-FILE $(OUTPUT)common-cmds.h archheaders \
 	arm64-sysreg-defs \
+	$(clone_flags_array) \
 	$(drm_ioctl_array) \
 	$(fadvise_advice_array) \
 	$(fsconfig_arrays) \
tools/perf/check-headers.sh +1 -1
index 859cd6f35b0ac9b1..413c9b747216020f 100755
@@ -15,7 +15,6 @@ FILES=(
   "include/uapi/linux/kvm.h"
   "include/uapi/linux/in.h"
   "include/uapi/linux/perf_event.h"
-  "include/uapi/linux/sched.h"
   "include/uapi/linux/seccomp.h"
   "include/uapi/linux/vhost.h"
   "include/linux/bits.h"
@@ -93,6 +92,7 @@ BEAUTY_FILES=(
   "include/uapi/linux/fs.h"
   "include/uapi/linux/mount.h"
   "include/uapi/linux/prctl.h"
+  "include/uapi/linux/sched.h"
   "include/uapi/linux/usbdevice_fs.h"
   "include/uapi/sound/asound.h"
 )
tools/perf/trace/beauty/clone.c +5 -41

57 lines not on this page · raw message

tools/perf/trace/beauty/clone.sh new file +17 -0
new file mode 100755
index 0000000000000000..18b6c0d75693721d
@@ -0,0 +1,17 @@
+#!/bin/sh
+# SPDX-License-Identifier: LGPL-2.1
+
+if [ $# -ne 1 ] ; then
+	beauty_uapi_linux_dir=tools/perf/trace/beauty/include/uapi/linux/
+else
+	beauty_uapi_linux_dir=$1
+fi
+
+linux_sched=${beauty_uapi_linux_dir}/sched.h
+
+printf "static const char *clone_flags[] = {\n"
+regex='^[[:space:]]*#[[:space:]]*define[[:space:]]+CLONE_([^_]+[[:alnum:]_]+)[[:space:]]+(0x[[:xdigit:]]+)[[:space:]]*.*'
+grep -E $regex ${linux_sched} | \
+	sed -r "s/$regex/\2 \1/g"	| \
+	xargs printf "\t[ilog2(%s) + 1] = \"%s\",\n"
+printf "};\n"
tools/perf/trace/beauty/include/uapi/linux/sched.h renamed from tools/include/uapi/linux/sched.h +0 -0
similarity index 100%
rename from tools/include/uapi/linux/sched.h
rename to tools/perf/trace/beauty/include/uapi/linux/sched.h
Signature · 1 line
2.44.0
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 16 lines · Arnaldo Carvalho de Melo

It was using the first variation on producing a string representation for a binary flag, one that used the copy of uapi/linux/sched.h with preprocessor tricks that had to be updated everytime a new flag was introduced.

Use the more recent scraper script + strarray + strarray__scnprintf_flags() combo.

  $ tools/perf/trace/beauty/clone.sh | head -5
  static const char *clone_flags[] = {
        [ilog2(0x00000100) + 1] = "VM",
        [ilog2(0x00000200) + 1] = "FS",
        [ilog2(0x00000400) + 1] = "FILES",
        [ilog2(0x00000800) + 1] = "SIGHAND",
  $
Quoted 2 lines

Now we can move uapi/linux/sched.h from tools/include/, that is used for building perf to the scrap only directory tools/perf/trace/beauty/include.

nit: scrape?

Thanks,
Ian

Quoted 163 lines
Cc Adrian Hunter <[email protected]>
Cc Ian Rogers <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
---
 tools/perf/Makefile.perf                      | 14 ++++--
 tools/perf/check-headers.sh                   |  2 +-
 tools/perf/trace/beauty/clone.c               | 46 ++-----------------
 tools/perf/trace/beauty/clone.sh              | 17 +++++++
 .../trace/beauty}/include/uapi/linux/sched.h  |  0
 5 files changed, 34 insertions(+), 45 deletions(-)
 create mode 100755 tools/perf/trace/beauty/clone.sh
 rename tools/{ => perf/trace/beauty}/include/uapi/linux/sched.h (100%)
tools/perf/Makefile.perf +11 -3
index f5654d06e31385b7..ccd2dcbc64f720d2 100644
@@ -485,13 +485,20 @@ x86_arch_asm_dir := $(srctree)/tools/arch/x86/include/asm/
 
 beauty_outdir := $(OUTPUT)trace/beauty/generated
 beauty_ioctl_outdir := $(beauty_outdir)/ioctl
-drm_ioctl_array := $(beauty_ioctl_outdir)/drm_ioctl_array.c
-drm_hdr_dir := $(srctree)/tools/include/uapi/drm
-drm_ioctl_tbl := $(srctree)/tools/perf/trace/beauty/drm_ioctl.sh
 
 # Create output directory if not already present
 $(shell [ -d '$(beauty_ioctl_outdir)' ] || mkdir -p '$(beauty_ioctl_outdir)')
 
+clone_flags_array := $(beauty_outdir)/clone_flags_array.c
+clone_flags_tbl := $(srctree)/tools/perf/trace/beauty/clone.sh
+
+$(clone_flags_array): $(beauty_uapi_linux_dir)/sched.h $(clone_flags_tbl)
+       $(Q)$(SHELL) '$(clone_flags_tbl)' $(beauty_uapi_linux_dir) > $@
+
+drm_ioctl_array := $(beauty_ioctl_outdir)/drm_ioctl_array.c
+drm_hdr_dir := $(srctree)/tools/include/uapi/drm
+drm_ioctl_tbl := $(srctree)/tools/perf/trace/beauty/drm_ioctl.sh
+
 $(drm_ioctl_array): $(drm_hdr_dir)/drm.h $(drm_hdr_dir)/i915_drm.h $(drm_ioctl_tbl)
        $(Q)$(SHELL) '$(drm_ioctl_tbl)' $(drm_hdr_dir) > $@
 
@@ -765,6 +772,7 @@ build-dir   = $(or $(__build-dir),.)
 
 prepare: $(OUTPUT)PERF-VERSION-FILE $(OUTPUT)common-cmds.h archheaders \
        arm64-sysreg-defs \
+       $(clone_flags_array) \
        $(drm_ioctl_array) \
        $(fadvise_advice_array) \
        $(fsconfig_arrays) \
tools/perf/check-headers.sh +1 -1
index 859cd6f35b0ac9b1..413c9b747216020f 100755
@@ -15,7 +15,6 @@ FILES=(
   "include/uapi/linux/kvm.h"
   "include/uapi/linux/in.h"
   "include/uapi/linux/perf_event.h"
-  "include/uapi/linux/sched.h"
   "include/uapi/linux/seccomp.h"
   "include/uapi/linux/vhost.h"
   "include/linux/bits.h"
@@ -93,6 +92,7 @@ BEAUTY_FILES=(
   "include/uapi/linux/fs.h"
   "include/uapi/linux/mount.h"
   "include/uapi/linux/prctl.h"
+  "include/uapi/linux/sched.h"
   "include/uapi/linux/usbdevice_fs.h"
   "include/uapi/sound/asound.h"
 )
tools/perf/trace/beauty/clone.c +5 -41
index f4db894e0af6d14b..c9fa8f7e82b909fb 100644
@@ -7,52 +7,16 @@
 
 #include "trace/beauty/beauty.h"
 #include <linux/kernel.h>
+#include <linux/log2.h>
 #include <sys/types.h>
-#include <uapi/linux/sched.h>
+#include <sched.h>
 
 static size_t clone__scnprintf_flags(unsigned long flags, char *bf, size_t size, bool show_prefix)
 {
-       const char *prefix = "CLONE_";
-       int printed = 0;
+#include "trace/beauty/generated/clone_flags_array.c"
+       static DEFINE_STRARRAY(clone_flags, "CLONE_");
 
-#define        P_FLAG(n) \
-       if (flags & CLONE_##n) { \
-               printed += scnprintf(bf + printed, size - printed, "%s%s%s", printed ? "|" : "", show_prefix ? prefix : "", #n); \
-               flags &= ~CLONE_##n; \
-       }
-
-       P_FLAG(VM);
-       P_FLAG(FS);
-       P_FLAG(FILES);
-       P_FLAG(SIGHAND);
-       P_FLAG(PIDFD);
-       P_FLAG(PTRACE);
-       P_FLAG(VFORK);
-       P_FLAG(PARENT);
-       P_FLAG(THREAD);
-       P_FLAG(NEWNS);
-       P_FLAG(SYSVSEM);
-       P_FLAG(SETTLS);
-       P_FLAG(PARENT_SETTID);
-       P_FLAG(CHILD_CLEARTID);
-       P_FLAG(DETACHED);
-       P_FLAG(UNTRACED);
-       P_FLAG(CHILD_SETTID);
-       P_FLAG(NEWCGROUP);
-       P_FLAG(NEWUTS);
-       P_FLAG(NEWIPC);
-       P_FLAG(NEWUSER);
-       P_FLAG(NEWPID);
-       P_FLAG(NEWNET);
-       P_FLAG(IO);
-       P_FLAG(CLEAR_SIGHAND);
-       P_FLAG(INTO_CGROUP);
-#undef P_FLAG
-
-       if (flags)
-               printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
-
-       return printed;
+       return strarray__scnprintf_flags(&strarray__clone_flags, bf, size, show_prefix, flags);
 }
 
 size_t syscall_arg__scnprintf_clone_flags(char *bf, size_t size, struct syscall_arg *arg)
tools/perf/trace/beauty/clone.sh new file +17 -0
new file mode 100755
index 0000000000000000..18b6c0d75693721d
@@ -0,0 +1,17 @@
+#!/bin/sh
+# SPDX-License-Identifier: LGPL-2.1
+
+if [ $# -ne 1 ] ; then
+       beauty_uapi_linux_dir=tools/perf/trace/beauty/include/uapi/linux/
+else
+       beauty_uapi_linux_dir=$1
+fi
+
+linux_sched=${beauty_uapi_linux_dir}/sched.h
+
+printf "static const char *clone_flags[] = {\n"
+regex='^[[:space:]]*#[[:space:]]*define[[:space:]]+CLONE_([^_]+[[:alnum:]_]+)[[:space:]]+(0x[[:xdigit:]]+)[[:space:]]*.*'
+grep -E $regex ${linux_sched} | \
+       sed -r "s/$regex/\2 \1/g"       | \
+       xargs printf "\t[ilog2(%s) + 1] = \"%s\",\n"
+printf "};\n"
tools/perf/trace/beauty/include/uapi/linux/sched.h renamed from tools/include/uapi/linux/sched.h +0 -1
similarity index 100%
rename from tools/include/uapi/linux/sched.h
rename to tools/perf/trace/beauty/include/uapi/linux/sched.h
--

2.44.0

author maintainer
#2 lore raw
Quoted 18 lines · Ian Rogers
Quoted 16 lines · Arnaldo Carvalho de Melo

It was using the first variation on producing a string representation for a binary flag, one that used the copy of uapi/linux/sched.h with preprocessor tricks that had to be updated everytime a new flag was introduced.

Use the more recent scraper script + strarray + strarray__scnprintf_flags() combo.

  $ tools/perf/trace/beauty/clone.sh | head -5
  static const char *clone_flags[] = {
        [ilog2(0x00000100) + 1] = "VM",
        [ilog2(0x00000200) + 1] = "FS",
        [ilog2(0x00000400) + 1] = "FILES",
        [ilog2(0x00000800) + 1] = "SIGHAND",
  $
Quoted 4 lines
Quoted 2 lines

Now we can move uapi/linux/sched.h from tools/include/, that is used for building perf to the scrap only directory tools/perf/trace/beauty/include.

nit: scrape?

I learn this at some point, keep trying :-)

Quoted 1 line

Thanks!

- Arnaldo

Quoted 166 lines

Thanks,
Ian

Quoted 163 lines
Cc Adrian Hunter <[email protected]>
Cc Ian Rogers <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
---
 tools/perf/Makefile.perf                      | 14 ++++--
 tools/perf/check-headers.sh                   |  2 +-
 tools/perf/trace/beauty/clone.c               | 46 ++-----------------
 tools/perf/trace/beauty/clone.sh              | 17 +++++++
 .../trace/beauty}/include/uapi/linux/sched.h  |  0
 5 files changed, 34 insertions(+), 45 deletions(-)
 create mode 100755 tools/perf/trace/beauty/clone.sh
 rename tools/{ => perf/trace/beauty}/include/uapi/linux/sched.h (100%)
tools/perf/Makefile.perf +11 -3
index f5654d06e31385b7..ccd2dcbc64f720d2 100644
@@ -485,13 +485,20 @@ x86_arch_asm_dir := $(srctree)/tools/arch/x86/include/asm/
 
 beauty_outdir := $(OUTPUT)trace/beauty/generated
 beauty_ioctl_outdir := $(beauty_outdir)/ioctl
-drm_ioctl_array := $(beauty_ioctl_outdir)/drm_ioctl_array.c
-drm_hdr_dir := $(srctree)/tools/include/uapi/drm
-drm_ioctl_tbl := $(srctree)/tools/perf/trace/beauty/drm_ioctl.sh
 
 # Create output directory if not already present
 $(shell [ -d '$(beauty_ioctl_outdir)' ] || mkdir -p '$(beauty_ioctl_outdir)')
 
+clone_flags_array := $(beauty_outdir)/clone_flags_array.c
+clone_flags_tbl := $(srctree)/tools/perf/trace/beauty/clone.sh
+
+$(clone_flags_array): $(beauty_uapi_linux_dir)/sched.h $(clone_flags_tbl)
+       $(Q)$(SHELL) '$(clone_flags_tbl)' $(beauty_uapi_linux_dir) > $@
+
+drm_ioctl_array := $(beauty_ioctl_outdir)/drm_ioctl_array.c
+drm_hdr_dir := $(srctree)/tools/include/uapi/drm
+drm_ioctl_tbl := $(srctree)/tools/perf/trace/beauty/drm_ioctl.sh
+
 $(drm_ioctl_array): $(drm_hdr_dir)/drm.h $(drm_hdr_dir)/i915_drm.h $(drm_ioctl_tbl)
        $(Q)$(SHELL) '$(drm_ioctl_tbl)' $(drm_hdr_dir) > $@
 
@@ -765,6 +772,7 @@ build-dir   = $(or $(__build-dir),.)
 
 prepare: $(OUTPUT)PERF-VERSION-FILE $(OUTPUT)common-cmds.h archheaders \
        arm64-sysreg-defs \
+       $(clone_flags_array) \
        $(drm_ioctl_array) \
        $(fadvise_advice_array) \
        $(fsconfig_arrays) \
tools/perf/check-headers.sh +1 -1
index 859cd6f35b0ac9b1..413c9b747216020f 100755
@@ -15,7 +15,6 @@ FILES=(
   "include/uapi/linux/kvm.h"
   "include/uapi/linux/in.h"
   "include/uapi/linux/perf_event.h"
-  "include/uapi/linux/sched.h"
   "include/uapi/linux/seccomp.h"
   "include/uapi/linux/vhost.h"
   "include/linux/bits.h"
@@ -93,6 +92,7 @@ BEAUTY_FILES=(
   "include/uapi/linux/fs.h"
   "include/uapi/linux/mount.h"
   "include/uapi/linux/prctl.h"
+  "include/uapi/linux/sched.h"
   "include/uapi/linux/usbdevice_fs.h"
   "include/uapi/sound/asound.h"
 )
tools/perf/trace/beauty/clone.c +5 -41
index f4db894e0af6d14b..c9fa8f7e82b909fb 100644
@@ -7,52 +7,16 @@
 
 #include "trace/beauty/beauty.h"
 #include <linux/kernel.h>
+#include <linux/log2.h>
 #include <sys/types.h>
-#include <uapi/linux/sched.h>
+#include <sched.h>
 
 static size_t clone__scnprintf_flags(unsigned long flags, char *bf, size_t size, bool show_prefix)
 {
-       const char *prefix = "CLONE_";
-       int printed = 0;
+#include "trace/beauty/generated/clone_flags_array.c"
+       static DEFINE_STRARRAY(clone_flags, "CLONE_");
 
-#define        P_FLAG(n) \
-       if (flags & CLONE_##n) { \
-               printed += scnprintf(bf + printed, size - printed, "%s%s%s", printed ? "|" : "", show_prefix ? prefix : "", #n); \
-               flags &= ~CLONE_##n; \
-       }
-
-       P_FLAG(VM);
-       P_FLAG(FS);
-       P_FLAG(FILES);
-       P_FLAG(SIGHAND);
-       P_FLAG(PIDFD);
-       P_FLAG(PTRACE);
-       P_FLAG(VFORK);
-       P_FLAG(PARENT);
-       P_FLAG(THREAD);
-       P_FLAG(NEWNS);
-       P_FLAG(SYSVSEM);
-       P_FLAG(SETTLS);
-       P_FLAG(PARENT_SETTID);
-       P_FLAG(CHILD_CLEARTID);
-       P_FLAG(DETACHED);
-       P_FLAG(UNTRACED);
-       P_FLAG(CHILD_SETTID);
-       P_FLAG(NEWCGROUP);
-       P_FLAG(NEWUTS);
-       P_FLAG(NEWIPC);
-       P_FLAG(NEWUSER);
-       P_FLAG(NEWPID);
-       P_FLAG(NEWNET);
-       P_FLAG(IO);
-       P_FLAG(CLEAR_SIGHAND);
-       P_FLAG(INTO_CGROUP);
-#undef P_FLAG
-
-       if (flags)
-               printed += scnprintf(bf + printed, size - printed, "%s%#x", printed ? "|" : "", flags);
-
-       return printed;
+       return strarray__scnprintf_flags(&strarray__clone_flags, bf, size, show_prefix, flags);
 }
 
 size_t syscall_arg__scnprintf_clone_flags(char *bf, size_t size, struct syscall_arg *arg)
tools/perf/trace/beauty/clone.sh new file +17 -0
new file mode 100755
index 0000000000000000..18b6c0d75693721d
@@ -0,0 +1,17 @@
+#!/bin/sh
+# SPDX-License-Identifier: LGPL-2.1
+
+if [ $# -ne 1 ] ; then
+       beauty_uapi_linux_dir=tools/perf/trace/beauty/include/uapi/linux/
+else
+       beauty_uapi_linux_dir=$1
+fi
+
+linux_sched=${beauty_uapi_linux_dir}/sched.h
+
+printf "static const char *clone_flags[] = {\n"
+regex='^[[:space:]]*#[[:space:]]*define[[:space:]]+CLONE_([^_]+[[:alnum:]_]+)[[:space:]]+(0x[[:xdigit:]]+)[[:space:]]*.*'
+grep -E $regex ${linux_sched} | \
+       sed -r "s/$regex/\2 \1/g"       | \
+       xargs printf "\t[ilog2(%s) + 1] = \"%s\",\n"
+printf "};\n"
tools/perf/trace/beauty/include/uapi/linux/sched.h renamed from tools/include/uapi/linux/sched.h +0 -1
similarity index 100%
rename from tools/include/uapi/linux/sched.h
rename to tools/perf/trace/beauty/include/uapi/linux/sched.h
--

2.44.0