jstat java工具在linux上的源码分析

来源:互联网 发布:八爪鱼采集软件 编辑:程序博客网 时间:2024/05/21 12:42

jstat通常是作为使用查看gc 状态,编译信息等具体信息,加载class量等,笔者在前面的博客里谈了jmap,jstack工具的如何实现可具体参考(http://blog.csdn.net/raintungli/article/details/7023092) ,谈到通常的方法必须让jvm的所有线程进入一个safe point的状态,也就是在使用jmap, jstack工具的时候会有线程的停顿感,并且影响到当时jvm所正在处理的事情,如果在产线上这是会影响客户体验的。

 在调用jstat 的时候,并没有感觉到线程的停顿,也就是jstat 使用与jmap,jstack完全不同的方式,接着还是源码分析来解释。

在jstat.java中初始化LocalMonitoredVM中,初始化了PerfDataBuffer, 在PerfDataBuffer是通过Perf attach vm的进程生成byte buffer对象

  private ByteBuffer attachImpl(String user, int lvmid, int mode)            throws IllegalArgumentException, IOException    {        final ByteBuffer b = attach(user, lvmid, mode);        if (lvmid == 0) {            // The native instrumentation buffer for this Java virtual            // machine is never unmapped.            return b;        }        else {            // This is an instrumentation buffer for another Java virtual            // machine with native resources that need to be managed. We            // create a duplicate of the native ByteBuffer and manage it            // with a Cleaner object (PhantomReference). When the duplicate            // becomes only phantomly reachable, the native resources will            // be released.            final ByteBuffer dup = b.duplicate();            Cleaner.create(dup, new Runnable() {                    public void run() {                        try {                            instance.detach(b);                        }                        catch (Throwable th) {                            // avoid crashing the reference handler thread,                            // but provide for some diagnosability                            assert false : th.toString();                        }                    }                });            return dup;        }    }    private native ByteBuffer attach(String user, int lvmid, int mode)                   throws IllegalArgumentException, IOException;

关键就在native程序中, 查看perf.cpp 中对应的方法

PerfMemory::attach(user_utf, vmid, (PerfMemory::PerfMemoryMode) mode,                     &address, &capacity, CHECK_NULL);

也就是类PerfMemory的attach方法

在perfMemory_linux.cpp中,我们看到了attach 的实现

void PerfMemory::attach(const char* user, int vmid, PerfMemoryMode mode, char** addrp, size_t* sizep, TRAPS) {  if (vmid == 0 || vmid == os::current_process_id()) {     *addrp = start();     *sizep = capacity();     return;  }  mmap_attach_shared(user, vmid, mode, addrp, sizep, CHECK);}

在mmap_attach_shared函数中,

 char* filename = get_sharedmem_filename(dirname, vmid);  // copy heap memory to resource memory. the open_sharedmem_file  // method below need to use the filename, but could throw an  // exception. using a resource array prevents the leak that  // would otherwise occur.  char* rfilename = NEW_RESOURCE_ARRAY(char, strlen(filename) + 1);  strcpy(rfilename, filename);  // free the c heap resources that are no longer needed  if (luser != user) FREE_C_HEAP_ARRAY(char, luser);  FREE_C_HEAP_ARRAY(char, dirname);  FREE_C_HEAP_ARRAY(char, filename);  // open the shared memory file for the give vmid  fd = open_sharedmem_file(rfilename, file_flags, CHECK);  assert(fd != OS_ERR, "unexpected value");  if (*sizep == 0) {    size = sharedmem_filesize(fd, CHECK);    assert(size != 0, "unexpected size");  }  mapAddress = (char*)::mmap((char*)0, size, mmap_prot, MAP_SHARED, fd, 0);

该函数把一个文件mmap成内存,然后通过读取内存来读取该文件的内容,而文件的路径就是/tmp/hsperfdata_{userid}/{pid} 

而在create_shared_memory函数中,也就是jvm 在vm_init_globals初始化后会创建这个文件并且用mmap映射成内存,接着虚拟机会把gc 或者其他需要的状态写入这个mmap文件中,jstat工具通过访问该文件,读取内容,显示在屏幕上,该文件通过内存映射实现,没有什么性能损耗,完整的jstat 的流程就全部走通了。





原创粉丝点击