8261600: NMT: Relax memory order for updating MemoryCounter and fix racy updating of peak values

Reviewed-by: dholmes, shade
This commit is contained in:
Zhengyu Gu 2021-02-18 13:30:46 +00:00
parent 1a7adc8642
commit f94a845287
2 changed files with 53 additions and 19 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -30,6 +30,36 @@
size_t MallocMemorySummary::_snapshot[CALC_OBJ_SIZE_IN_TYPE(MallocMemorySnapshot, size_t)];
#ifdef ASSERT
void MemoryCounter::update_peak_count(size_t count) {
size_t peak_cnt = peak_count();
while (peak_cnt < count) {
size_t old_cnt = Atomic::cmpxchg(&_peak_count, peak_cnt, count, memory_order_relaxed);
if (old_cnt != peak_cnt) {
peak_cnt = old_cnt;
}
}
}
void MemoryCounter::update_peak_size(size_t sz) {
size_t peak_sz = peak_size();
while (peak_sz < sz) {
size_t old_sz = Atomic::cmpxchg(&_peak_size, peak_sz, sz, memory_order_relaxed);
if (old_sz != peak_sz) {
peak_sz = old_sz;
}
}
}
size_t MemoryCounter::peak_count() const {
return Atomic::load(&_peak_count);
}
size_t MemoryCounter::peak_size() const {
return Atomic::load(&_peak_size);
}
#endif
// Total malloc'd memory amount
size_t MallocMemorySnapshot::total() const {
size_t amount = 0;