8221260: Initialize more class members on construction, remove some unused ones
Reviewed-by:

0 /*                                                                                                                         
1  * Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.                                            
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.                                                           
3  *                                                                                                                         
4  * This code is free software; you can redistribute it and/or modify it                                                    
5  * under the terms of the GNU General Public License version 2 only, as                                                    
6  * published by the Free Software Foundation.                                                                              
7  *                                                                                                                         
8  * This code is distributed in the hope that it will be useful, but WITHOUT                                                
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or                                                   
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License                                                   
11  * version 2 for more details (a copy is included in the LICENSE file that                                                 
12  * accompanied this code).                                                                                                 
13  *                                                                                                                         
14  * You should have received a copy of the GNU General Public License version                                               
15  * 2 along with this work; if not, write to the Free Software Foundation,                                                  
16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.                                                           
17  *                                                                                                                         
18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA                                                 
19  * or visit www.oracle.com if you need additional information or have any                                                  
20  * questions.                                                                                                              
21  *                                                                                                                         
22  */                                                                                                                        
23 
24 #include "precompiled.hpp"                                                                                                 
25 #include "gc/g1/g1BiasedArray.hpp"                                                                                         
26 #include "gc/g1/g1RegionToSpaceMapper.hpp"                                                                                 
27 #include "logging/log.hpp"                                                                                                 
28 #include "memory/allocation.inline.hpp"                                                                                    
29 #include "memory/virtualspace.hpp"                                                                                         
30 #include "runtime/java.hpp"                                                                                                
31 #include "runtime/os.inline.hpp"                                                                                           
32 #include "services/memTracker.hpp"                                                                                         
33 #include "utilities/align.hpp"                                                                                             
34 #include "utilities/bitMap.inline.hpp"                                                                                     
35 #include "utilities/formatBuffer.hpp"                                                                                      
36 
37 G1RegionToSpaceMapper::G1RegionToSpaceMapper(ReservedSpace rs,                                                             
38                                              size_t used_size,                                                             
39                                              size_t page_size,                                                             
40                                              size_t region_granularity,                                                    
41                                              size_t commit_factor,                                                         
42                                              MemoryType type) :                                                            
43   _listener(NULL),                                                                                                         
44   _storage(rs, used_size, page_size),                                                                                      
45   _region_granularity(region_granularity),                                                                                 
46   _commit_map(rs.size() * commit_factor / region_granularity, mtGC) {                                                      
47   guarantee(is_power_of_2(page_size), "must be");                                                                          
48   guarantee(is_power_of_2(region_granularity), "must be");                                                                 
49 
50   MemTracker::record_virtual_memory_type((address)rs.base(), type);                                                        
51 }                                                                                                                          
52 
53 // G1RegionToSpaceMapper implementation where the region granularity is larger than                                        
54 // or the same as the commit granularity.                                                                                  
55 // Basically, the space corresponding to one region region spans several OS pages.                                         
56 class G1RegionsLargerThanCommitSizeMapper : public G1RegionToSpaceMapper {                                                 
57  private:                                                                                                                  
58   size_t _pages_per_region;                                                                                                
59 
60  public:                                                                                                                   
61   G1RegionsLargerThanCommitSizeMapper(ReservedSpace rs,                                                                    
62                                       size_t actual_size,                                                                  
63                                       size_t page_size,                                                                    
64                                       size_t alloc_granularity,                                                            
65                                       size_t commit_factor,                                                                
66                                       MemoryType type) :                                                                   
67     G1RegionToSpaceMapper(rs, actual_size, page_size, alloc_granularity, commit_factor, type),                             
68     _pages_per_region(alloc_granularity / (page_size * commit_factor)) {                                                   
69 
70     guarantee(alloc_granularity >= page_size, "allocation granularity smaller than commit granularity");                   
71   }                                                                                                                        
72 
73   virtual void commit_regions(uint start_idx, size_t num_regions, WorkGang* pretouch_gang) {                               
74     size_t const start_page = (size_t)start_idx * _pages_per_region;                                                       
75     bool zero_filled = _storage.commit(start_page, num_regions * _pages_per_region);                                       
76     if (AlwaysPreTouch) {                                                                                                  
77       _storage.pretouch(start_page, num_regions * _pages_per_region, pretouch_gang);                                       
78     }                                                                                                                      
79     _commit_map.set_range(start_idx, start_idx + num_regions);                                                             
80     fire_on_commit(start_idx, num_regions, zero_filled);                                                                   
81   }                                                                                                                        
82 
83   virtual void uncommit_regions(uint start_idx, size_t num_regions) {                                                      
84     _storage.uncommit((size_t)start_idx * _pages_per_region, num_regions * _pages_per_region);                             
85     _commit_map.clear_range(start_idx, start_idx + num_regions);                                                           
86   }                                                                                                                        
87 };                                                                                                                         
88 
89 // G1RegionToSpaceMapper implementation where the region granularity is smaller                                            
90 // than the commit granularity.                                                                                            
91 // Basically, the contents of one OS page span several regions.                                                            
92 class G1RegionsSmallerThanCommitSizeMapper : public G1RegionToSpaceMapper {                                                
93  private:                                                                                                                  
94   class CommitRefcountArray : public G1BiasedMappedArray<uint> {                                                           
95    protected:                                                                                                              
96      virtual uint default_value() const { return 0; }                                                                      
97   };                                                                                                                       
98 
99   size_t _regions_per_page;                                                                                                
100 
101   CommitRefcountArray _refcounts;                                                                                          
102 
103   uintptr_t region_idx_to_page_idx(uint region) const {                                                                    
104     return region / _regions_per_page;                                                                                     
105   }                                                                                                                        
106 
107  public:                                                                                                                   
108   G1RegionsSmallerThanCommitSizeMapper(ReservedSpace rs,                                                                   
109                                        size_t actual_size,                                                                 
110                                        size_t page_size,                                                                   
111                                        size_t alloc_granularity,                                                           
112                                        size_t commit_factor,                                                               
113                                        MemoryType type) :                                                                  
114     G1RegionToSpaceMapper(rs, actual_size, page_size, alloc_granularity, commit_factor, type),                             
115     _regions_per_page((page_size * commit_factor) / alloc_granularity), _refcounts() {                                     
116 
117     guarantee((page_size * commit_factor) >= alloc_granularity, "allocation granularity smaller than commit granularity"); 
118     _refcounts.initialize((HeapWord*)rs.base(), (HeapWord*)(rs.base() + align_up(rs.size(), page_size)), page_size);       
119   }                                                                                                                        
120 
121   virtual void commit_regions(uint start_idx, size_t num_regions, WorkGang* pretouch_gang) {                               
122     size_t const NoPage = ~(size_t)0;                                                                                      
123 
124     size_t first_committed = NoPage;                                                                                       
125     size_t num_committed = 0;                                                                                              
126 
127     bool all_zero_filled = true;                                                                                           
128 
129     for (uint i = start_idx; i < start_idx + num_regions; i++) {                                                           
130       assert(!_commit_map.at(i), "Trying to commit storage at region %u that is already committed", i);                    
131       size_t idx = region_idx_to_page_idx(i);                                                                              
132       uint old_refcount = _refcounts.get_by_index(idx);                                                                    
133 
134       bool zero_filled = false;                                                                                            
135       if (old_refcount == 0) {                                                                                             
136         if (first_committed == NoPage) {                                                                                   
137           first_committed = idx;                                                                                           
138           num_committed = 1;                                                                                               
139         } else {                                                                                                           
140           num_committed++;                                                                                                 
141         }                                                                                                                  
142         zero_filled = _storage.commit(idx, 1);                                                                             
143       }                                                                                                                    
144       all_zero_filled &= zero_filled;                                                                                      
145 
146       _refcounts.set_by_index(idx, old_refcount + 1);                                                                      
147       _commit_map.set_bit(i);                                                                                              
148     }                                                                                                                      
149     if (AlwaysPreTouch && num_committed > 0) {                                                                             
150       _storage.pretouch(first_committed, num_committed, pretouch_gang);                                                    
151     }                                                                                                                      
152     fire_on_commit(start_idx, num_regions, all_zero_filled);                                                               
153   }                                                                                                                        
154 
155   virtual void uncommit_regions(uint start_idx, size_t num_regions) {                                                      
156     for (uint i = start_idx; i < start_idx + num_regions; i++) {                                                           
157       assert(_commit_map.at(i), "Trying to uncommit storage at region %u that is not committed", i);                       
158       size_t idx = region_idx_to_page_idx(i);                                                                              
159       uint old_refcount = _refcounts.get_by_index(idx);                                                                    
160       assert(old_refcount > 0, "must be");                                                                                 
161       if (old_refcount == 1) {                                                                                             
162         _storage.uncommit(idx, 1);                                                                                         
163       }                                                                                                                    
164       _refcounts.set_by_index(idx, old_refcount - 1);                                                                      
165       _commit_map.clear_bit(i);                                                                                            
166     }                                                                                                                      
167   }                                                                                                                        
168 };                                                                                                                         
169 
170 void G1RegionToSpaceMapper::fire_on_commit(uint start_idx, size_t num_regions, bool zero_filled) {                         
171   if (_listener != NULL) {                                                                                                 
172     _listener->on_commit(start_idx, num_regions, zero_filled);                                                             
173   }                                                                                                                        
174 }                                                                                                                          
175 
176 static bool map_nvdimm_space(ReservedSpace rs) {                                                                           
177   assert(AllocateOldGenAt != NULL, "");                                                                                    
178   int _backing_fd = os::create_file_for_heap(AllocateOldGenAt);                                                            
179   if (_backing_fd == -1) {                                                                                                 
180     log_error(gc, init)("Could not create file for Old generation at location %s", AllocateOldGenAt);                      
181     return false;                                                                                                          
182   }                                                                                                                        
183   // commit this memory in nv-dimm                                                                                         
184   char* ret = os::attempt_reserve_memory_at(rs.size(), rs.base(), _backing_fd);                                            
185 
186   if (ret != rs.base()) {                                                                                                  
187     if (ret != NULL) {                                                                                                     
188       os::unmap_memory(rs.base(), rs.size());                                                                              
189     }                                                                                                                      
190     log_error(gc, init)("Error in mapping Old Gen to given AllocateOldGenAt = %s", AllocateOldGenAt);                      
191     os::close(_backing_fd);                                                                                                
192     return false;                                                                                                          
193   }                                                                                                                        
194 
195   os::close(_backing_fd);                                                                                                  
196   return true;                                                                                                             
197 }                                                                                                                          
198 
199 G1RegionToHeteroSpaceMapper::G1RegionToHeteroSpaceMapper(ReservedSpace rs,                                                 
200                                                          size_t actual_size,                                               
201                                                          size_t page_size,                                                 
202                                                          size_t alloc_granularity,                                         
203                                                          size_t commit_factor,                                             
204                                                          MemoryType type) :                                                
205   G1RegionToSpaceMapper(rs, actual_size, page_size, alloc_granularity, commit_factor, type),                               
206   _rs(rs),                                                                                                                 
                                                                                                                           
