Skip to content

Commit 1c72b35

Browse files
jsikstroxmas92
andcommitted
8357053: ZGC: Improved utility for ZPageAge
Co-authored-by: Axel Boldt-Christmas <aboldtch@openjdk.org> Reviewed-by: sjohanss, stefank
1 parent 52338c9 commit 1c72b35

File tree

14 files changed

+191
-58
lines changed

14 files changed

+191
-58
lines changed

src/hotspot/share/gc/z/zAllocator.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include "gc/z/zAllocator.hpp"
2525
#include "gc/z/zObjectAllocator.hpp"
26+
#include "gc/z/zPageAge.inline.hpp"
2627

2728
ZAllocatorEden* ZAllocator::_eden;
2829
ZAllocatorForRelocation* ZAllocator::_relocation[ZAllocator::_relocation_allocators];
@@ -47,7 +48,7 @@ ZPageAge ZAllocatorForRelocation::install() {
4748
for (uint i = 0; i < ZAllocator::_relocation_allocators; ++i) {
4849
if (_relocation[i] == nullptr) {
4950
_relocation[i] = this;
50-
return static_cast<ZPageAge>(i + 1);
51+
return to_zpageage(i + 1);
5152
}
5253
}
5354

src/hotspot/share/gc/z/zAllocator.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -35,7 +35,7 @@ class ZPage;
3535

3636
class ZAllocator {
3737
public:
38-
static constexpr uint _relocation_allocators = static_cast<uint>(ZPageAge::old);
38+
static constexpr uint _relocation_allocators = ZPageAgeCount - 1;
3939

4040
protected:
4141
ZObjectAllocator _object_allocator;

src/hotspot/share/gc/z/zAllocator.inline.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -28,13 +28,14 @@
2828

2929
#include "gc/z/zAddress.inline.hpp"
3030
#include "gc/z/zHeap.hpp"
31+
#include "gc/z/zPageAge.inline.hpp"
3132

3233
inline ZAllocatorEden* ZAllocator::eden() {
3334
return _eden;
3435
}
3536

3637
inline ZAllocatorForRelocation* ZAllocator::relocation(ZPageAge page_age) {
37-
return _relocation[static_cast<uint>(page_age) - 1];
38+
return _relocation[untype(page_age - 1)];
3839
}
3940

4041
inline ZAllocatorForRelocation* ZAllocator::old() {

src/hotspot/share/gc/z/zGeneration.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "gc/z/zHeap.inline.hpp"
4242
#include "gc/z/zJNICritical.hpp"
4343
#include "gc/z/zMark.inline.hpp"
44+
#include "gc/z/zPageAge.inline.hpp"
4445
#include "gc/z/zPageAllocator.hpp"
4546
#include "gc/z/zRelocationSet.inline.hpp"
4647
#include "gc/z/zRelocationSetSelector.inline.hpp"
@@ -699,11 +700,10 @@ uint ZGenerationYoung::compute_tenuring_threshold(ZRelocationSetSelectorStats st
699700
uint last_populated_age = 0;
700701
size_t last_populated_live = 0;
701702

702-
for (uint i = 0; i <= ZPageAgeMax; ++i) {
703-
const ZPageAge age = static_cast<ZPageAge>(i);
703+
for (ZPageAge age : ZPageAgeRange()) {
704704
const size_t young_live = stats.small(age).live() + stats.medium(age).live() + stats.large(age).live();
705705
if (young_live > 0) {
706-
last_populated_age = i;
706+
last_populated_age = untype(age);
707707
last_populated_live = young_live;
708708
if (young_live_last > 0) {
709709
young_life_expectancy_sum += double(young_live) / double(young_live_last);
@@ -842,8 +842,8 @@ void ZGenerationYoung::mark_start() {
842842

843843
// Retire allocating pages
844844
ZAllocator::eden()->retire_pages();
845-
for (ZPageAge i = ZPageAge::survivor1; i <= ZPageAge::survivor14; i = static_cast<ZPageAge>(static_cast<uint>(i) + 1)) {
846-
ZAllocator::relocation(i)->retire_pages();
845+
for (ZPageAge age : ZPageAgeRangeSurvivor) {
846+
ZAllocator::relocation(age)->retire_pages();
847847
}
848848

849849
// Reset allocated/reclaimed/used statistics

src/hotspot/share/gc/z/zPageAge.hpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -24,6 +24,7 @@
2424
#ifndef SHARE_GC_Z_ZPAGEAGE_HPP
2525
#define SHARE_GC_Z_ZPAGEAGE_HPP
2626

27+
#include "utilities/enumIterator.hpp"
2728
#include "utilities/globalDefinitions.hpp"
2829

2930
enum class ZPageAge : uint8_t {
@@ -45,6 +46,19 @@ enum class ZPageAge : uint8_t {
4546
old
4647
};
4748

48-
constexpr uint ZPageAgeMax = static_cast<uint>(ZPageAge::old);
49+
constexpr uint ZPageAgeCount = static_cast<uint>(ZPageAge::old) + 1;
50+
constexpr ZPageAge ZPageAgeLastPlusOne = static_cast<ZPageAge>(ZPageAgeCount);
51+
52+
ENUMERATOR_RANGE(ZPageAge,
53+
ZPageAge::eden,
54+
ZPageAge::old);
55+
56+
using ZPageAgeRange = EnumRange<ZPageAge>;
57+
58+
constexpr ZPageAgeRange ZPageAgeRangeEden = ZPageAgeRange::create<ZPageAge::eden, ZPageAge::survivor1>();
59+
constexpr ZPageAgeRange ZPageAgeRangeYoung = ZPageAgeRange::create<ZPageAge::eden, ZPageAge::old>();
60+
constexpr ZPageAgeRange ZPageAgeRangeSurvivor = ZPageAgeRange::create<ZPageAge::survivor1, ZPageAge::old>();
61+
constexpr ZPageAgeRange ZPageAgeRangeRelocation = ZPageAgeRange::create<ZPageAge::survivor1, ZPageAgeLastPlusOne>();
62+
constexpr ZPageAgeRange ZPageAgeRangeOld = ZPageAgeRange::create<ZPageAge::old, ZPageAgeLastPlusOne>();
4963

5064
#endif // SHARE_GC_Z_ZPAGEAGE_HPP
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
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+
#ifndef SHARE_GC_Z_ZPAGEAGE_INLINE_HPP
25+
#define SHARE_GC_Z_ZPAGEAGE_INLINE_HPP
26+
27+
#include "gc/z/zPageAge.hpp"
28+
29+
#include "utilities/checkedCast.hpp"
30+
31+
#include <type_traits>
32+
33+
inline uint untype(ZPageAge age) {
34+
return static_cast<uint>(age);
35+
}
36+
37+
inline ZPageAge to_zpageage(uint age) {
38+
assert(age < ZPageAgeCount, "Invalid age");
39+
return static_cast<ZPageAge>(age);
40+
}
41+
42+
inline ZPageAge operator+(ZPageAge age, size_t size) {
43+
const auto size_value = checked_cast<std::underlying_type_t<ZPageAge>>(size);
44+
return to_zpageage(untype(age) + size_value);
45+
}
46+
47+
inline ZPageAge operator-(ZPageAge age, size_t size) {
48+
const auto size_value = checked_cast<std::underlying_type_t<ZPageAge>>(size);
49+
return to_zpageage(untype(age) - size_value);
50+
}
51+
52+
#endif // SHARE_GC_Z_ZPAGEAGE_INLINE_HPP

src/hotspot/share/gc/z/zRelocate.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -488,11 +488,11 @@ class ZRelocateMediumAllocator {
488488
}
489489

490490
ZPage* shared(ZPageAge age) {
491-
return _shared[static_cast<uint>(age) - 1];
491+
return _shared[untype(age - 1)];
492492
}
493493

494494
void set_shared(ZPageAge age, ZPage* page) {
495-
_shared[static_cast<uint>(age) - 1] = page;
495+
_shared[untype(age - 1)] = page;
496496
}
497497

498498
ZPage* alloc_and_retire_target_page(ZForwarding* forwarding, ZPage* target) {
@@ -570,11 +570,11 @@ class ZRelocateWork : public StackObj {
570570

571571

572572
ZPage* target(ZPageAge age) {
573-
return _target[static_cast<uint>(age) - 1];
573+
return _target[untype(age - 1)];
574574
}
575575

576576
void set_target(ZPageAge age, ZPage* page) {
577-
_target[static_cast<uint>(age) - 1] = page;
577+
_target[untype(age - 1)] = page;
578578
}
579579

580580
size_t object_alignment() const {
@@ -1232,12 +1232,12 @@ ZPageAge ZRelocate::compute_to_age(ZPageAge from_age) {
12321232
return ZPageAge::old;
12331233
}
12341234

1235-
const uint age = static_cast<uint>(from_age);
1235+
const uint age = untype(from_age);
12361236
if (age >= ZGeneration::young()->tenuring_threshold()) {
12371237
return ZPageAge::old;
12381238
}
12391239

1240-
return static_cast<ZPageAge>(age + 1);
1240+
return to_zpageage(age + 1);
12411241
}
12421242

12431243
class ZFlipAgePagesTask : public ZTask {

src/hotspot/share/gc/z/zRelocationSetSelector.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "gc/z/zArray.inline.hpp"
2626
#include "gc/z/zForwarding.inline.hpp"
2727
#include "gc/z/zPage.inline.hpp"
28+
#include "gc/z/zPageAge.inline.hpp"
2829
#include "gc/z/zRelocationSetSelector.inline.hpp"
2930
#include "jfr/jfrEvents.hpp"
3031
#include "logging/log.hpp"
@@ -117,8 +118,8 @@ void ZRelocationSetSelectorGroup::select_inner() {
117118
const int npages = _live_pages.length();
118119
int selected_from = 0;
119120
int selected_to = 0;
120-
size_t npages_selected[ZPageAgeMax + 1] = { 0 };
121-
size_t selected_live_bytes[ZPageAgeMax + 1] = { 0 };
121+
size_t npages_selected[ZPageAgeCount] = { 0 };
122+
size_t selected_live_bytes[ZPageAgeCount] = { 0 };
122123
size_t selected_forwarding_entries = 0;
123124

124125
size_t from_live_bytes = 0;
@@ -149,8 +150,8 @@ void ZRelocationSetSelectorGroup::select_inner() {
149150
if (diff_reclaimable > _fragmentation_limit) {
150151
selected_from = from;
151152
selected_to = to;
152-
selected_live_bytes[static_cast<uint>(page->age())] += page_live_bytes;
153-
npages_selected[static_cast<uint>(page->age())] += 1;
153+
selected_live_bytes[untype(page->age())] += page_live_bytes;
154+
npages_selected[untype(page->age())] += 1;
154155
selected_forwarding_entries = from_forwarding_entries;
155156
}
156157

@@ -172,7 +173,7 @@ void ZRelocationSetSelectorGroup::select_inner() {
172173
_forwarding_entries = selected_forwarding_entries;
173174

174175
// Update statistics
175-
for (uint i = 0; i <= ZPageAgeMax; ++i) {
176+
for (uint i = 0; i < ZPageAgeCount; ++i) {
176177
_stats[i]._relocate = selected_live_bytes[i];
177178
_stats[i]._npages_selected = npages_selected[i];
178179
}
@@ -200,7 +201,7 @@ void ZRelocationSetSelectorGroup::select() {
200201
}
201202

202203
ZRelocationSetSelectorGroupStats s{};
203-
for (uint i = 0; i <= ZPageAgeMax; ++i) {
204+
for (uint i = 0; i < ZPageAgeCount; ++i) {
204205
s._npages_candidates += _stats[i].npages_candidates();
205206
s._total += _stats[i].total();
206207
s._empty += _stats[i].empty();
@@ -239,8 +240,8 @@ void ZRelocationSetSelector::select() {
239240
ZRelocationSetSelectorStats ZRelocationSetSelector::stats() const {
240241
ZRelocationSetSelectorStats stats;
241242

242-
for (uint i = 0; i <= ZPageAgeMax; ++i) {
243-
const ZPageAge age = static_cast<ZPageAge>(i);
243+
for (ZPageAge age : ZPageAgeRange()) {
244+
const uint i = untype(age);
244245
stats._small[i] = _small.stats(age);
245246
stats._medium[i] = _medium.stats(age);
246247
stats._large[i] = _large.stats(age);

src/hotspot/share/gc/z/zRelocationSetSelector.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -62,9 +62,9 @@ class ZRelocationSetSelectorStats {
6262
friend class ZRelocationSetSelector;
6363

6464
private:
65-
ZRelocationSetSelectorGroupStats _small[ZPageAgeMax + 1];
66-
ZRelocationSetSelectorGroupStats _medium[ZPageAgeMax + 1];
67-
ZRelocationSetSelectorGroupStats _large[ZPageAgeMax + 1];
65+
ZRelocationSetSelectorGroupStats _small[ZPageAgeCount];
66+
ZRelocationSetSelectorGroupStats _medium[ZPageAgeCount];
67+
ZRelocationSetSelectorGroupStats _large[ZPageAgeCount];
6868

6969
size_t _has_relocatable_pages;
7070

@@ -90,7 +90,7 @@ class ZRelocationSetSelectorGroup {
9090
ZArray<ZPage*> _live_pages;
9191
ZArray<ZPage*> _not_selected_pages;
9292
size_t _forwarding_entries;
93-
ZRelocationSetSelectorGroupStats _stats[ZPageAgeMax + 1];
93+
ZRelocationSetSelectorGroupStats _stats[ZPageAgeCount];
9494

9595
bool is_disabled();
9696
bool is_selectable();

src/hotspot/share/gc/z/zRelocationSetSelector.inline.hpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -29,6 +29,7 @@
2929
#include "gc/z/zArray.inline.hpp"
3030
#include "gc/z/zGlobals.hpp"
3131
#include "gc/z/zPage.inline.hpp"
32+
#include "gc/z/zPageAge.inline.hpp"
3233
#include "utilities/powerOfTwo.hpp"
3334

3435
inline size_t ZRelocationSetSelectorGroupStats::npages_candidates() const {
@@ -60,15 +61,15 @@ inline bool ZRelocationSetSelectorStats::has_relocatable_pages() const {
6061
}
6162

6263
inline const ZRelocationSetSelectorGroupStats& ZRelocationSetSelectorStats::small(ZPageAge age) const {
63-
return _small[static_cast<uint>(age)];
64+
return _small[untype(age)];
6465
}
6566

6667
inline const ZRelocationSetSelectorGroupStats& ZRelocationSetSelectorStats::medium(ZPageAge age) const {
67-
return _medium[static_cast<uint>(age)];
68+
return _medium[untype(age)];
6869
}
6970

7071
inline const ZRelocationSetSelectorGroupStats& ZRelocationSetSelectorStats::large(ZPageAge age) const {
71-
return _large[static_cast<uint>(age)];
72+
return _large[untype(age)];
7273
}
7374

7475
inline bool ZRelocationSetSelectorGroup::pre_filter_page(const ZPage* page, size_t live_bytes) const {
@@ -113,7 +114,7 @@ inline void ZRelocationSetSelectorGroup::register_live_page(ZPage* page) {
113114
}
114115

115116
const size_t size = page->size();
116-
const uint age = static_cast<uint>(page->age());
117+
const uint age = untype(page->age());
117118
_stats[age]._npages_candidates++;
118119
_stats[age]._total += size;
119120
_stats[age]._live += live;
@@ -122,7 +123,7 @@ inline void ZRelocationSetSelectorGroup::register_live_page(ZPage* page) {
122123
inline void ZRelocationSetSelectorGroup::register_empty_page(ZPage* page) {
123124
const size_t size = page->size();
124125

125-
const uint age = static_cast<uint>(page->age());
126+
const uint age = untype(page->age());
126127
_stats[age]._npages_candidates++;
127128
_stats[age]._total += size;
128129
_stats[age]._empty += size;
@@ -141,7 +142,7 @@ inline size_t ZRelocationSetSelectorGroup::forwarding_entries() const {
141142
}
142143

143144
inline const ZRelocationSetSelectorGroupStats& ZRelocationSetSelectorGroup::stats(ZPageAge age) const {
144-
return _stats[static_cast<uint>(age)];
145+
return _stats[untype(age)];
145146
}
146147

147148
inline void ZRelocationSetSelector::register_live_page(ZPage* page) {
@@ -188,26 +189,23 @@ inline void ZRelocationSetSelector::clear_empty_pages() {
188189

189190
inline size_t ZRelocationSetSelector::total() const {
190191
size_t sum = 0;
191-
for (uint i = 0; i <= ZPageAgeMax; ++i) {
192-
const ZPageAge age = static_cast<ZPageAge>(i);
192+
for (ZPageAge age : ZPageAgeRange()) {
193193
sum += _small.stats(age).total() + _medium.stats(age).total() + _large.stats(age).total();
194194
}
195195
return sum;
196196
}
197197

198198
inline size_t ZRelocationSetSelector::empty() const {
199199
size_t sum = 0;
200-
for (uint i = 0; i <= ZPageAgeMax; ++i) {
201-
const ZPageAge age = static_cast<ZPageAge>(i);
200+
for (ZPageAge age : ZPageAgeRange()) {
202201
sum += _small.stats(age).empty() + _medium.stats(age).empty() + _large.stats(age).empty();
203202
}
204203
return sum;
205204
}
206205

207206
inline size_t ZRelocationSetSelector::relocate() const {
208207
size_t sum = 0;
209-
for (uint i = 0; i <= ZPageAgeMax; ++i) {
210-
const ZPageAge age = static_cast<ZPageAge>(i);
208+
for (ZPageAge age : ZPageAgeRange()) {
211209
sum += _small.stats(age).relocate() + _medium.stats(age).relocate() + _large.stats(age).relocate();
212210
}
213211
return sum;

0 commit comments

Comments
 (0)