メモる。
ホストのカーネルは、Ubuntu22.04
手順
以下最初の1回目のみ。
sudo apt update && sudo apt install -y docker.io
sudo /bin/su -c "echo 'kernel.perf_event_paranoid = -1' >> /etc/sysctl.conf"
sudo sysctl -p
必要な理由
これをすっ飛ばすと、perf record 実行時の以下のエラーが発生する。
$ sudo perf record miniruby
# Error:
# Access to performance monitoring and observability operations is limited.
# Consider adjusting /proc/sys/kernel/perf_event_paranoid setting to open
# access to performance monitoring and observability operations for processes
# without CAP_PERFMON, CAP_SYS_PTRACE or CAP_SYS_ADMIN Linux capability.
# More information can be found at 'Perf events and tool security' document:
# https://www.kernel.org/doc/html/latest/admin-guide/perf-security.html
# perf_event_paranoid setting is 4:
# -1: Allow use of (almost) all events by all users
# Ignore mlock limit after perf_event_mlock_kb without CAP_IPC_LOCK
# >= 0: Disallow raw and ftrace function tracepoint access
# >= 1: Disallow CPU event access
# >= 2: Disallow kernel profiling
# To make the adjusted perf_event_paranoid setting permanent preserve it
# in /etc/sysctl.conf (e.g. kernel.perf_event_paranoid = <setting>)
以下の内容をDockerfileとしておく。
FROM ubuntu:22.04
# For apt intall gcc (ref. https://www.linuxmaster.jp/linux_blog/2022/02/ubuntu2004failed-to-fetch-404-not-found.html)
RUN rm -rf /var/lib/apt/lists/*
RUN apt update
RUN apt install -y \
# sudo \
curl \
git \
gcc \
autoconf \
gperf \
ruby \
make \
bison
RUN git clone -b ruby_3_1 https://github.com/ruby/ruby.git
WORKDIR /ruby
# https://docs.ruby-lang.org/en/master/contributing/building_ruby_md.html
RUN ./autogen.sh
RUN mkdir build
WORKDIR /ruby/build
RUN mkdir ~/.rubies
RUN ../configure --prefix="${HOME}/.rubies/ruby-master"
RUN make miniruby
RUN apt install sudo
RUN sudo apt install -y\
linux-tools-$(uname -r) \
vim
# ref. https://qiita.com/k0kubun/items/b094c4b9bd4fe0027a48
RUN sudo apt install -y libc6-dbg
RUN touch ../test.rb
ドッカーイメージをビルドし、コンテナ内に入る。
sudo docker build . -t fsp
sudo docker run -it --privileged fsp /bin/bash
コンテナ内で、../test.rb
を編集し、プロファイリングしたいRubyプログラムを書く。
以下でプロファイリングする。
perf record -- ./miniruby ../test.rb
perf report