Skip to content

Commit 6352ee1

Browse files
committed
8349007: The jtreg test ResolvedMethodTableHash takes excessive time
Reviewed-by: lmesnik, matsaave
1 parent 5f2a604 commit 6352ee1

File tree

2 files changed

+78
-51
lines changed

2 files changed

+78
-51
lines changed

src/hotspot/share/prims/resolvedMethodTable.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,13 @@ oop ResolvedMethodTable::find_method(const Method* method) {
175175

176176
ResolvedMethodTableLookup lookup(thread, method_hash(method), method);
177177
ResolvedMethodGet rmg(thread, method);
178-
_local_table->get(thread, lookup, rmg);
178+
bool rehash_warning = false;
179+
_local_table->get(thread, lookup, rmg, &rehash_warning);
180+
if (rehash_warning) {
181+
// if load factor is low but we need to rehash that's a problem with the hash function.
182+
log_info(membername, table)("Rehash warning, load factor %g", get_load_factor());
183+
trigger_concurrent_work();
184+
}
179185

180186
return rmg.get_res_oop();
181187
}
Lines changed: 71 additions & 50 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, 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
@@ -25,7 +25,11 @@
2525
* @test
2626
* @bug 8249719
2727
* @summary ResolvedMethodTable hash function should take method class into account
28-
* @run main/othervm/manual -Xmx256m -XX:MaxMetaspaceSize=256m ResolvedMethodTableHash 200000
28+
* @requires vm.flagless
29+
* @library /test/lib
30+
* @modules java.base/jdk.internal.misc
31+
* java.management
32+
* @run driver ResolvedMethodTableHash
2933
*/
3034

3135
import java.lang.invoke.MethodHandle;
@@ -35,72 +39,89 @@
3539
import java.util.ArrayList;
3640
import java.util.List;
3741

