Mon, 03 Aug 2009 12:59:30 -0700
6865703: G1: Parallelize hot card cache cleanup
Summary: Have the GC worker threads clear the hot card cache in parallel by having each worker thread claim a chunk of the card cache and process the cards in that chunk. The size of the chunks that each thread will claim is determined at VM initialization from the size of the card cache and the number of worker threads.
Reviewed-by: jmasa, tonyp
1 /*
2 * Copyright 2001-2009 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
25 // We need to sort heap regions by collection desirability.
27 class CSetChooserCache VALUE_OBJ_CLASS_SPEC {
28 private:
29 enum {
30 CacheLength = 16
31 } PrivateConstants;
33 HeapRegion* _cache[CacheLength];
34 int _occupancy; // number of region in cache
35 int _first; // "first" region in the cache
37 // adding CacheLength to deal with negative values
38 inline int trim_index(int index) {
39 return (index + CacheLength) % CacheLength;
40 }
42 inline int get_sort_index(int index) {
43 return -index-2;
44 }
45 inline int get_index(int sort_index) {
46 return -sort_index-2;
47 }
49 public:
50 CSetChooserCache(void);
52 inline int occupancy(void) { return _occupancy; }
53 inline bool is_full() { return _occupancy == CacheLength; }
54 inline bool is_empty() { return _occupancy == 0; }
56 void clear(void);
57 void insert(HeapRegion *hr);
58 HeapRegion *remove_first(void);
59 void remove (HeapRegion *hr);
60 inline HeapRegion *get_first(void) {
61 return _cache[_first];
62 }
64 #ifndef PRODUCT
65 bool verify (void);
66 bool region_in_cache(HeapRegion *hr) {
67 int sort_index = hr->sort_index();
68 if (sort_index < -1) {
69 int index = get_index(sort_index);
70 guarantee(index < CacheLength, "should be within bounds");
71 return _cache[index] == hr;
72 } else
73 return 0;
74 }
75 #endif // PRODUCT
76 };
78 class CollectionSetChooser: public CHeapObj {
80 GrowableArray<HeapRegion*> _markedRegions;
81 int _curMarkedIndex;
82 int _numMarkedRegions;
83 CSetChooserCache _cache;
85 // True iff last collection pause ran of out new "age 0" regions, and
86 // returned an "age 1" region.
87 bool _unmarked_age_1_returned_as_new;
89 jint _first_par_unreserved_idx;
91 public:
93 HeapRegion* getNextMarkedRegion(double time_so_far, double avg_prediction);
95 CollectionSetChooser();
97 void printSortedHeapRegions();
99 void sortMarkedHeapRegions();
100 void fillCache();
101 bool addRegionToCache(void);
102 void addMarkedHeapRegion(HeapRegion *hr);
104 // Must be called before calls to getParMarkedHeapRegionChunk.
105 // "n_regions" is the number of regions, "chunkSize" the chunk size.
106 void prepareForAddMarkedHeapRegionsPar(size_t n_regions, size_t chunkSize);
107 // Returns the first index in a contiguous chunk of "n_regions" indexes
108 // that the calling thread has reserved. These must be set by the
109 // calling thread using "setMarkedHeapRegion" (to NULL if necessary).
110 jint getParMarkedHeapRegionChunk(jint n_regions);
111 // Set the marked array entry at index to hr. Careful to claim the index
112 // first if in parallel.
113 void setMarkedHeapRegion(jint index, HeapRegion* hr);
114 // Atomically increment the number of claimed regions by "inc_by".
115 void incNumMarkedHeapRegions(jint inc_by);
117 void clearMarkedHeapRegions();
119 void updateAfterFullCollection();
121 // Ensure that "hr" is not a member of the marked region array or the cache
122 void removeRegion(HeapRegion* hr);
124 bool unmarked_age_1_returned_as_new() { return _unmarked_age_1_returned_as_new; }
126 // Returns true if the used portion of "_markedRegions" is properly
127 // sorted, otherwise asserts false.
128 #ifndef PRODUCT
129 bool verify(void);
130 bool regionProperlyOrdered(HeapRegion* r) {
131 int si = r->sort_index();
132 return (si == -1) ||
133 (si > -1 && _markedRegions.at(si) == r) ||
134 (si < -1 && _cache.region_in_cache(r));
135 }
136 #endif
138 };