< prev index next >

src/os/bsd/vm/os_bsd.cpp

Print this page
rev 10257 : 8149036: Add tracing for thread related events at os level
Reviewed-by: coleenp


  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 // no precompiled headers
  26 #include "classfile/classLoader.hpp"
  27 #include "classfile/systemDictionary.hpp"
  28 #include "classfile/vmSymbols.hpp"
  29 #include "code/icBuffer.hpp"
  30 #include "code/vtableStubs.hpp"
  31 #include "compiler/compileBroker.hpp"
  32 #include "compiler/disassembler.hpp"
  33 #include "interpreter/interpreter.hpp"
  34 #include "jvm_bsd.h"

  35 #include "memory/allocation.inline.hpp"
  36 #include "memory/filemap.hpp"
  37 #include "mutex_bsd.inline.hpp"
  38 #include "oops/oop.inline.hpp"
  39 #include "os_bsd.inline.hpp"
  40 #include "os_share_bsd.hpp"
  41 #include "prims/jniFastGetField.hpp"
  42 #include "prims/jvm.h"
  43 #include "prims/jvm_misc.hpp"
  44 #include "runtime/arguments.hpp"
  45 #include "runtime/atomic.inline.hpp"
  46 #include "runtime/extendedPC.hpp"
  47 #include "runtime/globals.hpp"
  48 #include "runtime/interfaceSupport.hpp"
  49 #include "runtime/java.hpp"
  50 #include "runtime/javaCalls.hpp"
  51 #include "runtime/mutexLocker.hpp"
  52 #include "runtime/objectMonitor.hpp"
  53 #include "runtime/orderAccess.inline.hpp"
  54 #include "runtime/osThread.hpp"


 664 #endif
 665 
 666 // Thread start routine for all newly created threads
 667 static void *java_start(Thread *thread) {
 668   // Try to randomize the cache line index of hot stack frames.
 669   // This helps when threads of the same stack traces evict each other's
 670   // cache lines. The threads can be either from the same JVM instance, or
 671   // from different JVM instances. The benefit is especially true for
 672   // processors with hyperthreading technology.
 673   static int counter = 0;
 674   int pid = os::current_process_id();
 675   alloca(((pid ^ counter++) & 7) * 128);
 676 
 677   thread->initialize_thread_current();
 678 
 679   OSThread* osthread = thread->osthread();
 680   Monitor* sync = osthread->startThread_lock();
 681 
 682   osthread->set_thread_id(os::Bsd::gettid());
 683 



 684 #ifdef __APPLE__
 685   uint64_t unique_thread_id = locate_unique_thread_id(osthread->thread_id());
 686   guarantee(unique_thread_id != 0, "unique thread id was not found");
 687   osthread->set_unique_thread_id(unique_thread_id);
 688 #endif
 689   // initialize signal mask for this thread
 690   os::Bsd::hotspot_sigmask(thread);
 691 
 692   // initialize floating point control register
 693   os::Bsd::init_thread_fpu_state();
 694 
 695 #ifdef __APPLE__
 696   // register thread with objc gc
 697   if (objc_registerThreadWithCollectorFunction != NULL) {
 698     objc_registerThreadWithCollectorFunction();
 699   }
 700 #endif
 701 
 702   // handshaking with parent thread
 703   {
 704     MutexLockerEx ml(sync, Mutex::_no_safepoint_check_flag);
 705 
 706     // notify parent thread
 707     osthread->set_state(INITIALIZED);
 708     sync->notify_all();
 709 
 710     // wait until os::start_thread()
 711     while (osthread->get_state() == INITIALIZED) {
 712       sync->wait(Mutex::_no_safepoint_check_flag);
 713     }
 714   }
 715 
 716   // call one more level start routine
 717   thread->run();
 718 



 719   return 0;
 720 }
 721 
 722 bool os::create_thread(Thread* thread, ThreadType thr_type, size_t stack_size) {
 723   assert(thread->osthread() == NULL, "caller responsible");
 724 
 725   // Allocate the OSThread object
 726   OSThread* osthread = new OSThread(NULL, NULL);
 727   if (osthread == NULL) {
 728     return false;
 729   }
 730 
 731   // set the correct thread state
 732   osthread->set_thread_type(thr_type);
 733 
 734   // Initial state is ALLOCATED but not INITIALIZED
 735   osthread->set_state(ALLOCATED);
 736 
 737   thread->set_osthread(osthread);
 738 


 759       } // else fall through:
 760         // use VMThreadStackSize if CompilerThreadStackSize is not defined
 761     case os::vm_thread:
 762     case os::pgc_thread:
 763     case os::cgc_thread:
 764     case os::watcher_thread:
 765       if (VMThreadStackSize > 0) stack_size = (size_t)(VMThreadStackSize * K);
 766       break;
 767     }
 768   }
 769 
 770   stack_size = MAX2(stack_size, os::Bsd::min_stack_allowed);
 771   pthread_attr_setstacksize(&attr, stack_size);
 772 
 773   ThreadState state;
 774 
 775   {
 776     pthread_t tid;
 777     int ret = pthread_create(&tid, &attr, (void* (*)(void*)) java_start, thread);
 778 












 779     pthread_attr_destroy(&attr);
 780 
 781     if (ret != 0) {
 782       if (PrintMiscellaneous && (Verbose || WizardMode)) {
 783         perror("pthread_create()");
 784       }
 785       // Need to clean up stuff we've allocated so far
 786       thread->set_osthread(NULL);
 787       delete osthread;
 788       return false;
 789     }
 790 
 791     // Store pthread info into the OSThread
 792     osthread->set_pthread_id(tid);
 793 
 794     // Wait until child thread is either initialized or aborted
 795     {
 796       Monitor* sync_with_child = osthread->startThread_lock();
 797       MutexLockerEx ml(sync_with_child, Mutex::_no_safepoint_check_flag);
 798       while ((state = osthread->get_state()) == ALLOCATED) {
 799         sync_with_child->wait(Mutex::_no_safepoint_check_flag);
 800       }
 801     }
 802 
 803   }
 804 


 840 
 841   // Store pthread info into the OSThread
 842 #ifdef __APPLE__
 843   uint64_t unique_thread_id = locate_unique_thread_id(osthread->thread_id());
 844   guarantee(unique_thread_id != 0, "just checking");
 845   osthread->set_unique_thread_id(unique_thread_id);
 846 #endif
 847   osthread->set_pthread_id(::pthread_self());
 848 
 849   // initialize floating point control register
 850   os::Bsd::init_thread_fpu_state();
 851 
 852   // Initial thread state is RUNNABLE
 853   osthread->set_state(RUNNABLE);
 854 
 855   thread->set_osthread(osthread);
 856 
 857   // initialize signal mask for this thread
 858   // and save the caller's signal mask
 859   os::Bsd::hotspot_sigmask(thread);



 860 
 861   return true;
 862 }
 863 
 864 void os::pd_start_thread(Thread* thread) {
 865   OSThread * osthread = thread->osthread();
 866   assert(osthread->get_state() != INITIALIZED, "just checking");
 867   Monitor* sync_with_child = osthread->startThread_lock();
 868   MutexLockerEx ml(sync_with_child, Mutex::_no_safepoint_check_flag);
 869   sync_with_child->notify();
 870 }
 871 
 872 // Free Bsd resources related to the OSThread
 873 void os::free_thread(OSThread* osthread) {
 874   assert(osthread != NULL, "osthread not set");
 875 
 876   if (Thread::current()->osthread() == osthread) {
 877     // Restore caller's signal mask
 878     sigset_t sigmask = osthread->caller_sigmask();
 879     pthread_sigmask(SIG_SETMASK, &sigmask, NULL);




  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 // no precompiled headers
  26 #include "classfile/classLoader.hpp"
  27 #include "classfile/systemDictionary.hpp"
  28 #include "classfile/vmSymbols.hpp"
  29 #include "code/icBuffer.hpp"
  30 #include "code/vtableStubs.hpp"
  31 #include "compiler/compileBroker.hpp"
  32 #include "compiler/disassembler.hpp"
  33 #include "interpreter/interpreter.hpp"
  34 #include "jvm_bsd.h"
  35 #include "logging/log.hpp"
  36 #include "memory/allocation.inline.hpp"
  37 #include "memory/filemap.hpp"
  38 #include "mutex_bsd.inline.hpp"
  39 #include "oops/oop.inline.hpp"
  40 #include "os_bsd.inline.hpp"
  41 #include "os_share_bsd.hpp"
  42 #include "prims/jniFastGetField.hpp"
  43 #include "prims/jvm.h"
  44 #include "prims/jvm_misc.hpp"
  45 #include "runtime/arguments.hpp"
  46 #include "runtime/atomic.inline.hpp"
  47 #include "runtime/extendedPC.hpp"
  48 #include "runtime/globals.hpp"
  49 #include "runtime/interfaceSupport.hpp"
  50 #include "runtime/java.hpp"
  51 #include "runtime/javaCalls.hpp"
  52 #include "runtime/mutexLocker.hpp"
  53 #include "runtime/objectMonitor.hpp"
  54 #include "runtime/orderAccess.inline.hpp"
  55 #include "runtime/osThread.hpp"


 665 #endif
 666 
 667 // Thread start routine for all newly created threads
 668 static void *java_start(Thread *thread) {
 669   // Try to randomize the cache line index of hot stack frames.
 670   // This helps when threads of the same stack traces evict each other's
 671   // cache lines. The threads can be either from the same JVM instance, or
 672   // from different JVM instances. The benefit is especially true for
 673   // processors with hyperthreading technology.
 674   static int counter = 0;
 675   int pid = os::current_process_id();
 676   alloca(((pid ^ counter++) & 7) * 128);
 677 
 678   thread->initialize_thread_current();
 679 
 680   OSThread* osthread = thread->osthread();
 681   Monitor* sync = osthread->startThread_lock();
 682 
 683   osthread->set_thread_id(os::Bsd::gettid());
 684 
 685   log_debug(os)("Thread is alive (tid: " UINTX_FORMAT ", pthread id: " UINTX_FORMAT ".",
 686     os::current_thread_id(), (uintx) pthread_self());
 687 
 688 #ifdef __APPLE__
 689   uint64_t unique_thread_id = locate_unique_thread_id(osthread->thread_id());
 690   guarantee(unique_thread_id != 0, "unique thread id was not found");
 691   osthread->set_unique_thread_id(unique_thread_id);
 692 #endif
 693   // initialize signal mask for this thread
 694   os::Bsd::hotspot_sigmask(thread);
 695 
 696   // initialize floating point control register
 697   os::Bsd::init_thread_fpu_state();
 698 
 699 #ifdef __APPLE__
 700   // register thread with objc gc
 701   if (objc_registerThreadWithCollectorFunction != NULL) {
 702     objc_registerThreadWithCollectorFunction();
 703   }
 704 #endif
 705 
 706   // handshaking with parent thread
 707   {
 708     MutexLockerEx ml(sync, Mutex::_no_safepoint_check_flag);
 709 
 710     // notify parent thread
 711     osthread->set_state(INITIALIZED);
 712     sync->notify_all();
 713 
 714     // wait until os::start_thread()
 715     while (osthread->get_state() == INITIALIZED) {
 716       sync->wait(Mutex::_no_safepoint_check_flag);
 717     }
 718   }
 719 
 720   // call one more level start routine
 721   thread->run();
 722 
 723   log_debug(os)("Thread finished (tid " UINTX_FORMAT ", pthread id " UINTX_FORMAT ").",
 724     os::current_thread_id(), (uintx) pthread_self());
 725 
 726   return 0;
 727 }
 728 
 729 bool os::create_thread(Thread* thread, ThreadType thr_type, size_t stack_size) {
 730   assert(thread->osthread() == NULL, "caller responsible");
 731 
 732   // Allocate the OSThread object
 733   OSThread* osthread = new OSThread(NULL, NULL);
 734   if (osthread == NULL) {
 735     return false;
 736   }
 737 
 738   // set the correct thread state
 739   osthread->set_thread_type(thr_type);
 740 
 741   // Initial state is ALLOCATED but not INITIALIZED
 742   osthread->set_state(ALLOCATED);
 743 
 744   thread->set_osthread(osthread);
 745 


 766       } // else fall through:
 767         // use VMThreadStackSize if CompilerThreadStackSize is not defined
 768     case os::vm_thread:
 769     case os::pgc_thread:
 770     case os::cgc_thread:
 771     case os::watcher_thread:
 772       if (VMThreadStackSize > 0) stack_size = (size_t)(VMThreadStackSize * K);
 773       break;
 774     }
 775   }
 776 
 777   stack_size = MAX2(stack_size, os::Bsd::min_stack_allowed);
 778   pthread_attr_setstacksize(&attr, stack_size);
 779 
 780   ThreadState state;
 781 
 782   {
 783     pthread_t tid;
 784     int ret = pthread_create(&tid, &attr, (void* (*)(void*)) java_start, thread);
 785 
 786     LogHandle(os) log;
 787     if (log.is_debug()) {
 788       char buf[64];
 789       if (ret == 0) {
 790         log.debug("Thread started (pthread id: " UINTX_FORMAT ", attributes: %s). ",
 791           (uintx) tid, os::Posix::describe_pthread_attr(buf, sizeof(buf), &attr));
 792       } else {
 793         log.warning("Failed to start thread - pthread_create failed (%s) for attributes: %s.",
 794           strerror(errno), os::Posix::describe_pthread_attr(buf, sizeof(buf), &attr));
 795       }
 796     }
 797 
 798     pthread_attr_destroy(&attr);
 799 
 800     if (ret != 0) {



 801       // Need to clean up stuff we've allocated so far
 802       thread->set_osthread(NULL);
 803       delete osthread;
 804       return false;
 805     }
 806 
 807     // Store pthread info into the OSThread
 808     osthread->set_pthread_id(tid);
 809 
 810     // Wait until child thread is either initialized or aborted
 811     {
 812       Monitor* sync_with_child = osthread->startThread_lock();
 813       MutexLockerEx ml(sync_with_child, Mutex::_no_safepoint_check_flag);
 814       while ((state = osthread->get_state()) == ALLOCATED) {
 815         sync_with_child->wait(Mutex::_no_safepoint_check_flag);
 816       }
 817     }
 818 
 819   }
 820 


 856 
 857   // Store pthread info into the OSThread
 858 #ifdef __APPLE__
 859   uint64_t unique_thread_id = locate_unique_thread_id(osthread->thread_id());
 860   guarantee(unique_thread_id != 0, "just checking");
 861   osthread->set_unique_thread_id(unique_thread_id);
 862 #endif
 863   osthread->set_pthread_id(::pthread_self());
 864 
 865   // initialize floating point control register
 866   os::Bsd::init_thread_fpu_state();
 867 
 868   // Initial thread state is RUNNABLE
 869   osthread->set_state(RUNNABLE);
 870 
 871   thread->set_osthread(osthread);
 872 
 873   // initialize signal mask for this thread
 874   // and save the caller's signal mask
 875   os::Bsd::hotspot_sigmask(thread);
 876 
 877   log_debug(os)("Thread attached (tid: " UINTX_FORMAT ", pthread id: " UINTX_FORMAT ".",
 878     os::current_thread_id(), (uintx) pthread_self());
 879 
 880   return true;
 881 }
 882 
 883 void os::pd_start_thread(Thread* thread) {
 884   OSThread * osthread = thread->osthread();
 885   assert(osthread->get_state() != INITIALIZED, "just checking");
 886   Monitor* sync_with_child = osthread->startThread_lock();
 887   MutexLockerEx ml(sync_with_child, Mutex::_no_safepoint_check_flag);
 888   sync_with_child->notify();
 889 }
 890 
 891 // Free Bsd resources related to the OSThread
 892 void os::free_thread(OSThread* osthread) {
 893   assert(osthread != NULL, "osthread not set");
 894 
 895   if (Thread::current()->osthread() == osthread) {
 896     // Restore caller's signal mask
 897     sigset_t sigmask = osthread->caller_sigmask();
 898     pthread_sigmask(SIG_SETMASK, &sigmask, NULL);


< prev index next >