user375868
user375868

Reputation: 1378

linux top command CPU usage doesn't add up / how to view Java threads usage in realtime?

When I run top -p <pid> command, I can see that process is using about 10% of cpu but when I press "H" to list threads, cpu usage of each of the thread is shown as 0%.

Why is that so? I want to see Java threads CPU usage in realtime, is top not the best way?

Screenshots (before pressing H)

enter image description here

After pressing H-

enter image description here

Upvotes: 1

Views: 1616

Answers (4)

Mohit M
Mohit M

Reputation: 829

Java 1.6 and above version have inbuild java tools for monitoring / debugging java modules. Here isthe below sample script : ` #/bin/bash

#Initilization of variable
CUREENT_DATE=`date -d "0 day" "+%Y%m%d"`      #2012-06-22
CURRENT_MONTH=`date -d "0 month" "+%B%Y"`      #June2012
LAST_DATE=`date -d "1 day ago" "+%Y%m%d"`  #2012-06-21
BASE_PATH="/home/javaMonitoring"
SCRIPT_LOGS="/home/javaMonitoring/logs"
JAVA_PATH=`echo $JAVA_HOME`
PERM=0
MSG="No Message"
HOSTIP="localhost"
THRESHOLD=80;


if [ ! -d $BASE_PATH ];then
        echo " Base Directory not exists![$BASE_PATH]"
        exit
fi
cd $BASE_PATH
if [ ! -d $SCRIPT_LOGS ];then
                echo " Log Directory note found [$SCRIPT_LOGS]"
                mkdir -p $SCRIPT_LOGS
        fi

##LOG Directory
if [ ! -d $CUREENT_DATE ];then
                echo " Log Directory note found [$CUREENT_DATE]"
                mkdir -p $CUREENT_DATE
        fi

log_file_path="$SCRIPT_LOGS/${CUREENT_DATE}-"`basename $0 .sh`".log"

echo "############ Script:$log_file_path##################" >$log_file_path
#####Funcations

cd $JAVA_PATH/bin
BOOTSTRAP=`${JAVA_PATH}/bin/jps|grep Bootstrap|cut -d " " -f1`
if [ $? -ne 0 ];then
                        echo "No Bootstrap PID exists[${JAVA_PATH}/bin/jps|grep Bootstrap]" >>$log_file_path
                        exit

#If log file not exists
else
        if [ ! -f ${BASE_PATH}/${CUREENT_DATE}/gcutil.txt ];then
                touch ${BASE_PATH}/${CUREENT_DATE}/gcutil.txt
                echo "Timestamp         S0     S1     E      O      P     YGC     YGCT    FGC    FGCT     GCT" > ${BASE_PATH}/${CUREENT_DATE}/gcutil.txt
        fi

        ./jstat -gcutil -t $BOOTSTRAP|grep -v Timestamp >> ${BASE_PATH}/${CUREENT_DATE}/gcutil.txt
        PERM=`jstat -gcutil -t  ${BOOTSTRAP}  | grep -v Timestamp|awk -F " " '{print $6}'`

                #if [ ${PERM} -ge ${THRESHOLD} ];then
                if [ $PERM > $THRESHOLD ];then
                         MSG="MCOM: Alert `date` on ${HOSTIP} Server Current Perm is  :: ${PERM}%"

                fi

#continue;
fi `

OUTPUT save in :: ../YYYYMMDD/gcutil.txt

Timestamp         S0     S1     E      O      P     YGC     YGCT    FGC    FGCT     GCT
        16193.4   0.00  59.95  55.13  93.86  89.63    226   26.192    41   48.532   74.724
        16196.5   0.00  59.95  55.14  93.86  89.63    226   26.192    41   48.532   74.724

Upvotes: 1

dcernahoschi
dcernahoschi

Reputation: 15250

If you prefer to work with a script like tool there is also jvmtop that gives you the cpu usage per thread.

Sample output bellow.

Sample output

Upvotes: 2

zengr
zengr

Reputation: 38909

You can use VisualVM or jconsole.

Upvotes: 1

shazin
shazin

Reputation: 21923

You can use jconsole if you are using the Sun Java JDK Distribution. It is built in as part of the bundle from Java 1.5 onwards if I am not mistaken. You can see Threads, Classes loaded, Memory Usage and CPU Usage of JVMs in real time.

Upvotes: 2

Related Questions