< prev index next >

src/os/aix/vm/os_aix.cpp

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


  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 // According to the AIX OS doc #pragma alloca must be used
  27 // with C++ compiler before referencing the function alloca()
  28 #pragma alloca
  29 
  30 // no precompiled headers
  31 #include "classfile/classLoader.hpp"
  32 #include "classfile/systemDictionary.hpp"
  33 #include "classfile/vmSymbols.hpp"
  34 #include "code/icBuffer.hpp"
  35 #include "code/vtableStubs.hpp"
  36 #include "compiler/compileBroker.hpp"
  37 #include "interpreter/interpreter.hpp"
  38 #include "jvm_aix.h"

  39 #include "libo4.hpp"
  40 #include "libperfstat_aix.hpp"
  41 #include "libodm_aix.hpp"
  42 #include "loadlib_aix.hpp"
  43 #include "memory/allocation.inline.hpp"
  44 #include "memory/filemap.hpp"
  45 #include "misc_aix.hpp"
  46 #include "mutex_aix.inline.hpp"
  47 #include "oops/oop.inline.hpp"
  48 #include "os_aix.inline.hpp"
  49 #include "os_share_aix.hpp"
  50 #include "porting_aix.hpp"
  51 #include "prims/jniFastGetField.hpp"
  52 #include "prims/jvm.h"
  53 #include "prims/jvm_misc.hpp"
  54 #include "runtime/arguments.hpp"
  55 #include "runtime/atomic.inline.hpp"
  56 #include "runtime/extendedPC.hpp"
  57 #include "runtime/globals.hpp"
  58 #include "runtime/interfaceSupport.hpp"


 774 
 775 //////////////////////////////////////////////////////////////////////////////
 776 // create new thread
 777 
 778 // Thread start routine for all newly created threads
 779 static void *java_start(Thread *thread) {
 780 
 781   // find out my own stack dimensions
 782   {
 783     // actually, this should do exactly the same as thread->record_stack_base_and_size...
 784     address base = 0;
 785     size_t size = 0;
 786     query_stack_dimensions(&base, &size);
 787     thread->set_stack_base(base);
 788     thread->set_stack_size(size);
 789   }
 790 
 791   const pthread_t pthread_id = ::pthread_self();
 792   const tid_t kernel_thread_id = ::thread_self();
 793 
 794   trcVerbose("newborn Thread : pthread-id %u, ktid " UINT64_FORMAT
 795     ", stack %p ... %p, stacksize 0x%IX (%IB)",
 796     pthread_id, kernel_thread_id,
 797     thread->stack_end(),
 798     thread->stack_base(),
 799     thread->stack_size(),
 800     thread->stack_size());
 801 
 802   // Normally, pthread stacks on AIX live in the data segment (are allocated with malloc()
 803   // by the pthread library). In rare cases, this may not be the case, e.g. when third-party
 804   // tools hook pthread_create(). In this case, we may run into problems establishing
 805   // guard pages on those stacks, because the stacks may reside in memory which is not
 806   // protectable (shmated).
 807   if (thread->stack_base() > ::sbrk(0)) {
 808     trcVerbose("Thread " UINT64_FORMAT ": stack not in data segment.", (uint64_t) pthread_id);
 809   }
 810 
 811   // Try to randomize the cache line index of hot stack frames.
 812   // This helps when threads of the same stack traces evict each other's
 813   // cache lines. The threads can be either from the same JVM instance, or
 814   // from different JVM instances. The benefit is especially true for
 815   // processors with hyperthreading technology.
 816 
 817   static int counter = 0;
 818   int pid = os::current_process_id();
 819   alloca(((pid ^ counter++) & 7) * 128);
 820 
 821   thread->initialize_thread_current();
 822 
 823   OSThread* osthread = thread->osthread();
 824 
 825   // Thread_id is pthread id.
 826   osthread->set_thread_id(pthread_id);
 827 
 828   // .. but keep kernel thread id too for diagnostics
 829   osthread->set_kernel_thread_id(kernel_thread_id);
 830 
 831   // Initialize signal mask for this thread.
 832   os::Aix::hotspot_sigmask(thread);
 833 
 834   // Initialize floating point control register.
 835   os::Aix::init_thread_fpu_state();
 836 
 837   assert(osthread->get_state() == RUNNABLE, "invalid os thread state");
 838 
 839   // Call one more level start routine.
 840   thread->run();
 841 
 842   trcVerbose("Thread finished : pthread-id %u, ktid " UINT64_FORMAT ".",
 843     pthread_id, kernel_thread_id);
 844 
 845   return 0;
 846 }
 847 
 848 bool os::create_thread(Thread* thread, ThreadType thr_type, size_t stack_size) {
 849 
 850   assert(thread->osthread() == NULL, "caller responsible");
 851 
 852   // Allocate the OSThread object
 853   OSThread* osthread = new OSThread(NULL, NULL);
 854   if (osthread == NULL) {
 855     return false;
 856   }
 857 
 858   // set the correct thread state
 859   osthread->set_thread_type(thr_type);
 860 
 861   // Initial state is ALLOCATED but not INITIALIZED
 862   osthread->set_state(ALLOCATED);
 863 


 891       if (CompilerThreadStackSize > 0) {
 892         stack_size = (size_t)(CompilerThreadStackSize * K);
 893         break;
 894       } // else fall through:
 895         // use VMThreadStackSize if CompilerThreadStackSize is not defined
 896     case os::vm_thread:
 897     case os::pgc_thread:
 898     case os::cgc_thread:
 899     case os::watcher_thread:
 900       if (VMThreadStackSize > 0) stack_size = (size_t)(VMThreadStackSize * K);
 901       break;
 902     }
 903   }
 904 
 905   stack_size = MAX2(stack_size, os::Aix::min_stack_allowed);
 906   pthread_attr_setstacksize(&attr, stack_size);
 907 
 908   pthread_t tid;
 909   int ret = pthread_create(&tid, &attr, (void* (*)(void*)) java_start, thread);
 910 
 911   pthread_attr_destroy(&attr);
 912 

 913   if (ret == 0) {
 914     trcVerbose("Created New Thread : pthread-id %u", tid);

 915   } else {
 916     if (os::Aix::on_pase()) {
 917       // QIBM_MULTI_THREADED=Y is needed when the launcher is started on iSeries
 918       // using QSH. Otherwise pthread_create fails with errno=11.
 919       trcVerbose("(Please make sure you set the environment variable "
 920               "QIBM_MULTI_THREADED=Y before running this program.)");
 921     }
 922     if (PrintMiscellaneous && (Verbose || WizardMode)) {
 923       perror("pthread_create()");
 924     }




 925     // Need to clean up stuff we've allocated so far
 926     thread->set_osthread(NULL);
 927     delete osthread;
 928     return false;
 929   }
 930 
 931   // OSThread::thread_id is the pthread id.
 932   osthread->set_thread_id(tid);
 933 
 934   return true;
 935 }
 936 
 937 /////////////////////////////////////////////////////////////////////////////
 938 // attach existing thread
 939 
 940 // bootstrap the main thread
 941 bool os::create_main_thread(JavaThread* thread) {
 942   assert(os::Aix::_main_thread == pthread_self(), "should be called inside main thread");
 943   return create_attached_thread(thread);
 944 }
 945 
 946 bool os::create_attached_thread(JavaThread* thread) {
 947 #ifdef ASSERT
 948     thread->verify_not_published();
 949 #endif
 950 
 951   // Allocate the OSThread object
 952   OSThread* osthread = new OSThread(NULL, NULL);
 953 
 954   if (osthread == NULL) {
 955     return false;
 956   }
 957 
 958   const pthread_t pthread_id = ::pthread_self();
 959   const tid_t kernel_thread_id = ::thread_self();
 960 
 961   trcVerbose("attaching Thread : pthread-id %u, ktid " UINT64_FORMAT ", stack %p ... %p, stacksize 0x%IX (%IB)",
 962     pthread_id, kernel_thread_id,
 963     thread->stack_end(),
 964     thread->stack_base(),
 965     thread->stack_size(),
 966     thread->stack_size());
 967 
 968   // OSThread::thread_id is the pthread id.
 969   osthread->set_thread_id(pthread_id);
 970 
 971   // .. but keep kernel thread id too for diagnostics
 972   osthread->set_kernel_thread_id(kernel_thread_id);
 973 
 974   // initialize floating point control register
 975   os::Aix::init_thread_fpu_state();
 976 
 977   // Initial thread state is RUNNABLE
 978   osthread->set_state(RUNNABLE);
 979 
 980   thread->set_osthread(osthread);
 981 
 982   if (UseNUMA) {
 983     int lgrp_id = os::numa_get_group_id();
 984     if (lgrp_id != -1) {
 985       thread->set_lgrp_id(lgrp_id);
 986     }
 987   }
 988 
 989   // initialize signal mask for this thread
 990   // and save the caller's signal mask
 991   os::Aix::hotspot_sigmask(thread);



 992 
 993   return true;
 994 }
 995 
 996 void os::pd_start_thread(Thread* thread) {
 997   int status = pthread_continue_np(thread->osthread()->pthread_id());
 998   assert(status == 0, "thr_continue failed");
 999 }
