Skip to content

Commit 336d230

Browse files
committed
8297958: NMT: Display peak values
Reviewed-by: jsjolen, sjohanss
1 parent 0d2a9ee commit 336d230

File tree

5 files changed

+81
-57
lines changed

5 files changed

+81
-57
lines changed

src/hotspot/share/services/mallocSiteTable.hpp

+4
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,12 @@ class MallocSite : public AllocationSite {
4646

4747
// Memory allocated from this code path
4848
size_t size() const { return _c.size(); }
49+
// Peak memory ever allocated from this code path
50+
size_t peak_size() const { return _c.peak_size(); }
4951
// The number of calls were made
5052
size_t count() const { return _c.count(); }
53+
54+
const MemoryCounter* counter() const { return &_c; }
5155
};
5256

5357
// Malloc site hashtable entry

src/hotspot/share/services/mallocTracker.cpp

+10-23
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "jvm_io.h"
2828
#include "logging/log.hpp"
2929
#include "runtime/arguments.hpp"
30+
#include "runtime/atomic.hpp"
3031
#include "runtime/os.hpp"
3132
#include "runtime/safefetch.hpp"
3233
#include "services/mallocHeader.inline.hpp"
@@ -42,34 +43,20 @@ size_t MallocMemorySummary::_limits_per_category[mt_number_of_types] = { 0 };
4243
size_t MallocMemorySummary::_total_limit = 0;
4344

4445
#ifdef ASSERT
45-
void MemoryCounter::update_peak_count(size_t count) {
46-
size_t peak_cnt = peak_count();
47-
while (peak_cnt < count) {
48-
size_t old_cnt = Atomic::cmpxchg(&_peak_count, peak_cnt, count, memory_order_relaxed);
49-
if (old_cnt != peak_cnt) {
50-
peak_cnt = old_cnt;
51-
}
52-
}
53-
}
54-
55-
void MemoryCounter::update_peak_size(size_t sz) {
46+
void MemoryCounter::update_peak(size_t size, size_t cnt) {
5647
size_t peak_sz = peak_size();
57-
while (peak_sz < sz) {
58-
size_t old_sz = Atomic::cmpxchg(&_peak_size, peak_sz, sz, memory_order_relaxed);
59-
if (old_sz != peak_sz) {
48+
while (peak_sz < size) {
49+
size_t old_sz = Atomic::cmpxchg(&_peak_size, peak_sz, size, memory_order_relaxed);
50+
if (old_sz == peak_sz) {
51+
// I won
52+
_peak_count = cnt;
53+
break;
54+
} else {
6055
peak_sz = old_sz;
6156
}
6257
}
6358
}
64-
65-
size_t MemoryCounter::peak_count() const {
66-
return Atomic::load(&_peak_count);
67-
}
68-
69-
size_t MemoryCounter::peak_size() const {
70-
return Atomic::load(&_peak_size);
71-
}
72-
#endif
59+
#endif // ASSERT
7360

7461
// Total malloc'd memory used by arenas
7562
size_t MallocMemorySnapshot::total_arena() const {

src/hotspot/share/services/mallocTracker.hpp

+20-13
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,13 @@ class MemoryCounter {
4545
volatile size_t _count;
4646
volatile size_t _size;
4747

48-
DEBUG_ONLY(volatile size_t _peak_count;)
49-
DEBUG_ONLY(volatile size_t _peak_size; )
48+
#ifdef ASSERT
49+
// Peak size and count. Note: Peak count is the count at the point
50+
// peak size was reached, not the absolute highest peak count.
51+
volatile size_t _peak_count;
52+
volatile size_t _peak_size;
53+
void update_peak(size_t size, size_t cnt);
54+
#endif // ASSERT
5055

5156
public:
5257
MemoryCounter() : _count(0), _size(0) {
@@ -58,9 +63,8 @@ class MemoryCounter {
5863
size_t cnt = Atomic::add(&_count, size_t(1), memory_order_relaxed);
5964
if (sz > 0) {
6065
size_t sum = Atomic::add(&_size, sz, memory_order_relaxed);
61-
DEBUG_ONLY(update_peak_size(sum);)
66+
DEBUG_ONLY(update_peak(sum, cnt);)
6267
}
63-
DEBUG_ONLY(update_peak_count(cnt);)
6468
}
6569

6670
inline void deallocate(size_t sz) {
@@ -76,19 +80,20 @@ class MemoryCounter {
7680
if (sz != 0) {
7781
assert(sz >= 0 || size() >= size_t(-sz), "Must be");
7882
size_t sum = Atomic::add(&_size, size_t(sz), memory_order_relaxed);
79-
DEBUG_ONLY(update_peak_size(sum);)
83+
DEBUG_ONLY(update_peak(sum, _count);)
8084
}
8185
}
8286

8387
inline size_t count() const { return Atomic::load(&_count); }
8488
inline size_t size() const { return Atomic::load(&_size); }
8589

86-
#ifdef ASSERT
87-
void update_peak_count(size_t cnt);
88-
void update_peak_size(size_t sz);
89-
size_t peak_count() const;
90-
size_t peak_size() const;
91-
#endif // ASSERT
90+
inline size_t peak_count() const {
91+
return DEBUG_ONLY(Atomic::load(&_peak_count)) NOT_DEBUG(0);
92+
}
93+
94+
inline size_t peak_size() const {
95+
return DEBUG_ONLY(Atomic::load(&_peak_size)) NOT_DEBUG(0);
96+
}
9297
};
9398

9499
/*
@@ -125,12 +130,14 @@ class MallocMemory {
125130
}
126131

127132
inline size_t malloc_size() const { return _malloc.size(); }
133+
inline size_t malloc_peak_size() const { return _malloc.peak_size(); }
128134
inline size_t malloc_count() const { return _malloc.count();}
129135
inline size_t arena_size() const { return _arena.size(); }
136+
inline size_t arena_peak_size() const { return _arena.peak_size(); }
130137
inline size_t arena_count() const { return _arena.count(); }
131138

132-
DEBUG_ONLY(inline const MemoryCounter& malloc_counter() const { return _malloc; })
133-
DEBUG_ONLY(inline const MemoryCounter& arena_counter() const { return _arena; })
139+
const MemoryCounter* malloc_counter() const { return &_malloc; }
140+
const MemoryCounter* arena_counter() const { return &_arena; }
134141
};
135142

136143
class MallocMemorySummary;

src/hotspot/share/services/memReporter.cpp

+44-18
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,14 @@ void MemReporterBase::print_total(size_t reserved, size_t committed) const {
4545
amount_in_current_scale(reserved), scale, amount_in_current_scale(committed), scale);
4646
}
4747

48-
void MemReporterBase::print_malloc(size_t amount, size_t count, MEMFLAGS flag) const {
48+
void MemReporterBase::print_malloc(const MemoryCounter* c, MEMFLAGS flag) const {
4949
const char* scale = current_scale();
5050
outputStream* out = output();
5151
const char* alloc_type = (flag == mtThreadStack) ? "" : "malloc=";
5252

53+
const size_t amount = c->size();
54+
const size_t count = c->count();
55+
5356
if (flag != mtNone) {
5457
out->print("(%s" SIZE_FORMAT "%s type=%s", alloc_type,
5558
amount_in_current_scale(amount), scale, NMTUtil::flag_to_name(flag));
@@ -58,11 +61,21 @@ void MemReporterBase::print_malloc(size_t amount, size_t count, MEMFLAGS flag) c
5861
amount_in_current_scale(amount), scale);
5962
}
6063

64+
// blends out mtChunk count number
6165
if (count > 0) {
6266
out->print(" #" SIZE_FORMAT "", count);
6367
}
6468

6569
out->print(")");
70+
71+
size_t pk_amount = c->peak_size();
72+
if (pk_amount == amount) {
73+
out->print_raw(" (at peak)");
74+
} else if (pk_amount > amount) {
75+
size_t pk_count = c->peak_count();
76+
out->print(" (peak=" SIZE_FORMAT "%s #" SIZE_FORMAT ")",
77+
amount_in_current_scale(pk_amount), scale, pk_count);
78+
}
6679
}
6780

6881
void MemReporterBase::print_virtual_memory(size_t reserved, size_t committed) const {
@@ -71,9 +84,9 @@ void MemReporterBase::print_virtual_memory(size_t reserved, size_t committed) co
7184
amount_in_current_scale(reserved), scale, amount_in_current_scale(committed), scale);
7285
}
7386

74-
void MemReporterBase::print_malloc_line(size_t amount, size_t count) const {
87+
void MemReporterBase::print_malloc_line(const MemoryCounter* c) const {
7588
output()->print("%28s", " ");
76-
print_malloc(amount, count);
89+
print_malloc(c);
7790
output()->print_cr(" ");
7891
}
7992

@@ -83,10 +96,26 @@ void MemReporterBase::print_virtual_memory_line(size_t reserved, size_t committe
8396
output()->print_cr(" ");
8497
}
8598

86-
void MemReporterBase::print_arena_line(size_t amount, size_t count) const {
99+
void MemReporterBase::print_arena_line(const MemoryCounter* c) const {
87100
const char* scale = current_scale();
88-
output()->print_cr("%27s (arena=" SIZE_FORMAT "%s #" SIZE_FORMAT ")", " ",
101+
outputStream* out = output();
102+
103+
const size_t amount = c->size();
104+
const size_t count = c->count();
105+
106+
out->print("%27s (arena=" SIZE_FORMAT "%s #" SIZE_FORMAT ")", "",
89107
amount_in_current_scale(amount), scale, count);
108+
109+
size_t pk_amount = c->peak_size();
110+
if (pk_amount == amount) {
111+
out->print_raw(" (at peak)");
112+
} else if (pk_amount > amount) {
113+
size_t pk_count = c->peak_count();
114+
out->print(" (peak=" SIZE_FORMAT "%s #" SIZE_FORMAT ")",
115+
amount_in_current_scale(pk_amount), scale, pk_count);
116+
}
117+
118+
out->cr();
90119
}
91120

92121
void MemReporterBase::print_virtual_memory_region(const char* type, address base, size_t size) const {
@@ -195,18 +224,18 @@ void MemSummaryReporter::report_summary_of_type(MEMFLAGS flag,
195224
}
196225

197226
// report malloc'd memory
198-
if (amount_in_current_scale(malloc_memory->malloc_size()) > 0) {
199-
// We don't know how many arena chunks are in used, so don't report the count
200-
size_t count = (flag == mtChunk) ? 0 : malloc_memory->malloc_count();
201-
print_malloc_line(malloc_memory->malloc_size(), count);
227+
if (amount_in_current_scale(malloc_memory->malloc_size()) > 0
228+
DEBUG_ONLY(|| amount_in_current_scale(malloc_memory->malloc_peak_size()) > 0)) {
229+
print_malloc_line(malloc_memory->malloc_counter());
202230
}
203231

204232
if (amount_in_current_scale(virtual_memory->reserved()) > 0) {
205233
print_virtual_memory_line(virtual_memory->reserved(), virtual_memory->committed());
206234
}
207235

208-
if (amount_in_current_scale(malloc_memory->arena_size()) > 0) {
209-
print_arena_line(malloc_memory->arena_size(), malloc_memory->arena_count());
236+
if (amount_in_current_scale(malloc_memory->arena_size()) > 0
237+
DEBUG_ONLY(|| amount_in_current_scale(malloc_memory->arena_peak_size()) > 0)) {
238+
print_arena_line(malloc_memory->arena_counter());
210239
}
211240

212241
if (flag == mtNMT &&
@@ -271,12 +300,9 @@ int MemDetailReporter::report_malloc_sites() {
271300
const MallocSite* malloc_site;
272301
int num_omitted = 0;
273302
while ((malloc_site = malloc_itr.next()) != NULL) {
274-
// Don't report free sites; does not count toward omitted count.
275-
if (malloc_site->size() == 0) {
276-
continue;
277-
}
278-
// Don't report if site has allocated less than one unit of whatever our scale is
279-
if (scale() > 1 && amount_in_current_scale(malloc_site->size()) == 0) {
303+
// Don't report if site has never allocated less than one unit of whatever our scale is
304+
if (scale() > 1 && amount_in_current_scale(malloc_site->size()) == 0
305+
DEBUG_ONLY(&& amount_in_current_scale(malloc_site->peak_size()) == 0)) {
280306
num_omitted ++;
281307
continue;
282308
}
@@ -286,7 +312,7 @@ int MemDetailReporter::report_malloc_sites() {
286312
MEMFLAGS flag = malloc_site->flag();
287313
assert(NMTUtil::flag_is_valid(flag) && flag != mtNone,
288314
"Must have a valid memory type");
289-
print_malloc(malloc_site->size(), malloc_site->count(),flag);
315+
print_malloc(malloc_site->counter(), flag);
290316
out->print_cr("\n");
291317
}
292318
return num_omitted;

src/hotspot/share/services/memReporter.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ class MemReporterBase : public StackObj {
8080

8181
// Print summary total, malloc and virtual memory
8282
void print_total(size_t reserved, size_t committed) const;
83-
void print_malloc(size_t amount, size_t count, MEMFLAGS flag = mtNone) const;
83+
void print_malloc(const MemoryCounter* c, MEMFLAGS flag = mtNone) const;
8484
void print_virtual_memory(size_t reserved, size_t committed) const;
8585

86-
void print_malloc_line(size_t amount, size_t count) const;
86+
void print_malloc_line(const MemoryCounter* c) const;
8787
void print_virtual_memory_line(size_t reserved, size_t committed) const;
88-
void print_arena_line(size_t amount, size_t count) const;
88+
void print_arena_line(const MemoryCounter* c) const;
8989

9090
void print_virtual_memory_region(const char* type, address base, size_t size) const;
9191
};

0 commit comments

Comments
 (0)