Skip to content

Commit

Permalink
vjmap 在ctrl+C退出时,打印当前未完成的结果 #148
Browse files Browse the repository at this point in the history
  • Loading branch information
calvin1978 committed Dec 18, 2018
1 parent 25d7dfa commit ec7e257
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 22 deletions.
4 changes: 1 addition & 3 deletions vjmap/src/main/java/com/vip/vjtools/vjmap/ClassStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@ public class ClassStats {

public ClassStats(Klass k) {
this.klass = k;
description = initDescription();
}

public String getDescription() {
if (description == null) {
description = initDescription();
}
return description;
}

Expand Down
77 changes: 67 additions & 10 deletions vjmap/src/main/java/com/vip/vjtools/vjmap/VJMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.sun.tools.attach.VirtualMachine;
import com.vip.vjtools.vjmap.oops.GenAddressAccessor;
import com.vip.vjtools.vjmap.oops.HeapHistogramVisitor;
import com.vip.vjtools.vjmap.oops.HeapUtils;
import com.vip.vjtools.vjmap.oops.LoadedClassAccessor;
import com.vip.vjtools.vjmap.oops.OldgenAccessor;
import com.vip.vjtools.vjmap.oops.SurvivorAccessor;
Expand All @@ -19,22 +20,41 @@

public class VJMap {

public static final String VERSION = "1.0.7";
public static final String VERSION = "1.0.8";

private static PrintStream tty = System.out;
// 用于ctrl-C退出时仍然打印结果
private static OldGenProcessor oldGenProcessor;
private static HeapProcessor heapProcessor;

public static void runHeapVisitor(int pid, boolean orderByName, long minSize) {
ObjectHeap heap = VM.getVM().getObjectHeap();
HeapHistogramVisitor visitor = new HeapHistogramVisitor();
heapProcessor = new HeapProcessor(orderByName, minSize);

tty.println("Iterating over heap. This may take a while...");
tty.println("Geting live regions...");

heap.iterate(visitor);
heap.iterate(heapProcessor.visitor);

List<ClassStats> list = visitor.getClassStatsList();
ResultPrinter resultPrinter = new ResultPrinter();
resultPrinter.printAllGens(tty, list, orderByName, minSize);
heapProcessor.printResult();
heapProcessor = null;
}

public static class HeapProcessor {
HeapHistogramVisitor visitor = new HeapHistogramVisitor();
boolean orderByName;
long minSize;

public HeapProcessor(boolean orderByName, long minSize) {
this.orderByName = orderByName;
this.minSize = minSize;
}

public void printResult() {
List<ClassStats> list = HeapUtils.getClassStatsList(visitor.getClassStatsMap());
ResultPrinter resultPrinter = new ResultPrinter();
resultPrinter.printAllGens(tty, list, orderByName, minSize);
}
}

public static void runSurviorAccessor(int age, int minAge, boolean orderByName, long minSize) {
Expand All @@ -48,13 +68,28 @@ public static void runSurviorAccessor(int age, int minAge, boolean orderByName,
}

public static void runOldGenAccessor(boolean orderByName, long minSize) {
oldGenProcessor = new OldGenProcessor(orderByName, minSize);
tty.println("Iterating over oldgen area. This may take a while...");
oldGenProcessor.accessor.caculateHistogram();
oldGenProcessor.printResult();
oldGenProcessor = null;
}

public static class OldGenProcessor {
OldgenAccessor accessor = new OldgenAccessor();
boolean orderByName;
long minSize;

tty.println("Iterating over oldgen area. This may take a while...");
List<ClassStats> list = accessor.caculateHistogram();
public OldGenProcessor(boolean orderByName, long minSize) {
this.orderByName = orderByName;
this.minSize = minSize;
}

ResultPrinter resultPrinter = new ResultPrinter();
resultPrinter.printOldGen(tty, list, orderByName, minSize);
public void printResult() {
List<ClassStats> list = HeapUtils.getClassStatsList(accessor.getClassStatsMap());
ResultPrinter resultPrinter = new ResultPrinter();
resultPrinter.printOldGen(tty, list, orderByName, minSize);
}
}

public static void printGenAddress() {
Expand All @@ -68,6 +103,7 @@ public static void printLoadedClass() {
}

public static void main(String[] args) {
// 分析参数
boolean orderByName = false;
long minSize = -1;
int minAge = 2;
Expand Down Expand Up @@ -124,6 +160,7 @@ public static void main(String[] args) {
coredumpPath = args[2];
}

// 如有需要,执行GC
if (live) {
if (pid == null) {
tty.println("only a running vm can be attached when live option is on");
Expand All @@ -132,14 +169,34 @@ public static void main(String[] args) {
triggerGc(pid);
}


//// 正式执行
HotSpotAgent agent = new HotSpotAgent();

Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {

// 如果ctrl+C退出,仍尽量打印结果
if (oldGenProcessor != null) {
tty.println("VJMap exited by user, below is the uncompleted summary ");
oldGenProcessor.printResult();
}
if (heapProcessor != null) {
tty.println("VJMap exited by user, below is the uncompleted summary ");
heapProcessor.printResult();
}
tty.flush();
}
});

try {
if (args.length == 2) {
agent.attach(pid);
} else {
agent.attach(executablePath, coredumpPath);
}

long startTime = System.currentTimeMillis();
if (modeFlag.startsWith("-all")) {
runHeapVisitor(pid, orderByName, minSize);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.vip.vjtools.vjmap.oops;

import java.util.HashMap;
import java.util.List;

import com.vip.vjtools.vjmap.ClassStats;
import com.vip.vjtools.vjmap.utils.ProgressNotifier;
Expand Down Expand Up @@ -145,8 +144,8 @@ public Place getParLocation(Oop obj) {
return Place.Unknown;
}

public List<ClassStats> getClassStatsList() {
return HeapUtils.getClassStatsList(classStatsMap);
public HashMap<Klass, ClassStats> getClassStatsMap() {
return classStatsMap;
}

public enum Place {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.PrintStream;
import java.util.HashMap;
import java.util.List;

import com.vip.vjtools.vjmap.ClassStats;
import com.vip.vjtools.vjmap.utils.ProgressNotifier;
Expand Down Expand Up @@ -34,11 +33,13 @@ public class OldgenAccessor {
private Address cur;
private Address regionStart;
private int liveRegions = 0;
private HashMap<Klass, ClassStats> classStatsMap = new HashMap<>(2048, 0.2f);

public List<ClassStats> caculateHistogram() {

HashMap<Klass, ClassStats> classStatsMap = new HashMap<>(2048, 0.2f);
public HashMap<Klass, ClassStats> getClassStatsMap() {
return classStatsMap;
}

public void caculateHistogram() {
ObjectHeap objectHeap = HeapUtils.getObjectHeap();
CollectedHeap heap = checkHeapType();
ConcurrentMarkSweepGeneration cmsGen = HeapUtils.getOldGenForCMS(heap);
Expand Down Expand Up @@ -91,8 +92,6 @@ public List<ClassStats> caculateHistogram() {
}

tty.println("\ntotal live regions:" + liveRegions);

return HeapUtils.getClassStatsList(classStatsMap);
}

private CollectedHeap checkHeapType() {
Expand Down

0 comments on commit ec7e257

Please sign in to comment.