comparison src/os/linux/vm/os_linux.cpp @ 14422:2b8e28fdf503

Merge
author kvn
date Tue, 05 Nov 2013 17:38:04 -0800
parents e2722a66aba7 e4f478e7781b
children 935bf3340572
comparison
equal deleted inserted replaced
14421:3068270ba476 14422:2b8e28fdf503
129 bool os::Linux::_is_floating_stack = false; 129 bool os::Linux::_is_floating_stack = false;
130 bool os::Linux::_is_NPTL = false; 130 bool os::Linux::_is_NPTL = false;
131 bool os::Linux::_supports_fast_thread_cpu_time = false; 131 bool os::Linux::_supports_fast_thread_cpu_time = false;
132 const char * os::Linux::_glibc_version = NULL; 132 const char * os::Linux::_glibc_version = NULL;
133 const char * os::Linux::_libpthread_version = NULL; 133 const char * os::Linux::_libpthread_version = NULL;
134 pthread_condattr_t os::Linux::_condattr[1];
134 135
135 static jlong initial_time_count=0; 136 static jlong initial_time_count=0;
136 137
137 static int clock_tics_per_sec = 100; 138 static int clock_tics_per_sec = 100;
138 139
1332 1333
1333 // Time since start-up in seconds to a fine granularity. 1334 // Time since start-up in seconds to a fine granularity.
1334 // Used by VMSelfDestructTimer and the MemProfiler. 1335 // Used by VMSelfDestructTimer and the MemProfiler.
1335 double os::elapsedTime() { 1336 double os::elapsedTime() {
1336 1337
1337 return (double)(os::elapsed_counter()) * 0.000001; 1338 return ((double)os::elapsed_counter()) / os::elapsed_frequency(); // nanosecond resolution
1338 } 1339 }
1339 1340
1340 jlong os::elapsed_counter() { 1341 jlong os::elapsed_counter() {
1341 timeval time; 1342 return javaTimeNanos() - initial_time_count;
1342 int status = gettimeofday(&time, NULL);
1343 return jlong(time.tv_sec) * 1000 * 1000 + jlong(time.tv_usec) - initial_time_count;
1344 } 1343 }
1345 1344
1346 jlong os::elapsed_frequency() { 1345 jlong os::elapsed_frequency() {
1347 return (1000 * 1000); 1346 return NANOSECS_PER_SEC; // nanosecond resolution
1348 } 1347 }
1349 1348
1350 bool os::supports_vtime() { return true; } 1349 bool os::supports_vtime() { return true; }
1351 bool os::enable_vtime() { return false; } 1350 bool os::enable_vtime() { return false; }
1352 bool os::vtime_enabled() { return false; } 1351 bool os::vtime_enabled() { return false; }
1399 struct timespec tp; 1398 struct timespec tp;
1400 if (clock_getres_func (CLOCK_MONOTONIC, &res) == 0 && 1399 if (clock_getres_func (CLOCK_MONOTONIC, &res) == 0 &&
1401 clock_gettime_func(CLOCK_MONOTONIC, &tp) == 0) { 1400 clock_gettime_func(CLOCK_MONOTONIC, &tp) == 0) {
1402 // yes, monotonic clock is supported 1401 // yes, monotonic clock is supported
1403 _clock_gettime = clock_gettime_func; 1402 _clock_gettime = clock_gettime_func;
1403 return;
1404 } else { 1404 } else {
1405 // close librt if there is no monotonic clock 1405 // close librt if there is no monotonic clock
1406 dlclose(handle); 1406 dlclose(handle);
1407 } 1407 }
1408 } 1408 }
1409 } 1409 }
1410 warning("No monotonic clock was available - timed services may " \
1411 "be adversely affected if the time-of-day clock changes");
1410 } 1412 }
1411 1413
1412 #ifndef SYS_clock_getres 1414 #ifndef SYS_clock_getres
1413 1415
1414 #if defined(IA32) || defined(AMD64) 1416 #if defined(IA32) || defined(AMD64)
2165 2167
2166 os::Linux::print_full_memory_info(st); 2168 os::Linux::print_full_memory_info(st);
2167 } 2169 }
2168 2170
2169 // Try to identify popular distros. 2171 // Try to identify popular distros.
2170 // Most Linux distributions have /etc/XXX-release file, which contains 2172 // Most Linux distributions have a /etc/XXX-release file, which contains
2171 // the OS version string. Some have more than one /etc/XXX-release file 2173 // the OS version string. Newer Linux distributions have a /etc/lsb-release
2172 // (e.g. Mandrake has both /etc/mandrake-release and /etc/redhat-release.), 2174 // file that also contains the OS version string. Some have more than one
2173 // so the order is important. 2175 // /etc/XXX-release file (e.g. Mandrake has both /etc/mandrake-release and
2176 // /etc/redhat-release.), so the order is important.
2177 // Any Linux that is based on Redhat (i.e. Oracle, Mandrake, Sun JDS...) have
2178 // their own specific XXX-release file as well as a redhat-release file.
2179 // Because of this the XXX-release file needs to be searched for before the
2180 // redhat-release file.
2181 // Since Red Hat has a lsb-release file that is not very descriptive the
2182 // search for redhat-release needs to be before lsb-release.
2183 // Since the lsb-release file is the new standard it needs to be searched
2184 // before the older style release files.
2185 // Searching system-release (Red Hat) and os-release (other Linuxes) are a
2186 // next to last resort. The os-release file is a new standard that contains
2187 // distribution information and the system-release file seems to be an old
2188 // standard that has been replaced by the lsb-release and os-release files.
2189 // Searching for the debian_version file is the last resort. It contains
2190 // an informative string like "6.0.6" or "wheezy/sid". Because of this
2191 // "Debian " is printed before the contents of the debian_version file.
2174 void os::Linux::print_distro_info(outputStream* st) { 2192 void os::Linux::print_distro_info(outputStream* st) {
2175 if (!_print_ascii_file("/etc/mandrake-release", st) && 2193 if (!_print_ascii_file("/etc/oracle-release", st) &&
2176 !_print_ascii_file("/etc/sun-release", st) && 2194 !_print_ascii_file("/etc/mandriva-release", st) &&
2177 !_print_ascii_file("/etc/redhat-release", st) && 2195 !_print_ascii_file("/etc/mandrake-release", st) &&
2178 !_print_ascii_file("/etc/SuSE-release", st) && 2196 !_print_ascii_file("/etc/sun-release", st) &&
2179 !_print_ascii_file("/etc/turbolinux-release", st) && 2197 !_print_ascii_file("/etc/redhat-release", st) &&
2180 !_print_ascii_file("/etc/gentoo-release", st) && 2198 !_print_ascii_file("/etc/lsb-release", st) &&
2181 !_print_ascii_file("/etc/debian_version", st) && 2199 !_print_ascii_file("/etc/SuSE-release", st) &&
2182 !_print_ascii_file("/etc/ltib-release", st) && 2200 !_print_ascii_file("/etc/turbolinux-release", st) &&
2183 !_print_ascii_file("/etc/angstrom-version", st)) { 2201 !_print_ascii_file("/etc/gentoo-release", st) &&
2184 st->print("Linux"); 2202 !_print_ascii_file("/etc/ltib-release", st) &&
2185 } 2203 !_print_ascii_file("/etc/angstrom-version", st) &&
2186 st->cr(); 2204 !_print_ascii_file("/etc/system-release", st) &&
2205 !_print_ascii_file("/etc/os-release", st)) {
2206
2207 if (file_exists("/etc/debian_version")) {
2208 st->print("Debian ");
2209 _print_ascii_file("/etc/debian_version", st);
2210 } else {
2211 st->print("Linux");
2212 }
2213 }
2214 st->cr();
2187 } 2215 }
2188 2216
2189 void os::Linux::print_libversion_info(outputStream* st) { 2217 void os::Linux::print_libversion_info(outputStream* st) {
2190 // libc, pthread 2218 // libc, pthread
2191 st->print("libc:"); 2219 st->print("libc:");
2721 2749
2722 void os::numa_make_global(char *addr, size_t bytes) { 2750 void os::numa_make_global(char *addr, size_t bytes) {
2723 Linux::numa_interleave_memory(addr, bytes); 2751 Linux::numa_interleave_memory(addr, bytes);
2724 } 2752 }
2725 2753
2754 // Define for numa_set_bind_policy(int). Setting the argument to 0 will set the
2755 // bind policy to MPOL_PREFERRED for the current thread.
2756 #define USE_MPOL_PREFERRED 0
2757
2726 void os::numa_make_local(char *addr, size_t bytes, int lgrp_hint) { 2758 void os::numa_make_local(char *addr, size_t bytes, int lgrp_hint) {
2759 // To make NUMA and large pages more robust when both enabled, we need to ease
2760 // the requirements on where the memory should be allocated. MPOL_BIND is the
2761 // default policy and it will force memory to be allocated on the specified
2762 // node. Changing this to MPOL_PREFERRED will prefer to allocate the memory on
2763 // the specified node, but will not force it. Using this policy will prevent
2764 // getting SIGBUS when trying to allocate large pages on NUMA nodes with no
2765 // free large pages.
2766 Linux::numa_set_bind_policy(USE_MPOL_PREFERRED);
2727 Linux::numa_tonode_memory(addr, bytes, lgrp_hint); 2767 Linux::numa_tonode_memory(addr, bytes, lgrp_hint);
2728 } 2768 }
2729 2769
2730 bool os::numa_topology_changed() { return false; } 2770 bool os::numa_topology_changed() { return false; }
2731 2771
2823 libnuma_dlsym(handle, "numa_available"))); 2863 libnuma_dlsym(handle, "numa_available")));
2824 set_numa_tonode_memory(CAST_TO_FN_PTR(numa_tonode_memory_func_t, 2864 set_numa_tonode_memory(CAST_TO_FN_PTR(numa_tonode_memory_func_t,
2825 libnuma_dlsym(handle, "numa_tonode_memory"))); 2865 libnuma_dlsym(handle, "numa_tonode_memory")));
2826 set_numa_interleave_memory(CAST_TO_FN_PTR(numa_interleave_memory_func_t, 2866 set_numa_interleave_memory(CAST_TO_FN_PTR(numa_interleave_memory_func_t,
2827 libnuma_dlsym(handle, "numa_interleave_memory"))); 2867 libnuma_dlsym(handle, "numa_interleave_memory")));
2868 set_numa_set_bind_policy(CAST_TO_FN_PTR(numa_set_bind_policy_func_t,
2869 libnuma_dlsym(handle, "numa_set_bind_policy")));
2828 2870
2829 2871
2830 if (numa_available() != -1) { 2872 if (numa_available() != -1) {
2831 set_numa_all_nodes((unsigned long*)libnuma_dlsym(handle, "numa_all_nodes")); 2873 set_numa_all_nodes((unsigned long*)libnuma_dlsym(handle, "numa_all_nodes"));
2832 // Create a cpu -> node mapping 2874 // Create a cpu -> node mapping
2889 os::Linux::numa_node_to_cpus_func_t os::Linux::_numa_node_to_cpus; 2931 os::Linux::numa_node_to_cpus_func_t os::Linux::_numa_node_to_cpus;
2890 os::Linux::numa_max_node_func_t os::Linux::_numa_max_node; 2932 os::Linux::numa_max_node_func_t os::Linux::_numa_max_node;
2891 os::Linux::numa_available_func_t os::Linux::_numa_available; 2933 os::Linux::numa_available_func_t os::Linux::_numa_available;
2892 os::Linux::numa_tonode_memory_func_t os::Linux::_numa_tonode_memory; 2934 os::Linux::numa_tonode_memory_func_t os::Linux::_numa_tonode_memory;
2893 os::Linux::numa_interleave_memory_func_t os::Linux::_numa_interleave_memory; 2935 os::Linux::numa_interleave_memory_func_t os::Linux::_numa_interleave_memory;
2936 os::Linux::numa_set_bind_policy_func_t os::Linux::_numa_set_bind_policy;
2894 unsigned long* os::Linux::_numa_all_nodes; 2937 unsigned long* os::Linux::_numa_all_nodes;
2895 2938
2896 bool os::pd_uncommit_memory(char* addr, size_t size) { 2939 bool os::pd_uncommit_memory(char* addr, size_t size) {
2897 uintptr_t res = (uintptr_t) ::mmap(addr, size, PROT_NONE, 2940 uintptr_t res = (uintptr_t) ::mmap(addr, size, PROT_NONE,
2898 MAP_PRIVATE|MAP_FIXED|MAP_NORESERVE|MAP_ANONYMOUS, -1, 0); 2941 MAP_PRIVATE|MAP_FIXED|MAP_NORESERVE|MAP_ANONYMOUS, -1, 0);
2899 return res != (uintptr_t) MAP_FAILED; 2942 return res != (uintptr_t) MAP_FAILED;
2900 } 2943 }
2944
2945 static
2946 address get_stack_commited_bottom(address bottom, size_t size) {
2947 address nbot = bottom;
2948 address ntop = bottom + size;
2949
2950 size_t page_sz = os::vm_page_size();
2951 unsigned pages = size / page_sz;
2952
2953 unsigned char vec[1];
2954 unsigned imin = 1, imax = pages + 1, imid;
2955 int mincore_return_value;
2956
2957 while (imin < imax) {
2958 imid = (imax + imin) / 2;
2959 nbot = ntop - (imid * page_sz);
2960
2961 // Use a trick with mincore to check whether the page is mapped or not.
2962 // mincore sets vec to 1 if page resides in memory and to 0 if page
2963 // is swapped output but if page we are asking for is unmapped
2964 // it returns -1,ENOMEM
2965 mincore_return_value = mincore(nbot, page_sz, vec);
2966
2967 if (mincore_return_value == -1) {
2968 // Page is not mapped go up
2969 // to find first mapped page
2970 if (errno != EAGAIN) {
2971 assert(errno == ENOMEM, "Unexpected mincore errno");
2972 imax = imid;
2973 }
2974 } else {
2975 // Page is mapped go down
2976 // to find first not mapped page
2977 imin = imid + 1;
2978 }
2979 }
2980
2981 nbot = nbot + page_sz;
2982
2983 // Adjust stack bottom one page up if last checked page is not mapped
2984 if (mincore_return_value == -1) {
2985 nbot = nbot + page_sz;
2986 }
2987
2988 return nbot;
2989 }
2990
2901 2991
2902 // Linux uses a growable mapping for the stack, and if the mapping for 2992 // Linux uses a growable mapping for the stack, and if the mapping for
2903 // the stack guard pages is not removed when we detach a thread the 2993 // the stack guard pages is not removed when we detach a thread the
2904 // stack cannot grow beyond the pages where the stack guard was 2994 // stack cannot grow beyond the pages where the stack guard was
2905 // mapped. If at some point later in the process the stack expands to 2995 // mapped. If at some point later in the process the stack expands to
2911 // so if the stack mapping has already grown beyond the guard pages at 3001 // so if the stack mapping has already grown beyond the guard pages at
2912 // the time we create them, we have to truncate the stack mapping. 3002 // the time we create them, we have to truncate the stack mapping.
2913 // So, we need to know the extent of the stack mapping when 3003 // So, we need to know the extent of the stack mapping when
2914 // create_stack_guard_pages() is called. 3004 // create_stack_guard_pages() is called.
2915 3005
2916 // Find the bounds of the stack mapping. Return true for success.
2917 //
2918 // We only need this for stacks that are growable: at the time of 3006 // We only need this for stacks that are growable: at the time of
2919 // writing thread stacks don't use growable mappings (i.e. those 3007 // writing thread stacks don't use growable mappings (i.e. those
2920 // creeated with MAP_GROWSDOWN), and aren't marked "[stack]", so this 3008 // creeated with MAP_GROWSDOWN), and aren't marked "[stack]", so this
2921 // only applies to the main thread. 3009 // only applies to the main thread.
2922 3010
2923 static
2924 bool get_stack_bounds(uintptr_t *bottom, uintptr_t *top) {
2925
2926 char buf[128];
2927 int fd, sz;
2928
2929 if ((fd = ::open("/proc/self/maps", O_RDONLY)) < 0) {
2930 return false;
2931 }
2932
2933 const char kw[] = "[stack]";
2934 const int kwlen = sizeof(kw)-1;
2935
2936 // Address part of /proc/self/maps couldn't be more than 128 bytes
2937 while ((sz = os::get_line_chars(fd, buf, sizeof(buf))) > 0) {
2938 if (sz > kwlen && ::memcmp(buf+sz-kwlen, kw, kwlen) == 0) {
2939 // Extract addresses
2940 if (sscanf(buf, "%" SCNxPTR "-%" SCNxPTR, bottom, top) == 2) {
2941 uintptr_t sp = (uintptr_t) __builtin_frame_address(0);
2942 if (sp >= *bottom && sp <= *top) {
2943 ::close(fd);
2944 return true;
2945 }
2946 }
2947 }
2948 }
2949
2950 ::close(fd);
2951 return false;
2952 }
2953
2954
2955 // If the (growable) stack mapping already extends beyond the point 3011 // If the (growable) stack mapping already extends beyond the point
2956 // where we're going to put our guard pages, truncate the mapping at 3012 // where we're going to put our guard pages, truncate the mapping at
2957 // that point by munmap()ping it. This ensures that when we later 3013 // that point by munmap()ping it. This ensures that when we later
2958 // munmap() the guard pages we don't leave a hole in the stack 3014 // munmap() the guard pages we don't leave a hole in the stack
2959 // mapping. This only affects the main/initial thread, but guard 3015 // mapping. This only affects the main/initial thread
2960 // against future OS changes 3016
2961 bool os::pd_create_stack_guard_pages(char* addr, size_t size) { 3017 bool os::pd_create_stack_guard_pages(char* addr, size_t size) {
2962 uintptr_t stack_extent, stack_base; 3018
2963 bool chk_bounds = NOT_DEBUG(os::Linux::is_initial_thread()) DEBUG_ONLY(true); 3019 if (os::Linux::is_initial_thread()) {
2964 if (chk_bounds && get_stack_bounds(&stack_extent, &stack_base)) { 3020 // As we manually grow stack up to bottom inside create_attached_thread(),
2965 assert(os::Linux::is_initial_thread(), 3021 // it's likely that os::Linux::initial_thread_stack_bottom is mapped and
2966 "growable stack in non-initial thread"); 3022 // we don't need to do anything special.
2967 if (stack_extent < (uintptr_t)addr) 3023 // Check it first, before calling heavy function.
2968 ::munmap((void*)stack_extent, (uintptr_t)addr - stack_extent); 3024 uintptr_t stack_extent = (uintptr_t) os::Linux::initial_thread_stack_bottom();
3025 unsigned char vec[1];
3026
3027 if (mincore((address)stack_extent, os::vm_page_size(), vec) == -1) {
3028 // Fallback to slow path on all errors, including EAGAIN
3029 stack_extent = (uintptr_t) get_stack_commited_bottom(
3030 os::Linux::initial_thread_stack_bottom(),
3031 (size_t)addr - stack_extent);
3032 }
3033
3034 if (stack_extent < (uintptr_t)addr) {
3035 ::munmap((void*)stack_extent, (uintptr_t)(addr - stack_extent));
3036 }
2969 } 3037 }
2970 3038
2971 return os::commit_memory(addr, size, !ExecMem); 3039 return os::commit_memory(addr, size, !ExecMem);
2972 } 3040 }
2973 3041
2974 // If this is a growable mapping, remove the guard pages entirely by 3042 // If this is a growable mapping, remove the guard pages entirely by
2975 // munmap()ping them. If not, just call uncommit_memory(). This only 3043 // munmap()ping them. If not, just call uncommit_memory(). This only
2976 // affects the main/initial thread, but guard against future OS changes 3044 // affects the main/initial thread, but guard against future OS changes
3045 // It's safe to always unmap guard pages for initial thread because we
3046 // always place it right after end of the mapped region
3047
2977 bool os::remove_stack_guard_pages(char* addr, size_t size) { 3048 bool os::remove_stack_guard_pages(char* addr, size_t size) {
2978 uintptr_t stack_extent, stack_base; 3049 uintptr_t stack_extent, stack_base;
2979 bool chk_bounds = NOT_DEBUG(os::Linux::is_initial_thread()) DEBUG_ONLY(true); 3050
2980 if (chk_bounds && get_stack_bounds(&stack_extent, &stack_base)) { 3051 if (os::Linux::is_initial_thread()) {
2981 assert(os::Linux::is_initial_thread(),
2982 "growable stack in non-initial thread");
2983
2984 return ::munmap(addr, size) == 0; 3052 return ::munmap(addr, size) == 0;
2985 } 3053 }
2986 3054
2987 return os::uncommit_memory(addr, size); 3055 return os::uncommit_memory(addr, size);
2988 } 3056 }
3245 3313
3246 bool os::Linux::setup_large_page_type(size_t page_size) { 3314 bool os::Linux::setup_large_page_type(size_t page_size) {
3247 if (FLAG_IS_DEFAULT(UseHugeTLBFS) && 3315 if (FLAG_IS_DEFAULT(UseHugeTLBFS) &&
3248 FLAG_IS_DEFAULT(UseSHM) && 3316 FLAG_IS_DEFAULT(UseSHM) &&
3249 FLAG_IS_DEFAULT(UseTransparentHugePages)) { 3317 FLAG_IS_DEFAULT(UseTransparentHugePages)) {
3250 // If UseLargePages is specified on the command line try all methods, 3318
3251 // if it's default, then try only UseTransparentHugePages. 3319 // The type of large pages has not been specified by the user.
3252 if (FLAG_IS_DEFAULT(UseLargePages)) { 3320
3253 UseTransparentHugePages = true; 3321 // Try UseHugeTLBFS and then UseSHM.
3254 } else { 3322 UseHugeTLBFS = UseSHM = true;
3255 UseHugeTLBFS = UseTransparentHugePages = UseSHM = true; 3323
3256 } 3324 // Don't try UseTransparentHugePages since there are known
3325 // performance issues with it turned on. This might change in the future.
3326 UseTransparentHugePages = false;
3257 } 3327 }
3258 3328
3259 if (UseTransparentHugePages) { 3329 if (UseTransparentHugePages) {
3260 bool warn_on_failure = !FLAG_IS_DEFAULT(UseTransparentHugePages); 3330 bool warn_on_failure = !FLAG_IS_DEFAULT(UseTransparentHugePages);
3261 if (transparent_huge_pages_sanity_check(warn_on_failure, page_size)) { 3331 if (transparent_huge_pages_sanity_check(warn_on_failure, page_size)) {
3277 3347
3278 return UseSHM; 3348 return UseSHM;
3279 } 3349 }
3280 3350
3281 void os::large_page_init() { 3351 void os::large_page_init() {
3282 if (!UseLargePages) { 3352 if (!UseLargePages &&
3353 !UseTransparentHugePages &&
3354 !UseHugeTLBFS &&
3355 !UseSHM) {
3356 // Not using large pages.
3357 return;
3358 }
3359
3360 if (!FLAG_IS_DEFAULT(UseLargePages) && !UseLargePages) {
3361 // The user explicitly turned off large pages.
3362 // Ignore the rest of the large pages flags.
3363 UseTransparentHugePages = false;
3283 UseHugeTLBFS = false; 3364 UseHugeTLBFS = false;
3284 UseTransparentHugePages = false;
3285 UseSHM = false; 3365 UseSHM = false;
3286 return; 3366 return;
3287 } 3367 }
3288 3368
3289 size_t large_page_size = Linux::setup_large_page_size(); 3369 size_t large_page_size = Linux::setup_large_page_size();
4624 4704
4625 // main_thread points to the aboriginal thread 4705 // main_thread points to the aboriginal thread
4626 Linux::_main_thread = pthread_self(); 4706 Linux::_main_thread = pthread_self();
4627 4707
4628 Linux::clock_init(); 4708 Linux::clock_init();
4629 initial_time_count = os::elapsed_counter(); 4709 initial_time_count = javaTimeNanos();
4710
4711 // pthread_condattr initialization for monotonic clock
4712 int status;
4713 pthread_condattr_t* _condattr = os::Linux::condAttr();
4714 if ((status = pthread_condattr_init(_condattr)) != 0) {
4715 fatal(err_msg("pthread_condattr_init: %s", strerror(status)));
4716 }
4717 // Only set the clock if CLOCK_MONOTONIC is available
4718 if (Linux::supports_monotonic_clock()) {
4719 if ((status = pthread_condattr_setclock(_condattr, CLOCK_MONOTONIC)) != 0) {
4720 if (status == EINVAL) {
4721 warning("Unable to use monotonic clock with relative timed-waits" \
4722 " - changes to the time-of-day clock may have adverse affects");
4723 } else {
4724 fatal(err_msg("pthread_condattr_setclock: %s", strerror(status)));
4725 }
4726 }
4727 }
4728 // else it defaults to CLOCK_REALTIME
4729
4630 pthread_mutex_init(&dl_mutex, NULL); 4730 pthread_mutex_init(&dl_mutex, NULL);
4631 4731
4632 // If the pagesize of the VM is greater than 8K determine the appropriate 4732 // If the pagesize of the VM is greater than 8K determine the appropriate
4633 // number of initial guard pages. The user can change this with the 4733 // number of initial guard pages. The user can change this with the
4634 // command line arguments, if needed. 4734 // command line arguments, if needed.
4670 #ifndef PRODUCT 4770 #ifndef PRODUCT
4671 if(Verbose && PrintMiscellaneous) 4771 if(Verbose && PrintMiscellaneous)
4672 tty->print("[Memory Serialize Page address: " INTPTR_FORMAT "]\n", (intptr_t)mem_serialize_page); 4772 tty->print("[Memory Serialize Page address: " INTPTR_FORMAT "]\n", (intptr_t)mem_serialize_page);
4673 #endif 4773 #endif
4674 } 4774 }
4675
4676 os::large_page_init();
4677 4775
4678 // initialize suspend/resume support - must do this before signal_sets_init() 4776 // initialize suspend/resume support - must do this before signal_sets_init()
4679 if (SR_initialize() != 0) { 4777 if (SR_initialize() != 0) {
4680 perror("SR_initialize failed"); 4778 perror("SR_initialize failed");
4681 return JNI_ERR; 4779 return JNI_ERR;
4706 // the yellow/red zones can be guarded. 4804 // the yellow/red zones can be guarded.
4707 JavaThread::set_stack_size_at_create(round_to(threadStackSizeInBytes, 4805 JavaThread::set_stack_size_at_create(round_to(threadStackSizeInBytes,
4708 vm_page_size())); 4806 vm_page_size()));
4709 4807
4710 Linux::capture_initial_stack(JavaThread::stack_size_at_create()); 4808 Linux::capture_initial_stack(JavaThread::stack_size_at_create());
4809
4810 #if defined(IA32)
4811 workaround_expand_exec_shield_cs_limit();
4812 #endif
4711 4813
4712 Linux::libpthread_init(); 4814 Linux::libpthread_init();
4713 if (PrintMiscellaneous && (Verbose || WizardMode)) { 4815 if (PrintMiscellaneous && (Verbose || WizardMode)) {
4714 tty->print_cr("[HotSpot is running with %s, %s(%s)]\n", 4816 tty->print_cr("[HotSpot is running with %s, %s(%s)]\n",
4715 Linux::glibc_version(), Linux::libpthread_version(), 4817 Linux::glibc_version(), Linux::libpthread_version(),
5434 // abstime will be the absolute timeout time 5536 // abstime will be the absolute timeout time
5435 // TODO: replace compute_abstime() with unpackTime() 5537 // TODO: replace compute_abstime() with unpackTime()
5436 5538
5437 static struct timespec* compute_abstime(timespec* abstime, jlong millis) { 5539 static struct timespec* compute_abstime(timespec* abstime, jlong millis) {
5438 if (millis < 0) millis = 0; 5540 if (millis < 0) millis = 0;
5439 struct timeval now; 5541
5440 int status = gettimeofday(&now, NULL);
5441 assert(status == 0, "gettimeofday");
5442 jlong seconds = millis / 1000; 5542 jlong seconds = millis / 1000;
5443 millis %= 1000; 5543 millis %= 1000;
5444 if (seconds > 50000000) { // see man cond_timedwait(3T) 5544 if (seconds > 50000000) { // see man cond_timedwait(3T)
5445 seconds = 50000000; 5545 seconds = 50000000;
5446 } 5546 }
5447 abstime->tv_sec = now.tv_sec + seconds; 5547
5448 long usec = now.tv_usec + millis * 1000; 5548 if (os::Linux::supports_monotonic_clock()) {
5449 if (usec >= 1000000) { 5549 struct timespec now;
5450 abstime->tv_sec += 1; 5550 int status = os::Linux::clock_gettime(CLOCK_MONOTONIC, &now);
5451 usec -= 1000000; 5551 assert_status(status == 0, status, "clock_gettime");
5452 } 5552 abstime->tv_sec = now.tv_sec + seconds;
5453 abstime->tv_nsec = usec * 1000; 5553 long nanos = now.tv_nsec + millis * NANOSECS_PER_MILLISEC;
5554 if (nanos >= NANOSECS_PER_SEC) {
5555 abstime->tv_sec += 1;
5556 nanos -= NANOSECS_PER_SEC;
5557 }
5558 abstime->tv_nsec = nanos;
5559 } else {
5560 struct timeval now;
5561 int status = gettimeofday(&now, NULL);
5562 assert(status == 0, "gettimeofday");
5563 abstime->tv_sec = now.tv_sec + seconds;
5564 long usec = now.tv_usec + millis * 1000;
5565 if (usec >= 1000000) {
5566 abstime->tv_sec += 1;
5567 usec -= 1000000;
5568 }
5569 abstime->tv_nsec = usec * 1000;
5570 }
5454 return abstime; 5571 return abstime;
5455 } 5572 }
5456 5573
5457 5574
5458 // Test-and-clear _Event, always leaves _Event set to 0, returns immediately. 5575 // Test-and-clear _Event, always leaves _Event set to 0, returns immediately.
5540 5657
5541 while (_Event < 0) { 5658 while (_Event < 0) {
5542 status = os::Linux::safe_cond_timedwait(_cond, _mutex, &abst); 5659 status = os::Linux::safe_cond_timedwait(_cond, _mutex, &abst);
5543 if (status != 0 && WorkAroundNPTLTimedWaitHang) { 5660 if (status != 0 && WorkAroundNPTLTimedWaitHang) {
5544 pthread_cond_destroy (_cond); 5661 pthread_cond_destroy (_cond);
5545 pthread_cond_init (_cond, NULL) ; 5662 pthread_cond_init (_cond, os::Linux::condAttr()) ;
5546 } 5663 }
5547 assert_status(status == 0 || status == EINTR || 5664 assert_status(status == 0 || status == EINTR ||
5548 status == ETIME || status == ETIMEDOUT, 5665 status == ETIME || status == ETIMEDOUT,
5549 status, "cond_timedwait"); 5666 status, "cond_timedwait");
5550 if (!FilterSpuriousWakeups) break ; // previous semantics 5667 if (!FilterSpuriousWakeups) break ; // previous semantics
5641 * years from "now". 5758 * years from "now".
5642 */ 5759 */
5643 5760
5644 static void unpackTime(timespec* absTime, bool isAbsolute, jlong time) { 5761 static void unpackTime(timespec* absTime, bool isAbsolute, jlong time) {
5645 assert (time > 0, "convertTime"); 5762 assert (time > 0, "convertTime");
5646 5763 time_t max_secs = 0;
5647 struct timeval now; 5764
5648 int status = gettimeofday(&now, NULL); 5765 if (!os::Linux::supports_monotonic_clock() || isAbsolute) {
5649 assert(status == 0, "gettimeofday"); 5766 struct timeval now;
5650 5767 int status = gettimeofday(&now, NULL);
5651 time_t max_secs = now.tv_sec + MAX_SECS; 5768 assert(status == 0, "gettimeofday");
5652 5769
5653 if (isAbsolute) { 5770 max_secs = now.tv_sec + MAX_SECS;
5654 jlong secs = time / 1000; 5771
5655 if (secs > max_secs) { 5772 if (isAbsolute) {
5656 absTime->tv_sec = max_secs; 5773 jlong secs = time / 1000;
5657 } 5774 if (secs > max_secs) {
5658 else { 5775 absTime->tv_sec = max_secs;
5659 absTime->tv_sec = secs; 5776 } else {
5660 } 5777 absTime->tv_sec = secs;
5661 absTime->tv_nsec = (time % 1000) * NANOSECS_PER_MILLISEC; 5778 }
5662 } 5779 absTime->tv_nsec = (time % 1000) * NANOSECS_PER_MILLISEC;
5663 else { 5780 } else {
5781 jlong secs = time / NANOSECS_PER_SEC;
5782 if (secs >= MAX_SECS) {
5783 absTime->tv_sec = max_secs;
5784 absTime->tv_nsec = 0;
5785 } else {
5786 absTime->tv_sec = now.tv_sec + secs;
5787 absTime->tv_nsec = (time % NANOSECS_PER_SEC) + now.tv_usec*1000;
5788 if (absTime->tv_nsec >= NANOSECS_PER_SEC) {
5789 absTime->tv_nsec -= NANOSECS_PER_SEC;
5790 ++absTime->tv_sec; // note: this must be <= max_secs
5791 }
5792 }
5793 }
5794 } else {
5795 // must be relative using monotonic clock
5796 struct timespec now;
5797 int status = os::Linux::clock_gettime(CLOCK_MONOTONIC, &now);
5798 assert_status(status == 0, status, "clock_gettime");
5799 max_secs = now.tv_sec + MAX_SECS;
5664 jlong secs = time / NANOSECS_PER_SEC; 5800 jlong secs = time / NANOSECS_PER_SEC;
5665 if (secs >= MAX_SECS) { 5801 if (secs >= MAX_SECS) {
5666 absTime->tv_sec = max_secs; 5802 absTime->tv_sec = max_secs;
5667 absTime->tv_nsec = 0; 5803 absTime->tv_nsec = 0;
5668 } 5804 } else {
5669 else {
5670 absTime->tv_sec = now.tv_sec + secs; 5805 absTime->tv_sec = now.tv_sec + secs;
5671 absTime->tv_nsec = (time % NANOSECS_PER_SEC) + now.tv_usec*1000; 5806 absTime->tv_nsec = (time % NANOSECS_PER_SEC) + now.tv_nsec;
5672 if (absTime->tv_nsec >= NANOSECS_PER_SEC) { 5807 if (absTime->tv_nsec >= NANOSECS_PER_SEC) {
5673 absTime->tv_nsec -= NANOSECS_PER_SEC; 5808 absTime->tv_nsec -= NANOSECS_PER_SEC;
5674 ++absTime->tv_sec; // note: this must be <= max_secs 5809 ++absTime->tv_sec; // note: this must be <= max_secs
5675 } 5810 }
5676 } 5811 }
5746 5881
5747 OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */); 5882 OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */);
5748 jt->set_suspend_equivalent(); 5883 jt->set_suspend_equivalent();
5749 // cleared by handle_special_suspend_equivalent_condition() or java_suspend_self() 5884 // cleared by handle_special_suspend_equivalent_condition() or java_suspend_self()
5750 5885
5886 assert(_cur_index == -1, "invariant");
5751 if (time == 0) { 5887 if (time == 0) {
5752 status = pthread_cond_wait (_cond, _mutex) ; 5888 _cur_index = REL_INDEX; // arbitrary choice when not timed
5889 status = pthread_cond_wait (&_cond[_cur_index], _mutex) ;
5753 } else { 5890 } else {
5754 status = os::Linux::safe_cond_timedwait (_cond, _mutex, &absTime) ; 5891 _cur_index = isAbsolute ? ABS_INDEX : REL_INDEX;
5892 status = os::Linux::safe_cond_timedwait (&_cond[_cur_index], _mutex, &absTime) ;
5755 if (status != 0 && WorkAroundNPTLTimedWaitHang) { 5893 if (status != 0 && WorkAroundNPTLTimedWaitHang) {
5756 pthread_cond_destroy (_cond) ; 5894 pthread_cond_destroy (&_cond[_cur_index]) ;
5757 pthread_cond_init (_cond, NULL); 5895 pthread_cond_init (&_cond[_cur_index], isAbsolute ? NULL : os::Linux::condAttr());
5758 } 5896 }
5759 } 5897 }
5898 _cur_index = -1;
5760 assert_status(status == 0 || status == EINTR || 5899 assert_status(status == 0 || status == EINTR ||
5761 status == ETIME || status == ETIMEDOUT, 5900 status == ETIME || status == ETIMEDOUT,
5762 status, "cond_timedwait"); 5901 status, "cond_timedwait");
5763 5902
5764 #ifdef ASSERT 5903 #ifdef ASSERT
5783 status = pthread_mutex_lock(_mutex); 5922 status = pthread_mutex_lock(_mutex);
5784 assert (status == 0, "invariant") ; 5923 assert (status == 0, "invariant") ;
5785 s = _counter; 5924 s = _counter;
5786 _counter = 1; 5925 _counter = 1;
5787 if (s < 1) { 5926 if (s < 1) {
5788 if (WorkAroundNPTLTimedWaitHang) { 5927 // thread might be parked
5789 status = pthread_cond_signal (_cond) ; 5928 if (_cur_index != -1) {
5790 assert (status == 0, "invariant") ; 5929 // thread is definitely parked
5930 if (WorkAroundNPTLTimedWaitHang) {
5931 status = pthread_cond_signal (&_cond[_cur_index]);
5932 assert (status == 0, "invariant");
5791 status = pthread_mutex_unlock(_mutex); 5933 status = pthread_mutex_unlock(_mutex);
5792 assert (status == 0, "invariant") ; 5934 assert (status == 0, "invariant");
5793 } else { 5935 } else {
5794 status = pthread_mutex_unlock(_mutex); 5936 status = pthread_mutex_unlock(_mutex);
5795 assert (status == 0, "invariant") ; 5937 assert (status == 0, "invariant");
5796 status = pthread_cond_signal (_cond) ; 5938 status = pthread_cond_signal (&_cond[_cur_index]);
5797 assert (status == 0, "invariant") ; 5939 assert (status == 0, "invariant");
5798 } 5940 }
5941 } else {
5942 pthread_mutex_unlock(_mutex);
5943 assert (status == 0, "invariant") ;
5944 }
5799 } else { 5945 } else {
5800 pthread_mutex_unlock(_mutex); 5946 pthread_mutex_unlock(_mutex);
5801 assert (status == 0, "invariant") ; 5947 assert (status == 0, "invariant") ;
5802 } 5948 }
5803 } 5949 }