Skip to content

Commit a5b9d31

Browse files
committed
🆕 [perf] init
0 parents  commit a5b9d31

9 files changed

Lines changed: 10834 additions & 0 deletions

File tree

.github/LICENSE

Lines changed: 313 additions & 0 deletions
Large diffs are not rendered by default.

.github/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../perf

.github/scripts/export.sh

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#
2+
# The MIT License (MIT)
3+
#
4+
# Copyright (c) 2024-2025 Kris Jusiak <kris@jusiak.net>
5+
#
6+
# Permission is hereby granted, free of charge, to any person obtaining a copy
7+
# of this software and associated documentation files (the "Software"), to deal
8+
# in the Software without restriction, including without limitation the rights
9+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
# copies of the Software, and to permit persons to whom the Software is
11+
# furnished to do so, subject to the following conditions:
12+
#
13+
# The above copyright notice and this permission notice shall be included in all
14+
# copies or substantial portions of the Software.
15+
#
16+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
# SOFTWARE.
23+
#
24+
# Requirements:
25+
# - apt-get install coreutils # base64
26+
# - apt-get install imagemagick # convert
27+
# - apt-get install libsixel-bin # sixel
28+
# - pip install ansi2html # ansi2html
29+
#
30+
# Usage:
31+
# ./app 2>&1 | ./export.sh markdown > app.md
32+
# ./app 2>&1 | ./export.sh notebook > app.ipynb
33+
# ./app 2>&1 | ./export.sh html > app.html
34+
35+
function markdown() {
36+
stdin=$(mktemp)
37+
stdout=$(mktemp)
38+
39+
cat - > $stdin
40+
41+
while grep -q $'\x1bP' $stdin; do
42+
sed -z 's/\(.*\)\x1bP.*/\1/' $stdin >> $stdout
43+
sed -z 's/.*\(\x1bP.*\x1b\\\).*/\1/' $stdin | \
44+
convert sixel:- png:- | base64 | tr -d '\n' | \
45+
xargs -i{} echo -e "\n\`\`\`\n![image](data:image/png;base64,{})\n\`\`\`\n" >> $stdout
46+
sed -z 's/.*\x1b\\\(.*\)/\1/' $stdin >> $stdout
47+
mv $stdout $stdin
48+
done
49+
echo "\`\`\`"
50+
cat $stdin
51+
echo "\`\`\`"
52+
}
53+
54+
function html() {
55+
stdin=$(mktemp)
56+
stdout=$(mktemp)
57+
58+
cat - | ansi2html > $stdin
59+
60+
while grep -q $'\x1bP' $stdin; do
61+
sed -z 's/\(.*\)\x1bP.*/\1/' $stdin >> $stdout
62+
sed -z 's/.*\(\x1bP.*\x1b\\\).*/\1/' $stdin | \
63+
convert sixel:- png:- | base64 | tr -d '\n' | \
64+
xargs -i{} echo "<img src='data:image/png;base64,{}'/><br />" >> $stdout
65+
sed -z 's/.*\x1b\\\(.*\)/\1/' $stdin >> $stdout
66+
mv $stdout $stdin
67+
done
68+
cat $stdin
69+
}
70+
71+
function notebook() {
72+
cat <<EOF
73+
{
74+
"cells": [
75+
{
76+
"cell_type": "markdown",
77+
"metadata": {},
78+
"source": [
79+
$(cat - | markdown | sed 's/.*/"&\\n",/')
80+
""
81+
]
82+
}
83+
],
84+
"metadata": {
85+
"jupytext": {
86+
"cell_metadata_filter": "-all",
87+
"main_language": "python",
88+
"notebook_metadata_filter": "-all"
89+
}
90+
},
91+
"nbformat": 4,
92+
"nbformat_minor": 5
93+
}
94+
EOF
95+
}
96+
97+
if [ $# -lt 1 ]; then
98+
echo "Usage: $0 <markdown|notebook|html> # reads from stdin"
99+
exit 1
100+
fi
101+
102+
cat - | $1

.github/scripts/setup.sh

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/bin/bash
2+
#
3+
# The MIT License (MIT)
4+
#
5+
# Copyright (c) 2024-2025 Kris Jusiak <kris@jusiak.net>
6+
#
7+
# Permission is hereby granted, free of charge, to any person obtaining a copy
8+
# of this software and associated documentation files (the "Software"), to deal
9+
# in the Software without restriction, including without limitation the rights
10+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
# copies of the Software, and to permit persons to whom the Software is
12+
# furnished to do so, subject to the following conditions:
13+
#
14+
# The above copyright notice and this permission notice shall be included in all
15+
# copies or substantial portions of the Software.
16+
#
17+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
# SOFTWARE.
24+
#
25+
SET_PERF=false
26+
SET_RDPMC=false
27+
MAX_SAMPLE_RATE=""
28+
29+
usage() {
30+
echo "Usage: $0 [options]"
31+
echo ""
32+
echo "Options:"
33+
echo " -p, --perf Enable perf setup: sysctls, remount debugfs, set uprobe_events [default: not set]"
34+
echo " -r, --rdpmc Enable rdpmc (sets value to 2) [default: not set]"
35+
echo " -s, --max-sample-rate RATE Set perf_event_max_sample_rate (e.g., 100000) [default: not set]"
36+
echo " -h, --help Show this help message"
37+
echo ""
38+
echo "Usage:"
39+
echo " $0"
40+
echo " Run with defaults (perf setup enabled, rdpmc off, no sample rate)"
41+
exit 1
42+
}
43+
44+
while [[ "$#" -gt 0 ]]; do
45+
case $1 in
46+
-p|--perf) SET_PERF=true ;;
47+
-r|--rdpmc) SET_RDPMC=true ;;
48+
-s|--max-sample-rate) MAX_SAMPLE_RATE="$2"; shift ;;
49+
-h|--help) usage ;;
50+
*) echo "Unknown parameter: $1"; usage ;;
51+
esac
52+
shift
53+
done
54+
55+
if [[ -z "$MAX_SAMPLE_RATE" && "$SET_RDPMC" == false && "$SET_PERF" == false ]]; then
56+
usage
57+
fi
58+
59+
if $SET_PERF; then
60+
sudo mount -o remount,mode=755 /sys/kernel/debug
61+
sudo mount -o remount,mode=755 /sys/kernel/debug/tracing
62+
echo 0 | sudo tee /proc/sys/kernel/kptr_restrict
63+
echo -1 | sudo tee /proc/sys/kernel/perf_event_paranoid
64+
sudo chown "$(whoami)" /sys/kernel/debug/tracing/uprobe_events
65+
sudo chmod a+rw /sys/kernel/debug/tracing/uprobe_events
66+
fi
67+
68+
if [[ -n "$MAX_SAMPLE_RATE" ]]; then
69+
echo "$MAX_SAMPLE_RATE" | sudo tee /proc/sys/kernel/perf_event_max_sample_rate
70+
fi
71+
72+
if $SET_RDPMC; then
73+
echo 2 | sudo tee /sys/devices/cpu_core/rdpmc
74+
fi

