src/share/vm/gc_implementation/g1/g1CollectedHeap.cpp

Print this page
rev 4801 : imported patch code-movement
rev 4802 : imported patch optimize-nmethod-scanning
rev 4803 : imported patch thomas-comments-2


1159                                                       false /* bot_updates */);
1160   } else {
1161     HeapWord* result = humongous_obj_allocate(word_size);
1162     if (result != NULL && g1_policy()->need_to_start_conc_mark("STW humongous allocation")) {
1163       g1_policy()->set_initiate_conc_mark_if_possible();
1164     }
1165     return result;
1166   }
1167 
1168   ShouldNotReachHere();
1169 }
1170 
1171 class PostMCRemSetClearClosure: public HeapRegionClosure {
1172   G1CollectedHeap* _g1h;
1173   ModRefBarrierSet* _mr_bs;
1174 public:
1175   PostMCRemSetClearClosure(G1CollectedHeap* g1h, ModRefBarrierSet* mr_bs) :
1176     _g1h(g1h), _mr_bs(mr_bs) {}
1177 
1178   bool doHeapRegion(HeapRegion* r) {


1179     if (r->continuesHumongous()) {
1180       // We'll assert that the strong code root list is empty
1181       assert(r->strong_code_root_list()->is_empty(), "sanity");
1182       return false;
1183     }

1184     _g1h->reset_gc_time_stamps(r);
1185     HeapRegionRemSet* hrrs = r->rem_set();
1186     if (hrrs != NULL) hrrs->clear();
1187     // You might think here that we could clear just the cards
1188     // corresponding to the used region.  But no: if we leave a dirty card
1189     // in a region we might allocate into, then it would prevent that card
1190     // from being enqueued, and cause it to be missed.
1191     // Re: the performance cost: we shouldn't be doing full GC anyway!
1192     _mr_bs->clear(MemRegion(r->bottom(), r->end()));
1193 
1194     // We'll also clear the strong code root list for this region
1195     r->strong_code_root_list()->clear();
1196 
1197     return false;
1198   }
1199 };
1200 
1201 void G1CollectedHeap::clear_rsets_post_compaction() {
1202   PostMCRemSetClearClosure rs_clear(this, mr_bs());
1203   heap_region_iterate(&rs_clear);
1204 }
1205 
1206 class RebuildRSOutOfRegionClosure: public HeapRegionClosure {
1207   G1CollectedHeap*   _g1h;
1208   UpdateRSOopClosure _cl;
1209   int                _worker_i;
1210 public:
1211   RebuildRSOutOfRegionClosure(G1CollectedHeap* g1, int worker_i = 0) :
1212     _cl(g1->g1_rem_set(), worker_i),
1213     _worker_i(worker_i),
1214     _g1h(g1)
1215   { }
1216 


3090     if (!G1VerifyHeapRegionCodeRoots) {
3091       // We're not verifying the code roots attached to heap region.
3092       return;
3093     }
3094 
3095     // Don't check the code roots during marking verification in a full GC
3096     if (_vo == VerifyOption_G1UseMarkWord) {
3097       return;
3098     }
3099 
3100     // Now verify that the current nmethod (which contains p) is
3101     // in the code root list of the heap region containing the
3102     // object referenced by p.
3103 
3104     T heap_oop = oopDesc::load_heap_oop(p);
3105     if (!oopDesc::is_null(heap_oop)) {
3106       oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
3107       
3108       // Now fetch the region containing the object
3109       HeapRegion* hr = _g1h->heap_region_containing(obj);

3110       // Verify that the strong code root list for this region
3111       // contains the nmethod
3112       if (!hr->strong_code_root_list()->contains(_nm)) {
3113         gclog_or_tty->print_cr("Code root location "PTR_FORMAT" "
3114                               "from nmethod "PTR_FORMAT" not in strong "
3115                               "code roots for region ["PTR_FORMAT","PTR_FORMAT")",
3116                               p, _nm, hr->bottom(), hr->end());
3117         _failures = true;
3118       }
3119     }
3120   }
3121 
3122 public:
3123   G1VerifyCodeRootOopClosure(G1CollectedHeap* g1h, OopClosure* root_cl, VerifyOption vo):
3124     _g1h(g1h), _root_cl(root_cl), _vo(vo), _nm(NULL), _failures(false) {}
3125 
3126   void do_oop(oop* p) { do_oop_work(p); }
3127   void do_oop(narrowOop* p) { do_oop_work(p); }
3128 
3129   void set_nmethod(nmethod* nm) { _nm = nm; }
3130   bool failures() { return _failures; }
3131 };
3132 


