探索JVM运行状态的利器JVMPI,HPROF

本文介绍 JVMPI (Java Virtual Machine Profiler Interface) 的基本概念及其使用方法。通过一个示例程序详细展示了如何利用 JVMPI 监控 Java 虚拟机的各种事件,包括堆内存的分配与回收、垃圾收集过程等。

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 );

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值