1 /*
  2  * Copyright (c) 2016, 2020, Red Hat, Inc. All rights reserved.
  3  * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHCOLLECTIONSET_HPP
 27 #define SHARE_GC_SHENANDOAH_SHENANDOAHCOLLECTIONSET_HPP
 28 
 29 #include "memory/allocation.hpp"
 30 #include "memory/virtualspace.hpp"
 31 #include "gc/shenandoah/shenandoahHeap.hpp"
 32 #include "gc/shenandoah/shenandoahHeapRegion.hpp"
 33 #include "gc/shenandoah/shenandoahPadding.hpp"
 34 
 35 class ShenandoahCollectionSet : public CHeapObj<mtGC> {
 36   friend class ShenandoahHeap;
 37   friend class ShenandoahCollectionSetPreselector;
 38 
 39   void establish_preselected(bool *preselected) {
 40    assert(_preselected_regions == nullptr, "Over-writing");
 41    _preselected_regions = preselected;
 42   }
 43   void abandon_preselected() { _preselected_regions = nullptr; }
 44 
 45 private:
 46   size_t const          _map_size;
 47   size_t const          _region_size_bytes_shift;
 48   ReservedSpace         _map_space;
 49   char* const           _cset_map;
 50   // Bias cset map's base address for fast test if an oop is in cset
 51   char* const           _biased_cset_map;
 52 
 53   ShenandoahHeap* const _heap;
 54 
 55   bool                  _has_old_regions;
 56   size_t                _garbage;
 57   size_t                _used;
 58   size_t                _live;
 59   size_t                _region_count;
 60 
 61   size_t                _young_bytes_to_evacuate;
 62   size_t                _young_bytes_to_promote;
 63   size_t                _old_bytes_to_evacuate;
 64 
 65   // How many bytes of old garbage are present in a mixed collection set?
 66   size_t                _old_garbage;
 67 
 68   // Points to array identifying which tenure-age regions have been preselected
 69   // for inclusion in collection set. This field is only valid during brief
 70   // spans of time while collection set is being constructed.
 71   bool*                 _preselected_regions;
 72 
 73   // When a region having memory available to be allocated is added to the collection set, the region's available memory
 74   // should be subtracted from what's available.
 75   size_t                _young_available_bytes_collected;
 76 
 77   shenandoah_padding(0);
 78   volatile size_t       _current_index;
 79   shenandoah_padding(1);
 80 
 81 public:
 82   ShenandoahCollectionSet(ShenandoahHeap* heap, ReservedSpace space, char* heap_base);
 83 
 84   // Add region to collection set
 85   void add_region(ShenandoahHeapRegion* r);
 86 
 87   // MT version
 88   ShenandoahHeapRegion* claim_next();
 89 
 90   // Single-thread version
 91   ShenandoahHeapRegion* next();
 92 
 93   size_t count()  const { return _region_count; }
 94   bool is_empty() const { return _region_count == 0; }
 95 
 96   void clear_current_index() {
 97     _current_index = 0;
 98   }
 99 
100   inline bool is_in(ShenandoahHeapRegion* r) const;
101   inline bool is_in(size_t region_idx)       const;
102   inline bool is_in(oop obj)                 const;
103   inline bool is_in_loc(void* loc)           const;
104 
105   void print_on(outputStream* out) const;
106 
107   // It is not known how many of these bytes will be promoted.
108   inline size_t get_young_bytes_reserved_for_evacuation();
109   inline size_t get_old_bytes_reserved_for_evacuation();
110 
111   inline size_t get_young_bytes_to_be_promoted();
112 
113   size_t get_young_available_bytes_collected() { return _young_available_bytes_collected; }
114 
115   inline size_t get_old_garbage();
116 
117   bool is_preselected(size_t region_idx) {
118     assert(_preselected_regions != nullptr, "Missing etsablish after abandon");
119     return _preselected_regions[region_idx];
120   }
121 
122   bool* preselected_regions() {
123     assert(_preselected_regions != nullptr, "Null ptr");
124     return _preselected_regions;
125   }
126 
127   bool has_old_regions() const { return _has_old_regions; }
128   size_t used()          const { return _used; }
129   size_t live()          const { return _live; }
130   size_t garbage()       const { return _garbage; }
131 
132   void clear();
133 
134 private:
135   char* map_address() const {
136     return _cset_map;
137   }
138   char* biased_map_address() const {
139     return _biased_cset_map;
140   }
141 };
142 
143 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHCOLLECTIONSET_HPP