< prev index next >

src/os/posix/vm/os_posix.cpp

Print this page




 141 }
 142 
 143 // Multiple threads can race in this code, and can remap over each other with MAP_FIXED,
 144 // so on posix, unmap the section at the start and at the end of the chunk that we mapped
 145 // rather than unmapping and remapping the whole chunk to get requested alignment.
 146 char* os::reserve_memory_aligned(size_t size, size_t alignment) {
 147   assert((alignment & (os::vm_allocation_granularity() - 1)) == 0,
 148       "Alignment must be a multiple of allocation granularity (page size)");
 149   assert((size & (alignment -1)) == 0, "size must be 'alignment' aligned");
 150 
 151   size_t extra_size = size + alignment;
 152   assert(extra_size >= size, "overflow, size is too large to allow alignment");
 153 
 154   char* extra_base = os::reserve_memory(extra_size, NULL, alignment);
 155 
 156   if (extra_base == NULL) {
 157     return NULL;
 158   }
 159 
 160   // Do manual alignment
 161   char* aligned_base = align_ptr_up(extra_base, alignment);
 162 
 163   // [  |                                       |  ]
 164   // ^ extra_base
 165   //    ^ extra_base + begin_offset == aligned_base
 166   //     extra_base + begin_offset + size       ^
 167   //                       extra_base + extra_size ^
 168   // |<>| == begin_offset
 169   //                              end_offset == |<>|
 170   size_t begin_offset = aligned_base - extra_base;
 171   size_t end_offset = (extra_base + extra_size) - (aligned_base + size);
 172 
 173   if (begin_offset > 0) {
 174       os::release_memory(extra_base, begin_offset);
 175   }
 176 
 177   if (end_offset > 0) {
 178       os::release_memory(extra_base + begin_offset + size, end_offset);
 179   }
 180 
 181   return aligned_base;


 305   // not be allocated) or the lower limit (if the that value could be allocated)
 306   // until the difference between these limits is "small".
 307 
 308   // the minimum amount of memory we care about allocating.
 309   const julong min_allocation_size = M;
 310 
 311   julong upper_limit = *limit;
 312 
 313   // first check a few trivial cases
 314   if (is_allocatable(upper_limit) || (upper_limit <= min_allocation_size)) {
 315     *limit = upper_limit;
 316   } else if (!is_allocatable(min_allocation_size)) {
 317     // we found that not even min_allocation_size is allocatable. Return it
 318     // anyway. There is no point to search for a better value any more.
 319     *limit = min_allocation_size;
 320   } else {
 321     // perform the binary search.
 322     julong lower_limit = min_allocation_size;
 323     while ((upper_limit - lower_limit) > min_allocation_size) {
 324       julong temp_limit = ((upper_limit - lower_limit) / 2) + lower_limit;
 325       temp_limit = align_size_down_(temp_limit, min_allocation_size);
 326       if (is_allocatable(temp_limit)) {
 327         lower_limit = temp_limit;
 328       } else {
 329         upper_limit = temp_limit;
 330       }
 331     }
 332     *limit = lower_limit;
 333   }
 334   return true;
 335 #endif
 336 }
 337 
 338 const char* os::get_current_directory(char *buf, size_t buflen) {
 339   return getcwd(buf, buflen);
 340 }
 341 
 342 FILE* os::open(int fd, const char* mode) {
 343   return ::fdopen(fd, mode);
 344 }
 345 


1163 }
1164 
1165 
1166 // Check minimum allowable stack sizes for thread creation and to initialize
1167 // the java system classes, including StackOverflowError - depends on page
1168 // size.
1169 // The space needed for frames during startup is platform dependent. It
1170 // depends on word size, platform calling conventions, C frame layout and
1171 // interpreter/C1/C2 design decisions. Therefore this is given in a
1172 // platform (os/cpu) dependent constant.
1173 // To this, space for guard mechanisms is added, which depends on the
1174 // page size which again depends on the concrete system the VM is running
1175 // on. Space for libc guard pages is not included in this size.
1176 jint os::Posix::set_minimum_stack_sizes() {
1177   size_t os_min_stack_allowed = SOLARIS_ONLY(thr_min_stack()) NOT_SOLARIS(PTHREAD_STACK_MIN);
1178 
1179   _java_thread_min_stack_allowed = _java_thread_min_stack_allowed +
1180                                    JavaThread::stack_guard_zone_size() +
1181                                    JavaThread::stack_shadow_zone_size();
1182 
1183   _java_thread_min_stack_allowed = align_size_up(_java_thread_min_stack_allowed, vm_page_size());
1184   _java_thread_min_stack_allowed = MAX2(_java_thread_min_stack_allowed, os_min_stack_allowed);
1185 
1186   size_t stack_size_in_bytes = ThreadStackSize * K;
1187   if (stack_size_in_bytes != 0 &&
1188       stack_size_in_bytes < _java_thread_min_stack_allowed) {
1189     // The '-Xss' and '-XX:ThreadStackSize=N' options both set
1190     // ThreadStackSize so we go with "Java thread stack size" instead
1191     // of "ThreadStackSize" to be more friendly.
1192     tty->print_cr("\nThe Java thread stack size specified is too small. "
1193                   "Specify at least " SIZE_FORMAT "k",
1194                   _java_thread_min_stack_allowed / K);
1195     return JNI_ERR;
1196   }
1197 
1198   // Make the stack size a multiple of the page size so that
1199   // the yellow/red zones can be guarded.
1200   JavaThread::set_stack_size_at_create(round_to(stack_size_in_bytes, vm_page_size()));
1201 
1202   // Reminder: a compiler thread is a Java thread.
1203   _compiler_thread_min_stack_allowed = _compiler_thread_min_stack_allowed +
1204                                        JavaThread::stack_guard_zone_size() +
1205                                        JavaThread::stack_shadow_zone_size();
1206 
1207   _compiler_thread_min_stack_allowed = align_size_up(_compiler_thread_min_stack_allowed, vm_page_size());
1208   _compiler_thread_min_stack_allowed = MAX2(_compiler_thread_min_stack_allowed, os_min_stack_allowed);
1209 
1210   stack_size_in_bytes = CompilerThreadStackSize * K;
1211   if (stack_size_in_bytes != 0 &&
1212       stack_size_in_bytes < _compiler_thread_min_stack_allowed) {
1213     tty->print_cr("\nThe CompilerThreadStackSize specified is too small. "
1214                   "Specify at least " SIZE_FORMAT "k",
1215                   _compiler_thread_min_stack_allowed / K);
1216     return JNI_ERR;
1217   }
1218 
1219   _vm_internal_thread_min_stack_allowed = align_size_up(_vm_internal_thread_min_stack_allowed, vm_page_size());
1220   _vm_internal_thread_min_stack_allowed = MAX2(_vm_internal_thread_min_stack_allowed, os_min_stack_allowed);
1221 
1222   stack_size_in_bytes = VMThreadStackSize * K;
1223   if (stack_size_in_bytes != 0 &&
1224       stack_size_in_bytes < _vm_internal_thread_min_stack_allowed) {
1225     tty->print_cr("\nThe VMThreadStackSize specified is too small. "
1226                   "Specify at least " SIZE_FORMAT "k",
1227                   _vm_internal_thread_min_stack_allowed / K);
1228     return JNI_ERR;
1229   }
1230   return JNI_OK;
1231 }
1232 
1233 // Called when creating the thread.  The minimum stack sizes have already been calculated
1234 size_t os::Posix::get_initial_stack_size(ThreadType thr_type, size_t req_stack_size) {
1235   size_t stack_size;
1236   if (req_stack_size == 0) {
1237     stack_size = default_stack_size(thr_type);
1238   } else {
1239     stack_size = req_stack_size;


1259                       _compiler_thread_min_stack_allowed);
1260     break;
1261   case os::vm_thread:
1262   case os::pgc_thread:
1263   case os::cgc_thread:
1264   case os::watcher_thread:
1265   default:  // presume the unknown thr_type is a VM internal
1266     if (req_stack_size == 0 && VMThreadStackSize > 0) {
1267       // no requested size and we have a more specific default value
1268       stack_size = (size_t)(VMThreadStackSize * K);
1269     }
1270 
1271     stack_size = MAX2(stack_size,
1272                       _vm_internal_thread_min_stack_allowed);
1273     break;
1274   }
1275 
1276   // pthread_attr_setstacksize() may require that the size be rounded up to the OS page size.
1277   // Be careful not to round up to 0. Align down in that case.
1278   if (stack_size <= SIZE_MAX - vm_page_size()) {
1279     stack_size = align_size_up(stack_size, vm_page_size());
1280   } else {
1281     stack_size = align_size_down(stack_size, vm_page_size());
1282   }
1283 
1284   return stack_size;
1285 }
1286 
1287 os::WatcherThreadCrashProtection::WatcherThreadCrashProtection() {
1288   assert(Thread::current()->is_Watcher_thread(), "Must be WatcherThread");
1289 }
1290 
1291 /*
1292  * See the caveats for this class in os_posix.hpp
1293  * Protects the callback call so that SIGSEGV / SIGBUS jumps back into this
1294  * method and returns false. If none of the signals are raised, returns true.
1295  * The callback is supposed to provide the method that should be protected.
1296  */
1297 bool os::WatcherThreadCrashProtection::call(os::CrashProtectionCallback& cb) {
1298   sigset_t saved_sig_mask;
1299 
1300   assert(Thread::current()->is_Watcher_thread(), "Only for WatcherThread");
1301   assert(!WatcherThread::watcher_thread()->has_crash_protection(),




 141 }
 142 
 143 // Multiple threads can race in this code, and can remap over each other with MAP_FIXED,
 144 // so on posix, unmap the section at the start and at the end of the chunk that we mapped
 145 // rather than unmapping and remapping the whole chunk to get requested alignment.
 146 char* os::reserve_memory_aligned(size_t size, size_t alignment) {
 147   assert((alignment & (os::vm_allocation_granularity() - 1)) == 0,
 148       "Alignment must be a multiple of allocation granularity (page size)");
 149   assert((size & (alignment -1)) == 0, "size must be 'alignment' aligned");
 150 
 151   size_t extra_size = size + alignment;
 152   assert(extra_size >= size, "overflow, size is too large to allow alignment");
 153 
 154   char* extra_base = os::reserve_memory(extra_size, NULL, alignment);
 155 
 156   if (extra_base == NULL) {
 157     return NULL;
 158   }
 159 
 160   // Do manual alignment
 161   char* aligned_base = align_up(extra_base, alignment);
 162 
 163   // [  |                                       |  ]
 164   // ^ extra_base
 165   //    ^ extra_base + begin_offset == aligned_base
 166   //     extra_base + begin_offset + size       ^
 167   //                       extra_base + extra_size ^
 168   // |<>| == begin_offset
 169   //                              end_offset == |<>|
 170   size_t begin_offset = aligned_base - extra_base;
 171   size_t end_offset = (extra_base + extra_size) - (aligned_base + size);
 172 
 173   if (begin_offset > 0) {
 174       os::release_memory(extra_base, begin_offset);
 175   }
 176 
 177   if (end_offset > 0) {
 178       os::release_memory(extra_base + begin_offset + size, end_offset);
 179   }
 180 
 181   return aligned_base;


 305   // not be allocated) or the lower limit (if the that value could be allocated)
 306   // until the difference between these limits is "small".
 307 
 308   // the minimum amount of memory we care about allocating.
 309   const julong min_allocation_size = M;
 310 
 311   julong upper_limit = *limit;
 312 
 313   // first check a few trivial cases
 314   if (is_allocatable(upper_limit) || (upper_limit <= min_allocation_size)) {
 315     *limit = upper_limit;
 316   } else if (!is_allocatable(min_allocation_size)) {
 317     // we found that not even min_allocation_size is allocatable. Return it
 318     // anyway. There is no point to search for a better value any more.
 319     *limit = min_allocation_size;
 320   } else {
 321     // perform the binary search.
 322     julong lower_limit = min_allocation_size;
 323     while ((upper_limit - lower_limit) > min_allocation_size) {
 324       julong temp_limit = ((upper_limit - lower_limit) / 2) + lower_limit;
 325       temp_limit = align_down_(temp_limit, min_allocation_size);
 326       if (is_allocatable(temp_limit)) {
 327         lower_limit = temp_limit;
 328       } else {
 329         upper_limit = temp_limit;
 330       }
 331     }
 332     *limit = lower_limit;
 333   }
 334   return true;
 335 #endif
 336 }
 337 
 338 const char* os::get_current_directory(char *buf, size_t buflen) {
 339   return getcwd(buf, buflen);
 340 }
 341 
 342 FILE* os::open(int fd, const char* mode) {
 343   return ::fdopen(fd, mode);
 344 }
 345 


1163 }
1164 
1165 
1166 // Check minimum allowable stack sizes for thread creation and to initialize
1167 // the java system classes, including StackOverflowError - depends on page
1168 // size.
1169 // The space needed for frames during startup is platform dependent. It
1170 // depends on word size, platform calling conventions, C frame layout and
1171 // interpreter/C1/C2 design decisions. Therefore this is given in a
1172 // platform (os/cpu) dependent constant.
1173 // To this, space for guard mechanisms is added, which depends on the
1174 // page size which again depends on the concrete system the VM is running
1175 // on. Space for libc guard pages is not included in this size.
1176 jint os::Posix::set_minimum_stack_sizes() {
1177   size_t os_min_stack_allowed = SOLARIS_ONLY(thr_min_stack()) NOT_SOLARIS(PTHREAD_STACK_MIN);
1178 
1179   _java_thread_min_stack_allowed = _java_thread_min_stack_allowed +
1180                                    JavaThread::stack_guard_zone_size() +
1181                                    JavaThread::stack_shadow_zone_size();
1182 
1183   _java_thread_min_stack_allowed = align_up(_java_thread_min_stack_allowed, vm_page_size());
1184   _java_thread_min_stack_allowed = MAX2(_java_thread_min_stack_allowed, os_min_stack_allowed);
1185 
1186   size_t stack_size_in_bytes = ThreadStackSize * K;
1187   if (stack_size_in_bytes != 0 &&
1188       stack_size_in_bytes < _java_thread_min_stack_allowed) {
1189     // The '-Xss' and '-XX:ThreadStackSize=N' options both set
1190     // ThreadStackSize so we go with "Java thread stack size" instead
1191     // of "ThreadStackSize" to be more friendly.
1192     tty->print_cr("\nThe Java thread stack size specified is too small. "
1193                   "Specify at least " SIZE_FORMAT "k",
1194                   _java_thread_min_stack_allowed / K);
1195     return JNI_ERR;
1196   }
1197 
1198   // Make the stack size a multiple of the page size so that
1199   // the yellow/red zones can be guarded.
1200   JavaThread::set_stack_size_at_create(round_to(stack_size_in_bytes, vm_page_size()));
1201 
1202   // Reminder: a compiler thread is a Java thread.
1203   _compiler_thread_min_stack_allowed = _compiler_thread_min_stack_allowed +
1204                                        JavaThread::stack_guard_zone_size() +
1205                                        JavaThread::stack_shadow_zone_size();
1206 
1207   _compiler_thread_min_stack_allowed = align_up(_compiler_thread_min_stack_allowed, vm_page_size());
1208   _compiler_thread_min_stack_allowed = MAX2(_compiler_thread_min_stack_allowed, os_min_stack_allowed);
1209 
1210   stack_size_in_bytes = CompilerThreadStackSize * K;
1211   if (stack_size_in_bytes != 0 &&
1212       stack_size_in_bytes < _compiler_thread_min_stack_allowed) {
1213     tty->print_cr("\nThe CompilerThreadStackSize specified is too small. "
1214                   "Specify at least " SIZE_FORMAT "k",
1215                   _compiler_thread_min_stack_allowed / K);
1216     return JNI_ERR;
1217   }
1218 
1219   _vm_internal_thread_min_stack_allowed = align_up(_vm_internal_thread_min_stack_allowed, vm_page_size());
1220   _vm_internal_thread_min_stack_allowed = MAX2(_vm_internal_thread_min_stack_allowed, os_min_stack_allowed);
1221 
1222   stack_size_in_bytes = VMThreadStackSize * K;
1223   if (stack_size_in_bytes != 0 &&
1224       stack_size_in_bytes < _vm_internal_thread_min_stack_allowed) {
1225     tty->print_cr("\nThe VMThreadStackSize specified is too small. "
1226                   "Specify at least " SIZE_FORMAT "k",
1227                   _vm_internal_thread_min_stack_allowed / K);
1228     return JNI_ERR;
1229   }
1230   return JNI_OK;
1231 }
1232 
1233 // Called when creating the thread.  The minimum stack sizes have already been calculated
1234 size_t os::Posix::get_initial_stack_size(ThreadType thr_type, size_t req_stack_size) {
1235   size_t stack_size;
1236   if (req_stack_size == 0) {
1237     stack_size = default_stack_size(thr_type);
1238   } else {
1239     stack_size = req_stack_size;


1259                       _compiler_thread_min_stack_allowed);
1260     break;
1261   case os::vm_thread:
1262   case os::pgc_thread:
1263   case os::cgc_thread:
1264   case os::watcher_thread:
1265   default:  // presume the unknown thr_type is a VM internal
1266     if (req_stack_size == 0 && VMThreadStackSize > 0) {
1267       // no requested size and we have a more specific default value
1268       stack_size = (size_t)(VMThreadStackSize * K);
1269     }
1270 
1271     stack_size = MAX2(stack_size,
1272                       _vm_internal_thread_min_stack_allowed);
1273     break;
1274   }
1275 
1276   // pthread_attr_setstacksize() may require that the size be rounded up to the OS page size.
1277   // Be careful not to round up to 0. Align down in that case.
1278   if (stack_size <= SIZE_MAX - vm_page_size()) {
1279     stack_size = align_up(stack_size, vm_page_size());
1280   } else {
1281     stack_size = align_down(stack_size, vm_page_size());
1282   }
1283 
1284   return stack_size;
1285 }
1286 
1287 os::WatcherThreadCrashProtection::WatcherThreadCrashProtection() {
1288   assert(Thread::current()->is_Watcher_thread(), "Must be WatcherThread");
1289 }
1290 
1291 /*
1292  * See the caveats for this class in os_posix.hpp
1293  * Protects the callback call so that SIGSEGV / SIGBUS jumps back into this
1294  * method and returns false. If none of the signals are raised, returns true.
1295  * The callback is supposed to provide the method that should be protected.
1296  */
1297 bool os::WatcherThreadCrashProtection::call(os::CrashProtectionCallback& cb) {
1298   sigset_t saved_sig_mask;
1299 
1300   assert(Thread::current()->is_Watcher_thread(), "Only for WatcherThread");
1301   assert(!WatcherThread::watcher_thread()->has_crash_protection(),


< prev index next >