1 /* 2 * Copyright (c) 2001, 2009, 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 */ 24 25 // We need to sort heap regions by collection desirability. 26 27 class CSetChooserCache VALUE_OBJ_CLASS_SPEC { 28 private: 29 enum { 30 CacheLength = 16 31 } PrivateConstants; 32 33 HeapRegion* _cache[CacheLength]; 34 int _occupancy; // number of region in cache 35 int _first; // "first" region in the cache 36 37 // adding CacheLength to deal with negative values 38 inline int trim_index(int index) { 39 return (index + CacheLength) % CacheLength; 40 } 41 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 } 48 49 public: 50 CSetChooserCache(void); 51 52 inline int occupancy(void) { return _occupancy; } 53 inline bool is_full() { return _occupancy == CacheLength; } 54 inline bool is_empty() { return _occupancy == 0; } 55 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 } 63 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 }; 77 78 class CollectionSetChooser: public CHeapObj { 79 80 GrowableArray<HeapRegion*> _markedRegions; 81 int _curMarkedIndex; 82 int _numMarkedRegions; 83 CSetChooserCache _cache; 84 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; 88 89 jint _first_par_unreserved_idx; 90 91 public: 92 93 HeapRegion* getNextMarkedRegion(double time_so_far, double avg_prediction); 94 95 CollectionSetChooser(); 96 97 void printSortedHeapRegions(); 98 99 void sortMarkedHeapRegions(); 100 void fillCache(); 101 bool addRegionToCache(void); 102 void addMarkedHeapRegion(HeapRegion *hr); 103 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); 116 117 void clearMarkedHeapRegions(); 118 119 void updateAfterFullCollection(); 120 121 // Ensure that "hr" is not a member of the marked region array or the cache 122 void removeRegion(HeapRegion* hr); 123 124 bool unmarked_age_1_returned_as_new() { return _unmarked_age_1_returned_as_new; } 125 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 137 138 };