author | stefank |
Fri, 13 Feb 2015 14:37:35 +0100 | |
changeset 29081 | c61eb4914428 |
parent 26288 | 631bf42cf7c2 |
child 46711 | 0ccef2260315 |
permissions | -rw-r--r-- |
13195 | 1 |
/* |
25946 | 2 |
* Copyright (c) 2012, 2014, 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 |
||
62 |
||
25946 | 63 |
int compare_virtual_memory_site(const VirtualMemoryAllocationSite& s1, |
64 |
const VirtualMemoryAllocationSite& s2) { |
|
65 |
return s1.call_stack()->compare(*s2.call_stack()); |
|
13195 | 66 |
} |
67 |
||
25946 | 68 |
/* |
69 |
* Walker to walk malloc allocation site table |
|
70 |
*/ |
|
71 |
class MallocAllocationSiteWalker : public MallocSiteWalker { |
|
72 |
private: |
|
26288 | 73 |
SortedLinkedList<MallocSite, compare_malloc_size> _malloc_sites; |
25946 | 74 |
size_t _count; |
75 |
||
76 |
// Entries in MallocSiteTable with size = 0 and count = 0, |
|
77 |
// when the malloc site is not longer there. |
|
78 |
public: |
|
26288 | 79 |
MallocAllocationSiteWalker() : _count(0) { } |
25946 | 80 |
|
81 |
inline size_t count() const { return _count; } |
|
82 |
||
83 |
LinkedList<MallocSite>* malloc_sites() { |
|
84 |
return &_malloc_sites; |
|
85 |
} |
|
86 |
||
87 |
bool do_malloc_site(const MallocSite* site) { |
|
88 |
if (site->size() >= MemBaseline::SIZE_THRESHOLD) { |
|
89 |
if (_malloc_sites.add(*site) != NULL) { |
|
90 |
_count++; |
|
91 |
return true; |
|
92 |
} else { |
|
93 |
return false; // OOM |
|
94 |
} |
|
95 |
} else { |
|
96 |
// malloc site does not meet threshold, ignore and continue |
|
97 |
return true; |
|
98 |
} |
|
99 |
} |
|
100 |
}; |
|
101 |
||
102 |
// Compare virtual memory region's base address |
|
103 |
int compare_virtual_memory_base(const ReservedMemoryRegion& r1, const ReservedMemoryRegion& r2) { |
|
104 |
return r1.compare(r2); |
|
13195 | 105 |
} |
106 |
||
25946 | 107 |
// Walk all virtual memory regions for baselining |
108 |
class VirtualMemoryAllocationWalker : public VirtualMemoryWalker { |
|
109 |
private: |
|
26288 | 110 |
SortedLinkedList<ReservedMemoryRegion, compare_virtual_memory_base> |
25946 | 111 |
_virtual_memory_regions; |
112 |
size_t _count; |
|
113 |
||
114 |
public: |
|
26288 | 115 |
VirtualMemoryAllocationWalker() : _count(0) { } |
25946 | 116 |
|
117 |
bool do_allocation_site(const ReservedMemoryRegion* rgn) { |
|
118 |
if (rgn->size() >= MemBaseline::SIZE_THRESHOLD) { |
|
119 |
if (_virtual_memory_regions.add(*rgn) != NULL) { |
|
120 |
_count ++; |
|
121 |
return true; |
|
122 |
} else { |
|
123 |
return false; |
|
13195 | 124 |
} |
125 |
} |
|
25946 | 126 |
return true; |
13195 | 127 |
} |
128 |
||
25946 | 129 |
LinkedList<ReservedMemoryRegion>* virtual_memory_allocations() { |
130 |
return &_virtual_memory_regions; |
|
131 |
} |
|
132 |
}; |
|
133 |
||
134 |
||
135 |
bool MemBaseline::baseline_summary() { |
|
26288 | 136 |
MallocMemorySummary::snapshot(&_malloc_memory_snapshot); |
137 |
VirtualMemorySummary::snapshot(&_virtual_memory_snapshot); |
|
25946 | 138 |
return true; |
139 |
} |
|
140 |
||
141 |
bool MemBaseline::baseline_allocation_sites() { |
|
142 |
// Malloc allocation sites |
|
26288 | 143 |
MallocAllocationSiteWalker malloc_walker; |
25946 | 144 |
if (!MallocSiteTable::walk_malloc_site(&malloc_walker)) { |
145 |
return false; |
|
146 |
} |
|
147 |
||
26288 | 148 |
_malloc_sites.move(malloc_walker.malloc_sites()); |
25946 | 149 |
// The malloc sites are collected in size order |
150 |
_malloc_sites_order = by_size; |
|
151 |
||
152 |
// Virtual memory allocation sites |
|
26288 | 153 |
VirtualMemoryAllocationWalker virtual_memory_walker; |
25946 | 154 |
if (!VirtualMemoryTracker::walk_virtual_memory(&virtual_memory_walker)) { |
155 |
return false; |
|
156 |
} |
|
157 |
||
158 |
// Virtual memory allocations are collected in call stack order |
|
26288 | 159 |
_virtual_memory_allocations.move(virtual_memory_walker.virtual_memory_allocations()); |
25946 | 160 |
|
161 |
if (!aggregate_virtual_memory_allocation_sites()) { |
|
162 |
return false; |
|
163 |
} |
|
164 |
// Virtual memory allocation sites are aggregrated in call stack order |
|
165 |
_virtual_memory_sites_order = by_address; |
|
13195 | 166 |
|
167 |
return true; |
|
168 |
} |
|
169 |
||
25946 | 170 |
bool MemBaseline::baseline(bool summaryOnly) { |
171 |
reset(); |
|
14120
7d298141c258
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
13195
diff
changeset
|
172 |
|
25946 | 173 |
_class_count = InstanceKlass::number_of_instance_classes(); |
14120
7d298141c258
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
13195
diff
changeset
|
174 |
|
25946 | 175 |
if (!baseline_summary()) { |
14120
7d298141c258
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
13195
diff
changeset
|
176 |
return false; |
7d298141c258
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
13195
diff
changeset
|
177 |
} |
7d298141c258
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
13195
diff
changeset
|
178 |
|
25946 | 179 |
_baseline_type = Summary_baselined; |
14120
7d298141c258
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
13195
diff
changeset
|
180 |
|
25946 | 181 |
// baseline details |
182 |
if (!summaryOnly && |
|
183 |
MemTracker::tracking_level() == NMT_detail) { |
|
184 |
baseline_allocation_sites(); |
|
185 |
_baseline_type = Detail_baselined; |
|
14120
7d298141c258
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
13195
diff
changeset
|
186 |
} |
7d298141c258
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
13195
diff
changeset
|
187 |
|
13195 | 188 |
return true; |
189 |
} |
|
190 |
||
25946 | 191 |
int compare_allocation_site(const VirtualMemoryAllocationSite& s1, |
192 |
const VirtualMemoryAllocationSite& s2) { |
|
193 |
return s1.call_stack()->compare(*s2.call_stack()); |
|
194 |
} |
|
195 |
||
196 |
bool MemBaseline::aggregate_virtual_memory_allocation_sites() { |
|
26288 | 197 |
SortedLinkedList<VirtualMemoryAllocationSite, compare_allocation_site> allocation_sites; |
25946 | 198 |
|
199 |
VirtualMemoryAllocationIterator itr = virtual_memory_allocations(); |
|
200 |
const ReservedMemoryRegion* rgn; |
|
201 |
VirtualMemoryAllocationSite* site; |
|
202 |
while ((rgn = itr.next()) != NULL) { |
|
203 |
VirtualMemoryAllocationSite tmp(*rgn->call_stack()); |
|
204 |
site = allocation_sites.find(tmp); |
|
205 |
if (site == NULL) { |
|
206 |
LinkedListNode<VirtualMemoryAllocationSite>* node = |
|
207 |
allocation_sites.add(tmp); |
|
208 |
if (node == NULL) return false; |
|
209 |
site = node->data(); |
|
210 |
} |
|
211 |
site->reserve_memory(rgn->size()); |
|
212 |
site->commit_memory(rgn->committed_size()); |
|
17074 | 213 |
} |
13195 | 214 |
|
26288 | 215 |
_virtual_memory_sites.move(&allocation_sites); |
25946 | 216 |
return true; |
13195 | 217 |
} |
218 |
||
25946 | 219 |
MallocSiteIterator MemBaseline::malloc_sites(SortingOrder order) { |
26288 | 220 |
assert(!_malloc_sites.is_empty(), "Not detail baseline"); |
25946 | 221 |
switch(order) { |
222 |
case by_size: |
|
223 |
malloc_sites_to_size_order(); |
|
224 |
break; |
|
225 |
case by_site: |
|
226 |
malloc_sites_to_allocation_site_order(); |
|
227 |
break; |
|
228 |
case by_address: |
|
229 |
default: |
|
230 |
ShouldNotReachHere(); |
|
13195 | 231 |
} |
25946 | 232 |
return MallocSiteIterator(_malloc_sites.head()); |
13195 | 233 |
} |
234 |
||
25946 | 235 |
VirtualMemorySiteIterator MemBaseline::virtual_memory_sites(SortingOrder order) { |
26288 | 236 |
assert(!_virtual_memory_sites.is_empty(), "Not detail baseline"); |
25946 | 237 |
switch(order) { |
238 |
case by_size: |
|
239 |
virtual_memory_sites_to_size_order(); |
|
240 |
break; |
|
241 |
case by_site: |
|
242 |
virtual_memory_sites_to_reservation_site_order(); |
|
243 |
break; |
|
244 |
case by_address: |
|
245 |
default: |
|
246 |
ShouldNotReachHere(); |
|
13195 | 247 |
} |
25946 | 248 |
return VirtualMemorySiteIterator(_virtual_memory_sites.head()); |
13195 | 249 |
} |
250 |
||
251 |
||
25946 | 252 |
// Sorting allocations sites in different orders |
253 |
void MemBaseline::malloc_sites_to_size_order() { |
|
254 |
if (_malloc_sites_order != by_size) { |
|
26288 | 255 |
SortedLinkedList<MallocSite, compare_malloc_size> tmp; |
13195 | 256 |
|
25946 | 257 |
// Add malloc sites to sorted linked list to sort into size order |
258 |
tmp.move(&_malloc_sites); |
|
259 |
_malloc_sites.set_head(tmp.head()); |
|
260 |
tmp.set_head(NULL); |
|
261 |
_malloc_sites_order = by_size; |
|
13195 | 262 |
} |
263 |
} |
|
264 |
||
25946 | 265 |
void MemBaseline::malloc_sites_to_allocation_site_order() { |
266 |
if (_malloc_sites_order != by_site) { |
|
26288 | 267 |
SortedLinkedList<MallocSite, compare_malloc_site> tmp; |
25946 | 268 |
// Add malloc sites to sorted linked list to sort into site (address) order |
269 |
tmp.move(&_malloc_sites); |
|
270 |
_malloc_sites.set_head(tmp.head()); |
|
271 |
tmp.set_head(NULL); |
|
272 |
_malloc_sites_order = by_site; |
|
273 |
} |
|
13195 | 274 |
} |
275 |
||
25946 | 276 |
void MemBaseline::virtual_memory_sites_to_size_order() { |
277 |
if (_virtual_memory_sites_order != by_size) { |
|
26288 | 278 |
SortedLinkedList<VirtualMemoryAllocationSite, compare_virtual_memory_size> tmp; |
13195 | 279 |
|
25946 | 280 |
tmp.move(&_virtual_memory_sites); |
281 |
||
282 |
_virtual_memory_sites.set_head(tmp.head()); |
|
283 |
tmp.set_head(NULL); |
|
284 |
_virtual_memory_sites_order = by_size; |
|
285 |
} |
|
13195 | 286 |
} |
287 |
||
25946 | 288 |
void MemBaseline::virtual_memory_sites_to_reservation_site_order() { |
289 |
if (_virtual_memory_sites_order != by_size) { |
|
26288 | 290 |
SortedLinkedList<VirtualMemoryAllocationSite, compare_virtual_memory_site> tmp; |
13195 | 291 |
|
26288 | 292 |
tmp.move(&_virtual_memory_sites); |
25946 | 293 |
|
294 |
_virtual_memory_sites.set_head(tmp.head()); |
|
295 |
tmp.set_head(NULL); |
|
296 |
||
297 |
_virtual_memory_sites_order = by_size; |
|
298 |
} |
|
13195 | 299 |
} |
300 |