src/hotspot/os/linux/os_linux.cpp
Index Unified diffs Context diffs Sdiffs Wdiffs Patch New Old Previous File Next File open Sdiff src/hotspot/os/linux

src/hotspot/os/linux/os_linux.cpp

Print this page




  21  * questions.
  22  *
  23  */
  24 
  25 // no precompiled headers
  26 #include "classfile/classLoader.hpp"
  27 #include "classfile/systemDictionary.hpp"
  28 #include "classfile/vmSymbols.hpp"
  29 #include "code/icBuffer.hpp"
  30 #include "code/vtableStubs.hpp"
  31 #include "compiler/compileBroker.hpp"
  32 #include "compiler/disassembler.hpp"
  33 #include "interpreter/interpreter.hpp"
  34 #include "jvm_linux.h"
  35 #include "logging/log.hpp"
  36 #include "memory/allocation.inline.hpp"
  37 #include "memory/filemap.hpp"
  38 #include "oops/oop.inline.hpp"
  39 #include "os_linux.inline.hpp"
  40 #include "os_share_linux.hpp"

  41 #include "prims/jniFastGetField.hpp"
  42 #include "prims/jvm.h"
  43 #include "prims/jvm_misc.hpp"
  44 #include "runtime/arguments.hpp"
  45 #include "runtime/atomic.hpp"
  46 #include "runtime/extendedPC.hpp"
  47 #include "runtime/globals.hpp"
  48 #include "runtime/interfaceSupport.hpp"
  49 #include "runtime/init.hpp"
  50 #include "runtime/java.hpp"
  51 #include "runtime/javaCalls.hpp"
  52 #include "runtime/mutexLocker.hpp"
  53 #include "runtime/objectMonitor.hpp"
  54 #include "runtime/orderAccess.inline.hpp"
  55 #include "runtime/osThread.hpp"
  56 #include "runtime/perfMemory.hpp"
  57 #include "runtime/sharedRuntime.hpp"
  58 #include "runtime/statSampler.hpp"
  59 #include "runtime/stubRoutines.hpp"
  60 #include "runtime/thread.inline.hpp"


 155 static sigset_t check_signal_done;
 156 static bool check_signals = true;
 157 
 158 // Signal number used to suspend/resume a thread
 159 
 160 // do not use any signal number less than SIGSEGV, see 4355769
 161 static int SR_signum = SIGUSR2;
 162 sigset_t SR_sigset;
 163 
 164 // utility functions
 165 
 166 static int SR_initialize();
 167 
 168 julong os::available_memory() {
 169   return Linux::available_memory();
 170 }
 171 
 172 julong os::Linux::available_memory() {
 173   // values in struct sysinfo are "unsigned long"
 174   struct sysinfo si;
 175   sysinfo(&si);
 176 
 177   return (julong)si.freeram * si.mem_unit;






















 178 }
 179 
 180 julong os::physical_memory() {
 181   return Linux::physical_memory();

















 182 }
 183 
 184 // Return true if user is running as root.
 185 
 186 bool os::have_special_privileges() {
 187   static bool init = false;
 188   static bool privileges = false;
 189   if (!init) {
 190     privileges = (getuid() != geteuid()) || (getgid() != getegid());
 191     init = true;
 192   }
 193   return privileges;
 194 }
 195 
 196 
 197 #ifndef SYS_gettid
 198 // i386: 224, ia64: 1105, amd64: 186, sparc 143
 199   #ifdef __ia64__
 200     #define SYS_gettid 1105
 201   #else


