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
* @since 3.5.1
*/
public class Vmtool implements VMtoolMXBean {
public class VmTool implements VmToolMXBean {
/**
* 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);
}
public static synchronized Vmtool getInstance(String libPath) {
public static synchronized VmTool getInstance(String libPath) {
if (instance != null) {
return instance;
}
@ -54,7 +54,7 @@ public class Vmtool implements VMtoolMXBean {
System.load(libPath);
}
instance = new Vmtool();
instance = new VmTool();
return instance;
}

@ -3,19 +3,19 @@ package arthas;
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>
* {@code
* ManagementFactory.getPlatformMBeanServer().registerMBean(
* VMtool.getInstance(),
* new ObjectName("arthas:type=VMtool")
* VmTool.getInstance(),
* new ObjectName("arthas:type=VmTool")
* );
* }
* </pre>
* @author hengyunabc 2021-04-26
*/
public interface VMtoolMXBean {
public interface VmToolMXBean {
/**
* jni-libOK
*/

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

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

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