src/share/vm/code/codeCache.cpp

Print this page




 343   }
 344 }
 345 
 346 CodeBlob* CodeCache::next_blob(CodeHeap* heap, CodeBlob* cb) {
 347   assert_locked_or_safepoint(CodeCache_lock);
 348   assert(heap != NULL, "heap is null");
 349   return (CodeBlob*)heap->next(cb);
 350 }
 351 
 352 CodeBlob* CodeCache::next_blob(CodeBlob* cb) {
 353   return next_blob(get_code_heap(cb), cb);
 354 }
 355 
 356 /**
 357  * Do not seize the CodeCache lock here--if the caller has not
 358  * already done so, we are going to lose bigtime, since the code
 359  * cache will contain a garbage CodeBlob until the caller can
 360  * run the constructor for the CodeBlob subclass he is busy
 361  * instantiating.
 362  */
 363 CodeBlob* CodeCache::allocate(int size, int code_blob_type) {
 364   // Possibly wakes up the sweeper thread.
 365   NMethodSweeper::notify(code_blob_type);
 366   assert_locked_or_safepoint(CodeCache_lock);
 367   assert(size > 0, err_msg_res("Code cache allocation request must be > 0 but is %d", size));
 368   if (size <= 0) {
 369     return NULL;
 370   }
 371   CodeBlob* cb = NULL;
 372 
 373   // Get CodeHeap for the given CodeBlobType
 374   CodeHeap* heap = get_code_heap(code_blob_type);
 375   assert(heap != NULL, "heap is null");
 376 
 377   while (true) {
 378     cb = (CodeBlob*)heap->allocate(size);
 379     if (cb != NULL) break;
 380     if (!heap->expand_by(CodeCacheExpansionSize)) {
 381       // Expansion failed
 382       if (SegmentedCodeCache && (code_blob_type == CodeBlobType::NonNMethod)) {
 383         // Fallback solution: Store non-nmethod code in the non-profiled code heap.
 384         // Note that at in the sweeper, we check the reverse_free_ratio of the non-profiled
 385         // code heap and force stack scanning if less than 10% if the code heap are free.
 386         return allocate(size, CodeBlobType::MethodNonProfiled);

















 387       }
 388       MutexUnlockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 389       CompileBroker::handle_full_code_cache(code_blob_type);
 390       return NULL;
 391     }
 392     if (PrintCodeCacheExtension) {
 393       ResourceMark rm;
 394       if (SegmentedCodeCache) {
 395         tty->print("%s", heap->name());
 396       } else {
 397         tty->print("CodeCache");
 398       }
 399       tty->print_cr(" extended to [" INTPTR_FORMAT ", " INTPTR_FORMAT "] (" SSIZE_FORMAT " bytes)",
 400                     (intptr_t)heap->low_boundary(), (intptr_t)heap->high(),
 401                     (address)heap->high() - (address)heap->low_boundary());
 402     }
 403   }
 404   print_trace("allocation", cb, size);
 405   _number_of_blobs++;
 406   return cb;




 343   }
 344 }
 345 
 346 CodeBlob* CodeCache::next_blob(CodeHeap* heap, CodeBlob* cb) {
 347   assert_locked_or_safepoint(CodeCache_lock);
 348   assert(heap != NULL, "heap is null");
 349   return (CodeBlob*)heap->next(cb);
 350 }
 351 
 352 CodeBlob* CodeCache::next_blob(CodeBlob* cb) {
 353   return next_blob(get_code_heap(cb), cb);
 354 }
 355 
 356 /**
 357  * Do not seize the CodeCache lock here--if the caller has not
 358  * already done so, we are going to lose bigtime, since the code
 359  * cache will contain a garbage CodeBlob until the caller can
 360  * run the constructor for the CodeBlob subclass he is busy
 361  * instantiating.
 362  */
 363 CodeBlob* CodeCache::allocate(int size, int code_blob_type, bool strict) {
 364   // Possibly wakes up the sweeper thread.
 365   NMethodSweeper::notify(code_blob_type);
 366   assert_locked_or_safepoint(CodeCache_lock);
 367   assert(size > 0, err_msg_res("Code cache allocation request must be > 0 but is %d", size));
 368   if (size <= 0) {
 369     return NULL;
 370   }
 371   CodeBlob* cb = NULL;
 372 
 373   // Get CodeHeap for the given CodeBlobType
 374   CodeHeap* heap = get_code_heap(code_blob_type);
 375   assert(heap != NULL, "heap is null");
 376 
 377   while (true) {
 378     cb = (CodeBlob*)heap->allocate(size);
 379     if (cb != NULL) break;
 380     if (!heap->expand_by(CodeCacheExpansionSize)) {
 381       // Expansion failed
 382       if (SegmentedCodeCache && !strict) {
 383         // Fallback solution: Try to store code in another code heap.
 384         // Note that in the sweeper, we check the reverse_free_ratio of the code heap
 385         // and force stack scanning if less than 10% of the code heap are free.
 386         int type = code_blob_type;
 387         switch (type) {
 388         case CodeBlobType::NonNMethod:
 389           type = CodeBlobType::MethodNonProfiled;
 390           strict = false;   // Allow recursive search for other heaps
 391           break;
 392         case CodeBlobType::MethodProfiled:
 393           type = CodeBlobType::MethodNonProfiled;
 394           strict = true;
 395           break;
 396         case CodeBlobType::MethodNonProfiled:
 397           type = CodeBlobType::MethodProfiled;
 398           strict = true;
 399           break;
 400         }
 401         if (heap_available(type)) {
 402           return allocate(size, type, strict);
 403         }
 404       }
 405       MutexUnlockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 406       CompileBroker::handle_full_code_cache(code_blob_type);
 407       return NULL;
 408     }
 409     if (PrintCodeCacheExtension) {
 410       ResourceMark rm;
 411       if (SegmentedCodeCache) {
 412         tty->print("%s", heap->name());
 413       } else {
 414         tty->print("CodeCache");
 415       }
 416       tty->print_cr(" extended to [" INTPTR_FORMAT ", " INTPTR_FORMAT "] (" SSIZE_FORMAT " bytes)",
 417                     (intptr_t)heap->low_boundary(), (intptr_t)heap->high(),
 418                     (address)heap->high() - (address)heap->low_boundary());
 419     }
 420   }
 421   print_trace("allocation", cb, size);
 422   _number_of_blobs++;
 423   return cb;