author | rfield |
Tue, 14 Nov 2017 19:33:37 -0800 (2017-11-15) | |
changeset 47840 | e0f08a49f3e3 |
parent 47553 | 5d20359dd938 |
child 48873 | 9536c39ac6de |
permissions | -rw-r--r-- |
13195 | 1 |
/* |
46711
0ccef2260315
8184991: NMT detail diff should take memory type into account
zgu
parents:
26288
diff
changeset
|
2 |
* Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved. |
13195 | 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 |
#include "precompiled.hpp" |
|
25946 | 25 |
|
13195 | 26 |
#include "memory/allocation.hpp" |
17074 | 27 |
#include "runtime/safepoint.hpp" |
28 |
#include "runtime/thread.inline.hpp" |
|
13195 | 29 |
#include "services/memBaseline.hpp" |
30 |
#include "services/memTracker.hpp" |
|
31 |
||
25946 | 32 |
/* |
33 |
* Sizes are sorted in descenting order for reporting |
|
34 |
*/ |
|
35 |
int compare_malloc_size(const MallocSite& s1, const MallocSite& s2) { |
|
36 |
if (s1.size() == s2.size()) { |
|
37 |
return 0; |
|
38 |
} else if (s1.size() > s2.size()) { |
|
39 |
return -1; |
|
40 |
} else { |
|
41 |
return 1; |
|
13195 | 42 |
} |
43 |
} |
|
44 |
||
45 |
||
25946 | 46 |
int compare_virtual_memory_size(const VirtualMemoryAllocationSite& s1, |
47 |
const VirtualMemoryAllocationSite& s2) { |
|
48 |
if (s1.reserved() == s2.reserved()) { |
|
49 |
return 0; |
|
50 |
} else if (s1.reserved() > s2.reserved()) { |
|
51 |
return -1; |
|
52 |
} else { |
|
53 |
return 1; |
|
13195 | 54 |
} |
25946 | 55 |
} |
13195 | 56 |
|
25946 | 57 |
// Sort into allocation site addresses order for baseline comparison |
58 |
int compare_malloc_site(const MallocSite& s1, const MallocSite& s2) { |
|
59 |
return s1.call_stack()->compare(*s2.call_stack()); |
|
13195 | 60 |
} |
61 |
||
46711
0ccef2260315
8184991: NMT detail diff should take memory type into account
zgu
parents:
26288
diff
changeset
|
62 |
// Sort into allocation site addresses and memory type order for baseline comparison |
0ccef2260315
8184991: NMT detail diff should take memory type into account
zgu
parents:
26288
diff
changeset
|
63 |
int compare_malloc_site_and_type(const MallocSite& s1, const MallocSite& s2) { |
0ccef2260315
8184991: NMT detail diff should take memory type into account
zgu
parents:
26288
diff
changeset
|
64 |
int res = compare_malloc_site(s1, s2); |
0ccef2260315
8184991: NMT detail diff should take memory type into account
zgu
parents:
26288
diff
changeset
|
65 |
if (res == 0) { |
0ccef2260315
8184991: NMT detail diff should take memory type into account
zgu
parents:
26288
diff
changeset
|
66 |
res = (int)(s1.flags() - s2.flags()); |
0ccef2260315
8184991: NMT detail diff should take memory type into account
zgu
parents:
26288
diff
changeset
|
67 |
} |
0ccef2260315
8184991: NMT detail diff should take memory type into account
zgu
parents:
26288
diff
changeset
|
68 |
|
0ccef2260315
8184991: NMT detail diff should take memory type into account
zgu
parents:
26288
diff
changeset
|
69 |
return res; |
0ccef2260315
8184991: NMT detail diff should take memory type into account
zgu
parents:
26288
diff
changeset
|
70 |
} |
13195 | 71 |
|
25946 | 72 |
int compare_virtual_memory_site(const VirtualMemoryAllocationSite& s1, |
73 |
const VirtualMemoryAllocationSite& s2) { |
|
74 |
return s1.call_stack()->compare(*s2.call_stack()); |
|
13195 | 75 |
} |
76 |
||
25946 | 77 |
/* |
78 |
* Walker to walk malloc allocation site table |
|
79 |
*/ |
|
80 |
class MallocAllocationSiteWalker : public MallocSiteWalker { |
|
81 |
private: |
|
26288 | 82 |
SortedLinkedList<MallocSite, compare_malloc_size> _malloc_sites; |
25946 | 83 |
size_t _count; |
84 |
||
85 |
// Entries in MallocSiteTable with size = 0 and count = 0, |
|
86 |
// when the malloc site is not longer there. |
|
87 |
public: |
|
26288 | 88 |
MallocAllocationSiteWalker() : _count(0) { } |
25946 | 89 |
|
90 |
inline size_t count() const { return _count; } |
|
91 |
||
92 |
LinkedList<MallocSite>* malloc_sites() { |
|
93 |
return &_malloc_sites; |
|
94 |
} |
|
95 |
||
96 |
bool do_malloc_site(const MallocSite* site) { |
|
97 |
if (site->size() >= MemBaseline::SIZE_THRESHOLD) { |
|
98 |
if (_malloc_sites.add(*site) != NULL) { |
|
99 |
_count++; |
|
100 |
return true; |
|
101 |
} else { |
|
102 |
return false; // OOM |
|
103 |
} |
|
104 |
} else { |
|
105 |
// malloc site does not meet threshold, ignore and continue |
|
106 |
return true; |
|
107 |
} |
|
108 |
} |
|
109 |
}; |
|
110 |
||
111 |
// Compare virtual memory region's base address |
|
112 |
int compare_virtual_memory_base(const ReservedMemoryRegion& r1, const ReservedMemoryRegion& r2) { |
|
113 |
return r1.compare(r2); |
|
13195 | 114 |
} |
115 |
||
25946 | 116 |
// Walk all virtual memory regions for baselining |
117 |
class VirtualMemoryAllocationWalker : public VirtualMemoryWalker { |
|
118 |
private: |
|
26288 | 119 |
SortedLinkedList<ReservedMemoryRegion, compare_virtual_memory_base> |
25946 | 120 |
_virtual_memory_regions; |
121 |
size_t _count; |
|
122 |
||
123 |
public: |
|
26288 | 124 |
VirtualMemoryAllocationWalker() : _count(0) { } |
25946 | 125 |
|
126 |
bool do_allocation_site(const ReservedMemoryRegion* rgn) { |
|
127 |
if (rgn->size() >= MemBaseline::SIZE_THRESHOLD) { |
|
128 |
if (_virtual_memory_regions.add(*rgn) != NULL) { |
|
129 |
_count ++; |
|
130 |
return true; |
|
131 |
} else { |
|
132 |
return false; |
|
13195 | 133 |
} |
134 |
} |
|
25946 | 135 |
return true; |
13195 | 136 |
} |
137 |
||
25946 | 138 |
LinkedList<ReservedMemoryRegion>* virtual_memory_allocations() { |
139 |
return &_virtual_memory_regions; |
|
140 |
} |
|
141 |
}; |
|
142 |
||
143 |
||
144 |
bool MemBaseline::baseline_summary() { |
|
26288 | 145 |
MallocMemorySummary::snapshot(&_malloc_memory_snapshot); |
146 |
VirtualMemorySummary::snapshot(&_virtual_memory_snapshot); |
|
47553
5d20359dd938
8186770: NMT: Report metadata information in NMT summary
zgu
parents:
47216
diff
changeset
|
147 |
MetaspaceSnapshot::snapshot(_metaspace_snapshot); |
25946 | 148 |
return true; |
149 |
} |
|
150 |
||
151 |
bool MemBaseline::baseline_allocation_sites() { |
|
152 |
// Malloc allocation sites |
|
26288 | 153 |
MallocAllocationSiteWalker malloc_walker; |
25946 | 154 |
if (!MallocSiteTable::walk_malloc_site(&malloc_walker)) { |
155 |
return false; |
|
156 |
} |
|
157 |
||
26288 | 158 |
_malloc_sites.move(malloc_walker.malloc_sites()); |
25946 | 159 |
// The malloc sites are collected in size order |
160 |
_malloc_sites_order = by_size; |
|
161 |
||
162 |
// Virtual memory allocation sites |
|
26288 | 163 |
VirtualMemoryAllocationWalker virtual_memory_walker; |
25946 | 164 |
if (!VirtualMemoryTracker::walk_virtual_memory(&virtual_memory_walker)) { |
165 |
return false; |
|
166 |
} |
|
167 |
||
168 |
// Virtual memory allocations are collected in call stack order |
|
26288 | 169 |
_virtual_memory_allocations.move(virtual_memory_walker.virtual_memory_allocations()); |
25946 | 170 |
|
171 |
if (!aggregate_virtual_memory_allocation_sites()) { |
|
172 |
return false; |
|
173 |
} |
|
174 |
// Virtual memory allocation sites are aggregrated in call stack order |
|
175 |
_virtual_memory_sites_order = by_address; |
|
13195 | 176 |
|
177 |
return true; |
|
178 |
} |
|
179 |
||
25946 | 180 |
bool MemBaseline::baseline(bool summaryOnly) { |
181 |
reset(); |
|
14120
7d298141c258
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
13195
diff
changeset
|
182 |
|
25946 | 183 |
_class_count = InstanceKlass::number_of_instance_classes(); |
14120
7d298141c258
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
13195
diff
changeset
|
184 |
|
25946 | 185 |
if (!baseline_summary()) { |
14120
7d298141c258
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
13195
diff
changeset
|
186 |
return false; |
7d298141c258
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
13195
diff
changeset
|
187 |
} |
7d298141c258
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
13195
diff
changeset
|
188 |
|
25946 | 189 |
_baseline_type = Summary_baselined; |
14120
7d298141c258
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
13195
diff
changeset
|
190 |
|
25946 | 191 |
// baseline details |
192 |
if (!summaryOnly && |
|
193 |
MemTracker::tracking_level() == NMT_detail) { |
|
194 |
baseline_allocation_sites(); |
|
195 |
_baseline_type = Detail_baselined; |
|
14120
7d298141c258
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
13195
diff
changeset
|
196 |
} |
7d298141c258
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
13195
diff
changeset
|
197 |
|
13195 | 198 |
return true; |
199 |
} |
|
200 |
||
25946 | 201 |
int compare_allocation_site(const VirtualMemoryAllocationSite& s1, |
202 |
const VirtualMemoryAllocationSite& s2) { |
|
203 |
return s1.call_stack()->compare(*s2.call_stack()); |
|
204 |
} |
|
205 |
||
206 |
bool MemBaseline::aggregate_virtual_memory_allocation_sites() { |
|
26288 | 207 |
SortedLinkedList<VirtualMemoryAllocationSite, compare_allocation_site> allocation_sites; |
25946 | 208 |
|
209 |
VirtualMemoryAllocationIterator itr = virtual_memory_allocations(); |
|
210 |
const ReservedMemoryRegion* rgn; |
|
211 |
VirtualMemoryAllocationSite* site; |
|
212 |
while ((rgn = itr.next()) != NULL) { |
|
213 |
VirtualMemoryAllocationSite tmp(*rgn->call_stack()); |
|
214 |
site = allocation_sites.find(tmp); |
|
215 |
if (site == NULL) { |
|
216 |
LinkedListNode<VirtualMemoryAllocationSite>* node = |
|
217 |
allocation_sites.add(tmp); |
|
218 |
if (node == NULL) return false; |
|
219 |
site = node->data(); |
|
220 |
} |
|
221 |
site->reserve_memory(rgn->size()); |
|
222 |
site->commit_memory(rgn->committed_size()); |
|
17074 | 223 |
} |
13195 | 224 |
|
26288 | 225 |
_virtual_memory_sites.move(&allocation_sites); |
25946 | 226 |
return true; |
13195 | 227 |
} |
228 |
||
25946 | 229 |
MallocSiteIterator MemBaseline::malloc_sites(SortingOrder order) { |
26288 | 230 |
assert(!_malloc_sites.is_empty(), "Not detail baseline"); |
25946 | 231 |
switch(order) { |
232 |
case by_size: |
|
233 |
malloc_sites_to_size_order(); |
|
234 |
break; |
|
235 |
case by_site: |
|
236 |
malloc_sites_to_allocation_site_order(); |
|
237 |
break; |
|
46711
0ccef2260315
8184991: NMT detail diff should take memory type into account
zgu
parents:
26288
diff
changeset
|
238 |
case by_site_and_type: |
0ccef2260315
8184991: NMT detail diff should take memory type into account
zgu
parents:
26288
diff
changeset
|
239 |
malloc_sites_to_allocation_site_and_type_order(); |
0ccef2260315
8184991: NMT detail diff should take memory type into account
zgu
parents:
26288
diff
changeset
|
240 |
break; |
25946 | 241 |
case by_address: |
242 |
default: |
|
243 |
ShouldNotReachHere(); |
|
13195 | 244 |
} |
25946 | 245 |
return MallocSiteIterator(_malloc_sites.head()); |
13195 | 246 |
} |
247 |
||
25946 | 248 |
VirtualMemorySiteIterator MemBaseline::virtual_memory_sites(SortingOrder order) { |
26288 | 249 |
assert(!_virtual_memory_sites.is_empty(), "Not detail baseline"); |
25946 | 250 |
switch(order) { |
251 |
case by_size: |
|
252 |
virtual_memory_sites_to_size_order(); |
|
253 |
break; |
|
254 |
case by_site: |
|
255 |
virtual_memory_sites_to_reservation_site_order(); |
|
256 |
break; |
|
257 |
case by_address: |
|
258 |
default: |
|
259 |
ShouldNotReachHere(); |
|
13195 | 260 |
} |
25946 | 261 |
return VirtualMemorySiteIterator(_virtual_memory_sites.head()); |
13195 | 262 |
} |
263 |
||
264 |
||
25946 | 265 |
// Sorting allocations sites in different orders |
266 |
void MemBaseline::malloc_sites_to_size_order() { |
|
267 |
if (_malloc_sites_order != by_size) { |
|
26288 | 268 |
SortedLinkedList<MallocSite, compare_malloc_size> tmp; |
13195 | 269 |
|
25946 | 270 |
// Add malloc sites to sorted linked list to sort into size order |
271 |
tmp.move(&_malloc_sites); |
|
272 |
_malloc_sites.set_head(tmp.head()); |
|
273 |
tmp.set_head(NULL); |
|
274 |
_malloc_sites_order = by_size; |
|
13195 | 275 |
} |
276 |
} |
|
277 |
||
25946 | 278 |
void MemBaseline::malloc_sites_to_allocation_site_order() { |
46711
0ccef2260315
8184991: NMT detail diff should take memory type into account
zgu
parents:
26288
diff
changeset
|
279 |
if (_malloc_sites_order != by_site && _malloc_sites_order != by_site_and_type) { |
26288 | 280 |
SortedLinkedList<MallocSite, compare_malloc_site> tmp; |
25946 | 281 |
// Add malloc sites to sorted linked list to sort into site (address) order |
282 |
tmp.move(&_malloc_sites); |
|
283 |
_malloc_sites.set_head(tmp.head()); |
|
284 |
tmp.set_head(NULL); |
|
285 |
_malloc_sites_order = by_site; |
|
286 |
} |
|
13195 | 287 |
} |
288 |
||
46711
0ccef2260315
8184991: NMT detail diff should take memory type into account
zgu
parents:
26288
diff
changeset
|
289 |
void MemBaseline::malloc_sites_to_allocation_site_and_type_order() { |
0ccef2260315
8184991: NMT detail diff should take memory type into account
zgu
parents:
26288
diff
changeset
|
290 |
if (_malloc_sites_order != by_site_and_type) { |
0ccef2260315
8184991: NMT detail diff should take memory type into account
zgu
parents:
26288
diff
changeset
|
291 |
SortedLinkedList<MallocSite, compare_malloc_site_and_type> tmp; |
0ccef2260315
8184991: NMT detail diff should take memory type into account
zgu
parents:
26288
diff
changeset
|
292 |
// Add malloc sites to sorted linked list to sort into site (address) order |
0ccef2260315
8184991: NMT detail diff should take memory type into account
zgu
parents:
26288
diff
changeset
|
293 |
tmp.move(&_malloc_sites); |
0ccef2260315
8184991: NMT detail diff should take memory type into account
zgu
parents:
26288
diff
changeset
|
294 |
_malloc_sites.set_head(tmp.head()); |
0ccef2260315
8184991: NMT detail diff should take memory type into account
zgu
parents:
26288
diff
changeset
|
295 |
tmp.set_head(NULL); |
0ccef2260315
8184991: NMT detail diff should take memory type into account
zgu
parents:
26288
diff
changeset
|
296 |
_malloc_sites_order = by_site_and_type; |
0ccef2260315
8184991: NMT detail diff should take memory type into account
zgu
parents:
26288
diff
changeset
|
297 |
} |
0ccef2260315
8184991: NMT detail diff should take memory type into account
zgu
parents:
26288
diff
changeset
|
298 |
} |
0ccef2260315
8184991: NMT detail diff should take memory type into account
zgu
parents:
26288
diff
changeset
|
299 |
|
25946 | 300 |
void MemBaseline::virtual_memory_sites_to_size_order() { |
301 |
if (_virtual_memory_sites_order != by_size) { |
|
26288 | 302 |
SortedLinkedList<VirtualMemoryAllocationSite, compare_virtual_memory_size> tmp; |
13195 | 303 |
|
25946 | 304 |
tmp.move(&_virtual_memory_sites); |
305 |
||
306 |
_virtual_memory_sites.set_head(tmp.head()); |
|
307 |
tmp.set_head(NULL); |
|
308 |
_virtual_memory_sites_order = by_size; |
|
309 |
} |
|
13195 | 310 |
} |
311 |
||
25946 | 312 |
void MemBaseline::virtual_memory_sites_to_reservation_site_order() { |
313 |
if (_virtual_memory_sites_order != by_size) { |
|
26288 | 314 |
SortedLinkedList<VirtualMemoryAllocationSite, compare_virtual_memory_site> tmp; |
13195 | 315 |
|
26288 | 316 |
tmp.move(&_virtual_memory_sites); |
25946 | 317 |
|
318 |
_virtual_memory_sites.set_head(tmp.head()); |
|
319 |
tmp.set_head(NULL); |
|
320 |
||
321 |
_virtual_memory_sites_order = by_size; |
|
322 |
} |
|
13195 | 323 |
} |
324 |