Java Tools (jmap,jstack) source analysis on Linux (ii) Signal processing __ Data structure

Source: Internet
Author: User
Tags strcmp

When the Java virtual machine is started, many internal threads are started, which are mainly implemented in the Thread.cpp Create_vm method body

And in the Thread.cpp, there are 2 threads to handle the signal-related

  Jvmtiexport::enter_live_phase ();

  Signal Dispatcher needs to being started before Vminit event is posted
  os::signal_init ();

  Start Attach Listener If +startattachlistener or it can ' t be started lazily
  if (! Disableattachmechanism) {
    if (Startattachlistener | | Attachlistener::init_at_startup ()) {
      attachlistener::init ();
    }
  }

1. Signal Dispatcher thread

In the Signal_init () function in Os.cpp, the signal dispatcher thread is started, and the signal dispather thread is mainly used for processing signals, waiting for signals and distributing processing, which can be seen in detail signal_thread_ The method of entry

static void Signal_thread_entry (javathread* thread, traps) {os::set_priority (thread, nearmaxpriority);
    while (true) {int sig; {//fixme:currently we have not decieded what should is the status//For this Java thread blocked Here.
      Once we decide about//we should fix this.
    sig = Os::signal_wait ();
    } if (sig = = OS::SIGEXITNUM_PD ()) {//Terminate the signal thread return;  Switch (SIG) {case Sigbreak: {//Check if the signal are a trigger to start the Attach listener-in
        That//Case don ' t print stack traces. if (!
        Disableattachmechanism && Attachlistener::is_init_trigger ()) {continue; }//Print stack traces//Any sigbreak operations added this should make sure to flush/  Put stream (e.g. Tty->flush ()) after output.
        4803766. Each module also prints a extra carriage return on its OUtput.
        Vm_printthreads op;
        Vmthread::execute (&OP);
        Vm_printjni Jni_op;
        Vmthread::execute (&JNI_OP);
        Vm_finddeadlocks OP1 (TTY);
        Vmthread::execute (&OP1);
        Universe::p Rint_heap_at_sigbreak (); if (printclasshistogram) {vm_gc_heapinspection OP1 (Gclog_or_tty, True/* Force full GC before Heap *
          /, True/* need_prologue * *;
        Vmthread::execute (&OP1);
        } if (Jvmtiexport::should_post_data_dump ()) {jvmtiexport::p ost_data_dump ();
      } break;
        } default: {//Dispatch the signal to Java Handlemark HM (THREAD);
        Klassoop k = Systemdictionary::resolve_or_null (Vmsymbolhandles::sun_misc_signal (), THREAD);
        Klasshandle Klass (THREAD, K);
          if (Klass.not_null ()) {javavalue result (t_void);
          Javacallarguments args;
          Args.push_int (SIG); JavacalLs::call_static (&result, Klass, Vmsymbolhandles::d ispatch_name (), VmS
        Ymbolhandles::int_void_signature (), &args, THREAD);
          } if (has_pending_exception) {//TTY is initialized early so we don ' t expect it to be null but
          If it is we can ' t risk doing the initialization that might//trigger additional out-of-memory conditions
            if (TTY!= NULL) {char klass_name[256];
            Char tmp_sig_name[16];
            Const char* Sig_name = "UNKNOWN";
            Instanceklass::cast (Pending_exception->klass ())-> name ()->as_klass_external_name (Klass_name, 256);
            if (Os::exception_name (SIG, Tmp_sig_name,)!= NULL) sig_name = Tmp_sig_name; Warning ("Exception%s occurred dispatching signal%s to handler" "-the VM could need to is forcibly ter
        Minated ",            Klass_name, Sig_name);
        } clear_pending_exception; }
      }
    }
  }
}


You can see through the os::signal_wait (), wait for the signal, and in Linux is through the sem_wait () to receive the Sigbreak (Linux quit) signal (for signal processing please refer to another blog of the author: Java For the first time by calling Attachlistener::is_init_trigger () to initialize the attach listener thread for signal processing under Linux, see 2 in detail. Attach Listener Thread.

The first time a signal is received, the initialization begins, and when the initialization succeeds, it returns directly and does not return any thread stack information (returned through the operation of the socket file), and the second time does not need to be initialized. If the initialization is unsuccessful, the thread stack information is printed directly in the console's outputstream. The second time the signal is received, if initialized, the thread's stack information will be printed directly in the console. If there is no initialization, continue initializing, walking and the first time the same process. 2. Attach Listener thread

The Attach Listener thread is responsible for receiving an external command, while executing the command and returning the result to the sender. When the JVM is started, if +startattachlistener is not specified, the thread will not start, and we have just discussed that after receiving the quit signal, we will call Attachlistener::is_init_ Trigger () starts the Attach Listener thread with Attachlistener::init () and initializes it under a different operating system, which is implemented in Linux in the Attachlistener_linux.cpp file.

If a file is found in Linux, the attach listener thread is started and the socket file is initialized, which is usually jmap,jstack tool, first created attach_pid# PID file, and then send the Quit signal, in this way the attach listener thread was launched (see Blog: http://blog.csdn.net/raintungli/article/details/7023092).

Implementation of threads is implemented in Attach_listener_thread_entry method body

static void Attach_listener_thread_entry (javathread* thread, traps) {os::set_priority (thread, nearmaxpriority);
  if (Attachlistener::p d_init ()!= 0) {return;

  } attachlistener::set_initialized (); for (;;)  
     {attachoperation* op = attachlistener::d equeue ();   if (op = NULL) {return;
    Dequeue failed or shutdown} Resourcemark rm;
    BufferedStream St;

    Jint res = JNI_OK;
      Handle Special Detachall operation if strcmp (Op->name (), Attachoperation::d etachall_operation_name ()) = = 0) {
    Attachlistener::d etachall ();
      else {//find ' function to dispatch too attachoperationfunctioninfo* info = NULL;
        for (int i=0; funcs[i].name!= NULL; i++) {Const char* name = Funcs[i].name;
        ASSERT (strlen (name) <= Attachoperation::name_length_max, "Operation <= Name_length_max");
          if (strcmp (Op->name (), name) = = 0) {info = & (Funcs[i]);
        Break

}
      }      Check for platform dependent attach operation if (info = = NULL) {info = Attachlistener::p d_find_op
      Eration (Op->name ()); } if (info!= NULL) {//dispatch to the function that implements this operation res = (Info->fu
      NC) (OP, &st);
        else {st.print ("Operation%s not recognized!", op->name ());
      res = Jni_err;
  }//Operation Complete-send result and output to client Op->complete (res, &st); }
}


In Attachlistener::d equeue (); In the liunx is to listen to the implementation of the socket just created the file, if there is a request to come in, find the corresponding operation, call operation to get results and write the results to this socket file, if you delete the socket file, Jstack/jmap will appear error message Unable to open socket file: ...

We often use the kill-3 PID operation to print the thread stack information, we can see that the specific implementation is done in the signal Dispatcher thread, because the kill-3 PID does not create the. attach_pid#pid file, so it always initializes unsuccessfully. The thread's stack information is then printed to the console.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.