Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding threadsearch (or s) param & Windows fix #96

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/main/assembly/distribution.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@
<id>distribution</id>
<formats>
<format>tar.gz</format>
<format>zip</format>
</formats>
<files>
<file>
<source>INSTALL</source>
<outputDirectory>/</outputDirectory>
</file>
<file>
<source>LICENSE</source>
<outputDirectory>/</outputDirectory>
</file>
<file>
<source>README.md</source>
<outputDirectory>/</outputDirectory>
</file>
<file>
<source>src/main/wrappers/jvmtop.bat</source>
<outputDirectory>/</outputDirectory>
Expand Down
24 changes: 20 additions & 4 deletions src/main/java/com/jvmtop/JvmTop.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
public class JvmTop
{

public static final String VERSION = "0.8.0 alpha";
public static final String VERSION = "0.9.1 beta";

private Double delay_ = 1.0;

Expand Down Expand Up @@ -112,6 +112,12 @@ private static OptionParser createOptionParser()
"sets displayed thread name length in detail mode (defaults to 30)")
.withRequiredArg().ofType(Integer.class);

parser
.acceptsAll(Arrays.asList(new String[] { "s", "threadsearch" }),
"search string in showed fields")
.withRequiredArg().ofType(String.class);


return parser;
}

Expand Down Expand Up @@ -149,6 +155,8 @@ public static void main(String[] args) throws Exception
boolean threadLimitEnabled = true;

Integer threadNameWidth = null;

String threadNameSearch = null;

if (a.hasArgument("delay"))
{
Expand Down Expand Up @@ -202,6 +210,11 @@ public static void main(String[] args) throws Exception
threadNameWidth = (Integer) a.valueOf("threadnamewidth");
}

if (a.hasArgument("threadsearch"))
{
threadNameSearch = (String) a.valueOf("threadsearch");
}

if (sysInfoOption)
{
outputSystemProps();
Expand Down Expand Up @@ -233,6 +246,10 @@ public static void main(String[] args) throws Exception
{
vmDetailView.setThreadNameDisplayWidth(threadNameWidth);
}
if (threadNameSearch != null)
{
vmDetailView.setThreadSearch(threadNameSearch);
}
jvmTop.run(vmDetailView);

}
Expand Down Expand Up @@ -330,9 +347,8 @@ private void clearTerminal()
{
if (System.getProperty("os.name").contains("Windows"))
{
//hack
System.out
.printf("%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n");
System.out.print("\033[H\033[2J");
System.out.flush();
}
else if (System.getProperty("jvmtop.altClear") != null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/jvmtop/view/AbstractConsoleView.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public String toHHMM(long millis)
StringBuilder sb = new StringBuilder();
Formatter formatter = new Formatter(sb);
formatter
.format("%2d:%2dm", millis / 1000 / 3600,
.format("%02d:%02dm", millis / 1000 / 3600,
(millis / 1000 / 60) % 60);
return sb.toString();
}
Expand Down
66 changes: 46 additions & 20 deletions src/main/java/com/jvmtop/view/VMDetailView.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ public class VMDetailView extends AbstractConsoleView

private boolean sortByTotalCPU_ = false;

private int numberOfDisplayedThreads_ = 10;
private int numberOfDisplayedThreads_ = 100;

private int threadNameDisplayWidth_ = 30;
private int threadNameDisplayWidth_ = 65;

private boolean displayedThreadLimit_ = true;
private boolean displayedThreadLimit_ = false;

private String threadSearch = null;
//TODO: refactor
private Map<Long, Long> previousThreadCPUMillis = new HashMap<Long, Long>();

Expand Down Expand Up @@ -120,15 +121,15 @@ public void printView() throws Exception
System.out.printf(" ARGS: [UNKNOWN] %n");
}

String join = join(vmInfo_.getRuntimeMXBean().getInputArguments(), " ");
if (join.length() > 65)
{
System.out.printf(" VMARGS: %s[...]%n", leftStr(join, 65));
}
else
{
System.out.printf(" VMARGS: %s%n", join);
}
String join = "\n " + join(vmInfo_.getRuntimeMXBean().getInputArguments(), "\n ");
// if (join.length() > 65)
// {
// System.out.printf(" VMARGS: %s[...]%n", leftStr(join, 65));
// }
// else
// {
System.out.printf(" VMARGS: %s%n", join);
// }

System.out.printf(" VM: %s %s %s%n", properties.get("java.vendor"),
properties.get("java.vm.name"), properties.get("java.version"));
Expand Down Expand Up @@ -159,10 +160,11 @@ public void printView() throws Exception
/**
* @throws Exception
*/
@SuppressWarnings("unchecked")
private void printTopThreads() throws Exception
{
System.out.printf(" %6s %-" + threadNameDisplayWidth_
+ "s %13s %8s %8s %5s %n", "TID", "NAME", "STATE", "CPU",
+ "s %-13s %-8s %-8s %-5s %n", "TID", "NAME", "STATE", "CPU",
"TOTALCPU", "BLOCKEDBY");

if (vmInfo_.getThreadMXBean().isThreadCpuTimeSupported())
Expand All @@ -173,19 +175,30 @@ private void printTopThreads() throws Exception

Map<Long, Long> cpuTimeMap = new TreeMap<Long, Long>();

String threadSearchLower = (threadSearch==null?null:threadSearch.toLowerCase());
for (Long tid : vmInfo_.getThreadMXBean().getAllThreadIds())
{
long threadCpuTime = vmInfo_.getThreadMXBean().getThreadCpuTime(tid);
ThreadInfo info = vmInfo_.getThreadMXBean().getThreadInfo(tid);
long deltaThreadCpuTime = 0;
if (previousThreadCPUMillis.containsKey(tid))
{
deltaThreadCpuTime = threadCpuTime - previousThreadCPUMillis.get(tid);

cpuTimeMap.put(tid, deltaThreadCpuTime);
if (threadSearch != null && info != null) {
if (info.getThreadName().toLowerCase().contains(threadSearchLower)
||(""+info.getThreadState()).toLowerCase().contains(threadSearchLower)
||(tid.toString()).toLowerCase().contains(threadSearchLower))
{
cpuTimeMap.put(tid, deltaThreadCpuTime);
}
}
else
{
cpuTimeMap.put(tid, deltaThreadCpuTime);
}
}
newThreadCPUMillis.put(tid, threadCpuTime);
}

cpuTimeMap = sortByValue(cpuTimeMap, true);

int displayedThreads = 0;
Expand All @@ -202,15 +215,17 @@ private void printTopThreads() throws Exception
{
System.out.printf(
" %6d %-" + threadNameDisplayWidth_
+ "s %13s %5.2f%% %5.2f%% %5s %n",
+ "s %-13s %5.2f%% %5.2f%% %5s %n",
tid,
leftStr(info.getThreadName(), threadNameDisplayWidth_),
info.getThreadState(),
getThreadCPUUtilization(cpuTimeMap.get(tid),
vmInfo_.getDeltaUptime()),
getThreadCPUUtilization(vmInfo_.getThreadMXBean()
.getThreadCpuTime(tid), vmInfo_.getProxyClient()
.getProcessCpuTime(), 1), getBlockedThread(info));
getThreadCPUUtilization(
vmInfo_.getThreadMXBean().getThreadCpuTime(tid),
vmInfo_.getProxyClient().getProcessCpuTime(),
1),
getBlockedThread(info));
}
}
if (newThreadCPUMillis.size() >= numberOfDisplayedThreads_
Expand Down Expand Up @@ -273,6 +288,17 @@ public void setThreadNameDisplayWidth(int threadNameDisplayWidth_)
this.threadNameDisplayWidth_ = threadNameDisplayWidth_;
}

