src/os/bsd/vm/os_bsd.cpp

Print this page
rev 3111 : imported patch osx-threadid


 962   // processors with hyperthreading technology.
 963   static int counter = 0;
 964   int pid = os::current_process_id();
 965   alloca(((pid ^ counter++) & 7) * 128);
 966 
 967   ThreadLocalStorage::set_thread(thread);
 968 
 969   OSThread* osthread = thread->osthread();
 970   Monitor* sync = osthread->startThread_lock();
 971 
 972   // non floating stack BsdThreads needs extra check, see above
 973   if (!_thread_safety_check(thread)) {
 974     // notify parent thread
 975     MutexLockerEx ml(sync, Mutex::_no_safepoint_check_flag);
 976     osthread->set_state(ZOMBIE);
 977     sync->notify_all();
 978     return NULL;
 979   }
 980 
 981 #ifdef _ALLBSD_SOURCE
 982   // thread_id is pthread_id on BSD
 983   osthread->set_thread_id(::pthread_self());
 984 #else
 985   // thread_id is kernel thread id (similar to Solaris LWP id)
 986   osthread->set_thread_id(os::Bsd::gettid());
 987 
 988   if (UseNUMA) {
 989     int lgrp_id = os::numa_get_group_id();
 990     if (lgrp_id != -1) {
 991       thread->set_lgrp_id(lgrp_id);
 992     }
 993   }
 994 #endif
 995   // initialize signal mask for this thread
 996   os::Bsd::hotspot_sigmask(thread);
 997 
 998   // initialize floating point control register
 999   os::Bsd::init_thread_fpu_state();
