1 .什么是 JVMPI ? Java Virtual Machine Profiler Interface 。参考
http://java.sun.com/j2se/1.4.2/docs/guide/jvmpi/jvmpi.html#overview
JVMPI 可以做什么?它可以监控 VM 发生的各种事件。例如当 JVM 创建,关闭, Java 类被加载,创建对象,或 GC 回收,等 37 种事件。既然是接口自然就是有一个头文件, [JAVA_HOME]\include\jvmpi.h 。您可以开发自己的 Profiler 监控 Java VM 的发生的各种事件。
下面是我编写的使用 JVMPI 的例子,输出结果说明了为了运行一个 Java 类, JVM 需要做的各种操作。
#include <jvmpi.h>
//1.>cl -LDd -Zi -I. -IC:\j2sdk1.4.2_10\include -IC
// :\j2sdk1.4.2_10\include\win32 -Tp.\jvmtrace.c -o jvmtrace.dll
//2.>copy jvmtrace.dll C:\j2sdk1.4.2_10\bin\
//3.>java -Xrunjvmtrace org.colimas.jni.test.JniTest
//jvmpi interface 全局指 针
static JVMPI_Interface *jvmpi_interface;
// 时间 通知 处 理函数
void notifyEvent(JVMPI_Event *event) {
switch (event->event_type) {
/ * 非常可怕的输出 ,好奇就试试。
case JVMPI_EVENT_CLASS_LOAD:
fprintf(stderr, "trace> Class Load : %s\n", event->u.class_load.class_name);
break;*/
case JVMPI_EVENT_ARENA_DELETE :
fprintf(stderr, "trace> The heap arena :%d is deleted.\n" , event->u.delete_arena.arena_id);
break ;
case JVMPI_EVENT_ARENA_NEW :
fprintf(stderr, "trace> The heap arena %s:%d is created.\n" ,
event->u.new_arena.arena_name,
event->u.new_arena.arena_id);
break ;
case JVMPI_EVENT_GC_FINISH :
fprintf(stderr, "trace> GC is finished. Used object:%d, space:%d, total object space:%d.\n" ,
event->u.gc_info.used_objects,
event->u.gc_info.used_object_space,
event->u.gc_info.total_object_space);
break ;
case JVMPI_EVENT_GC_START :
fprintf(stderr, "trace>GC is started.\n" );
break ;
case JVMPI_EVENT_HEAP_DUMP :
fprintf(stderr, "trace> The heap dump begin %s,end %s.\n" ,
event->u.heap_dump.begin,
event->u.heap_dump.end);
break ;
case JVMPI_EVENT_JVM_INIT_DONE :
fprintf(stderr, "trace> JVM initialization is done.\n" );
break ;
case JVMPI_EVENT_JVM_SHUT_DOWN :
fprintf(stderr, "trace> JVM is shutting down.\n" );
break ;
case JVMPI_EVENT_THREAD_END :
fprintf(stderr, "trace> A thread ends.\n" );
break ;
case JVMPI_EVENT_THREAD_START :
fprintf(stderr, "trace> The thread %s begins whose group is %s, parent is %s.\n" ,
event->u.thread_start.thread_name ,
event->u.thread_start.group_name,
event->u.thread_start.parent_name);
break ;
}
}
// profiler agent entry point
extern "C" {
JNIEXPORT jint JNICALL JVM_OnLoad(JavaVM *jvm, char *options, void *reserved) {
fprintf(stderr, "trace> initializing ..... \n" );
// get jvmpi interface pointer
if ((jvm->GetEnv(( void **)&jvmpi_interface, JVMPI_VERSION_1)) < 0) {
fprintf(stderr, "trace> error in obtaining jvmpi interface pointer\n" );
return JNI_ERR;
}
// initialize jvmpi interface
jvmpi_interface->NotifyEvent = notifyEvent;
// enabling class load event notification
//jvmpi_interface->EnableEvent(JVMPI_EVENT_CLASS_LOAD, NULL);
jvmpi_interface->EnableEvent(JVMPI_EVENT_ARENA_DELETE, NULL );
本文介绍 JVMPI (Java Virtual Machine Profiler Interface) 的基本概念及其使用方法。通过一个示例程序详细展示了如何利用 JVMPI 监控 Java 虚拟机的各种事件,包括堆内存的分配与回收、垃圾收集过程等。

被折叠的 条评论
为什么被折叠?



