Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Java] speed test codegen speed by avoid duplicate codegen #929

Merged
merged 8 commits into from
Oct 3, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refine gc trigger
chaokunyang committed Oct 3, 2023

Verified

This commit was signed with the committer’s verified signature.
Cruikshanks Alan Cruikshanks
commit 300e9ea72eccd522d4600b6614e292bfe524bf6f
2 changes: 1 addition & 1 deletion java/fury-core/src/test/java/io/fury/FuryTest.java
Original file line number Diff line number Diff line change
@@ -441,7 +441,7 @@ public void testClassGC() {
TestUtils.triggerOOMForSoftGC(
() -> {
System.out.printf("Wait map keys %s gc.\n", map.keySet());
return map.size() > 0;
return !map.isEmpty();
});
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
}
26 changes: 17 additions & 9 deletions java/fury-core/src/test/java/io/fury/TestUtils.java
Original file line number Diff line number Diff line change
@@ -45,24 +45,32 @@ public static <K, V> ImmutableMap<K, V> mapOf(K k1, V v1) {
* @param predicate whether stop Trigger OOM.
*/
public static void triggerOOMForSoftGC(Supplier<Boolean> predicate) {
System.gc();
while (predicate.get()) {
// Force an OoM
triggerOOM();
System.gc();
System.out.printf("Wait gc.");
try {
Thread.sleep(50);
} catch (InterruptedException e1) {
throw new RuntimeException(e1);
}
}
}

private static void triggerOOM() {
while (true) {
// Force an OOM
try {
final ArrayList<Object[]> allocations = new ArrayList<>();
int size;
while ((size =
Math.min(Math.abs((int) Runtime.getRuntime().freeMemory()), Integer.MAX_VALUE))
> 0) allocations.add(new Object[size]);
} catch (OutOfMemoryError e) {
System.out.println("Trigger OOM to clear LoaderBinding.furySoftMap soft references.");
System.out.println("Met OOM.");
break;
}
System.gc();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.printf("Wait gc.");
}
}
}
24 changes: 8 additions & 16 deletions java/fury-core/src/test/java/io/fury/ThreadSafeFuryTest.java
Original file line number Diff line number Diff line change
@@ -25,7 +25,6 @@
import io.fury.test.bean.BeanA;
import io.fury.test.bean.Struct;
import io.fury.util.LoaderBinding.StagingType;
import java.util.ArrayList;
import java.util.WeakHashMap;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
@@ -224,21 +223,14 @@ public void testClassDuplicateName(StagingType staging) {
public void testClassGC() throws Exception {
// Can't inline `generateClassForGC` in current method, generated classes won't be gc.
WeakHashMap<Class<?>, Boolean> map = generateClassForGC();
while (map.size() > 0) {
// Force an OoM
try {
final ArrayList<Object[]> allocations = new ArrayList<>();
int size;
while ((size =
Math.min(Math.abs((int) Runtime.getRuntime().freeMemory()), Integer.MAX_VALUE))
> 0) allocations.add(new Object[size]);
} catch (OutOfMemoryError e) {
System.out.println("Trigger OOM to clear LoaderBinding.furySoftMap soft references.");
}
System.gc();
Thread.sleep(1000);
System.out.printf("Wait classes %s gc.\n", map.keySet());
}
TestUtils.triggerOOMForSoftGC(
() -> {
if (!map.isEmpty()) {
System.out.printf("Wait classes %s gc.\n", map.keySet());
return true;
}
return false;
});
}

private WeakHashMap<Class<?>, Boolean> generateClassForGC() {
Original file line number Diff line number Diff line change
@@ -168,7 +168,7 @@ public void testJaninoCompiler() throws Exception {
WeakReference<? extends Class<?>> clsRef = new WeakReference<>(compileClassByJaninoCompiler());
while (clsRef.get() != null) {
System.gc();
Thread.sleep(1000);
Thread.sleep(50);
System.out.printf("Wait cls %s gc.\n", clsRef.get());
}
}
@@ -212,10 +212,10 @@ public Class<?> compileClassByJaninoCompiler() throws Exception {
@Test
public void testJaninoCompileDependentClass() throws Exception {
WeakReference<? extends Class<?>> clsRef =
janinoCompileDependentClass(Struct.createStructClass("A", 1));
janinoCompileDependentClass(Struct.createStructClass("A", 1, false));
while (clsRef.get() != null) {
System.gc();
Thread.sleep(1000);
Thread.sleep(10);
System.out.printf("Wait cls %s gc.\n", clsRef.get());
}
}
@@ -287,10 +287,9 @@ public void testGetClassStats() {
@Test(timeOut = 60000)
public void testJaninoGeneratedClassGC() throws InterruptedException {
WeakReference<Class<?>> clsRef = janinoGenerateClass();
;
while (clsRef.get() != null) {
System.gc();
Thread.sleep(1000);
Thread.sleep(10);
System.out.printf("Wait cls %s gc.\n", clsRef.get());
}
}
@@ -322,13 +321,13 @@ public void testJDKGeneratedClassGC() throws InterruptedException {
WeakReference<Class<?>> clsRef = jdkGenerateClass();
while (clsRef.get() != null) {
System.gc();
Thread.sleep(1000);
Thread.sleep(10);
System.out.printf("Wait cls %s gc.\n", clsRef.get());
}
}

private WeakReference<Class<?>> jdkGenerateClass() {
Class<?> cls = Struct.createStructClass("TestGeneratedClassGC", 1);
Class<?> cls = Struct.createStructClass("TestGeneratedClassGC", 1, false);
return new WeakReference<>(cls);
}
}
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@ public void testMap() throws Exception {
referencesField.setAccessible(true);
Set<Reference<?>> references = (Set<Reference<?>>) referencesField.get(null);
System.gc();
Thread.sleep(1000);
Thread.sleep(50);
LOG.info("Before references: {}", references);
int size = references.size();
Object o1 = new Object();
@@ -47,7 +47,7 @@ public void testMap() throws Exception {

// `references` is global and may contain references put by others.
while (references.size() != size) {
Thread.sleep(1000);
Thread.sleep(10);
System.gc();
LOG.info("wait object gc, references: {}", references);
}
Original file line number Diff line number Diff line change
@@ -190,6 +190,13 @@ public static Object createPOJO(Class<?> clz) {
new ConcurrentHashMap<>();

public static Class<?> loadClass(Object key, Supplier<Class<?>> func) {
return loadClass(key, true, func);
}

public static Class<?> loadClass(Object key, boolean cache, Supplier<Class<?>> func) {
if (!cache) {
return func.get();
}
Object lock = cacheLock.computeIfAbsent(key, k -> new Object());
synchronized (lock) {
SoftReference<Class<?>> ref = classCache.get(key);
@@ -254,13 +261,17 @@ public static Class<?> createNumberStructClass(String classname, int repeat) {

/** Create Class. */
public static Class<?> createStructClass(String classname, int repeat) {
return createStructClass(classname, repeat, true);
}

public static Class<?> createStructClass(String classname, int repeat, boolean cache) {
if (StringUtils.isBlank(classname)) {
throw new IllegalArgumentException("Class name is empty");
}
String key = "createStructClass" + classname + repeat;

return loadClass(
key,
cache,
() -> {
StringBuilder classCode =
new StringBuilder(