2024-03-14 06:30:19

by Yang Jihong

[permalink] [raw]
Subject: [PATCH] perf build: Add LIBTRACEEVENT_DIR build option

Currently, when libtraceevent is not linked,
perf does not support tracepoint:

# ./perf record -e sched:sched_switch -a sleep 10
event syntax error: 'sched:sched_switch'
\___ unsupported tracepoint

libtraceevent is necessary for tracepoint support
Run 'perf list' for a list of valid events

Usage: perf record [<options>] [<command>]
or: perf record [<options>] -- <command> [<options>]

-e, --event <event> event selector. use 'perf list' to list available events

For cross-compilation scenario, library may not be installed in the default
system path. Based on the above requirements, add LIBTRACEEVENT_DIR build
option to support specifying path of libtraceevent.

Example:

1. Cross compile libtraceevent
# cd /opt/libtraceevent
# CROSS_COMPILE=aarch64-linux-gnu- make

2. Cross compile perf
# cd tool/perf
# make VF=1 ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- NO_LIBELF=1 LDFLAGS=--static LIBTRACEEVENT_DIR=/opt/libtraceevent
<SNIP>
Auto-detecting system features:
<SNIP>
... LIBTRACEEVENT_DIR: /opt/libtraceevent

Signed-off-by: Yang Jihong <[email protected]>
---
tools/perf/Makefile.config | 20 ++++++++++++++++----
1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/tools/perf/Makefile.config b/tools/perf/Makefile.config
index 1fe8df97fe88..7783479de691 100644
--- a/tools/perf/Makefile.config
+++ b/tools/perf/Makefile.config
@@ -182,6 +182,16 @@ endif
FEATURE_CHECK_CFLAGS-libzstd := $(LIBZSTD_CFLAGS)
FEATURE_CHECK_LDFLAGS-libzstd := $(LIBZSTD_LDFLAGS)

+# for linking with debug library, run like:
+# make DEBUG=1 LIBTRACEEVENT_DIR=/opt/libtraceevent/
+TRACEEVENTLIBS := -ltraceevent
+ifdef LIBTRACEEVENT_DIR
+ LIBTRACEEVENT_CFLAGS := -I$(LIBTRACEEVENT_DIR)/include
+ LIBTRACEEVENT_LDFLAGS := -L$(LIBTRACEEVENT_DIR)/lib
+endif
+FEATURE_CHECK_CFLAGS-libtraceevent := $(LIBTRACEEVENT_CFLAGS)
+FEATURE_CHECK_LDFLAGS-libtraceevent := $(LIBTRACEEVENT_LDFLAGS) $(TRACEEVENTLIBS)
+
FEATURE_CHECK_CFLAGS-bpf = -I. -I$(srctree)/tools/include -I$(srctree)/tools/arch/$(SRCARCH)/include/uapi -I$(srctree)/tools/include/uapi
# include ARCH specific config
-include $(src-perf)/arch/$(SRCARCH)/Makefile
@@ -1165,9 +1175,10 @@ endif
ifneq ($(NO_LIBTRACEEVENT),1)
$(call feature_check,libtraceevent)
ifeq ($(feature-libtraceevent), 1)
- CFLAGS += -DHAVE_LIBTRACEEVENT
- EXTLIBS += -ltraceevent
- LIBTRACEEVENT_VERSION := $(shell $(PKG_CONFIG) --modversion libtraceevent)
+ CFLAGS += -DHAVE_LIBTRACEEVENT $(LIBTRACEEVENT_CFLAGS)
+ LDFLAGS += $(LIBTRACEEVENT_LDFLAGS)
+ EXTLIBS += ${TRACEEVENTLIBS}
+ LIBTRACEEVENT_VERSION := $(shell PKG_CONFIG_PATH=$(LIBTRACEEVENT_DIR) $(PKG_CONFIG) --modversion libtraceevent)
LIBTRACEEVENT_VERSION_1 := $(word 1, $(subst ., ,$(LIBTRACEEVENT_VERSION)))
LIBTRACEEVENT_VERSION_2 := $(word 2, $(subst ., ,$(LIBTRACEEVENT_VERSION)))
LIBTRACEEVENT_VERSION_3 := $(word 3, $(subst ., ,$(LIBTRACEEVENT_VERSION)))
@@ -1175,7 +1186,7 @@ ifneq ($(NO_LIBTRACEEVENT),1)
CFLAGS += -DLIBTRACEEVENT_VERSION=$(LIBTRACEEVENT_VERSION_CPP)
$(call detected,CONFIG_LIBTRACEEVENT)
else
- $(error ERROR: libtraceevent is missing. Please install libtraceevent-dev/libtraceevent-devel or build with NO_LIBTRACEEVENT=1)
+ $(error ERROR: libtraceevent is missing. Please install libtraceevent-dev/libtraceevent-devel and/or set LIBTRACEEVENT_DIR or build with NO_LIBTRACEEVENT=1)
endif