1000 
1001 #ifdef __APPLE__
1002   // register thread with objc gc
1003   if (objc_registerThreadWithCollectorFunction != NULL) {


1154 // bootstrap the main thread
1155 bool os::create_main_thread(JavaThread* thread) {
1156   assert(os::Bsd::_main_thread == pthread_self(), "should be called inside main thread");
1157   return create_attached_thread(thread);
1158 }
1159 
1160 bool os::create_attached_thread(JavaThread* thread) {
1161 #ifdef ASSERT
1162     thread->verify_not_published();
1163 #endif
1164 
1165   // Allocate the OSThread object
1166   OSThread* osthread = new OSThread(NULL, NULL);
1167 
1168   if (osthread == NULL) {
1169     return false;
1170   }
1171 
1172   // Store pthread info into the OSThread
1173 #ifdef _ALLBSD_SOURCE
1174   osthread->set_thread_id(::pthread_self());
1175 #else
1176   osthread->set_thread_id(os::Bsd::gettid());
1177 #endif
1178   osthread->set_pthread_id(::pthread_self());
1179 
1180   // initialize floating point control register
1181   os::Bsd::init_thread_fpu_state();
1182 
1183   // Initial thread state is RUNNABLE
1184   osthread->set_state(RUNNABLE);
1185 
1186   thread->set_osthread(osthread);
1187 
1188 #ifndef _ALLBSD_SOURCE
1189   if (UseNUMA) {
1190     int lgrp_id = os::numa_get_group_id();
1191     if (lgrp_id != -1) {
1192       thread->set_lgrp_id(lgrp_id);
1193     }
1194   }


1771 void os::set_error_file(const char *logfile) {}
1772 
1773 
1774 // This method is a copy of JDK's sysGetLastErrorString
1775 // from src/solaris/hpi/src/system_md.c
1776 
1777 size_t os::lasterror(char *buf, size_t len) {
1778 
1779   if (errno == 0)  return 0;
1780 
1781   const char *s = ::strerror(errno);
1782   size_t n = ::strlen(s);
1783   if (n >= len) {
1784     n = len - 1;
1785   }
1786   ::strncpy(buf, s, n);
1787   buf[n] = '\0';
1788   return n;
1789 }
1790 
1791 intx os::current_thread_id() { return (intx)pthread_self(); }
1792 int os::current_process_id() {
1793 
1794   // Under the old bsd thread library, bsd gives each thread
1795   // its own process id. Because of this each thread will return
1796   // a different pid if this method were to return the result
1797   // of getpid(2). Bsd provides no api that returns the pid
1798   // of the launcher thread for the vm. This implementation
1799   // returns a unique pid, the pid of the launcher thread
1800   // that starts the vm 'process'.
1801 
1802   // Under the NPTL, getpid() returns the same pid as the
1803   // launcher thread rather than a unique pid per thread.
1804   // Use gettid() if you want the old pre NPTL behaviour.
1805 
1806   // if you are looking for the result of a call to getpid() that
1807   // returns a unique pid for the calling thread, then look at the
1808   // OSThread::thread_id() method in osThread_bsd.hpp file
1809 
1810   return (int)(_initial_pid ? _initial_pid : getpid());
1811 }


5116 #endif
5117 }
5118 
5119 jlong os::current_thread_cpu_time(bool user_sys_cpu_time) {
5120 #ifdef __APPLE__
5121   return os::thread_cpu_time(Thread::current(), user_sys_cpu_time);
5122 #elif !defined(_ALLBSD_SOURCE)
5123   if (user_sys_cpu_time && os::Bsd::supports_fast_thread_cpu_time()) {
5124     return os::Bsd::fast_thread_cpu_time(CLOCK_THREAD_CPUTIME_ID);
5125   } else {
5126     return slow_thread_cpu_time(Thread::current(), user_sys_cpu_time);
5127   }
5128 #endif
5129 }
5130 
5131 jlong os::thread_cpu_time(Thread *thread, bool user_sys_cpu_time) {
5132 #ifdef __APPLE__
5133   struct thread_basic_info tinfo;
5134   mach_msg_type_number_t tcount = THREAD_INFO_MAX;
5135   kern_return_t kr;
5136   mach_port_t mach_thread;
5137 
5138   mach_thread = pthread_mach_thread_np(thread->osthread()->thread_id());
5139   kr = thread_info(mach_thread, THREAD_BASIC_INFO, (thread_info_t)&tinfo, &tcount);
5140   if (kr != KERN_SUCCESS)
5141     return -1;
5142 
5143   if (user_sys_cpu_time) {
5144     jlong nanos;
5145     nanos = ((jlong) tinfo.system_time.seconds + tinfo.user_time.seconds) * (jlong)1000000000;
5146     nanos += ((jlong) tinfo.system_time.microseconds + (jlong) tinfo.user_time.microseconds) * (jlong)1000;
5147     return nanos;
5148   } else {
5149     return ((jlong)tinfo.user_time.seconds * 1000000000) + ((jlong)tinfo.user_time.microseconds * (jlong)1000);
5150   }
5151 #elif !defined(_ALLBSD_SOURCE)
5152   if (user_sys_cpu_time && os::Bsd::supports_fast_thread_cpu_time()) {
5153     return os::Bsd::fast_thread_cpu_time(thread_cpu_clockid(thread));
5154   } else {
5155     return slow_thread_cpu_time(thread, user_sys_cpu_time);
5156   }
5157 #endif
5158 }




 962   // processors with hyperthreading technology.
 963   static int counter = 0;
 964   int pid = os::current_process_id();
 965   alloca(((pid ^ counter++) & 7) * 128);
 966 
 967   ThreadLocalStorage::set_thread(thread);
 968 
 969   OSThread* osthread = thread->osthread();
 970   Monitor* sync = osthread->startThread_lock();
 971 
 972   // non floating stack BsdThreads needs extra check, see above
 973   if (!_thread_safety_check(thread)) {
 974     // notify parent thread
 975     MutexLockerEx ml(sync, Mutex::_no_safepoint_check_flag);
 976     osthread->set_state(ZOMBIE);
 977     sync->notify_all();
 978     return NULL;
 979   }
 980 
 981 #ifdef _ALLBSD_SOURCE
 982   // thread_id is mach thread on BSD
 983   osthread->set_thread_id(::mach_thread_self());
 984 #else
 985   // thread_id is kernel thread id (similar to Solaris LWP id)
 986   osthread->set_thread_id(os::Bsd::gettid());
 987 
 988   if (UseNUMA) {
 989     int lgrp_id = os::numa_get_group_id();
 990     if (lgrp_id != -1) {
 991       thread->set_lgrp_id(lgrp_id);
 992     }
 993   }
 994 #endif
 995   // initialize signal mask for this thread
 996   os::Bsd::hotspot_sigmask(thread);
 997 
 998   // initialize floating point control register
 999   os::Bsd::init_thread_fpu_state();
1000 
1001 #ifdef __APPLE__
1002   // register thread with objc gc
1003   if (objc_registerThreadWithCollectorFunction != NULL) {


1154 // bootstrap the main thread
1155 bool os::create_main_thread(JavaThread* thread) {
1156   assert(os::Bsd::_main_thread == pthread_self(), "should be called inside main thread");
1157   return create_attached_thread(thread);
1158 }
1159 
1160 bool os::create_attached_thread(JavaThread* thread) {
1161 #ifdef ASSERT
1162     thread->verify_not_published();
1163 #endif
1164 
1165   // Allocate the OSThread object
1166   OSThread* osthread = new OSThread(NULL, NULL);
1167 
1168   if (osthread == NULL) {
1169     return false;
1170   }
1171 
1172   // Store pthread info into the OSThread
1173 #ifdef _ALLBSD_SOURCE
1174   osthread->set_thread_id(::mach_thread_self());
1175 #else
1176   osthread->set_thread_id(os::Bsd::gettid());
1177 #endif
1178   osthread->set_pthread_id(::pthread_self());
1179 
1180   // initialize floating point control register
1181   os::Bsd::init_thread_fpu_state();
1182 
1183   // Initial thread state is RUNNABLE
1184   osthread->set_state(RUNNABLE);
1185 
1186   thread->set_osthread(osthread);
1187 
1188 #ifndef _ALLBSD_SOURCE
1189   if (UseNUMA) {
1190     int lgrp_id = os::numa_get_group_id();
1191     if (lgrp_id != -1) {
1192       thread->set_lgrp_id(lgrp_id);
1193     }
1194   }


1771 void os::set_error_file(const char *logfile) {}
1772 
1773 
1774 // This method is a copy of JDK's sysGetLastErrorString
1775 // from src/solaris/hpi/src/system_md.c
1776 
1777 size_t os::lasterror(char *buf, size_t len) {
1778 
1779   if (errno == 0)  return 0;
1780 
1781   const char *s = ::strerror(errno);
1782   size_t n = ::strlen(s);
1783   if (n >= len) {
1784     n = len - 1;
1785   }
1786   ::strncpy(buf, s, n);
1787   buf[n] = '\0';
1788   return n;
1789 }
1790 
1791 intx os::current_thread_id() { return (intx)::mach_thread_self(); }
1792 int os::current_process_id() {
1793 
1794   // Under the old bsd thread library, bsd gives each thread
1795   // its own process id. Because of this each thread will return
1796   // a different pid if this method were to return the result
1797   // of getpid(2). Bsd provides no api that returns the pid
1798   // of the launcher thread for the vm. This implementation
1799   // returns a unique pid, the pid of the launcher thread
1800   // that starts the vm 'process'.
1801 
1802   // Under the NPTL, getpid() returns the same pid as the
1803   // launcher thread rather than a unique pid per thread.
1804   // Use gettid() if you want the old pre NPTL behaviour.
1805 
1806   // if you are looking for the result of a call to getpid() that
1807   // returns a unique pid for the calling thread, then look at the
1808   // OSThread::thread_id() method in osThread_bsd.hpp file
1809 
1810   return (int)(_initial_pid ? _initial_pid : getpid());
1811 }


5116 #endif
5117 }
5118 
5119 jlong os::current_thread_cpu_time(bool user_sys_cpu_time) {
5120 #ifdef __APPLE__
5121   return os::thread_cpu_time(Thread::current(), user_sys_cpu_time);
5122 #elif !defined(_ALLBSD_SOURCE)
5123   if (user_sys_cpu_time && os::Bsd::supports_fast_thread_cpu_time()) {
5124     return os::Bsd::fast_thread_cpu_time(CLOCK_THREAD_CPUTIME_ID);
5125   } else {
5126     return slow_thread_cpu_time(Thread::current(), user_sys_cpu_time);
5127   }
5128 #endif
5129 }
5130 
5131 jlong os::thread_cpu_time(Thread *thread, bool user_sys_cpu_time) {
5132 #ifdef __APPLE__
5133   struct thread_basic_info tinfo;
5134   mach_msg_type_number_t tcount = THREAD_INFO_MAX;
5135   kern_return_t kr;
5136   thread_t mach_thread;
5137 
5138   mach_thread = thread->osthread()->thread_id();
5139   kr = thread_info(mach_thread, THREAD_BASIC_INFO, (thread_info_t)&tinfo, &tcount);
5140   if (kr != KERN_SUCCESS)
5141     return -1;
5142 
5143   if (user_sys_cpu_time) {
5144     jlong nanos;
5145     nanos = ((jlong) tinfo.system_time.seconds + tinfo.user_time.seconds) * (jlong)1000000000;
5146     nanos += ((jlong) tinfo.system_time.microseconds + (jlong) tinfo.user_time.microseconds) * (jlong)1000;
5147     return nanos;
5148   } else {
5149     return ((jlong)tinfo.user_time.seconds * 1000000000) + ((jlong)tinfo.user_time.microseconds * (jlong)1000);
5150   }
5151 #elif !defined(_ALLBSD_SOURCE)
5152   if (user_sys_cpu_time && os::Bsd::supports_fast_thread_cpu_time()) {
5153     return os::Bsd::fast_thread_cpu_time(thread_cpu_clockid(thread));
5154   } else {
5155     return slow_thread_cpu_time(thread, user_sys_cpu_time);
5156   }
5157 #endif
5158 }