Skip to content

Commit

Permalink
#97 支持-o cleanconsole 模式
Browse files Browse the repository at this point in the history
  • Loading branch information
calvin1978 committed Sep 6, 2018
1 parent 671b4d4 commit ee2c5c6
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 48 deletions.
18 changes: 9 additions & 9 deletions vjtop/src/main/java/com/vip/vjtools/vjtop/InteractiveTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.io.IOException;
import java.io.PrintStream;

import com.vip.vjtools.vjtop.VMDetailView.DetailMode;
import com.vip.vjtools.vjtop.VMDetailView.ThreadMode;

/**
* 与用户交互动态的控制器
Expand Down Expand Up @@ -121,21 +121,21 @@ private void changeDisplayMode() {

String mode = readLine(
" Input number of Display Mode(1.cpu, 2.syscpu 3.total cpu 4.total syscpu 5.memory 6.total memory, current "
+ app.view.mode + "): ");
DetailMode detailMode = DetailMode.parse(mode);
+ app.view.threadMode + "): ");
ThreadMode detailMode = ThreadMode.parse(mode);
if (detailMode == null) {
tty.println(" Wrong option for display mode(1-6)");
} else if (detailMode == app.view.mode) {
} else if (detailMode == app.view.threadMode) {
tty.println(" Nothing be changed");
} else {
if (app.view.mode.isCpuMode != detailMode.isCpuMode) {
if (app.view.threadMode.isCpuMode != detailMode.isCpuMode) {
app.view.switchCpuAndMemory();
app.view.mode = detailMode;
tty.println(" Display mode changed to " + app.view.mode + " for next flush");
app.view.threadMode = detailMode;
tty.println(" Display mode changed to " + app.view.threadMode + " for next flush");
app.interruptSleep();
} else {
app.view.mode = detailMode;
tty.println(" Display mode changed to " + app.view.mode + " for next flush(" + app.nextFlushTime()
app.view.threadMode = detailMode;
tty.println(" Display mode changed to " + app.view.threadMode + " for next flush(" + app.nextFlushTime()
+ "s later)");
}
}
Expand Down
16 changes: 8 additions & 8 deletions vjtop/src/main/java/com/vip/vjtools/vjtop/TopThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.io.IOException;
import java.lang.management.ThreadInfo;

import com.vip.vjtools.vjtop.VMDetailView.DetailMode;
import com.vip.vjtools.vjtop.VMDetailView.ThreadMode;
import com.vip.vjtools.vjtop.util.LongObjectHashMap;
import com.vip.vjtools.vjtop.util.LongObjectMap;
import com.vip.vjtools.vjtop.util.Utils;
Expand All @@ -21,7 +21,7 @@ public TopThread(VMInfo vmInfo) {
this.vmInfo = vmInfo;
}

public TopCpuResult topCpuThreads(DetailMode mode, int threadLimit) throws IOException {
public TopCpuResult topCpuThreads(ThreadMode mode, int threadLimit) throws IOException {

long tids[] = vmInfo.getThreadMXBean().getAllThreadIds();

Expand Down Expand Up @@ -83,16 +83,16 @@ public TopCpuResult topCpuThreads(DetailMode mode, int threadLimit) throws IOExc
}

// 按不同类型排序,过滤
if (mode == DetailMode.cpu) {
if (mode == ThreadMode.cpu) {
topTidArray = Utils.sortAndFilterThreadIdsByValue(result.threadCpuDeltaTimes, threadLimit);
result.noteableThreads = result.threadCpuDeltaTimes.size();
} else if (mode == DetailMode.syscpu) {
} else if (mode == ThreadMode.syscpu) {
topTidArray = Utils.sortAndFilterThreadIdsByValue(result.threadSysCpuDeltaTimes, threadLimit);
result.noteableThreads = result.threadSysCpuDeltaTimes.size();
} else if (mode == DetailMode.totalcpu) {
} else if (mode == ThreadMode.totalcpu) {
topTidArray = Utils.sortAndFilterThreadIdsByValue(result.threadCpuTotalTimes, threadLimit);
result.noteableThreads = result.threadCpuTotalTimes.size();
} else if (mode == DetailMode.totalsyscpu) {
} else if (mode == ThreadMode.totalsyscpu) {
topTidArray = Utils.sortAndFilterThreadIdsByValue(result.threadSysCpuTotalTimes, threadLimit);
result.noteableThreads = result.threadSysCpuTotalTimes.size();
} else {
Expand All @@ -112,7 +112,7 @@ public TopCpuResult topCpuThreads(DetailMode mode, int threadLimit) throws IOExc
}


public TopMemoryResult topMemoryThreads(DetailMode mode, int threadLimit) throws IOException {
public TopMemoryResult topMemoryThreads(ThreadMode mode, int threadLimit) throws IOException {
long tids[] = vmInfo.getThreadMXBean().getAllThreadIds();
TopMemoryResult result = new TopMemoryResult();

Expand Down Expand Up @@ -153,7 +153,7 @@ public TopMemoryResult topMemoryThreads(DetailMode mode, int threadLimit) throws

// 线程排序
long[] topTidArray;
if (mode == DetailMode.memory) {
if (mode == ThreadMode.memory) {
topTidArray = Utils.sortAndFilterThreadIdsByValue(result.threadMemoryDeltaBytesMap, threadLimit);
result.noteableThreads = result.threadMemoryDeltaBytesMap.size();
} else {
Expand Down
41 changes: 30 additions & 11 deletions vjtop/src/main/java/com/vip/vjtools/vjtop/VJTop.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import java.io.PrintStream;
import java.util.Arrays;

import com.vip.vjtools.vjtop.VMDetailView.DetailMode;
import com.vip.vjtools.vjtop.VMDetailView.OutputFormat;
import com.vip.vjtools.vjtop.VMDetailView.ThreadMode;
import com.vip.vjtools.vjtop.VMInfo.VMInfoState;
import com.vip.vjtools.vjtop.util.Formats;
import com.vip.vjtools.vjtop.util.Utils;
Expand Down Expand Up @@ -62,8 +63,8 @@ private static OptionParser createOptionParser() {
.withRequiredArg().ofType(Integer.class);

parser.acceptsAll(Arrays.asList(new String[] { "o", "output" }),
"output format: \n" + " console(default): console with warning and flush ascii code\n"
+ " cleanconsole: console without warning and flush ascii code\n"
"output format: \n" + " console(default): console with warning and flush ansi code\n"
+ " clean: console without warning and flush ansi code\n"
+ " text: plain text like /proc/status for 3rd tools\n")
.withRequiredArg().ofType(String.class);

Expand Down Expand Up @@ -102,7 +103,9 @@ public static void main(String[] args) {
}

// 3. create view
VMDetailView.DetailMode displayMode = parseDisplayMode(optionSet);
VMDetailView.ThreadMode threadMode = parseThreadMode(optionSet);
VMDetailView.OutputFormat format = parseOutputFormat(optionSet);

Integer width = null;
if (optionSet.hasArgument("width")) {
width = (Integer) optionSet.valueOf("width");
Expand All @@ -116,7 +119,7 @@ public static void main(String[] args) {
}
}

VMDetailView view = new VMDetailView(vminfo, displayMode, width, interval);
VMDetailView view = new VMDetailView(vminfo, format, threadMode, width, interval);

if (optionSet.hasArgument("limit")) {
Integer limit = (Integer) optionSet.valueOf("limit");
Expand All @@ -140,11 +143,10 @@ public static void main(String[] args) {
}

// 5. start thread to get user input
if (app.maxIterations == -1) {
if (app.maxIterations == -1 && format == OutputFormat.console) {
InteractiveTask task = new InteractiveTask(app);
if (task.inputEnabled()) {
view.displayCommandHints = true;

Thread interactiveThread = new Thread(task, "InteractiveThread");
interactiveThread.setDaemon(true);
interactiveThread.start();
Expand Down Expand Up @@ -196,13 +198,30 @@ private void run(VMDetailView view) throws Exception {
}
}

private static VMDetailView.DetailMode parseDisplayMode(OptionSet optionSet) {
VMDetailView.DetailMode displayMode = VMDetailView.DetailMode.cpu;
private static VMDetailView.ThreadMode parseThreadMode(OptionSet optionSet) {
VMDetailView.ThreadMode threadMode = VMDetailView.ThreadMode.cpu;
if (optionSet.hasArgument("mode")) {
Integer mode = (Integer) optionSet.valueOf("mode");
displayMode = DetailMode.parse(mode.toString());
threadMode = ThreadMode.parse(mode.toString());
}
return threadMode;
}

private static VMDetailView.OutputFormat parseOutputFormat(OptionSet optionSet) {
VMDetailView.OutputFormat outputFormat = VMDetailView.OutputFormat.console;
if (optionSet.hasArgument("output")) {
String format = (String) optionSet.valueOf("output");
if (format.equals("clean")) {
outputFormat = OutputFormat.cleanConsole;
} else if (format.equals("text")) {
outputFormat = OutputFormat.text;
}
}
// 同步是否支持ascii
if (!outputFormat.ansi) {
Formats.disableAnsi();
}
return displayMode;
return outputFormat;
}

private static String parsePid(OptionParser parser, OptionSet optionSet) {
Expand Down
47 changes: 33 additions & 14 deletions vjtop/src/main/java/com/vip/vjtools/vjtop/VMDetailView.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public class VMDetailView {
private static final int DEFAULT_WIDTH = 100;
private static final int MIN_WIDTH = 80;

public DetailMode mode;
public ThreadMode threadMode;
public OutputFormat format;
public int threadLimit = 10;
public int interval;
public String threadNameFilter = null;
Expand All @@ -36,16 +37,19 @@ public class VMDetailView {
private boolean firstTime = true;
public boolean displayCommandHints = false;

public VMDetailView(VMInfo vmInfo, DetailMode mode, Integer width, Integer interval) throws Exception {
public VMDetailView(VMInfo vmInfo, OutputFormat format, ThreadMode mode, Integer width, Integer interval)
throws Exception {
this.vmInfo = vmInfo;
this.topThread = new TopThread(vmInfo);
this.warning = vmInfo.warningRule;
this.mode = mode;
this.threadMode = mode;
this.format = format;
this.interval = interval;
setWidth(width);
}

public void printView() throws Exception {

Formats.clearTerminal();

long iterationStartTime = 0;
Expand Down Expand Up @@ -75,10 +79,10 @@ public void printView() throws Exception {

// 打印线程级别内容
try {
if (mode.isCpuMode) {
printTopCpuThreads(mode);
if (threadMode.isCpuMode) {
printTopCpuThreads(threadMode);
} else {
printTopMemoryThreads(mode);
printTopMemoryThreads(threadMode);
}
} catch (Exception e) {
e.printStackTrace();
Expand Down Expand Up @@ -170,7 +174,7 @@ private void printJvmInfo() {
System.out.println("");
}

private void printTopCpuThreads(DetailMode mode) throws IOException {
private void printTopCpuThreads(ThreadMode mode) throws IOException {
if (!vmInfo.threadCpuTimeSupported) {
System.out.printf("%n -Thread CPU telemetries are not available on the monitored jvm/platform-%n");
return;
Expand Down Expand Up @@ -237,7 +241,7 @@ private void printTopCpuThreads(DetailMode mode) throws IOException {
interval);
}

private void printTopMemoryThreads(DetailMode mode) throws IOException {
private void printTopMemoryThreads(ThreadMode mode) throws IOException {
if (!vmInfo.threadMemoryAllocatedSupported) {
System.out.printf(
"%n -Thread Memory Allocated telemetries are not available on the monitored jvm/platform-%n");
Expand Down Expand Up @@ -315,15 +319,21 @@ public void printIterationCost(long iterationStartTime, long iterationStartCpu)
private void printWelcome() {
if (firstTime) {
if (!vmInfo.isLinux) {
System.out.printf("%n OS isn't linux, Process's MEMORY, THREAD, DISK data will be skipped.%n");
System.out.printf("%n" + Formats.YELLOW_ANSI[0]
+ " OS isn't linux, Process's MEMORY, THREAD, DISK data will be skipped."
+ Formats.YELLOW_ANSI[1] + "%n");
}

if (!vmInfo.ioDataSupport) {
System.out.printf("%n /proc/%s/io is not readable, Process's DISK data will be skipped.%n", vmInfo.pid);
System.out.printf("%n" + Formats.YELLOW_ANSI[0]
+ " /proc/%s/io is not readable, Process's DISK data will be skipped." + Formats.YELLOW_ANSI[1]
+ "%n", vmInfo.pid);
}

if (!vmInfo.perfDataSupport) {
System.out.printf("%n Perfdata doesn't support, SAFE-POINT data will be skipped.%n");
System.out.printf(
"%n" + Formats.YELLOW_ANSI[0] + " Perfdata doesn't support, SAFE-POINT data will be skipped."
+ Formats.YELLOW_ANSI[1] + "%n");
}

System.out.printf("%n VMARGS: %s%n%n", vmInfo.vmArgs);
Expand Down Expand Up @@ -443,16 +453,16 @@ private int getThreadNameWidth() {
}


public enum DetailMode {
public enum ThreadMode {
cpu(true), totalcpu(true), syscpu(true), totalsyscpu(true), memory(false), totalmemory(false);

public boolean isCpuMode;

private DetailMode(boolean isCpuMode) {
private ThreadMode(boolean isCpuMode) {
this.isCpuMode = isCpuMode;
}

public static DetailMode parse(String mode) {
public static ThreadMode parse(String mode) {
switch (mode) {
case "1":
return cpu;
Expand All @@ -471,4 +481,13 @@ public static DetailMode parse(String mode) {
}
}
}

public enum OutputFormat {
console(true), cleanConsole(false), text(false);
OutputFormat(boolean ansi) {
this.ansi = ansi;
}

public boolean ansi;
}
}
20 changes: 14 additions & 6 deletions vjtop/src/main/java/com/vip/vjtools/vjtop/util/Formats.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,20 @@ public class Formats {
private static final long GB_SIZE = MB_SIZE * 1024;
private static final long TB_SIZE = GB_SIZE * 1024;

public static final String[] RED_ANSI = new String[] { "\033[31m\033[01m", "\033[0m" };
private static final String[] YELLOW_ANSI = new String[] { "\033[33m\033[01m", "\033[0m" };
public static String[] RED_ANSI = new String[] { "\033[31m\033[01m", "\033[0m" };
public static String[] YELLOW_ANSI = new String[] { "\033[33m\033[01m", "\033[0m" };
private static final String[] NORMAL_ANSI = new String[] { "", "" };
private static final String CLEAR_TERMINAL_ANSI_CMD = new String(
new byte[] { (byte) 0x1b, (byte) 0x5b, (byte) 0x32, (byte) 0x4a, (byte) 0x1b, (byte) 0x5b, (byte) 0x48 });


public static boolean isWindows = System.getProperty("os.name").toLowerCase(Locale.US).contains("windows");
private static boolean ansi = true;

public static void disableAnsi() {
ansi = false;
RED_ANSI = NORMAL_ANSI;
YELLOW_ANSI = NORMAL_ANSI;
}

public static String toMBWithColor(long bytes, LongWarning warning) {
String[] ansi = colorAnsi(bytes, warning);
Expand Down Expand Up @@ -134,7 +140,7 @@ public static String formatUsageWithColor(Usage usage, LongWarning warning) {
}

public static String[] colorAnsi(long value, LongWarning warning) {
if (isWindows || value < warning.yellow) {
if (isWindows || !ansi || value < warning.yellow) {
return NORMAL_ANSI;
} else if (value >= warning.red) {
return RED_ANSI;
Expand All @@ -144,7 +150,7 @@ public static String[] colorAnsi(long value, LongWarning warning) {
}

public static String[] colorAnsi(double value, DoubleWarning warning) {
if (isWindows || value < warning.yellow) {
if (isWindows || !ansi || value < warning.yellow) {
return NORMAL_ANSI;
} else if (value >= warning.red) {
return RED_ANSI;
Expand Down Expand Up @@ -250,7 +256,9 @@ public static long parseFromSize(String str) {
}

public static void clearTerminal() {
if (isWindows) {
if (!ansi) {

} else if (isWindows) {
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");
} else {
System.out.print(CLEAR_TERMINAL_ANSI_CMD);
Expand Down

0 comments on commit ee2c5c6

Please sign in to comment.