$(call feature_check,libtracefs)
@@ -1301,6 +1312,7 @@ ifeq ($(VF),1)
$(call print_var,LIBUNWIND_DIR)
$(call print_var,LIBDW_DIR)
$(call print_var,JDIR)
+ $(call print_var,LIBTRACEEVENT_DIR)

ifeq ($(dwarf-post-unwind),1)
$(call feature_print_text,"DWARF post unwind library", $(dwarf-post-unwind-text)) $(info $(MSG))
--
2.25.1



2024-03-23 19:08:28

by Ian Rogers

[permalink] [raw]
Subject: Re: [PATCH] perf build: Add LIBTRACEEVENT_DIR build option

On Wed, Mar 13, 2024 at 11:30 PM Yang Jihong <[email protected]> wrote:
>
> Currently, when libtraceevent is not linked,
> perf does not support tracepoint:
>
> # ./perf record -e sched:sched_switch -a sleep 10
> event syntax error: 'sched:sched_switch'
> \___ unsupported tracepoint
>
> libtraceevent is necessary for tracepoint support
> Run 'perf list' for a list of valid events
>
> Usage: perf record [<options>] [<command>]
> or: perf record [<options>] -- <command> [<options>]
>
> -e, --event <event> event selector. use 'perf list' to list available events
>
> For cross-compilation scenario, library may not be installed in the default
> system path. Based on the above requirements, add LIBTRACEEVENT_DIR build
> option to support specifying path of libtraceevent.
>
> Example:
>
> 1. Cross compile libtraceevent
> # cd /opt/libtraceevent
> # CROSS_COMPILE=aarch64-linux-gnu- make
>
> 2. Cross compile perf
> # cd tool/perf
> # make VF=1 ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- NO_LIBELF=1 LDFLAGS=--static LIBTRACEEVENT_DIR=/opt/libtraceevent
> <SNIP>
> Auto-detecting system features:
> <SNIP>
> ... LIBTRACEEVENT_DIR: /opt/libtraceevent
>
> Signed-off-by: Yang Jihong <[email protected]>

This all looks good to me, thanks!

Reviewed-by: Ian Rogers <[email protected]>

