src/hotspot/os/linux/os_linux.cpp
changeset 47903 7f22774a5f42
parent 47881 0ce0ac68ace7
child 48005 9fd89aabb6cd
equal deleted inserted replaced
47896:7092940fbaff 47903:7f22774a5f42
    36 #include "memory/allocation.inline.hpp"
    36 #include "memory/allocation.inline.hpp"
    37 #include "memory/filemap.hpp"
    37 #include "memory/filemap.hpp"
    38 #include "oops/oop.inline.hpp"
    38 #include "oops/oop.inline.hpp"
    39 #include "os_linux.inline.hpp"
    39 #include "os_linux.inline.hpp"
    40 #include "os_share_linux.hpp"
    40 #include "os_share_linux.hpp"
       
    41 #include "osContainer_linux.hpp"
    41 #include "prims/jniFastGetField.hpp"
    42 #include "prims/jniFastGetField.hpp"
    42 #include "prims/jvm_misc.hpp"
    43 #include "prims/jvm_misc.hpp"
    43 #include "runtime/arguments.hpp"
    44 #include "runtime/arguments.hpp"
    44 #include "runtime/atomic.hpp"
    45 #include "runtime/atomic.hpp"
    45 #include "runtime/extendedPC.hpp"
    46 #include "runtime/extendedPC.hpp"
   169 }
   170 }
   170 
   171 
   171 julong os::Linux::available_memory() {
   172 julong os::Linux::available_memory() {
   172   // values in struct sysinfo are "unsigned long"
   173   // values in struct sysinfo are "unsigned long"
   173   struct sysinfo si;
   174   struct sysinfo si;
       
   175   julong avail_mem;
       
   176 
       
   177   if (OSContainer::is_containerized()) {
       
   178     jlong mem_limit, mem_usage;
       
   179     if ((mem_limit = OSContainer::memory_limit_in_bytes()) > 0) {
       
   180       if ((mem_usage = OSContainer::memory_usage_in_bytes()) > 0) {
       
   181         if (mem_limit > mem_usage) {
       
   182           avail_mem = (julong)mem_limit - (julong)mem_usage;
       
   183         } else {
       
   184           avail_mem = 0;
       
   185         }
       
   186         log_trace(os)("available container memory: " JULONG_FORMAT, avail_mem);
       
   187         return avail_mem;
       
   188       } else {
       
   189         log_debug(os,container)("container memory usage call failed: " JLONG_FORMAT, mem_usage);
       
   190       }
       
   191     } else {
       
   192       log_debug(os,container)("container memory unlimited or failed: " JLONG_FORMAT, mem_limit);
       
   193     }
       
   194   }
       
   195 
   174   sysinfo(&si);
   196   sysinfo(&si);
   175 
   197   avail_mem = (julong)si.freeram * si.mem_unit;
   176   return (julong)si.freeram * si.mem_unit;
   198   log_trace(os)("available memory: " JULONG_FORMAT, avail_mem);
       
   199   return avail_mem;
   177 }
   200 }
   178 
   201 
   179 julong os::physical_memory() {
   202 julong os::physical_memory() {
   180   return Linux::physical_memory();
   203   if (OSContainer::is_containerized()) {
       
   204     jlong mem_limit;
       
   205     if ((mem_limit = OSContainer::memory_limit_in_bytes()) > 0) {
       
   206       log_trace(os)("total container memory: " JLONG_FORMAT, mem_limit);
       
   207       return (julong)mem_limit;
       
   208     } else {
       
   209       if (mem_limit == OSCONTAINER_ERROR) {
       
   210         log_debug(os,container)("container memory limit call failed");
       
   211       }
       
   212       if (mem_limit == -1) {
       
   213         log_debug(os,container)("container memory unlimited, using host value");
       
   214       }
       
   215     }
       
   216   }
       
   217 
       
   218   jlong phys_mem = Linux::physical_memory();
       
   219   log_trace(os)("total system memory: " JLONG_FORMAT, phys_mem);
       
   220   return phys_mem;
   181 }
   221 }
   182 
   222 
   183 // Return true if user is running as root.
   223 // Return true if user is running as root.
   184 
   224 
   185 bool os::have_special_privileges() {
   225 bool os::have_special_privileges() {
  1948   os::Posix::print_rlimit_info(st);
  1988   os::Posix::print_rlimit_info(st);
  1949 
  1989 
  1950   os::Posix::print_load_average(st);
  1990   os::Posix::print_load_average(st);
  1951 
  1991 
  1952   os::Linux::print_full_memory_info(st);
  1992   os::Linux::print_full_memory_info(st);
       
  1993 
       
  1994   os::Linux::print_container_info(st);
  1953 }
  1995 }
  1954 
  1996 
  1955 // Try to identify popular distros.
  1997 // Try to identify popular distros.
  1956 // Most Linux distributions have a /etc/XXX-release file, which contains
  1998 // Most Linux distributions have a /etc/XXX-release file, which contains
  1957 // the OS version string. Newer Linux distributions have a /etc/lsb-release
  1999 // the OS version string. Newer Linux distributions have a /etc/lsb-release
  2083 
  2125 
  2084 void os::Linux::print_full_memory_info(outputStream* st) {
  2126 void os::Linux::print_full_memory_info(outputStream* st) {
  2085   st->print("\n/proc/meminfo:\n");
  2127   st->print("\n/proc/meminfo:\n");
  2086   _print_ascii_file("/proc/meminfo", st);
  2128   _print_ascii_file("/proc/meminfo", st);
  2087   st->cr();
  2129   st->cr();
       
  2130 }
       
  2131 
       
  2132 void os::Linux::print_container_info(outputStream* st) {
       
  2133   if (OSContainer::is_containerized()) {
       
  2134     st->print("container (cgroup) information:\n");
       
  2135 
       
  2136     char *p = OSContainer::container_type();
       
  2137     if (p == NULL)
       
  2138       st->print("container_type() failed\n");
       
  2139     else {
       
  2140       st->print("container_type: %s\n", p);
       
  2141     }
       
  2142 
       
  2143     p = OSContainer::cpu_cpuset_cpus();
       
  2144     if (p == NULL)
       
  2145       st->print("cpu_cpuset_cpus() failed\n");
       
  2146     else {
       
  2147       st->print("cpu_cpuset_cpus: %s\n", p);
       
  2148       free(p);
       
  2149     }
       
  2150 
       
  2151     p = OSContainer::cpu_cpuset_memory_nodes();
       
  2152     if (p < 0)
       
  2153       st->print("cpu_memory_nodes() failed\n");
       
  2154     else {
       
  2155       st->print("cpu_memory_nodes: %s\n", p);
       
  2156       free(p);
       
  2157     }
       
  2158 
       
  2159     int i = OSContainer::active_processor_count();
       
  2160     if (i < 0)
       
  2161       st->print("active_processor_count() failed\n");
       
  2162     else
       
  2163       st->print("active_processor_count: %d\n", i);
       
  2164 
       
  2165     i = OSContainer::cpu_quota();
       
  2166     st->print("cpu_quota: %d\n", i);
       
  2167 
       
  2168     i = OSContainer::cpu_period();
       
  2169     st->print("cpu_period: %d\n", i);
       
  2170 
       
  2171     i = OSContainer::cpu_shares();
       
  2172     st->print("cpu_shares: %d\n", i);
       
  2173 
       
  2174     jlong j = OSContainer::memory_limit_in_bytes();
       
  2175     st->print("memory_limit_in_bytes: " JLONG_FORMAT "\n", j);
       
  2176 
       
  2177     j = OSContainer::memory_and_swap_limit_in_bytes();
       
  2178     st->print("memory_and_swap_limit_in_bytes: " JLONG_FORMAT "\n", j);
       
  2179 
       
  2180     j = OSContainer::memory_soft_limit_in_bytes();
       
  2181     st->print("memory_soft_limit_in_bytes: " JLONG_FORMAT "\n", j);
       
  2182 
       
  2183     j = OSContainer::OSContainer::memory_usage_in_bytes();
       
  2184     st->print("memory_usage_in_bytes: " JLONG_FORMAT "\n", j);
       
  2185 
       
  2186     j = OSContainer::OSContainer::memory_max_usage_in_bytes();
       
  2187     st->print("memory_max_usage_in_bytes: " JLONG_FORMAT "\n", j);
       
  2188     st->cr();
       
  2189   }
  2088 }
  2190 }
  2089 
  2191 
  2090 void os::print_memory_info(outputStream* st) {
  2192 void os::print_memory_info(outputStream* st) {
  2091 
  2193 
  2092   st->print("Memory:");
  2194   st->print("Memory:");
  4796   static void perfMemory_exit_helper() {
  4898   static void perfMemory_exit_helper() {
  4797     perfMemory_exit();
  4899     perfMemory_exit();
  4798   }
  4900   }
  4799 }
  4901 }
  4800 
  4902 
       
  4903 void os::pd_init_container_support() {
       
  4904   OSContainer::init();
       
  4905 }
       
  4906 
  4801 // this is called _after_ the global arguments have been parsed
  4907 // this is called _after_ the global arguments have been parsed
  4802 jint os::init_2(void) {
  4908 jint os::init_2(void) {
  4803 
  4909 
  4804   os::Posix::init_2();
  4910   os::Posix::init_2();
  4805 
  4911 
  4944 // sched_getaffinity gives an accurate answer as it accounts for cpusets.
  5050 // sched_getaffinity gives an accurate answer as it accounts for cpusets.
  4945 // If it appears there may be more than 1024 processors then we do a
  5051 // If it appears there may be more than 1024 processors then we do a
  4946 // dynamic check - see 6515172 for details.
  5052 // dynamic check - see 6515172 for details.
  4947 // If anything goes wrong we fallback to returning the number of online
  5053 // If anything goes wrong we fallback to returning the number of online
  4948 // processors - which can be greater than the number available to the process.
  5054 // processors - which can be greater than the number available to the process.
  4949 int os::active_processor_count() {
  5055 int os::Linux::active_processor_count() {
  4950   cpu_set_t cpus;  // can represent at most 1024 (CPU_SETSIZE) processors
  5056   cpu_set_t cpus;  // can represent at most 1024 (CPU_SETSIZE) processors
  4951   cpu_set_t* cpus_p = &cpus;
  5057   cpu_set_t* cpus_p = &cpus;
  4952   int cpus_size = sizeof(cpu_set_t);
  5058   int cpus_size = sizeof(cpu_set_t);
  4953 
  5059 
  4954   int configured_cpus = processor_count();  // upper bound on available cpus
  5060   int configured_cpus = os::processor_count();  // upper bound on available cpus
  4955   int cpu_count = 0;
  5061   int cpu_count = 0;
  4956 
  5062 
  4957 // old build platforms may not support dynamic cpu sets
  5063 // old build platforms may not support dynamic cpu sets
  4958 #ifdef CPU_ALLOC
  5064 #ifdef CPU_ALLOC
  4959 
  5065 
  5012 
  5118 
  5013   if (cpus_p != &cpus) { // can only be true when CPU_ALLOC used
  5119   if (cpus_p != &cpus) { // can only be true when CPU_ALLOC used
  5014     CPU_FREE(cpus_p);
  5120     CPU_FREE(cpus_p);
  5015   }
  5121   }
  5016 
  5122 
  5017   assert(cpu_count > 0 && cpu_count <= processor_count(), "sanity check");
  5123   assert(cpu_count > 0 && cpu_count <= os::processor_count(), "sanity check");
  5018   return cpu_count;
  5124   return cpu_count;
       
  5125 }
       
  5126 
       
  5127 // Determine the active processor count from one of
       
  5128 // three different sources:
       
  5129 //
       
  5130 // 1. User option -XX:ActiveProcessorCount
       
  5131 // 2. kernel os calls (sched_getaffinity or sysconf(_SC_NPROCESSORS_ONLN)
       
  5132 // 3. extracted from cgroup cpu subsystem (shares and quotas)
       
  5133 //
       
  5134 // Option 1, if specified, will always override.
       
  5135 // If the cgroup subsystem is active and configured, we
       
  5136 // will return the min of the cgroup and option 2 results.
       
  5137 // This is required since tools, such as numactl, that
       
  5138 // alter cpu affinity do not update cgroup subsystem
       
  5139 // cpuset configuration files.
       
  5140 int os::active_processor_count() {
       
  5141   // User has overridden the number of active processors
       
  5142   if (ActiveProcessorCount > 0) {
       
  5143     log_trace(os)("active_processor_count: "
       
  5144                   "active processor count set by user : %d",
       
  5145                   ActiveProcessorCount);
       
  5146     return ActiveProcessorCount;
       
  5147   }
       
  5148 
       
  5149   int active_cpus;
       
  5150   if (OSContainer::is_containerized()) {
       
  5151     active_cpus = OSContainer::active_processor_count();
       
  5152     log_trace(os)("active_processor_count: determined by OSContainer: %d",
       
  5153                    active_cpus);
       
  5154   } else {
       
  5155     active_cpus = os::Linux::active_processor_count();
       
  5156   }
       
  5157 
       
  5158   return active_cpus;
  5019 }
  5159 }
  5020 
  5160 
  5021 void os::set_native_thread_name(const char *name) {
  5161 void os::set_native_thread_name(const char *name) {
  5022   if (Linux::_pthread_setname_np) {
  5162   if (Linux::_pthread_setname_np) {
  5023     char buf [16]; // according to glibc manpage, 16 chars incl. '/0'
  5163     char buf [16]; // according to glibc manpage, 16 chars incl. '/0'