797 // complete compaction phase than we've tried so far might be |
797 // complete compaction phase than we've tried so far might be |
798 // appropriate. |
798 // appropriate. |
799 return NULL; |
799 return NULL; |
800 } |
800 } |
801 |
801 |
802 MetaWord* CollectorPolicy::satisfy_failed_metadata_allocation( |
|
803 ClassLoaderData* loader_data, |
|
804 size_t word_size, |
|
805 Metaspace::MetadataType mdtype) { |
|
806 uint loop_count = 0; |
|
807 uint gc_count = 0; |
|
808 uint full_gc_count = 0; |
|
809 |
|
810 assert(!Heap_lock->owned_by_self(), "Should not be holding the Heap_lock"); |
|
811 |
|
812 do { |
|
813 MetaWord* result = loader_data->metaspace_non_null()->allocate(word_size, mdtype); |
|
814 if (result != NULL) { |
|
815 return result; |
|
816 } |
|
817 |
|
818 if (GCLocker::is_active_and_needs_gc()) { |
|
819 // If the GCLocker is active, just expand and allocate. |
|
820 // If that does not succeed, wait if this thread is not |
|
821 // in a critical section itself. |
|
822 result = |
|
823 loader_data->metaspace_non_null()->expand_and_allocate(word_size, |
|
824 mdtype); |
|
825 if (result != NULL) { |
|
826 return result; |
|
827 } |
|
828 JavaThread* jthr = JavaThread::current(); |
|
829 if (!jthr->in_critical()) { |
|
830 // Wait for JNI critical section to be exited |
|
831 GCLocker::stall_until_clear(); |
|
832 // The GC invoked by the last thread leaving the critical |
|
833 // section will be a young collection and a full collection |
|
834 // is (currently) needed for unloading classes so continue |
|
835 // to the next iteration to get a full GC. |
|
836 continue; |
|
837 } else { |
|
838 if (CheckJNICalls) { |
|
839 fatal("Possible deadlock due to allocating while" |
|
840 " in jni critical section"); |
|
841 } |
|
842 return NULL; |
|
843 } |
|
844 } |
|
845 |
|
846 { // Need lock to get self consistent gc_count's |
|
847 MutexLocker ml(Heap_lock); |
|
848 gc_count = Universe::heap()->total_collections(); |
|
849 full_gc_count = Universe::heap()->total_full_collections(); |
|
850 } |
|
851 |
|
852 // Generate a VM operation |
|
853 VM_CollectForMetadataAllocation op(loader_data, |
|
854 word_size, |
|
855 mdtype, |
|
856 gc_count, |
|
857 full_gc_count, |
|
858 GCCause::_metadata_GC_threshold); |
|
859 VMThread::execute(&op); |
|
860 |
|
861 // If GC was locked out, try again. Check before checking success because the |
|
862 // prologue could have succeeded and the GC still have been locked out. |
|
863 if (op.gc_locked()) { |
|
864 continue; |
|
865 } |
|
866 |
|
867 if (op.prologue_succeeded()) { |
|
868 return op.result(); |
|
869 } |
|
870 loop_count++; |
|
871 if ((QueuedAllocationWarningCount > 0) && |
|
872 (loop_count % QueuedAllocationWarningCount == 0)) { |
|
873 log_warning(gc, ergo)("satisfy_failed_metadata_allocation() retries %d times," |
|
874 " size=" SIZE_FORMAT, loop_count, word_size); |
|
875 } |
|
876 } while (true); // Until a GC is done |
|
877 } |
|
878 |
|
879 // Return true if any of the following is true: |
802 // Return true if any of the following is true: |
880 // . the allocation won't fit into the current young gen heap |
803 // . the allocation won't fit into the current young gen heap |
881 // . gc locker is occupied (jni critical section) |
804 // . gc locker is occupied (jni critical section) |
882 // . heap memory is tight -- the most recent previous collection |
805 // . heap memory is tight -- the most recent previous collection |
883 // was a full collection because a partial collection (would |
806 // was a full collection because a partial collection (would |