hotspot/src/os/posix/vm/os_posix.cpp
changeset 16605 ba13efd453bc
parent 14840 8994c2377547
child 17121 e40a97c700d9
equal deleted inserted replaced
16604:800a65bfe347 16605:ba13efd453bc
   186   st->print(name.version); st->print(" ");
   186   st->print(name.version); st->print(" ");
   187   st->print(name.machine);
   187   st->print(name.machine);
   188   st->cr();
   188   st->cr();
   189 }
   189 }
   190 
   190 
   191 
   191 bool os::has_allocatable_memory_limit(julong* limit) {
       
   192   struct rlimit rlim;
       
   193   int getrlimit_res = getrlimit(RLIMIT_AS, &rlim);
       
   194   // if there was an error when calling getrlimit, assume that there is no limitation
       
   195   // on virtual memory.
       
   196   bool result;
       
   197   if ((getrlimit_res != 0) || (rlim.rlim_cur == RLIM_INFINITY)) {
       
   198     result = false;
       
   199   } else {
       
   200     *limit = (julong)rlim.rlim_cur;
       
   201     result = true;
       
   202   }
       
   203 #ifdef _LP64
       
   204   return result;
       
   205 #else
       
   206   // arbitrary virtual space limit for 32 bit Unices found by testing. If
       
   207   // getrlimit above returned a limit, bound it with this limit. Otherwise
       
   208   // directly use it.
       
   209   const julong max_virtual_limit = (julong)3800*M;
       
   210   if (result) {
       
   211     *limit = MIN2(*limit, max_virtual_limit);
       
   212   } else {
       
   213     *limit = max_virtual_limit;
       
   214   }
       
   215 
       
   216   // bound by actually allocatable memory. The algorithm uses two bounds, an
       
   217   // upper and a lower limit. The upper limit is the current highest amount of
       
   218   // memory that could not be allocated, the lower limit is the current highest
       
   219   // amount of memory that could be allocated.
       
   220   // The algorithm iteratively refines the result by halving the difference
       
   221   // between these limits, updating either the upper limit (if that value could
       
   222   // not be allocated) or the lower limit (if the that value could be allocated)
       
   223   // until the difference between these limits is "small".
       
   224 
       
   225   // the minimum amount of memory we care about allocating.
       
   226   const julong min_allocation_size = M;
       
   227 
       
   228   julong upper_limit = *limit;
       
   229 
       
   230   // first check a few trivial cases
       
   231   if (is_allocatable(upper_limit) || (upper_limit <= min_allocation_size)) {
       
   232     *limit = upper_limit;
       
   233   } else if (!is_allocatable(min_allocation_size)) {
       
   234     // we found that not even min_allocation_size is allocatable. Return it
       
   235     // anyway. There is no point to search for a better value any more.
       
   236     *limit = min_allocation_size;
       
   237   } else {
       
   238     // perform the binary search.
       
   239     julong lower_limit = min_allocation_size;
       
   240     while ((upper_limit - lower_limit) > min_allocation_size) {
       
   241       julong temp_limit = ((upper_limit - lower_limit) / 2) + lower_limit;
       
   242       temp_limit = align_size_down_(temp_limit, min_allocation_size);
       
   243       if (is_allocatable(temp_limit)) {
       
   244         lower_limit = temp_limit;
       
   245       } else {
       
   246         upper_limit = temp_limit;
       
   247       }
       
   248     }
       
   249     *limit = lower_limit;
       
   250   }
       
   251   return true;
       
   252 #endif
       
   253 }