collectionSetChooser.hpp 7.2 KB
Newer Older
1
/*
2
 * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
22 23 24
 *
 */

25 26 27 28 29 30
#ifndef SHARE_VM_GC_IMPLEMENTATION_G1_COLLECTIONSETCHOOSER_HPP
#define SHARE_VM_GC_IMPLEMENTATION_G1_COLLECTIONSETCHOOSER_HPP

#include "gc_implementation/g1/heapRegion.hpp"
#include "utilities/growableArray.hpp"

Z
zgu 已提交
31
class CollectionSetChooser: public CHeapObj<mtGC> {
32

33
  GrowableArray<HeapRegion*> _regions;
34

35 36 37
  // Unfortunately, GrowableArray uses ints for length and indexes. To
  // avoid excessive casting in the rest of the class the following
  // wrapper methods are provided that use uints.
38

39 40 41 42
  uint regions_length()          { return (uint) _regions.length(); }
  HeapRegion* regions_at(uint i) { return _regions.at((int) i);     }
  void regions_at_put(uint i, HeapRegion* hr) {
    _regions.at_put((int) i, hr);
43
  }
44 45
  void regions_at_put_grow(uint i, HeapRegion* hr) {
    _regions.at_put_grow((int) i, hr);
46
  }
47
  void regions_trunc_to(uint i)  { _regions.trunc_to((uint) i); }
48

49 50
  // The index of the next candidate old region to be considered for
  // addition to the CSet.
51
  uint _curr_index;
52

53
  // The number of candidate old regions added to the CSet chooser.
54
  uint _length;
55

56 57 58
  // Keeps track of the start of the next array chunk to be claimed by
  // parallel GC workers.
  uint _first_par_unreserved_idx;
59

60 61 62
  // If a region has more live bytes than this threshold, it will not
  // be added to the CSet chooser and will not be a candidate for
  // collection.
63
  size_t _region_live_threshold_bytes;
64 65

  // The sum of reclaimable bytes over all the regions in the CSet chooser.
66
  size_t _remaining_reclaimable_bytes;
67

68 69
public:

70 71 72 73 74
  // Return the current candidate region to be considered for
  // collection without removing it from the CSet chooser.
  HeapRegion* peek() {
    HeapRegion* res = NULL;
    if (_curr_index < _length) {
75
      res = regions_at(_curr_index);
76
      assert(res != NULL,
77
             err_msg("Unexpected NULL hr in _regions at index %u",
78 79 80 81 82 83 84 85 86 87 88
                     _curr_index));
    }
    return res;
  }

  // Remove the given region from the CSet chooser and move to the
  // next one. The given region should be the current candidate region
  // in the CSet chooser.
  void remove_and_move_to_next(HeapRegion* hr) {
    assert(hr != NULL, "pre-condition");
    assert(_curr_index < _length, "pre-condition");
89 90 91
    assert(regions_at(_curr_index) == hr, "pre-condition");
    regions_at_put(_curr_index, NULL);
    assert(hr->reclaimable_bytes() <= _remaining_reclaimable_bytes,
92 93
           err_msg("remaining reclaimable bytes inconsistent "
                   "from region: "SIZE_FORMAT" remaining: "SIZE_FORMAT,
94 95
                   hr->reclaimable_bytes(), _remaining_reclaimable_bytes));
    _remaining_reclaimable_bytes -= hr->reclaimable_bytes();
96 97
    _curr_index += 1;
  }
98 99 100

  CollectionSetChooser();

101
  void sort_regions();
102 103 104 105 106

  // Determine whether to add the given region to the CSet chooser or
  // not. Currently, we skip humongous regions (we never add them to
  // the CSet, we only reclaim them during cleanup) and regions whose
  // live bytes are over the threshold.
107
  bool should_add(HeapRegion* hr) {
108 109 110
    assert(hr->is_marked(), "pre-condition");
    assert(!hr->is_young(), "should never consider young regions");
    return !hr->isHumongous() &&
111
            hr->live_bytes() < _region_live_threshold_bytes;
112 113 114 115
  }

  // Calculate the minimum number of old regions we'll add to the CSet
  // during a mixed GC.
116
  uint calc_min_old_cset_length();
117 118 119

  // Calculate the maximum number of old regions we'll add to the CSet
  // during a mixed GC.
120
  uint calc_max_old_cset_length();
121 122

  // Serial version.
123
  void add_region(HeapRegion *hr);
124

125 126 127 128
  // Must be called before calls to claim_array_chunk().
  // n_regions is the number of regions, chunk_size the chunk size.
  void prepare_for_par_region_addition(uint n_regions, uint chunk_size);
  // Returns the first index in a contiguous chunk of chunk_size indexes
129
  // that the calling thread has reserved.  These must be set by the
130 131
  // calling thread using set_region() (to NULL if necessary).
  uint claim_array_chunk(uint chunk_size);
132 133
  // Set the marked array entry at index to hr.  Careful to claim the index
  // first if in parallel.
134
  void set_region(uint index, HeapRegion* hr);
135 136
  // Atomically increment the number of added regions by region_num
  // and the amount of reclaimable bytes by reclaimable_bytes.
137
  void update_totals(uint region_num, size_t reclaimable_bytes);
138

139
  void clear();
140

141
  // Return the number of candidate regions that remain to be collected.
142
  uint remaining_regions() { return _length - _curr_index; }
143 144

  // Determine whether the CSet chooser has more candidate regions or not.
145
  bool is_empty() { return remaining_regions() == 0; }
146

147 148
  // Return the reclaimable bytes that remain to be collected on
  // all the candidate regions in the CSet chooser.
149
  size_t remaining_reclaimable_bytes() { return _remaining_reclaimable_bytes; }
150

151
  // Returns true if the used portion of "_regions" is properly
152
  // sorted, otherwise asserts false.
153
  void verify() PRODUCT_RETURN;
154
};
155

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
class CSetChooserParUpdater : public StackObj {
private:
  CollectionSetChooser* _chooser;
  bool _parallel;
  uint _chunk_size;
  uint _cur_chunk_idx;
  uint _cur_chunk_end;
  uint _regions_added;
  size_t _reclaimable_bytes_added;

public:
  CSetChooserParUpdater(CollectionSetChooser* chooser,
                        bool parallel, uint chunk_size) :
    _chooser(chooser), _parallel(parallel), _chunk_size(chunk_size),
    _cur_chunk_idx(0), _cur_chunk_end(0),
    _regions_added(0), _reclaimable_bytes_added(0) { }

  ~CSetChooserParUpdater() {
    if (_parallel && _regions_added > 0) {
      _chooser->update_totals(_regions_added, _reclaimable_bytes_added);
    }
  }

  void add_region(HeapRegion* hr) {
    if (_parallel) {
      if (_cur_chunk_idx == _cur_chunk_end) {
        _cur_chunk_idx = _chooser->claim_array_chunk(_chunk_size);
        _cur_chunk_end = _cur_chunk_idx + _chunk_size;
      }
      assert(_cur_chunk_idx < _cur_chunk_end, "invariant");
      _chooser->set_region(_cur_chunk_idx, hr);
      _cur_chunk_idx += 1;
    } else {
      _chooser->add_region(hr);
    }
    _regions_added += 1;
    _reclaimable_bytes_added += hr->reclaimable_bytes();
  }

  bool should_add(HeapRegion* hr) { return _chooser->should_add(hr); }
};

198
#endif // SHARE_VM_GC_IMPLEMENTATION_G1_COLLECTIONSETCHOOSER_HPP
199