< prev index next >

src/share/vm/gc/g1/g1AllocRegion.cpp

Print this page




  74       // If the allocation was successful we should fill in the space.
  75       CollectedHeap::fill_with_object(dummy, free_word_size);
  76       alloc_region->set_pre_dummy_top(dummy);
  77       result += free_word_size * HeapWordSize;
  78       break;
  79     }
  80 
  81     free_word_size = alloc_region->free() / HeapWordSize;
  82     // It's also possible that someone else beats us to the
  83     // allocation and they fill up the region. In that case, we can
  84     // just get out of the loop.
  85   }
  86   result += alloc_region->free();
  87 
  88   assert(alloc_region->free() / HeapWordSize < min_word_size_to_fill,
  89          "post-condition");
  90   return result;
  91 }
  92 
  93 size_t G1AllocRegion::retire(bool fill_up) {
  94   assert(_alloc_region != NULL, ar_ext_msg(this, "not initialized properly"));
  95 
  96   size_t result = 0;
  97 
  98   trace("retiring");
  99   HeapRegion* alloc_region = _alloc_region;
 100   if (alloc_region != _dummy_region) {
 101     // We never have to check whether the active region is empty or not,
 102     // and potentially free it if it is, given that it's guaranteed that
 103     // it will never be empty.
 104     assert(!alloc_region->is_empty(),
 105            ar_ext_msg(this, "the alloc region should never be empty"));
 106 
 107     if (fill_up) {
 108       result = fill_up_remaining_space(alloc_region, _bot_updates);
 109     }
 110 
 111     assert(alloc_region->used() >= _used_bytes_before,
 112            ar_ext_msg(this, "invariant"));
 113     size_t allocated_bytes = alloc_region->used() - _used_bytes_before;
 114     retire_region(alloc_region, allocated_bytes);
 115     _used_bytes_before = 0;
 116     _alloc_region = _dummy_region;
 117   }
 118   trace("retired");
 119 
 120   return result;
 121 }
 122 
 123 HeapWord* G1AllocRegion::new_alloc_region_and_allocate(size_t word_size,
 124                                                        bool force) {
 125   assert(_alloc_region == _dummy_region, ar_ext_msg(this, "pre-condition"));
 126   assert(_used_bytes_before == 0, ar_ext_msg(this, "pre-condition"));
 127 
 128   trace("attempting region allocation");
 129   HeapRegion* new_alloc_region = allocate_new_region(word_size, force);
 130   if (new_alloc_region != NULL) {
 131     new_alloc_region->reset_pre_dummy_top();
 132     // Need to do this before the allocation
 133     _used_bytes_before = new_alloc_region->used();
 134     HeapWord* result = allocate(new_alloc_region, word_size, _bot_updates);
 135     assert(result != NULL, ar_ext_msg(this, "the allocation should succeeded"));
 136 
 137     OrderAccess::storestore();
 138     // Note that we first perform the allocation and then we store the
 139     // region in _alloc_region. This is the reason why an active region
 140     // can never be empty.
 141     update_alloc_region(new_alloc_region);
 142     trace("region allocation successful");
 143     return result;
 144   } else {
 145     trace("region allocation failed");
 146     return NULL;
 147   }
 148   ShouldNotReachHere();
 149 }
 150 
 151 void G1AllocRegion::fill_in_ext_msg(ar_ext_msg* msg, const char* message) {
 152   msg->append("[%s] %s c: %u b: %s r: " PTR_FORMAT " u: " SIZE_FORMAT,
 153               _name, message, _count, BOOL_TO_STR(_bot_updates),
 154               p2i(_alloc_region), _used_bytes_before);
 155 }
 156 
 157 void G1AllocRegion::init() {
 158   trace("initializing");
 159   assert(_alloc_region == NULL && _used_bytes_before == 0,
 160          ar_ext_msg(this, "pre-condition"));
 161   assert(_dummy_region != NULL, ar_ext_msg(this, "should have been set"));
 162   _alloc_region = _dummy_region;
 163   _count = 0;
 164   trace("initialized");
 165 }
 166 
 167 void G1AllocRegion::set(HeapRegion* alloc_region) {
 168   trace("setting");
 169   // We explicitly check that the region is not empty to make sure we
 170   // maintain the "the alloc region cannot be empty" invariant.
 171   assert(alloc_region != NULL && !alloc_region->is_empty(),
 172          ar_ext_msg(this, "pre-condition"));
 173   assert(_alloc_region == _dummy_region &&
 174          _used_bytes_before == 0 && _count == 0,
 175          ar_ext_msg(this, "pre-condition"));
 176 
 177   _used_bytes_before = alloc_region->used();
 178   _alloc_region = alloc_region;
 179   _count += 1;
 180   trace("set");
 181 }
 182 
 183 void G1AllocRegion::update_alloc_region(HeapRegion* alloc_region) {
 184   trace("update");
 185   // We explicitly check that the region is not empty to make sure we
 186   // maintain the "the alloc region cannot be empty" invariant.
 187   assert(alloc_region != NULL && !alloc_region->is_empty(),
 188          ar_ext_msg(this, "pre-condition"));
 189 
 190   _alloc_region = alloc_region;
 191   _alloc_region->set_allocation_context(allocation_context());
 192   _count += 1;
 193   trace("updated");
 194 }
 195 
 196 HeapRegion* G1AllocRegion::release() {
 197   trace("releasing");
 198   HeapRegion* alloc_region = _alloc_region;
 199   retire(false /* fill_up */);
 200   assert(_alloc_region == _dummy_region,
 201          ar_ext_msg(this, "post-condition of retire()"));
 202   _alloc_region = NULL;
 203   trace("released");
 204   return (alloc_region == _dummy_region) ? NULL : alloc_region;
 205 }
 206 
 207 #if G1_ALLOC_REGION_TRACING
 208 void G1AllocRegion::trace(const char* str, size_t min_word_size, size_t desired_word_size, size_t actual_word_size, HeapWord* result) {
 209   // All the calls to trace that set either just the size or the size
 210   // and the result are considered part of level 2 tracing and are
 211   // skipped during level 1 tracing.
 212   if ((actual_word_size == 0 && result == NULL) || (G1_ALLOC_REGION_TRACING > 1)) {
 213     const size_t buffer_length = 128;
 214     char hr_buffer[buffer_length];
 215     char rest_buffer[buffer_length];
 216 
 217     HeapRegion* alloc_region = _alloc_region;
 218     if (alloc_region == NULL) {
 219       jio_snprintf(hr_buffer, buffer_length, "NULL");
 220     } else if (alloc_region == _dummy_region) {
 221       jio_snprintf(hr_buffer, buffer_length, "DUMMY");




  74       // If the allocation was successful we should fill in the space.
  75       CollectedHeap::fill_with_object(dummy, free_word_size);
  76       alloc_region->set_pre_dummy_top(dummy);
  77       result += free_word_size * HeapWordSize;
  78       break;
  79     }
  80 
  81     free_word_size = alloc_region->free() / HeapWordSize;
  82     // It's also possible that someone else beats us to the
  83     // allocation and they fill up the region. In that case, we can
  84     // just get out of the loop.
  85   }
  86   result += alloc_region->free();
  87 
  88   assert(alloc_region->free() / HeapWordSize < min_word_size_to_fill,
  89          "post-condition");
  90   return result;
  91 }
  92 
  93 size_t G1AllocRegion::retire(bool fill_up) {
  94   G1_ALLOC_REGION_ASSERT(_alloc_region != NULL, "not initialized properly");
  95 
  96   size_t result = 0;
  97 
  98   trace("retiring");
  99   HeapRegion* alloc_region = _alloc_region;
 100   if (alloc_region != _dummy_region) {
 101     // We never have to check whether the active region is empty or not,
 102     // and potentially free it if it is, given that it's guaranteed that
 103     // it will never be empty.
 104     G1_ALLOC_REGION_ASSERT(!alloc_region->is_empty(),
 105                            "the alloc region should never be empty");
 106 
 107     if (fill_up) {
 108       result = fill_up_remaining_space(alloc_region, _bot_updates);
 109     }
 110 
 111     G1_ALLOC_REGION_ASSERT(alloc_region->used() >= _used_bytes_before, "invariant");

 112     size_t allocated_bytes = alloc_region->used() - _used_bytes_before;
 113     retire_region(alloc_region, allocated_bytes);
 114     _used_bytes_before = 0;
 115     _alloc_region = _dummy_region;
 116   }
 117   trace("retired");
 118 
 119   return result;
 120 }
 121 
 122 HeapWord* G1AllocRegion::new_alloc_region_and_allocate(size_t word_size,
 123                                                        bool force) {
 124   G1_ALLOC_REGION_ASSERT(_alloc_region == _dummy_region, "pre-condition");
 125   G1_ALLOC_REGION_ASSERT(_used_bytes_before == 0, "pre-condition");
 126 
 127   trace("attempting region allocation");
 128   HeapRegion* new_alloc_region = allocate_new_region(word_size, force);
 129   if (new_alloc_region != NULL) {
 130     new_alloc_region->reset_pre_dummy_top();
 131     // Need to do this before the allocation
 132     _used_bytes_before = new_alloc_region->used();
 133     HeapWord* result = allocate(new_alloc_region, word_size, _bot_updates);
 134     G1_ALLOC_REGION_ASSERT(result != NULL, "the allocation should succeeded");
 135 
 136     OrderAccess::storestore();
 137     // Note that we first perform the allocation and then we store the
 138     // region in _alloc_region. This is the reason why an active region
 139     // can never be empty.
 140     update_alloc_region(new_alloc_region);
 141     trace("region allocation successful");
 142     return result;
 143   } else {
 144     trace("region allocation failed");
 145     return NULL;
 146   }
 147   ShouldNotReachHere();
 148 }
 149 






 150 void G1AllocRegion::init() {
 151   trace("initializing");
 152   G1_ALLOC_REGION_ASSERT(_alloc_region == NULL && _used_bytes_before == 0, "pre-condition");
 153   G1_ALLOC_REGION_ASSERT(_dummy_region != NULL, "should have been set");

 154   _alloc_region = _dummy_region;
 155   _count = 0;
 156   trace("initialized");
 157 }
 158 
 159 void G1AllocRegion::set(HeapRegion* alloc_region) {
 160   trace("setting");
 161   // We explicitly check that the region is not empty to make sure we
 162   // maintain the "the alloc region cannot be empty" invariant.
 163   G1_ALLOC_REGION_ASSERT(alloc_region != NULL && !alloc_region->is_empty(), "pre-condition");
 164   G1_ALLOC_REGION_ASSERT(_alloc_region == _dummy_region &&

 165                          _used_bytes_before == 0 && _count == 0,
 166                          "pre-condition");
 167 
 168   _used_bytes_before = alloc_region->used();
 169   _alloc_region = alloc_region;
 170   _count += 1;
 171   trace("set");
 172 }
 173 
 174 void G1AllocRegion::update_alloc_region(HeapRegion* alloc_region) {
 175   trace("update");
 176   // We explicitly check that the region is not empty to make sure we
 177   // maintain the "the alloc region cannot be empty" invariant.
 178   G1_ALLOC_REGION_ASSERT(alloc_region != NULL && !alloc_region->is_empty(), "pre-condition");

 179 
 180   _alloc_region = alloc_region;
 181   _alloc_region->set_allocation_context(allocation_context());
 182   _count += 1;
 183   trace("updated");
 184 }
 185 
 186 HeapRegion* G1AllocRegion::release() {
 187   trace("releasing");
 188   HeapRegion* alloc_region = _alloc_region;
 189   retire(false /* fill_up */);
 190   G1_ALLOC_REGION_ASSERT(_alloc_region == _dummy_region, "post-condition of retire()");

 191   _alloc_region = NULL;
 192   trace("released");
 193   return (alloc_region == _dummy_region) ? NULL : alloc_region;
 194 }
 195 
 196 #if G1_ALLOC_REGION_TRACING
 197 void G1AllocRegion::trace(const char* str, size_t min_word_size, size_t desired_word_size, size_t actual_word_size, HeapWord* result) {
 198   // All the calls to trace that set either just the size or the size
 199   // and the result are considered part of level 2 tracing and are
 200   // skipped during level 1 tracing.
 201   if ((actual_word_size == 0 && result == NULL) || (G1_ALLOC_REGION_TRACING > 1)) {
 202     const size_t buffer_length = 128;
 203     char hr_buffer[buffer_length];
 204     char rest_buffer[buffer_length];
 205 
 206     HeapRegion* alloc_region = _alloc_region;
 207     if (alloc_region == NULL) {
 208       jio_snprintf(hr_buffer, buffer_length, "NULL");
 209     } else if (alloc_region == _dummy_region) {
 210       jio_snprintf(hr_buffer, buffer_length, "DUMMY");


< prev index next >