Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/hotspot/jtreg/runtime/Metaspace/elastic/RandomAllocator.java
64507 views
1
/*
2
* Copyright (c) 2021 SAP SE. All rights reserved.
3
* Copyright (c) 2021, Oracle and/or its affiliates. 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 java.util.ArrayList;
27
import java.util.Random;
28
29
/**
30
* RandomAllocator sits atop an arena and allocates from it.
31
*
32
* It will, according to an allocation profile, allocate random blocks in a certain size range and, from time to time,
33
* deallocate old blocks.
34
*
35
* At some point it will reach a limit: either the commit/reserve limit of the underlying MetaspaceTestContext,
36
* or the allocation ceiling imposed by the test. From that point on allocations will start failing. We can (and do)
37
* deallocate a bit more, but since that will only exercise the Arena's internal free block list and nothing much else,
38
* this is unexciting in terms of stressing Metaspace. So, the caller may decide to kill the arena and create a new one.
39
*
40
*/
41
public class RandomAllocator {
42
43
final MetaspaceTestArena arena;
44
final AllocationProfile profile;
45
46
ArrayList<Allocation> to_dealloc = new ArrayList<>();
47
48
long ticks = 0;
49
50
// Allocate (breathe in) until arena is full, then - to test the arena deallocator - deallocate some allocations
51
// and breathe in again until full.
52
boolean breatheIn = true;
53
int breatheOutTicks = 0;
54
55
Random localRandom;
56
57
// Roll dice and return true if probability was hit
58
private boolean rollDice(double probability) {
59
return ((double)localRandom.nextInt(100) > (100.0 * (1.0 - probability))) ? true : false;
60
}
61
62
// Allocate a random amount from the arena. If dice hits right, add this to the deallocation list.
63
void allocateRandomly() {
64
long word_size = profile.randomAllocationSize();
65
Allocation a = arena.allocate(word_size);
66
if (a != null) {
67
if (to_dealloc.size() < 10000) {
68
to_dealloc.add(a);
69
}
70
} else {
71
// On allocation error, breathe out a bit
72
breatheIn = false;
73
breatheOutTicks = 0;
74
}
75
}
76
77
// Randomly choose one of the allocated in the deallocation list and deallocate it
78
void deallocateRandomly() {
79
if (to_dealloc.size() == 0) {
80
return;
81
}
82
int n = localRandom.nextInt(to_dealloc.size());
83
Allocation a = to_dealloc.remove(n);
84
arena.deallocate(a);
85
}
86
87
public void tick() {
88
if (breatheIn) {
89
// allocate until we hit the ceiling
90
allocateRandomly();
91
if (rollDice(profile.randomDeallocProbability)) {
92
deallocateRandomly();
93
}
94
} else {
95
if (++breatheOutTicks < 100) {
96
deallocateRandomly();
97
} else {
98
breatheIn = true;
99
}
100
}
101
ticks ++;
102
}
103
104
public RandomAllocator(MetaspaceTestArena arena) {
105
this.arena = arena;
106
this.profile = AllocationProfile.randomProfile();
107
// reproducable randoms (we assume each allocator is only used from within one thread, and gets created from the main thread).
108
this.localRandom = new Random(RandomHelper.random().nextInt());
109
}
110
111
long numAllocationFailures() {
112
return arena.numAllocationFailures;
113
}
114
115
@Override
116
public String toString() {
117
return arena.toString() + ", ticks=" + ticks;
118
}
119
120
}
121
122