|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Downloaded from https://gist.github.com/karlivory/9111f906f4eb06370b2b237c62a6b00e |
| 3 | +# Usage: sudo bash build-perf.sh |
| 4 | + |
| 5 | +set -euo pipefail |
| 6 | + |
| 7 | +# --- Helpers --- |
| 8 | +log() { echo -e "\033[1;32m==>\033[0m $*"; } |
| 9 | +err() { echo -e "\033[1;31m[ERR]\033[0m $*" >&2; } |
| 10 | + |
| 11 | +[[ $EUID -eq 0 ]] || { |
| 12 | + err "Please run as root (use: sudo bash build-perf.sh)" |
| 13 | + exit 1 |
| 14 | +} |
| 15 | +export DEBIAN_FRONTEND=noninteractive |
| 16 | + |
| 17 | +FULLREL="$(uname -r)" # e.g. 6.14.0-29-generic |
| 18 | +MAJMIN="$(uname -r | awk -F'[.-]' '{print $1 "." $2}')" # e.g. 6.14 |
| 19 | +WRAP_DIR="/usr/lib/linux-tools/${FULLREL}" |
| 20 | + |
| 21 | +log "Detected running kernel: ${FULLREL}" |
| 22 | +log "Using upstream kernel source line: ${MAJMIN}" |
| 23 | + |
| 24 | +# --- Install deps --- |
| 25 | +log "Installing build dependencies..." |
| 26 | +apt-get update -y |
| 27 | +apt-get install -y \ |
| 28 | + build-essential make gcc g++ pkg-config flex bison \ |
| 29 | + curl ca-certificates xz-utils tar \ |
| 30 | + libelf-dev libdw-dev libdwarf-dev libunwind-dev \ |
| 31 | + libnuma-dev libssl-dev libcap-dev \ |
| 32 | + zlib1g-dev liblzma-dev libzstd-dev \ |
| 33 | + libaio-dev libtraceevent-dev libpfm4-dev \ |
| 34 | + libslang2-dev systemtap-sdt-dev \ |
| 35 | + python3 python3-dev python3-setuptools \ |
| 36 | + binutils-dev libiberty-dev libbabeltrace-dev \ |
| 37 | + libbfd-dev clang llvm || true |
| 38 | +apt-get install -y debuginfod || true |
| 39 | + |
| 40 | +# --- Fetch kernel sources --- |
| 41 | +WORKDIR="$(mktemp -d -t perfbuild-XXXXXX)" |
| 42 | +trap 'rm -rf "$WORKDIR"' EXIT |
| 43 | +pushd "$WORKDIR" >/dev/null |
| 44 | + |
| 45 | +BASE_URL="https://mirrors.edge.kernel.org/pub/linux/kernel/v6.x" |
| 46 | +ARCHIVE="" |
| 47 | +for f in "linux-${MAJMIN}.tar.xz" "linux-${MAJMIN}.tar.gz"; do |
| 48 | + log "Downloading: ${BASE_URL}/${f}" |
| 49 | + if curl -fsSLO "${BASE_URL}/${f}"; then |
| 50 | + ARCHIVE="$f" |
| 51 | + break |
| 52 | + fi |
| 53 | +done |
| 54 | +[[ -n "$ARCHIVE" ]] || { |
| 55 | + err "Failed to download linux-${MAJMIN} from ${BASE_URL}" |
| 56 | + exit 2 |
| 57 | +} |
| 58 | + |
| 59 | +log "Extracting ${ARCHIVE} ..." |
| 60 | +tar -xf "${ARCHIVE}" |
| 61 | +SRC_DIR="${WORKDIR}/linux-${MAJMIN}" |
| 62 | +[[ -d "${SRC_DIR}/tools/perf" ]] || { |
| 63 | + err "tools/perf not found in sources" |
| 64 | + exit 3 |
| 65 | +} |
| 66 | +[[ -d "${SRC_DIR}/tools/bpf/bpftool" ]] || { |
| 67 | + err "tools/bpf/bpftool not found in sources" |
| 68 | + exit 3 |
| 69 | +} |
| 70 | + |
| 71 | +# --- Build perf --- |
| 72 | +pushd "${SRC_DIR}/tools/perf" >/dev/null |
| 73 | +log "Building perf (WERROR=0) ..." |
| 74 | +make -j"$(nproc)" WERROR=0 |
| 75 | +log "Built perf version: $(./perf --version)" |
| 76 | +popd >/dev/null |
| 77 | + |
| 78 | +# --- Build bpftool --- |
| 79 | +pushd "${SRC_DIR}/tools/bpf/bpftool" >/dev/null |
| 80 | +log "Building bpftool ..." |
| 81 | +make -j"$(nproc)" |
| 82 | +log "Built bpftool version: $(./bpftool version || true)" |
| 83 | +popd >/dev/null |
| 84 | + |
| 85 | +# --- Install into wrapper path(s) --- |
| 86 | +log "Installing perf to ${WRAP_DIR}/perf" |
| 87 | +install -d "${WRAP_DIR}" |
| 88 | +install -m 0755 "${SRC_DIR}/tools/perf/perf" "${WRAP_DIR}/perf" |
| 89 | + |
| 90 | +log "Installing bpftool to ${WRAP_DIR}/bpftool" |
| 91 | +install -m 0755 "${SRC_DIR}/tools/bpf/bpftool/bpftool" "${WRAP_DIR}/bpftool" |
| 92 | + |
| 93 | +# --- Verify via Ubuntu wrappers --- |
| 94 | +log "Verifying /usr/bin/perf (Ubuntu wrapper) ..." |
| 95 | +if /usr/bin/perf --version >/dev/null 2>&1; then |
| 96 | + /usr/bin/perf --version |
| 97 | + log "Success: wrapper found perf at ${WRAP_DIR}/perf" |
| 98 | +else |
| 99 | + err "Wrapper did not run perf. Check that ${WRAP_DIR}/perf exists and is executable." |
| 100 | + ls -l "${WRAP_DIR}/perf" || true |
| 101 | + exit 4 |
| 102 | +fi |
| 103 | + |
| 104 | +log "Verifying /usr/sbin/bpftool (Ubuntu wrapper) ..." |
| 105 | +if /usr/sbin/bpftool version >/dev/null 2>&1; then |
| 106 | + /usr/sbin/bpftool version |
| 107 | + log "Success: wrapper found bpftool at ${WRAP_DIR}/bpftool" |
| 108 | +else |
| 109 | + err "Wrapper did not run bpftool. Check that ${WRAP_DIR}/bpftool exists and is executable." |
| 110 | + ls -l "${WRAP_DIR}/bpftool" || true |
| 111 | + exit 5 |
| 112 | +fi |
| 113 | + |
| 114 | +log "Done." |
0 commit comments