1934 void os::print_os_info(outputStream* st) {
1935   st->print("OS:");
1936 
1937   os::Linux::print_distro_info(st);
1938 
1939   os::Posix::print_uname_info(st);
1940 
1941   // Print warning if unsafe chroot environment detected
1942   if (unsafe_chroot_detected) {
1943     st->print("WARNING!! ");
1944     st->print_cr("%s", unstable_chroot_error);
1945   }
1946 
1947   os::Linux::print_libversion_info(st);
1948 
1949   os::Posix::print_rlimit_info(st);
1950 
1951   os::Posix::print_load_average(st);
1952 
1953   os::Linux::print_full_memory_info(st);


1954 }
1955 
1956 // Try to identify popular distros.
1957 // Most Linux distributions have a /etc/XXX-release file, which contains
1958 // the OS version string. Newer Linux distributions have a /etc/lsb-release
1959 // file that also contains the OS version string. Some have more than one
1960 // /etc/XXX-release file (e.g. Mandrake has both /etc/mandrake-release and
1961 // /etc/redhat-release.), so the order is important.
1962 // Any Linux that is based on Redhat (i.e. Oracle, Mandrake, Sun JDS...) have
1963 // their own specific XXX-release file as well as a redhat-release file.
1964 // Because of this the XXX-release file needs to be searched for before the
1965 // redhat-release file.
1966 // Since Red Hat and SuSE have an lsb-release file that is not very descriptive the
1967 // search for redhat-release / SuSE-release needs to be before lsb-release.
1968 // Since the lsb-release file is the new standard it needs to be searched
1969 // before the older style release files.
1970 // Searching system-release (Red Hat) and os-release (other Linuxes) are a
1971 // next to last resort.  The os-release file is a new standard that contains
1972 // distribution information and the system-release file seems to be an old
1973 // standard that has been replaced by the lsb-release and os-release files.


2071     parse_os_info(&buf[7], buflen-7, "/etc/debian_version");
2072   } else {
2073     strncpy(buf, "Linux", buflen);
2074   }
2075 }
2076 
2077 void os::Linux::print_libversion_info(outputStream* st) {
2078   // libc, pthread
2079   st->print("libc:");
2080   st->print("%s ", os::Linux::glibc_version());
2081   st->print("%s ", os::Linux::libpthread_version());
2082   st->cr();
2083 }
2084 
2085 void os::Linux::print_full_memory_info(outputStream* st) {
2086   st->print("\n/proc/meminfo:\n");
2087   _print_ascii_file("/proc/meminfo", st);
2088   st->cr();
2089 }
2090 




























































2091 void os::print_memory_info(outputStream* st) {
2092 
2093   st->print("Memory:");
2094   st->print(" %dk page", os::vm_page_size()>>10);
2095 
2096   // values in struct sysinfo are "unsigned long"
2097   struct sysinfo si;
2098   sysinfo(&si);
2099 
2100   st->print(", physical " UINT64_FORMAT "k",
2101             os::physical_memory() >> 10);
2102   st->print("(" UINT64_FORMAT "k free)",
2103             os::available_memory() >> 10);
2104   st->print(", swap " UINT64_FORMAT "k",
2105             ((jlong)si.totalswap * si.mem_unit) >> 10);
2106   st->print("(" UINT64_FORMAT "k free)",
2107             ((jlong)si.freeswap * si.mem_unit) >> 10);
2108   st->cr();
2109 }
2110 


4782   // main_thread points to the aboriginal thread
4783   Linux::_main_thread = pthread_self();
4784 
4785   Linux::clock_init();
4786   initial_time_count = javaTimeNanos();
4787 
4788   // retrieve entry point for pthread_setname_np
4789   Linux::_pthread_setname_np =
4790     (int(*)(pthread_t, const char*))dlsym(RTLD_DEFAULT, "pthread_setname_np");
4791 
4792   os::Posix::init();
4793 }
4794 
4795 // To install functions for atexit system call
4796 extern "C" {
4797   static void perfMemory_exit_helper() {
4798     perfMemory_exit();
4799   }
4800 }
4801 




