ARTIFACTORY: How to collect Artifactory thread dumps
Introduction
When trying to debug an issue in Artifactory, the issue is not always clear directly from the logs and may need a deeper understanding/investigation into Artifactory’s behavior. For this reason, we can take thread dumps. The thread dumps can be helpful in Diagnosing Performance issues such as application hangs and long-performing operations.
This article will walk you through how to take thread dumps in Artifactory, and how to set up a bash script to automatically take thread dumps when experiencing issues with Aritfactory without manual intervention.
How to Manually Take Thread dumps
The following methods are sorted and prioritized.
In some of the methods, you are required to obtain the PID of the tomcat process.
On Windows:
$ netstat -ano | findstr <PORT>
*With <PORT> being the port Artifactory id running on (default is 8081)
On Linux use:
$ ps -aef | grep java
Note: Starting from version 7.90.x, the Access microservice runs on a dedicated JVM. As a result, the command above will include process IDs for both Java of Artifactory and Access.
To find the PID of the Artifactory process, run:
ps -aef | grep /opt/jfrog/artifactory/app/artifactory
To find the PID of the Access process, run:ps -aef | grep /opt/jfrog/artifactory/app/access
Method #1 - Running ‘$ kill -3 <PID>’
The thread dump will be printed to the catalina.out file.
Please note that when Artifactory isn’t running as a service, the dump will go to the STDOUT of the shell session that runs the server and won't go to the timestamped catalina.out file.
Method #2 - Using the Support Bundle’s built-in thread dump collector
Thread dumps are collected live, so ensure that this process is run during an incident to collect thread dumps that capture the issue.
Method #3 - Using ‘jcmd’
-
Navigate to ‘{JAVA_HOME}/bin’ -
*Run $ ./jcmd <pid> Thread.print > "/tmp/artifactory.$(date +%Y%m%d%H%M%S).td"
*Note that the user executing jcmd has to be the same user who owns the JVM process or have sufficient privileges to access it (means that usually you have to run this as the 'artifactory' user).
Automatic Thread Dump Collection
Method #1 - Interval thread dumps
One can use the script below to set up thread dumps to be collected regularly. This is beneficial if you want an idea of how Artifactory is operating over a period of time:
It takes the PID as a variable to be passed with the running of the script e.g:
./threaddump.sh 34567
It has other hard coded variable- at the top of the script; they determine the thread dump frequency and retention of thread dumps. Please adjust as needed.
#!/bin/bash
# Ensure script is executed with a valid PID
if [ -z "$1" ]; then
echo "Usage: $0 <pid>"
exit 1
fi
PID=$1
THREAD_DIR="/home/tds"
INTERVAL=10 # Interval in seconds between dumps
ZIP_COUNTER=60 # Number of intervals before creating a zip
CURRENT_COUNTER=0 # Counter to track intervals
RETENTION_PERIOD=1440 # Retention period in minutes before deleting old zips
# Log file
LOG_FILE="$THREAD_DIR/script.log"
# Path to jcmd
JCMDPATH="/opt/jfrog/artifactory/app/third-party/java/bin/jcmd"
# Create directory if it doesn't exist
mkdir -p "$THREAD_DIR"
while true; do
# Collect thread dump
TIMESTAMP=$(date +%Y%m%d%H%M%S)
DUMP_FILE="$THREAD_DIR/artifactory.$TIMESTAMP.td"
if $JCMDPATH "$PID" Thread.print > "$DUMP_FILE"; then
echo "Thread dump created: $DUMP_FILE" >> "$LOG_FILE"
else
echo "Failed to create thread dump for PID $PID" >> "$LOG_FILE"
fi
((CURRENT_COUNTER++))
# Zip files every ZIP_COUNTER intervals
if [ "$CURRENT_COUNTER" -ge "$ZIP_COUNTER" ]; then
CURRENT_COUNTER=0
echo "Zip activity triggered at $(date +%d%H%M%S)" >> "$LOG_FILE"
ZIP_FILE="$THREAD_DIR/artifactory.$(date +%Y%m%d%H%M%S).zip"
# Zip files and suppress output
zip -r "$ZIP_FILE" "$THREAD_DIR"/*.td > /dev/null 2>&1
# Check if zip was successful before removing files
if [ $? -eq 0 ]; then
echo "Files zipped successfully: $ZIP_FILE" >> "$LOG_FILE"
rm "$THREAD_DIR"/*.td
else
echo "Failed to create zip file: $ZIP_FILE" >> "$LOG_FILE"
fi
fi
# Remove old zip files older than RETENTION_PERIOD minutes
OLD_ZIPS=$(find "$THREAD_DIR" -name 'artifactory.*.zip' -type f -mmin +$RETENTION_PERIOD)
if [ -n "$OLD_ZIPS" ]; then
echo "Retention deletion triggered at $(date +%d%H%M%S)" >> "$LOG_FILE"
for ZIP in $OLD_ZIPS; do
echo "Deleting old zip file: $ZIP" >> "$LOG_FILE"
rm "$ZIP"
done
fi
# Wait INTERVAL before taking the next dump
sleep "$INTERVAL"
done
Method #2 - Dynamic thread dumps
One can also collect thread dumps when Artifactory is detected to be unhealthy. This is done through regular health checks. If Artifactory is reported to be unhealthy (status code != 200) thread dumps will then be collected. By default, this health check occurs every 30 seconds, however, this can be adjusted at the top of the script.
#!/bin/bash
JCMD_PATH="/opt/jfrog/artifactory/app/third-party/java/bin/jcmd"
HEALTH_CHECK_URL="http://localhost:8082/router/api/v1/system/health"
THREAD_DUMP_DIR="/tmp"
DEFAULT_INTERVAL=30
INTERVAL=${1:-$DEFAULT_INTERVAL}
while true; do
# Get the process ID of Artifactory
pid=$(systemctl show -p MainPID --value artifactory)
# Check if the Artifactory process is running
if [ -z "$pid" ]; then
echo "Artifactory process not found or not running. Health check skipped."
else
# Perform health check
response=$(curl -s -o /dev/null -w "%{http_code}" $HEALTH_CHECK_URL)
if [ "$response" -eq 200 ]; then
echo "Health is good"
else
# Perform thread dump if health check fails
echo "Health check returned status $response. Performing thread dump."
$JCMD_PATH "$pid" Thread.print > "$THREAD_DUMP_DIR/artifactory.$(date +%Y%m%d%H%M%S).td"
break
fi
fi
sleep $INTERVAL
done