Skip to content

Commit 5caf686

Browse files
committed
8261644: NMT: Simplifications and cleanups
Reviewed-by: coleenp, zgu
1 parent ed93bc9 commit 5caf686

10 files changed

+135
-224
lines changed

src/hotspot/share/services/allocationSite.hpp

+5-9
Original file line numberDiff line numberDiff line change
@@ -30,30 +30,26 @@
3030

3131
// Allocation site represents a code path that makes a memory
3232
// allocation
33-
template <class E> class AllocationSite {
33+
class AllocationSite {
3434
private:
35-
NativeCallStack _call_stack;
36-
E e;
37-
MEMFLAGS _flag;
35+
const NativeCallStack _call_stack;
36+
const MEMFLAGS _flag;
3837
public:
3938
AllocationSite(const NativeCallStack& stack, MEMFLAGS flag) : _call_stack(stack), _flag(flag) { }
4039
int hash() const { return _call_stack.hash(); }
40+
4141
bool equals(const NativeCallStack& stack) const {
4242
return _call_stack.equals(stack);
4343
}
4444

45-
bool equals(const AllocationSite<E>& other) const {
45+
bool equals(const AllocationSite& other) const {
4646
return other.equals(_call_stack);
4747
}
4848

4949
const NativeCallStack* call_stack() const {
5050
return &_call_stack;
5151
}
5252

53-
// Information regarding this allocation
54-
E* data() { return &e; }
55-
const E* peek() const { return &e; }
56-
5753
MEMFLAGS flag() const { return _flag; }
5854
};
5955

src/hotspot/share/services/mallocSiteTable.cpp

+76
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,82 @@ void MallocSiteTable::AccessLock::exclusiveLock() {
242242
_lock_state = ExclusiveLock;
243243
}
244244

245+
void MallocSiteTable::print_tuning_statistics(outputStream* st) {
246+
247+
AccessLock locker(&_access_count);
248+
if (locker.sharedLock()) {
249+
// Total number of allocation sites, include empty sites
250+
int total_entries = 0;
251+
// Number of allocation sites that have all memory freed
252+
int empty_entries = 0;
253+
// Number of captured call stack distribution
254+
int stack_depth_distribution[NMT_TrackingStackDepth + 1] = { 0 };
255+
// Chain lengths
256+
int lengths[table_size] = { 0 };
257+
258+
for (int i = 0; i < table_size; i ++) {
259+
int this_chain_length = 0;
260+
const MallocSiteHashtableEntry* head = _table[i];
261+
while (head != NULL) {
262+
total_entries ++;
263+
this_chain_length ++;
264+
if (head->size() == 0) {
265+
empty_entries ++;
266+
}
267+
const int callstack_depth = head->peek()->call_stack()->frames();
268+
assert(callstack_depth >= 0 && callstack_depth <= NMT_TrackingStackDepth,
269+
"Sanity (%d)", callstack_depth);
270+
stack_depth_distribution[callstack_depth] ++;
271+
head = head->next();
272+
}
273+
lengths[i] = this_chain_length;
274+
}
275+
276+
st->print_cr("Malloc allocation site table:");
277+
st->print_cr("\tTotal entries: %d", total_entries);
278+
st->print_cr("\tEmpty entries: %d (%2.2f%%)", empty_entries, ((float)empty_entries * 100) / total_entries);
279+
st->cr();
280+
281+
// We report the hash distribution (chain length distribution) of the n shortest chains
282+
// - under the assumption that this usually contains all lengths. Reporting threshold
283+
// is 20, and the expected avg chain length is 5..6 (see table size).
284+
static const int chain_length_threshold = 20;
285+
int chain_length_distribution[chain_length_threshold] = { 0 };
286+
int over_threshold = 0;
287+
int longest_chain_length = 0;
288+
for (int i = 0; i < table_size; i ++) {
289+
if (lengths[i] >= chain_length_threshold) {
290+
over_threshold ++;
291+
} else {
292+
chain_length_distribution[lengths[i]] ++;
293+
}
294+
longest_chain_length = MAX2(longest_chain_length, lengths[i]);
295+
}
296+
297+
st->print_cr("Hash distribution:");
298+
if (chain_length_distribution[0] == 0) {
299+
st->print_cr("no empty buckets.");
300+
} else {
301+
st->print_cr("%d buckets are empty.", chain_length_distribution[0]);
302+
}
303+
for (int len = 1; len < MIN2(longest_chain_length + 1, chain_length_threshold); len ++) {
304+
st->print_cr("%2d %s: %d.", len, (len == 1 ? " entry" : "entries"), chain_length_distribution[len]);
305+
}
306+
if (longest_chain_length >= chain_length_threshold) {
307+
st->print_cr(">=%2d entries: %d.", chain_length_threshold, over_threshold);
308+
}
309+
st->print_cr("most entries: %d.", longest_chain_length);
310+
st->cr();
311+
312+
st->print_cr("Call stack depth distribution:");
313+
for (int i = 0; i <= NMT_TrackingStackDepth; i ++) {
314+
st->print_cr("\t%d: %d", i, stack_depth_distribution[i]);
315+
}
316+
st->cr();
317+
} // lock
318+
}
319+
320+
245321
bool MallocSiteHashtableEntry::atomic_insert(MallocSiteHashtableEntry* entry) {
246322
return Atomic::replace_if_null(&_next, entry);
247323
}

src/hotspot/share/services/mallocSiteTable.hpp

+9-19
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,19 @@
3838

3939
// MallocSite represents a code path that eventually calls
4040
// os::malloc() to allocate memory
41-
class MallocSite : public AllocationSite<MemoryCounter> {
41+
class MallocSite : public AllocationSite {
42+
MemoryCounter _c;
4243
public:
43-
MallocSite() :
44-
AllocationSite<MemoryCounter>(NativeCallStack::empty_stack(), mtNone) {}
45-
4644
MallocSite(const NativeCallStack& stack, MEMFLAGS flags) :
47-
AllocationSite<MemoryCounter>(stack, flags) {}
48-
45+
AllocationSite(stack, flags) {}
4946

50-
void allocate(size_t size) { data()->allocate(size); }
51-
void deallocate(size_t size) { data()->deallocate(size); }
47+
void allocate(size_t size) { _c.allocate(size); }
48+
void deallocate(size_t size) { _c.deallocate(size); }
5249

5350
// Memory allocated from this code path
54-
size_t size() const { return peek()->size(); }
51+
size_t size() const { return _c.size(); }
5552
// The number of calls were made
56-
size_t count() const { return peek()->count(); }
53+
size_t count() const { return _c.count(); }
5754
};
5855

5956
// Malloc site hashtable entry
@@ -63,7 +60,6 @@ class MallocSiteHashtableEntry : public CHeapObj<mtNMT> {
6360
MallocSiteHashtableEntry* volatile _next;
6461

6562
public:
66-
MallocSiteHashtableEntry() : _next(NULL) { }
6763

6864
MallocSiteHashtableEntry(NativeCallStack stack, MEMFLAGS flags):
6965
_malloc_site(stack, flags), _next(NULL) {
@@ -79,17 +75,9 @@ class MallocSiteHashtableEntry : public CHeapObj<mtNMT> {
7975
// The operation can be failed due to contention from other thread.
8076
bool atomic_insert(MallocSiteHashtableEntry* entry);
8177

82-
void set_callsite(const MallocSite& site) {
83-
_malloc_site = site;
84-
}
85-
8678
inline const MallocSite* peek() const { return &_malloc_site; }
8779
inline MallocSite* data() { return &_malloc_site; }
8880

89-
inline long hash() const { return _malloc_site.hash(); }
90-
inline bool equals(const NativeCallStack& stack) const {
91-
return _malloc_site.equals(stack);
92-
}
9381
// Allocation/deallocation on this allocation site
9482
inline void allocate(size_t size) { _malloc_site.allocate(size); }
9583
inline void deallocate(size_t size) { _malloc_site.deallocate(size); }
@@ -229,6 +217,8 @@ class MallocSiteTable : AllStatic {
229217
// Walk this table.
230218
static bool walk_malloc_site(MallocSiteWalker* walker);
231219

220+
static void print_tuning_statistics(outputStream* st);
221+
232222
private:
233223
static MallocSiteHashtableEntry* new_entry(const NativeCallStack& key, MEMFLAGS flags);
234224
static void reset();

src/hotspot/share/services/memTracker.cpp

+2-128
Original file line numberDiff line numberDiff line change
@@ -211,138 +211,12 @@ void MemTracker::report(bool summary_only, outputStream* output, size_t scale) {
211211
}
212212
}
213213

214-
// This is a walker to gather malloc site hashtable statistics,
215-
// the result is used for tuning.
216-
class StatisticsWalker : public MallocSiteWalker {
217-
private:
218-
enum Threshold {
219-
// aggregates statistics over this threshold into one
220-
// line item.
221-
report_threshold = 20
222-
};
223-
224-
private:
225-
// Number of allocation sites that have all memory freed
226-
int _empty_entries;
227-
// Total number of allocation sites, include empty sites
228-
int _total_entries;
229-
// Number of captured call stack distribution
230-
int _stack_depth_distribution[NMT_TrackingStackDepth];
231-
// Hash distribution
232-
int _hash_distribution[report_threshold];
233-
// Number of hash buckets that have entries over the threshold
234-
int _bucket_over_threshold;
235-
236-
// The hash bucket that walker is currently walking
237-
int _current_hash_bucket;
238-
// The length of current hash bucket
239-
int _current_bucket_length;
240-
// Number of hash buckets that are not empty
241-
int _used_buckets;
242-
// Longest hash bucket length
243-
int _longest_bucket_length;
244-
245-
public:
246-
StatisticsWalker() : _empty_entries(0), _total_entries(0) {
247-
int index = 0;
248-
for (index = 0; index < NMT_TrackingStackDepth; index ++) {
249-
_stack_depth_distribution[index] = 0;
250-
}
251-
for (index = 0; index < report_threshold; index ++) {
252-
_hash_distribution[index] = 0;
253-
}
254-
_bucket_over_threshold = 0;
255-
_longest_bucket_length = 0;
256-
_current_hash_bucket = -1;
257-
_current_bucket_length = 0;
258-
_used_buckets = 0;
259-
}
260-
261-
virtual bool do_malloc_site(const MallocSite* e) {
262-
if (e->size() == 0) _empty_entries ++;
263-
_total_entries ++;
264-
265-
// stack depth distrubution
266-
int frames = e->call_stack()->frames();
267-
_stack_depth_distribution[frames - 1] ++;
268-
269-
// hash distribution
270-
int hash_bucket = ((unsigned)e->hash()) % MallocSiteTable::hash_buckets();
271-
if (_current_hash_bucket == -1) {
272-
_current_hash_bucket = hash_bucket;
273-
_current_bucket_length = 1;
274-
} else if (_current_hash_bucket == hash_bucket) {
275-
_current_bucket_length ++;
276-
} else {
277-
record_bucket_length(_current_bucket_length);
278-
_current_hash_bucket = hash_bucket;
279-
_current_bucket_length = 1;
280-
}
281-
return true;
282-
}
283-
284-
// walk completed
285-
void completed() {
286-
record_bucket_length(_current_bucket_length);
287-
}
288-
289-
void report_statistics(outputStream* out) {
290-
int index;
291-
out->print_cr("Malloc allocation site table:");
292-
out->print_cr("\tTotal entries: %d", _total_entries);
293-
out->print_cr("\tEmpty entries: %d (%2.2f%%)", _empty_entries, ((float)_empty_entries * 100) / _total_entries);
294-
out->print_cr(" ");
295-
out->print_cr("Hash distribution:");
296-
if (_used_buckets < MallocSiteTable::hash_buckets()) {
297-
out->print_cr("empty bucket: %d", (MallocSiteTable::hash_buckets() - _used_buckets));
298-
}
299-
for (index = 0; index < report_threshold; index ++) {
300-
if (_hash_distribution[index] != 0) {
301-
if (index == 0) {
302-
out->print_cr(" %d entry: %d", 1, _hash_distribution[0]);
303-
} else if (index < 9) { // single digit
304-
out->print_cr(" %d entries: %d", (index + 1), _hash_distribution[index]);
305-
} else {
306-
out->print_cr(" %d entries: %d", (index + 1), _hash_distribution[index]);
307-
}
308-
}
309-
}
310-
if (_bucket_over_threshold > 0) {
311-
out->print_cr(" >%d entries: %d", report_threshold, _bucket_over_threshold);
312-
}
313-
out->print_cr("most entries: %d", _longest_bucket_length);
314-
out->print_cr(" ");
315-
out->print_cr("Call stack depth distribution:");
316-
for (index = 0; index < NMT_TrackingStackDepth; index ++) {
317-
if (_stack_depth_distribution[index] > 0) {
318-
out->print_cr("\t%d: %d", index + 1, _stack_depth_distribution[index]);
319-
}
320-
}
321-
}
322-
323-
private:
324-
void record_bucket_length(int length) {
325-
_used_buckets ++;
326-
if (length <= report_threshold) {
327-
_hash_distribution[length - 1] ++;
328-
} else {
329-
_bucket_over_threshold ++;
330-
}
331-
_longest_bucket_length = MAX2(_longest_bucket_length, length);
332-
}
333-
};
334-
335-
336214
void MemTracker::tuning_statistics(outputStream* out) {
337215
// NMT statistics
338-
StatisticsWalker walker;
339-
MallocSiteTable::walk_malloc_site(&walker);
340-
walker.completed();
341-
342216
out->print_cr("Native Memory Tracking Statistics:");
343217
out->print_cr("Malloc allocation site table size: %d", MallocSiteTable::hash_buckets());
344218
out->print_cr(" Tracking stack depth: %d", NMT_TrackingStackDepth);
345219
NOT_PRODUCT(out->print_cr("Peak concurrent access: %d", MallocSiteTable::access_peak_count());)
346-
out->print_cr(" ");
347-
walker.report_statistics(out);
220+
out->cr();
221+
MallocSiteTable::print_tuning_statistics(out);
348222
}

src/hotspot/share/services/memTracker.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ class MemTracker : AllStatic {
8787
#include "services/virtualMemoryTracker.hpp"
8888

8989
#define CURRENT_PC ((MemTracker::tracking_level() == NMT_detail) ? \
90-
NativeCallStack(0, true) : NativeCallStack::empty_stack())
90+
NativeCallStack(0) : NativeCallStack::empty_stack())
9191
#define CALLER_PC ((MemTracker::tracking_level() == NMT_detail) ? \
92-
NativeCallStack(1, true) : NativeCallStack::empty_stack())
92+
NativeCallStack(1) : NativeCallStack::empty_stack())
9393

9494
class MemBaseline;
9595

src/hotspot/share/services/threadStackTracker.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ void ThreadStackTracker::delete_thread_stack(void* base, size_t size) {
9191
if (MemTracker::tracking_level() == NMT_detail) {
9292
ThreadCritical tc;
9393
assert(_simple_thread_stacks != NULL, "Must be initialized");
94-
SimpleThreadStackSite site((address)base, size);
94+
SimpleThreadStackSite site((address)base, size, NativeCallStack::empty_stack()); // Fake object just to serve as compare target for delete
9595
bool removed = _simple_thread_stacks->remove(site);
9696
assert(removed, "Must exist");
9797
}

0 commit comments

Comments
 (0)