4802 // this is called _after_ the global arguments have been parsed
4803 jint os::init_2(void) {
4804 
4805   os::Posix::init_2();
4806 
4807   Linux::fast_thread_clock_init();
4808 
4809   // Allocate a single page and mark it as readable for safepoint polling
4810   address polling_page = (address) ::mmap(NULL, Linux::page_size(), PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
4811   guarantee(polling_page != MAP_FAILED, "os::init_2: failed to allocate polling page");
4812 
4813   os::set_polling_page(polling_page);
4814   log_info(os)("SafePoint Polling address: " INTPTR_FORMAT, p2i(polling_page));
4815 
4816   if (!UseMembar) {
4817     address mem_serialize_page = (address) ::mmap(NULL, Linux::page_size(), PROT_READ | PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
4818     guarantee(mem_serialize_page != MAP_FAILED, "mmap Failed for memory serialize page");
4819     os::set_memory_serialize_page(mem_serialize_page);
4820     log_info(os)("Memory Serialize Page address: " INTPTR_FORMAT, p2i(mem_serialize_page));
4821   }


4944   // only look up to the number of configured processors
4945   for (int i = 0; i < os::processor_count(); i++) {
4946     if (CPU_ISSET(i, cpus)) {
4947       count++;
4948     }
4949   }
4950   return count;
4951 }
4952 
4953 #define CPU_COUNT(cpus) _cpu_count(cpus)
4954 
4955 #endif // CPU_COUNT
4956 
4957 // Get the current number of available processors for this process.
4958 // This value can change at any time during a process's lifetime.
4959 // sched_getaffinity gives an accurate answer as it accounts for cpusets.
4960 // If it appears there may be more than 1024 processors then we do a
4961 // dynamic check - see 6515172 for details.
4962 // If anything goes wrong we fallback to returning the number of online
4963 // processors - which can be greater than the number available to the process.
4964 int os::active_processor_count() {
4965   cpu_set_t cpus;  // can represent at most 1024 (CPU_SETSIZE) processors
4966   cpu_set_t* cpus_p = &cpus;
4967   int cpus_size = sizeof(cpu_set_t);
4968 
4969   int configured_cpus = processor_count();  // upper bound on available cpus
4970   int cpu_count = 0;
4971 
4972 // old build platforms may not support dynamic cpu sets
4973 #ifdef CPU_ALLOC
4974 
4975   // To enable easy testing of the dynamic path on different platforms we
4976   // introduce a diagnostic flag: UseCpuAllocPath
4977   if (configured_cpus >= CPU_SETSIZE || UseCpuAllocPath) {
4978     // kernel may use a mask bigger than cpu_set_t
4979     log_trace(os)("active_processor_count: using dynamic path %s"
4980                   "- configured processors: %d",
4981                   UseCpuAllocPath ? "(forced) " : "",
4982                   configured_cpus);
4983     cpus_p = CPU_ALLOC(configured_cpus);
4984     if (cpus_p != NULL) {
4985       cpus_size = CPU_ALLOC_SIZE(configured_cpus);
4986       // zero it just to be safe
4987       CPU_ZERO_S(cpus_size, cpus_p);
4988     }
4989     else {


5012   // pid 0 means the current thread - which we have to assume represents the process
5013   if (sched_getaffinity(0, cpus_size, cpus_p) == 0) {
5014     if (cpus_p != &cpus) { // can only be true when CPU_ALLOC used
5015       cpu_count = CPU_COUNT_S(cpus_size, cpus_p);
5016     }
5017     else {
5018       cpu_count = CPU_COUNT(cpus_p);
5019     }
5020     log_trace(os)("active_processor_count: sched_getaffinity processor count: %d", cpu_count);
5021   }
5022   else {
5023     cpu_count = ::sysconf(_SC_NPROCESSORS_ONLN);
5024     warning("sched_getaffinity failed (%s)- using online processor count (%d) "
5025             "which may exceed available processors", os::strerror(errno), cpu_count);
5026   }
5027 
5028   if (cpus_p != &cpus) { // can only be true when CPU_ALLOC used
5029     CPU_FREE(cpus_p);
5030   }
5031 
5032   assert(cpu_count > 0 && cpu_count <= processor_count(), "sanity check");
5033   return cpu_count;


































5034 }
5035 
5036 void os::set_native_thread_name(const char *name) {
5037   if (Linux::_pthread_setname_np) {
5038     char buf [16]; // according to glibc manpage, 16 chars incl. '/0'
5039     snprintf(buf, sizeof(buf), "%s", name);
5040     buf[sizeof(buf) - 1] = '\0';
5041     const int rc = Linux::_pthread_setname_np(pthread_self(), buf);
5042     // ERANGE should not happen; all other errors should just be ignored.
5043     assert(rc != ERANGE, "pthread_setname_np failed");
5044   }
5045 }
5046 
5047 bool os::distribute_processes(uint length, uint* distribution) {
5048   // Not yet implemented.
5049   return false;
5050 }
5051 
5052 bool os::bind_to_processor(uint processor_id) {
5053   // Not yet implemented.




  21  * questions.
  22  *
  23  */
  24 
  25 // no precompiled headers
  26 #include "classfile/classLoader.hpp"
  27 #include "classfile/systemDictionary.hpp"
  28 #include "classfile/vmSymbols.hpp"
  29 #include "code/icBuffer.hpp"
  30 #include "code/vtableStubs.hpp"
  31 #include "compiler/compileBroker.hpp"
  32 #include "compiler/disassembler.hpp"
  33 #include "interpreter/interpreter.hpp"
  34 #include "jvm_linux.h"
  35 #include "logging/log.hpp"
  36 #include "memory/allocation.inline.hpp"
  37 #include "memory/filemap.hpp"
  38 #include "oops/oop.inline.hpp"
  39 #include "os_linux.inline.hpp"
  40 #include "os_share_linux.hpp"
  41 #include "osContainer_linux.hpp"
  42 #include "prims/jniFastGetField.hpp"
  43 #include "prims/jvm.h"
  44 #include "prims/jvm_misc.hpp"
  45 #include "runtime/arguments.hpp"
  46 #include "runtime/atomic.hpp"
  47 #include "runtime/extendedPC.hpp"
  48 #include "runtime/globals.hpp"
  49 #include "runtime/interfaceSupport.hpp"
  50 #include "runtime/init.hpp"
  51 #include "runtime/java.hpp"
  52 #include "runtime/javaCalls.hpp"
  53 #include "runtime/mutexLocker.hpp"
  54 #include "runtime/objectMonitor.hpp"
  55 #include "runtime/orderAccess.inline.hpp"
  56 #include "runtime/osThread.hpp"
  57 #include "runtime/perfMemory.hpp"
  58 #include "runtime/sharedRuntime.hpp"
  59 #include "runtime/statSampler.hpp"
  60 #include "runtime/stubRoutines.hpp"
  61 #include "runtime/thread.inline.hpp"


 156 static sigset_t check_signal_done;
 157 static bool check_signals = true;
 158 
 159 // Signal number used to suspend/resume a thread
 160 
 161 // do not use any signal number less than SIGSEGV, see 4355769
 162 static int SR_signum = SIGUSR2;
 163 sigset_t SR_sigset;
 164 
 165 // utility functions
 166 
 167 static int SR_initialize();
 168 
 169 julong os::available_memory() {
 170   return Linux::available_memory();
 171 }
 172 
 173 julong os::Linux::available_memory() {
 174   // values in struct sysinfo are "unsigned long"
 175   struct sysinfo si;
 176   julong avail_mem;
 177 
 178   if (OSContainer::is_containerized()) {
 179     jlong mem_limit, mem_usage;
 180     if ((mem_limit = OSContainer::memory_limit_in_bytes()) > 0) {
 181       if ((mem_usage = OSContainer::memory_usage_in_bytes()) > 0) {
 182         if (mem_limit > mem_usage) {
 183           avail_mem = (julong)mem_limit - (julong)mem_usage;
 184         } else {
 185           avail_mem = 0;
 186         }
 187         log_trace(os)("available container memory: " JULONG_FORMAT, avail_mem);
 188         return avail_mem;
 189       } else {
 190         log_debug(os,container)("container memory usage call failed: " JLONG_FORMAT, mem_usage);
 191       }
 192     } else {
 193       log_debug(os,container)("container memory unlimited or failed: " JLONG_FORMAT, mem_limit);
 194     }
 195   }
 196 
 197   sysinfo(&si);
 198   avail_mem = (julong)si.freeram * si.mem_unit;
 199   log_trace(os)("available memory: " JULONG_FORMAT, avail_mem);
 200   return avail_mem;
 201 }
 202 
 203 julong os::physical_memory() {
 204   if (OSContainer::is_containerized()) {
 205     jlong mem_limit;
 206     if ((mem_limit = OSContainer::memory_limit_in_bytes()) > 0) {
 207       log_trace(os)("total container memory: " JLONG_FORMAT, mem_limit);
 208       return (julong)mem_limit; 
 209     } else {
 210       if (mem_limit == OSCONTAINER_ERROR) {
 211         log_debug(os,container)("container memory limit call failed");
 212       }
 213       if (mem_limit == -1) {
 214         log_debug(os,container)("container memory unlimited, using host value");
 215       }
 216     }
 217   }
 218 
 219   jlong phys_mem = Linux::physical_memory();
 220   log_trace(os)("total system memory: " JLONG_FORMAT, phys_mem);
 221   return phys_mem;
 222 }
 223 
 224 // Return true if user is running as root.
 225 
 226 bool os::have_special_privileges() {
 227   static bool init = false;
 228   static bool privileges = false;
 229   if (!init) {
 230     privileges = (getuid() != geteuid()) || (getgid() != getegid());
 231     init = true;
 232   }
 233   return privileges;
 234 }
 235 
 236 
 237 #ifndef SYS_gettid
 238 // i386: 224, ia64: 1105, amd64: 186, sparc 143
 239   #ifdef __ia64__
 240     #define SYS_gettid 1105
 241   #else


1974 void os::print_os_info(outputStream* st) {
1975   st->print("OS:");
1976 
1977   os::Linux::print_distro_info(st);
1978 
1979   os::Posix::print_uname_info(st);
1980 
1981   // Print warning if unsafe chroot environment detected
1982   if (unsafe_chroot_detected) {
1983     st->print("WARNING!! ");
1984     st->print_cr("%s", unstable_chroot_error);
1985   }
1986 
1987   os::Linux::print_libversion_info(st);
1988 
1989   os::Posix::print_rlimit_info(st);
1990 
1991   os::Posix::print_load_average(st);
1992 
1993   os::Linux::print_full_memory_info(st);
1994 
1995   os::Linux::print_container_info(st);
1996 }
1997 
1998 // Try to identify popular distros.
1999 // Most Linux distributions have a /etc/XXX-release file, which contains
2000 // the OS version string. Newer Linux distributions have a /etc/lsb-release
2001 // file that also contains the OS version string. Some have more than one
2002 // /etc/XXX-release file (e.g. Mandrake has both /etc/mandrake-release and
2003 // /etc/redhat-release.), so the order is important.
2004 // Any Linux that is based on Redhat (i.e. Oracle, Mandrake, Sun JDS...) have
2005 // their own specific XXX-release file as well as a redhat-release file.
2006 // Because of this the XXX-release file needs to be searched for before the
2007 // redhat-release file.
2008 // Since Red Hat and SuSE have an lsb-release file that is not very descriptive the
2009 // search for redhat-release / SuSE-release needs to be before lsb-release.
2010 // Since the lsb-release file is the new standard it needs to be searched
2011 // before the older style release files.
2012 // Searching system-release (Red Hat) and os-release (other Linuxes) are a
2013 // next to last resort.  The os-release file is a new standard that contains
2014 // distribution information and the system-release file seems to be an old
2015 // standard that has been replaced by the lsb-release and os-release files.


2113     parse_os_info(&buf[7], buflen-7, "/etc/debian_version");
2114   } else {
2115     strncpy(buf, "Linux", buflen);
2116   }
2117 }
2118 
2119 void os::Linux::print_libversion_info(outputStream* st) {
2120   // libc, pthread
2121   st->print("libc:");
2122   st->print("%s ", os::Linux::glibc_version());
2123   st->print("%s ", os::Linux::libpthread_version());
2124   st->cr();
2125 }
2126 
2127 void os::Linux::print_full_memory_info(outputStream* st) {
2128   st->print("\n/proc/meminfo:\n");
2129   _print_ascii_file("/proc/meminfo", st);
2130   st->cr();
2131 }
2132 
2133 void os::Linux::print_container_info(outputStream* st) {
2134   if (OSContainer::is_containerized()) {
2135     st->print("container (cgroup) information:\n");
2136 
2137     char *p = OSContainer::container_type(); 
2138     if (p == NULL)
2139       st->print("container_type() failed\n");
2140     else {
2141       st->print("container_type: %s\n", p);
2142     }
2143 
2144     p = OSContainer::cpu_cpuset_cpus(); 
2145     if (p == NULL)
2146       st->print("cpu_cpuset_cpus() failed\n");
2147     else {
2148       st->print("cpu_cpuset_cpus: %s", p);
2149       free(p);
2150     }
2151 
2152     p = OSContainer::cpu_cpuset_memory_nodes(); 
2153     if (p < 0)
2154       st->print("cpu_memory_nodes() failed\n");
2155     else {
2156       st->print("cpu_memory_nodes: %s", p);
2157       free(p);
2158     }
2159 
2160     int i = OSContainer::active_processor_count();
2161     if (i < 0)
2162       st->print("active_processor_count() failed\n");
2163     else 
2164       st->print("active_processor_count: %d\n", i);
2165 
2166     i = OSContainer::cpu_quota();
2167     st->print("cpu_quota: %d\n", i);
2168 
2169     i = OSContainer::cpu_period();
2170     st->print("cpu_period: %d\n", i);
2171 
2172     i = OSContainer::cpu_shares();
2173     st->print("cpu_shares: %d\n", i);
2174 
2175     jlong j = OSContainer::memory_limit_in_bytes();
2176     st->print("memory_limit_in_bytes: " JLONG_FORMAT "\n", j);
2177 
2178     j = OSContainer::memory_and_swap_limit_in_bytes();
2179     st->print("memory_and_swap_limit_in_bytes: " JLONG_FORMAT "\n", j);
2180 
2181     j = OSContainer::memory_soft_limit_in_bytes();
2182     st->print("memory_soft_limit_in_bytes: " JLONG_FORMAT "\n", j);
2183 
2184     j = OSContainer::OSContainer::memory_usage_in_bytes();
2185     st->print("memory_usage_in_bytes: " JLONG_FORMAT "\n", j);
2186 
2187     j = OSContainer::OSContainer::memory_max_usage_in_bytes();
2188     st->print("memory_max_usage_in_bytes: " JLONG_FORMAT "\n", j);
2189     st->cr();
2190   }
2191 }
2192 
2193 void os::print_memory_info(outputStream* st) {
2194 
2195   st->print("Memory:");
2196   st->print(" %dk page", os::vm_page_size()>>10);
2197 
2198   // values in struct sysinfo are "unsigned long"
2199   struct sysinfo si;
2200   sysinfo(&si);
2201 
2202   st->print(", physical " UINT64_FORMAT "k",
2203             os::physical_memory() >> 10);
2204   st->print("(" UINT64_FORMAT "k free)",
2205             os::available_memory() >> 10);
2206   st->print(", swap " UINT64_FORMAT "k",
2207             ((jlong)si.totalswap * si.mem_unit) >> 10);
2208   st->print("(" UINT64_FORMAT "k free)",
2209             ((jlong)si.freeswap * si.mem_unit) >> 10);
2210   st->cr();
2211 }
2212 


4884   // main_thread points to the aboriginal thread
4885   Linux::_main_thread = pthread_self();
4886 
4887   Linux::clock_init();
4888   initial_time_count = javaTimeNanos();
4889 
4890   // retrieve entry point for pthread_setname_np
4891   Linux::_pthread_setname_np =
4892     (int(*)(pthread_t, const char*))dlsym(RTLD_DEFAULT, "pthread_setname_np");
4893 
4894   os::Posix::init();
4895 }
4896 
4897 // To install functions for atexit system call
4898 extern "C" {
4899   static void perfMemory_exit_helper() {
4900     perfMemory_exit();
4901   }
4902 }
4903 
4904 void os::pd_init_container_support() {
4905   OSContainer::init();
4906 }
4907 
4908 // this is called _after_ the global arguments have been parsed
4909 jint os::init_2(void) {
4910 
4911   os::Posix::init_2();
4912 
4913   Linux::fast_thread_clock_init();
4914 
4915   // Allocate a single page and mark it as readable for safepoint polling
4916   address polling_page = (address) ::mmap(NULL, Linux::page_size(), PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
4917   guarantee(polling_page != MAP_FAILED, "os::init_2: failed to allocate polling page");
4918 
4919   os::set_polling_page(polling_page);
4920   log_info(os)("SafePoint Polling address: " INTPTR_FORMAT, p2i(polling_page));
4921 
4922   if (!UseMembar) {
4923     address mem_serialize_page = (address) ::mmap(NULL, Linux::page_size(), PROT_READ | PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
4924     guarantee(mem_serialize_page != MAP_FAILED, "mmap Failed for memory serialize page");
4925     os::set_memory_serialize_page(mem_serialize_page);
4926     log_info(os)("Memory Serialize Page address: " INTPTR_FORMAT, p2i(mem_serialize_page));
4927   }


5050   // only look up to the number of configured processors
5051   for (int i = 0; i < os::processor_count(); i++) {
5052     if (CPU_ISSET(i, cpus)) {
5053       count++;
5054     }
5055   }
5056   return count;
5057 }
5058 
5059 #define CPU_COUNT(cpus) _cpu_count(cpus)
5060 
5061 #endif // CPU_COUNT
5062 
5063 // Get the current number of available processors for this process.
5064 // This value can change at any time during a process's lifetime.
5065 // sched_getaffinity gives an accurate answer as it accounts for cpusets.
5066 // If it appears there may be more than 1024 processors then we do a
5067 // dynamic check - see 6515172 for details.
5068 // If anything goes wrong we fallback to returning the number of online
5069 // processors - which can be greater than the number available to the process.
5070 int os::Linux::active_processor_count() {
5071   cpu_set_t cpus;  // can represent at most 1024 (CPU_SETSIZE) processors
5072   cpu_set_t* cpus_p = &cpus;
5073   int cpus_size = sizeof(cpu_set_t);
5074 
5075   int configured_cpus = os::processor_count();  // upper bound on available cpus
5076   int cpu_count = 0;
5077 
5078 // old build platforms may not support dynamic cpu sets
5079 #ifdef CPU_ALLOC
5080 
5081   // To enable easy testing of the dynamic path on different platforms we
5082   // introduce a diagnostic flag: UseCpuAllocPath
5083   if (configured_cpus >= CPU_SETSIZE || UseCpuAllocPath) {
5084     // kernel may use a mask bigger than cpu_set_t
5085     log_trace(os)("active_processor_count: using dynamic path %s"
5086                   "- configured processors: %d",
5087                   UseCpuAllocPath ? "(forced) " : "",
5088                   configured_cpus);
5089     cpus_p = CPU_ALLOC(configured_cpus);
5090     if (cpus_p != NULL) {
5091       cpus_size = CPU_ALLOC_SIZE(configured_cpus);
5092       // zero it just to be safe
5093       CPU_ZERO_S(cpus_size, cpus_p);
5094     }
5095     else {


5118   // pid 0 means the current thread - which we have to assume represents the process
5119   if (sched_getaffinity(0, cpus_size, cpus_p) == 0) {
5120     if (cpus_p != &cpus) { // can only be true when CPU_ALLOC used
5121       cpu_count = CPU_COUNT_S(cpus_size, cpus_p);
5122     }
5123     else {
5124       cpu_count = CPU_COUNT(cpus_p);
5125     }
5126     log_trace(os)("active_processor_count: sched_getaffinity processor count: %d", cpu_count);
5127   }
5128   else {
5129     cpu_count = ::sysconf(_SC_NPROCESSORS_ONLN);
5130     warning("sched_getaffinity failed (%s)- using online processor count (%d) "
5131             "which may exceed available processors", os::strerror(errno), cpu_count);
5132   }
5133 
5134   if (cpus_p != &cpus) { // can only be true when CPU_ALLOC used
5135     CPU_FREE(cpus_p);
5136   }
5137 
5138   assert(cpu_count > 0 && cpu_count <= os::processor_count(), "sanity check");
5139   return cpu_count;
5140 }
5141 
5142 // Determine the active processor count from one of
5143 // three different sources:
5144 // 
5145 // 1. User option -XX:ActiveProcessorCount
5146 // 2. kernel os calls (sched_getaffinity or sysconf(_SC_NPROCESSORS_ONLN)
5147 // 3. extracted from cgroup cpu subsystem (shares and quotas)
5148 //
5149 // Option 1, if specified, will always override.
5150 // If the cgroup subsystem is active and configured, we
5151 // will return the min of the cgroup and option 2 results.
5152 // This is required since tools, such as numactl, that
5153 // alter cpu affinity do not update cgroup subsystem 
5154 // cpuset configuration files.
5155 int os::active_processor_count() {
5156   // User has overridden the number of active processors
5157   if (ActiveProcessorCount > 0) {
5158     log_trace(os)("active_processor_count: "
5159                   "active processor count set by user : %d",
5160                   ActiveProcessorCount);
5161     return ActiveProcessorCount;
5162   }
5163 
5164   int active_cpus;
5165   if (OSContainer::is_containerized()) {
5166     active_cpus = OSContainer::active_processor_count();
5167     log_trace(os)("active_processor_count: determined by OSContainer: %d",
5168                    active_cpus);
5169   } else {
5170     active_cpus = os::Linux::active_processor_count();
5171   }
5172 
5173   return active_cpus;
5174 }
5175 
5176 void os::set_native_thread_name(const char *name) {
5177   if (Linux::_pthread_setname_np) {
5178     char buf [16]; // according to glibc manpage, 16 chars incl. '/0'
5179     snprintf(buf, sizeof(buf), "%s", name);
5180     buf[sizeof(buf) - 1] = '\0';
5181     const int rc = Linux::_pthread_setname_np(pthread_self(), buf);
5182     // ERANGE should not happen; all other errors should just be ignored.
5183     assert(rc != ERANGE, "pthread_setname_np failed");
5184   }
5185 }
5186 
5187 bool os::distribute_processes(uint length, uint* distribution) {
5188   // Not yet implemented.
5189   return false;
5190 }
5191 
5192 bool os::bind_to_processor(uint processor_id) {
5193   // Not yet implemented.


src/hotspot/os/linux/os_linux.cpp
Index Unified diffs Context diffs Sdiffs Wdiffs Patch New Old Previous File Next File