public String getThreadSearch()
{
return threadSearch;
}

public void setThreadSearch(String threadSearch_)
{
this.threadSearch = threadSearch_;
}


private double getThreadCPUUtilization(long deltaThreadCpuTime, long totalTime)
{
return getThreadCPUUtilization(deltaThreadCpuTime, totalTime, 1000 * 1000);
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/jvmtop/view/VMOverviewView.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,19 @@ else if (vmInfo.getState() == VMInfoState.ATTACHED_UPDATE_ERROR)
{
System.out
.printf(
"%5d %-15.15s [ERROR: Could not fetch telemetries (Process DEAD?)] %n",
"%5d %-25.25s [ERROR: Could not fetch telemetries (Process DEAD?)] %n",
vmInfo.getId(), getEntryPointClass(vmInfo.getDisplayName()));

}
else if (vmInfo.getState() == VMInfoState.ERROR_DURING_ATTACH)
{
System.out.printf("%5d %-15.15s [ERROR: Could not attach to VM] %n",
System.out.printf("%5d %-25.25s [ERROR: Could not attach to VM] %n",
vmInfo.getId(), getEntryPointClass(vmInfo.getDisplayName()));
}
else if (vmInfo.getState() == VMInfoState.CONNECTION_REFUSED)
{
System.out.printf(
"%5d %-15.15s [ERROR: Connection refused/access denied] %n",
"%5d %-25.25s [ERROR: Connection refused/access denied] %n",
vmInfo.getId(), getEntryPointClass(vmInfo.getDisplayName()));
}

Expand Down Expand Up @@ -121,7 +121,7 @@ private void printVM(VMInfo vmInfo) throws Exception

System.out
.printf(
"%5d %-15.15s %5s %5s %5s %5s %5.2f%% %5.2f%% %-5.5s %8.8s %4d %2.2s%n",
"%5d %-25.25s %5s %5s %5s %5s %5.2f%% %5.2f%% %-5.5s %8.8s %4d %2.2s%n",
vmInfo.getId(), getEntryPointClass(vmInfo.getDisplayName()),
toMB(vmInfo.getHeapUsed()), toMB(vmInfo.getHeapMax()),
toMB(vmInfo.getNonHeapUsed()), toMB(vmInfo.getNonHeapMax()),
Expand Down Expand Up @@ -173,7 +173,7 @@ private void scanForNewVMs()
*/
private void printHeader()
{
System.out.printf("%5s %-15.15s %5s %5s %5s %5s %6s %6s %5s %8s %4s %2s%n",
System.out.printf("%5s %-25.25s %5s %5s %5s %5s %6s %6s %5s %8s %4s %2s%n",
"PID", "MAIN-CLASS", "HPCUR", "HPMAX", "NHCUR", "NHMAX", "CPU", "GC",
"VM", "USERNAME", "#T", "DL");
}
Expand Down