Sunday, August 7, 2011

Scripts to take multiple thread dump after regular interval - Weblogic

Its always recommended to take multiple thread dumps at close intervals (10 or 20 dumps at 10-30 seconds intervals). Why? A thread dump is a snapshot of threads in execution or various states. Taking multiple thread dumps allows us to peek into the threads as they continue execution.

In order take thread dumps use following script, name this scripts to some values, let's say we name it as td_take.sh and it is intended to make the task of collecting thread dumps easier. Its usage is quite simply and can be seen by running the script with no arguments, e.g.: ./td_take.sh
  1. Start the relevant Server the usual way.
  2. When the issue occurs, use td_take.sh to take thread dumps 10-20 seconds apart.
  3. After the Server has started completely, hung or crashed, provide us with the following:
  • Log file for server, usually located at: <WLS_HOME>/user_projects/domains/<domain_name>/servers/<server_name>/logs
  • Standard output file (.out) for server, usually located at: <WLS_HOME>/user_projects/domains/<domain_name>/servers/<server_name>/logs
  • If using the "nohup" command to start the server than the nohup.out, otherwise discard this last file.
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 #!/bin/sh

if [ $# -le 1 ]
then
    echo
    echo "Usage:"
    echo "To take threads indefinitely:"
    echo "    ./tdtake.sh <PID> <interval_in_seconds>"
    echo "To take a specific amount of threads:"
    echo "    ./tdtake.sh <PID> <interval_in_seconds> <amount_of_threads>
"
    echo "e.g.: ./tdtake.sh 512 10 5"
    echo "will take 5 thread dumps, 10 seconds apart each for process ID 512.
"

    exit 0
fi

function take_dumps {
    kill -3 $1
    echo Waiting $2 seconds...
    sleep $2
}

if [ -z $3 ]
then
    echo "Taking thread dumps indefinitely."
    echo "Press <Ctrl>+C to terminate."
    i=1
    while [ $i -gt 0 ]
    do
        echo Taking thread dump $i
        take_dumps $1 $2
        i=`expr $i + 1`
    done
else
    echo "Taking a total of $3 threads."
    echo "Press <Ctrl>+C to terminate before completion."
    i=1
    while [ $i -le $3 ]
    do
        echo Taking thread dump $i
        take_dumps $1 $2
        i=`expr $i + 1`
    done
fi

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =