Skip to content

Commit 23749ed

Browse files
committed
8387754
Hi all, please review this change to make several volatiles in the `G1Eden/SurvivorRegions` area use `Atomic` API instead of volatile. There is potentially opportunity to merge `G1Eden/SurivorRegions`, but I consider this out of scope. Testing: gha Thanks, Thomas
1 parent 16da917 commit 23749ed

5 files changed

Lines changed: 22 additions & 22 deletions

File tree

src/hotspot/share/gc/g1/g1EdenRegions.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2026, 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
@@ -27,15 +27,15 @@
2727

2828
#include "gc/g1/g1HeapRegion.hpp"
2929
#include "gc/g1/g1RegionsOnNodes.hpp"
30+
#include "runtime/atomic.hpp"
3031
#include "runtime/globals.hpp"
3132
#include "utilities/debug.hpp"
3233

3334
class G1EdenRegions {
34-
private:
35-
uint _length;
35+
uint _length;
3636
// Sum of used bytes from all retired eden regions.
3737
// I.e. updated when mutator regions are retired.
38-
volatile size_t _used_bytes;
38+
Atomic<size_t> _used_bytes;
3939
G1RegionsOnNodes _regions_on_node;
4040

4141
public:
@@ -49,17 +49,17 @@ class G1EdenRegions {
4949

5050
void clear() {
5151
_length = 0;
52-
_used_bytes = 0;
52+
_used_bytes.store_relaxed(0);
5353
_regions_on_node.clear();
5454
}
5555

5656
uint length() const { return _length; }
5757
uint regions_on_node(uint node_index) const { return _regions_on_node.count(node_index); }
5858

59-
size_t used_bytes() const { return _used_bytes; }
59+
size_t used_bytes() const { return _used_bytes.load_relaxed(); }
6060

6161
void add_used_bytes(size_t used_bytes) {
62-
_used_bytes += used_bytes;
62+
_used_bytes.add_then_fetch(used_bytes, memory_order_relaxed);
6363
}
6464
};
6565

src/hotspot/share/gc/g1/g1RegionsOnNodes.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2026, 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
@@ -27,7 +27,7 @@
2727
#include "gc/g1/g1RegionsOnNodes.hpp"
2828

2929
G1RegionsOnNodes::G1RegionsOnNodes() : _count_per_node(nullptr), _numa(G1NUMA::numa()) {
30-
_count_per_node = NEW_C_HEAP_ARRAY(uint, _numa->num_active_nodes(), mtGC);
30+
_count_per_node = NEW_C_HEAP_ARRAY(Atomic<uint>, _numa->num_active_nodes(), mtGC);
3131
clear();
3232
}
3333

@@ -40,16 +40,14 @@ void G1RegionsOnNodes::add(G1HeapRegion* hr) {
4040

4141
// Update only if the node index is valid.
4242
if (node_index < _numa->num_active_nodes()) {
43-
*(_count_per_node + node_index) += 1;
43+
_count_per_node[node_index].add_then_fetch(1u, memory_order_relaxed);
4444
}
4545
}
4646

4747
void G1RegionsOnNodes::clear() {
48-
for (uint i = 0; i < _numa->num_active_nodes(); i++) {
49-
_count_per_node[i] = 0;
50-
}
48+
::new (_count_per_node) Atomic<uint>[_numa->num_active_nodes()]{};
5149
}
5250

5351
uint G1RegionsOnNodes::count(uint node_index) const {
54-
return _count_per_node[node_index];
52+
return _count_per_node[node_index].load_relaxed();
5553
}

src/hotspot/share/gc/g1/g1RegionsOnNodes.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2026, 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
@@ -26,13 +26,14 @@
2626
#define SHARE_VM_GC_G1_G1REGIONS_HPP
2727

2828
#include "memory/allocation.hpp"
29+
#include "runtime/atomic.hpp"
2930

3031
class G1NUMA;
3132
class G1HeapRegion;
3233

3334
// Contains per node index region count
3435
class G1RegionsOnNodes : public StackObj {
35-
volatile uint* _count_per_node;
36+
Atomic<uint>* _count_per_node;
3637
G1NUMA* _numa;
3738

3839
public:

src/hotspot/share/gc/g1/g1SurvivorRegions.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2026, 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
@@ -55,10 +55,10 @@ void G1SurvivorRegions::convert_to_eden() {
5555

5656
void G1SurvivorRegions::clear() {
5757
_regions.clear();
58-
_used_bytes = 0;
58+
_used_bytes.store_relaxed(0);
5959
_regions_on_node.clear();
6060
}
6161

6262
void G1SurvivorRegions::add_used_bytes(size_t used_bytes) {
63-
_used_bytes += used_bytes;
63+
_used_bytes.add_then_fetch(used_bytes, memory_order_relaxed);
6464
}

src/hotspot/share/gc/g1/g1SurvivorRegions.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2026, 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
@@ -26,6 +26,7 @@
2626
#define SHARE_GC_G1_G1SURVIVORREGIONS_HPP
2727

2828
#include "gc/g1/g1RegionsOnNodes.hpp"
29+
#include "runtime/atomic.hpp"
2930
#include "runtime/globals.hpp"
3031
#include "utilities/growableArray.hpp"
3132

@@ -36,7 +37,7 @@ class G1HeapRegion;
3637
// Set of current survivor regions.
3738
class G1SurvivorRegions {
3839
GrowableArray<G1HeapRegion*> _regions;
39-
volatile size_t _used_bytes;
40+
Atomic<size_t> _used_bytes;
4041
G1RegionsOnNodes _regions_on_node;
4142

4243
public:
@@ -56,7 +57,7 @@ class G1SurvivorRegions {
5657
}
5758

5859
// Used bytes of all survivor regions.
59-
size_t used_bytes() const { return _used_bytes; }
60+
size_t used_bytes() const { return _used_bytes.load_relaxed(); }
6061

6162
void add_used_bytes(size_t used_bytes);
6263
};

0 commit comments

Comments
 (0)