> ---
> tools/perf/Makefile.config | 20 ++++++++++++++++----
> 1 file changed, 16 insertions(+), 4 deletions(-)
>
> diff --git a/tools/perf/Makefile.config b/tools/perf/Makefile.config
> index 1fe8df97fe88..7783479de691 100644
> --- a/tools/perf/Makefile.config
> +++ b/tools/perf/Makefile.config
> @@ -182,6 +182,16 @@ endif
> FEATURE_CHECK_CFLAGS-libzstd := $(LIBZSTD_CFLAGS)
> FEATURE_CHECK_LDFLAGS-libzstd := $(LIBZSTD_LDFLAGS)
>
> +# for linking with debug library, run like:
> +# make DEBUG=1 LIBTRACEEVENT_DIR=/opt/libtraceevent/
> +TRACEEVENTLIBS := -ltraceevent
> +ifdef LIBTRACEEVENT_DIR
> + LIBTRACEEVENT_CFLAGS := -I$(LIBTRACEEVENT_DIR)/include
> + LIBTRACEEVENT_LDFLAGS := -L$(LIBTRACEEVENT_DIR)/lib
> +endif
> +FEATURE_CHECK_CFLAGS-libtraceevent := $(LIBTRACEEVENT_CFLAGS)
> +FEATURE_CHECK_LDFLAGS-libtraceevent := $(LIBTRACEEVENT_LDFLAGS) $(TRACEEVENTLIBS)
> +
> FEATURE_CHECK_CFLAGS-bpf = -I. -I$(srctree)/tools/include -I$(srctree)/tools/arch/$(SRCARCH)/include/uapi -I$(srctree)/tools/include/uapi
> # include ARCH specific config
> -include $(src-perf)/arch/$(SRCARCH)/Makefile
> @@ -1165,9 +1175,10 @@ endif
> ifneq ($(NO_LIBTRACEEVENT),1)
> $(call feature_check,libtraceevent)
> ifeq ($(feature-libtraceevent), 1)
> - CFLAGS += -DHAVE_LIBTRACEEVENT
> - EXTLIBS += -ltraceevent
> - LIBTRACEEVENT_VERSION := $(shell $(PKG_CONFIG) --modversion libtraceevent)
> + CFLAGS += -DHAVE_LIBTRACEEVENT $(LIBTRACEEVENT_CFLAGS)
> + LDFLAGS += $(LIBTRACEEVENT_LDFLAGS)
> + EXTLIBS += ${TRACEEVENTLIBS}
> + LIBTRACEEVENT_VERSION := $(shell PKG_CONFIG_PATH=$(LIBTRACEEVENT_DIR) $(PKG_CONFIG) --modversion libtraceevent)
> LIBTRACEEVENT_VERSION_1 := $(word 1, $(subst ., ,$(LIBTRACEEVENT_VERSION)))
> LIBTRACEEVENT_VERSION_2 := $(word 2, $(subst ., ,$(LIBTRACEEVENT_VERSION)))
> LIBTRACEEVENT_VERSION_3 := $(word 3, $(subst ., ,$(LIBTRACEEVENT_VERSION)))
> @@ -1175,7 +1186,7 @@ ifneq ($(NO_LIBTRACEEVENT),1)
> CFLAGS += -DLIBTRACEEVENT_VERSION=$(LIBTRACEEVENT_VERSION_CPP)
> $(call detected,CONFIG_LIBTRACEEVENT)
> else
> - $(error ERROR: libtraceevent is missing. Please install libtraceevent-dev/libtraceevent-devel or build with NO_LIBTRACEEVENT=1)
> + $(error ERROR: libtraceevent is missing. Please install libtraceevent-dev/libtraceevent-devel and/or set LIBTRACEEVENT_DIR or build with NO_LIBTRACEEVENT=1)
> endif
>
> $(call feature_check,libtracefs)
> @@ -1301,6 +1312,7 @@ ifeq ($(VF),1)
> $(call print_var,LIBUNWIND_DIR)
> $(call print_var,LIBDW_DIR)
> $(call print_var,JDIR)
> + $(call print_var,LIBTRACEEVENT_DIR)
>
> ifeq ($(dwarf-post-unwind),1)
> $(call feature_print_text,"DWARF post unwind library", $(dwarf-post-unwind-text)) $(info $(MSG))
> --
> 2.25.1
>

2024-04-30 17:47:07

by Ian Rogers

[permalink] [raw]
Subject: Re: [PATCH] perf build: Add LIBTRACEEVENT_DIR build option