6541   _free_list.verify_end();
6542 }
6543 
6544 // Optimized nmethod scanning
6545 
6546 class RegisterNMethodOopClosure: public OopClosure {
6547   G1CollectedHeap* _g1h;
6548   nmethod* _nm;
6549 
6550   template <class T> void do_oop_work(T* p) {
6551     T heap_oop = oopDesc::load_heap_oop(p);
6552     if (!oopDesc::is_null(heap_oop)) {
6553       oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
6554       HeapRegion* hr = _g1h->heap_region_containing(obj);
6555       assert(!hr->isHumongous(), "code root in humongous region?");
6556 
6557       // HeapRegion::add_strong_code_root() avoids adding duplicate
6558       // entries but having duplicates is  OK since we "mark" nmethods
6559       // as visited when we scan the strong code root lists during the GC.
6560       hr->add_strong_code_root(_nm);
6561       assert(hr->strong_code_root_list()->contains(_nm), "push failed?");
6562     }
6563   }
6564 
6565 public:
6566   RegisterNMethodOopClosure(G1CollectedHeap* g1h, nmethod* nm) :
6567     _g1h(g1h), _nm(nm) {}
6568 
6569   void do_oop(oop* p)       { do_oop_work(p); }
6570   void do_oop(narrowOop* p) { do_oop_work(p); }
6571 };
6572 
6573 class UnregisterNMethodOopClosure: public OopClosure {
6574   G1CollectedHeap* _g1h;
6575   nmethod* _nm;
6576 
6577   template <class T> void do_oop_work(T* p) {
6578     T heap_oop = oopDesc::load_heap_oop(p);
6579     if (!oopDesc::is_null(heap_oop)) {
6580       oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
6581       HeapRegion* hr = _g1h->heap_region_containing(obj);
6582       assert(!hr->isHumongous(), "code root in humongous region?");
6583       hr->remove_strong_code_root(_nm);
6584       assert(!hr->strong_code_root_list()->contains(_nm), "remove failed?");
6585     }
6586   }
6587 
6588 public:
6589   UnregisterNMethodOopClosure(G1CollectedHeap* g1h, nmethod* nm) :
6590     _g1h(g1h), _nm(nm) {}
6591 
6592   void do_oop(oop* p)       { do_oop_work(p); }
6593   void do_oop(narrowOop* p) { do_oop_work(p); }
6594 };
6595 
6596 void G1CollectedHeap::register_nmethod(nmethod* nm) {
6597   guarantee(nm != NULL, "sanity");
6598   RegisterNMethodOopClosure reg_cl(this, nm);
6599   nm->oops_do(&reg_cl);
6600 }
6601 
6602 void G1CollectedHeap::unregister_nmethod(nmethod* nm) {
6603   guarantee(nm != NULL, "sanity");
6604   UnregisterNMethodOopClosure reg_cl(this, nm);


6670   MarkStrongCodeRootCodeBlobClosure(ConcurrentMark* cm, HeapRegion* hr, uint worker_id):
6671     _oop_cl(cm, hr, worker_id) {}
6672 
6673   void do_code_blob(CodeBlob* cb) {
6674     nmethod* nm = (cb == NULL) ? NULL : cb->as_nmethod_or_null();
6675     if (nm != NULL) {
6676       nm->oops_do(&_oop_cl);
6677     }
6678   }
6679 };
6680 
6681 class MarkStrongCodeRootsHRClosure: public HeapRegionClosure {
6682   G1CollectedHeap* _g1h;
6683   uint _worker_id;
6684 
6685 public:
6686   MarkStrongCodeRootsHRClosure(G1CollectedHeap* g1h, uint worker_id) :
6687     _g1h(g1h), _worker_id(worker_id) {}
6688 
6689   bool doHeapRegion(HeapRegion *hr) {

6690     if (hr->isHumongous()) {
6691       // Code roots should never be attached to a humongous region
6692       assert(hr->strong_code_root_list()->is_empty(), "sanity");
6693       return false;
6694     }
6695 
6696     if (hr->in_collection_set()) {
6697       // Don't mark code roots into regions in the collection set here.
6698       // They will be marked when we scan them.
6699       return false;
6700     }
6701 
6702     MarkStrongCodeRootCodeBlobClosure cb_cl(_g1h->concurrent_mark(), hr, _worker_id);
6703     hr->strong_code_roots_do(&cb_cl);
6704     return false;
6705   }
6706 };
6707 
6708 void G1CollectedHeap::mark_strong_code_roots(uint worker_id) {
6709   MarkStrongCodeRootsHRClosure cl(this, worker_id);
6710   heap_region_par_iterate_chunked(&cl,
6711                                   worker_id,
6712                                   workers()->active_workers(),




1159                                                       false /* bot_updates */);
1160   } else {
1161     HeapWord* result = humongous_obj_allocate(word_size);
1162     if (result != NULL && g1_policy()->need_to_start_conc_mark("STW humongous allocation")) {
1163       g1_policy()->set_initiate_conc_mark_if_possible();
1164     }
1165     return result;
1166   }
1167 
1168   ShouldNotReachHere();
1169 }
1170 
1171 class PostMCRemSetClearClosure: public HeapRegionClosure {
1172   G1CollectedHeap* _g1h;
1173   ModRefBarrierSet* _mr_bs;
1174 public:
1175   PostMCRemSetClearClosure(G1CollectedHeap* g1h, ModRefBarrierSet* mr_bs) :
1176     _g1h(g1h), _mr_bs(mr_bs) {}
1177 
1178   bool doHeapRegion(HeapRegion* r) {
1179     HeapRegionRemSet* hrrs = r->rem_set();
1180 
1181     if (r->continuesHumongous()) {
1182       // We'll assert that the strong code root list is empty
1183       assert(hrrs->strong_code_roots_list_length() == 0, "sanity");
1184       return false;
1185     }
1186 
1187     _g1h->reset_gc_time_stamps(r);
1188     hrrs->clear();

1189     // You might think here that we could clear just the cards
1190     // corresponding to the used region.  But no: if we leave a dirty card
1191     // in a region we might allocate into, then it would prevent that card
1192     // from being enqueued, and cause it to be missed.
1193     // Re: the performance cost: we shouldn't be doing full GC anyway!
1194     _mr_bs->clear(MemRegion(r->bottom(), r->end()));
1195 



1196     return false;
1197   }
1198 };
1199 
1200 void G1CollectedHeap::clear_rsets_post_compaction() {
1201   PostMCRemSetClearClosure rs_clear(this, mr_bs());
1202   heap_region_iterate(&rs_clear);
1203 }
1204 
1205 class RebuildRSOutOfRegionClosure: public HeapRegionClosure {
1206   G1CollectedHeap*   _g1h;
1207   UpdateRSOopClosure _cl;
1208   int                _worker_i;
1209 public:
1210   RebuildRSOutOfRegionClosure(G1CollectedHeap* g1, int worker_i = 0) :
1211     _cl(g1->g1_rem_set(), worker_i),
1212     _worker_i(worker_i),
1213     _g1h(g1)
1214   { }
1215 


3089     if (!G1VerifyHeapRegionCodeRoots) {
3090       // We're not verifying the code roots attached to heap region.
3091       return;
3092     }
3093 
3094     // Don't check the code roots during marking verification in a full GC
3095     if (_vo == VerifyOption_G1UseMarkWord) {
3096       return;
3097     }
3098 
3099     // Now verify that the current nmethod (which contains p) is
3100     // in the code root list of the heap region containing the
3101     // object referenced by p.
3102 
3103     T heap_oop = oopDesc::load_heap_oop(p);
3104     if (!oopDesc::is_null(heap_oop)) {
3105       oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
3106       
3107       // Now fetch the region containing the object
3108       HeapRegion* hr = _g1h->heap_region_containing(obj);
3109       HeapRegionRemSet* hrrs = hr->rem_set();
3110       // Verify that the strong code root list for this region
3111       // contains the nmethod
3112       if (!hrrs->strong_code_roots_list_contains(_nm)) {
3113         gclog_or_tty->print_cr("Code root location "PTR_FORMAT" "
3114                               "from nmethod "PTR_FORMAT" not in strong "
3115                               "code roots for region ["PTR_FORMAT","PTR_FORMAT")",
3116                               p, _nm, hr->bottom(), hr->end());
3117         _failures = true;
3118       }
3119     }
3120   }
3121 
3122 public:
3123   G1VerifyCodeRootOopClosure(G1CollectedHeap* g1h, OopClosure* root_cl, VerifyOption vo):
3124     _g1h(g1h), _root_cl(root_cl), _vo(vo), _nm(NULL), _failures(false) {}
3125 
3126   void do_oop(oop* p) { do_oop_work(p); }
3127   void do_oop(narrowOop* p) { do_oop_work(p); }
3128 
3129   void set_nmethod(nmethod* nm) { _nm = nm; }
3130   bool failures() { return _failures; }
3131 };
3132 


