Skip to content

Instantly share code, notes, and snippets.

@user454322
Forked from emres/CpuThreadAnalyser.sh
Created December 20, 2013 01:36
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 user454322/8049166 to your computer and use it in GitHub Desktop.
Save user454322/8049166 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Prints information about the Java threads that consumes most of the CPU
# Derived from the information given in the blog entry at
#
# http://nurkiewicz.blogspot.be/2012/08/which-java-thread-consumes-my-cpu.html
#
# Changelog:
#
# - Removed the `top', `perl', and `grep' dependencies.
# - Used a single command chain (instead of two, as done in the original blog
# entry) to assign two columns (process id and thread id) to an array variable.
#
# TO DO:
#
# - Remove `sort' dependency and use --sort of `ps' (variations of --sort=cpu, pcpu, c
# + / - did not sort according to CPU utilization). More research required for less
# dependency.
#
# - More testing required (as usual).
IDS=($(ps -fLC java | sort -rnk5 | head -n1 | cut -d' ' -f9,13))
PID=${IDS[0]}
NID=$(printf "%x" ${IDS[1]})
jstack $PID | grep -A500 $NID | grep -m1 "^$" -B 500
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment