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