207   _num_committed_dram(0),                                                                                                  
208   _num_committed_nvdimm(0),                                                                                                
                                                                                                                           
209   _page_size(page_size),                                                                                                   
210   _commit_factor(commit_factor),                                                                                           
211   _type(type) {                                                                                                            
212   assert(actual_size == 2 * MaxHeapSize, "For 2-way heterogenuous heap, reserved space is two times MaxHeapSize");         
213 }                                                                                                                          
214 
215 bool G1RegionToHeteroSpaceMapper::initialize() {                                                                           
216   // Since we need to re-map the reserved space - 'Xmx' to nv-dimm and 'Xmx' to dram, we need to release the reserved memor
217   // Because on some OSes (e.g. Windows) you cannot do a file mapping on memory reserved with regular mapping.             
218   os::release_memory(_rs.base(), _rs.size());                                                                              
219   // First half of size Xmx is for nv-dimm.                                                                                
220   ReservedSpace rs_nvdimm = _rs.first_part(MaxHeapSize);                                                                   
221   assert(rs_nvdimm.base() == _rs.base(), "We should get the same base address");                                           
222 
223   // Second half of reserved memory is mapped to dram.                                                                     
224   ReservedSpace rs_dram = _rs.last_part(MaxHeapSize);                                                                      
225 
226   assert(rs_dram.size() == rs_nvdimm.size() && rs_nvdimm.size() == MaxHeapSize, "They all should be same");                
227 
228   // Reserve dram memory                                                                                                   
229   char* base = os::attempt_reserve_memory_at(rs_dram.size(), rs_dram.base());                                              
230   if (base != rs_dram.base()) {                                                                                            
231     if (base != NULL) {                                                                                                    
232       os::release_memory(base, rs_dram.size());                                                                            
233     }                                                                                                                      
234     log_error(gc, init)("Error in re-mapping memory on dram during G1 heterogenous memory initialization");                
235     return false;                                                                                                          
236   }                                                                                                                        
237 
238   // We reserve and commit this entire space to NV-DIMM.                                                                   
239   if (!map_nvdimm_space(rs_nvdimm)) {                                                                                      
240     log_error(gc, init)("Error in re-mapping memory to nv-dimm during G1 heterogenous memory initialization");             
241     return false;                                                                                                          
242   }                                                                                                                        
243 
244   if (_region_granularity >= (_page_size * _commit_factor)) {                                                              
245     _dram_mapper = new G1RegionsLargerThanCommitSizeMapper(rs_dram, rs_dram.size(), _page_size, _region_granularity, _commi
246   } else {                                                                                                                 
247     _dram_mapper = new G1RegionsSmallerThanCommitSizeMapper(rs_dram, rs_dram.size(), _page_size, _region_granularity, _comm
248   }                                                                                                                        
249 
250   _start_index_of_nvdimm = 0;                                                                                              
251   _start_index_of_dram = (uint)(rs_nvdimm.size() / _region_granularity);                                                   
252   return true;                                                                                                             
253 }                                                                                                                          
254 
255 void G1RegionToHeteroSpaceMapper::commit_regions(uint start_idx, size_t num_regions, WorkGang* pretouch_gang) {            
256   uint end_idx = (start_idx + (uint)num_regions - 1);                                                                      
257 
258   uint num_dram = end_idx >= _start_index_of_dram ? MIN2((end_idx - _start_index_of_dram + 1), (uint)num_regions) : 0;     
259   uint num_nvdimm = (uint)num_regions - num_dram;                                                                          
260 
261   if (num_nvdimm > 0) {                                                                                                    
262     // We do not need to commit nv-dimm regions, since they are committed in the beginning.                                
263     _num_committed_nvdimm += num_nvdimm;                                                                                   
264   }                                                                                                                        
265   if (num_dram > 0) {                                                                                                      
266     _dram_mapper->commit_regions(start_idx > _start_index_of_dram ? (start_idx - _start_index_of_dram) : 0, num_dram, preto
267     _num_committed_dram += num_dram;                                                                                       
268   }                                                                                                                        
269 }                                                                                                                          
270 
271 void G1RegionToHeteroSpaceMapper::uncommit_regions(uint start_idx, size_t num_regions) {                                   
272   uint end_idx = (start_idx + (uint)num_regions - 1);                                                                      
273   uint num_dram = end_idx >= _start_index_of_dram ? MIN2((end_idx - _start_index_of_dram + 1), (uint)num_regions) : 0;     
274   uint num_nvdimm = (uint)num_regions - num_dram;                                                                          
275 
276   if (num_nvdimm > 0) {                                                                                                    
277     // We do not uncommit memory for nv-dimm regions.                                                                      
278     _num_committed_nvdimm -= num_nvdimm;                                                                                   
279   }                                                                                                                        
280 
281   if (num_dram > 0) {                                                                                                      
282     _dram_mapper->uncommit_regions(start_idx > _start_index_of_dram ? (start_idx - _start_index_of_dram) : 0, num_dram);   
283     _num_committed_dram -= num_dram;                                                                                       
284   }                                                                                                                        
285 }                                                                                                                          
286 
287 uint G1RegionToHeteroSpaceMapper::num_committed_dram() const {                                                             
288   return _num_committed_dram;                                                                                              
289 }                                                                                                                          
290 
291 uint G1RegionToHeteroSpaceMapper::num_committed_nvdimm() const {                                                           
292   return _num_committed_nvdimm;                                                                                            
293 }                                                                                                                          
294 
295 G1RegionToSpaceMapper* G1RegionToSpaceMapper::create_heap_mapper(ReservedSpace rs,                                         
296                                                                  size_t actual_size,                                       
297                                                                  size_t page_size,                                         
298                                                                  size_t region_granularity,                                
299                                                                  size_t commit_factor,                                     
300                                                                  MemoryType type) {                                        
301   if (AllocateOldGenAt != NULL) {                                                                                          
302     G1RegionToHeteroSpaceMapper* mapper = new G1RegionToHeteroSpaceMapper(rs, actual_size, page_size, region_granularity, c
303     if (!mapper->initialize()) {                                                                                           
304       delete mapper;                                                                                                       
305       return NULL;                                                                                                         
306     }                                                                                                                      
307     return (G1RegionToSpaceMapper*)mapper;                                                                                 
308   } else {                                                                                                                 
309     return create_mapper(rs, actual_size, page_size, region_granularity, commit_factor, type);                             
310   }                                                                                                                        
311 }                                                                                                                          
312 
313 G1RegionToSpaceMapper* G1RegionToSpaceMapper::create_mapper(ReservedSpace rs,                                              
314                                                             size_t actual_size,                                            
315                                                             size_t page_size,                                              
316                                                             size_t region_granularity,                                     
317                                                             size_t commit_factor,                                          
318                                                             MemoryType type) {                                             
319   if (region_granularity >= (page_size * commit_factor)) {                                                                 
320     return new G1RegionsLargerThanCommitSizeMapper(rs, actual_size, page_size, region_granularity, commit_factor, type);   
321   } else {                                                                                                                 
322     return new G1RegionsSmallerThanCommitSizeMapper(rs, actual_size, page_size, region_granularity, commit_factor, type);  
323   }                                                                                                                        
324 }                                                                                                                          
325 
326 void G1RegionToSpaceMapper::commit_and_set_special() {                                                                     
327   _storage.commit_and_set_special();                                                                                       
328 }                                                                                                                          
--- EOF ---