comparison src/os/linux/vm/os_linux.cpp @ 24218:719853999215 jvmci-0.32

Merge with jdk8u141-b15
author Doug Simon <doug.simon@oracle.com>
date Mon, 14 Aug 2017 23:20:38 +0200
parents 9b69cec6d01b 75021e6fe108
children 8128b98d4736
comparison
equal deleted inserted replaced
24167:ac6f03d5b89e 24218:719853999215
1 /* 1 /*
2 * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved. 2 * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * 4 *
5 * This code is free software; you can redistribute it and/or modify it 5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as 6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
101 # include <stdint.h> 101 # include <stdint.h>
102 # include <inttypes.h> 102 # include <inttypes.h>
103 # include <sys/ioctl.h> 103 # include <sys/ioctl.h>
104 104
105 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC 105 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
106
107 #ifndef _GNU_SOURCE
108 #define _GNU_SOURCE
109 #include <sched.h>
110 #undef _GNU_SOURCE
111 #else
112 #include <sched.h>
113 #endif
106 114
107 // if RUSAGE_THREAD for getrusage() has not been defined, do it here. The code calling 115 // if RUSAGE_THREAD for getrusage() has not been defined, do it here. The code calling
108 // getrusage() is prepared to handle the associated failure. 116 // getrusage() is prepared to handle the associated failure.
109 #ifndef RUSAGE_THREAD 117 #ifndef RUSAGE_THREAD
110 #define RUSAGE_THREAD (1) /* only the calling thread */ 118 #define RUSAGE_THREAD (1) /* only the calling thread */
1065 return false; 1073 return false;
1066 } 1074 }
1067 1075
1068 // Locate initial thread stack. This special handling of initial thread stack 1076 // Locate initial thread stack. This special handling of initial thread stack
1069 // is needed because pthread_getattr_np() on most (all?) Linux distros returns 1077 // is needed because pthread_getattr_np() on most (all?) Linux distros returns
1070 // bogus value for initial thread. 1078 // bogus value for the primordial process thread. While the launcher has created
1079 // the VM in a new thread since JDK 6, we still have to allow for the use of the
1080 // JNI invocation API from a primordial thread.
1071 void os::Linux::capture_initial_stack(size_t max_size) { 1081 void os::Linux::capture_initial_stack(size_t max_size) {
1072 // stack size is the easy part, get it from RLIMIT_STACK 1082
1073 size_t stack_size; 1083 // max_size is either 0 (which means accept OS default for thread stacks) or
1084 // a user-specified value known to be at least the minimum needed. If we
1085 // are actually on the primordial thread we can make it appear that we have a
1086 // smaller max_size stack by inserting the guard pages at that location. But we
1087 // cannot do anything to emulate a larger stack than what has been provided by
1088 // the OS or threading library. In fact if we try to use a stack greater than
1089 // what is set by rlimit then we will crash the hosting process.
1090
1091 // Maximum stack size is the easy part, get it from RLIMIT_STACK.
1092 // If this is "unlimited" then it will be a huge value.
1074 struct rlimit rlim; 1093 struct rlimit rlim;
1075 getrlimit(RLIMIT_STACK, &rlim); 1094 getrlimit(RLIMIT_STACK, &rlim);
1076 stack_size = rlim.rlim_cur; 1095 size_t stack_size = rlim.rlim_cur;
1077 1096
1078 // 6308388: a bug in ld.so will relocate its own .data section to the 1097 // 6308388: a bug in ld.so will relocate its own .data section to the
1079 // lower end of primordial stack; reduce ulimit -s value a little bit 1098 // lower end of primordial stack; reduce ulimit -s value a little bit
1080 // so we won't install guard page on ld.so's data section. 1099 // so we won't install guard page on ld.so's data section.
1081 stack_size -= 2 * page_size(); 1100 stack_size -= 2 * page_size();
1082 1101
1083 // 4441425: avoid crash with "unlimited" stack size on SuSE 7.1 or Redhat
1084 // 7.1, in both cases we will get 2G in return value.
1085 // 4466587: glibc 2.2.x compiled w/o "--enable-kernel=2.4.0" (RH 7.0,
1086 // SuSE 7.2, Debian) can not handle alternate signal stack correctly
1087 // for initial thread if its stack size exceeds 6M. Cap it at 2M,
1088 // in case other parts in glibc still assumes 2M max stack size.
1089 // FIXME: alt signal stack is gone, maybe we can relax this constraint?
1090 // Problem still exists RH7.2 (IA64 anyway) but 2MB is a little small
1091 if (stack_size > 2 * K * K IA64_ONLY(*2))
1092 stack_size = 2 * K * K IA64_ONLY(*2);
1093 // Try to figure out where the stack base (top) is. This is harder. 1102 // Try to figure out where the stack base (top) is. This is harder.
1094 // 1103 //
1095 // When an application is started, glibc saves the initial stack pointer in 1104 // When an application is started, glibc saves the initial stack pointer in
1096 // a global variable "__libc_stack_end", which is then used by system 1105 // a global variable "__libc_stack_end", which is then used by system
1097 // libraries. __libc_stack_end should be pretty close to stack top. The 1106 // libraries. __libc_stack_end should be pretty close to stack top. The
1247 } 1256 }
1248 1257
1249 // stack_top could be partially down the page so align it 1258 // stack_top could be partially down the page so align it
1250 stack_top = align_size_up(stack_top, page_size()); 1259 stack_top = align_size_up(stack_top, page_size());
1251 1260
1252 if (max_size && stack_size > max_size) { 1261 // Allowed stack value is minimum of max_size and what we derived from rlimit
1253 _initial_thread_stack_size = max_size; 1262 if (max_size > 0) {
1263 _initial_thread_stack_size = MIN2(max_size, stack_size);
1254 } else { 1264 } else {
1255 _initial_thread_stack_size = stack_size; 1265 // Accept the rlimit max, but if stack is unlimited then it will be huge, so
1266 // clamp it at 8MB as we do on Solaris
1267 _initial_thread_stack_size = MIN2(stack_size, 8*M);
1256 } 1268 }
1257 1269
1258 _initial_thread_stack_size = align_size_down(_initial_thread_stack_size, page_size()); 1270 _initial_thread_stack_size = align_size_down(_initial_thread_stack_size, page_size());
1259 _initial_thread_stack_bottom = (address)stack_top - _initial_thread_stack_size; 1271 _initial_thread_stack_bottom = (address)stack_top - _initial_thread_stack_size;
1272 assert(_initial_thread_stack_bottom < (address)stack_top, "overflow!");
1260 } 1273 }
1261 1274
1262 //////////////////////////////////////////////////////////////////////////////// 1275 ////////////////////////////////////////////////////////////////////////////////
1263 // time support 1276 // time support
1264 1277
5014 if( !linux_mprotect((char *)_polling_page, Linux::page_size(), PROT_READ)) { 5027 if( !linux_mprotect((char *)_polling_page, Linux::page_size(), PROT_READ)) {
5015 fatal("Could not enable polling page"); 5028 fatal("Could not enable polling page");
5016 } 5029 }
5017 }; 5030 };
5018 5031
5032 static int os_cpu_count(const cpu_set_t* cpus) {
5033 int count = 0;
5034 // only look up to the number of configured processors
5035 for (int i = 0; i < os::processor_count(); i++) {
5036 if (CPU_ISSET(i, cpus)) {
5037 count++;
5038 }
5039 }
5040 return count;
5041 }
5042
5043 // Get the current number of available processors for this process.
5044 // This value can change at any time during a process's lifetime.
5045 // sched_getaffinity gives an accurate answer as it accounts for cpusets.
5046 // If anything goes wrong we fallback to returning the number of online
5047 // processors - which can be greater than the number available to the process.
5019 int os::active_processor_count() { 5048 int os::active_processor_count() {
5020 // Linux doesn't yet have a (official) notion of processor sets, 5049 cpu_set_t cpus; // can represent at most 1024 (CPU_SETSIZE) processors
5021 // so just return the number of online processors. 5050 int cpus_size = sizeof(cpu_set_t);
5022 int online_cpus = ::sysconf(_SC_NPROCESSORS_ONLN); 5051 int cpu_count = 0;
5023 assert(online_cpus > 0 && online_cpus <= processor_count(), "sanity check"); 5052
5024 return online_cpus; 5053 // pid 0 means the current thread - which we have to assume represents the process
5054 if (sched_getaffinity(0, cpus_size, &cpus) == 0) {
5055 cpu_count = os_cpu_count(&cpus);
5056 if (PrintActiveCpus) {
5057 tty->print_cr("active_processor_count: sched_getaffinity processor count: %d", cpu_count);
5058 }
5059 }
5060 else {
5061 cpu_count = ::sysconf(_SC_NPROCESSORS_ONLN);
5062 warning("sched_getaffinity failed (%s)- using online processor count (%d) "
5063 "which may exceed available processors", strerror(errno), cpu_count);
5064 }
5065
5066 assert(cpu_count > 0 && cpu_count <= processor_count(), "sanity check");
5067 return cpu_count;
5025 } 5068 }
5026 5069
5027 void os::set_native_thread_name(const char *name) { 5070 void os::set_native_thread_name(const char *name) {
5028 // Not yet implemented. 5071 // Not yet implemented.
5029 return; 5072 return;