Skip to content

Commit

Permalink
vipshop#149 vjmap最多运行15分钟,超时后退出并建议用户更改模式
Browse files Browse the repository at this point in the history
  • Loading branch information
calvin1978 committed Dec 18, 2018
1 parent ec7e257 commit 2bcab4c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
11 changes: 9 additions & 2 deletions vjmap/src/main/java/com/vip/vjtools/vjmap/VJMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.vip.vjtools.vjmap.oops.LoadedClassAccessor;
import com.vip.vjtools.vjmap.oops.OldgenAccessor;
import com.vip.vjtools.vjmap.oops.SurvivorAccessor;
import com.vip.vjtools.vjmap.utils.TimeController.TimeoutException;

import sun.jvm.hotspot.HotSpotAgent;
import sun.jvm.hotspot.oops.ObjectHeap;
Expand Down Expand Up @@ -179,11 +180,11 @@ public void run() {

// 如果ctrl+C退出,仍尽量打印结果
if (oldGenProcessor != null) {
tty.println("VJMap exited by user, below is the uncompleted summary ");
tty.println("VJMap aborted. Below is the incomplete summary: ");
oldGenProcessor.printResult();
}
if (heapProcessor != null) {
tty.println("VJMap exited by user, below is the uncompleted summary ");
tty.println("VJMap aborted. Below is the incomplete summary: ");
heapProcessor.printResult();
}
tty.flush();
Expand Down Expand Up @@ -219,6 +220,10 @@ public void run() {
double secs = (endTime - startTime) / 1000.0d;
tty.printf("%n Heap traversal took %.1f seconds.%n", secs);
tty.flush();
} catch (TimeoutException e) {
tty.println("\n\nVJMap aborted by timeout.");
tty.println("Try to use live option to reduce the fragments which make progress very slow.");
tty.println("./vjmap.sh -old:live PID\n\n");
} catch (Exception e) {
tty.println("Error Happen:" + e.getMessage());
if (e.getMessage() != null && e.getMessage().contains("Can't attach to the process")) {
Expand Down Expand Up @@ -271,8 +276,10 @@ private static void printHelp() {
tty.println("vjmap " + VERSION
+ " - prints per GC generation (Eden, Survivor, OldGen) object details of a given process.");
tty.println("Usage: vjmap.sh <options> <PID>");
tty.println("Usage: vjmap.sh <options> <executable java path> <coredump file path>");
tty.println("");
tty.printf(format, "-all", "print all gens histogram, order by total size");
tty.printf(format, "-all:live", "print all gens histogram, live objects only");
tty.printf(format, "-all:minsize=1024", "print all gens histogram, total size>=1024");
tty.printf(format, "-all:minsize=1024,byname",
"print all gens histogram, total size>=1024, order by class name");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public boolean doObj(Oop obj) {

updateWith(classStats, objSize, place);

// 每完成1% 打印一个,每完成10% 打印百分比提示
// 每完成1% 打印一个.,每完成10% 打印百分比提示
progressNodifier.processingSize += objSize;
if (progressNodifier.processingSize > progressNodifier.nextNotificationSize) {
progressNodifier.printProgress();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class ProgressNotifier {

private PrintStream tty = System.out;

private TimeController timeController = new TimeController();

public ProgressNotifier(long totalSize) {
this.totalSize = totalSize;
onePercentSize = totalSize / 100;
Expand All @@ -26,6 +28,7 @@ public void printHead() {
}

public void printProgress() {
timeController.checkTimedOut();
tty.print(".");
processingPercent++;
nextNotificationSize += onePercentSize;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.vip.vjtools.vjmap.utils;

/**
* 最多等待15分钟,超时后打印当前结果并退出,建议用户使用live参数
*/
public class TimeController {

long start = System.currentTimeMillis();

// 15 minutes
long maxTime = Long.parseLong(System.getProperty("vjmap.timeout", String.valueOf(60 * 1000 * 15)));

public void checkTimedOut() {
if (System.currentTimeMillis() - start > maxTime) {
throw new TimeoutException();
}
}

public static class TimeoutException extends RuntimeException {

}
}

0 comments on commit 2bcab4c

Please sign in to comment.