Java开源工具在linux上的源码分析(三):执行的线程

开发 后端
在前面的博客中所提到的信号转发线程,Attach Listener 线程都只是操作socket文件,并没有去执行比如stack 分析,或者heap的分析,真正的工作线程其实是vm thread.

在前面的博客中(http://blog.csdn.net/raintungli/article/details/7034005)所提到的信号转发线程,Attach Listener 线程都只是操作socket文件,并没有去执行比如stack 分析,或者heap的分析,真正的工作线程其实是vm thread.

(一)启动vm thread

  1. jint Threads::create_vm(JavaVMInitArgs* args, bool* canTryAgain) {  
  2. ...  
  3.   // Create the VMThread  
  4.   { TraceTime timer("Start VMThread", TraceStartupTime);  
  5.     VMThread::create();  
  6.     Thread* vmthread = VMThread::vm_thread();  
  7.  
  8.     if (!os::create_thread(vmthread, os::vm_thread))  
  9.       vm_exit_during_initialization("Cannot create VM thread. Out of system resources.");  
  10.  
  11.     // Wait for the VM thread to become ready, and VMThread::run to initialize  
  12.     // Monitors can have spurious returns, must always check another state flag  
  13.     {  
  14.       MutexLocker ml(Notify_lock);  
  15.       os::start_thread(vmthread);  
  16.       while (vmthread->active_handles() == NULL) {  
  17.         Notify_lock->wait();  
  18.       }  
  19.     }  
  20.   }  
  21. ...  
  22.  
  23.  

我们可以看到,在thread.cpp里启动了线程vm thread,在这里我们同时也稍微的略带的讲一下jvm在linux里如何启动线程的。

通常在linux中启动线程,是调用:

  1. int pthread_create((pthread_t *__thread, __const pthread_attr_t *__attr,void *(*__start_routine) (void *), void *__arg)); 

而在java里却增加了os:create_thread --初始化线程 和os:start_thread--启动线程。

我们去看一下jvm里面是如何在linux里做到的。

在os_linux.cpp中来看create_thread的方法:

  1. bool os::create_thread(Thread* thread, ThreadType thr_type, size_t stack_size) {  
  2. ....  
  3. int ret = pthread_create(&tid, &attr, (void* (*)(void*)) java_start, thread);  
  4. ....  

继续看java_start方法:

  1. static void *java_start(Thread *thread) {  
  2. ....  
  3.   // handshaking with parent thread  
  4.   {  
  5.     MutexLockerEx ml(sync, Mutex::_no_safepoint_check_flag);  
  6.  
  7.     // notify parent thread  
  8.     osthread->set_state(INITIALIZED);  
  9.     sync->notify_all();  
  10.  
  11.     // wait until os::start_thread()  
  12.     while (osthread->get_state() == INITIALIZED) {  
  13.       sync->wait(Mutex::_no_safepoint_check_flag);  
  14.     }  
  15.   }  
  16.  
  17.   // call one more level start routine  
  18.   thread->run();  
  19.  
  20.   return 0;  

首先jvm先设置了当前线程的状态是Initialized, 然后notify所有的线程,

  1. while (osthread->get_state() == INITIALIZED) {  
  2.      sync->wait(Mutex::_no_safepoint_check_flag);  
  3.    } 

不停的查看线程的当前状态是不是Initialized, 如果是的话,调用了sync->wait()的方法等待。

来看os:start_thread的方法 os.cpp

  1. void os::start_thread(Thread* thread) {  
  2.   // guard suspend/resume  
  3.   MutexLockerEx ml(thread->SR_lock(), Mutex::_no_safepoint_check_flag);  
  4.   OSThread* osthread = thread->osthread();  
  5.   osthread->set_state(RUNNABLE);  
  6.   pd_start_thread(thread);  
  7. }

这时候设置了线程的状态为runnable,但没有notify线程。

在 pd_start_thread(thread)中, os_linux.cpp中:

  1. void os::pd_start_thread(Thread* thread) {  
  2.   OSThread * osthread = thread->osthread();  
  3.   assert(osthread->get_state() != INITIALIZED, "just checking");  
  4.   Monitor* sync_with_child = osthread->startThread_lock();  
  5.   MutexLockerEx ml(sync_with_child, Mutex::_no_safepoint_check_flag);  
  6.   sync_with_child->notify();  

这时候我们看到了notify 线程的操作,也就是这时候notify了线程,因为这时候的线程的状态是RUNNABLE, 方法java_start继续往下执行,于是调用了thread->run()的方法。

 

对于线程vm Thread 也就是调用了vmthread::run方法。

vmThread.cpp

  1. void VMThread::run() {  
  2. ...  
  3. this->loop();  
  4. ...  

调用了loop函数,处理了VM_Operation 的queue 关于queue的级别和优先级处理算法:可以参考 另一篇博客:http://blog.csdn.net/raintungli/article/details/6553337

(二)Jstack 运行在vm thread里的VM_Operation

jstack 处理也就是在前面博客所提到的attach Listener 线程所做的 operation

  1. static jint thread_dump(AttachOperation* op, outputStream* out) {  
  2.   bool print_concurrent_locks = false;  
  3.   if (op->arg(0) != NULL && strcmp(op->arg(0), "-l") == 0) {  
  4.     print_concurrent_locks = true;  
  5.   }  
  6.  
  7.   // thread stacks  
  8.   VM_PrintThreads op1(out, print_concurrent_locks);  
  9.   VMThread::execute(&op1);  
  10.  
  11.   // JNI global handles  
  12.   VM_PrintJNI op2(out);  
  13.   VMThread::execute(&op2);  
  14.  
  15.   // Deadlock detection  
  16.   VM_FindDeadlocks op3(out);  
  17.   VMThread::execute(&op3);  
  18.  
  19.   return JNI_OK;  

简单看一下类VM_PrintThreads 它 继承了VM_Operation

  1. class VM_PrintThreads: public VM_Operation {  
  2.  private:  
  3.   outputStream* _out;  
  4.   bool _print_concurrent_locks;  
  5.  public:  
  6.   VM_PrintThreads()                                                { _out = tty; _print_concurrent_locks = PrintConcurrentLocks; }  
  7.   VM_PrintThreads(outputStream* out, bool print_concurrent_locks)  { _out = out; _print_concurrent_locks = print_concurrent_locks; }  
  8.   VMOp_Type type() const                                           {  return VMOp_PrintThreads; }  
  9.   void doit();  
  10.   bool doit_prologue();  
  11.   void doit_epilogue();  
  12. }; 

当调用VMThread::execute()也就是将VM_PrintThreads 放入了_vm_queue中,交给vm thread 处理,对vm thread来说取出queue里的VM_Operation,并且调用doit方法。

在jstack里,attach listener 的线程产生了VM_PrintThreads,VM_PrintJNI,VM_FindDeadlocks 3个operations,交给了vm thread 的线程处理。

原文链接:http://blog.csdn.net/raintungli/article/details/7045024

【系列文章】

  1. Java开源工具在linux上的源码分析(一):跟踪方式
  2. Java开源工具在linux上的源码分析(二):信号处理
  3. Java开源工具在linux上的源码分析(四):safe point
  4. Java开源工具在linux上的源码分析(五):-F参数的bug
  5. Java开源工具在linux上的源码分析(六):符号表的读取
责任编辑:林师授 来源: raintungli的博客
相关推荐

2012-03-02 12:14:19

JavaJstackJmap

2012-03-02 12:20:21

Javajmapjstack

2012-03-02 12:31:50

Javajmapjstack

2012-03-02 12:38:49

Javajmapjstack

2012-03-02 13:29:38

Javajmapjstack

2022-06-26 18:09:43

Linux开源

2012-03-30 11:16:29

JavaVisualVM

2010-01-27 09:58:59

Linuxunix程序日志

2019-10-16 17:00:51

LinuxUbuntuVMware

2018-10-31 15:54:47

Java线程池源码

2021-03-09 11:25:04

Linux开源工具服务器

2012-05-22 00:28:21

JavaJava开源开源工具

2022-06-06 14:20:25

个人财务开源预算

2021-08-31 09:41:57

LinuxiPhone开源工具

2021-09-01 09:47:25

Linux 工具 开发

2020-05-09 12:01:40

Linux开源软件SDN

2019-05-23 14:36:24

LinuxSOSReportxsos

2022-08-19 11:17:09

Linux

2017-01-12 15:58:17

Linux死锁分析方法

2021-10-08 14:14:03

jconsoleJavaLinux
点赞
收藏

51CTO技术栈公众号