Skip to content

Instantly share code, notes, and snippets.

@ludenus
Last active June 29, 2022 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ludenus/5056287f2967e7becce29c71ad2a8c96 to your computer and use it in GitHub Desktop.
Save ludenus/5056287f2967e7becce29c71ad2a8c96 to your computer and use it in GitHub Desktop.
This script run java async profiler against dockerized app
#!/bin/bash
set -e
function die() {
local msg=${1:-"ERROR: die for reason unknown"}
local code=${2:-254}
echo "${msg}" >&2
exit ${code}
}
export app_name=$1
export profile_events=${2:-'cpu'}
export profile_duration_seconds=${3:-'100'}
echo "==== This script launches java async profiler against application running in docker"
if [ -z "${app_name}" ]; then
echo "ERROR: app_name not specified" >&2
die "USAGE: $0 <app_name> [profile_events] [profile_duration_seconds]" 11
fi
if [ "`sysctl kernel.perf_event_paranoid`" != "kernel.perf_event_paranoid = 1" ]; then
die "ERROR: java async profiler requires
kernel.perf_event_paranoid = 1
current value is:
`sysctl kernel.perf_event_paranoid`
Check https://github.com/jvm-profiling-tools/async-profiler#basic-usage for details.
" 12
fi
if [ "`sysctl kernel.kptr_restrict`" != "kernel.kptr_restrict = 0" ]; then
die "ERROR: java async profiler requires
kernel.kptr_restrict = 0
current value is:
`sysctl kernel.kptr_restrict`
Check https://github.com/jvm-profiling-tools/async-profiler#basic-usage for details.
" 13
fi
export version=${VERSION:-'2.8.1'}
export async_profiler_tarball_url="https://github.com/jvm-profiling-tools/async-profiler/releases/download/v${version}/async-profiler-${version}-linux-x64.tar.gz"
export workdir=${WORKDIR:-'/tmp'}
echo "==== Create workdir: $workdir"
mkdir -p ${workdir}
echo "==== Check java async profiler package"
if [ ! -d "${workdir}/async-profiler-${version}-linux-x64" ]; then
echo "==== Download java async profiler package: ${async_profiler_tarball_url} "
wget ${async_profiler_tarball_url} -O - | tar -xz -C ${workdir}
else
echo "==== Java async profiler package found: ${workdir}/async-profiler-${version}-linux-x64"
fi
echo "==== Resolve ${app_name} container id"
export resolved_container_id=`docker ps | grep "${app_name}" | awk '{print $1}'`
if [ -z "${resolved_container_id}" ]; then
die "ERROR: ${app_name} app is not running in docker" 21
fi
echo "==== Copy java async profiler into ${app_name} docker container: ${resolved_container_id}"
docker exec -i ${resolved_container_id} mkdir -p ${workdir}
docker cp ${workdir}/async-profiler-${version}-linux-x64 ${resolved_container_id}:${workdir}
echo "==== Resolve ${app_name} process id"
export resolved_pid=`docker top ${resolved_container_id} | grep com.flo.rater.RaterApplication | awk '{print $2}'`
if [ -z "${resolved_pid}" ]; then
die "ERROR: failed to detect ${app_name} PID" 22
fi
export report_html="async_profiler_${profile_events}_`date +'%Y-%m-%d_%H-%M-%S'`.html"
echo "==== Run java async profiler against ${app_name} PID: ${resolved_pid}, report_html: ${workdir}/${report_html}"
set -x
${workdir}/async-profiler-${version}-linux-x64/profiler.sh -d ${profile_duration_seconds} -e ${profile_events} --title ${profile_events} -f ${workdir}/${report_html} ${resolved_pid}
set +x
echo "==== Fetch profiler report from ${app_name} container ${resolved_container_id}"
docker cp ${resolved_container_id}:${workdir}/${report_html} ${workdir}
ls ${workdir}/${report_html}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment