pull/1787/head
hengyunabc 4 years ago
parent 5b9eb89826
commit b704e7cabd

@ -9,7 +9,7 @@ import com.taobao.arthas.common.OSUtils;
* @author hengyunabc 2021-04-26 * @author hengyunabc 2021-04-26
* @since 3.5.1 * @since 3.5.1
*/ */
public class Vmtool implements VMtoolMXBean { public class VmTool implements VmToolMXBean {
/** /**
* jni-lib * jni-lib
@ -34,16 +34,16 @@ public class Vmtool implements VMtoolMXBean {
} }
} }
private static Vmtool instance; private static VmTool instance;
private Vmtool() { private VmTool() {
} }
public static Vmtool getInstance() { public static VmTool getInstance() {
return getInstance(null); return getInstance(null);
} }
public static synchronized Vmtool getInstance(String libPath) { public static synchronized VmTool getInstance(String libPath) {
if (instance != null) { if (instance != null) {
return instance; return instance;
} }
@ -54,7 +54,7 @@ public class Vmtool implements VMtoolMXBean {
System.load(libPath); System.load(libPath);
} }
instance = new Vmtool(); instance = new VmTool();
return instance; return instance;
} }

@ -3,19 +3,19 @@ package arthas;
import java.util.ArrayList; import java.util.ArrayList;
/** /**
* VMtool interface for JMX server. How to register VMtool MBean: * VmTool interface for JMX server. How to register VmTool MBean:
* *
* <pre> * <pre>
* {@code * {@code
* ManagementFactory.getPlatformMBeanServer().registerMBean( * ManagementFactory.getPlatformMBeanServer().registerMBean(
* VMtool.getInstance(), * VmTool.getInstance(),
* new ObjectName("arthas:type=VMtool") * new ObjectName("arthas:type=VmTool")
* ); * );
* } * }
* </pre> * </pre>
* @author hengyunabc 2021-04-26 * @author hengyunabc 2021-04-26
*/ */
public interface VMtoolMXBean { public interface VmToolMXBean {
/** /**
* jni-libOK * jni-libOK
*/ */

@ -1,58 +1,58 @@
/* DO NOT EDIT THIS FILE - it is machine generated */ /* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h> #include <jni.h>
/* Header for class arthas_Vmtool */ /* Header for class arthas_VmTool */
#ifndef _Included_arthas_Vmtool #ifndef _Included_arthas_VmTool
#define _Included_arthas_Vmtool #define _Included_arthas_VmTool
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
/* /*
* Class: arthas_Vmtool * Class: arthas_VmTool
* Method: check * Method: check
* Signature: ()Ljava/lang/String; * Signature: ()Ljava/lang/String;
*/ */
JNIEXPORT jstring JNICALL Java_arthas_Vmtool_check0 JNIEXPORT jstring JNICALL Java_arthas_VmTool_check0
(JNIEnv *, jclass); (JNIEnv *, jclass);
/* /*
* Class: arthas_Vmtool * Class: arthas_VmTool
* Method: getInstances * Method: getInstances
* Signature: (Ljava/lang/Class;)Ljava/util/ArrayList; * Signature: (Ljava/lang/Class;)Ljava/util/ArrayList;
*/ */
JNIEXPORT jobject JNICALL Java_arthas_Vmtool_getInstances JNIEXPORT jobject JNICALL Java_arthas_VmTool_getInstances
(JNIEnv *, jclass, jclass); (JNIEnv *, jclass, jclass);
/* /*
* Class: arthas_Vmtool * Class: arthas_VmTool
* Method: sumInstanceSize * Method: sumInstanceSize
* Signature: (Ljava/lang/Class;)J * Signature: (Ljava/lang/Class;)J
*/ */
JNIEXPORT jlong JNICALL Java_arthas_Vmtool_sumInstanceSize JNIEXPORT jlong JNICALL Java_arthas_VmTool_sumInstanceSize
(JNIEnv *, jclass, jclass); (JNIEnv *, jclass, jclass);
/* /*
* Class: arthas_Vmtool * Class: arthas_VmTool
* Method: getInstanceSize * Method: getInstanceSize
* Signature: (Ljava/lang/Object;)J * Signature: (Ljava/lang/Object;)J
*/ */
JNIEXPORT jlong JNICALL Java_arthas_Vmtool_getInstanceSize JNIEXPORT jlong JNICALL Java_arthas_VmTool_getInstanceSize
(JNIEnv *, jclass, jobject); (JNIEnv *, jclass, jobject);
/* /*
* Class: arthas_Vmtool * Class: arthas_VmTool
* Method: countInstances * Method: countInstances
* Signature: (Ljava/lang/Class;)J * Signature: (Ljava/lang/Class;)J
*/ */
JNIEXPORT jlong JNICALL Java_arthas_Vmtool_countInstances JNIEXPORT jlong JNICALL Java_arthas_VmTool_countInstances
(JNIEnv *, jclass, jclass); (JNIEnv *, jclass, jclass);
/* /*
* Class: arthas_Vmtool * Class: arthas_VmTool
* Method: getAllLoadedClasses * Method: getAllLoadedClasses
* Signature: ()Ljava/util/ArrayList; * Signature: ()Ljava/util/ArrayList;
*/ */
JNIEXPORT jobject JNICALL Java_arthas_Vmtool_getAllLoadedClasses JNIEXPORT jobject JNICALL Java_arthas_VmTool_getAllLoadedClasses
(JNIEnv *, jclass); (JNIEnv *, jclass);
#ifdef __cplusplus #ifdef __cplusplus

@ -2,11 +2,11 @@
#include <jni.h> #include <jni.h>
#include <jni_md.h> #include <jni_md.h>
#include <jvmti.h> #include <jvmti.h>
#include "arthas_Vmtool.h" #include "arthas_VmTool.h"
extern "C" extern "C"
JNIEXPORT jstring JNICALL JNIEXPORT jstring JNICALL
Java_arthas_Vmtool_check0(JNIEnv *env, jclass thisClass) { Java_arthas_VmTool_check0(JNIEnv *env, jclass thisClass) {
return env->NewStringUTF("OK"); return env->NewStringUTF("OK");
} }
@ -47,7 +47,7 @@ HeapObjectCallback(jlong class_tag, jlong size, jlong *tag_ptr, void *user_data)
extern "C" extern "C"
JNIEXPORT jobject JNICALL JNIEXPORT jobject JNICALL
Java_arthas_Vmtool_getInstances0(JNIEnv *env, jclass thisClass, jclass klass) { Java_arthas_VmTool_getInstances0(JNIEnv *env, jclass thisClass, jclass klass) {
jvmtiEnv *jvmti = getJvmtiEnv(env); jvmtiEnv *jvmti = getJvmtiEnv(env);
@ -88,7 +88,7 @@ Java_arthas_Vmtool_getInstances0(JNIEnv *env, jclass thisClass, jclass klass) {
extern "C" extern "C"
JNIEXPORT jlong JNICALL JNIEXPORT jlong JNICALL
Java_arthas_Vmtool_sumInstanceSize0(JNIEnv *env, jclass thisClass, jclass klass) { Java_arthas_VmTool_sumInstanceSize0(JNIEnv *env, jclass thisClass, jclass klass) {
jvmtiEnv *jvmti = getJvmtiEnv(env); jvmtiEnv *jvmti = getJvmtiEnv(env);
@ -126,7 +126,7 @@ Java_arthas_Vmtool_sumInstanceSize0(JNIEnv *env, jclass thisClass, jclass klass)
} }
extern "C" extern "C"
JNIEXPORT jlong JNICALL Java_arthas_Vmtool_getInstanceSize0 JNIEXPORT jlong JNICALL Java_arthas_VmTool_getInstanceSize0
(JNIEnv *env, jclass thisClass, jobject instance) { (JNIEnv *env, jclass thisClass, jobject instance) {
jvmtiEnv *jvmti = getJvmtiEnv(env); jvmtiEnv *jvmti = getJvmtiEnv(env);
@ -142,7 +142,7 @@ JNIEXPORT jlong JNICALL Java_arthas_Vmtool_getInstanceSize0
extern "C" extern "C"
JNIEXPORT jlong JNICALL JNIEXPORT jlong JNICALL
Java_arthas_Vmtool_countInstances0(JNIEnv *env, jclass thisClass, jclass klass) { Java_arthas_VmTool_countInstances0(JNIEnv *env, jclass thisClass, jclass klass) {
jvmtiEnv *jvmti = getJvmtiEnv(env); jvmtiEnv *jvmti = getJvmtiEnv(env);
@ -172,7 +172,7 @@ Java_arthas_Vmtool_countInstances0(JNIEnv *env, jclass thisClass, jclass klass)
} }
extern "C" extern "C"
JNIEXPORT jobject JNICALL Java_arthas_Vmtool_getAllLoadedClasses0 JNIEXPORT jobject JNICALL Java_arthas_VmTool_getAllLoadedClasses0
(JNIEnv *env, jclass thisClass) { (JNIEnv *env, jclass thisClass) {
jvmtiEnv *jvmti = getJvmtiEnv(env); jvmtiEnv *jvmti = getJvmtiEnv(env);

@ -2,19 +2,17 @@ package arthas;
import org.junit.Test; import org.junit.Test;
import arthas.Vmtool;
import java.io.File; import java.io.File;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.util.ArrayList; import java.util.ArrayList;
public class VmtoolTest { public class VmToolTest {
/** /**
* macbook * macbook
* allLoadedClasses->1050 * allLoadedClasses->1050
* arthas.JvmUtils@5bb21b69 arthas.JvmUtils@6b9651f3 * arthas.VmTool@5bb21b69 arthas.VmTool@6b9651f3
* before instances->[arthas.JvmUtils@5bb21b69, arthas.JvmUtils@6b9651f3] * before instances->[arthas.VmTool@5bb21b69, arthas.VmTool@6b9651f3]
* size->16 * size->16
* count->2 * count->2
* sum size->32 * sum size->32
@ -24,31 +22,31 @@ public class VmtoolTest {
@Test @Test
public void test01() { public void test01() {
try { try {
String path = Vmtool.class.getProtectionDomain().getCodeSource().getLocation().getPath(); String path = VmTool.class.getProtectionDomain().getCodeSource().getLocation().getPath();
System.err.println(path); System.err.println(path);
String libPath = new File(path, Vmtool.detectLibName()).getAbsolutePath(); String libPath = new File(path, VmTool.detectLibName()).getAbsolutePath();
Vmtool vmtool = Vmtool.getInstance(libPath); VmTool vmtool = VmTool.getInstance(libPath);
//调用native方法获取已加载的类不包括小类型(如int) //调用native方法获取已加载的类不包括小类型(如int)
ArrayList<Class<?>> allLoadedClasses = vmtool.getAllLoadedClasses(); ArrayList<Class<?>> allLoadedClasses = vmtool.getAllLoadedClasses();
System.out.println("allLoadedClasses->" + allLoadedClasses.size()); System.out.println("allLoadedClasses->" + allLoadedClasses.size());
//通过下面的例子可以看到getInstances(Class<T> klass)拿到的是当前存活的所有对象 //通过下面的例子可以看到getInstances(Class<T> klass)拿到的是当前存活的所有对象
WeakReference<VmtoolTest> weakReference1 = new WeakReference<VmtoolTest>(new VmtoolTest()); WeakReference<VmToolTest> weakReference1 = new WeakReference<VmToolTest>(new VmToolTest());
WeakReference<VmtoolTest> weakReference2 = new WeakReference<VmtoolTest>(new VmtoolTest()); WeakReference<VmToolTest> weakReference2 = new WeakReference<VmToolTest>(new VmToolTest());
System.out.println(weakReference1.get() + " " + weakReference2.get()); System.out.println(weakReference1.get() + " " + weakReference2.get());
ArrayList<Vmtool> beforeInstances = vmtool.getInstances(Vmtool.class); ArrayList<VmTool> beforeInstances = vmtool.getInstances(VmTool.class);
System.out.println("before instances->" + beforeInstances); System.out.println("before instances->" + beforeInstances);
System.out.println("size->" + vmtool.getInstanceSize(weakReference1.get())); System.out.println("size->" + vmtool.getInstanceSize(weakReference1.get()));
System.out.println("count->" + vmtool.countInstances(Vmtool.class)); System.out.println("count->" + vmtool.countInstances(VmTool.class));
System.out.println("sum size->" + vmtool.sumInstanceSize(Vmtool.class)); System.out.println("sum size->" + vmtool.sumInstanceSize(VmTool.class));
beforeInstances = null; beforeInstances = null;
System.gc(); System.gc();
Thread.sleep(100); Thread.sleep(100);
System.out.println(weakReference1.get() + " " + weakReference2.get()); System.out.println(weakReference1.get() + " " + weakReference2.get());
ArrayList<Vmtool> afterInstances = vmtool.getInstances(Vmtool.class); ArrayList<VmTool> afterInstances = vmtool.getInstances(VmTool.class);
System.out.println("after instances->" + afterInstances); System.out.println("after instances->" + afterInstances);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
Loading…
Cancel
Save