Skip to content

Instantly share code, notes, and snippets.

@fcamblor
Last active September 16, 2023 21:07
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fcamblor/1dc5a4edc27f89636b473e30c4efa7d2 to your computer and use it in GitHub Desktop.
Save fcamblor/1dc5a4edc27f89636b473e30c4efa7d2 to your computer and use it in GitHub Desktop.
Async profiler on docker-alpine
FROM tomcat:7-jre8-alpine
# See https://github.com/jvm-profiling-tools/async-profiler/issues/207
RUN apk update && apk add --no-cache libc6-compat perl openjdk8-dbg
RUN mkdir /usr/local/async-profiler/ &&\
wget -O /usr/local/async-profiler/async-profiler.tar.gz https://github.com/jvm-profiling-tools/async-profiler/releases/download/v1.5/async-profiler-1.5-linux-x64.tar.gz &&\
cd /usr/local/async-profiler/ &&\
tar -xvzf async-profiler.tar.gz &&\
rm -f /usr/local/async-profiler/async-profiler.tar.gz
RUN mkdir /usr/local/flame-graph/ &&\
wget -O /usr/local/flame-graph/flame-graph.zip https://github.com/brendangregg/FlameGraph/archive/1b1c6deede9c33c5134c920bdb7a44cc5528e9a7.zip &&\
cd /usr/local/flame-graph/ && unzip flame-graph.zip &&\
cd /usr/local/flame-graph/ &&\
mv FlameGraph-1b1c6deede9c33c5134c920bdb7a44cc5528e9a7/* ./ &&\
rm -Rf FlameGraph-1b1c6deede9c33c5134c920bdb7a44cc5528e9a7 &&\
rm -f /usr/local/flame-graph/flame-graph.zip
# Maybe do some specific stuff (like copy your WAR file inside /usr/local/tomcat/webapps/ROOT, or create a setenv.sh file into /usr/local/tomcat/bin/setenv.sh)
CMD ["sh", "-c", "cd /usr/local/tomcat/ && catalina.sh run"]
# It will gather call stacks during 30s on your docker process instance
# Note that I added -e itimer option as I never succeeded to configure perf event on docker instance
# event through --security-opt=seccomp:unconfined docker configuration property
# See https://github.com/jvm-profiling-tools/async-profiler#profiling-java-in-a-container
# and https://github.com/jvm-profiling-tools/async-profiler#troubleshooting
docker exec -ti <INSTANCE_DOCKER> /usr/local/async-profiler/profiler.sh -d 30 -o collapsed -e itimer -f /tmp/collapsed.txt 1
# It will transform the RAW async profiler data into a flamechart SVG file directly on your HOST machine
# (in /tmp/results.svg)
docker exec -ti <INSTANCE_DOCKER> /usr/local/flame-graph/flamegraph.pl --colors=java /tmp/collapsed.txt > /tmp/results.svg
@alinbutura
Copy link

alinbutura commented Nov 12, 2019

Hi,

I just tried your solution in a Dockerfile relying on openjdk:8u212-jdk-alpine as the base image and as soon as I execute the
docker exec -ti $CONTAINER_NAME /usr/local/async-profiler/profiler.sh -d 30 -o collapsed -e itimer -f /tmp/collapsed.txt $PID
command the container exits with the error

Spring Boot (v2.1.6.RELEASE)

A fatal error has been detected by the Java Runtime Environment:
SIGSEGV (0xb) at pc=0x00007fffd99ce8cc, pid=8, tid=0x00007fffda15eb10
JRE version: OpenJDK Runtime Environment (8.0_212-b04) (build 1.8.0_212-b04)

Java VM: OpenJDK 64-Bit Server VM (25.212-b04 mixed mode linux-amd64 compressed oops)

Derivative: IcedTea 3.12.0
Distribution: Custom build (Sat May 4 17:33:35 UTC 2019)

Problematic frame:
C [libasyncProfiler.so+0x1d8cc] VMStructs::init(NativeCodeCache*)+0x14c
Core dump written. Default location: //core or core.8
An error report file with more information is saved as:
//hs_err_pid8.log

If you would like to submit a bug report, please include
instructions on how to reproduce the bug and visit:
https://icedtea.classpath.org/bugzilla
The crash happened outside the Java Virtual Machine in native code.
See problematic frame for where to report the bug.

Our app is using quite a lot of CPU and we would like to profile it...

Any suggestion is more than welcome! :-) Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment