Skip to content

Commit 20d41b9

Browse files
committed
8297958: NMT: Display peak values
Backport-of: 336d230a39e41eeed8b9d2ce3fec42e9de1d11fe
1 parent 76fd18e commit 20d41b9

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
@@ -49,8 +49,12 @@ class MallocSite : public AllocationSite {
4949

5050
// Memory allocated from this code path
5151
size_t size() const { return _c.size(); }
52+
// Peak memory ever allocated from this code path
53+
size_t peak_size() const { return _c.peak_size(); }
5254
// The number of calls were made
5355
size_t count() const { return _c.count(); }
56+
57+
const MemoryCounter* counter() const { return &_c; }
5458
};
5559

5660
// Malloc site hashtable entry

src/hotspot/share/services/mallocTracker.cpp

+10-23
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424
#include "precompiled.hpp"
2525

26+
#include "runtime/atomic.hpp"
2627
#include "runtime/os.hpp"
2728
#include "services/mallocSiteTable.hpp"
2829
#include "services/mallocTracker.hpp"
@@ -33,34 +34,20 @@
3334
size_t MallocMemorySummary::_snapshot[CALC_OBJ_SIZE_IN_TYPE(MallocMemorySnapshot, size_t)];
3435

3536
#ifdef ASSERT
36-
void MemoryCounter::update_peak_count(size_t count) {
37-
size_t peak_cnt = peak_count();
38-
while (peak_cnt < count) {
39-
size_t old_cnt = Atomic::cmpxchg(&_peak_count, peak_cnt, count, memory_order_relaxed);
40-
if (old_cnt != peak_cnt) {
41-
peak_cnt = old_cnt;
42-
}
43-
}
44-
}
45-
46-
void MemoryCounter::update_peak_size(size_t sz) {
37+
void MemoryCounter::update_peak(size_t size, size_t cnt) {
4738
size_t peak_sz = peak_size();
48-
while (peak_sz < sz) {
49-
size_t old_sz = Atomic::cmpxchg(&_peak_size, peak_sz, sz, memory_order_relaxed);
50-
if (old_sz != peak_sz) {
39+
while (peak_sz < size) {
40+
size_t old_sz = Atomic::cmpxchg(&_peak_size, peak_sz, size, memory_order_relaxed);
41+
if (old_sz == peak_sz) {
42+
// I won
43+
_peak_count = cnt;
44+
break;
45+
} else {
5146
peak_sz = old_sz;
5247
}
5348
}
5449
}
55-
56-
size_t MemoryCounter::peak_count() const {
57-
return Atomic::load(&_peak_count);
58-
}
59-
60-
size_t MemoryCounter::peak_size() const {
61-
return Atomic::load(&_peak_size);
62-
}
63-
#endif
50+
#endif // ASSERT
6451

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

src/hotspot/share/services/mallocTracker.hpp

+20-13
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,13 @@ class MemoryCounter {
4343
volatile size_t _count;
4444
volatile size_t _size;
4545

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

4954
public:
5055
MemoryCounter() : _count(0), _size(0) {
@@ -56,9 +61,8 @@ class MemoryCounter {
5661
size_t cnt = Atomic::add(&_count, size_t(1), memory_order_relaxed);
5762
if (sz > 0) {
5863
size_t sum = Atomic::add(&_size, sz, memory_order_relaxed);
59-
DEBUG_ONLY(update_peak_size(sum);)
64+
DEBUG_ONLY(update_peak(sum, cnt);)
6065
}
61-
DEBUG_ONLY(update_peak_count(cnt);)
6266
}
6367

6468
inline void deallocate(size_t sz) {
@@ -74,19 +78,20 @@ class MemoryCounter {
7478
if (sz != 0) {
7579
assert(sz >= 0 || size() >= size_t(-sz), "Must be");
7680
size_t sum = Atomic::add(&_size, size_t(sz), memory_order_relaxed);
77-
DEBUG_ONLY(update_peak_size(sum);)
81+
DEBUG_ONLY(update_peak(sum, _count);)
7882
}
7983
}
8084

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

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

9297
/*
@@ -123,12 +128,14 @@ class MallocMemory {
123128
}
124129

125130
inline size_t malloc_size() const { return _malloc.size(); }
131+
inline size_t malloc_peak_size() const { return _malloc.peak_size(); }
126132
inline size_t malloc_count() const { return _malloc.count();}
127133
inline size_t arena_size() const { return _arena.size(); }
134+
inline size_t arena_peak_size() const { return _arena.peak_size(); }
128135
inline size_t arena_count() const { return _arena.count(); }
129136

130-
DEBUG_ONLY(inline const MemoryCounter& malloc_counter() const { return _malloc; })
131-
DEBUG_ONLY(inline const MemoryCounter& arena_counter() const { return _arena; })
137+
const MemoryCounter* malloc_counter() const { return &_malloc; }
138+
const MemoryCounter* arena_counter() const { return &_arena; }
132139
};
133140

134141
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
@@ -82,12 +82,12 @@ class MemReporterBase : public StackObj {
8282

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

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

9292
void print_virtual_memory_region(const char* type, address base, size_t size) const;
9393
};

0 commit comments

Comments
 (0)