On Sat, Mar 23, 2024 at 12:07 PM Ian Rogers <[email protected]> wrote:
>
> On Wed, Mar 13, 2024 at 11:30 PM Yang Jihong <[email protected]> wrote:
> >
> > Currently, when libtraceevent is not linked,
> > perf does not support tracepoint:
> >
> > # ./perf record -e sched:sched_switch -a sleep 10
> > event syntax error: 'sched:sched_switch'
> > \___ unsupported tracepoint
> >
> > libtraceevent is necessary for tracepoint support
> > Run 'perf list' for a list of valid events
> >
> > Usage: perf record [<options>] [<command>]
> > or: perf record [<options>] -- <command> [<options>]
> >
> > -e, --event <event> event selector. use 'perf list' to list available events
> >
> > For cross-compilation scenario, library may not be installed in the default
> > system path. Based on the above requirements, add LIBTRACEEVENT_DIR build
> > option to support specifying path of libtraceevent.
> >
> > Example:
> >
> > 1. Cross compile libtraceevent
> > # cd /opt/libtraceevent
> > # CROSS_COMPILE=aarch64-linux-gnu- make
> >
> > 2. Cross compile perf
> > # cd tool/perf
> > # make VF=1 ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- NO_LIBELF=1 LDFLAGS=--static LIBTRACEEVENT_DIR=/opt/libtraceevent
> > <SNIP>
> > Auto-detecting system features:
> > <SNIP>
> > ... LIBTRACEEVENT_DIR: /opt/libtraceevent
> >
> > Signed-off-by: Yang Jihong <[email protected]>
>
> This all looks good to me, thanks!
>
> Reviewed-by: Ian Rogers <[email protected]>
>
> > ---
> > tools/perf/Makefile.config | 20 ++++++++++++++++----
> > 1 file changed, 16 insertions(+), 4 deletions(-)
> >
> > diff --git a/tools/perf/Makefile.config b/tools/perf/Makefile.config
> > index 1fe8df97fe88..7783479de691 100644
> > --- a/tools/perf/Makefile.config
> > +++ b/tools/perf/Makefile.config
> > @@ -182,6 +182,16 @@ endif
> > FEATURE_CHECK_CFLAGS-libzstd := $(LIBZSTD_CFLAGS)
> > FEATURE_CHECK_LDFLAGS-libzstd := $(LIBZSTD_LDFLAGS)
> >
> > +# for linking with debug library, run like:
> > +# make DEBUG=1 LIBTRACEEVENT_DIR=/opt/libtraceevent/
> > +TRACEEVENTLIBS := -ltraceevent
> > +ifdef LIBTRACEEVENT_DIR
> > + LIBTRACEEVENT_CFLAGS := -I$(LIBTRACEEVENT_DIR)/include
> > + LIBTRACEEVENT_LDFLAGS := -L$(LIBTRACEEVENT_DIR)/lib
> > +endif

I'm finding to test a libtraceevent asan build on a system that has
libtraceevent installed, I need to carry this change:
```
diff --git a/tools/perf/Makefile.config b/tools/perf/Makefile.config
index 7f1e016a9253..b356520d8291 100644
--- a/tools/perf/Makefile.config
+++ b/tools/perf/Makefile.config
@@ -187,7 +187,7 @@ FEATURE_CHECK_LDFLAGS-libzstd := $(LIBZSTD_LDFLAGS)
TRACEEVENTLIBS := -ltraceevent
ifdef LIBTRACEEVENT_DIR
LIBTRACEEVENT_CFLAGS := -I$(LIBTRACEEVENT_DIR)/include
- LIBTRACEEVENT_LDFLAGS := -L$(LIBTRACEEVENT_DIR)/lib
+ LIBTRACEEVENT_LDFLAGS := -Wl,-rpath,$(LIBTRACEEVENT_DIR)/lib64
endif
FEATURE_CHECK_CFLAGS-libtraceevent := $(LIBTRACEEVENT_CFLAGS)
FEATURE_CHECK_LDFLAGS-libtraceevent := $(LIBTRACEEVENT_LDFLAGS)
$(TRACEEVENTLIBS)
```

I'm not sure how to make this something that'll work well with cross
compilation, etc.

Thanks,
Ian