1000 
1001 // Free OS resources related to the OSThread
1002 void os::free_thread(OSThread* osthread) {
1003   assert(osthread != NULL, "osthread not set");
1004 
1005   if (Thread::current()->osthread() == osthread) {
1006     // Restore caller's signal mask
1007     sigset_t sigmask = osthread->caller_sigmask();
1008     pthread_sigmask(SIG_SETMASK, &sigmask, NULL);
1009    }
1010 
1011   delete osthread;




  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 // According to the AIX OS doc #pragma alloca must be used
  27 // with C++ compiler before referencing the function alloca()
  28 #pragma alloca
  29 
  30 // no precompiled headers
  31 #include "classfile/classLoader.hpp"
  32 #include "classfile/systemDictionary.hpp"
  33 #include "classfile/vmSymbols.hpp"
  34 #include "code/icBuffer.hpp"
  35 #include "code/vtableStubs.hpp"
  36 #include "compiler/compileBroker.hpp"
  37 #include "interpreter/interpreter.hpp"
  38 #include "jvm_aix.h"
  39 #include "logging/log.hpp"
  40 #include "libo4.hpp"
  41 #include "libperfstat_aix.hpp"
  42 #include "libodm_aix.hpp"
  43 #include "loadlib_aix.hpp"
  44 #include "memory/allocation.inline.hpp"
  45 #include "memory/filemap.hpp"
  46 #include "misc_aix.hpp"
  47 #include "mutex_aix.inline.hpp"
  48 #include "oops/oop.inline.hpp"
  49 #include "os_aix.inline.hpp"
  50 #include "os_share_aix.hpp"
  51 #include "porting_aix.hpp"
  52 #include "prims/jniFastGetField.hpp"
  53 #include "prims/jvm.h"
  54 #include "prims/jvm_misc.hpp"
  55 #include "runtime/arguments.hpp"
  56 #include "runtime/atomic.inline.hpp"
  57 #include "runtime/extendedPC.hpp"
  58 #include "runtime/globals.hpp"
  59 #include "runtime/interfaceSupport.hpp"


 775 
 776 //////////////////////////////////////////////////////////////////////////////
 777 // create new thread
 778 
 779 // Thread start routine for all newly created threads
 780 static void *java_start(Thread *thread) {
 781 
 782   // find out my own stack dimensions
 783   {
 784     // actually, this should do exactly the same as thread->record_stack_base_and_size...
 785     address base = 0;
 786     size_t size = 0;
 787     query_stack_dimensions(&base, &size);
 788     thread->set_stack_base(base);
 789     thread->set_stack_size(size);
 790   }
 791 
 792   const pthread_t pthread_id = ::pthread_self();
 793   const tid_t kernel_thread_id = ::thread_self();
 794 
 795   log_debug(os, thread)("Thread is alive (pthread id " UINTX_FORMAT ", tid " UINTX_FORMAT ")",
 796     (uintx) pthread_id, (uintx) kernel_thread_id);





 797 
 798   // Normally, pthread stacks on AIX live in the data segment (are allocated with malloc()
 799   // by the pthread library). In rare cases, this may not be the case, e.g. when third-party
 800   // tools hook pthread_create(). In this case, we may run into problems establishing
 801   // guard pages on those stacks, because the stacks may reside in memory which is not
 802   // protectable (shmated).
 803   if (thread->stack_base() > ::sbrk(0)) {
 804     log_warning(os, thread)("Thread " UINTX_FORMAT ": stack not in data segment.", (uintx)pthread_id);
 805   }
 806 
 807   // Try to randomize the cache line index of hot stack frames.
 808   // This helps when threads of the same stack traces evict each other's
 809   // cache lines. The threads can be either from the same JVM instance, or
 810   // from different JVM instances. The benefit is especially true for
 811   // processors with hyperthreading technology.
 812 
 813   static int counter = 0;
 814   int pid = os::current_process_id();
 815   alloca(((pid ^ counter++) & 7) * 128);
 816 
 817   thread->initialize_thread_current();
 818 
 819   OSThread* osthread = thread->osthread();
 820 
 821   // Thread_id is pthread id.
 822   osthread->set_thread_id(pthread_id);
 823 
 824   // .. but keep kernel thread id too for diagnostics
 825   osthread->set_kernel_thread_id(kernel_thread_id);
 826 
 827   // Initialize signal mask for this thread.
 828   os::Aix::hotspot_sigmask(thread);
 829 
 830   // Initialize floating point control register.
 831   os::Aix::init_thread_fpu_state();
 832 
 833   assert(osthread->get_state() == RUNNABLE, "invalid os thread state");
 834 
 835   // Call one more level start routine.
 836   thread->run();
 837 
 838   log_debug(os, thread)("Thread finished (pthread id " UINTX_FORMAT ", tid " UINTX_FORMAT ").",
 839     (uintx) pthread_id, (uintx) kernel_thread_id);
 840 
 841   return 0;
 842 }
 843 
 844 bool os::create_thread(Thread* thread, ThreadType thr_type, size_t stack_size) {
 845 
 846   assert(thread->osthread() == NULL, "caller responsible");
 847 
 848   // Allocate the OSThread object
 849   OSThread* osthread = new OSThread(NULL, NULL);
 850   if (osthread == NULL) {
 851     return false;
 852   }
 853 
 854   // set the correct thread state
 855   osthread->set_thread_type(thr_type);
 856 
 857   // Initial state is ALLOCATED but not INITIALIZED
 858   osthread->set_state(ALLOCATED);
 859 


 887       if (CompilerThreadStackSize > 0) {
 888         stack_size = (size_t)(CompilerThreadStackSize * K);
 889         break;
 890       } // else fall through:
 891         // use VMThreadStackSize if CompilerThreadStackSize is not defined
 892     case os::vm_thread:
 893     case os::pgc_thread:
 894     case os::cgc_thread:
 895     case os::watcher_thread:
 896       if (VMThreadStackSize > 0) stack_size = (size_t)(VMThreadStackSize * K);
 897       break;
 898     }
 899   }
 900 
 901   stack_size = MAX2(stack_size, os::Aix::min_stack_allowed);
 902   pthread_attr_setstacksize(&attr, stack_size);
 903 
 904   pthread_t tid;
 905   int ret = pthread_create(&tid, &attr, (void* (*)(void*)) java_start, thread);
 906 
 907   LogHandle(os, thread) log;
 908   if (log.is_debug()) {
 909     char buf[64];
 910     if (ret == 0) {
 911       log.debug("Thread started (pthread id: " UINTX_FORMAT ", attributes: %s). ",
 912         (uintx) tid, os::Posix::describe_pthread_attr(buf, sizeof(buf), &attr));
 913     } else {
 914       log.warning("Failed to start thread - pthread_create failed (%s) for attributes: %s.",
 915         strerror(errno), os::Posix::describe_pthread_attr(buf, sizeof(buf), &attr));



 916     }


 917   }
 918 
 919   pthread_attr_destroy(&attr);
 920 
 921   if (ret != 0) {
 922     // Need to clean up stuff we've allocated so far
 923     thread->set_osthread(NULL);
 924     delete osthread;
 925     return false;
 926   }
 927 
 928   // OSThread::thread_id is the pthread id.
 929   osthread->set_thread_id(tid);
 930 
 931   return true;
 932 }
 933 
 934 /////////////////////////////////////////////////////////////////////////////
 935 // attach existing thread
 936 
 937 // bootstrap the main thread
 938 bool os::create_main_thread(JavaThread* thread) {
 939   assert(os::Aix::_main_thread == pthread_self(), "should be called inside main thread");
 940   return create_attached_thread(thread);
 941 }
 942 
 943 bool os::create_attached_thread(JavaThread* thread) {
 944 #ifdef ASSERT
 945     thread->verify_not_published();
 946 #endif
 947 
 948   // Allocate the OSThread object
 949   OSThread* osthread = new OSThread(NULL, NULL);
 950 
 951   if (osthread == NULL) {
 952     return false;
 953   }
 954 
 955   const pthread_t pthread_id = ::pthread_self();
 956   const tid_t kernel_thread_id = ::thread_self();
 957 







 958   // OSThread::thread_id is the pthread id.
 959   osthread->set_thread_id(pthread_id);
 960 
 961   // .. but keep kernel thread id too for diagnostics
 962   osthread->set_kernel_thread_id(kernel_thread_id);
 963 
 964   // initialize floating point control register
 965   os::Aix::init_thread_fpu_state();
 966 
 967   // Initial thread state is RUNNABLE
 968   osthread->set_state(RUNNABLE);
 969 
 970   thread->set_osthread(osthread);
 971 
 972   if (UseNUMA) {
 973     int lgrp_id = os::numa_get_group_id();
 974     if (lgrp_id != -1) {
 975       thread->set_lgrp_id(lgrp_id);
 976     }
 977   }
 978 
 979   // initialize signal mask for this thread
 980   // and save the caller's signal mask
 981   os::Aix::hotspot_sigmask(thread);
 982 
 983   log_debug(os, thread)("Thread attached (pthread id " UINTX_FORMAT ", tid " UINTX_FORMAT ")",
 984     (uintx) pthread_id, (uintx) kernel_thread_id);
 985 
 986   return true;
 987 }
 988 
 989 void os::pd_start_thread(Thread* thread) {
 990   int status = pthread_continue_np(thread->osthread()->pthread_id());
 991   assert(status == 0, "thr_continue failed");
 992 }
 993 
 994 // Free OS resources related to the OSThread
 995 void os::free_thread(OSThread* osthread) {
 996   assert(osthread != NULL, "osthread not set");
 997 
 998   if (Thread::current()->osthread() == osthread) {
 999     // Restore caller's signal mask
1000     sigset_t sigmask = osthread->caller_sigmask();
1001     pthread_sigmask(SIG_SETMASK, &sigmask, NULL);
1002    }
1003 
1004   delete osthread;


< prev index next >