Skip to content

Instantly share code, notes, and snippets.

@ahgittin
Created September 16, 2012 22:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahgittin/3734619 to your computer and use it in GitHub Desktop.
Save ahgittin/3734619 to your computer and use it in GitHub Desktop.
script to run jstack but just show those threads which are active ... a poor man's profiler, or how to make eclipse beach ball slightly more exciting :)
#!/bin/bash
jstack $@ | awk '
function onTraceBegin() {
onTraceEnd()
data["lineCount"]=0
data["head"]=$0
}
function onTraceEnd() {
if ("head" in data) {
if (data["active"] && "topClass" in data) {
print data["head"]
for (l=1; l<=data["lineCount"]; l++) { print lines[l] }
print ""
}
}
delete lines
delete data
}
function startsWith(whole,prefix) {
return (substr(whole,1,length(prefix))==prefix);
}
{
if (!match($0,"[^\\s]")) onTraceEnd();
else if (substr($0,1,1)=="\"") onTraceBegin();
else if ("head" in data) {
lc = ++data["lineCount"];
lines[lc]=$0
if (lc==1) {
state=data["state"]=$2;
data["active"] = !(state=="WAITING" || state=="BLOCKED" || state=="TIMED_WAITING")
}
if ($1=="at") {
if ("topClass" in data) {
} else {
data["topClass"] = $0;
tc = $2;
if (data["active"]) {
if (startsWith(tc,"java.net") || startsWith(tc,"sun.nio")) data["active"]=false;
}
}
}
}
}
END {
onTraceEnd();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment