src/share/vm/gc_implementation/g1/g1Allocator.hpp

changeset 7118
227a9e5e4b4a
child 7131
d35872270666
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/gc_implementation/g1/g1Allocator.hpp	Fri Sep 05 09:49:19 2014 +0200
     1.3 @@ -0,0 +1,236 @@
     1.4 +/*
     1.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#ifndef SHARE_VM_GC_IMPLEMENTATION_G1_G1ALLOCATOR_HPP
    1.29 +#define SHARE_VM_GC_IMPLEMENTATION_G1_G1ALLOCATOR_HPP
    1.30 +
    1.31 +#include "gc_implementation/g1/g1AllocationContext.hpp"
    1.32 +#include "gc_implementation/g1/g1AllocRegion.hpp"
    1.33 +#include "gc_implementation/shared/parGCAllocBuffer.hpp"
    1.34 +
    1.35 +enum GCAllocPurpose {
    1.36 +  GCAllocForTenured,
    1.37 +  GCAllocForSurvived,
    1.38 +  GCAllocPurposeCount
    1.39 +};
    1.40 +
    1.41 +// Base class for G1 allocators.
    1.42 +class G1Allocator : public CHeapObj<mtGC> {
    1.43 +  friend class VMStructs;
    1.44 +protected:
    1.45 +  G1CollectedHeap* _g1h;
    1.46 +
    1.47 +  // Outside of GC pauses, the number of bytes used in all regions other
    1.48 +  // than the current allocation region.
    1.49 +  size_t _summary_bytes_used;
    1.50 +
    1.51 +public:
    1.52 +   G1Allocator(G1CollectedHeap* heap) :
    1.53 +     _g1h(heap), _summary_bytes_used(0) { }
    1.54 +
    1.55 +   static G1Allocator* create_allocator(G1CollectedHeap* g1h);
    1.56 +
    1.57 +   virtual void init_mutator_alloc_region() = 0;
    1.58 +   virtual void release_mutator_alloc_region() = 0;
    1.59 +
    1.60 +   virtual void init_gc_alloc_regions(EvacuationInfo& evacuation_info) = 0;
    1.61 +   virtual void release_gc_alloc_regions(uint no_of_gc_workers, EvacuationInfo& evacuation_info) = 0;
    1.62 +   virtual void abandon_gc_alloc_regions() = 0;
    1.63 +
    1.64 +   virtual MutatorAllocRegion*    mutator_alloc_region(AllocationContext_t context) = 0;
    1.65 +   virtual SurvivorGCAllocRegion* survivor_gc_alloc_region(AllocationContext_t context) = 0;
    1.66 +   virtual OldGCAllocRegion*      old_gc_alloc_region(AllocationContext_t context) = 0;
    1.67 +   virtual size_t                 used() = 0;
    1.68 +   virtual bool                   is_retained_old_region(HeapRegion* hr) = 0;
    1.69 +
    1.70 +   void                           reuse_retained_old_region(EvacuationInfo& evacuation_info,
    1.71 +                                                            OldGCAllocRegion* old,
    1.72 +                                                            HeapRegion** retained);
    1.73 +
    1.74 +   size_t used_unlocked() const {
    1.75 +     return _summary_bytes_used;
    1.76 +   }
    1.77 +
    1.78 +   void increase_used(size_t bytes) {
    1.79 +     _summary_bytes_used += bytes;
    1.80 +   }
    1.81 +
    1.82 +   void decrease_used(size_t bytes) {
    1.83 +     assert(_summary_bytes_used >= bytes,
    1.84 +            err_msg("invariant: _summary_bytes_used: "SIZE_FORMAT" should be >= bytes: "SIZE_FORMAT,
    1.85 +                _summary_bytes_used, bytes));
    1.86 +     _summary_bytes_used -= bytes;
    1.87 +   }
    1.88 +
    1.89 +   void set_used(size_t bytes) {
    1.90 +     _summary_bytes_used = bytes;
    1.91 +   }
    1.92 +};
    1.93 +
    1.94 +// The default allocator for G1.
    1.95 +class G1DefaultAllocator : public G1Allocator {
    1.96 +protected:
    1.97 +  // Alloc region used to satisfy mutator allocation requests.
    1.98 +  MutatorAllocRegion _mutator_alloc_region;
    1.99 +
   1.100 +  // Alloc region used to satisfy allocation requests by the GC for
   1.101 +  // survivor objects.
   1.102 +  SurvivorGCAllocRegion _survivor_gc_alloc_region;
   1.103 +
   1.104 +  // Alloc region used to satisfy allocation requests by the GC for
   1.105 +  // old objects.
   1.106 +  OldGCAllocRegion _old_gc_alloc_region;
   1.107 +
   1.108 +  HeapRegion* _retained_old_gc_alloc_region;
   1.109 +public:
   1.110 +  G1DefaultAllocator(G1CollectedHeap* heap) : G1Allocator(heap), _retained_old_gc_alloc_region(NULL) { }
   1.111 +
   1.112 +  virtual void init_mutator_alloc_region();
   1.113 +  virtual void release_mutator_alloc_region();
   1.114 +
   1.115 +  virtual void init_gc_alloc_regions(EvacuationInfo& evacuation_info);
   1.116 +  virtual void release_gc_alloc_regions(uint no_of_gc_workers, EvacuationInfo& evacuation_info);
   1.117 +  virtual void abandon_gc_alloc_regions();
   1.118 +
   1.119 +  virtual bool is_retained_old_region(HeapRegion* hr) {
   1.120 +    return _retained_old_gc_alloc_region == hr;
   1.121 +  }
   1.122 +
   1.123 +  virtual MutatorAllocRegion* mutator_alloc_region(AllocationContext_t context) {
   1.124 +    return &_mutator_alloc_region;
   1.125 +  }
   1.126 +
   1.127 +  virtual SurvivorGCAllocRegion* survivor_gc_alloc_region(AllocationContext_t context) {
   1.128 +    return &_survivor_gc_alloc_region;
   1.129 +  }
   1.130 +
   1.131 +  virtual OldGCAllocRegion* old_gc_alloc_region(AllocationContext_t context) {
   1.132 +    return &_old_gc_alloc_region;
   1.133 +  }
   1.134 +
   1.135 +  virtual size_t used() {
   1.136 +    assert(Heap_lock->owner() != NULL,
   1.137 +           "Should be owned on this thread's behalf.");
   1.138 +    size_t result = _summary_bytes_used;
   1.139 +
   1.140 +    // Read only once in case it is set to NULL concurrently
   1.141 +    HeapRegion* hr = mutator_alloc_region(AllocationContext::current())->get();
   1.142 +    if (hr != NULL) {
   1.143 +      result += hr->used();
   1.144 +    }
   1.145 +    return result;
   1.146 +  }
   1.147 +};
   1.148 +
   1.149 +class G1ParGCAllocBuffer: public ParGCAllocBuffer {
   1.150 +private:
   1.151 +  bool _retired;
   1.152 +
   1.153 +public:
   1.154 +  G1ParGCAllocBuffer(size_t gclab_word_size);
   1.155 +  virtual ~G1ParGCAllocBuffer() {
   1.156 +    guarantee(_retired, "Allocation buffer has not been retired");
   1.157 +  }
   1.158 +
   1.159 +  virtual void set_buf(HeapWord* buf) {
   1.160 +    ParGCAllocBuffer::set_buf(buf);
   1.161 +    _retired = false;
   1.162 +  }
   1.163 +
   1.164 +  virtual void retire(bool end_of_gc, bool retain) {
   1.165 +    if (_retired) {
   1.166 +      return;
   1.167 +    }
   1.168 +    ParGCAllocBuffer::retire(end_of_gc, retain);
   1.169 +    _retired = true;
   1.170 +  }
   1.171 +};
   1.172 +
   1.173 +class G1ParGCAllocator : public CHeapObj<mtGC> {
   1.174 +  friend class G1ParScanThreadState;
   1.175 +protected:
   1.176 +  G1CollectedHeap* _g1h;
   1.177 +
   1.178 +  size_t _alloc_buffer_waste;
   1.179 +  size_t _undo_waste;
   1.180 +
   1.181 +  void add_to_alloc_buffer_waste(size_t waste) { _alloc_buffer_waste += waste; }
   1.182 +  void add_to_undo_waste(size_t waste)         { _undo_waste += waste; }
   1.183 +
   1.184 +  HeapWord* allocate_slow(GCAllocPurpose purpose, size_t word_sz, AllocationContext_t context);
   1.185 +
   1.186 +  virtual void retire_alloc_buffers() = 0;
   1.187 +  virtual G1ParGCAllocBuffer* alloc_buffer(GCAllocPurpose purpose, AllocationContext_t context) = 0;
   1.188 +
   1.189 +public:
   1.190 +  G1ParGCAllocator(G1CollectedHeap* g1h) :
   1.191 +    _g1h(g1h), _alloc_buffer_waste(0), _undo_waste(0) {
   1.192 +  }
   1.193 +
   1.194 +  static G1ParGCAllocator* create_allocator(G1CollectedHeap* g1h);
   1.195 +
   1.196 +  size_t alloc_buffer_waste() { return _alloc_buffer_waste; }
   1.197 +  size_t undo_waste() {return _undo_waste; }
   1.198 +
   1.199 +  HeapWord* allocate(GCAllocPurpose purpose, size_t word_sz, AllocationContext_t context) {
   1.200 +    HeapWord* obj = NULL;
   1.201 +    if (purpose == GCAllocForSurvived) {
   1.202 +      obj = alloc_buffer(purpose, context)->allocate_aligned(word_sz, SurvivorAlignmentInBytes);
   1.203 +    } else {
   1.204 +      obj = alloc_buffer(purpose, context)->allocate(word_sz);
   1.205 +    }
   1.206 +    if (obj != NULL) {
   1.207 +      return obj;
   1.208 +    }
   1.209 +    return allocate_slow(purpose, word_sz, context);
   1.210 +  }
   1.211 +
   1.212 +  void undo_allocation(GCAllocPurpose purpose, HeapWord* obj, size_t word_sz, AllocationContext_t context) {
   1.213 +    if (alloc_buffer(purpose, context)->contains(obj)) {
   1.214 +      assert(alloc_buffer(purpose, context)->contains(obj + word_sz - 1),
   1.215 +             "should contain whole object");
   1.216 +      alloc_buffer(purpose, context)->undo_allocation(obj, word_sz);
   1.217 +    } else {
   1.218 +      CollectedHeap::fill_with_object(obj, word_sz);
   1.219 +      add_to_undo_waste(word_sz);
   1.220 +    }
   1.221 +  }
   1.222 +};
   1.223 +
   1.224 +class G1DefaultParGCAllocator : public G1ParGCAllocator {
   1.225 +  G1ParGCAllocBuffer  _surviving_alloc_buffer;
   1.226 +  G1ParGCAllocBuffer  _tenured_alloc_buffer;
   1.227 +  G1ParGCAllocBuffer* _alloc_buffers[GCAllocPurposeCount];
   1.228 +
   1.229 +public:
   1.230 +  G1DefaultParGCAllocator(G1CollectedHeap* g1h);
   1.231 +
   1.232 +  virtual G1ParGCAllocBuffer* alloc_buffer(GCAllocPurpose purpose, AllocationContext_t context) {
   1.233 +    return _alloc_buffers[purpose];
   1.234 +  }
   1.235 +
   1.236 +  virtual void retire_alloc_buffers() ;
   1.237 +};
   1.238 +
   1.239 +#endif // SHARE_VM_GC_IMPLEMENTATION_G1_G1ALLOCATOR_HPP

mercurial