From 8d4ff9dc59e32ed9f7979968ce534ee55f1feb79 Mon Sep 17 00:00:00 2001 From: Marco Herrn Date: Thu, 18 Oct 2018 10:03:33 +0200 Subject: [PATCH 1/4] Added parameter height for setting terminal height - Currently only respected by the profile view (as the width parameter) - Minimum allowed height is 25 - Closes: patric-r/jvmtop#105 --- src/main/java/com/jvmtop/JvmTop.java | 17 ++++++++++++++--- .../com/jvmtop/view/AbstractConsoleView.java | 9 ++++++++- src/main/java/com/jvmtop/view/VMDetailView.java | 4 ++-- .../java/com/jvmtop/view/VMOverviewView.java | 4 ++-- .../java/com/jvmtop/view/VMProfileView.java | 9 ++++++--- 5 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/jvmtop/JvmTop.java b/src/main/java/com/jvmtop/JvmTop.java index dd4165c..e137202 100644 --- a/src/main/java/com/jvmtop/JvmTop.java +++ b/src/main/java/com/jvmtop/JvmTop.java @@ -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)") @@ -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"); @@ -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"); @@ -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) { diff --git a/src/main/java/com/jvmtop/view/AbstractConsoleView.java b/src/main/java/com/jvmtop/view/AbstractConsoleView.java index 6f4de12..7b48ccd 100644 --- a/src/main/java/com/jvmtop/view/AbstractConsoleView.java +++ b/src/main/java/com/jvmtop/view/AbstractConsoleView.java @@ -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; } /** diff --git a/src/main/java/com/jvmtop/view/VMDetailView.java b/src/main/java/com/jvmtop/view/VMDetailView.java index 21a40f0..5c3c7c8 100644 --- a/src/main/java/com/jvmtop/view/VMDetailView.java +++ b/src/main/java/com/jvmtop/view/VMDetailView.java @@ -55,9 +55,9 @@ public class VMDetailView extends AbstractConsoleView //TODO: refactor private Map previousThreadCPUMillis = new HashMap(); - 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); diff --git a/src/main/java/com/jvmtop/view/VMOverviewView.java b/src/main/java/com/jvmtop/view/VMOverviewView.java index 6f8d862..1c79077 100644 --- a/src/main/java/com/jvmtop/view/VMOverviewView.java +++ b/src/main/java/com/jvmtop/view/VMOverviewView.java @@ -45,8 +45,8 @@ public class VMOverviewView extends AbstractConsoleView private Map vmMap = new HashMap(); - public VMOverviewView(Integer width) { - super(width); + public VMOverviewView(Integer width, Integer height) { + super(width, height); } public void printView() throws Exception diff --git a/src/main/java/com/jvmtop/view/VMProfileView.java b/src/main/java/com/jvmtop/view/VMProfileView.java index aba76ac..672d061 100644 --- a/src/main/java/com/jvmtop/view/VMProfileView.java +++ b/src/main/java/com/jvmtop/view/VMProfileView.java @@ -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); @@ -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 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 iterator = cpuSampler_.getTop(noOfMethods).iterator(); iterator .hasNext();) { MethodStats stats = iterator.next(); From bafd52dd47a67cec5dd4fca9316237d5c7d25e01 Mon Sep 17 00:00:00 2001 From: Marco Herrn Date: Thu, 18 Oct 2018 12:29:52 +0200 Subject: [PATCH 2/4] Respect width parameter in OverviewView --- .../java/com/jvmtop/view/VMOverviewView.java | 33 +++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/jvmtop/view/VMOverviewView.java b/src/main/java/com/jvmtop/view/VMOverviewView.java index 1c79077..6b72f56 100644 --- a/src/main/java/com/jvmtop/view/VMOverviewView.java +++ b/src/main/java/com/jvmtop/view/VMOverviewView.java @@ -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); } @@ -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)); } } @@ -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); } /** @@ -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, @@ -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 @@ -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"); } From eda72bd7bf05861445a9f0427a303a61b562d36a Mon Sep 17 00:00:00 2001 From: Marco Herrn Date: Thu, 18 Oct 2018 13:30:07 +0200 Subject: [PATCH 3/4] Respect width parameter in DetailView --- src/main/java/com/jvmtop/view/VMDetailView.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/jvmtop/view/VMDetailView.java b/src/main/java/com/jvmtop/view/VMDetailView.java index 5c3c7c8..91c2132 100644 --- a/src/main/java/com/jvmtop/view/VMDetailView.java +++ b/src/main/java/com/jvmtop/view/VMDetailView.java @@ -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()) @@ -201,8 +204,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(), @@ -287,4 +290,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; + } } From 23c9551f4d23c057e501ba0c90ebf7743d8fb61c Mon Sep 17 00:00:00 2001 From: Marco Herrn Date: Thu, 18 Oct 2018 13:38:11 +0200 Subject: [PATCH 4/4] Respect height parameter in DetailView - Closes: patric-r/jvmtop#26 --- src/main/java/com/jvmtop/view/VMDetailView.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/com/jvmtop/view/VMDetailView.java b/src/main/java/com/jvmtop/view/VMDetailView.java index 91c2132..a366ff5 100644 --- a/src/main/java/com/jvmtop/view/VMDetailView.java +++ b/src/main/java/com/jvmtop/view/VMDetailView.java @@ -191,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()) {