1 /*
2  * Copyright (c) 2001, 2019, 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 #include "precompiled.hpp"
26 #include "gc/g1/g1BiasedArray.hpp"
27 #include "gc/g1/g1NUMA.hpp"
28 #include "gc/g1/g1RegionToSpaceMapper.hpp"
29 #include "logging/log.hpp"
30 #include "memory/allocation.inline.hpp"
31 #include "memory/virtualspace.hpp"
32 #include "runtime/java.hpp"
33 #include "runtime/os.inline.hpp"
34 #include "services/memTracker.hpp"
35 #include "utilities/align.hpp"
36 #include "utilities/bitMap.inline.hpp"
37 #include "utilities/formatBuffer.hpp"
38 #include "utilities/powerOfTwo.hpp"
39 
G1RegionToSpaceMapper(ReservedSpace rs,size_t used_size,size_t page_size,size_t region_granularity,size_t commit_factor,MemoryType type)40 G1RegionToSpaceMapper::G1RegionToSpaceMapper(ReservedSpace rs,
41                                              size_t used_size,
42                                              size_t page_size,
43                                              size_t region_granularity,
44                                              size_t commit_factor,
45                                              MemoryType type) :
46   _listener(NULL),
47   _storage(rs, used_size, page_size),
48   _region_granularity(region_granularity),
49   _region_commit_map(rs.size() * commit_factor / region_granularity, mtGC),
50   _memory_type(type) {
51   guarantee(is_power_of_2(page_size), "must be");
52   guarantee(is_power_of_2(region_granularity), "must be");
53 
54   MemTracker::record_virtual_memory_type((address)rs.base(), type);
55 }
56 
57 // G1RegionToSpaceMapper implementation where the region granularity is larger than
58 // or the same as the commit granularity.
59 // Basically, the space corresponding to one region region spans several OS pages.
60 class G1RegionsLargerThanCommitSizeMapper : public G1RegionToSpaceMapper {
61  private:
62   size_t _pages_per_region;
63 
64  public:
G1RegionsLargerThanCommitSizeMapper(ReservedSpace rs,size_t actual_size,size_t page_size,size_t alloc_granularity,size_t commit_factor,MemoryType type)65   G1RegionsLargerThanCommitSizeMapper(ReservedSpace rs,
66                                       size_t actual_size,
67                                       size_t page_size,
68                                       size_t alloc_granularity,
69                                       size_t commit_factor,
70                                       MemoryType type) :
71     G1RegionToSpaceMapper(rs, actual_size, page_size, alloc_granularity, commit_factor, type),
72     _pages_per_region(alloc_granularity / (page_size * commit_factor)) {
73 
74     guarantee(alloc_granularity >= page_size, "allocation granularity smaller than commit granularity");
75   }
76 
commit_regions(uint start_idx,size_t num_regions,WorkGang * pretouch_gang)77   virtual void commit_regions(uint start_idx, size_t num_regions, WorkGang* pretouch_gang) {
78     const size_t start_page = (size_t)start_idx * _pages_per_region;
79     const size_t size_in_pages = num_regions * _pages_per_region;
80     bool zero_filled = _storage.commit(start_page, size_in_pages);
81     if (_memory_type == mtJavaHeap) {
82       for (uint region_index = start_idx; region_index < start_idx + num_regions; region_index++ ) {
83         void* address = _storage.page_start(region_index * _pages_per_region);
84         size_t size_in_bytes = _storage.page_size() * _pages_per_region;
85         G1NUMA::numa()->request_memory_on_node(address, size_in_bytes, region_index);
86       }
87     }
88     if (AlwaysPreTouch) {
89       _storage.pretouch(start_page, size_in_pages, pretouch_gang);
90     }
91     _region_commit_map.set_range(start_idx, start_idx + num_regions);
92     fire_on_commit(start_idx, num_regions, zero_filled);
93   }
94 
uncommit_regions(uint start_idx,size_t num_regions)95   virtual void uncommit_regions(uint start_idx, size_t num_regions) {
96     _storage.uncommit((size_t)start_idx * _pages_per_region, num_regions * _pages_per_region);
97     _region_commit_map.clear_range(start_idx, start_idx + num_regions);
98   }
99 };
100 
101 // G1RegionToSpaceMapper implementation where the region granularity is smaller
102 // than the commit granularity.
103 // Basically, the contents of one OS page span several regions.
104 class G1RegionsSmallerThanCommitSizeMapper : public G1RegionToSpaceMapper {
105   size_t _regions_per_page;
106 
region_idx_to_page_idx(uint region_idx) const107   size_t region_idx_to_page_idx(uint region_idx) const {
108     return region_idx / _regions_per_page;
109   }
110 
is_page_committed(size_t page_idx)111   bool is_page_committed(size_t page_idx) {
112     size_t region = page_idx * _regions_per_page;
113     size_t region_limit = region + _regions_per_page;
114     // Committed if there is a bit set in the range.
115     return _region_commit_map.get_next_one_offset(region, region_limit) != region_limit;
116   }
117 
numa_request_on_node(size_t page_idx)118   void numa_request_on_node(size_t page_idx) {
119     if (_memory_type == mtJavaHeap) {
120       uint region = (uint)(page_idx * _regions_per_page);
121       void* address = _storage.page_start(page_idx);
122       size_t size_in_bytes = _storage.page_size();
123       G1NUMA::numa()->request_memory_on_node(address, size_in_bytes, region);
124     }
125   }
126 
127  public:
G1RegionsSmallerThanCommitSizeMapper(ReservedSpace rs,size_t actual_size,size_t page_size,size_t alloc_granularity,size_t commit_factor,MemoryType type)128   G1RegionsSmallerThanCommitSizeMapper(ReservedSpace rs,
129                                        size_t actual_size,
130                                        size_t page_size,
131                                        size_t alloc_granularity,
132                                        size_t commit_factor,
133                                        MemoryType type) :
134     G1RegionToSpaceMapper(rs, actual_size, page_size, alloc_granularity, commit_factor, type),
135     _regions_per_page((page_size * commit_factor) / alloc_granularity) {
136 
137     guarantee((page_size * commit_factor) >= alloc_granularity, "allocation granularity smaller than commit granularity");
138   }
139 
commit_regions(uint start_idx,size_t num_regions,WorkGang * pretouch_gang)140   virtual void commit_regions(uint start_idx, size_t num_regions, WorkGang* pretouch_gang) {
141     uint region_limit = (uint)(start_idx + num_regions);
142     assert(num_regions > 0, "Must commit at least one region");
143     assert(_region_commit_map.get_next_one_offset(start_idx, region_limit) == region_limit,
144            "Should be no committed regions in the range [%u, %u)", start_idx, region_limit);
145 
146     size_t const NoPage = ~(size_t)0;
147 
148     size_t first_committed = NoPage;
149     size_t num_committed = 0;
150 
151     size_t start_page = region_idx_to_page_idx(start_idx);
152     size_t end_page = region_idx_to_page_idx(region_limit - 1);
153 
154     bool all_zero_filled = true;
155     for (size_t page = start_page; page <= end_page; page++) {
156       if (!is_page_committed(page)) {
157         // Page not committed.
158         if (num_committed == 0) {
159           first_committed = page;
160         }
161         num_committed++;
162 
163         if (!_storage.commit(page, 1)) {
164           // Found dirty region during commit.
165           all_zero_filled = false;
166         }
167 
168         // Move memory to correct NUMA node for the heap.
169         numa_request_on_node(page);
170       } else {
171         // Page already committed.
172         all_zero_filled = false;
173       }
174     }
175 
176     // Update the commit map for the given range.
177     _region_commit_map.set_range(start_idx, region_limit);
178 
179     if (AlwaysPreTouch && num_committed > 0) {
180       _storage.pretouch(first_committed, num_committed, pretouch_gang);
181     }
182 
183     fire_on_commit(start_idx, num_regions, all_zero_filled);
184   }
185 
uncommit_regions(uint start_idx,size_t num_regions)186   virtual void uncommit_regions(uint start_idx, size_t num_regions) {
187     uint region_limit = (uint)(start_idx + num_regions);
188     assert(num_regions > 0, "Must uncommit at least one region");
189     assert(_region_commit_map.get_next_zero_offset(start_idx, region_limit) == region_limit,
190            "Should only be committed regions in the range [%u, %u)", start_idx, region_limit);
191 
192     size_t start_page = region_idx_to_page_idx(start_idx);
193     size_t end_page = region_idx_to_page_idx(region_limit - 1);
194 
195     // Clear commit map for the given range.
196     _region_commit_map.clear_range(start_idx, region_limit);
197 
198     for (size_t page = start_page; page <= end_page; page++) {
199       // We know all pages were committed before clearing the map. If the
200       // the page is still marked as committed after the clear we should
201       // not uncommit it.
202       if (!is_page_committed(page)) {
203         _storage.uncommit(page, 1);
204       }
205     }
206   }
207 };
208 
fire_on_commit(uint start_idx,size_t num_regions,bool zero_filled)209 void G1RegionToSpaceMapper::fire_on_commit(uint start_idx, size_t num_regions, bool zero_filled) {
210   if (_listener != NULL) {
211     _listener->on_commit(start_idx, num_regions, zero_filled);
212   }
213 }
214 
map_nvdimm_space(ReservedSpace rs)215 static bool map_nvdimm_space(ReservedSpace rs) {
216   assert(AllocateOldGenAt != NULL, "");
217   int _backing_fd = os::create_file_for_heap(AllocateOldGenAt);
218   if (_backing_fd == -1) {
219     log_error(gc, init)("Could not create file for Old generation at location %s", AllocateOldGenAt);
220     return false;
221   }
222   // commit this memory in nv-dimm
223   char* ret = os::attempt_reserve_memory_at(rs.size(), rs.base(), _backing_fd);
224 
225   if (ret != rs.base()) {
226     if (ret != NULL) {
227       os::unmap_memory(rs.base(), rs.size());
228     }
229     log_error(gc, init)("Error in mapping Old Gen to given AllocateOldGenAt = %s", AllocateOldGenAt);
230     os::close(_backing_fd);
231     return false;
232   }
233 
234   os::close(_backing_fd);
235   return true;
236 }
237 
G1RegionToHeteroSpaceMapper(ReservedSpace rs,size_t actual_size,size_t page_size,size_t alloc_granularity,size_t commit_factor,MemoryType type)238 G1RegionToHeteroSpaceMapper::G1RegionToHeteroSpaceMapper(ReservedSpace rs,
239                                                          size_t actual_size,
240                                                          size_t page_size,
241                                                          size_t alloc_granularity,
242                                                          size_t commit_factor,
243                                                          MemoryType type) :
244   G1RegionToSpaceMapper(rs, actual_size, page_size, alloc_granularity, commit_factor, type),
245   _rs(rs),
246   _dram_mapper(NULL),
247   _num_committed_dram(0),
248   _num_committed_nvdimm(0),
249   _start_index_of_dram(0),
250   _page_size(page_size),
251   _commit_factor(commit_factor),
252   _type(type) {
253   assert(actual_size == 2 * MaxHeapSize, "For 2-way heterogenuous heap, reserved space is two times MaxHeapSize");
254 }
255 
initialize()256 bool G1RegionToHeteroSpaceMapper::initialize() {
257   // Since we need to re-map the reserved space - 'Xmx' to nv-dimm and 'Xmx' to dram, we need to release the reserved memory first.
258   // Because on some OSes (e.g. Windows) you cannot do a file mapping on memory reserved with regular mapping.
259   os::release_memory(_rs.base(), _rs.size());
260   // First half of size Xmx is for nv-dimm.
261   ReservedSpace rs_nvdimm = _rs.first_part(MaxHeapSize);
262   assert(rs_nvdimm.base() == _rs.base(), "We should get the same base address");
263 
264   // Second half of reserved memory is mapped to dram.
265   ReservedSpace rs_dram = _rs.last_part(MaxHeapSize);
266 
267   assert(rs_dram.size() == rs_nvdimm.size() && rs_nvdimm.size() == MaxHeapSize, "They all should be same");
268 
269   // Reserve dram memory
270   char* base = os::attempt_reserve_memory_at(rs_dram.size(), rs_dram.base());
271   if (base != rs_dram.base()) {
272     if (base != NULL) {
273       os::release_memory(base, rs_dram.size());
274     }
275     log_error(gc, init)("Error in re-mapping memory on dram during G1 heterogenous memory initialization");
276     return false;
277   }
278 
279   // We reserve and commit this entire space to NV-DIMM.
280   if (!map_nvdimm_space(rs_nvdimm)) {
281     log_error(gc, init)("Error in re-mapping memory to nv-dimm during G1 heterogenous memory initialization");
282     return false;
283   }
284 
285   if (_region_granularity >= (_page_size * _commit_factor)) {
286     _dram_mapper = new G1RegionsLargerThanCommitSizeMapper(rs_dram, rs_dram.size(), _page_size, _region_granularity, _commit_factor, _type);
287   } else {
288     _dram_mapper = new G1RegionsSmallerThanCommitSizeMapper(rs_dram, rs_dram.size(), _page_size, _region_granularity, _commit_factor, _type);
289   }
290 
291   _start_index_of_dram = (uint)(rs_nvdimm.size() / _region_granularity);
292   return true;
293 }
294 
commit_regions(uint start_idx,size_t num_regions,WorkGang * pretouch_gang)295 void G1RegionToHeteroSpaceMapper::commit_regions(uint start_idx, size_t num_regions, WorkGang* pretouch_gang) {
296   uint end_idx = (start_idx + (uint)num_regions - 1);
297 
298   uint num_dram = end_idx >= _start_index_of_dram ? MIN2((end_idx - _start_index_of_dram + 1), (uint)num_regions) : 0;
299   uint num_nvdimm = (uint)num_regions - num_dram;
300 
301   if (num_nvdimm > 0) {
302     // We do not need to commit nv-dimm regions, since they are committed in the beginning.
303     _num_committed_nvdimm += num_nvdimm;
304   }
305   if (num_dram > 0) {
306     _dram_mapper->commit_regions(start_idx > _start_index_of_dram ? (start_idx - _start_index_of_dram) : 0, num_dram, pretouch_gang);
307     _num_committed_dram += num_dram;
308   }
309 }
310 
uncommit_regions(uint start_idx,size_t num_regions)311 void G1RegionToHeteroSpaceMapper::uncommit_regions(uint start_idx, size_t num_regions) {
312   uint end_idx = (start_idx + (uint)num_regions - 1);
313   uint num_dram = end_idx >= _start_index_of_dram ? MIN2((end_idx - _start_index_of_dram + 1), (uint)num_regions) : 0;
314   uint num_nvdimm = (uint)num_regions - num_dram;
315 
316   if (num_nvdimm > 0) {
317     // We do not uncommit memory for nv-dimm regions.
318     _num_committed_nvdimm -= num_nvdimm;
319   }
320 
321   if (num_dram > 0) {
322     _dram_mapper->uncommit_regions(start_idx > _start_index_of_dram ? (start_idx - _start_index_of_dram) : 0, num_dram);
323     _num_committed_dram -= num_dram;
324   }
325 }
326 
num_committed_dram() const327 uint G1RegionToHeteroSpaceMapper::num_committed_dram() const {
328   return _num_committed_dram;
329 }
330 
num_committed_nvdimm() const331 uint G1RegionToHeteroSpaceMapper::num_committed_nvdimm() const {
332   return _num_committed_nvdimm;
333 }
334 
create_heap_mapper(ReservedSpace rs,size_t actual_size,size_t page_size,size_t region_granularity,size_t commit_factor,MemoryType type)335 G1RegionToSpaceMapper* G1RegionToSpaceMapper::create_heap_mapper(ReservedSpace rs,
336                                                                  size_t actual_size,
337                                                                  size_t page_size,
338                                                                  size_t region_granularity,
339                                                                  size_t commit_factor,
340                                                                  MemoryType type) {
341   if (AllocateOldGenAt != NULL) {
342     G1RegionToHeteroSpaceMapper* mapper = new G1RegionToHeteroSpaceMapper(rs, actual_size, page_size, region_granularity, commit_factor, type);
343     if (!mapper->initialize()) {
344       delete mapper;
345       return NULL;
346     }
347     return (G1RegionToSpaceMapper*)mapper;
348   } else {
349     return create_mapper(rs, actual_size, page_size, region_granularity, commit_factor, type);
350   }
351 }
352 
create_mapper(ReservedSpace rs,size_t actual_size,size_t page_size,size_t region_granularity,size_t commit_factor,MemoryType type)353 G1RegionToSpaceMapper* G1RegionToSpaceMapper::create_mapper(ReservedSpace rs,
354                                                             size_t actual_size,
355                                                             size_t page_size,
356                                                             size_t region_granularity,
357                                                             size_t commit_factor,
358                                                             MemoryType type) {
359   if (region_granularity >= (page_size * commit_factor)) {
360     return new G1RegionsLargerThanCommitSizeMapper(rs, actual_size, page_size, region_granularity, commit_factor, type);
361   } else {
362     return new G1RegionsSmallerThanCommitSizeMapper(rs, actual_size, page_size, region_granularity, commit_factor, type);
363   }
364 }
365 
commit_and_set_special()366 void G1RegionToSpaceMapper::commit_and_set_special() {
367   _storage.commit_and_set_special();
368 }
369