Mon, 07 Nov 2011 22:11:12 -0500
7092309: G1: introduce old region set
Summary: Keep track of all the old regions in the heap with a heap region set.
Reviewed-by: brutisso, johnc
1 /*
2 * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
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 */
25 #ifndef SHARE_VM_GC_IMPLEMENTATION_G1_COLLECTIONSETCHOOSER_HPP
26 #define SHARE_VM_GC_IMPLEMENTATION_G1_COLLECTIONSETCHOOSER_HPP
28 #include "gc_implementation/g1/heapRegion.hpp"
29 #include "utilities/growableArray.hpp"
31 // We need to sort heap regions by collection desirability.
32 // This sorting is currently done in two "stages". An initial sort is
33 // done following a cleanup pause as soon as all of the marked but
34 // non-empty regions have been identified and the completely empty
35 // ones reclaimed.
36 // This gives us a global sort on a GC efficiency metric
37 // based on predictive data available at that time. However,
38 // any of these regions that are collected will only be collected
39 // during a future GC pause, by which time it is possible that newer
40 // data might allow us to revise and/or refine the earlier
41 // pause predictions, leading to changes in expected gc efficiency
42 // order. To somewhat mitigate this obsolescence, more so in the
43 // case of regions towards the end of the list, which will be
44 // picked later, these pre-sorted regions from the _markedRegions
45 // array are not used as is, but a small prefix thereof is
46 // insertion-sorted again into a small cache, based on more
47 // recent remembered set information. Regions are then drawn
48 // from this cache to construct the collection set at each
49 // incremental GC.
50 // This scheme and/or its implementation may be subject to
51 // revision in the future.
53 class CSetChooserCache VALUE_OBJ_CLASS_SPEC {
54 private:
55 enum {
56 CacheLength = 16
57 } PrivateConstants;
59 HeapRegion* _cache[CacheLength];
60 int _occupancy; // number of regions in cache
61 int _first; // (index of) "first" region in the cache
63 // adding CacheLength to deal with negative values
64 inline int trim_index(int index) {
65 return (index + CacheLength) % CacheLength;
66 }
68 inline int get_sort_index(int index) {
69 return -index-2;
70 }
71 inline int get_index(int sort_index) {
72 return -sort_index-2;
73 }
75 public:
76 CSetChooserCache(void);
78 inline int occupancy(void) { return _occupancy; }
79 inline bool is_full() { return _occupancy == CacheLength; }
80 inline bool is_empty() { return _occupancy == 0; }
82 void clear(void);
83 void insert(HeapRegion *hr);
84 HeapRegion *remove_first(void);
85 inline HeapRegion *get_first(void) {
86 return _cache[_first];
87 }
89 #ifndef PRODUCT
90 bool verify (void);
91 bool region_in_cache(HeapRegion *hr) {
92 int sort_index = hr->sort_index();
93 if (sort_index < -1) {
94 int index = get_index(sort_index);
95 guarantee(index < CacheLength, "should be within bounds");
96 return _cache[index] == hr;
97 } else
98 return 0;
99 }
100 #endif // PRODUCT
101 };
103 class CollectionSetChooser: public CHeapObj {
105 GrowableArray<HeapRegion*> _markedRegions;
106 int _curMarkedIndex;
107 int _numMarkedRegions;
108 CSetChooserCache _cache;
110 // True iff last collection pause ran of out new "age 0" regions, and
111 // returned an "age 1" region.
112 bool _unmarked_age_1_returned_as_new;
114 jint _first_par_unreserved_idx;
116 public:
118 HeapRegion* getNextMarkedRegion(double time_so_far, double avg_prediction);
120 CollectionSetChooser();
122 void sortMarkedHeapRegions();
123 void fillCache();
124 void addMarkedHeapRegion(HeapRegion *hr);
126 // Must be called before calls to getParMarkedHeapRegionChunk.
127 // "n_regions" is the number of regions, "chunkSize" the chunk size.
128 void prepareForAddMarkedHeapRegionsPar(size_t n_regions, size_t chunkSize);
129 // Returns the first index in a contiguous chunk of "n_regions" indexes
130 // that the calling thread has reserved. These must be set by the
131 // calling thread using "setMarkedHeapRegion" (to NULL if necessary).
132 jint getParMarkedHeapRegionChunk(jint n_regions);
133 // Set the marked array entry at index to hr. Careful to claim the index
134 // first if in parallel.
135 void setMarkedHeapRegion(jint index, HeapRegion* hr);
136 // Atomically increment the number of claimed regions by "inc_by".
137 void incNumMarkedHeapRegions(jint inc_by);
139 void clearMarkedHeapRegions();
141 void updateAfterFullCollection();
143 bool unmarked_age_1_returned_as_new() { return _unmarked_age_1_returned_as_new; }
145 // Returns true if the used portion of "_markedRegions" is properly
146 // sorted, otherwise asserts false.
147 #ifndef PRODUCT
148 bool verify(void);
149 bool regionProperlyOrdered(HeapRegion* r) {
150 int si = r->sort_index();
151 return (si == -1) ||
152 (si > -1 && _markedRegions.at(si) == r) ||
153 (si < -1 && _cache.region_in_cache(r));
154 }
155 #endif
157 };
159 #endif // SHARE_VM_GC_IMPLEMENTATION_G1_COLLECTIONSETCHOOSER_HPP