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

Param height #112

Open
wants to merge 4 commits 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
17 changes: 14 additions & 3 deletions src/main/java/com/jvmtop/JvmTop.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ private static OptionParser createOptionParser()
.acceptsAll(Arrays.asList(new String[] { "w", "width" }),
"Width in columns for the console display").withRequiredArg().ofType(Integer.class);

parser
.acceptsAll(Arrays.asList(new String[] { "h", "height" }),
"Height in rows for the console display").withRequiredArg().ofType(Integer.class);

parser
.accepts("threadnamewidth",
"sets displayed thread name length in detail mode (defaults to 30)")
Expand Down Expand Up @@ -138,6 +142,8 @@ public static void main(String[] args) throws Exception

Integer width = null;

Integer height = null;

double delay = 1.0;

boolean profileMode = a.has("profile");
Expand Down Expand Up @@ -180,6 +186,11 @@ public static void main(String[] args) throws Exception
width = (Integer) a.valueOf("width");
}

if (a.hasArgument("height"))
{
height = (Integer) a.valueOf("height");
}

if (a.hasArgument("threadlimit"))
{
threadlimit = (Integer) a.valueOf("threadlimit");
Expand Down Expand Up @@ -213,17 +224,17 @@ public static void main(String[] args) throws Exception
jvmTop.setMaxIterations(iterations);
if (pid == null)
{
jvmTop.run(new VMOverviewView(width));
jvmTop.run(new VMOverviewView(width, height));
}
else
{
if (profileMode)
{
jvmTop.run(new VMProfileView(pid, width));
jvmTop.run(new VMProfileView(pid, width, height));
}
else
{
VMDetailView vmDetailView = new VMDetailView(pid, width);
VMDetailView vmDetailView = new VMDetailView(pid, width, height);
vmDetailView.setDisplayedThreadLimit(threadLimitEnabled);
if (threadlimit != null)
{
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/com/jvmtop/view/AbstractConsoleView.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,26 @@ public abstract class AbstractConsoleView implements ConsoleView

private static final int MIN_WIDTH = 80;

private static final int MIN_HEIGHT= 25;

private boolean shouldExit_ = false;

protected final int width;

protected final int height;

/**
*
*/
public AbstractConsoleView(Integer width)
public AbstractConsoleView(Integer width, Integer height)
{
super();
if (width == null) width = MIN_WIDTH;
if (width < MIN_WIDTH) width = MIN_WIDTH;
this.width = width;
if (height == null) height = MIN_HEIGHT;
if (height < MIN_HEIGHT) height = MIN_HEIGHT;
this.height= height;
}

/**
Expand Down
23 changes: 17 additions & 6 deletions src/main/java/com/jvmtop/view/VMDetailView.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ public class VMDetailView extends AbstractConsoleView
//TODO: refactor
private Map<Long, Long> previousThreadCPUMillis = new HashMap<Long, Long>();

public VMDetailView(int vmid, Integer width) throws Exception
public VMDetailView(int vmid, Integer width, Integer height) throws Exception
{
super(width);
super(width, height);
LocalVirtualMachine localVirtualMachine = LocalVirtualMachine
.getLocalVirtualMachine(vmid);
vmInfo_ = VMInfo.processNewVM(localVirtualMachine, vmid);
Expand Down Expand Up @@ -161,8 +161,11 @@ public void printView() throws Exception
*/
private void printTopThreads() throws Exception
{
System.out.printf(" %6s %-" + threadNameDisplayWidth_
+ "s %13s %8s %8s %5s %n", "TID", "NAME", "STATE", "CPU",
// FIXME: Updating a field of the class here isn't very smart
this.threadNameDisplayWidth_= getUsableThreadNameWidth();

System.out.printf("%6s %-" + threadNameDisplayWidth_
+ "s %13s %6s %8s %9s %n", "TID", "NAME", "STATE", "CPU",
"TOTALCPU", "BLOCKEDBY");

if (vmInfo_.getThreadMXBean().isThreadCpuTimeSupported())
Expand All @@ -188,6 +191,8 @@ private void printTopThreads() throws Exception

cpuTimeMap = sortByValue(cpuTimeMap, true);

this.numberOfDisplayedThreads_= height - 12; // 11 lines are taken up with static info, 1 line needs to be left at the bottom

int displayedThreads = 0;
for (Long tid : cpuTimeMap.keySet())
{
Expand All @@ -201,8 +206,8 @@ private void printTopThreads() throws Exception
if (info != null)
{
System.out.printf(
" %6d %-" + threadNameDisplayWidth_
+ "s %13s %5.2f%% %5.2f%% %5s %n",
"%6d %-" + threadNameDisplayWidth_
+ "s %13s %5.2f%% %5.2f%% %5s %n",
tid,
leftStr(info.getThreadName(), threadNameDisplayWidth_),
info.getThreadState(),
Expand Down Expand Up @@ -287,4 +292,10 @@ private double getThreadCPUUtilization(long deltaThreadCpuTime,
}
return deltaThreadCpuTime / factor / totalTime * 100d;
}


private int getUsableThreadNameWidth() {
// the usable width for the thread column is the terminal width - other columns - whitespace
return this.width - 7 - 14 - 7 - 9 - 10;
}
}
37 changes: 25 additions & 12 deletions src/main/java/com/jvmtop/view/VMOverviewView.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public class VMOverviewView extends AbstractConsoleView

private Map<Integer, LocalVirtualMachine> vmMap = new HashMap<Integer, LocalVirtualMachine>();

public VMOverviewView(Integer width) {
super(width);
public VMOverviewView(Integer width, Integer height) {
super(width, height);
}

public void printView() throws Exception
Expand All @@ -60,10 +60,12 @@ public void printView() throws Exception

Collections.sort(vmInfoList, VMInfo.CPU_LOAD_COMPARATOR);

//FIXME: Is this the correct width for this purpose?
int w= this.getUsableMainClassWidth();

for (VMInfo vmInfo : vmInfoList)
{
if (vmInfo.getState() == VMInfoState.ATTACHED
)
if (vmInfo.getState() == VMInfoState.ATTACHED)
{
printVM(vmInfo);
}
Expand All @@ -72,19 +74,19 @@ else if (vmInfo.getState() == VMInfoState.ATTACHED_UPDATE_ERROR)
System.out
.printf(
"%5d %-15.15s [ERROR: Could not fetch telemetries (Process DEAD?)] %n",
vmInfo.getId(), getEntryPointClass(vmInfo.getDisplayName()));
vmInfo.getId(), getEntryPointClass(vmInfo.getDisplayName(), w));

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

}
Expand All @@ -94,13 +96,13 @@ else if (vmInfo.getState() == VMInfoState.CONNECTION_REFUSED)
* @param name
* @return
*/
private String getEntryPointClass(String name)
private String getEntryPointClass(String name, int length)
{
if (name.indexOf(' ') > 0)
{
name = name.substring(0, name.indexOf(' '));
}
return rightStr(name, 15);
return rightStr(name, length);
}

/**
Expand All @@ -119,10 +121,12 @@ private void printVM(VMInfo vmInfo) throws Exception
deadlockState = "!D";
}

int w= this.getUsableMainClassWidth();

System.out
.printf(
"%5d %-15.15s %5s %5s %5s %5s %5.2f%% %5.2f%% %-5.5s %8.8s %4d %2.2s%n",
vmInfo.getId(), getEntryPointClass(vmInfo.getDisplayName()),
"%5d %-"+w+"."+w+"s %5s %5s %5s %5s %5.2f%% %5.2f%% %-5.5s %8.8s %4d %2.2s%n",
vmInfo.getId(), getEntryPointClass(vmInfo.getDisplayName(), w),
toMB(vmInfo.getHeapUsed()), toMB(vmInfo.getHeapMax()),
toMB(vmInfo.getNonHeapUsed()), toMB(vmInfo.getNonHeapMax()),
vmInfo.getCpuLoad() * 100, vmInfo.getGcLoad() * 100,
Expand All @@ -131,6 +135,13 @@ private void printVM(VMInfo vmInfo) throws Exception

}


private int getUsableMainClassWidth() {
// the usable width for the main-class column is the terminal width - other columns - whitespace
return this.width - 6 - 6 - 6 - 6 - 6 - 7 - 7 - 6 - 9 - 5 - 3;
}


/**
* @param vmList
* @throws Exception
Expand Down Expand Up @@ -173,7 +184,9 @@ private void scanForNewVMs()
*/
private void printHeader()
{
System.out.printf("%5s %-15.15s %5s %5s %5s %5s %6s %6s %5s %8s %4s %2s%n",
int w= this.getUsableMainClassWidth();

System.out.printf("%5s %-"+w+"."+w+"s %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
9 changes: 6 additions & 3 deletions src/main/java/com/jvmtop/view/VMProfileView.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public class VMProfileView extends AbstractConsoleView

private VMInfo vmInfo_;

public VMProfileView(int vmid, Integer width) throws Exception
public VMProfileView(int vmid, Integer width, Integer height) throws Exception
{
super(width);
super(width, height);
LocalVirtualMachine localVirtualMachine = LocalVirtualMachine
.getLocalVirtualMachine(vmid);
vmInfo_ = VMInfo.processNewVM(localVirtualMachine, vmid);
Expand Down Expand Up @@ -87,7 +87,10 @@ public void printView() throws Exception
// these are the spaces taken up by the formatting, the rest is usable
// for printing out the method name
w = width - (1 + 6 + 3 + 9 + 3 + 2);
for (Iterator<MethodStats> iterator = cpuSampler_.getTop(20).iterator(); iterator

int noOfMethods= height - 6; // 5 lines are taken up with static info, 1 line needs to be left at the bottom

for (Iterator<MethodStats> iterator = cpuSampler_.getTop(noOfMethods).iterator(); iterator
.hasNext();)
{
MethodStats stats = iterator.next();
Expand Down