Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion tools/bpf/bpftool/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,21 @@ $(LIBBPF_BOOTSTRAP)-clean: FORCE | $(LIBBPF_BOOTSTRAP_OUTPUT)
prefix ?= /usr/local
bash_compdir ?= /usr/share/bash-completion/completions

try-run = $(shell set -e; \
if ($(1)) >/dev/null 2>&1; \
then echo "$(2)"; \
else echo "$(3)"; \
fi)

__cc-option = $(call try-run,\
$(1) -Werror $(2) -c -x c /dev/null -o /dev/null,$(2),)
cc-option = $(call __cc-option, $(CC),$(1))

CFLAGS += -O2
CFLAGS += -W
CFLAGS += -Wall
CFLAGS += -Wextra
CFLAGS += -Wformat-signedness
CFLAGS += $(call cc-option,-Wformat-signedness)
CFLAGS += -Wno-unused-parameter
CFLAGS += -Wno-missing-field-initializers
CFLAGS += $(filter-out -Wswitch-enum -Wnested-externs,$(EXTRA_WARNINGS))
Expand Down
4 changes: 2 additions & 2 deletions tools/build/feature/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,10 @@ $(OUTPUT)test-libcapstone.bin:
$(BUILD) # -lcapstone provided by $(FEATURE_CHECK_LDFLAGS-libcapstone)

$(OUTPUT)test-compile-32.bin:
$(CC) -m32 -o $@ test-compile.c
$(CC) -m32 -Wall -Werror -o $@ test-compile.c

$(OUTPUT)test-compile-x32.bin:
$(CC) -mx32 -o $@ test-compile.c
$(CC) -mx32 -Wall -Werror -o $@ test-compile.c

$(OUTPUT)test-zlib.bin:
$(BUILD) -lz
Expand Down
18 changes: 18 additions & 0 deletions tools/perf/Documentation/Build.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,21 @@ configuration paths for cross building:
In this case, the variable PKG_CONFIG_SYSROOT_DIR can be used alongside the
variable PKG_CONFIG_LIBDIR or PKG_CONFIG_PATH to prepend the sysroot path to
the library paths for cross compilation.

5) Build with clang
===================
By default, the makefile uses GCC as compiler. With specifying environment
variables HOSTCC, CC and CXX, it allows to build perf with clang.

Using clang for native build:

$ HOSTCC=clang CC=clang CXX=clang++ make -C tools/perf

Using clang for cross compilation:

$ HOSTCC=clang CC=clang CXX=clang++ \
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- -C tools/perf \
NO_LIBELF=1 NO_LIBTRACEEVENT=1 NO_JEVENTS=1

In the example above, due to lack libelf, python and libtraceevent for
cross comiplation, disable the features accordingly.
82 changes: 8 additions & 74 deletions tools/perf/Documentation/android.txt
Original file line number Diff line number Diff line change
@@ -1,78 +1,12 @@
How to compile perf for Android
=========================================
===============================

I. Set the Android NDK environment
------------------------------------------------
There have two ways to build perf and run it on Android.

(a). Use the Android NDK
------------------------------------------------
1. You need to download and install the Android Native Development Kit (NDK).
Set the NDK variable to point to the path where you installed the NDK:
export NDK=/path/to/android-ndk
- The first method is to build perf with static linking, please refer to
Build.txt, section "4) Cross compilation" for how to build a static
perf binary.

2. Set cross-compiling environment variables for NDK toolchain and sysroot.
For arm:
export NDK_TOOLCHAIN=${NDK}/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-
export NDK_SYSROOT=${NDK}/platforms/android-24/arch-arm
For x86:
export NDK_TOOLCHAIN=${NDK}/toolchains/x86-4.9/prebuilt/linux-x86_64/bin/i686-linux-android-
export NDK_SYSROOT=${NDK}/platforms/android-24/arch-x86

This method is only tested for Android NDK versions Revision 11b and later.
perf uses some bionic enhancements that are not included in prior NDK versions.
You can use method (b) described below instead.

(b). Use the Android source tree
-----------------------------------------------
1. Download the master branch of the Android source tree.
Set the environment for the target you want using:
source build/envsetup.sh
lunch

2. Build your own NDK sysroot to contain latest bionic changes and set the
NDK sysroot environment variable.
cd ${ANDROID_BUILD_TOP}/ndk
For arm:
./build/tools/build-ndk-sysroot.sh --abi=arm
export NDK_SYSROOT=${ANDROID_BUILD_TOP}/ndk/build/platforms/android-3/arch-arm
For x86:
./build/tools/build-ndk-sysroot.sh --abi=x86
export NDK_SYSROOT=${ANDROID_BUILD_TOP}/ndk/build/platforms/android-3/arch-x86

3. Set the NDK toolchain environment variable.
For arm:
export NDK_TOOLCHAIN=${ANDROID_TOOLCHAIN}/arm-linux-androideabi-
For x86:
export NDK_TOOLCHAIN=${ANDROID_TOOLCHAIN}/i686-linux-android-

II. Compile perf for Android
------------------------------------------------
You need to run make with the NDK toolchain and sysroot defined above:
For arm:
make WERROR=0 ARCH=arm CROSS_COMPILE=${NDK_TOOLCHAIN} EXTRA_CFLAGS="-pie --sysroot=${NDK_SYSROOT}"
For x86:
make WERROR=0 ARCH=x86 CROSS_COMPILE=${NDK_TOOLCHAIN} EXTRA_CFLAGS="-pie --sysroot=${NDK_SYSROOT}"

