Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/hotspot/jtreg/runtime/Metaspace/elastic/MetaspaceTestArena.java
64507 views
1
/*
2
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3
* Copyright (c) 2020 SAP SE. All rights reserved.
4
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
*
6
* This code is free software; you can redistribute it and/or modify it
7
* under the terms of the GNU General Public License version 2 only, as
8
* published by the Free Software Foundation.
9
*
10
* This code is distributed in the hope that it will be useful, but WITHOUT
11
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13
* version 2 for more details (a copy is included in the LICENSE file that
14
* accompanied this code).
15
*
16
* You should have received a copy of the GNU General Public License version
17
* 2 along with this work; if not, write to the Free Software Foundation,
18
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19
*
20
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21
* or visit www.oracle.com if you need additional information or have any
22
* questions.
23
*
24
*/
25
26
import sun.hotspot.WhiteBox;
27
28
public class MetaspaceTestArena {
29
30
long arena;
31
32
final long allocationCeiling;
33
34
// Number and word size of allocations
35
long allocatedWords = 0;
36
long numAllocated = 0;
37
long deallocatedWords = 0;
38
long numDeallocated = 0;
39
volatile long numAllocationFailures = 0;
40
41
private synchronized boolean reachedCeiling() {
42
return (allocatedWords - deallocatedWords) > allocationCeiling;
43
}
44
45
private synchronized void accountAllocation(long words) {
46
numAllocated ++;
47
allocatedWords += words;
48
}
49
50
private synchronized void accountDeallocation(long words) {
51
numDeallocated ++;
52
deallocatedWords += words;
53
}
54
55
MetaspaceTestArena(long arena0, long allocationCeiling) {
56
this.allocationCeiling = allocationCeiling;
57
this.arena = arena0;
58
}
59
60
public Allocation allocate(long words) {
61
if (reachedCeiling()) {
62
numAllocationFailures ++;
63
return null;
64
}
65
WhiteBox wb = WhiteBox.getWhiteBox();
66
long p = wb.allocateFromMetaspaceTestArena(arena, words);
67
if (p == 0) {
68
numAllocationFailures ++;
69
return null;
70
} else {
71
accountAllocation(words);
72
}
73
return new Allocation(p, words);
74
}
75
76
public void deallocate(Allocation a) {
77
WhiteBox wb = WhiteBox.getWhiteBox();
78
wb.deallocateToMetaspaceTestArena(arena, a.p, a.word_size);
79
accountDeallocation(a.word_size);
80
}
81
82
//// Convenience functions ////
83
84
public Allocation allocate_expect_success(long words) {
85
Allocation a = allocate(words);
86
if (a.isNull()) {
87
throw new RuntimeException("Allocation failed (" + words + ")");
88
}
89
return a;
90
}
91
92
public void allocate_expect_failure(long words) {
93
Allocation a = allocate(words);
94
if (!a.isNull()) {
95
throw new RuntimeException("Allocation failed (" + words + ")");
96
}
97
}
98
99
boolean isLive() {
100
return arena != 0;
101
}
102
103
@Override
104
public String toString() {
105
return "arena=" + arena +
106
", ceiling=" + allocationCeiling +
107
", allocatedWords=" + allocatedWords +
108
", numAllocated=" + numAllocated +
109
", deallocatedWords=" + deallocatedWords +
110
", numDeallocated=" + numDeallocated +
111
", numAllocationFailures=" + numAllocationFailures +
112
'}';
113
}
114
115
}
116
117