1 /*
2  * Copyright (c) 2001, 2021, 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_GC_PARALLEL_MUTABLESPACE_HPP
26 #define SHARE_GC_PARALLEL_MUTABLESPACE_HPP
27 
28 #include "memory/allocation.hpp"
29 #include "memory/iterator.hpp"
30 #include "memory/memRegion.hpp"
31 #include "utilities/copy.hpp"
32 #include "utilities/globalDefinitions.hpp"
33 #include "utilities/macros.hpp"
34 
35 class WorkGang;
36 
37 // A MutableSpace supports the concept of allocation. This includes the
38 // concepts that a space may be only partially full, and the query methods
39 // that go with such an assumption.
40 //
41 // MutableSpace is also responsible for minimizing the
42 // page allocation time by having the memory pretouched (with
43 // AlwaysPretouch) and for optimizing page placement on NUMA systems
44 // by make the underlying region interleaved (with UseNUMA).
45 //
46 // Invariant: bottom() <= top() <= end()
47 // top() and end() are exclusive.
48 
49 class MutableSpaceMangler;
50 
51 class MutableSpace: public CHeapObj<mtGC> {
52   friend class VMStructs;
53 
54   // Helper for mangling unused space in debug builds
55   MutableSpaceMangler* _mangler;
56   // The last region which page had been setup to be interleaved.
57   MemRegion _last_setup_region;
58   size_t _alignment;
59   HeapWord* _bottom;
60   HeapWord* volatile _top;
61   HeapWord* _end;
62 
mangler()63   MutableSpaceMangler* mangler() { return _mangler; }
64 
65   void numa_setup_pages(MemRegion mr, bool clear_space);
66 
set_last_setup_region(MemRegion mr)67   void set_last_setup_region(MemRegion mr) { _last_setup_region = mr;   }
last_setup_region() const68   MemRegion last_setup_region() const      { return _last_setup_region; }
69 
70  public:
71   virtual ~MutableSpace();
72   MutableSpace(size_t page_size);
73 
74   // Accessors
bottom() const75   HeapWord* bottom() const                 { return _bottom; }
top() const76   HeapWord* top() const                    { return _top;    }
end() const77   HeapWord* end() const                    { return _end; }
78 
set_bottom(HeapWord * value)79   void set_bottom(HeapWord* value)         { _bottom = value; }
set_top(HeapWord * value)80   virtual void set_top(HeapWord* value)    { _top = value;   }
set_end(HeapWord * value)81   void set_end(HeapWord* value)            { _end = value; }
82 
top_addr()83   HeapWord* volatile* top_addr()           { return &_top; }
end_addr()84   HeapWord** end_addr()                    { return &_end; }
85 
alignment()86   size_t alignment()                       { return _alignment; }
87 
region() const88   MemRegion region() const { return MemRegion(bottom(), end()); }
89 
capacity_in_bytes() const90   size_t capacity_in_bytes() const { return capacity_in_words() * HeapWordSize; }
capacity_in_words() const91   size_t capacity_in_words() const { return pointer_delta(end(), bottom()); }
capacity_in_words(Thread *) const92   virtual size_t capacity_in_words(Thread*) const { return capacity_in_words(); }
93 
94   // Returns a subregion containing all objects in this space.
used_region()95   MemRegion used_region() { return MemRegion(bottom(), top()); }
96 
97   static const bool SetupPages = true;
98   static const bool DontSetupPages = false;
99 
100   // Initialization
101   virtual void initialize(MemRegion mr,
102                           bool clear_space,
103                           bool mangle_space,
104                           bool setup_pages = SetupPages,
105                           WorkGang* pretouch_gang = NULL);
106 
107   virtual void clear(bool mangle_space);
update()108   virtual void update() { }
accumulate_statistics()109   virtual void accumulate_statistics() { }
110 
111   // Methods used in mangling.  See descriptions under SpaceMangler.
112   virtual void mangle_unused_area() PRODUCT_RETURN;
113   virtual void mangle_unused_area_complete() PRODUCT_RETURN;
114   virtual void check_mangled_unused_area(HeapWord* limit) PRODUCT_RETURN;
115   virtual void check_mangled_unused_area_complete() PRODUCT_RETURN;
116   virtual void set_top_for_allocations(HeapWord* v) PRODUCT_RETURN;
117 
118   // Used to save the space's current top for later use during mangling.
119   virtual void set_top_for_allocations() PRODUCT_RETURN;
120 
ensure_parsability()121   virtual void ensure_parsability() { }
122 
123   virtual void mangle_region(MemRegion mr) PRODUCT_RETURN;
124 
125   // Boolean queries.
is_empty() const126   bool is_empty() const              { return used_in_words() == 0; }
not_empty() const127   bool not_empty() const             { return used_in_words() > 0; }
contains(const void * p) const128   bool contains(const void* p) const { return _bottom <= p && p < _end; }
129 
130   // Size computations.  Sizes are in bytes.
used_in_bytes() const131   size_t used_in_bytes() const                { return used_in_words() * HeapWordSize; }
free_in_bytes() const132   size_t free_in_bytes() const                { return free_in_words() * HeapWordSize; }
133 
134   // Size computations.  Sizes are in heapwords.
used_in_words() const135   virtual size_t used_in_words() const                    { return pointer_delta(top(), bottom()); }
free_in_words() const136   virtual size_t free_in_words() const                    { return pointer_delta(end(),    top()); }
tlab_capacity(Thread * thr) const137   virtual size_t tlab_capacity(Thread* thr) const         { return capacity_in_bytes();            }
tlab_used(Thread * thr) const138   virtual size_t tlab_used(Thread* thr) const             { return used_in_bytes();                }
unsafe_max_tlab_alloc(Thread * thr) const139   virtual size_t unsafe_max_tlab_alloc(Thread* thr) const { return free_in_bytes();                }
140 
141   // Allocation (return NULL if full)
142   virtual HeapWord* cas_allocate(size_t word_size);
143   // Optional deallocation. Used in NUMA-allocator.
144   bool cas_deallocate(HeapWord *obj, size_t size);
145   // Return true if this space needs to be expanded in order to satisfy an
146   // allocation request of the indicated size.  Concurrent allocations and
147   // resizes may change the result of a later call.  Used by oldgen allocator.
148   // precondition: holding ExpandHeap_lock
149   bool needs_expand(size_t word_size) const;
150 
151   // Iteration.
152   void oop_iterate(OopIterateClosure* cl);
153   void object_iterate(ObjectClosure* cl);
154 
155   // Debugging
156   virtual void print() const;
157   virtual void print_on(outputStream* st) const;
158   virtual void print_short() const;
159   virtual void print_short_on(outputStream* st) const;
160   virtual void verify();
161 };
162 
163 #endif // SHARE_GC_PARALLEL_MUTABLESPACE_HPP
164