III. Install perf
-----------------------------------------------
You need to connect to your Android device/emulator using adb.
Install perf using:
adb push perf /data/perf

If you also want to use perf-archive you need busybox tools for Android.
For installing perf-archive, you first need to replace #!/bin/bash with #!/system/bin/sh:
sed 's/#!\/bin\/bash/#!\/system\/bin\/sh/g' perf-archive >> /tmp/perf-archive
chmod +x /tmp/perf-archive
adb push /tmp/perf-archive /data/perf-archive

IV. Environment settings for running perf
------------------------------------------------
Some perf features need environment variables to run properly.
You need to set these before running perf on the target:
adb shell
# PERF_PAGER=cat

IV. Run perf
------------------------------------------------
Run perf on your device/emulator to which you previously connected using adb:
# ./data/perf
- The second method is to download Android NDK, then use the contained
clang compiler for building perf. Please refer to Build.txt, section
"5) Build with clang" for details.
32 changes: 31 additions & 1 deletion tools/perf/Makefile.config
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,38 @@ HOSTCFLAGS := $(filter-out -Wnested-externs,$(EXTRA_WARNINGS))
# borrowed from kernel headers depends on it, e.g. put_unaligned_*().
CFLAGS += -fno-strict-aliasing

# Enabled Wthread-safety analysis for clang builds.
# Set target flag and options when using clang as compiler.
ifeq ($(CC_NO_CLANG), 0)
CLANG_TARGET_FLAGS_arm := arm-linux-gnueabi
CLANG_TARGET_FLAGS_arm64 := aarch64-linux-gnu
CLANG_TARGET_FLAGS_m68k := m68k-linux-gnu
CLANG_TARGET_FLAGS_mips := mipsel-linux-gnu
CLANG_TARGET_FLAGS_powerpc := powerpc64le-linux-gnu
CLANG_TARGET_FLAGS_riscv := riscv64-linux-gnu
CLANG_TARGET_FLAGS_s390 := s390x-linux-gnu
CLANG_TARGET_FLAGS_x86 := x86_64-linux-gnu

# Default to host architecture if ARCH is not explicitly given.
ifeq ($(ARCH),)
CLANG_TARGET_FLAGS := $(shell $(CLANG) -print-target-triple)
else
CLANG_TARGET_FLAGS := $(CLANG_TARGET_FLAGS_$(ARCH))
endif

ifeq ($(CROSS_COMPILE),)
ifeq ($(CLANG_TARGET_FLAGS),)
$(error Specify CROSS_COMPILE or add CLANG_TARGET_FLAGS for $(ARCH))
else
CLANG_FLAGS += --target=$(CLANG_TARGET_FLAGS)
endif # CLANG_TARGET_FLAGS
else
CLANG_FLAGS += --target=$(notdir $(CROSS_COMPILE:%-=%))
endif # CROSS_COMPILE

CC := $(CLANG) $(CLANG_FLAGS) -fintegrated-as
CXX := $(CXX) $(CLANG_FLAGS) -fintegrated-as

# Enabled Wthread-safety analysis for clang builds.
CFLAGS += -Wthread-safety
endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ static void *thrfn(void *arg)
}
for (i = 0; i < len; i++)
memcpy(dst, src, a->size * 1024);

return NULL;
}

static pthread_t new_thr(void *(*fn) (void *arg), void *arg)
Expand Down
4 changes: 2 additions & 2 deletions tools/perf/tests/shell/coresight/thread_loop/thread_loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ static void *thrfn(void *arg)
}
asm volatile(
"loop:\n"
"add %[i], %[i], #1\n"
"cmp %[i], %[len]\n"
"add %w[i], %w[i], #1\n"
"cmp %w[i], %w[len]\n"
"blt loop\n"
: /* out */
: /* in */ [i] "r" (i), [len] "r" (len)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ static void *thrfn(void *arg)
for (i = 0; i < 10000; i++) {
asm volatile (
// force an unroll of thia add instruction so we can test long runs of code
#define SNIP1 "add %[in], %[in], #1\n"
#define SNIP1 "add %w[in], %w[in], #1\n"
// 10
#define SNIP2 SNIP1 SNIP1 SNIP1 SNIP1 SNIP1 SNIP1 SNIP1 SNIP1 SNIP1 SNIP1
// 100
Expand All @@ -36,6 +36,8 @@ static void *thrfn(void *arg)
: /* clobber */
);
}

return NULL;
}

static pthread_t new_thr(void *(*fn) (void *arg), void *arg)
Expand Down
5 changes: 4 additions & 1 deletion tools/perf/util/setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from os import getenv, path
from subprocess import Popen, PIPE
from re import sub
import shlex

cc = getenv("CC")
assert cc, "Environment variable CC not set"
Expand All @@ -22,7 +23,9 @@
src_feature_tests = f'{srctree}/tools/build/feature'

def clang_has_option(option):
cc_output = Popen([cc, cc_options + option, path.join(src_feature_tests, "test-hello.c") ], stderr=PIPE).stderr.readlines()
cmd = shlex.split(f"{cc} {cc_options} {option}")
cmd.append(path.join(src_feature_tests, "test-hello.c"))
cc_output = Popen(cmd, stderr=PIPE).stderr.readlines()
return [o for o in cc_output if ((b"unknown argument" in o) or (b"is not supported" in o) or (b"unknown warning option" in o))] == [ ]

if cc_is_clang:
Expand Down
Loading