@@ -19,13 +19,12 @@ Options:
1919 -p, --pid find out the highest cpu consumed threads from the specifed java process,
2020 default from all java process.
2121 -c, --count set the thread count to show, default is 5
22- -u, --user set java process user
2322 -h, --help display this help and exit
2423EOF
2524 exit $1
2625}
2726
28- readonly ARGS=` getopt -n " $PROG " -a -o c:p:u: h -l count:,pid:,help -- " $@ " `
27+ readonly ARGS=` getopt -n " $PROG " -a -o c:p:h -l count:,pid:,help -- " $@ " `
2928[ $? -ne 0 ] && usage 1
3029eval set -- " ${ARGS} "
3130
@@ -39,10 +38,6 @@ while true; do
3938 pid=" $2 "
4039 shift 2
4140 ;;
42- -u|--user)
43- jvmuser=" $2 "
44- shift 2
45- ;;
4641 -h|--help)
4742 usage
4843 ;;
@@ -90,39 +85,34 @@ trap "cleanupWhenExit" EXIT
9085printStackOfThread () {
9186 local threadLine
9287 local count=1
93- while read threadLine ; do
94- local pid=` echo ${threadLine} | awk ' {print $1} ' `
95- local threadId=` echo ${threadLine} | awk ' {print $2} ' `
88+ while IFS= " " read -a line ; do
89+ local pid=${line[0]}
90+ local threadId=${line[1]}
9691 local threadId0x=` printf %x ${threadId} `
97- local user=` echo ${threadLine} | awk ' {print $3} ' `
98- local pcpu=` echo ${threadLine} | awk ' {print $5} ' `
92+ local user=${line[2]}
93+ local pcpu=${line[4]}
9994
10095 local jstackFile=/tmp/${uuid} _${pid}
101-
102-
103- [ ! -f " ${jstackFile} " ] && {
104- if [ ` uname -m` == " x86_64" ]; then
105- flag=" -J-d64" # x86-64 specify jstack option "-J-d64"
106- fi
107- if [ -z ${jvmuser+x} ]; then
108- # root user
109- jstackCmd=" $JAVA_HOME /bin/jstack $flag "
110- else
111- # non-root user use sudo -u user
112- jstackCmd=" sudo -u $jvmuser $JAVA_HOME /bin/jstack $flag "
113- fi
114- $jstackCmd ${pid} > ${jstackFile} || {
115- redEcho " Fail to jstack java process ${pid} !"
116- rm ${jstackFile}
117- continue
118- }
119- }
12096
97+ [ ! -f " ${jstackFile} " ] && {
98+ {
99+ if [ " ${user} " == " ${USER} " ]; then
100+ jstack ${pid} > ${jstackFile}
101+ else
102+ sudo -u ${user} jstack ${pid} > ${jstackFile}
103+ fi
104+ } || {
105+ redEcho " Fail to jstack java process ${pid} !"
106+ rm ${jstackFile}
107+ continue
108+ }
109+ }
121110 redEcho " [$(( count++ )) ] Busy(${pcpu} %) thread(${threadId} /0x${threadId0x} ) stack of java process(${pid} ) under user(${user} ):"
122111 sed " /nid=0x${threadId0x} /,/^$/p" -n ${jstackFile}
123112 done
124113}
125114
115+
126116ps -Leo pid,lwp,user,comm,pcpu --no-headers | {
127117 [ -z " ${pid} " ] &&
128118 awk ' $4=="java"{print $0}' ||
0 commit comments