1 /*
  2  * Copyright (c) 2016, 2020, Red Hat, 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 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_GC_SHENANDOAH_SHENANDOAHCOLLECTIONSET_HPP
 26 #define SHARE_GC_SHENANDOAH_SHENANDOAHCOLLECTIONSET_HPP
 27 
 28 #include "memory/allocation.hpp"
 29 #include "memory/virtualspace.hpp"
 30 #include "gc/shenandoah/shenandoahHeap.hpp"
 31 #include "gc/shenandoah/shenandoahHeapRegion.hpp"
 32 #include "gc/shenandoah/shenandoahPadding.hpp"
 33 
 34 class ShenandoahCollectionSet : public CHeapObj<mtGC> {
 35   friend class ShenandoahHeap;
 36 private:
 37   size_t const          _map_size;
 38   size_t const          _region_size_bytes_shift;
 39   ReservedSpace         _map_space;
 40   char* const           _cset_map;
 41   // Bias cset map's base address for fast test if an oop is in cset
 42   char* const           _biased_cset_map;
 43 
 44   ShenandoahHeap* const _heap;
 45 
 46   bool                  _has_old_regions;
 47   size_t                _garbage;
 48   size_t                _used;
 49   size_t                _region_count;
 50   size_t                _immediate_trash;
 51   size_t                _evacuation_reserve; // How many bytes reserved in generation for evacuation replicas.  This does
 52                                              // not include bytes reserved for old-generation replicas.  The value is
 53                                              // conservative in that memory may be reserved for objects that will be promoted.
 54   size_t                _young_bytes_to_evacuate;
 55   size_t                _old_bytes_to_evacuate;
 56 
 57   size_t                _young_region_count;
 58   size_t                _old_region_count;
 59 
 60   shenandoah_padding(0);
 61   volatile size_t       _current_index;
 62   shenandoah_padding(1);
 63 
 64 public:
 65   ShenandoahCollectionSet(ShenandoahHeap* heap, ReservedSpace space, char* heap_base);
 66 
 67   // Add region to collection set
 68   void add_region(ShenandoahHeapRegion* r);
 69 
 70   // MT version
 71   ShenandoahHeapRegion* claim_next();
 72 
 73   // Single-thread version
 74   ShenandoahHeapRegion* next();
 75 
 76   size_t count()  const { return _region_count; }
 77   bool is_empty() const { return _region_count == 0; }
 78 
 79   void clear_current_index() {
 80     _current_index = 0;
 81   }
 82 
 83   inline bool is_in(ShenandoahHeapRegion* r) const;
 84   inline bool is_in(size_t region_idx)       const;
 85   inline bool is_in(oop obj)                 const;
 86   inline bool is_in_loc(void* loc)           const;
 87 
 88   void print_on(outputStream* out) const;
 89 
 90   inline size_t get_immediate_trash();
 91   inline void set_immediate_trash(size_t immediate_trash);
 92 
 93   // This represents total amount of work to be performed by evacuation, including evacuations to young, to old,
 94   // and promotions from young to old.  This equals get_young_bytes_reserved_for_evacuation() plus
 95   // get_old_bytes_reserved_for_evacuation().
 96   inline size_t get_bytes_reserved_for_evacuation();
 97 
 98   // It is not known how many of these bytes will be promoted.
 99   inline size_t get_young_bytes_reserved_for_evacuation();
100   inline void reserve_young_bytes_for_evacuation(size_t byte_count);
101 
102   inline size_t get_old_bytes_reserved_for_evacuation();
103   inline void reserve_old_bytes_for_evacuation(size_t byte_count);
104 
105   inline size_t get_old_region_count();
106 
107   inline size_t get_young_region_count();
108 
109   bool has_old_regions() const { return _has_old_regions; }
110   size_t used()          const { return _used; }
111 
112   size_t garbage()       const { return _garbage; }
113   void clear();
114 
115 private:
116   char* map_address() const {
117     return _cset_map;
118   }
119   char* biased_map_address() const {
120     return _biased_cset_map;
121   }
122 };
123 
124 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHCOLLECTIONSET_HPP