comparison src/os/bsd/vm/os_bsd.cpp @ 6948:e522a00b91aa

Merge with http://hg.openjdk.java.net/hsx/hsx25/hotspot/ after NPG - C++ build works
author Doug Simon <doug.simon@oracle.com>
date Mon, 12 Nov 2012 23:14:12 +0100
parents 957c266d8bc5 0af5da0c9d9d
children 2cb439954abf
comparison
equal deleted inserted replaced
6711:ae13cc658b80 6948:e522a00b91aa
80 #endif 80 #endif
81 #ifdef TARGET_ARCH_ppc 81 #ifdef TARGET_ARCH_ppc
82 # include "assembler_ppc.inline.hpp" 82 # include "assembler_ppc.inline.hpp"
83 # include "nativeInst_ppc.hpp" 83 # include "nativeInst_ppc.hpp"
84 #endif 84 #endif
85 #ifdef COMPILER1
86 #include "c1/c1_Runtime1.hpp"
87 #endif
88 #ifdef COMPILER2
89 #include "opto/runtime.hpp"
90 #endif
91 85
92 // put OS-includes here 86 // put OS-includes here
93 # include <sys/types.h> 87 # include <sys/types.h>
94 # include <sys/mman.h> 88 # include <sys/mman.h>
95 # include <sys/stat.h> 89 # include <sys/stat.h>
112 # include <pwd.h> 106 # include <pwd.h>
113 # include <poll.h> 107 # include <poll.h>
114 # include <semaphore.h> 108 # include <semaphore.h>
115 # include <fcntl.h> 109 # include <fcntl.h>
116 # include <string.h> 110 # include <string.h>
117 #ifdef _ALLBSD_SOURCE
118 # include <sys/param.h> 111 # include <sys/param.h>
119 # include <sys/sysctl.h> 112 # include <sys/sysctl.h>
120 #else
121 # include <syscall.h>
122 # include <sys/sysinfo.h>
123 # include <gnu/libc-version.h>
124 #endif
125 # include <sys/ipc.h> 113 # include <sys/ipc.h>
126 # include <sys/shm.h> 114 # include <sys/shm.h>
127 #ifndef __APPLE__ 115 #ifndef __APPLE__
128 # include <link.h> 116 # include <link.h>
129 #endif 117 #endif
154 #define LARGEPAGES_BIT (1 << 6) 142 #define LARGEPAGES_BIT (1 << 6)
155 //////////////////////////////////////////////////////////////////////////////// 143 ////////////////////////////////////////////////////////////////////////////////
156 // global variables 144 // global variables
157 julong os::Bsd::_physical_memory = 0; 145 julong os::Bsd::_physical_memory = 0;
158 146
159 #ifndef _ALLBSD_SOURCE
160 address os::Bsd::_initial_thread_stack_bottom = NULL;
161 uintptr_t os::Bsd::_initial_thread_stack_size = 0;
162 #endif
163 147
164 int (*os::Bsd::_clock_gettime)(clockid_t, struct timespec *) = NULL; 148 int (*os::Bsd::_clock_gettime)(clockid_t, struct timespec *) = NULL;
165 #ifndef _ALLBSD_SOURCE
166 int (*os::Bsd::_pthread_getcpuclockid)(pthread_t, clockid_t *) = NULL;
167 Mutex* os::Bsd::_createThread_lock = NULL;
168 #endif
169 pthread_t os::Bsd::_main_thread; 149 pthread_t os::Bsd::_main_thread;
170 int os::Bsd::_page_size = -1; 150 int os::Bsd::_page_size = -1;
171 #ifndef _ALLBSD_SOURCE
172 bool os::Bsd::_is_floating_stack = false;
173 bool os::Bsd::_is_NPTL = false;
174 bool os::Bsd::_supports_fast_thread_cpu_time = false;
175 const char * os::Bsd::_glibc_version = NULL;
176 const char * os::Bsd::_libpthread_version = NULL;
177 #endif
178 151
179 static jlong initial_time_count=0; 152 static jlong initial_time_count=0;
180 153
181 static int clock_tics_per_sec = 100; 154 static int clock_tics_per_sec = 100;
182 155
183 // For diagnostics to print a message once. see run_periodic_checks 156 // For diagnostics to print a message once. see run_periodic_checks
184 static sigset_t check_signal_done; 157 static sigset_t check_signal_done;
185 static bool check_signals = true;; 158 static bool check_signals = true;
186 159
187 static pid_t _initial_pid = 0; 160 static pid_t _initial_pid = 0;
188 161
189 /* Signal number used to suspend/resume a thread */ 162 /* Signal number used to suspend/resume a thread */
190 163
202 julong os::available_memory() { 175 julong os::available_memory() {
203 return Bsd::available_memory(); 176 return Bsd::available_memory();
204 } 177 }
205 178
206 julong os::Bsd::available_memory() { 179 julong os::Bsd::available_memory() {
207 #ifdef _ALLBSD_SOURCE
208 // XXXBSD: this is just a stopgap implementation 180 // XXXBSD: this is just a stopgap implementation
209 return physical_memory() >> 2; 181 return physical_memory() >> 2;
210 #else
211 // values in struct sysinfo are "unsigned long"
212 struct sysinfo si;
213 sysinfo(&si);
214
215 return (julong)si.freeram * si.mem_unit;
216 #endif
217 } 182 }
218 183
219 julong os::physical_memory() { 184 julong os::physical_memory() {
220 return Bsd::physical_memory(); 185 return Bsd::physical_memory();
221 } 186 }
259 } 224 }
260 return privileges; 225 return privileges;
261 } 226 }
262 227
263 228
264 #ifndef _ALLBSD_SOURCE
265 #ifndef SYS_gettid
266 // i386: 224, ia64: 1105, amd64: 186, sparc 143
267 #ifdef __ia64__
268 #define SYS_gettid 1105
269 #elif __i386__
270 #define SYS_gettid 224
271 #elif __amd64__
272 #define SYS_gettid 186
273 #elif __sparc__
274 #define SYS_gettid 143
275 #else
276 #error define gettid for the arch
277 #endif
278 #endif
279 #endif
280 229
281 // Cpu architecture string 230 // Cpu architecture string
282 #if defined(ZERO) 231 #if defined(ZERO)
283 static char cpu_arch[] = ZERO_LIBARCH; 232 static char cpu_arch[] = ZERO_LIBARCH;
284 #elif defined(IA64) 233 #elif defined(IA64)
306 #define COMPILER_VARIANT "server" 255 #define COMPILER_VARIANT "server"
307 #else 256 #else
308 #define COMPILER_VARIANT "client" 257 #define COMPILER_VARIANT "client"
309 #endif 258 #endif
310 259
311 #ifndef _ALLBSD_SOURCE 260
312 // pid_t gettid()
313 //
314 // Returns the kernel thread id of the currently running thread. Kernel
315 // thread id is used to access /proc.
316 //
317 // (Note that getpid() on BsdThreads returns kernel thread id too; but
318 // on NPTL, it returns the same pid for all threads, as required by POSIX.)
319 //
320 pid_t os::Bsd::gettid() {
321 int rslt = syscall(SYS_gettid);
322 if (rslt == -1) {
323 // old kernel, no NPTL support
324 return getpid();
325 } else {
326 return (pid_t)rslt;
327 }
328 }
329
330 // Most versions of bsd have a bug where the number of processors are
331 // determined by looking at the /proc file system. In a chroot environment,
332 // the system call returns 1. This causes the VM to act as if it is
333 // a single processor and elide locking (see is_MP() call).
334 static bool unsafe_chroot_detected = false;
335 static const char *unstable_chroot_error = "/proc file system not found.\n"
336 "Java may be unstable running multithreaded in a chroot "
337 "environment on Bsd when /proc filesystem is not mounted.";
338 #endif
339
340 #ifdef _ALLBSD_SOURCE
341 void os::Bsd::initialize_system_info() { 261 void os::Bsd::initialize_system_info() {
342 int mib[2]; 262 int mib[2];
343 size_t len; 263 size_t len;
344 int cpu_val; 264 int cpu_val;
345 u_long mem_val; 265 u_long mem_val;
374 getrlimit(RLIMIT_DATA, &limits); 294 getrlimit(RLIMIT_DATA, &limits);
375 _physical_memory = MIN2(_physical_memory, (julong)limits.rlim_cur); 295 _physical_memory = MIN2(_physical_memory, (julong)limits.rlim_cur);
376 } 296 }
377 #endif 297 #endif
378 } 298 }
379 #else
380 void os::Bsd::initialize_system_info() {
381 set_processor_count(sysconf(_SC_NPROCESSORS_CONF));
382 if (processor_count() == 1) {
383 pid_t pid = os::Bsd::gettid();
384 char fname[32];
385 jio_snprintf(fname, sizeof(fname), "/proc/%d", pid);
386 FILE *fp = fopen(fname, "r");
387 if (fp == NULL) {
388 unsafe_chroot_detected = true;
389 } else {
390 fclose(fp);
391 }
392 }
393 _physical_memory = (julong)sysconf(_SC_PHYS_PAGES) * (julong)sysconf(_SC_PAGESIZE);
394 assert(processor_count() > 0, "bsd error");
395 }
396 #endif
397 299
398 #ifdef __APPLE__ 300 #ifdef __APPLE__
399 static const char *get_home() { 301 static const char *get_home() {
400 const char *home_dir = ::getenv("HOME"); 302 const char *home_dir = ::getenv("HOME");
401 if ((home_dir == NULL) || (*home_dir == '\0')) { 303 if ((home_dir == NULL) || (*home_dir == '\0')) {
748 pthread_sigmask(SIG_BLOCK, vm_signals(), NULL); 650 pthread_sigmask(SIG_BLOCK, vm_signals(), NULL);
749 } 651 }
750 } 652 }
751 } 653 }
752 654
753 #ifndef _ALLBSD_SOURCE
754 //////////////////////////////////////////////////////////////////////////////
755 // detecting pthread library
756
757 void os::Bsd::libpthread_init() {
758 // Save glibc and pthread version strings. Note that _CS_GNU_LIBC_VERSION
759 // and _CS_GNU_LIBPTHREAD_VERSION are supported in glibc >= 2.3.2. Use a
760 // generic name for earlier versions.
761 // Define macros here so we can build HotSpot on old systems.
762 # ifndef _CS_GNU_LIBC_VERSION
763 # define _CS_GNU_LIBC_VERSION 2
764 # endif
765 # ifndef _CS_GNU_LIBPTHREAD_VERSION
766 # define _CS_GNU_LIBPTHREAD_VERSION 3
767 # endif
768
769 size_t n = confstr(_CS_GNU_LIBC_VERSION, NULL, 0);
770 if (n > 0) {
771 char *str = (char *)malloc(n);
772 confstr(_CS_GNU_LIBC_VERSION, str, n);
773 os::Bsd::set_glibc_version(str);
774 } else {
775 // _CS_GNU_LIBC_VERSION is not supported, try gnu_get_libc_version()
776 static char _gnu_libc_version[32];
777 jio_snprintf(_gnu_libc_version, sizeof(_gnu_libc_version),
778 "glibc %s %s", gnu_get_libc_version(), gnu_get_libc_release());
779 os::Bsd::set_glibc_version(_gnu_libc_version);
780 }
781
782 n = confstr(_CS_GNU_LIBPTHREAD_VERSION, NULL, 0);
783 if (n > 0) {
784 char *str = (char *)malloc(n);
785 confstr(_CS_GNU_LIBPTHREAD_VERSION, str, n);
786 // Vanilla RH-9 (glibc 2.3.2) has a bug that confstr() always tells
787 // us "NPTL-0.29" even we are running with BsdThreads. Check if this
788 // is the case. BsdThreads has a hard limit on max number of threads.
789 // So sysconf(_SC_THREAD_THREADS_MAX) will return a positive value.
790 // On the other hand, NPTL does not have such a limit, sysconf()
791 // will return -1 and errno is not changed. Check if it is really NPTL.
792 if (strcmp(os::Bsd::glibc_version(), "glibc 2.3.2") == 0 &&
793 strstr(str, "NPTL") &&
794 sysconf(_SC_THREAD_THREADS_MAX) > 0) {
795 free(str);
796 os::Bsd::set_libpthread_version("bsdthreads");
797 } else {
798 os::Bsd::set_libpthread_version(str);
799 }
800 } else {
801 // glibc before 2.3.2 only has BsdThreads.
802 os::Bsd::set_libpthread_version("bsdthreads");
803 }
804
805 if (strstr(libpthread_version(), "NPTL")) {
806 os::Bsd::set_is_NPTL();
807 } else {
808 os::Bsd::set_is_BsdThreads();
809 }
810
811 // BsdThreads have two flavors: floating-stack mode, which allows variable
812 // stack size; and fixed-stack mode. NPTL is always floating-stack.
813 if (os::Bsd::is_NPTL() || os::Bsd::supports_variable_stack_size()) {
814 os::Bsd::set_is_floating_stack();
815 }
816 }
817
818 /////////////////////////////////////////////////////////////////////////////
819 // thread stack
820
821 // Force Bsd kernel to expand current thread stack. If "bottom" is close
822 // to the stack guard, caller should block all signals.
823 //
824 // MAP_GROWSDOWN:
825 // A special mmap() flag that is used to implement thread stacks. It tells
826 // kernel that the memory region should extend downwards when needed. This
827 // allows early versions of BsdThreads to only mmap the first few pages
828 // when creating a new thread. Bsd kernel will automatically expand thread
829 // stack as needed (on page faults).
830 //
831 // However, because the memory region of a MAP_GROWSDOWN stack can grow on
832 // demand, if a page fault happens outside an already mapped MAP_GROWSDOWN
833 // region, it's hard to tell if the fault is due to a legitimate stack
834 // access or because of reading/writing non-exist memory (e.g. buffer
835 // overrun). As a rule, if the fault happens below current stack pointer,
836 // Bsd kernel does not expand stack, instead a SIGSEGV is sent to the
837 // application (see Bsd kernel fault.c).
838 //
839 // This Bsd feature can cause SIGSEGV when VM bangs thread stack for
840 // stack overflow detection.
841 //
842 // Newer version of BsdThreads (since glibc-2.2, or, RH-7.x) and NPTL do
843 // not use this flag. However, the stack of initial thread is not created
844 // by pthread, it is still MAP_GROWSDOWN. Also it's possible (though
845 // unlikely) that user code can create a thread with MAP_GROWSDOWN stack
846 // and then attach the thread to JVM.
847 //
848 // To get around the problem and allow stack banging on Bsd, we need to
849 // manually expand thread stack after receiving the SIGSEGV.
850 //
851 // There are two ways to expand thread stack to address "bottom", we used
852 // both of them in JVM before 1.5:
853 // 1. adjust stack pointer first so that it is below "bottom", and then
854 // touch "bottom"
855 // 2. mmap() the page in question
856 //
857 // Now alternate signal stack is gone, it's harder to use 2. For instance,
858 // if current sp is already near the lower end of page 101, and we need to
859 // call mmap() to map page 100, it is possible that part of the mmap() frame
860 // will be placed in page 100. When page 100 is mapped, it is zero-filled.
861 // That will destroy the mmap() frame and cause VM to crash.
862 //
863 // The following code works by adjusting sp first, then accessing the "bottom"
864 // page to force a page fault. Bsd kernel will then automatically expand the
865 // stack mapping.
866 //
867 // _expand_stack_to() assumes its frame size is less than page size, which
868 // should always be true if the function is not inlined.
869
870 #if __GNUC__ < 3 // gcc 2.x does not support noinline attribute
871 #define NOINLINE
872 #else
873 #define NOINLINE __attribute__ ((noinline))
874 #endif
875
876 static void _expand_stack_to(address bottom) NOINLINE;
877
878 static void _expand_stack_to(address bottom) {
879 address sp;
880 size_t size;
881 volatile char *p;
882
883 // Adjust bottom to point to the largest address within the same page, it
884 // gives us a one-page buffer if alloca() allocates slightly more memory.
885 bottom = (address)align_size_down((uintptr_t)bottom, os::Bsd::page_size());
886 bottom += os::Bsd::page_size() - 1;
887
888 // sp might be slightly above current stack pointer; if that's the case, we
889 // will alloca() a little more space than necessary, which is OK. Don't use
890 // os::current_stack_pointer(), as its result can be slightly below current
891 // stack pointer, causing us to not alloca enough to reach "bottom".
892 sp = (address)&sp;
893
894 if (sp > bottom) {
895 size = sp - bottom;
896 p = (volatile char *)alloca(size);
897 assert(p != NULL && p <= (volatile char *)bottom, "alloca problem?");
898 p[0] = '\0';
899 }
900 }
901
902 bool os::Bsd::manually_expand_stack(JavaThread * t, address addr) {
903 assert(t!=NULL, "just checking");
904 assert(t->osthread()->expanding_stack(), "expand should be set");
905 assert(t->stack_base() != NULL, "stack_base was not initialized");
906
907 if (addr < t->stack_base() && addr >= t->stack_yellow_zone_base()) {
908 sigset_t mask_all, old_sigset;
909 sigfillset(&mask_all);
910 pthread_sigmask(SIG_SETMASK, &mask_all, &old_sigset);
911 _expand_stack_to(addr);
912 pthread_sigmask(SIG_SETMASK, &old_sigset, NULL);
913 return true;
914 }
915 return false;
916 }
917 #endif
918 655
919 ////////////////////////////////////////////////////////////////////////////// 656 //////////////////////////////////////////////////////////////////////////////
920 // create new thread 657 // create new thread
921 658
922 static address highest_vm_reserved_address(); 659 static address highest_vm_reserved_address();
923 660
924 // check if it's safe to start a new thread 661 // check if it's safe to start a new thread
925 static bool _thread_safety_check(Thread* thread) { 662 static bool _thread_safety_check(Thread* thread) {
926 #ifdef _ALLBSD_SOURCE 663 return true;
927 return true;
928 #else
929 if (os::Bsd::is_BsdThreads() && !os::Bsd::is_floating_stack()) {
930 // Fixed stack BsdThreads (SuSE Bsd/x86, and some versions of Redhat)
931 // Heap is mmap'ed at lower end of memory space. Thread stacks are
932 // allocated (MAP_FIXED) from high address space. Every thread stack
933 // occupies a fixed size slot (usually 2Mbytes, but user can change
934 // it to other values if they rebuild BsdThreads).
935 //
936 // Problem with MAP_FIXED is that mmap() can still succeed even part of
937 // the memory region has already been mmap'ed. That means if we have too
938 // many threads and/or very large heap, eventually thread stack will
939 // collide with heap.
940 //
941 // Here we try to prevent heap/stack collision by comparing current
942 // stack bottom with the highest address that has been mmap'ed by JVM
943 // plus a safety margin for memory maps created by native code.
944 //
945 // This feature can be disabled by setting ThreadSafetyMargin to 0
946 //
947 if (ThreadSafetyMargin > 0) {
948 address stack_bottom = os::current_stack_base() - os::current_stack_size();
949
950 // not safe if our stack extends below the safety margin
951 return stack_bottom - ThreadSafetyMargin >= highest_vm_reserved_address();
952 } else {
953 return true;
954 }
955 } else {
956 // Floating stack BsdThreads or NPTL:
957 // Unlike fixed stack BsdThreads, thread stacks are not MAP_FIXED. When
958 // there's not enough space left, pthread_create() will fail. If we come
959 // here, that means enough space has been reserved for stack.
960 return true;
961 }
962 #endif
963 } 664 }
964 665
965 #ifdef __APPLE__ 666 #ifdef __APPLE__
966 // library handle for calling objc_registerThreadWithCollector() 667 // library handle for calling objc_registerThreadWithCollector()
967 // without static linking to the libobjc library 668 // without static linking to the libobjc library
995 osthread->set_state(ZOMBIE); 696 osthread->set_state(ZOMBIE);
996 sync->notify_all(); 697 sync->notify_all();
997 return NULL; 698 return NULL;
998 } 699 }
999 700
1000 #ifdef _ALLBSD_SOURCE
1001 #ifdef __APPLE__ 701 #ifdef __APPLE__
1002 // thread_id is mach thread on macos 702 // thread_id is mach thread on macos
1003 osthread->set_thread_id(::mach_thread_self()); 703 osthread->set_thread_id(::mach_thread_self());
1004 #else 704 #else
1005 // thread_id is pthread_id on BSD 705 // thread_id is pthread_id on BSD
1006 osthread->set_thread_id(::pthread_self()); 706 osthread->set_thread_id(::pthread_self());
1007 #endif
1008 #else
1009 // thread_id is kernel thread id (similar to Solaris LWP id)
1010 osthread->set_thread_id(os::Bsd::gettid());
1011
1012 if (UseNUMA) {
1013 int lgrp_id = os::numa_get_group_id();
1014 if (lgrp_id != -1) {
1015 thread->set_lgrp_id(lgrp_id);
1016 }
1017 }
1018 #endif 707 #endif
1019 // initialize signal mask for this thread 708 // initialize signal mask for this thread
1020 os::Bsd::hotspot_sigmask(thread); 709 os::Bsd::hotspot_sigmask(thread);
1021 710
1022 // initialize floating point control register 711 // initialize floating point control register
1103 pthread_attr_setstacksize(&attr, stack_size); 792 pthread_attr_setstacksize(&attr, stack_size);
1104 } else { 793 } else {
1105 // let pthread_create() pick the default value. 794 // let pthread_create() pick the default value.
1106 } 795 }
1107 796
1108 #ifndef _ALLBSD_SOURCE
1109 // glibc guard page
1110 pthread_attr_setguardsize(&attr, os::Bsd::default_guard_size(thr_type));
1111 #endif
1112
1113 ThreadState state; 797 ThreadState state;
1114 798
1115 { 799 {
1116
1117 #ifndef _ALLBSD_SOURCE
1118 // Serialize thread creation if we are running with fixed stack BsdThreads
1119 bool lock = os::Bsd::is_BsdThreads() && !os::Bsd::is_floating_stack();
1120 if (lock) {
1121 os::Bsd::createThread_lock()->lock_without_safepoint_check();
1122 }
1123 #endif
1124
1125 pthread_t tid; 800 pthread_t tid;
1126 int ret = pthread_create(&tid, &attr, (void* (*)(void*)) java_start, thread); 801 int ret = pthread_create(&tid, &attr, (void* (*)(void*)) java_start, thread);
1127 802
1128 pthread_attr_destroy(&attr); 803 pthread_attr_destroy(&attr);
1129 804
1132 perror("pthread_create()"); 807 perror("pthread_create()");
1133 } 808 }
1134 // Need to clean up stuff we've allocated so far 809 // Need to clean up stuff we've allocated so far
1135 thread->set_osthread(NULL); 810 thread->set_osthread(NULL);
1136 delete osthread; 811 delete osthread;
1137 #ifndef _ALLBSD_SOURCE
1138 if (lock) os::Bsd::createThread_lock()->unlock();
1139 #endif
1140 return false; 812 return false;
1141 } 813 }
1142 814
1143 // Store pthread info into the OSThread 815 // Store pthread info into the OSThread
1144 osthread->set_pthread_id(tid); 816 osthread->set_pthread_id(tid);
1150 while ((state = osthread->get_state()) == ALLOCATED) { 822 while ((state = osthread->get_state()) == ALLOCATED) {
1151 sync_with_child->wait(Mutex::_no_safepoint_check_flag); 823 sync_with_child->wait(Mutex::_no_safepoint_check_flag);
1152 } 824 }
1153 } 825 }
1154 826
1155 #ifndef _ALLBSD_SOURCE
1156 if (lock) {
1157 os::Bsd::createThread_lock()->unlock();
1158 }
1159 #endif
1160 } 827 }
1161 828
1162 // Aborted due to thread limit being reached 829 // Aborted due to thread limit being reached
1163 if (state == ZOMBIE) { 830 if (state == ZOMBIE) {
1164 thread->set_osthread(NULL); 831 thread->set_osthread(NULL);
1192 if (osthread == NULL) { 859 if (osthread == NULL) {
1193 return false; 860 return false;
1194 } 861 }
1195 862
1196 // Store pthread info into the OSThread 863 // Store pthread info into the OSThread
1197 #ifdef _ALLBSD_SOURCE
1198 #ifdef __APPLE__ 864 #ifdef __APPLE__
1199 osthread->set_thread_id(::mach_thread_self()); 865 osthread->set_thread_id(::mach_thread_self());
1200 #else 866 #else
1201 osthread->set_thread_id(::pthread_self()); 867 osthread->set_thread_id(::pthread_self());
1202 #endif 868 #endif
1203 #else
1204 osthread->set_thread_id(os::Bsd::gettid());
1205 #endif
1206 osthread->set_pthread_id(::pthread_self()); 869 osthread->set_pthread_id(::pthread_self());
1207 870
1208 // initialize floating point control register 871 // initialize floating point control register
1209 os::Bsd::init_thread_fpu_state(); 872 os::Bsd::init_thread_fpu_state();
1210 873
1211 // Initial thread state is RUNNABLE 874 // Initial thread state is RUNNABLE
1212 osthread->set_state(RUNNABLE); 875 osthread->set_state(RUNNABLE);
1213 876
1214 thread->set_osthread(osthread); 877 thread->set_osthread(osthread);
1215
1216 #ifndef _ALLBSD_SOURCE
1217 if (UseNUMA) {
1218 int lgrp_id = os::numa_get_group_id();
1219 if (lgrp_id != -1) {
1220 thread->set_lgrp_id(lgrp_id);
1221 }
1222 }
1223
1224 if (os::Bsd::is_initial_thread()) {
1225 // If current thread is initial thread, its stack is mapped on demand,
1226 // see notes about MAP_GROWSDOWN. Here we try to force kernel to map
1227 // the entire stack region to avoid SEGV in stack banging.
1228 // It is also useful to get around the heap-stack-gap problem on SuSE
1229 // kernel (see 4821821 for details). We first expand stack to the top
1230 // of yellow zone, then enable stack yellow zone (order is significant,
1231 // enabling yellow zone first will crash JVM on SuSE Bsd), so there
1232 // is no gap between the last two virtual memory regions.
1233
1234 JavaThread *jt = (JavaThread *)thread;
1235 address addr = jt->stack_yellow_zone_base();
1236 assert(addr != NULL, "initialization problem?");
1237 assert(jt->stack_available(addr) > 0, "stack guard should not be enabled");
1238
1239 osthread->set_expanding_stack();
1240 os::Bsd::manually_expand_stack(jt, addr);
1241 osthread->clear_expanding_stack();
1242 }
1243 #endif
1244 878
1245 // initialize signal mask for this thread 879 // initialize signal mask for this thread
1246 // and save the caller's signal mask 880 // and save the caller's signal mask
1247 os::Bsd::hotspot_sigmask(thread); 881 os::Bsd::hotspot_sigmask(thread);
1248 882
1294 928
1295 extern "C" Thread* get_thread() { 929 extern "C" Thread* get_thread() {
1296 return ThreadLocalStorage::thread(); 930 return ThreadLocalStorage::thread();
1297 } 931 }
1298 932
1299 //////////////////////////////////////////////////////////////////////////////
1300 // initial thread
1301
1302 #ifndef _ALLBSD_SOURCE
1303 // Check if current thread is the initial thread, similar to Solaris thr_main.
1304 bool os::Bsd::is_initial_thread(void) {
1305 char dummy;
1306 // If called before init complete, thread stack bottom will be null.
1307 // Can be called if fatal error occurs before initialization.
1308 if (initial_thread_stack_bottom() == NULL) return false;
1309 assert(initial_thread_stack_bottom() != NULL &&
1310 initial_thread_stack_size() != 0,
1311 "os::init did not locate initial thread's stack region");
1312 if ((address)&dummy >= initial_thread_stack_bottom() &&
1313 (address)&dummy < initial_thread_stack_bottom() + initial_thread_stack_size())
1314 return true;
1315 else return false;
1316 }
1317
1318 // Find the virtual memory area that contains addr
1319 static bool find_vma(address addr, address* vma_low, address* vma_high) {
1320 FILE *fp = fopen("/proc/self/maps", "r");
1321 if (fp) {
1322 address low, high;
1323 while (!feof(fp)) {
1324 if (fscanf(fp, "%p-%p", &low, &high) == 2) {
1325 if (low <= addr && addr < high) {
1326 if (vma_low) *vma_low = low;
1327 if (vma_high) *vma_high = high;
1328 fclose (fp);
1329 return true;
1330 }
1331 }
1332 for (;;) {
1333 int ch = fgetc(fp);
1334 if (ch == EOF || ch == (int)'\n') break;
1335 }
1336 }
1337 fclose(fp);
1338 }
1339 return false;
1340 }
1341
1342 // Locate initial thread stack. This special handling of initial thread stack
1343 // is needed because pthread_getattr_np() on most (all?) Bsd distros returns
1344 // bogus value for initial thread.
1345 void os::Bsd::capture_initial_stack(size_t max_size) {
1346 // stack size is the easy part, get it from RLIMIT_STACK
1347 size_t stack_size;
1348 struct rlimit rlim;
1349 getrlimit(RLIMIT_STACK, &rlim);
1350 stack_size = rlim.rlim_cur;
1351
1352 // 6308388: a bug in ld.so will relocate its own .data section to the
1353 // lower end of primordial stack; reduce ulimit -s value a little bit
1354 // so we won't install guard page on ld.so's data section.
1355 stack_size -= 2 * page_size();
1356
1357 // 4441425: avoid crash with "unlimited" stack size on SuSE 7.1 or Redhat
1358 // 7.1, in both cases we will get 2G in return value.
1359 // 4466587: glibc 2.2.x compiled w/o "--enable-kernel=2.4.0" (RH 7.0,
1360 // SuSE 7.2, Debian) can not handle alternate signal stack correctly
1361 // for initial thread if its stack size exceeds 6M. Cap it at 2M,
1362 // in case other parts in glibc still assumes 2M max stack size.
1363 // FIXME: alt signal stack is gone, maybe we can relax this constraint?
1364 #ifndef IA64
1365 if (stack_size > 2 * K * K) stack_size = 2 * K * K;
1366 #else
1367 // Problem still exists RH7.2 (IA64 anyway) but 2MB is a little small
1368 if (stack_size > 4 * K * K) stack_size = 4 * K * K;
1369 #endif
1370
1371 // Try to figure out where the stack base (top) is. This is harder.
1372 //
1373 // When an application is started, glibc saves the initial stack pointer in
1374 // a global variable "__libc_stack_end", which is then used by system
1375 // libraries. __libc_stack_end should be pretty close to stack top. The
1376 // variable is available since the very early days. However, because it is
1377 // a private interface, it could disappear in the future.
1378 //
1379 // Bsd kernel saves start_stack information in /proc/<pid>/stat. Similar
1380 // to __libc_stack_end, it is very close to stack top, but isn't the real
1381 // stack top. Note that /proc may not exist if VM is running as a chroot
1382 // program, so reading /proc/<pid>/stat could fail. Also the contents of
1383 // /proc/<pid>/stat could change in the future (though unlikely).
1384 //
1385 // We try __libc_stack_end first. If that doesn't work, look for
1386 // /proc/<pid>/stat. If neither of them works, we use current stack pointer
1387 // as a hint, which should work well in most cases.
1388
1389 uintptr_t stack_start;
1390
1391 // try __libc_stack_end first
1392 uintptr_t *p = (uintptr_t *)dlsym(RTLD_DEFAULT, "__libc_stack_end");
1393 if (p && *p) {
1394 stack_start = *p;
1395 } else {
1396 // see if we can get the start_stack field from /proc/self/stat
1397 FILE *fp;
1398 int pid;
1399 char state;
1400 int ppid;
1401 int pgrp;
1402 int session;
1403 int nr;
1404 int tpgrp;
1405 unsigned long flags;
1406 unsigned long minflt;
1407 unsigned long cminflt;
1408 unsigned long majflt;
1409 unsigned long cmajflt;
1410 unsigned long utime;
1411 unsigned long stime;
1412 long cutime;
1413 long cstime;
1414 long prio;
1415 long nice;
1416 long junk;
1417 long it_real;
1418 uintptr_t start;
1419 uintptr_t vsize;
1420 intptr_t rss;
1421 uintptr_t rsslim;
1422 uintptr_t scodes;
1423 uintptr_t ecode;
1424 int i;
1425
1426 // Figure what the primordial thread stack base is. Code is inspired
1427 // by email from Hans Boehm. /proc/self/stat begins with current pid,
1428 // followed by command name surrounded by parentheses, state, etc.
1429 char stat[2048];
1430 int statlen;
1431
1432 fp = fopen("/proc/self/stat", "r");
1433 if (fp) {
1434 statlen = fread(stat, 1, 2047, fp);
1435 stat[statlen] = '\0';
1436 fclose(fp);
1437
1438 // Skip pid and the command string. Note that we could be dealing with
1439 // weird command names, e.g. user could decide to rename java launcher
1440 // to "java 1.4.2 :)", then the stat file would look like
1441 // 1234 (java 1.4.2 :)) R ... ...
1442 // We don't really need to know the command string, just find the last
1443 // occurrence of ")" and then start parsing from there. See bug 4726580.
1444 char * s = strrchr(stat, ')');
1445
1446 i = 0;
1447 if (s) {
1448 // Skip blank chars
1449 do s++; while (isspace(*s));
1450
1451 #define _UFM UINTX_FORMAT
1452 #define _DFM INTX_FORMAT
1453
1454 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 */
1455 /* 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 */
1456 i = sscanf(s, "%c %d %d %d %d %d %lu %lu %lu %lu %lu %lu %lu %ld %ld %ld %ld %ld %ld " _UFM _UFM _DFM _UFM _UFM _UFM _UFM,
1457 &state, /* 3 %c */
1458 &ppid, /* 4 %d */
1459 &pgrp, /* 5 %d */
1460 &session, /* 6 %d */
1461 &nr, /* 7 %d */
1462 &tpgrp, /* 8 %d */
1463 &flags, /* 9 %lu */
1464 &minflt, /* 10 %lu */
1465 &cminflt, /* 11 %lu */
1466 &majflt, /* 12 %lu */
1467 &cmajflt, /* 13 %lu */
1468 &utime, /* 14 %lu */
1469 &stime, /* 15 %lu */
1470 &cutime, /* 16 %ld */
1471 &cstime, /* 17 %ld */
1472 &prio, /* 18 %ld */
1473 &nice, /* 19 %ld */
1474 &junk, /* 20 %ld */
1475 &it_real, /* 21 %ld */
1476 &start, /* 22 UINTX_FORMAT */
1477 &vsize, /* 23 UINTX_FORMAT */
1478 &rss, /* 24 INTX_FORMAT */
1479 &rsslim, /* 25 UINTX_FORMAT */
1480 &scodes, /* 26 UINTX_FORMAT */
1481 &ecode, /* 27 UINTX_FORMAT */
1482 &stack_start); /* 28 UINTX_FORMAT */
1483 }
1484
1485 #undef _UFM
1486 #undef _DFM
1487
1488 if (i != 28 - 2) {
1489 assert(false, "Bad conversion from /proc/self/stat");
1490 // product mode - assume we are the initial thread, good luck in the
1491 // embedded case.
1492 warning("Can't detect initial thread stack location - bad conversion");
1493 stack_start = (uintptr_t) &rlim;
1494 }
1495 } else {
1496 // For some reason we can't open /proc/self/stat (for example, running on
1497 // FreeBSD with a Bsd emulator, or inside chroot), this should work for
1498 // most cases, so don't abort:
1499 warning("Can't detect initial thread stack location - no /proc/self/stat");
1500 stack_start = (uintptr_t) &rlim;
1501 }
1502 }
1503
1504 // Now we have a pointer (stack_start) very close to the stack top, the
1505 // next thing to do is to figure out the exact location of stack top. We
1506 // can find out the virtual memory area that contains stack_start by
1507 // reading /proc/self/maps, it should be the last vma in /proc/self/maps,
1508 // and its upper limit is the real stack top. (again, this would fail if
1509 // running inside chroot, because /proc may not exist.)
1510
1511 uintptr_t stack_top;
1512 address low, high;
1513 if (find_vma((address)stack_start, &low, &high)) {
1514 // success, "high" is the true stack top. (ignore "low", because initial
1515 // thread stack grows on demand, its real bottom is high - RLIMIT_STACK.)
1516 stack_top = (uintptr_t)high;
1517 } else {
1518 // failed, likely because /proc/self/maps does not exist
1519 warning("Can't detect initial thread stack location - find_vma failed");
1520 // best effort: stack_start is normally within a few pages below the real
1521 // stack top, use it as stack top, and reduce stack size so we won't put
1522 // guard page outside stack.
1523 stack_top = stack_start;
1524 stack_size -= 16 * page_size();
1525 }
1526
1527 // stack_top could be partially down the page so align it
1528 stack_top = align_size_up(stack_top, page_size());
1529
1530 if (max_size && stack_size > max_size) {
1531 _initial_thread_stack_size = max_size;
1532 } else {
1533 _initial_thread_stack_size = stack_size;
1534 }
1535
1536 _initial_thread_stack_size = align_size_down(_initial_thread_stack_size, page_size());
1537 _initial_thread_stack_bottom = (address)stack_top - _initial_thread_stack_size;
1538 }
1539 #endif
1540 933
1541 //////////////////////////////////////////////////////////////////////////////// 934 ////////////////////////////////////////////////////////////////////////////////
1542 // time support 935 // time support
1543 936
1544 // Time since start-up in seconds to a fine granularity. 937 // Time since start-up in seconds to a fine granularity.
1580 973
1581 #ifdef __APPLE__ 974 #ifdef __APPLE__
1582 void os::Bsd::clock_init() { 975 void os::Bsd::clock_init() {
1583 // XXXDARWIN: Investigate replacement monotonic clock 976 // XXXDARWIN: Investigate replacement monotonic clock
1584 } 977 }
1585 #elif defined(_ALLBSD_SOURCE) 978 #else
1586 void os::Bsd::clock_init() { 979 void os::Bsd::clock_init() {
1587 struct timespec res; 980 struct timespec res;
1588 struct timespec tp; 981 struct timespec tp;
1589 if (::clock_getres(CLOCK_MONOTONIC, &res) == 0 && 982 if (::clock_getres(CLOCK_MONOTONIC, &res) == 0 &&
1590 ::clock_gettime(CLOCK_MONOTONIC, &tp) == 0) { 983 ::clock_gettime(CLOCK_MONOTONIC, &tp) == 0) {
1591 // yes, monotonic clock is supported 984 // yes, monotonic clock is supported
1592 _clock_gettime = ::clock_gettime; 985 _clock_gettime = ::clock_gettime;
1593 } 986 }
1594 } 987 }
1595 #else 988 #endif
1596 void os::Bsd::clock_init() { 989
1597 // we do dlopen's in this particular order due to bug in bsd
1598 // dynamical loader (see 6348968) leading to crash on exit
1599 void* handle = dlopen("librt.so.1", RTLD_LAZY);
1600 if (handle == NULL) {
1601 handle = dlopen("librt.so", RTLD_LAZY);
1602 }
1603
1604 if (handle) {
1605 int (*clock_getres_func)(clockid_t, struct timespec*) =
1606 (int(*)(clockid_t, struct timespec*))dlsym(handle, "clock_getres");
1607 int (*clock_gettime_func)(clockid_t, struct timespec*) =
1608 (int(*)(clockid_t, struct timespec*))dlsym(handle, "clock_gettime");
1609 if (clock_getres_func && clock_gettime_func) {
1610 // See if monotonic clock is supported by the kernel. Note that some
1611 // early implementations simply return kernel jiffies (updated every
1612 // 1/100 or 1/1000 second). It would be bad to use such a low res clock
1613 // for nano time (though the monotonic property is still nice to have).
1614 // It's fixed in newer kernels, however clock_getres() still returns
1615 // 1/HZ. We check if clock_getres() works, but will ignore its reported
1616 // resolution for now. Hopefully as people move to new kernels, this
1617 // won't be a problem.
1618 struct timespec res;
1619 struct timespec tp;
1620 if (clock_getres_func (CLOCK_MONOTONIC, &res) == 0 &&
1621 clock_gettime_func(CLOCK_MONOTONIC, &tp) == 0) {
1622 // yes, monotonic clock is supported
1623 _clock_gettime = clock_gettime_func;
1624 } else {
1625 // close librt if there is no monotonic clock
1626 dlclose(handle);
1627 }
1628 }
1629 }
1630 }
1631 #endif
1632
1633 #ifndef _ALLBSD_SOURCE
1634 #ifndef SYS_clock_getres
1635
1636 #if defined(IA32) || defined(AMD64)
1637 #define SYS_clock_getres IA32_ONLY(266) AMD64_ONLY(229)
1638 #define sys_clock_getres(x,y) ::syscall(SYS_clock_getres, x, y)
1639 #else
1640 #warning "SYS_clock_getres not defined for this platform, disabling fast_thread_cpu_time"
1641 #define sys_clock_getres(x,y) -1
1642 #endif
1643
1644 #else
1645 #define sys_clock_getres(x,y) ::syscall(SYS_clock_getres, x, y)
1646 #endif
1647
1648 void os::Bsd::fast_thread_clock_init() {
1649 if (!UseBsdPosixThreadCPUClocks) {
1650 return;
1651 }
1652 clockid_t clockid;
1653 struct timespec tp;
1654 int (*pthread_getcpuclockid_func)(pthread_t, clockid_t *) =
1655 (int(*)(pthread_t, clockid_t *)) dlsym(RTLD_DEFAULT, "pthread_getcpuclockid");
1656
1657 // Switch to using fast clocks for thread cpu time if
1658 // the sys_clock_getres() returns 0 error code.
1659 // Note, that some kernels may support the current thread
1660 // clock (CLOCK_THREAD_CPUTIME_ID) but not the clocks
1661 // returned by the pthread_getcpuclockid().
1662 // If the fast Posix clocks are supported then the sys_clock_getres()
1663 // must return at least tp.tv_sec == 0 which means a resolution
1664 // better than 1 sec. This is extra check for reliability.
1665
1666 if(pthread_getcpuclockid_func &&
1667 pthread_getcpuclockid_func(_main_thread, &clockid) == 0 &&
1668 sys_clock_getres(clockid, &tp) == 0 && tp.tv_sec == 0) {
1669
1670 _supports_fast_thread_cpu_time = true;
1671 _pthread_getcpuclockid = pthread_getcpuclockid_func;
1672 }
1673 }
1674 #endif
1675 990
1676 jlong os::javaTimeNanos() { 991 jlong os::javaTimeNanos() {
1677 if (Bsd::supports_monotonic_clock()) { 992 if (Bsd::supports_monotonic_clock()) {
1678 struct timespec tp; 993 struct timespec tp;
1679 int status = Bsd::clock_gettime(CLOCK_MONOTONIC, &tp); 994 int status = Bsd::clock_gettime(CLOCK_MONOTONIC, &tp);
1982 if (buf != NULL) buf[0] = '\0'; 1297 if (buf != NULL) buf[0] = '\0';
1983 if (offset != NULL) *offset = -1; 1298 if (offset != NULL) *offset = -1;
1984 return false; 1299 return false;
1985 } 1300 }
1986 1301
1987 #ifdef _ALLBSD_SOURCE
1988 // ported from solaris version 1302 // ported from solaris version
1989 bool os::dll_address_to_library_name(address addr, char* buf, 1303 bool os::dll_address_to_library_name(address addr, char* buf,
1990 int buflen, int* offset) { 1304 int buflen, int* offset) {
1991 Dl_info dlinfo; 1305 Dl_info dlinfo;
1992 1306
1998 if (buf) buf[0] = '\0'; 1312 if (buf) buf[0] = '\0';
1999 if (offset) *offset = -1; 1313 if (offset) *offset = -1;
2000 return false; 1314 return false;
2001 } 1315 }
2002 } 1316 }
2003 #else 1317
2004 struct _address_to_library_name { 1318 // Loads .dll/.so and
2005 address addr; // input : memory address 1319 // in case of error it checks if .dll/.so was built for the
2006 size_t buflen; // size of fname 1320 // same architecture as Hotspot is running on
2007 char* fname; // output: library name
2008 address base; // library base addr
2009 };
2010
2011 static int address_to_library_name_callback(struct dl_phdr_info *info,
2012 size_t size, void *data) {
2013 int i;
2014 bool found = false;
2015 address libbase = NULL;
2016 struct _address_to_library_name * d = (struct _address_to_library_name *)data;
2017
2018 // iterate through all loadable segments
2019 for (i = 0; i < info->dlpi_phnum; i++) {
2020 address segbase = (address)(info->dlpi_addr + info->dlpi_phdr[i].p_vaddr);
2021 if (info->dlpi_phdr[i].p_type == PT_LOAD) {
2022 // base address of a library is the lowest address of its loaded
2023 // segments.
2024 if (libbase == NULL || libbase > segbase) {
2025 libbase = segbase;
2026 }
2027 // see if 'addr' is within current segment
2028 if (segbase <= d->addr &&
2029 d->addr < segbase + info->dlpi_phdr[i].p_memsz) {
2030 found = true;
2031 }
2032 }
2033 }
2034
2035 // dlpi_name is NULL or empty if the ELF file is executable, return 0
2036 // so dll_address_to_library_name() can fall through to use dladdr() which
2037 // can figure out executable name from argv[0].
2038 if (found && info->dlpi_name && info->dlpi_name[0]) {
2039 d->base = libbase;
2040 if (d->fname) {
2041 jio_snprintf(d->fname, d->buflen, "%s", info->dlpi_name);
2042 }
2043 return 1;
2044 }
2045 return 0;
2046 }
2047
2048 bool os::dll_address_to_library_name(address addr, char* buf,
2049 int buflen, int* offset) {
2050 Dl_info dlinfo;
2051 struct _address_to_library_name data;
2052
2053 // There is a bug in old glibc dladdr() implementation that it could resolve
2054 // to wrong library name if the .so file has a base address != NULL. Here
2055 // we iterate through the program headers of all loaded libraries to find
2056 // out which library 'addr' really belongs to. This workaround can be
2057 // removed once the minimum requirement for glibc is moved to 2.3.x.
2058 data.addr = addr;
2059 data.fname = buf;
2060 data.buflen = buflen;
2061 data.base = NULL;
2062 int rslt = dl_iterate_phdr(address_to_library_name_callback, (void *)&data);
2063
2064 if (rslt) {
2065 // buf already contains library name
2066 if (offset) *offset = addr - data.base;
2067 return true;
2068 } else if (dladdr((void*)addr, &dlinfo)){
2069 if (buf) jio_snprintf(buf, buflen, "%s", dlinfo.dli_fname);
2070 if (offset) *offset = addr - (address)dlinfo.dli_fbase;
2071 return true;
2072 } else {
2073 if (buf) buf[0] = '\0';
2074 if (offset) *offset = -1;
2075 return false;
2076 }
2077 }
2078 #endif
2079
2080 // Loads .dll/.so and
2081 // in case of error it checks if .dll/.so was built for the
2082 // same architecture as Hotspot is running on
2083 1321
2084 #ifdef __APPLE__ 1322 #ifdef __APPLE__
2085 void * os::dll_load(const char *filename, char *ebuf, int ebuflen) { 1323 void * os::dll_load(const char *filename, char *ebuf, int ebuflen) {
2086 void * result= ::dlopen(filename, RTLD_LAZY); 1324 void * result= ::dlopen(filename, RTLD_LAZY);
2087 if (result != NULL) { 1325 if (result != NULL) {
2296 return true; 1534 return true;
2297 } 1535 }
2298 1536
2299 void os::print_dll_info(outputStream *st) { 1537 void os::print_dll_info(outputStream *st) {
2300 st->print_cr("Dynamic libraries:"); 1538 st->print_cr("Dynamic libraries:");
2301 #ifdef _ALLBSD_SOURCE
2302 #ifdef RTLD_DI_LINKMAP 1539 #ifdef RTLD_DI_LINKMAP
2303 Dl_info dli; 1540 Dl_info dli;
2304 void *handle; 1541 void *handle;
2305 Link_map *map; 1542 Link_map *map;
2306 Link_map *p; 1543 Link_map *p;
2340 st->print_cr(PTR_FORMAT " \t%s", slide, name); 1577 st->print_cr(PTR_FORMAT " \t%s", slide, name);
2341 } 1578 }
2342 #else 1579 #else
2343 st->print_cr("Error: Cannot print dynamic libraries."); 1580 st->print_cr("Error: Cannot print dynamic libraries.");
2344 #endif 1581 #endif
2345 #else
2346 char fname[32];
2347 pid_t pid = os::Bsd::gettid();
2348
2349 jio_snprintf(fname, sizeof(fname), "/proc/%d/maps", pid);
2350
2351 if (!_print_ascii_file(fname, st)) {
2352 st->print("Can not get library information for pid = %d\n", pid);
2353 }
2354 #endif
2355 } 1582 }
2356 1583
2357 void os::print_os_info_brief(outputStream* st) { 1584 void os::print_os_info_brief(outputStream* st) {
2358 st->print("Bsd"); 1585 st->print("Bsd");
2359 1586
2377 1604
2378 void os::print_memory_info(outputStream* st) { 1605 void os::print_memory_info(outputStream* st) {
2379 1606
2380 st->print("Memory:"); 1607 st->print("Memory:");
2381 st->print(" %dk page", os::vm_page_size()>>10); 1608 st->print(" %dk page", os::vm_page_size()>>10);
2382
2383 #ifndef _ALLBSD_SOURCE
2384 // values in struct sysinfo are "unsigned long"
2385 struct sysinfo si;
2386 sysinfo(&si);
2387 #endif
2388 1609
2389 st->print(", physical " UINT64_FORMAT "k", 1610 st->print(", physical " UINT64_FORMAT "k",
2390 os::physical_memory() >> 10); 1611 os::physical_memory() >> 10);
2391 st->print("(" UINT64_FORMAT "k free)", 1612 st->print("(" UINT64_FORMAT "k free)",
2392 os::available_memory() >> 10); 1613 os::available_memory() >> 10);
2393 #ifndef _ALLBSD_SOURCE
2394 st->print(", swap " UINT64_FORMAT "k",
2395 ((jlong)si.totalswap * si.mem_unit) >> 10);
2396 st->print("(" UINT64_FORMAT "k free)",
2397 ((jlong)si.freeswap * si.mem_unit) >> 10);
2398 #endif
2399 st->cr(); 1614 st->cr();
2400 1615
2401 // meminfo 1616 // meminfo
2402 st->print("\n/proc/meminfo:\n"); 1617 st->print("\n/proc/meminfo:\n");
2403 _print_ascii_file("/proc/meminfo", st); 1618 _print_ascii_file("/proc/meminfo", st);
2790 MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0); 2005 MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0);
2791 return res != (uintptr_t) MAP_FAILED; 2006 return res != (uintptr_t) MAP_FAILED;
2792 #endif 2007 #endif
2793 } 2008 }
2794 2009
2795 #ifndef _ALLBSD_SOURCE
2796 // Define MAP_HUGETLB here so we can build HotSpot on old systems.
2797 #ifndef MAP_HUGETLB
2798 #define MAP_HUGETLB 0x40000
2799 #endif
2800
2801 // Define MADV_HUGEPAGE here so we can build HotSpot on old systems.
2802 #ifndef MADV_HUGEPAGE
2803 #define MADV_HUGEPAGE 14
2804 #endif
2805 #endif
2806 2010
2807 bool os::pd_commit_memory(char* addr, size_t size, size_t alignment_hint, 2011 bool os::pd_commit_memory(char* addr, size_t size, size_t alignment_hint,
2808 bool exec) { 2012 bool exec) {
2809 #ifndef _ALLBSD_SOURCE
2810 if (UseHugeTLBFS && alignment_hint > (size_t)vm_page_size()) {
2811 int prot = exec ? PROT_READ|PROT_WRITE|PROT_EXEC : PROT_READ|PROT_WRITE;
2812 uintptr_t res =
2813 (uintptr_t) ::mmap(addr, size, prot,
2814 MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS|MAP_HUGETLB,
2815 -1, 0);
2816 return res != (uintptr_t) MAP_FAILED;
2817 }
2818 #endif
2819
2820 return commit_memory(addr, size, exec); 2013 return commit_memory(addr, size, exec);
2821 } 2014 }
2822 2015
2823 void os::pd_realign_memory(char *addr, size_t bytes, size_t alignment_hint) { 2016 void os::pd_realign_memory(char *addr, size_t bytes, size_t alignment_hint) {
2824 #ifndef _ALLBSD_SOURCE
2825 if (UseHugeTLBFS && alignment_hint > (size_t)vm_page_size()) {
2826 // We don't check the return value: madvise(MADV_HUGEPAGE) may not
2827 // be supported or the memory may already be backed by huge pages.
2828 ::madvise(addr, bytes, MADV_HUGEPAGE);
2829 }
2830 #endif
2831 } 2017 }
2832 2018
2833 void os::pd_free_memory(char *addr, size_t bytes, size_t alignment_hint) { 2019 void os::pd_free_memory(char *addr, size_t bytes, size_t alignment_hint) {
2834 ::madvise(addr, bytes, MADV_DONTNEED); 2020 ::madvise(addr, bytes, MADV_DONTNEED);
2835 } 2021 }
2864 2050
2865 char *os::scan_pages(char *start, char* end, page_info* page_expected, page_info* page_found) { 2051 char *os::scan_pages(char *start, char* end, page_info* page_expected, page_info* page_found) {
2866 return end; 2052 return end;
2867 } 2053 }
2868 2054
2869 #ifndef _ALLBSD_SOURCE
2870 // Something to do with the numa-aware allocator needs these symbols
2871 extern "C" JNIEXPORT void numa_warn(int number, char *where, ...) { }
2872 extern "C" JNIEXPORT void numa_error(char *where) { }
2873 extern "C" JNIEXPORT int fork1() { return fork(); }
2874
2875
2876 // If we are running with libnuma version > 2, then we should
2877 // be trying to use symbols with versions 1.1
2878 // If we are running with earlier version, which did not have symbol versions,
2879 // we should use the base version.
2880 void* os::Bsd::libnuma_dlsym(void* handle, const char *name) {
2881 void *f = dlvsym(handle, name, "libnuma_1.1");
2882 if (f == NULL) {
2883 f = dlsym(handle, name);
2884 }
2885 return f;
2886 }
2887
2888 bool os::Bsd::libnuma_init() {
2889 // sched_getcpu() should be in libc.
2890 set_sched_getcpu(CAST_TO_FN_PTR(sched_getcpu_func_t,
2891 dlsym(RTLD_DEFAULT, "sched_getcpu")));
2892
2893 if (sched_getcpu() != -1) { // Does it work?
2894 void *handle = dlopen("libnuma.so.1", RTLD_LAZY);
2895 if (handle != NULL) {
2896 set_numa_node_to_cpus(CAST_TO_FN_PTR(numa_node_to_cpus_func_t,
2897 libnuma_dlsym(handle, "numa_node_to_cpus")));
2898 set_numa_max_node(CAST_TO_FN_PTR(numa_max_node_func_t,
2899 libnuma_dlsym(handle, "numa_max_node")));
2900 set_numa_available(CAST_TO_FN_PTR(numa_available_func_t,
2901 libnuma_dlsym(handle, "numa_available")));
2902 set_numa_tonode_memory(CAST_TO_FN_PTR(numa_tonode_memory_func_t,
2903 libnuma_dlsym(handle, "numa_tonode_memory")));
2904 set_numa_interleave_memory(CAST_TO_FN_PTR(numa_interleave_memory_func_t,
2905 libnuma_dlsym(handle, "numa_interleave_memory")));
2906
2907
2908 if (numa_available() != -1) {
2909 set_numa_all_nodes((unsigned long*)libnuma_dlsym(handle, "numa_all_nodes"));
2910 // Create a cpu -> node mapping
2911 _cpu_to_node = new (ResourceObj::C_HEAP) GrowableArray<int>(0, true);
2912 rebuild_cpu_to_node_map();
2913 return true;
2914 }
2915 }
2916 }
2917 return false;
2918 }
2919
2920 // rebuild_cpu_to_node_map() constructs a table mapping cpud id to node id.
2921 // The table is later used in get_node_by_cpu().
2922 void os::Bsd::rebuild_cpu_to_node_map() {
2923 const size_t NCPUS = 32768; // Since the buffer size computation is very obscure
2924 // in libnuma (possible values are starting from 16,
2925 // and continuing up with every other power of 2, but less
2926 // than the maximum number of CPUs supported by kernel), and
2927 // is a subject to change (in libnuma version 2 the requirements
2928 // are more reasonable) we'll just hardcode the number they use
2929 // in the library.
2930 const size_t BitsPerCLong = sizeof(long) * CHAR_BIT;
2931
2932 size_t cpu_num = os::active_processor_count();
2933 size_t cpu_map_size = NCPUS / BitsPerCLong;
2934 size_t cpu_map_valid_size =
2935 MIN2((cpu_num + BitsPerCLong - 1) / BitsPerCLong, cpu_map_size);
2936
2937 cpu_to_node()->clear();
2938 cpu_to_node()->at_grow(cpu_num - 1);
2939 size_t node_num = numa_get_groups_num();
2940
2941 unsigned long *cpu_map = NEW_C_HEAP_ARRAY(unsigned long, cpu_map_size);
2942 for (size_t i = 0; i < node_num; i++) {
2943 if (numa_node_to_cpus(i, cpu_map, cpu_map_size * sizeof(unsigned long)) != -1) {
2944 for (size_t j = 0; j < cpu_map_valid_size; j++) {
2945 if (cpu_map[j] != 0) {
2946 for (size_t k = 0; k < BitsPerCLong; k++) {
2947 if (cpu_map[j] & (1UL << k)) {
2948 cpu_to_node()->at_put(j * BitsPerCLong + k, i);
2949 }
2950 }
2951 }
2952 }
2953 }
2954 }
2955 FREE_C_HEAP_ARRAY(unsigned long, cpu_map);
2956 }
2957
2958 int os::Bsd::get_node_by_cpu(int cpu_id) {
2959 if (cpu_to_node() != NULL && cpu_id >= 0 && cpu_id < cpu_to_node()->length()) {
2960 return cpu_to_node()->at(cpu_id);
2961 }
2962 return -1;
2963 }
2964
2965 GrowableArray<int>* os::Bsd::_cpu_to_node;
2966 os::Bsd::sched_getcpu_func_t os::Bsd::_sched_getcpu;
2967 os::Bsd::numa_node_to_cpus_func_t os::Bsd::_numa_node_to_cpus;
2968 os::Bsd::numa_max_node_func_t os::Bsd::_numa_max_node;
2969 os::Bsd::numa_available_func_t os::Bsd::_numa_available;
2970 os::Bsd::numa_tonode_memory_func_t os::Bsd::_numa_tonode_memory;
2971 os::Bsd::numa_interleave_memory_func_t os::Bsd::_numa_interleave_memory;
2972 unsigned long* os::Bsd::_numa_all_nodes;
2973 #endif
2974 2055
2975 bool os::pd_uncommit_memory(char* addr, size_t size) { 2056 bool os::pd_uncommit_memory(char* addr, size_t size) {
2976 #ifdef __OpenBSD__ 2057 #ifdef __OpenBSD__
2977 // XXX: Work-around mmap/MAP_FIXED bug temporarily on OpenBSD 2058 // XXX: Work-around mmap/MAP_FIXED bug temporarily on OpenBSD
2978 return ::mprotect(addr, size, PROT_NONE) == 0; 2059 return ::mprotect(addr, size, PROT_NONE) == 0;
3088 bool os::unguard_memory(char* addr, size_t size) { 2169 bool os::unguard_memory(char* addr, size_t size) {
3089 return bsd_mprotect(addr, size, PROT_READ|PROT_WRITE); 2170 return bsd_mprotect(addr, size, PROT_READ|PROT_WRITE);
3090 } 2171 }
3091 2172
3092 bool os::Bsd::hugetlbfs_sanity_check(bool warn, size_t page_size) { 2173 bool os::Bsd::hugetlbfs_sanity_check(bool warn, size_t page_size) {
3093 bool result = false; 2174 return false;
3094 #ifndef _ALLBSD_SOURCE
3095 void *p = mmap (NULL, page_size, PROT_READ|PROT_WRITE,
3096 MAP_ANONYMOUS|MAP_PRIVATE|MAP_HUGETLB,
3097 -1, 0);
3098
3099 if (p != (void *) -1) {
3100 // We don't know if this really is a huge page or not.
3101 FILE *fp = fopen("/proc/self/maps", "r");
3102 if (fp) {
3103 while (!feof(fp)) {
3104 char chars[257];
3105 long x = 0;
3106 if (fgets(chars, sizeof(chars), fp)) {
3107 if (sscanf(chars, "%lx-%*x", &x) == 1
3108 && x == (long)p) {
3109 if (strstr (chars, "hugepage")) {
3110 result = true;
3111 break;
3112 }
3113 }
3114 }
3115 }
3116 fclose(fp);
3117 }
3118 munmap (p, page_size);
3119 if (result)
3120 return true;
3121 }
3122
3123 if (warn) {
3124 warning("HugeTLBFS is not supported by the operating system.");
3125 }
3126 #endif
3127
3128 return result;
3129 } 2175 }
3130 2176
3131 /* 2177 /*
3132 * Set the coredump_filter bits to include largepages in core dump (bit 6) 2178 * Set the coredump_filter bits to include largepages in core dump (bit 6)
3133 * 2179 *
3168 // Large page support 2214 // Large page support
3169 2215
3170 static size_t _large_page_size = 0; 2216 static size_t _large_page_size = 0;
3171 2217
3172 void os::large_page_init() { 2218 void os::large_page_init() {
3173 #ifndef _ALLBSD_SOURCE 2219 }
3174 if (!UseLargePages) { 2220
3175 UseHugeTLBFS = false;
3176 UseSHM = false;
3177 return;
3178 }
3179
3180 if (FLAG_IS_DEFAULT(UseHugeTLBFS) && FLAG_IS_DEFAULT(UseSHM)) {
3181 // If UseLargePages is specified on the command line try both methods,
3182 // if it's default, then try only HugeTLBFS.
3183 if (FLAG_IS_DEFAULT(UseLargePages)) {
3184 UseHugeTLBFS = true;
3185 } else {
3186 UseHugeTLBFS = UseSHM = true;
3187 }
3188 }
3189
3190 if (LargePageSizeInBytes) {
3191 _large_page_size = LargePageSizeInBytes;
3192 } else {
3193 // large_page_size on Bsd is used to round up heap size. x86 uses either
3194 // 2M or 4M page, depending on whether PAE (Physical Address Extensions)
3195 // mode is enabled. AMD64/EM64T uses 2M page in 64bit mode. IA64 can use
3196 // page as large as 256M.
3197 //
3198 // Here we try to figure out page size by parsing /proc/meminfo and looking
3199 // for a line with the following format:
3200 // Hugepagesize: 2048 kB
3201 //
3202 // If we can't determine the value (e.g. /proc is not mounted, or the text
3203 // format has been changed), we'll use the largest page size supported by
3204 // the processor.
3205
3206 #ifndef ZERO
3207 _large_page_size = IA32_ONLY(4 * M) AMD64_ONLY(2 * M) IA64_ONLY(256 * M) SPARC_ONLY(4 * M)
3208 ARM_ONLY(2 * M) PPC_ONLY(4 * M);
3209 #endif // ZERO
3210
3211 FILE *fp = fopen("/proc/meminfo", "r");
3212 if (fp) {
3213 while (!feof(fp)) {
3214 int x = 0;
3215 char buf[16];
3216 if (fscanf(fp, "Hugepagesize: %d", &x) == 1) {
3217 if (x && fgets(buf, sizeof(buf), fp) && strcmp(buf, " kB\n") == 0) {
3218 _large_page_size = x * K;
3219 break;
3220 }
3221 } else {
3222 // skip to next line
3223 for (;;) {
3224 int ch = fgetc(fp);
3225 if (ch == EOF || ch == (int)'\n') break;
3226 }
3227 }
3228 }
3229 fclose(fp);
3230 }
3231 }
3232
3233 // print a warning if any large page related flag is specified on command line
3234 bool warn_on_failure = !FLAG_IS_DEFAULT(UseHugeTLBFS);
3235
3236 const size_t default_page_size = (size_t)Bsd::page_size();
3237 if (_large_page_size > default_page_size) {
3238 _page_sizes[0] = _large_page_size;
3239 _page_sizes[1] = default_page_size;
3240 _page_sizes[2] = 0;
3241 }
3242 UseHugeTLBFS = UseHugeTLBFS &&
3243 Bsd::hugetlbfs_sanity_check(warn_on_failure, _large_page_size);
3244
3245 if (UseHugeTLBFS)
3246 UseSHM = false;
3247
3248 UseLargePages = UseHugeTLBFS || UseSHM;
3249
3250 set_coredump_filter();
3251 #endif
3252 }
3253
3254 #ifndef _ALLBSD_SOURCE
3255 #ifndef SHM_HUGETLB
3256 #define SHM_HUGETLB 04000
3257 #endif
3258 #endif
3259 2221
3260 char* os::reserve_memory_special(size_t bytes, char* req_addr, bool exec) { 2222 char* os::reserve_memory_special(size_t bytes, char* req_addr, bool exec) {
3261 // "exec" is passed in but not used. Creating the shared image for 2223 // "exec" is passed in but not used. Creating the shared image for
3262 // the code cache doesn't have an SHM_X executable permission to check. 2224 // the code cache doesn't have an SHM_X executable permission to check.
3263 assert(UseLargePages && UseSHM, "only for SHM large pages"); 2225 assert(UseLargePages && UseSHM, "only for SHM large pages");
3271 ); 2233 );
3272 char msg[128]; 2234 char msg[128];
3273 2235
3274 // Create a large shared memory region to attach to based on size. 2236 // Create a large shared memory region to attach to based on size.
3275 // Currently, size is the total size of the heap 2237 // Currently, size is the total size of the heap
3276 #ifndef _ALLBSD_SOURCE
3277 int shmid = shmget(key, bytes, SHM_HUGETLB|IPC_CREAT|SHM_R|SHM_W);
3278 #else
3279 int shmid = shmget(key, bytes, IPC_CREAT|SHM_R|SHM_W); 2238 int shmid = shmget(key, bytes, IPC_CREAT|SHM_R|SHM_W);
3280 #endif
3281 if (shmid == -1) { 2239 if (shmid == -1) {
3282 // Possible reasons for shmget failure: 2240 // Possible reasons for shmget failure:
3283 // 1. shmmax is too small for Java heap. 2241 // 1. shmmax is too small for Java heap.
3284 // > check shmmax value: cat /proc/sys/kernel/shmmax 2242 // > check shmmax value: cat /proc/sys/kernel/shmmax
3285 // > increase shmmax value: echo "0xffffffff" > /proc/sys/kernel/shmmax 2243 // > increase shmmax value: echo "0xffffffff" > /proc/sys/kernel/shmmax
3562 // not the entire user process, and user level threads are 1:1 mapped to kernel 2520 // not the entire user process, and user level threads are 1:1 mapped to kernel
3563 // threads. It has always been the case, but could change in the future. For 2521 // threads. It has always been the case, but could change in the future. For
3564 // this reason, the code should not be used as default (ThreadPriorityPolicy=0). 2522 // this reason, the code should not be used as default (ThreadPriorityPolicy=0).
3565 // It is only used when ThreadPriorityPolicy=1 and requires root privilege. 2523 // It is only used when ThreadPriorityPolicy=1 and requires root privilege.
3566 2524
3567 #if defined(_ALLBSD_SOURCE) && !defined(__APPLE__) 2525 #if !defined(__APPLE__)
3568 int os::java_to_os_priority[CriticalPriority + 1] = { 2526 int os::java_to_os_priority[CriticalPriority + 1] = {
3569 19, // 0 Entry should never be used 2527 19, // 0 Entry should never be used
3570 2528
3571 0, // 1 MinPriority 2529 0, // 1 MinPriority
3572 3, // 2 2530 3, // 2
3582 2540
3583 31, // 10 MaxPriority 2541 31, // 10 MaxPriority
3584 2542
3585 31 // 11 CriticalPriority 2543 31 // 11 CriticalPriority
3586 }; 2544 };
3587 #elif defined(__APPLE__) 2545 #else
3588 /* Using Mach high-level priority assignments */ 2546 /* Using Mach high-level priority assignments */
3589 int os::java_to_os_priority[CriticalPriority + 1] = { 2547 int os::java_to_os_priority[CriticalPriority + 1] = {
3590 0, // 0 Entry should never be used (MINPRI_USER) 2548 0, // 0 Entry should never be used (MINPRI_USER)
3591 2549
3592 27, // 1 MinPriority 2550 27, // 1 MinPriority
3602 35, // 9 NearMaxPriority 2560 35, // 9 NearMaxPriority
3603 2561
3604 36, // 10 MaxPriority 2562 36, // 10 MaxPriority
3605 2563
3606 36 // 11 CriticalPriority 2564 36 // 11 CriticalPriority
3607 };
3608 #else
3609 int os::java_to_os_priority[CriticalPriority + 1] = {
3610 19, // 0 Entry should never be used
3611
3612 4, // 1 MinPriority
3613 3, // 2
3614 2, // 3
3615
3616 1, // 4
3617 0, // 5 NormPriority
3618 -1, // 6
3619
3620 -2, // 7
3621 -3, // 8
3622 -4, // 9 NearMaxPriority
3623
3624 -5, // 10 MaxPriority
3625
3626 -5 // 11 CriticalPriority
3627 }; 2565 };
3628 #endif 2566 #endif
3629 2567
3630 static int prio_init() { 2568 static int prio_init() {
3631 if (ThreadPriorityPolicy == 1) { 2569 if (ThreadPriorityPolicy == 1) {
4183 } 3121 }
4184 } 3122 }
4185 } 3123 }
4186 } 3124 }
4187 3125
4188 #ifndef _ALLBSD_SOURCE
4189 // This is the fastest way to get thread cpu time on Bsd.
4190 // Returns cpu time (user+sys) for any thread, not only for current.
4191 // POSIX compliant clocks are implemented in the kernels 2.6.16+.
4192 // It might work on 2.6.10+ with a special kernel/glibc patch.
4193 // For reference, please, see IEEE Std 1003.1-2004:
4194 // http://www.unix.org/single_unix_specification
4195
4196 jlong os::Bsd::fast_thread_cpu_time(clockid_t clockid) {
4197 struct timespec tp;
4198 int rc = os::Bsd::clock_gettime(clockid, &tp);
4199 assert(rc == 0, "clock_gettime is expected to return 0 code");
4200
4201 return (tp.tv_sec * NANOSECS_PER_SEC) + tp.tv_nsec;
4202 }
4203 #endif
4204 3126
4205 ///// 3127 /////
4206 // glibc on Bsd platform uses non-documented flag 3128 // glibc on Bsd platform uses non-documented flag
4207 // to indicate, that some special sort of signal 3129 // to indicate, that some special sort of signal
4208 // trampoline is used. 3130 // trampoline is used.
4462 } 3384 }
4463 3385
4464 // this is called _after_ the global arguments have been parsed 3386 // this is called _after_ the global arguments have been parsed
4465 jint os::init_2(void) 3387 jint os::init_2(void)
4466 { 3388 {
4467 #ifndef _ALLBSD_SOURCE
4468 Bsd::fast_thread_clock_init();
4469 #endif
4470
4471 // Allocate a single page and mark it as readable for safepoint polling 3389 // Allocate a single page and mark it as readable for safepoint polling
4472 address polling_page = (address) ::mmap(NULL, Bsd::page_size(), PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); 3390 address polling_page = (address) ::mmap(NULL, Bsd::page_size(), PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
4473 guarantee( polling_page != MAP_FAILED, "os::init_2: failed to allocate polling page" ); 3391 guarantee( polling_page != MAP_FAILED, "os::init_2: failed to allocate polling page" );
4474 3392
4475 os::set_polling_page( polling_page ); 3393 os::set_polling_page( polling_page );
4521 3439
4522 // Make the stack size a multiple of the page size so that 3440 // Make the stack size a multiple of the page size so that
4523 // the yellow/red zones can be guarded. 3441 // the yellow/red zones can be guarded.
4524 JavaThread::set_stack_size_at_create(round_to(threadStackSizeInBytes, 3442 JavaThread::set_stack_size_at_create(round_to(threadStackSizeInBytes,
4525 vm_page_size())); 3443 vm_page_size()));
4526
4527 #ifndef _ALLBSD_SOURCE
4528 Bsd::capture_initial_stack(JavaThread::stack_size_at_create());
4529
4530 Bsd::libpthread_init();
4531 if (PrintMiscellaneous && (Verbose || WizardMode)) {
4532 tty->print_cr("[HotSpot is running with %s, %s(%s)]\n",
4533 Bsd::glibc_version(), Bsd::libpthread_version(),
4534 Bsd::is_floating_stack() ? "floating stack" : "fixed stack");
4535 }
4536
4537 if (UseNUMA) {
4538 if (!Bsd::libnuma_init()) {
4539 UseNUMA = false;
4540 } else {
4541 if ((Bsd::numa_max_node() < 1)) {
4542 // There's only one node(they start from 0), disable NUMA.
4543 UseNUMA = false;
4544 }
4545 }
4546 // With SHM large pages we cannot uncommit a page, so there's not way
4547 // we can make the adaptive lgrp chunk resizing work. If the user specified
4548 // both UseNUMA and UseLargePages (or UseSHM) on the command line - warn and
4549 // disable adaptive resizing.
4550 if (UseNUMA && UseLargePages && UseSHM) {
4551 if (!FLAG_IS_DEFAULT(UseNUMA)) {
4552 if (FLAG_IS_DEFAULT(UseLargePages) && FLAG_IS_DEFAULT(UseSHM)) {
4553 UseLargePages = false;
4554 } else {
4555 warning("UseNUMA is not fully compatible with SHM large pages, disabling adaptive resizing");
4556 UseAdaptiveSizePolicy = false;
4557 UseAdaptiveNUMAChunkSizing = false;
4558 }
4559 } else {
4560 UseNUMA = false;
4561 }
4562 }
4563 if (!UseNUMA && ForceNUMA) {
4564 UseNUMA = true;
4565 }
4566 }
4567 #endif
4568 3444
4569 if (MaxFDLimit) { 3445 if (MaxFDLimit) {
4570 // set the number of file descriptors to max. print out error 3446 // set the number of file descriptors to max. print out error
4571 // if getrlimit/setrlimit fails but continue regardless. 3447 // if getrlimit/setrlimit fails but continue regardless.
4572 struct rlimit nbr_files; 3448 struct rlimit nbr_files;
4590 perror("os::init_2 setrlimit failed"); 3466 perror("os::init_2 setrlimit failed");
4591 } 3467 }
4592 } 3468 }
4593 } 3469 }
4594 3470
4595 #ifndef _ALLBSD_SOURCE
4596 // Initialize lock used to serialize thread creation (see os::create_thread)
4597 Bsd::set_createThread_lock(new Mutex(Mutex::leaf, "createThread_lock", false));
4598 #endif
4599
4600 // at-exit methods are called in the reverse order of their registration. 3471 // at-exit methods are called in the reverse order of their registration.
4601 // atexit functions are called on return from main or as a result of a 3472 // atexit functions are called on return from main or as a result of a
4602 // call to exit(3C). There can be only 32 of these functions registered 3473 // call to exit(3C). There can be only 32 of these functions registered
4603 // and atexit() does not set errno. 3474 // and atexit() does not set errno.
4604 3475
4645 fatal("Could not enable polling page"); 3516 fatal("Could not enable polling page");
4646 } 3517 }
4647 }; 3518 };
4648 3519
4649 int os::active_processor_count() { 3520 int os::active_processor_count() {
4650 #ifdef _ALLBSD_SOURCE
4651 return _processor_count; 3521 return _processor_count;
4652 #else
4653 // Bsd doesn't yet have a (official) notion of processor sets,
4654 // so just return the number of online processors.
4655 int online_cpus = ::sysconf(_SC_NPROCESSORS_ONLN);
4656 assert(online_cpus > 0 && online_cpus <= processor_count(), "sanity check");
4657 return online_cpus;
4658 #endif
4659 } 3522 }
4660 3523
4661 void os::set_native_thread_name(const char *name) { 3524 void os::set_native_thread_name(const char *name) {
4662 #if defined(__APPLE__) && MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_5 3525 #if defined(__APPLE__) && MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_5
4663 // This is only supported in Snow Leopard and beyond 3526 // This is only supported in Snow Leopard and beyond
4707 return epc; 3570 return epc;
4708 } 3571 }
4709 3572
4710 int os::Bsd::safe_cond_timedwait(pthread_cond_t *_cond, pthread_mutex_t *_mutex, const struct timespec *_abstime) 3573 int os::Bsd::safe_cond_timedwait(pthread_cond_t *_cond, pthread_mutex_t *_mutex, const struct timespec *_abstime)
4711 { 3574 {
4712 #ifdef _ALLBSD_SOURCE
4713 return pthread_cond_timedwait(_cond, _mutex, _abstime); 3575 return pthread_cond_timedwait(_cond, _mutex, _abstime);
4714 #else
4715 if (is_NPTL()) {
4716 return pthread_cond_timedwait(_cond, _mutex, _abstime);
4717 } else {
4718 #ifndef IA64
4719 // 6292965: BsdThreads pthread_cond_timedwait() resets FPU control
4720 // word back to default 64bit precision if condvar is signaled. Java
4721 // wants 53bit precision. Save and restore current value.
4722 int fpu = get_fpu_control_word();
4723 #endif // IA64
4724 int status = pthread_cond_timedwait(_cond, _mutex, _abstime);
4725 #ifndef IA64
4726 set_fpu_control_word(fpu);
4727 #endif // IA64
4728 return status;
4729 }
4730 #endif
4731 } 3576 }
4732 3577
4733 //////////////////////////////////////////////////////////////////////////////// 3578 ////////////////////////////////////////////////////////////////////////////////
4734 // debug support 3579 // debug support
4735 3580
5045 // Unmap a block of memory. 3890 // Unmap a block of memory.
5046 bool os::pd_unmap_memory(char* addr, size_t bytes) { 3891 bool os::pd_unmap_memory(char* addr, size_t bytes) {
5047 return munmap(addr, bytes) == 0; 3892 return munmap(addr, bytes) == 0;
5048 } 3893 }
5049 3894
5050 #ifndef _ALLBSD_SOURCE
5051 static jlong slow_thread_cpu_time(Thread *thread, bool user_sys_cpu_time);
5052
5053 static clockid_t thread_cpu_clockid(Thread* thread) {
5054 pthread_t tid = thread->osthread()->pthread_id();
5055 clockid_t clockid;
5056
5057 // Get thread clockid
5058 int rc = os::Bsd::pthread_getcpuclockid(tid, &clockid);
5059 assert(rc == 0, "pthread_getcpuclockid is expected to return 0 code");
5060 return clockid;
5061 }
5062 #endif
5063
5064 // current_thread_cpu_time(bool) and thread_cpu_time(Thread*, bool) 3895 // current_thread_cpu_time(bool) and thread_cpu_time(Thread*, bool)
5065 // are used by JVM M&M and JVMTI to get user+sys or user CPU time 3896 // are used by JVM M&M and JVMTI to get user+sys or user CPU time
5066 // of a thread. 3897 // of a thread.
5067 // 3898 //
5068 // current_thread_cpu_time() and thread_cpu_time(Thread*) returns 3899 // current_thread_cpu_time() and thread_cpu_time(Thread*) returns
5069 // the fast estimate available on the platform. 3900 // the fast estimate available on the platform.
5070 3901
5071 jlong os::current_thread_cpu_time() { 3902 jlong os::current_thread_cpu_time() {
5072 #ifdef __APPLE__ 3903 #ifdef __APPLE__
5073 return os::thread_cpu_time(Thread::current(), true /* user + sys */); 3904 return os::thread_cpu_time(Thread::current(), true /* user + sys */);
5074 #elif !defined(_ALLBSD_SOURCE)
5075 if (os::Bsd::supports_fast_thread_cpu_time()) {
5076 return os::Bsd::fast_thread_cpu_time(CLOCK_THREAD_CPUTIME_ID);
5077 } else {
5078 // return user + sys since the cost is the same
5079 return slow_thread_cpu_time(Thread::current(), true /* user + sys */);
5080 }
5081 #endif 3905 #endif
5082 } 3906 }
5083 3907
5084 jlong os::thread_cpu_time(Thread* thread) { 3908 jlong os::thread_cpu_time(Thread* thread) {
5085 #ifndef _ALLBSD_SOURCE
5086 // consistent with what current_thread_cpu_time() returns
5087 if (os::Bsd::supports_fast_thread_cpu_time()) {
5088 return os::Bsd::fast_thread_cpu_time(thread_cpu_clockid(thread));
5089 } else {
5090 return slow_thread_cpu_time(thread, true /* user + sys */);
5091 }
5092 #endif
5093 } 3909 }
5094 3910
5095 jlong os::current_thread_cpu_time(bool user_sys_cpu_time) { 3911 jlong os::current_thread_cpu_time(bool user_sys_cpu_time) {
5096 #ifdef __APPLE__ 3912 #ifdef __APPLE__
5097 return os::thread_cpu_time(Thread::current(), user_sys_cpu_time); 3913 return os::thread_cpu_time(Thread::current(), user_sys_cpu_time);
5098 #elif !defined(_ALLBSD_SOURCE)
5099 if (user_sys_cpu_time && os::Bsd::supports_fast_thread_cpu_time()) {
5100 return os::Bsd::fast_thread_cpu_time(CLOCK_THREAD_CPUTIME_ID);
5101 } else {
5102 return slow_thread_cpu_time(Thread::current(), user_sys_cpu_time);
5103 }
5104 #endif 3914 #endif
5105 } 3915 }
5106 3916
5107 jlong os::thread_cpu_time(Thread *thread, bool user_sys_cpu_time) { 3917 jlong os::thread_cpu_time(Thread *thread, bool user_sys_cpu_time) {
5108 #ifdef __APPLE__ 3918 #ifdef __APPLE__
5122 nanos += ((jlong) tinfo.system_time.microseconds + (jlong) tinfo.user_time.microseconds) * (jlong)1000; 3932 nanos += ((jlong) tinfo.system_time.microseconds + (jlong) tinfo.user_time.microseconds) * (jlong)1000;
5123 return nanos; 3933 return nanos;
5124 } else { 3934 } else {
5125 return ((jlong)tinfo.user_time.seconds * 1000000000) + ((jlong)tinfo.user_time.microseconds * (jlong)1000); 3935 return ((jlong)tinfo.user_time.seconds * 1000000000) + ((jlong)tinfo.user_time.microseconds * (jlong)1000);
5126 } 3936 }
5127 #elif !defined(_ALLBSD_SOURCE) 3937 #endif
5128 if (user_sys_cpu_time && os::Bsd::supports_fast_thread_cpu_time()) { 3938 }
5129 return os::Bsd::fast_thread_cpu_time(thread_cpu_clockid(thread)); 3939
5130 } else {
5131 return slow_thread_cpu_time(thread, user_sys_cpu_time);
5132 }
5133 #endif
5134 }
5135
5136 #ifndef _ALLBSD_SOURCE
5137 //
5138 // -1 on error.
5139 //
5140
5141 static jlong slow_thread_cpu_time(Thread *thread, bool user_sys_cpu_time) {
5142 static bool proc_pid_cpu_avail = true;
5143 static bool proc_task_unchecked = true;
5144 static const char *proc_stat_path = "/proc/%d/stat";
5145 pid_t tid = thread->osthread()->thread_id();
5146 int i;
5147 char *s;
5148 char stat[2048];
5149 int statlen;
5150 char proc_name[64];
5151 int count;
5152 long sys_time, user_time;
5153 char string[64];
5154 char cdummy;
5155 int idummy;
5156 long ldummy;
5157 FILE *fp;
5158
5159 // We first try accessing /proc/<pid>/cpu since this is faster to
5160 // process. If this file is not present (bsd kernels 2.5 and above)
5161 // then we open /proc/<pid>/stat.
5162 if ( proc_pid_cpu_avail ) {
5163 sprintf(proc_name, "/proc/%d/cpu", tid);
5164 fp = fopen(proc_name, "r");
5165 if ( fp != NULL ) {
5166 count = fscanf( fp, "%s %lu %lu\n", string, &user_time, &sys_time);
5167 fclose(fp);
5168 if ( count != 3 ) return -1;
5169
5170 if (user_sys_cpu_time) {
5171 return ((jlong)sys_time + (jlong)user_time) * (1000000000 / clock_tics_per_sec);
5172 } else {
5173 return (jlong)user_time * (1000000000 / clock_tics_per_sec);
5174 }
5175 }
5176 else proc_pid_cpu_avail = false;
5177 }
5178
5179 // The /proc/<tid>/stat aggregates per-process usage on
5180 // new Bsd kernels 2.6+ where NPTL is supported.
5181 // The /proc/self/task/<tid>/stat still has the per-thread usage.
5182 // See bug 6328462.
5183 // There can be no directory /proc/self/task on kernels 2.4 with NPTL
5184 // and possibly in some other cases, so we check its availability.
5185 if (proc_task_unchecked && os::Bsd::is_NPTL()) {
5186 // This is executed only once
5187 proc_task_unchecked = false;
5188 fp = fopen("/proc/self/task", "r");
5189 if (fp != NULL) {
5190 proc_stat_path = "/proc/self/task/%d/stat";
5191 fclose(fp);
5192 }
5193 }
5194
5195 sprintf(proc_name, proc_stat_path, tid);
5196 fp = fopen(proc_name, "r");
5197 if ( fp == NULL ) return -1;
5198 statlen = fread(stat, 1, 2047, fp);
5199 stat[statlen] = '\0';
5200 fclose(fp);
5201
5202 // Skip pid and the command string. Note that we could be dealing with
5203 // weird command names, e.g. user could decide to rename java launcher
5204 // to "java 1.4.2 :)", then the stat file would look like
5205 // 1234 (java 1.4.2 :)) R ... ...
5206 // We don't really need to know the command string, just find the last
5207 // occurrence of ")" and then start parsing from there. See bug 4726580.
5208 s = strrchr(stat, ')');
5209 i = 0;
5210 if (s == NULL ) return -1;
5211
5212 // Skip blank chars
5213 do s++; while (isspace(*s));
5214
5215 count = sscanf(s,"%c %d %d %d %d %d %lu %lu %lu %lu %lu %lu %lu",
5216 &cdummy, &idummy, &idummy, &idummy, &idummy, &idummy,
5217 &ldummy, &ldummy, &ldummy, &ldummy, &ldummy,
5218 &user_time, &sys_time);
5219 if ( count != 13 ) return -1;
5220 if (user_sys_cpu_time) {
5221 return ((jlong)sys_time + (jlong)user_time) * (1000000000 / clock_tics_per_sec);
5222 } else {
5223 return (jlong)user_time * (1000000000 / clock_tics_per_sec);
5224 }
5225 }
5226 #endif
5227 3940
5228 void os::current_thread_cpu_time_info(jvmtiTimerInfo *info_ptr) { 3941 void os::current_thread_cpu_time_info(jvmtiTimerInfo *info_ptr) {
5229 info_ptr->max_value = ALL_64_BITS; // will not wrap in less than 64 bits 3942 info_ptr->max_value = ALL_64_BITS; // will not wrap in less than 64 bits
5230 info_ptr->may_skip_backward = false; // elapsed time not wall time 3943 info_ptr->may_skip_backward = false; // elapsed time not wall time
5231 info_ptr->may_skip_forward = false; // elapsed time not wall time 3944 info_ptr->may_skip_forward = false; // elapsed time not wall time
5240 } 3953 }
5241 3954
5242 bool os::is_thread_cpu_time_supported() { 3955 bool os::is_thread_cpu_time_supported() {
5243 #ifdef __APPLE__ 3956 #ifdef __APPLE__
5244 return true; 3957 return true;
5245 #elif defined(_ALLBSD_SOURCE) 3958 #else
5246 return false; 3959 return false;
5247 #else
5248 return true;
5249 #endif 3960 #endif
5250 } 3961 }
5251 3962
5252 // System loadavg support. Returns -1 if load average cannot be obtained. 3963 // System loadavg support. Returns -1 if load average cannot be obtained.
5253 // Bsd doesn't yet have a (official) notion of processor sets, 3964 // Bsd doesn't yet have a (official) notion of processor sets,