Skip to content

Commit

Permalink
#67 live option implemented to collect live objects' stat
Browse files Browse the repository at this point in the history
  • Loading branch information
lixuanbin committed Sep 9, 2018
1 parent 7ca9ecf commit 43d0498
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 3 deletions.
6 changes: 6 additions & 0 deletions vjmap/README.md
Expand Up @@ -66,6 +66,12 @@ vjmap的运行需要一段时间,如果中途需要停止执行,请使用kil
./vjmap.sh -all:minsize=1024,byname PID > /tmp/histo.log
```

## 2.4 打印老年代中对象的统计信息,仅输出存活对象:

```
./vjmap.sh -old:live PID > /tmp/histo-old.log
```

# 3.输出示例


Expand Down
8 changes: 7 additions & 1 deletion vjmap/README_EN.md
Expand Up @@ -28,7 +28,7 @@ execute `kill -18 <PID_OF_TARGET_APP>` TWICE to awaken the target app.
## 2.1 Commands

```
// Prints object stats of all the heap, ordered by their respective size in total.
// Prints object stats of all gens, ordered by their respective size in total.
./vjmap.sh -all PID > /tmp/histo.log
// Prints oldgen object stats, ordered by size in OldGen. Only CMS is supported for this option.
Expand Down Expand Up @@ -70,6 +70,12 @@ for the CMS oldgen
./vjmap.sh -sur:minsize=1024,byname PID > /tmp/histo-sur.log
```

## 2.4 Prints object stats of old gen, live objects only:

```
./vjmap.sh -old:live PID > /tmp/histo-old.log
```

# 3.Outputs

## 3.1 Count Survivor Objects over the Age of 3.
Expand Down
8 changes: 8 additions & 0 deletions vjmap/pom.xml
Expand Up @@ -9,6 +9,7 @@
<properties>
<!-- change it to point to your sa-jdi.jar from your JDK -->
<sajdijar>${java.home}/../lib/sa-jdi.jar</sajdijar>
<toolsjar>${java.home}/../lib/tools.jar</toolsjar>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
Expand All @@ -23,6 +24,13 @@
<scope>system</scope>
<systemPath>${sajdijar}</systemPath>
</dependency>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>${java.version}</version>
<scope>system</scope>
<systemPath>${toolsjar}</systemPath>
</dependency>
</dependencies>

<build>
Expand Down
9 changes: 8 additions & 1 deletion vjmap/src/main/assembly/vjmap.sh
Expand Up @@ -18,10 +18,17 @@ if [ ! -f "$SAJDI_PATH" ] ; then
exit 1
fi

TOOLS_PATH=$JAVA_HOME/lib/tools.jar

if [ ! -f "$TOOLS_PATH" ] ; then
echo "$TOOLS_PATH doesn't exist !" >&2
exit 1
fi

echo -e "\033[31mWARNING!! STW(Stop-The-World) will be performed on your Java process, if this is NOT wanted, type 'Ctrl+C' to exit. \033[0m"

DIR=$( cd $(dirname $0) ; pwd -P )
JAVA_OPTS="-Xms512m -Xmx512m -Xmn400m -XX:+UseConcMarkSweepGC -XX:+TieredCompilation -Xverify:none -XX:AutoBoxCacheMax=20000"


"$JAVA_HOME"/bin/java $JAVA_OPTS -classpath $DIR/vjmap.jar:$SAJDI_PATH com.vip.vjtools.vjmap.VJMap $*
"$JAVA_HOME"/bin/java $JAVA_OPTS -classpath $DIR/vjmap.jar:$SAJDI_PATH:$TOOLS_PATH com.vip.vjtools.vjmap.VJMap $*
53 changes: 52 additions & 1 deletion vjmap/src/main/java/com/vip/vjtools/vjmap/VJMap.java
@@ -1,8 +1,11 @@
package com.vip.vjtools.vjmap;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.List;

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.LoadedClassAccessor;
Expand All @@ -12,6 +15,7 @@
import sun.jvm.hotspot.HotSpotAgent;
import sun.jvm.hotspot.oops.ObjectHeap;
import sun.jvm.hotspot.runtime.VM;
import sun.tools.attach.HotSpotVirtualMachine;;

public class VJMap {

Expand Down Expand Up @@ -67,6 +71,8 @@ public static void main(String[] args) {
boolean orderByName = false;
long minSize = -1;
int minAge = 3;
boolean live = false;
// boolean dead = false;
if (!(args.length == 2 || args.length == 3)) {
printHelp();
return;
Expand Down Expand Up @@ -94,6 +100,8 @@ public static void main(String[] args) {
return;
}
minAge = Integer.parseInt(values[1]);
} else if (addtionalFlag.toLowerCase().startsWith("live")) {
live = true;
}
}
}
Expand All @@ -108,8 +116,16 @@ public static void main(String[] args) {
coredumpPath = args[2];
}

if(live) {
if(pid == null) {
tty.println("only a running vm can be attached when live option is on");
return;
}
triggerGc(pid);
}

HotSpotAgent agent = new HotSpotAgent();

try {
if (args.length == 2) {
agent.attach(pid);
Expand Down Expand Up @@ -149,6 +165,40 @@ public static void main(String[] args) {
}
}

/**
* Trigger a remote gc using HotSpotVirtualMachine, inspired by jcmd's source code.
*
* @param pid
*/
private static void triggerGc(Integer pid) {
VirtualMachine vm = null;
try {
vm = VirtualMachine.attach(String.valueOf(pid));
HotSpotVirtualMachine hvm = (HotSpotVirtualMachine) vm;
try (InputStream in = hvm.executeJCmd("GC.run");) {
byte b[] = new byte[256];
int n;
do {
n = in.read(b);
if (n > 0) {
String s = new String(b, 0, n, "UTF-8");
tty.print(s);
}
} while (n > 0);
tty.println();
}
} catch (Exception e) {
tty.println(e.getMessage());
} finally {
if (vm != null) {
try {
vm.detach();
} catch (IOException e) {
tty.println(e.getMessage());
}
}
}
}

private static void printHelp() {
int leftLength = "-all:minsize=1024,byname".length();
Expand All @@ -163,6 +213,7 @@ private static void printHelp() {
"print all gens histogram, total size>=1024, order by class name");

tty.printf(format, "-old", "print oldgen histogram, order by oldgen size");
tty.printf(format, "-old:live", "print oldgen histogram, live objects only");
tty.printf(format, "-old:minsize=1024", "print oldgen histogram, oldgen size>=1024");
tty.printf(format, "-old:minsize=1024,byname",
"print oldgen histogram, oldgen size>=1024, order by class name");
Expand Down

0 comments on commit 43d0498

Please sign in to comment.