1/*
2 * Copyright (c) 2014, 2015, 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#ifndef SHARE_VM_GC_G1_G1INCSETSTATE_HPP
26#define SHARE_VM_GC_G1_G1INCSETSTATE_HPP
27
28#include "gc/g1/g1BiasedArray.hpp"
29#include "gc/g1/heapRegion.hpp"
30#include "memory/allocation.hpp"
31
32// Per-region state during garbage collection.
33struct InCSetState {
34 public:
35  // We use different types to represent the state value. Particularly SPARC puts
36  // values in structs from "left to right", i.e. MSB to LSB. This results in many
37  // unnecessary shift operations when loading and storing values of this type.
38  // This degrades performance significantly (>10%) on that platform.
39  // Other tested ABIs do not seem to have this problem, and actually tend to
40  // favor smaller types, so we use the smallest usable type there.
41#ifdef SPARC
42  #define CSETSTATE_FORMAT INTPTR_FORMAT
43  typedef intptr_t in_cset_state_t;
44#else
45  #define CSETSTATE_FORMAT "%d"
46  typedef int8_t in_cset_state_t;
47#endif
48 private:
49  in_cset_state_t _value;
50 public:
51  enum {
52    // Selection of the values were driven to micro-optimize the encoding and
53    // frequency of the checks.
54    // The most common check is whether the region is in the collection set or not,
55    // this encoding allows us to use an > 0 check.
56    // The positive values are encoded in increasing generation order, which
57    // makes getting the next generation fast by a simple increment. They are also
58    // used to index into arrays.
59    // The negative values are used for objects requiring various special cases,
60    // for example eager reclamation of humongous objects.
61    Ext          = -2,    // Extension point
62    Humongous    = -1,    // The region is humongous
63    NotInCSet    =  0,    // The region is not in the collection set.
64    Young        =  1,    // The region is in the collection set and a young region.
65    Old          =  2,    // The region is in the collection set and an old region.
66    Num
67  };
68
69  InCSetState(in_cset_state_t value = NotInCSet) : _value(value) {
70    assert(is_valid(), "Invalid state %d", _value);
71  }
72
73  in_cset_state_t value() const        { return _value; }
74
75  void set_old()                       { _value = Old; }
76
77  bool is_in_cset_or_humongous() const { return is_in_cset() || is_humongous(); }
78  bool is_in_cset() const              { return _value > NotInCSet; }
79
80  bool is_humongous() const            { return _value == Humongous; }
81  bool is_young() const                { return _value == Young; }
82  bool is_old() const                  { return _value == Old; }
83  bool is_ext() const                  { return _value == Ext; }
84
85#ifdef ASSERT
86  bool is_default() const              { return _value == NotInCSet; }
87  bool is_valid() const                { return (_value >= Ext) && (_value < Num); }
88  bool is_valid_gen() const            { return (_value >= Young && _value <= Old); }
89#endif
90};
91
92// Instances of this class are used for quick tests on whether a reference points
93// into the collection set and into which generation or is a humongous object
94//
95// Each of the array's elements indicates whether the corresponding region is in
96// the collection set and if so in which generation, or a humongous region.
97//
98// We use this to speed up reference processing during young collection and
99// quickly reclaim humongous objects. For the latter, by making a humongous region
100// succeed this test, we sort-of add it to the collection set. During the reference
101// iteration closures, when we see a humongous region, we then simply mark it as
102// referenced, i.e. live.
103class G1InCSetStateFastTestBiasedMappedArray : public G1BiasedMappedArray<InCSetState> {
104 protected:
105  InCSetState default_value() const { return InCSetState::NotInCSet; }
106 public:
107  void set_humongous(uintptr_t index) {
108    assert(get_by_index(index).is_default(),
109           "State at index " INTPTR_FORMAT " should be default but is " CSETSTATE_FORMAT, index, get_by_index(index).value());
110    set_by_index(index, InCSetState::Humongous);
111  }
112
113  void set_ext(uintptr_t index) {
114    assert(get_by_index(index).is_default(),
115           "State at index " INTPTR_FORMAT " should be default but is " CSETSTATE_FORMAT, index, get_by_index(index).value());
116    set_by_index(index, InCSetState::Ext);
117  }
118
119  void clear_humongous(uintptr_t index) {
120    set_by_index(index, InCSetState::NotInCSet);
121  }
122
123  void set_in_young(uintptr_t index) {
124    assert(get_by_index(index).is_default(),
125           "State at index " INTPTR_FORMAT " should be default but is " CSETSTATE_FORMAT, index, get_by_index(index).value());
126    set_by_index(index, InCSetState::Young);
127  }
128
129  void set_in_old(uintptr_t index) {
130    assert(get_by_index(index).is_default(),
131           "State at index " INTPTR_FORMAT " should be default but is " CSETSTATE_FORMAT, index, get_by_index(index).value());
132    set_by_index(index, InCSetState::Old);
133  }
134
135  bool is_in_cset_or_humongous(HeapWord* addr) const { return at(addr).is_in_cset_or_humongous(); }
136  bool is_in_cset(HeapWord* addr) const { return at(addr).is_in_cset(); }
137  bool is_in_cset(const HeapRegion* hr) const { return get_by_index(hr->hrm_index()).is_in_cset(); }
138  InCSetState at(HeapWord* addr) const { return get_by_address(addr); }
139  void clear() { G1BiasedMappedArray<InCSetState>::clear(); }
140  void clear(const HeapRegion* hr) { return set_by_index(hr->hrm_index(), InCSetState::NotInCSet); }
141};
142
143#endif // SHARE_VM_GC_G1_G1INCSETSTATE_HPP
144