1 /*
2  * Copyright (c) 2015, 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 #ifndef SHARE_GC_Z_ZPAGEALLOCATOR_HPP
25 #define SHARE_GC_Z_ZPAGEALLOCATOR_HPP
26 
27 #include "gc/z/zAllocationFlags.hpp"
28 #include "gc/z/zArray.hpp"
29 #include "gc/z/zList.hpp"
30 #include "gc/z/zLock.hpp"
31 #include "gc/z/zPageCache.hpp"
32 #include "gc/z/zPhysicalMemory.hpp"
33 #include "gc/z/zSafeDelete.hpp"
34 #include "gc/z/zVirtualMemory.hpp"
35 
36 class ThreadClosure;
37 class ZPageAllocation;
38 class ZPageAllocatorStats;
39 class ZWorkers;
40 class ZUncommitter;
41 class ZUnmapper;
42 
43 class ZPageAllocator {
44   friend class VMStructs;
45   friend class ZUnmapper;
46   friend class ZUncommitter;
47 
48 private:
49   mutable ZLock              _lock;
50   ZPageCache                 _cache;
51   ZVirtualMemoryManager      _virtual;
52   ZPhysicalMemoryManager     _physical;
53   const size_t               _min_capacity;
54   const size_t               _max_capacity;
55   volatile size_t            _current_max_capacity;
56   volatile size_t            _capacity;
57   volatile size_t            _claimed;
58   volatile size_t            _used;
59   size_t                     _used_high;
60   size_t                     _used_low;
61   ssize_t                    _reclaimed;
62   ZList<ZPageAllocation>     _stalled;
63   volatile uint64_t          _nstalled;
64   ZList<ZPageAllocation>     _satisfied;
65   ZUnmapper*                 _unmapper;
66   ZUncommitter*              _uncommitter;
67   mutable ZSafeDelete<ZPage> _safe_delete;
68   bool                       _initialized;
69 
70   bool prime_cache(ZWorkers* workers, size_t size);
71 
72   size_t increase_capacity(size_t size);
73   void decrease_capacity(size_t size, bool set_max_capacity);
74 
75   void increase_used(size_t size, bool relocation);
76   void decrease_used(size_t size, bool reclaimed);
77 
78   bool commit_page(ZPage* page);
79   void uncommit_page(ZPage* page);
80 
81   void map_page(const ZPage* page) const;
82   void unmap_page(const ZPage* page) const;
83 
84   void destroy_page(ZPage* page);
85 
86   bool is_alloc_allowed(size_t size) const;
87 
88   bool alloc_page_common_inner(uint8_t type, size_t size, ZList<ZPage>* pages);
89   bool alloc_page_common(ZPageAllocation* allocation);
90   bool alloc_page_stall(ZPageAllocation* allocation);
91   bool alloc_page_or_stall(ZPageAllocation* allocation);
92   ZPage* alloc_page_create(ZPageAllocation* allocation);
93   ZPage* alloc_page_finalize(ZPageAllocation* allocation);
94   void alloc_page_failed(ZPageAllocation* allocation);
95 
96   void satisfy_stalled();
97 
98   void free_page_inner(ZPage* page, bool reclaimed);
99 
100   size_t uncommit(uint64_t* timeout);
101 
102 public:
103   ZPageAllocator(ZWorkers* workers,
104                  size_t min_capacity,
105                  size_t initial_capacity,
106                  size_t max_capacity);
107 
108   bool is_initialized() const;
109 
110   size_t min_capacity() const;
111   size_t max_capacity() const;
112   size_t soft_max_capacity() const;
113   size_t capacity() const;
114   size_t used() const;
115   size_t unused() const;
116 
117   ZPageAllocatorStats stats() const;
118 
119   void reset_statistics();
120 
121   ZPage* alloc_page(uint8_t type, size_t size, ZAllocationFlags flags);
122   void free_page(ZPage* page, bool reclaimed);
123   void free_pages(const ZArray<ZPage*>* pages, bool reclaimed);
124 
125   void enable_deferred_delete() const;
126   void disable_deferred_delete() const;
127 
128   void debug_map_page(const ZPage* page) const;
129   void debug_unmap_page(const ZPage* page) const;
130 
131   bool has_alloc_stalled() const;
132   void check_out_of_memory();
133 
134   void pages_do(ZPageClosure* cl) const;
135 
136   void threads_do(ThreadClosure* tc) const;
137 };
138 
139 class ZPageAllocatorStats {
140 private:
141   size_t _min_capacity;
142   size_t _max_capacity;
143   size_t _soft_max_capacity;
144   size_t _current_max_capacity;
145   size_t _capacity;
146   size_t _used;
147   size_t _used_high;
148   size_t _used_low;
149   size_t _reclaimed;
150 
151 public:
152   ZPageAllocatorStats(size_t min_capacity,
153                       size_t max_capacity,
154                       size_t soft_max_capacity,
155                       size_t capacity,
156                       size_t used,
157                       size_t used_high,
158                       size_t used_low,
159                       size_t reclaimed);
160 
161   size_t min_capacity() const;
162   size_t max_capacity() const;
163   size_t soft_max_capacity() const;
164   size_t capacity() const;
165   size_t used() const;
166   size_t used_high() const;
167   size_t used_low() const;
168   size_t reclaimed() const;
169 };
170 
171 #endif // SHARE_GC_Z_ZPAGEALLOCATOR_HPP
172