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 |
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 |
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); |
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) { |
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; |
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"); |
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; |
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 |