9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #ifndef SHARE_VM_GC_IMPLEMENTATION_G1_G1ALLOCREGION_HPP
26 #define SHARE_VM_GC_IMPLEMENTATION_G1_G1ALLOCREGION_HPP
27
28 #include "gc_implementation/g1/heapRegion.hpp"
29
30 class G1CollectedHeap;
31
32 // 0 -> no tracing, 1 -> basic tracing, 2 -> basic + allocation tracing
33 #define G1_ALLOC_REGION_TRACING 0
34
35 class ar_ext_msg;
36
37 // A class that holds a region that is active in satisfying allocation
38 // requests, potentially issued in parallel. When the active region is
39 // full it will be retired and replaced with a new one. The
40 // implementation assumes that fast-path allocations will be lock-free
41 // and a lock will need to be taken when the active region needs to be
42 // replaced.
43
44 class G1AllocRegion VALUE_OBJ_CLASS_SPEC {
45 friend class ar_ext_msg;
46
47 private:
48 // The active allocating region we are currently allocating out
84 // purpose and it is not part of the heap) that is full (i.e., top()
85 // == end()). When we don't have a valid active region we make
86 // _alloc_region point to this. This allows us to skip checking
87 // whether the _alloc_region is NULL or not.
88 static HeapRegion* _dummy_region;
89
90 // Some of the methods below take a bot_updates parameter. Its value
91 // should be the same as the _bot_updates field. The idea is that
92 // the parameter will be a constant for a particular alloc region
93 // and, given that these methods will be hopefully inlined, the
94 // compiler should compile out the test.
95
96 // Perform a non-MT-safe allocation out of the given region.
97 static inline HeapWord* allocate(HeapRegion* alloc_region,
98 size_t word_size,
99 bool bot_updates);
100
101 // Perform a MT-safe allocation out of the given region.
102 static inline HeapWord* par_allocate(HeapRegion* alloc_region,
103 size_t word_size,
104 bool bot_updates);
105
106 // Ensure that the region passed as a parameter has been filled up
107 // so that noone else can allocate out of it any more.
108 static void fill_up_remaining_space(HeapRegion* alloc_region,
109 bool bot_updates);
110
111 // Retire the active allocating region. If fill_up is true then make
112 // sure that the region is full before we retire it so that noone
113 // else can allocate out of it.
114 void retire(bool fill_up);
115
116 // After a region is allocated by alloc_new_region, this
117 // method is used to set it as the active alloc_region
118 void update_alloc_region(HeapRegion* alloc_region);
119
120 // Allocate a new active region and use it to perform a word_size
121 // allocation. The force parameter will be passed on to
122 // G1CollectedHeap::allocate_new_alloc_region() and tells it to try
123 // to allocate a new region even if the max has been reached.
124 HeapWord* new_alloc_region_and_allocate(size_t word_size, bool force);
125
126 void fill_in_ext_msg(ar_ext_msg* msg, const char* message);
127
128 protected:
129 // For convenience as subclasses use it.
130 static G1CollectedHeap* _g1h;
131
132 virtual HeapRegion* allocate_new_region(size_t word_size, bool force) = 0;
133 virtual void retire_region(HeapRegion* alloc_region,
134 size_t allocated_bytes) = 0;
135
136 G1AllocRegion(const char* name, bool bot_updates);
137
138 public:
139 static void setup(G1CollectedHeap* g1h, HeapRegion* dummy_region);
140
141 HeapRegion* get() const {
142 HeapRegion * hr = _alloc_region;
143 // Make sure that the dummy region does not escape this class.
144 return (hr == _dummy_region) ? NULL : hr;
145 }
146
147 void set_allocation_context(AllocationContext_t context) { _allocation_context = context; }
148 AllocationContext_t allocation_context() { return _allocation_context; }
149
150 uint count() { return _count; }
151
152 // The following two are the building blocks for the allocation method.
153
154 // First-level allocation: Should be called without holding a
155 // lock. It will try to allocate lock-free out of the active region,
156 // or return NULL if it was unable to.
157 inline HeapWord* attempt_allocation(size_t word_size, bool bot_updates);
158
159 // Second-level allocation: Should be called while holding a
160 // lock. It will try to first allocate lock-free out of the active
161 // region or, if it's unable to, it will try to replace the active
162 // alloc region with a new one. We require that the caller takes the
163 // appropriate lock before calling this so that it is easier to make
164 // it conform to its locking protocol.
165 inline HeapWord* attempt_allocation_locked(size_t word_size,
166 bool bot_updates);
167
168 // Should be called to allocate a new region even if the max of this
169 // type of regions has been reached. Should only be called if other
170 // allocation attempts have failed and we are not holding a valid
171 // active region.
172 inline HeapWord* attempt_allocation_force(size_t word_size,
173 bool bot_updates);
174
175 // Should be called before we start using this object.
176 void init();
177
178 // This can be used to set the active region to a specific
179 // region. (Use Example: we try to retain the last old GC alloc
180 // region that we've used during a GC and we can use set() to
181 // re-instate it at the beginning of the next GC.)
182 void set(HeapRegion* alloc_region);
183
184 // Should be called when we want to release the active region which
185 // is returned after it's been retired.
186 virtual HeapRegion* release();
187
188 #if G1_ALLOC_REGION_TRACING
189 void trace(const char* str, size_t word_size = 0, HeapWord* result = NULL);
190 #else // G1_ALLOC_REGION_TRACING
191 void trace(const char* str, size_t word_size = 0, HeapWord* result = NULL) { }
192 #endif // G1_ALLOC_REGION_TRACING
193 };
194
195 class MutatorAllocRegion : public G1AllocRegion {
196 protected:
197 virtual HeapRegion* allocate_new_region(size_t word_size, bool force);
198 virtual void retire_region(HeapRegion* alloc_region, size_t allocated_bytes);
199 public:
200 MutatorAllocRegion()
201 : G1AllocRegion("Mutator Alloc Region", false /* bot_updates */) { }
202 };
203
204 class SurvivorGCAllocRegion : public G1AllocRegion {
205 protected:
206 virtual HeapRegion* allocate_new_region(size_t word_size, bool force);
207 virtual void retire_region(HeapRegion* alloc_region, size_t allocated_bytes);
208 public:
209 SurvivorGCAllocRegion()
210 : G1AllocRegion("Survivor GC Alloc Region", false /* bot_updates */) { }
211 };
212
213 class OldGCAllocRegion : public G1AllocRegion {
214 protected:
215 virtual HeapRegion* allocate_new_region(size_t word_size, bool force);
216 virtual void retire_region(HeapRegion* alloc_region, size_t allocated_bytes);
217 public:
218 OldGCAllocRegion()
219 : G1AllocRegion("Old GC Alloc Region", true /* bot_updates */) { }
220
221 // This specialization of release() makes sure that the last card that has
222 // been allocated into has been completely filled by a dummy object. This
223 // avoids races when remembered set scanning wants to update the BOT of the
224 // last card in the retained old gc alloc region, and allocation threads
225 // allocating into that card at the same time.
226 virtual HeapRegion* release();
227 };
228
229 class ar_ext_msg : public err_msg {
230 public:
231 ar_ext_msg(G1AllocRegion* alloc_region, const char *message) : err_msg("%s", "") {
232 alloc_region->fill_in_ext_msg(this, message);
233 }
234 };
235
236 #endif // SHARE_VM_GC_IMPLEMENTATION_G1_G1ALLOCREGION_HPP
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #ifndef SHARE_VM_GC_IMPLEMENTATION_G1_G1ALLOCREGION_HPP
26 #define SHARE_VM_GC_IMPLEMENTATION_G1_G1ALLOCREGION_HPP
27
28 #include "gc_implementation/g1/heapRegion.hpp"
29 #include "gc_implementation/g1/g1EvacStats.hpp"
30 #include "gc_implementation/g1/g1InCSetState.hpp"
31
32 class G1CollectedHeap;
33
34 // 0 -> no tracing, 1 -> basic tracing, 2 -> basic + allocation tracing
35 #define G1_ALLOC_REGION_TRACING 0
36
37 class ar_ext_msg;
38
39 // A class that holds a region that is active in satisfying allocation
40 // requests, potentially issued in parallel. When the active region is
41 // full it will be retired and replaced with a new one. The
42 // implementation assumes that fast-path allocations will be lock-free
43 // and a lock will need to be taken when the active region needs to be
44 // replaced.
45
46 class G1AllocRegion VALUE_OBJ_CLASS_SPEC {
47 friend class ar_ext_msg;
48
49 private:
50 // The active allocating region we are currently allocating out
86 // purpose and it is not part of the heap) that is full (i.e., top()
87 // == end()). When we don't have a valid active region we make
88 // _alloc_region point to this. This allows us to skip checking
89 // whether the _alloc_region is NULL or not.
90 static HeapRegion* _dummy_region;
91
92 // Some of the methods below take a bot_updates parameter. Its value
93 // should be the same as the _bot_updates field. The idea is that
94 // the parameter will be a constant for a particular alloc region
95 // and, given that these methods will be hopefully inlined, the
96 // compiler should compile out the test.
97
98 // Perform a non-MT-safe allocation out of the given region.
99 static inline HeapWord* allocate(HeapRegion* alloc_region,
100 size_t word_size,
101 bool bot_updates);
102
103 // Perform a MT-safe allocation out of the given region.
104 static inline HeapWord* par_allocate(HeapRegion* alloc_region,
105 size_t word_size,
106 bool bot_updates) { return par_allocate(alloc_region, word_size, word_size, bot_updates); }
107 static inline HeapWord* par_allocate(HeapRegion* alloc_region,
108 size_t min_word_size,
109 size_t& word_size,
110 bool bot_updates);
111
112 // Ensure that the region passed as a parameter has been filled up
113 // so that noone else can allocate out of it any more.
114 // Returns the number of bytes that have been wasted by filled up
115 // the space.
116 static size_t fill_up_remaining_space(HeapRegion* alloc_region,
117 bool bot_updates);
118
119 // After a region is allocated by alloc_new_region, this
120 // method is used to set it as the active alloc_region
121 void update_alloc_region(HeapRegion* alloc_region);
122
123 // Allocate a new active region and use it to perform a word_size
124 // allocation. The force parameter will be passed on to
125 // G1CollectedHeap::allocate_new_alloc_region() and tells it to try
126 // to allocate a new region even if the max has been reached.
127 HeapWord* new_alloc_region_and_allocate(size_t word_size, bool force);
128
129 void fill_in_ext_msg(ar_ext_msg* msg, const char* message);
130
131 protected:
132 // Retire the active allocating region. If fill_up is true then make
133 // sure that the region is full before we retire it so that no one
134 // else can allocate out of it.
135 // Returns the number of bytes that have been filled up during retire.
136 virtual size_t retire(bool fill_up);
137
138 // For convenience as subclasses use it.
139 static G1CollectedHeap* _g1h;
140
141 virtual HeapRegion* allocate_new_region(size_t word_size, bool force) = 0;
142 virtual void retire_region(HeapRegion* alloc_region,
143 size_t allocated_bytes) = 0;
144
145 G1AllocRegion(const char* name, bool bot_updates);
146
147 public:
148 static void setup(G1CollectedHeap* g1h, HeapRegion* dummy_region);
149
150 HeapRegion* get() const {
151 HeapRegion * hr = _alloc_region;
152 // Make sure that the dummy region does not escape this class.
153 return (hr == _dummy_region) ? NULL : hr;
154 }
155
156 void set_allocation_context(AllocationContext_t context) { _allocation_context = context; }
157 AllocationContext_t allocation_context() { return _allocation_context; }
158
159 uint count() { return _count; }
160
161 // The following two are the building blocks for the allocation method.
162
163 // First-level allocation: Should be called without holding a
164 // lock. It will try to allocate lock-free out of the active region,
165 // or return NULL if it was unable to.
166 inline HeapWord* attempt_allocation(size_t word_size,
167 bool bot_updates) { return attempt_allocation(word_size, word_size, bot_updates); }
168 inline HeapWord* attempt_allocation(size_t min_word_size,
169 size_t& word_size,
170 bool bot_updates);
171
172 // Second-level allocation: Should be called while holding a
173 // lock. It will try to first allocate lock-free out of the active
174 // region or, if it's unable to, it will try to replace the active
175 // alloc region with a new one. We require that the caller takes the
176 // appropriate lock before calling this so that it is easier to make
177 // it conform to its locking protocol.
178 inline HeapWord* attempt_allocation_locked(size_t word_size,
179 bool bot_updates) { return attempt_allocation_locked(word_size, word_size, bot_updates); }
180 inline HeapWord* attempt_allocation_locked(size_t min_word_size,
181 size_t& word_size,
182 bool bot_updates);
183
184 // Should be called to allocate a new region even if the max of this
185 // type of regions has been reached. Should only be called if other
186 // allocation attempts have failed and we are not holding a valid
187 // active region.
188 inline HeapWord* attempt_allocation_force(size_t word_size,
189 bool bot_updates);
190
191 // Should be called before we start using this object.
192 void init();
193
194 // This can be used to set the active region to a specific
195 // region. (Use Example: we try to retain the last old GC alloc
196 // region that we've used during a GC and we can use set() to
197 // re-instate it at the beginning of the next GC.)
198 void set(HeapRegion* alloc_region);
199
200 // Should be called when we want to release the active region which
201 // is returned after it's been retired.
202 virtual HeapRegion* release();
203
204 #if G1_ALLOC_REGION_TRACING
205 void trace(const char* str, size_t word_size = 0, HeapWord* result = NULL);
206 #else // G1_ALLOC_REGION_TRACING
207 void trace(const char* str, size_t word_size = 0, HeapWord* result = NULL) { }
208 #endif // G1_ALLOC_REGION_TRACING
209 };
210
211 class MutatorAllocRegion : public G1AllocRegion {
212 protected:
213 virtual HeapRegion* allocate_new_region(size_t word_size, bool force);
214 virtual void retire_region(HeapRegion* alloc_region, size_t allocated_bytes);
215 public:
216 MutatorAllocRegion()
217 : G1AllocRegion("Mutator Alloc Region", false /* bot_updates */) { }
218 };
219
220 // Common base class for allocation regions used during GC.
221 class G1GCAllocRegion : public G1AllocRegion {
222 protected:
223 G1EvacStats* _stats;
224 InCSetState::in_cset_state_t _purpose;
225
226 virtual HeapRegion* allocate_new_region(size_t word_size, bool force);
227 virtual void retire_region(HeapRegion* alloc_region, size_t allocated_bytes);
228
229 virtual size_t retire(bool fill_up);
230 public:
231 G1GCAllocRegion(const char* name, bool bot_updates, G1EvacStats* stats, InCSetState::in_cset_state_t purpose)
232 : G1AllocRegion(name, bot_updates), _stats(stats), _purpose(purpose) {
233 assert(stats != NULL, "Must pass non-NULL PLAB statistics");
234 }
235 };
236
237 class SurvivorGCAllocRegion : public G1GCAllocRegion {
238 public:
239 SurvivorGCAllocRegion(G1EvacStats* stats)
240 : G1GCAllocRegion("Survivor GC Alloc Region", false /* bot_updates */, stats, InCSetState::Young) { }
241 };
242
243 class OldGCAllocRegion : public G1GCAllocRegion {
244 public:
245 OldGCAllocRegion(G1EvacStats* stats)
246 : G1GCAllocRegion("Old GC Alloc Region", true /* bot_updates */, stats, InCSetState::Old) { }
247
248 // This specialization of release() makes sure that the last card that has
249 // been allocated into has been completely filled by a dummy object. This
250 // avoids races when remembered set scanning wants to update the BOT of the
251 // last card in the retained old gc alloc region, and allocation threads
252 // allocating into that card at the same time.
253 virtual HeapRegion* release();
254 };
255
256 class ar_ext_msg : public err_msg {
257 public:
258 ar_ext_msg(G1AllocRegion* alloc_region, const char *message) : err_msg("%s", "") {
259 alloc_region->fill_in_ext_msg(this, message);
260 }
261 };
262
263 #endif // SHARE_VM_GC_IMPLEMENTATION_G1_G1ALLOCREGION_HPP
|