6541   _free_list.verify_end();
6542 }
6543 
6544 // Optimized nmethod scanning
6545 
6546 class RegisterNMethodOopClosure: public OopClosure {
6547   G1CollectedHeap* _g1h;
6548   nmethod* _nm;
6549 
6550   template <class T> void do_oop_work(T* p) {
6551     T heap_oop = oopDesc::load_heap_oop(p);
6552     if (!oopDesc::is_null(heap_oop)) {
6553       oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
6554       HeapRegion* hr = _g1h->heap_region_containing(obj);
6555       assert(!hr->isHumongous(), "code root in humongous region?");
6556 
6557       // HeapRegion::add_strong_code_root() avoids adding duplicate
6558       // entries but having duplicates is  OK since we "mark" nmethods
6559       // as visited when we scan the strong code root lists during the GC.
6560       hr->add_strong_code_root(_nm);
6561       assert(hr->rem_set()->strong_code_roots_list_contains(_nm), "add failed?");
6562     }
6563   }
6564 
6565 public:
6566   RegisterNMethodOopClosure(G1CollectedHeap* g1h, nmethod* nm) :
6567     _g1h(g1h), _nm(nm) {}
6568 
6569   void do_oop(oop* p)       { do_oop_work(p); }
6570   void do_oop(narrowOop* p) { do_oop_work(p); }
6571 };
6572 
6573 class UnregisterNMethodOopClosure: public OopClosure {
6574   G1CollectedHeap* _g1h;
6575   nmethod* _nm;
6576 
6577   template <class T> void do_oop_work(T* p) {
6578     T heap_oop = oopDesc::load_heap_oop(p);
6579     if (!oopDesc::is_null(heap_oop)) {
6580       oop obj = oopDesc::decode_heap_oop_not_null(heap_oop);
6581       HeapRegion* hr = _g1h->heap_region_containing(obj);
6582       assert(!hr->isHumongous(), "code root in humongous region?");
6583       hr->remove_strong_code_root(_nm);
6584       assert(!hr->rem_set()->strong_code_roots_list_contains(_nm), "remove failed?");
6585     }
6586   }
6587 
6588 public:
6589   UnregisterNMethodOopClosure(G1CollectedHeap* g1h, nmethod* nm) :
6590     _g1h(g1h), _nm(nm) {}
6591 
6592   void do_oop(oop* p)       { do_oop_work(p); }
6593   void do_oop(narrowOop* p) { do_oop_work(p); }
6594 };
6595 
6596 void G1CollectedHeap::register_nmethod(nmethod* nm) {
6597   guarantee(nm != NULL, "sanity");
6598   RegisterNMethodOopClosure reg_cl(this, nm);
6599   nm->oops_do(&reg_cl);
6600 }
6601 
6602 void G1CollectedHeap::unregister_nmethod(nmethod* nm) {
6603   guarantee(nm != NULL, "sanity");
6604   UnregisterNMethodOopClosure reg_cl(this, nm);


6670   MarkStrongCodeRootCodeBlobClosure(ConcurrentMark* cm, HeapRegion* hr, uint worker_id):
6671     _oop_cl(cm, hr, worker_id) {}
6672 
6673   void do_code_blob(CodeBlob* cb) {
6674     nmethod* nm = (cb == NULL) ? NULL : cb->as_nmethod_or_null();
6675     if (nm != NULL) {
6676       nm->oops_do(&_oop_cl);
6677     }
6678   }
6679 };
6680 
6681 class MarkStrongCodeRootsHRClosure: public HeapRegionClosure {
6682   G1CollectedHeap* _g1h;
6683   uint _worker_id;
6684 
6685 public:
6686   MarkStrongCodeRootsHRClosure(G1CollectedHeap* g1h, uint worker_id) :
6687     _g1h(g1h), _worker_id(worker_id) {}
6688 
6689   bool doHeapRegion(HeapRegion *hr) {
6690     HeapRegionRemSet* hrrs = hr->rem_set();
6691     if (hr->isHumongous()) {
6692       // Code roots should never be attached to a humongous region
6693       assert(hrrs->strong_code_roots_list_length() == 0, "sanity");
6694       return false;
6695     }
6696 
6697     if (hr->in_collection_set()) {
6698       // Don't mark code roots into regions in the collection set here.
6699       // They will be marked when we scan them.
6700       return false;
6701     }
6702 
6703     MarkStrongCodeRootCodeBlobClosure cb_cl(_g1h->concurrent_mark(), hr, _worker_id);
6704     hr->strong_code_roots_do(&cb_cl);
6705     return false;
6706   }
6707 };
6708 
6709 void G1CollectedHeap::mark_strong_code_roots(uint worker_id) {
6710   MarkStrongCodeRootsHRClosure cl(this, worker_id);
6711   heap_region_par_iterate_chunked(&cl,
6712                                   worker_id,
6713                                   workers()->active_workers(),