> > +FEATURE_CHECK_CFLAGS-libtraceevent := $(LIBTRACEEVENT_CFLAGS)
> > +FEATURE_CHECK_LDFLAGS-libtraceevent := $(LIBTRACEEVENT_LDFLAGS) $(TRACEEVENTLIBS)
> > +
> > FEATURE_CHECK_CFLAGS-bpf = -I. -I$(srctree)/tools/include -I$(srctree)/tools/arch/$(SRCARCH)/include/uapi -I$(srctree)/tools/include/uapi
> > # include ARCH specific config
> > -include $(src-perf)/arch/$(SRCARCH)/Makefile
> > @@ -1165,9 +1175,10 @@ endif
> > ifneq ($(NO_LIBTRACEEVENT),1)
> > $(call feature_check,libtraceevent)
> > ifeq ($(feature-libtraceevent), 1)
> > - CFLAGS += -DHAVE_LIBTRACEEVENT
> > - EXTLIBS += -ltraceevent
> > - LIBTRACEEVENT_VERSION := $(shell $(PKG_CONFIG) --modversion libtraceevent)
> > + CFLAGS += -DHAVE_LIBTRACEEVENT $(LIBTRACEEVENT_CFLAGS)
> > + LDFLAGS += $(LIBTRACEEVENT_LDFLAGS)
> > + EXTLIBS += ${TRACEEVENTLIBS}
> > + LIBTRACEEVENT_VERSION := $(shell PKG_CONFIG_PATH=$(LIBTRACEEVENT_DIR) $(PKG_CONFIG) --modversion libtraceevent)
> > LIBTRACEEVENT_VERSION_1 := $(word 1, $(subst ., ,$(LIBTRACEEVENT_VERSION)))
> > LIBTRACEEVENT_VERSION_2 := $(word 2, $(subst ., ,$(LIBTRACEEVENT_VERSION)))
> > LIBTRACEEVENT_VERSION_3 := $(word 3, $(subst ., ,$(LIBTRACEEVENT_VERSION)))
> > @@ -1175,7 +1186,7 @@ ifneq ($(NO_LIBTRACEEVENT),1)
> > CFLAGS += -DLIBTRACEEVENT_VERSION=$(LIBTRACEEVENT_VERSION_CPP)
> > $(call detected,CONFIG_LIBTRACEEVENT)
> > else
> > - $(error ERROR: libtraceevent is missing. Please install libtraceevent-dev/libtraceevent-devel or build with NO_LIBTRACEEVENT=1)
> > + $(error ERROR: libtraceevent is missing. Please install libtraceevent-dev/libtraceevent-devel and/or set LIBTRACEEVENT_DIR or build with NO_LIBTRACEEVENT=1)
> > endif
> >
> > $(call feature_check,libtracefs)
> > @@ -1301,6 +1312,7 @@ ifeq ($(VF),1)
> > $(call print_var,LIBUNWIND_DIR)
> > $(call print_var,LIBDW_DIR)
> > $(call print_var,JDIR)
> > + $(call print_var,LIBTRACEEVENT_DIR)
> >
> > ifeq ($(dwarf-post-unwind),1)
> > $(call feature_print_text,"DWARF post unwind library", $(dwarf-post-unwind-text)) $(info $(MSG))
> > --
> > 2.25.1
> >

2024-05-06 08:31:52

by Yang Jihong

[permalink] [raw]
Subject: Re: [External] Re: [PATCH] perf build: Add LIBTRACEEVENT_DIR build option

Hello,

