1 /*
2  * Copyright (c) 2015, 2020, 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/zList.hpp"
29 #include "gc/z/zLock.hpp"
30 #include "gc/z/zPageCache.hpp"
31 #include "gc/z/zPhysicalMemory.hpp"
32 #include "gc/z/zSafeDelete.hpp"
33 #include "gc/z/zVirtualMemory.hpp"
34 
35 class ThreadClosure;
36 class ZPageAllocation;
37 class ZWorkers;
38 class ZUncommitter;
39 class ZUnmapper;
40 
41 class ZPageAllocator {
42   friend class VMStructs;
43   friend class ZUnmapper;
44   friend class ZUncommitter;
45 
46 private:
47   ZLock                      _lock;
48   ZPageCache                 _cache;
49   ZVirtualMemoryManager      _virtual;
50   ZPhysicalMemoryManager     _physical;
51   const size_t               _min_capacity;
52   const size_t               _max_capacity;
53   const size_t               _max_reserve;
54   volatile size_t            _current_max_capacity;
55   volatile size_t            _capacity;
56   volatile size_t            _claimed;
57   volatile size_t            _used;
58   size_t                     _used_high;
59   size_t                     _used_low;
60   size_t                     _allocated;
61   ssize_t                    _reclaimed;
62   ZList<ZPageAllocation>     _stalled;
63   ZList<ZPageAllocation>     _satisfied;
64   ZUnmapper*                 _unmapper;
65   ZUncommitter*              _uncommitter;
66   mutable ZSafeDelete<ZPage> _safe_delete;
67   bool                       _initialized;
68 
69   bool prime_cache(ZWorkers* workers, size_t size);
70 
71   size_t increase_capacity(size_t size);
72   void decrease_capacity(size_t size, bool set_max_capacity);
73 
74   void increase_used(size_t size, bool relocation);
75   void decrease_used(size_t size, bool reclaimed);
76 
77   bool commit_page(ZPage* page);
78   void uncommit_page(ZPage* page);
79 
80   void map_page(const ZPage* page) const;
81   void unmap_page(const ZPage* page) const;
82 
83   void destroy_page(ZPage* page);
84 
85   bool is_alloc_allowed(size_t size, bool no_reserve) const;
86   bool is_alloc_allowed_from_cache(size_t size, bool no_reserve) const;
87 
88   bool alloc_page_common_inner(uint8_t type, size_t size, bool no_reserve, 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                  size_t max_reserve);
108 
109   bool is_initialized() const;
110 
111   size_t min_capacity() const;
112   size_t max_capacity() const;
113   size_t soft_max_capacity() const;
114   size_t capacity() const;
115   size_t max_reserve() const;
116   size_t used_high() const;
117   size_t used_low() const;
118   size_t used() const;
119   size_t unused() const;
120   size_t allocated() const;
121   size_t reclaimed() const;
122 
123   void reset_statistics();
124 
125   ZPage* alloc_page(uint8_t type, size_t size, ZAllocationFlags flags);
126   void free_page(ZPage* page, bool reclaimed);
127 
128   void enable_deferred_delete() const;
129   void disable_deferred_delete() const;
130 
131   void debug_map_page(const ZPage* page) const;
132   void debug_unmap_page(const ZPage* page) const;
133 
134   bool is_alloc_stalled() const;
135   void check_out_of_memory();
136 
137   void pages_do(ZPageClosure* cl) const;
138 
139   void threads_do(ThreadClosure* tc) const;
140 };
141 
142 #endif // SHARE_GC_Z_ZPAGEALLOCATOR_HPP
143