Skip to content

Commit 50b6e41

Browse files
author
Sonia Zaldana Calles
committed
8300732: Whitebox functions for Metaspace test should use byte size
Reviewed-by: stuefe, asmehra
1 parent 67d1ef1 commit 50b6e41

File tree

13 files changed

+162
-91
lines changed

13 files changed

+162
-91
lines changed

src/hotspot/share/prims/whitebox.cpp

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1721,8 +1721,13 @@ int WhiteBox::array_bytes_to_length(size_t bytes) {
17211721
///////////////
17221722
// MetaspaceTestContext and MetaspaceTestArena
17231723
WB_ENTRY(jlong, WB_CreateMetaspaceTestContext(JNIEnv* env, jobject wb, jlong commit_limit, jlong reserve_limit))
1724+
assert(is_aligned(commit_limit, BytesPerWord),
1725+
"WB_CreateMetaspaceTestContext: commit_limit is not a multiple of the system word byte size");
1726+
assert(is_aligned(reserve_limit, BytesPerWord),
1727+
"WB_CreateMetaspaceTestContext: reserve_limit is not a multiple of the system word byte size");
17241728
metaspace::MetaspaceTestContext* context =
1725-
new metaspace::MetaspaceTestContext("whitebox-metaspace-context", (size_t) commit_limit, (size_t) reserve_limit);
1729+
new metaspace::MetaspaceTestContext("whitebox-metaspace-context", (size_t) commit_limit / BytesPerWord,
1730+
(size_t) reserve_limit / BytesPerWord);
17261731
return (jlong)p2i(context);
17271732
WB_END
17281733

@@ -1740,14 +1745,14 @@ WB_ENTRY(void, WB_PrintMetaspaceTestContext(JNIEnv* env, jobject wb, jlong conte
17401745
context0->print_on(tty);
17411746
WB_END
17421747

1743-
WB_ENTRY(jlong, WB_GetTotalCommittedWordsInMetaspaceTestContext(JNIEnv* env, jobject wb, jlong context))
1748+
WB_ENTRY(jlong, WB_GetTotalCommittedBytesInMetaspaceTestContext(JNIEnv* env, jobject wb, jlong context))
17441749
metaspace::MetaspaceTestContext* context0 = (metaspace::MetaspaceTestContext*) context;
1745-
return context0->committed_words();
1750+
return (jlong)context0->committed_words() * BytesPerWord;
17461751
WB_END
17471752

1748-
WB_ENTRY(jlong, WB_GetTotalUsedWordsInMetaspaceTestContext(JNIEnv* env, jobject wb, jlong context))
1753+
WB_ENTRY(jlong, WB_GetTotalUsedBytesInMetaspaceTestContext(JNIEnv* env, jobject wb, jlong context))
17491754
metaspace::MetaspaceTestContext* context0 = (metaspace::MetaspaceTestContext*) context;
1750-
return context0->used_words();
1755+
return (jlong)context0->used_words() * BytesPerWord;
17511756
WB_END
17521757

17531758
WB_ENTRY(jlong, WB_CreateArenaInTestContext(JNIEnv* env, jobject wb, jlong context, jboolean is_micro))
@@ -1760,21 +1765,33 @@ WB_ENTRY(void, WB_DestroyMetaspaceTestArena(JNIEnv* env, jobject wb, jlong arena
17601765
delete (metaspace::MetaspaceTestArena*) arena;
17611766
WB_END
17621767

1763-
WB_ENTRY(jlong, WB_AllocateFromMetaspaceTestArena(JNIEnv* env, jobject wb, jlong arena, jlong word_size))
1764-
metaspace::MetaspaceTestArena* arena0 = (metaspace::MetaspaceTestArena*) arena;
1765-
MetaWord* p = arena0->allocate((size_t) word_size);
1768+
WB_ENTRY(jlong, WB_AllocateFromMetaspaceTestArena(JNIEnv* env, jobject wb, jlong arena, jlong size))
1769+
assert(is_aligned(size, BytesPerWord),
1770+
"WB_AllocateFromMetaspaceTestArena: size is not a multiple of the system word byte size");
1771+
metaspace::MetaspaceTestArena *arena0 = (metaspace::MetaspaceTestArena *)arena;
1772+
MetaWord *p = arena0->allocate((size_t) size / BytesPerWord);
17661773
return (jlong)p2i(p);
17671774
WB_END
17681775

1769-
WB_ENTRY(void, WB_DeallocateToMetaspaceTestArena(JNIEnv* env, jobject wb, jlong arena, jlong p, jlong word_size))
1776+
WB_ENTRY(void, WB_DeallocateToMetaspaceTestArena(JNIEnv* env, jobject wb, jlong arena, jlong p, jlong size))
1777+
assert(is_aligned(size, BytesPerWord),
1778+
"WB_DeallocateToMetaspaceTestArena: size is not a multiple of the system word byte size");
17701779
metaspace::MetaspaceTestArena* arena0 = (metaspace::MetaspaceTestArena*) arena;
1771-
arena0->deallocate((MetaWord*)p, (size_t) word_size);
1780+
arena0->deallocate((MetaWord*)p, (size_t) size / BytesPerWord);
17721781
WB_END
17731782

17741783
WB_ENTRY(jlong, WB_GetMaxMetaspaceAllocationSize(JNIEnv* env, jobject wb))
17751784
return (jlong) Metaspace::max_allocation_word_size() * BytesPerWord;
17761785
WB_END
17771786

1787+
WB_ENTRY(jlong, WB_WordSize(JNIEnv* env))
1788+
return (jlong)BytesPerWord;
1789+
WB_END
1790+
1791+
WB_ENTRY(jlong, WB_RootChunkWordSize(JNIEnv* env))
1792+
return (jlong)Metaspace::reserve_alignment_words();
1793+
WB_END
1794+
17781795
//////////////
17791796

17801797
WB_ENTRY(jlong, WB_AllocateMetaspace(JNIEnv* env, jobject wb, jobject class_loader, jlong size))
@@ -2956,8 +2973,8 @@ static JNINativeMethod methods[] = {
29562973
{CC"destroyMetaspaceTestContext", CC"(J)V", (void*)&WB_DestroyMetaspaceTestContext},
29572974
{CC"purgeMetaspaceTestContext", CC"(J)V", (void*)&WB_PurgeMetaspaceTestContext},
29582975
{CC"printMetaspaceTestContext", CC"(J)V", (void*)&WB_PrintMetaspaceTestContext},
2959-
{CC"getTotalCommittedWordsInMetaspaceTestContext", CC"(J)J",(void*)&WB_GetTotalCommittedWordsInMetaspaceTestContext},
2960-
{CC"getTotalUsedWordsInMetaspaceTestContext", CC"(J)J", (void*)&WB_GetTotalUsedWordsInMetaspaceTestContext},
2976+
{CC"getTotalCommittedBytesInMetaspaceTestContext", CC"(J)J",(void*)&WB_GetTotalCommittedBytesInMetaspaceTestContext},
2977+
{CC"getTotalUsedBytesInMetaspaceTestContext", CC"(J)J", (void*)&WB_GetTotalUsedBytesInMetaspaceTestContext},
29612978
{CC"createArenaInTestContext", CC"(JZ)J", (void*)&WB_CreateArenaInTestContext},
29622979
{CC"destroyMetaspaceTestArena", CC"(J)V", (void*)&WB_DestroyMetaspaceTestArena},
29632980
{CC"allocateFromMetaspaceTestArena", CC"(JJ)J", (void*)&WB_AllocateFromMetaspaceTestArena},
@@ -2977,6 +2994,8 @@ static JNINativeMethod methods[] = {
29772994
{CC"cleanMetaspaces", CC"()V", (void*)&WB_CleanMetaspaces},
29782995
{CC"rss", CC"()J", (void*)&WB_Rss},
29792996
{CC"printString", CC"(Ljava/lang/String;I)Ljava/lang/String;", (void*)&WB_PrintString},
2997+
{CC"wordSize", CC"()J", (void*)&WB_WordSize},
2998+
{CC"rootChunkWordSize", CC"()J", (void*)&WB_RootChunkWordSize}
29802999
};
29813000

29823001

test/hotspot/jtreg/runtime/Metaspace/elastic/Allocation.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
33
* Copyright (c) 2020 SAP SE. All rights reserved.
44
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
55
*
@@ -26,11 +26,11 @@
2626
public class Allocation {
2727

2828
public long p;
29-
public long word_size;
29+
public long size;
3030

31-
public Allocation(long p, long word_size) {
31+
public Allocation(long p, long size) {
3232
this.p = p;
33-
this.word_size = word_size;
33+
this.size = size;
3434
}
3535

3636
public boolean isNull() {
@@ -41,7 +41,7 @@ public boolean isNull() {
4141
public String toString() {
4242
return "Allocation{" +
4343
"p=" + p +
44-
", word_size=" + word_size +
44+
", size=" + size +
4545
'}';
4646
}
4747
}

test/hotspot/jtreg/runtime/Metaspace/elastic/AllocationProfile.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
33
* Copyright (c) 2020 SAP SE. All rights reserved.
44
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
55
*
@@ -43,6 +43,9 @@ public AllocationProfile(String name, long minimumSingleAllocationSize, long max
4343
this.name = name;
4444
}
4545

46+
/**
47+
* Returns random allocation size measured in words
48+
*/
4649
public long randomAllocationSize() {
4750
Random r = new Random();
4851
return r.nextInt((int)(maximumSingleAllocationSize - minimumSingleAllocationSize + 1)) + minimumSingleAllocationSize;

test/hotspot/jtreg/runtime/Metaspace/elastic/MetaspaceTestArena.java

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright (c) 2020, 2023, SAP SE. All rights reserved.
3-
* Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
3+
* Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
44
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
55
*
66
* This code is free software; you can redistribute it and/or modify it
@@ -31,68 +31,68 @@ public class MetaspaceTestArena {
3131

3232
final long allocationCeiling;
3333

34-
// Number and word size of allocations
35-
long allocatedWords = 0;
34+
// Number and byte size of allocations
35+
long allocatedBytes = 0;
3636
long numAllocated = 0;
37-
long deallocatedWords = 0;
37+
long deallocatedBytes = 0;
3838
long numDeallocated = 0;
3939
volatile long numAllocationFailures = 0;
4040

4141
private synchronized boolean reachedCeiling() {
42-
return (allocatedWords - deallocatedWords) > allocationCeiling;
42+
return (allocatedBytes - deallocatedBytes) > allocationCeiling;
4343
}
4444

45-
private synchronized void accountAllocation(long words) {
45+
private synchronized void accountAllocation(long size) {
4646
numAllocated ++;
47-
allocatedWords += words;
47+
allocatedBytes += size;
4848
}
4949

50-
private synchronized void accountDeallocation(long words) {
50+
private synchronized void accountDeallocation(long size) {
5151
numDeallocated ++;
52-
deallocatedWords += words;
52+
deallocatedBytes += size;
5353
}
5454

5555
MetaspaceTestArena(long arena0, long allocationCeiling) {
5656
this.allocationCeiling = allocationCeiling;
5757
this.arena = arena0;
5858
}
5959

60-
public Allocation allocate(long words) {
60+
public Allocation allocate(long size) {
6161
if (reachedCeiling()) {
6262
numAllocationFailures ++;
6363
return null;
6464
}
6565
WhiteBox wb = WhiteBox.getWhiteBox();
66-
long p = wb.allocateFromMetaspaceTestArena(arena, words);
66+
long p = wb.allocateFromMetaspaceTestArena(arena, size);
6767
if (p == 0) {
6868
numAllocationFailures ++;
6969
return null;
7070
} else {
71-
accountAllocation(words);
71+
accountAllocation(size);
7272
}
73-
return new Allocation(p, words);
73+
return new Allocation(p, size);
7474
}
7575

7676
public void deallocate(Allocation a) {
7777
WhiteBox wb = WhiteBox.getWhiteBox();
78-
wb.deallocateToMetaspaceTestArena(arena, a.p, a.word_size);
79-
accountDeallocation(a.word_size);
78+
wb.deallocateToMetaspaceTestArena(arena, a.p, a.size);
79+
accountDeallocation(a.size);
8080
}
8181

8282
//// Convenience functions ////
8383

84-
public Allocation allocate_expect_success(long words) {
85-
Allocation a = allocate(words);
84+
public Allocation allocate_expect_success(long size) {
85+
Allocation a = allocate(size);
8686
if (a.isNull()) {
87-
throw new RuntimeException("Allocation failed (" + words + ")");
87+
throw new RuntimeException("Allocation failed (" + size + ")");
8888
}
8989
return a;
9090
}
9191

92-
public void allocate_expect_failure(long words) {
93-
Allocation a = allocate(words);
92+
public void allocate_expect_failure(long size) {
93+
Allocation a = allocate(size);
9494
if (!a.isNull()) {
95-
throw new RuntimeException("Allocation failed (" + words + ")");
95+
throw new RuntimeException("Allocation failed (" + size + ")");
9696
}
9797
}
9898

@@ -104,9 +104,9 @@ boolean isLive() {
104104
public String toString() {
105105
return "arena=" + arena +
106106
", ceiling=" + allocationCeiling +
107-
", allocatedWords=" + allocatedWords +
107+
", allocatedBytes=" + allocatedBytes +
108108
", numAllocated=" + numAllocated +
109-
", deallocatedWords=" + deallocatedWords +
109+
", deallocatedBytes=" + deallocatedBytes +
110110
", numDeallocated=" + numDeallocated +
111111
", numAllocationFailures=" + numAllocationFailures +
112112
'}';

test/hotspot/jtreg/runtime/Metaspace/elastic/MetaspaceTestContext.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ public class MetaspaceTestContext {
3939

4040
HashSet<MetaspaceTestArena> arenaList = new HashSet<>();
4141

42-
long allocatedWords;
42+
long allocatedBytes;
4343
long numAllocated;
44-
long deallocatedWords;
44+
long deallocatedBytes;
4545
long numDeallocated;
4646
long allocationFailures;
4747

@@ -101,20 +101,20 @@ public void destroyArena(MetaspaceTestArena a) {
101101
}
102102
}
103103

104-
public long committedWords() {
104+
public long committedBytes() {
105105
long l = 0;
106106
if (context != 0) {
107107
WhiteBox wb = WhiteBox.getWhiteBox();
108-
l = wb.getTotalCommittedWordsInMetaspaceTestContext(context);
108+
l = wb.getTotalCommittedBytesInMetaspaceTestContext(context);
109109
}
110110
return l;
111111
}
112112

113-
public long usedWords() {
113+
public long usedBytes() {
114114
long l = 0;
115115
if (context != 0) {
116116
WhiteBox wb = WhiteBox.getWhiteBox();
117-
l = wb.getTotalUsedWordsInMetaspaceTestContext(context);
117+
l = wb.getTotalUsedBytesInMetaspaceTestContext(context);
118118
}
119119
return l;
120120
}
@@ -124,10 +124,10 @@ public int numLiveArenas() {
124124
}
125125

126126
public void updateTotals() {
127-
allocatedWords = deallocatedWords = numAllocated = numDeallocated = 0;
127+
allocatedBytes = deallocatedBytes = numAllocated = numDeallocated = 0;
128128
for (MetaspaceTestArena a : arenaList) {
129-
allocatedWords += a.allocatedWords;
130-
deallocatedWords += a.deallocatedWords;
129+
allocatedBytes += a.allocatedBytes;
130+
deallocatedBytes += a.deallocatedBytes;
131131
numAllocated += a.numAllocated;
132132
numDeallocated += a.numDeallocated;
133133
allocationFailures += a.numAllocationFailures;
@@ -159,17 +159,17 @@ public void checkStatistics() {
159159

160160
updateTotals();
161161

162-
long usageMeasured = usedWords();
163-
long committedMeasured = committedWords();
162+
long usageMeasured = usedBytes();
163+
long committedMeasured = committedBytes();
164164

165-
System.out.println("context used words " + usageMeasured + ", committed words " + committedMeasured
165+
System.out.println("context used bytes " + usageMeasured + ", committed bytes " + committedMeasured
166166
+ ".");
167167

168168
if (usageMeasured > committedMeasured) {
169169
throw new RuntimeException("Weirdness.");
170170
}
171171

172-
if (deallocatedWords > allocatedWords) {
172+
if (deallocatedBytes > allocatedBytes) {
173173
throw new RuntimeException("Weirdness.");
174174
}
175175

@@ -183,13 +183,13 @@ public void checkStatistics() {
183183
}
184184
}
185185

186-
long expectedMinUsage = allocatedWords - deallocatedWords;
186+
long expectedMinUsage = allocatedBytes - deallocatedBytes;
187187

188188
if (usageMeasured < expectedMinUsage) {
189189
throw new RuntimeException("Usage too low: " + usageMeasured + " expected at least " + expectedMinUsage);
190190
}
191191

192-
long expectedMaxUsage = allocatedWords;
192+
long expectedMaxUsage = allocatedBytes;
193193

194194
// This is necessary a bit fuzzy, since Metaspace usage consists of:
195195
// - whatever we allocated
@@ -199,12 +199,12 @@ public void checkStatistics() {
199199

200200
// Overhead per allocation (see metaspaceArena.cpp, get_raw_allocation_word_size() )
201201
// Any allocation is 3 words least
202-
expectedMaxUsage += (numAllocated * 3);
202+
expectedMaxUsage += (numAllocated * 3 * Settings.WORD_SIZE);
203203

204204
// Lets add a overhead per arena. Each arena carries a free block list containing
205205
// deallocated/retired blocks. We do not know how much. In general, the free block list should not
206206
// accumulate a lot of memory but be drained in the course of allocating memory from the arena.
207-
long overheadPerArena = 1024 * 1024 * numLiveArenas();
207+
long overheadPerArena = 1024 * 1024 * numLiveArenas() * Settings.WORD_SIZE;
208208
expectedMaxUsage += overheadPerArena;
209209

210210
if (expectedMaxUsage < usageMeasured) {
@@ -229,7 +229,7 @@ public void checkStatistics() {
229229
// are stress tests,
230230
//
231231
long expectedMaxCommitted = usageMeasured;
232-
expectedMaxCommitted += Settings.rootChunkWordSize;
232+
expectedMaxCommitted += Settings.ROOT_CHUNK_WORD_SIZE * Settings.WORD_SIZE;
233233
expectedMaxCommitted *= 10.0;
234234

235235
if (committedMeasured > expectedMaxCommitted) {
@@ -247,9 +247,9 @@ public java.lang.String toString() {
247247
", numArenasCreated=" + numArenasCreated +
248248
", numArenasDestroyed=" + numArenasDestroyed +
249249
", numLiveArenas=" + numLiveArenas() +
250-
", allocatedWords=" + allocatedWords +
250+
", allocatedBytes=" + allocatedBytes +
251251
", numAllocated=" + numAllocated +
252-
", deallocatedWords=" + deallocatedWords +
252+
", deallocatedBytes=" + deallocatedBytes +
253253
", numDeallocated=" + numDeallocated +
254254
", allocationFailures=" + allocationFailures +
255255
'}';

0 commit comments

Comments
 (0)