rename JvmUtils to Vmtool. #1781

This commit is contained in:
hengyunabc 2021-04-26 21:00:41 +08:00
parent 52bb8afbee
commit 4f91d181e2
7 changed files with 150 additions and 132 deletions

View File

@ -198,10 +198,6 @@
<artifactId>arthas-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.scijava</groupId>
<artifactId>native-lib-loader</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>

View File

@ -1,28 +1,41 @@
package com.vdian.vclub;
import com.taobao.arthas.common.AnsiLog;
import org.scijava.nativelib.NativeLoader;
package arthas;
import java.util.ArrayList;
/**
* @author ZhangZiCheng 2021-02-12
* @author hengyunabc 2021-04-26
* @since 3.5.1
*/
public class JvmUtils {
public class Vmtool {
/**
* 不要修改jni-lib的名称
*/
private final static String JNI_LIBRARY_NAME = "ArthasJniLibrary";
public final static String JNI_LIBRARY_NAME = "ArthasJniLibrary";
static {
try {
NativeLoader.loadLibrary(JNI_LIBRARY_NAME);
AnsiLog.warn("checkResult->" + check() + ", jni-lib available !");
} catch (Throwable t) {
AnsiLog.error("load jni-lib failed:" + t.getMessage(), t);
private static Vmtool instance;
private Vmtool() {
}
public static Vmtool getInstance() {
return getInstance(null);
}
public static synchronized Vmtool getInstance(String libPath) {
if (instance != null) {
return instance;
}
if (libPath == null) {
System.loadLibrary(JNI_LIBRARY_NAME);
} else {
System.load(libPath);
}
instance = new Vmtool();
return instance;
}
/**

View File

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

View File

@ -1,61 +0,0 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_vdian_vclub_JvmUtils */
#ifndef _Included_com_vdian_vclub_JvmUtils
#define _Included_com_vdian_vclub_JvmUtils
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_vdian_vclub_JvmUtils
* Method: check
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_vdian_vclub_JvmUtils_check
(JNIEnv *, jclass);
/*
* Class: com_vdian_vclub_JvmUtils
* Method: getInstances
* Signature: (Ljava/lang/Class;)Ljava/util/ArrayList;
*/
JNIEXPORT jobject JNICALL Java_com_vdian_vclub_JvmUtils_getInstances
(JNIEnv *, jclass, jclass);
/*
* Class: com_vdian_vclub_JvmUtils
* Method: sumInstanceSize
* Signature: (Ljava/lang/Class;)J
*/
JNIEXPORT jlong JNICALL Java_com_vdian_vclub_JvmUtils_sumInstanceSize
(JNIEnv *, jclass, jclass);
/*
* Class: com_vdian_vclub_JvmUtils
* Method: getInstanceSize
* Signature: (Ljava/lang/Object;)J
*/
JNIEXPORT jlong JNICALL Java_com_vdian_vclub_JvmUtils_getInstanceSize
(JNIEnv *, jclass, jobject);
/*
* Class: com_vdian_vclub_JvmUtils
* Method: countInstances
* Signature: (Ljava/lang/Class;)J
*/
JNIEXPORT jlong JNICALL Java_com_vdian_vclub_JvmUtils_countInstances
(JNIEnv *, jclass, jclass);
/*
* Class: com_vdian_vclub_JvmUtils
* Method: getAllLoadedClasses
* Signature: ()Ljava/util/ArrayList;
*/
JNIEXPORT jobject JNICALL Java_com_vdian_vclub_JvmUtils_getAllLoadedClasses
(JNIEnv *, jclass);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -2,11 +2,11 @@
#include <jni.h>
#include <jni_md.h>
#include <jvmti.h>
#include "com_vdian_vclub_JvmUtils.h"
#include "arthas_Vmtool.h"
extern "C"
JNIEXPORT jstring JNICALL
Java_com_vdian_vclub_JvmUtils_check(JNIEnv *env, jclass thisClass) {
Java_arthas_Vmtool_check(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_com_vdian_vclub_JvmUtils_getInstances(JNIEnv *env, jclass thisClass, jclass klass) {
Java_arthas_Vmtool_getInstances(JNIEnv *env, jclass thisClass, jclass klass) {
jvmtiEnv *jvmti = getJvmtiEnv(env);
@ -88,7 +88,7 @@ Java_com_vdian_vclub_JvmUtils_getInstances(JNIEnv *env, jclass thisClass, jclass
extern "C"
JNIEXPORT jlong JNICALL
Java_com_vdian_vclub_JvmUtils_sumInstanceSize(JNIEnv *env, jclass thisClass, jclass klass) {
Java_arthas_Vmtool_sumInstanceSize(JNIEnv *env, jclass thisClass, jclass klass) {
jvmtiEnv *jvmti = getJvmtiEnv(env);
@ -126,7 +126,7 @@ Java_com_vdian_vclub_JvmUtils_sumInstanceSize(JNIEnv *env, jclass thisClass, jcl
}
extern "C"
JNIEXPORT jlong JNICALL Java_com_vdian_vclub_JvmUtils_getInstanceSize
JNIEXPORT jlong JNICALL Java_arthas_Vmtool_getInstanceSize
(JNIEnv *env, jclass thisClass, jobject instance) {
jvmtiEnv *jvmti = getJvmtiEnv(env);
@ -142,7 +142,7 @@ JNIEXPORT jlong JNICALL Java_com_vdian_vclub_JvmUtils_getInstanceSize
extern "C"
JNIEXPORT jlong JNICALL
Java_com_vdian_vclub_JvmUtils_countInstances(JNIEnv *env, jclass thisClass, jclass klass) {
Java_arthas_Vmtool_countInstances(JNIEnv *env, jclass thisClass, jclass klass) {
jvmtiEnv *jvmti = getJvmtiEnv(env);
@ -172,7 +172,7 @@ Java_com_vdian_vclub_JvmUtils_countInstances(JNIEnv *env, jclass thisClass, jcla
}
extern "C"
JNIEXPORT jobject JNICALL Java_com_vdian_vclub_JvmUtils_getAllLoadedClasses
JNIEXPORT jobject JNICALL Java_arthas_Vmtool_getAllLoadedClasses
(JNIEnv *env, jclass thisClass) {
jvmtiEnv *jvmti = getJvmtiEnv(env);

View File

@ -0,0 +1,57 @@
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 {
/**
* 在我的macbook上运行结果如下
* allLoadedClasses->1050
* arthas.JvmUtils@5bb21b69 arthas.JvmUtils@6b9651f3
* before instances->[arthas.JvmUtils@5bb21b69, arthas.JvmUtils@6b9651f3]
* size->16
* count->2
* sum size->32
* null null
* after instances->[]
*/
@Test
public void test01() {
try {
String path = Vmtool.class.getProtectionDomain().getCodeSource().getLocation().getPath();
System.err.println(path);
String libPath = new File(path, System.mapLibraryName(Vmtool.JNI_LIBRARY_NAME)).getAbsolutePath();
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());
System.out.println(weakReference1.get() + " " + weakReference2.get());
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));
beforeInstances = null;
System.gc();
Thread.sleep(100);
System.out.println(weakReference1.get() + " " + weakReference2.get());
ArrayList<Vmtool> afterInstances = Vmtool.getInstances(Vmtool.class);
System.out.println("after instances->" + afterInstances);
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -1,48 +0,0 @@
package com.vdian.vclub;
import org.junit.Test;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
public class JvmUtilsTest {
/**
* 在我的macbook上运行结果如下
* allLoadedClasses->1050
* com.vdian.vclub.JvmUtils@5bb21b69 com.vdian.vclub.JvmUtils@6b9651f3
* before instances->[com.vdian.vclub.JvmUtils@5bb21b69, com.vdian.vclub.JvmUtils@6b9651f3]
* size->16
* count->2
* sum size->32
* null null
* after instances->[]
*/
@Test
public void test01() {
try {
//调用native方法获取已加载的类不包括小类型(如int)
ArrayList<Class<?>> allLoadedClasses = JvmUtils.getAllLoadedClasses();
System.out.println("allLoadedClasses->" + allLoadedClasses.size());
//通过下面的例子可以看到getInstances(Class<T> klass)拿到的是当前存活的所有对象
WeakReference<JvmUtils> weakReference1 = new WeakReference<JvmUtils>(new JvmUtils());
WeakReference<JvmUtils> weakReference2 = new WeakReference<JvmUtils>(new JvmUtils());
System.out.println(weakReference1.get() + " " + weakReference2.get());
ArrayList<JvmUtils> beforeInstances = JvmUtils.getInstances(JvmUtils.class);
System.out.println("before instances->" + beforeInstances);
System.out.println("size->" + JvmUtils.getInstanceSize(weakReference1.get()));
System.out.println("count->" + JvmUtils.countInstances(JvmUtils.class));
System.out.println("sum size->" + JvmUtils.sumInstanceSize(JvmUtils.class));
beforeInstances = null;
System.gc();
Thread.sleep(100);
System.out.println(weakReference1.get() + " " + weakReference2.get());
ArrayList<JvmUtils> afterInstances = JvmUtils.getInstances(JvmUtils.class);
System.out.println("after instances->" + afterInstances);
} catch (Exception e) {
e.printStackTrace();
}
}
}