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 的流程就全部走通了。
- jstat java工具在linux上的源码分析
- Java 工具(jmap,jstack)在linux上的源码分析(五) -F 参数的bug
- Java 工具(jmap,jstack)在linux上的源码分析(一)
- Java 工具(jmap,jstack)在linux上的源码分析(二)信号处理
- Java 工具(jmap,jstack)在linux上的源码分析(四)safe point
- Java 工具(jmap,jstack)在linux上的源码分析(一)
- Java 工具(jmap,jstack)在linux上的源码分析(二)信号处理
- Java 工具(jmap,jstack)在linux上的源码分析(四)safe point
- Java 工具(jmap,jstack)在linux上的源码分析(三)执行的线程vm thread
- Java 工具(jmap,jstack)在linux上的源码分析(三)执行的线程vm thread
- Java 工具(jmap,jstack)在linux上的源码分析(六) -F 参数 如何读取线程列表
- Jstat在分析java的内存GC时的应用
- jstat使用方法 进程java分析工具
- Java内存分析工具——Jstat
- Java 工具(jmap,jstack)在linux上的源码分析(六) -F 参数 读取动态链接共享库文件中的符号表
- JVM源码分析之Jstat工具原理完全解读
- 在Linux系统上安装Java反编译工具的方法
- Linux源码分析工具
- 最大子段和详解
- Oracle的10046事件
- 最常被程序员们谎称读过的计算机书籍
- 【开源项目】花密(Flower Password)VB版之HMAC-MD5算法
- 最短摘要(待完成)
- jstat java工具在linux上的源码分析
- 隔夜开水不能乱喝啊
- C++文件操作
- EXT Grid控件精华展现 javascript
- jquery checkbox,select,select 取值
- 第八周实验报告1-2
- c++虚函数与继承的问题
- 探秘Java虚拟机——内存管理与垃圾回收
- [Code Sample]Alert "Do you want to save the changes you have made?"