38-
// The test generates thousands MethodHandles to the methods of the same name
39-
// and the same signature. This should not take too long, unless Method hash
42+
import jdk.test.lib.process.OutputAnalyzer;
43+
import jdk.test.lib.process.ProcessTools;
44+
45+
// The test generates a thousand MethodHandles to the methods of the same name
46+
// and the same signature. The rehash warning shouldn't be returned, unless Method hash
4047
// function takes only the name and the signature as an input.
41-
public class ResolvedMethodTableHash extends ClassLoader {
48+
public class ResolvedMethodTableHash {
4249

43-
// Generate a MethodHandle for ClassName.m()
44-
private MethodHandle generate(String className) throws ReflectiveOperationException {
45-
byte[] buf = new byte[100];
46-
int size = writeClass(buf, className);
47-
Class<?> cls = defineClass(null, buf, 0, size);
48-
return MethodHandles.publicLookup().findStatic(cls, "m", MethodType.methodType(void.class));
49-
}
50+
public static class ResolvedMethodTableHashTest extends ClassLoader {
51+
// Generate a MethodHandle for ClassName.m()
52+
private MethodHandle generate(String className) throws ReflectiveOperationException {
53+
byte[] buf = new byte[100];
54+
int size = writeClass(buf, className);
55+
Class<?> cls = defineClass(null, buf, 0, size);
56+
return MethodHandles.publicLookup().findStatic(cls, "m", MethodType.methodType(void.class));
57+
}
5058

51-
private MethodHandle generateWithSameName() throws ReflectiveOperationException {
52-
byte[] buf = new byte[100];
53-
int size = writeClass(buf, "MH$$");
54-
// use different classloader instances to load the classes with the same name
55-
Class<?> cls = new ResolvedMethodTableHash().defineClass(null, buf, 0, size);
56-
return MethodHandles.publicLookup().findStatic(cls, "m", MethodType.methodType(void.class));
57-
}
59+
private MethodHandle generateWithSameName() throws ReflectiveOperationException {
60+
byte[] buf = new byte[100];
61+
int size = writeClass(buf, "MH$$");
62+
// use different classloader instances to load the classes with the same name
63+
Class<?> cls = new ResolvedMethodTableHashTest().defineClass(null, buf, 0, size);
64+
return MethodHandles.publicLookup().findStatic(cls, "m", MethodType.methodType(void.class));
65+
}
5866

59-
// Produce a class file with the given name and a single method:
60-
// public static native void m();
61-
private int writeClass(byte[] buf, String className) {
62-
return ByteBuffer.wrap(buf)
63-
.putInt(0xCAFEBABE) // magic
64-
.putInt(50) // version: 50
65-
.putShort((short) 7) // constant_pool_count: 7
67+
// Produce a class file with the given name and a single method:
68+
// public static native void m();
69+
private int writeClass(byte[] buf, String className) {
70+
return ByteBuffer.wrap(buf)
71+
.putInt(0xCAFEBABE) // magic
72+
.putInt(50) // version: 50
73+
.putShort((short) 7) // constant_pool_count: 7
6674
.put((byte) 7).putShort((short) 2)
6775
.put((byte) 1).putShort((short) className.length()).put(className.getBytes())
6876
.put((byte) 7).putShort((short) 4)
6977
.put((byte) 1).putShort((short) 16).put("java/lang/Object".getBytes())
7078
.put((byte) 1).putShort((short) 1).put("m".getBytes())
7179
.put((byte) 1).putShort((short) 3).put("()V".getBytes())
7280
.putShort((short) 0x21) // access_flags: public super
73-
.putShort((short) 1) // this_class: #1
74-
.putShort((short) 3) // super_class: #3
75-
.putShort((short) 0) // interfaces_count: 0
76-
.putShort((short) 0) // fields_count: 0
77-
.putShort((short) 1) // methods_count: 1
81+
.putShort((short) 1) // this_class: #1
82+
.putShort((short) 3) // super_class: #3
83+
.putShort((short) 0) // interfaces_count: 0
84+
.putShort((short) 0) // fields_count: 0
85+
.putShort((short) 1) // methods_count: 1
7886
.putShort((short) 0x109) // access_flags: public static native
79-
.putShort((short) 5) // name_index: #5
80-
.putShort((short) 6) // descriptor_index: #6
81-
.putShort((short) 0) // attributes_count: 0
82-
.putShort((short) 0) // attributes_count: 0
87+
.putShort((short) 5) // name_index: #5
88+
.putShort((short) 6) // descriptor_index: #6
89+
.putShort((short) 0) // attributes_count: 0
90+
.putShort((short) 0) // attributes_count: 0
8391
.position();
84-
}
92+
}
8593

86-
public static void main(String[] args) throws Exception {
87-
ResolvedMethodTableHash generator = new ResolvedMethodTableHash();
88-
List<MethodHandle> handles = new ArrayList<>();
94+
public static void main(String[] args) throws Exception {
8995

90-
int count = args.length > 0 ? Integer.parseInt(args[0]) : 200000;
96+
ResolvedMethodTableHashTest generator = new ResolvedMethodTableHashTest();
97+
List<MethodHandle> handles = new ArrayList<>();
9198

92-
for (int i = 0; i < count; i++) {
93-
// prevents metaspace oom
94-
if (i % 20 != 0) {
95-
handles.add(generator.generate("MH$" + i));
96-
} else {
97-
handles.add(generator.generateWithSameName());
98-
}
99-
if (i % 1000 == 0) {
100-
System.out.println("Generated " + i + " handles");
99+
int count = 1001;
100+
101+
for (int i = 0; i < count; i++) {
102+
// prevents metaspace oom
103+
if (i % 20 != 0) {
104+
handles.add(generator.generate("MH$" + i));
105+
} else {
106+
handles.add(generator.generateWithSameName());
107+
}
108+
if (i % 1000 == 0) {
109+
System.out.println("Generated " + i + " handles");
110+
}
101111
}
112+
113+
System.out.println("Test passed");
102114
}
115+
}
116+
117+
public static void main(String[] args) throws Exception {
103118

104-
System.out.println("Test passed");
119+
// Running the table with only 1000 entries should not provoke a needs rehash warning, unless the hash code is bad.
120+
ProcessBuilder pb = ProcessTools.createLimitedTestJavaProcessBuilder("-Xlog:membername+table",
121+
ResolvedMethodTableHashTest.class.getName());
122+
OutputAnalyzer output = new OutputAnalyzer(pb.start());
123+
output.shouldNotContain("[membername,table] Rehash warning, load factor");
124+
output.shouldContain("Generated 1000 handles");
125+
output.shouldHaveExitValue(0);
105126
}
106127
}

0 commit comments

Comments
 (0)