On 5/1/24 01:46, Ian Rogers wrote:
> On Sat, Mar 23, 2024 at 12:07 PM Ian Rogers <[email protected]> wrote:
>>
>> On Wed, Mar 13, 2024 at 11:30 PM Yang Jihong <[email protected]> wrote:
>>>
>>> Currently, when libtraceevent is not linked,
>>> perf does not support tracepoint:
>>>
>>> # ./perf record -e sched:sched_switch -a sleep 10
>>> event syntax error: 'sched:sched_switch'
>>> \___ unsupported tracepoint
>>>
>>> libtraceevent is necessary for tracepoint support
>>> Run 'perf list' for a list of valid events
>>>
>>> Usage: perf record [<options>] [<command>]
>>> or: perf record [<options>] -- <command> [<options>]
>>>
>>> -e, --event <event> event selector. use 'perf list' to list available events
>>>
>>> For cross-compilation scenario, library may not be installed in the default
>>> system path. Based on the above requirements, add LIBTRACEEVENT_DIR build
>>> option to support specifying path of libtraceevent.
>>>
>>> Example:
>>>
>>> 1. Cross compile libtraceevent
>>> # cd /opt/libtraceevent
>>> # CROSS_COMPILE=aarch64-linux-gnu- make
>>>
>>> 2. Cross compile perf
>>> # cd tool/perf
>>> # make VF=1 ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- NO_LIBELF=1 LDFLAGS=--static LIBTRACEEVENT_DIR=/opt/libtraceevent
>>> <SNIP>
>>> Auto-detecting system features:
>>> <SNIP>
>>> ... LIBTRACEEVENT_DIR: /opt/libtraceevent
>>>
>>> Signed-off-by: Yang Jihong <[email protected]>
>>
>> This all looks good to me, thanks!
>>
>> Reviewed-by: Ian Rogers <[email protected]>
>>
>>> ---
>>> tools/perf/Makefile.config | 20 ++++++++++++++++----
>>> 1 file changed, 16 insertions(+), 4 deletions(-)
>>>
>>> diff --git a/tools/perf/Makefile.config b/tools/perf/Makefile.config
>>> index 1fe8df97fe88..7783479de691 100644
>>> --- a/tools/perf/Makefile.config
>>> +++ b/tools/perf/Makefile.config
>>> @@ -182,6 +182,16 @@ endif
>>> FEATURE_CHECK_CFLAGS-libzstd := $(LIBZSTD_CFLAGS)
>>> FEATURE_CHECK_LDFLAGS-libzstd := $(LIBZSTD_LDFLAGS)
>>>
>>> +# for linking with debug library, run like:
>>> +# make DEBUG=1 LIBTRACEEVENT_DIR=/opt/libtraceevent/
>>> +TRACEEVENTLIBS := -ltraceevent
>>> +ifdef LIBTRACEEVENT_DIR
>>> + LIBTRACEEVENT_CFLAGS := -I$(LIBTRACEEVENT_DIR)/include
>>> + LIBTRACEEVENT_LDFLAGS := -L$(LIBTRACEEVENT_DIR)/lib
>>> +endif
>
> I'm finding to test a libtraceevent asan build on a system that has
> libtraceevent installed, I need to carry this change:
> ```
> diff --git a/tools/perf/Makefile.config b/tools/perf/Makefile.config
> index 7f1e016a9253..b356520d8291 100644
> --- a/tools/perf/Makefile.config
> +++ b/tools/perf/Makefile.config
> @@ -187,7 +187,7 @@ FEATURE_CHECK_LDFLAGS-libzstd := $(LIBZSTD_LDFLAGS)
> TRACEEVENTLIBS := -ltraceevent
> ifdef LIBTRACEEVENT_DIR
> LIBTRACEEVENT_CFLAGS := -I$(LIBTRACEEVENT_DIR)/include
> - LIBTRACEEVENT_LDFLAGS := -L$(LIBTRACEEVENT_DIR)/lib
> + LIBTRACEEVENT_LDFLAGS := -Wl,-rpath,$(LIBTRACEEVENT_DIR)/lib64
> endif
> FEATURE_CHECK_CFLAGS-libtraceevent := $(LIBTRACEEVENT_CFLAGS)
> FEATURE_CHECK_LDFLAGS-libtraceevent := $(LIBTRACEEVENT_LDFLAGS)
> $(TRACEEVENTLIBS)
> ```
>
> I'm not sure how to make this something that'll work well with cross
> compilation, etc.

Because of the holiday, the reply is a bit late.

It seems that perf built by asan/msan will not search for shared
libraries in the -L directory.
We generally use cross-compile perf to analyze performance problems. In
most cases, it is static compilation (rpath does not work), and it will
not enable sanitizers.
so can we simply check if EXTRA_CFLAGS contains
-fsanitize=address/memory, and add libtraceevent dir to rpath?

I submitted a patch, please see if the solution is feasible:
https://lore.kernel.org/all/[email protected]/


Or we can add libtraceevent's ld.so.conf to the environment. This
requires manual configuration of the build environment, which is
inconvenient.

# echo "/opt/libtraceevent/lib" > /etc/ld.so.conf.d/libtracevent.conf
# ldconfig

Thanks,
Yang