.github/scripts/tune.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/bash
2+
3+
## todo on/off
4+
5+
# Disable CPU Frequency Scaling (apt install cpufrequtils)
6+
sudo cpupower frequency-set --governor performance
7+
# cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
8+
9+
# Disable Address Space Randomization
10+
echo 0 > /proc/sys/kernel/randomize_va_space
11+
12+
# Disable Processor Boosting
13+
echo 0 | sudo tee /sys/devices/system/cpu/cpufreq/boost
14+
15+
# Disable Turbo Mode
16+
echo 1 > /sys/devices/system/cpu/intel_pstate/no_turbo
17+
18+
# Disable Hyperthreading/SMT
19+
echo off | sudo tee /sys/devices/system/cpu/smt/control
20+
21+
# Restrict memory to a single socket
22+
#numactl -m 0 -N 0 ./a.out
23+
24+
# Enable Huge Pages
25+
#sudo numactl --cpunodebind=1 --membind=1 hugeadm --obey-mempolicy --pool-pages-min=1G:64
26+
#sudo hugeadm --create-mounts

.github/workflows/Dockerfile

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# The MIT License (MIT)
2+
#
3+
# Copyright (c) 2024-2025 Kris Jusiak <kris@jusiak.net>
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
23+
# ```
24+
# docker build -t perf .
25+
# ```
26+
27+
# ```
28+
# docker run \
29+
# --rm \
30+
# --user $(id -u):$(id -g) \
31+
# --privileged \
32+
# --network=host \
33+
# -e DISPLAY=${DISPLAY} \
34+
# -v ${PWD}:${PWD} \
35+
# -w ${PWD} \
36+
# perf
37+
# ```
38+
39+
FROM ubuntu:24.04
40+
ARG CLANG=20
41+
ARG GCC=14
42+
43+
RUN apt-get update && apt-get install -y \
44+
gnupg software-properties-common lsb-release ca-certificates \
45+
python3-pip python3-venv cmake \
46+
wget bzip2 vim pkg-config dbus-x11 git gh \
47+
kmod linux-headers-generic linux-tools-generic linux-tools-*-generic util-linux hwloc msr-tools \
48+
llvm-dev libpfm4 libpapi-dev papi-tools libipt-dev \
49+
libgoogle-perftools-dev google-perftools valgrind kcachegrind coz-profiler pahole likwid hotspot hyperfine \
50+
graphviz gnuplot coreutils imagemagick libsixel-bin
51+
52+
RUN python3 -m venv /venv
53+
RUN /venv/bin/pip install \
54+
pyperf osaca \
55+
jupyter numpy pandas matplotlib ansi2html
56+
57+
RUN wget -qO - https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB | \
58+
gpg --dearmor -o /usr/share/keyrings/intel-oneapi-archive-keyring.gpg
59+
RUN echo "deb [signed-by=/usr/share/keyrings/intel-oneapi-archive-keyring.gpg] https://apt.repos.intel.com/oneapi all main" | \
60+
tee /etc/apt/sources.list.d/oneAPI.list
61+
RUN apt-get update && apt-get install -y libnss3 intel-oneapi-vtune
62+
63+
RUN wget https://apt.llvm.org/llvm.sh && chmod +x llvm.sh && ./llvm.sh $CLANG && rm llvm.sh
64+
RUN add-apt-repository ppa:ubuntu-toolchain-r/test && apt update && apt install -y g++-$GCC
65+
66+
ENV PATH="/venv/bin:$PATH"
67+
ENV DISPLAY=:0

.github/workflows/linux.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: linux
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
7+
pull_request:
8+
branches: [ main ]
9+
10+
jobs:
11+
build:
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- name: checkout
16+
uses: actions/checkout@v3
17+
18+
- name: image
19+
run: docker build -f .github/workflows/Dockerfile -t perf .
20+
21+
- name: build
22+
run: |
23+
cat <<EOF > perf.cpp
24+
#include <perf>
25+
int main() { }
26+
EOF
27+
docker run --rm --privileged -v ${PWD}:${PWD} -w ${PWD} perf sh -c "\
28+
clang++-20 -DPERF_INTEL=0 -std=c++23 -O3 -I. -I/usr/lib/llvm-20/include -lLLVM-20 -lipt perf.cpp -o perf && ./perf"
29+
30+
# TODO
31+
# - header/module
32+
# - gcc/clang
33+
# - LLVM INTEL
34+
# - NTEST
35+
# - O1/O2/O3
36+
# - -g

0 commit comments

Comments
 (0)