-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.navercorp.pinpoint.profiler.cache; | ||
|
||
import com.github.benmanes.caffeine.cache.Caffeine; | ||
|
||
public class CaffeineBuilder { | ||
Check warning on line 5 in agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/cache/CaffeineBuilder.java Codecov / codecov/patchagent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/cache/CaffeineBuilder.java#L5
|
||
|
||
static final ExecutorManager MANAGER = new ExecutorManager(); | ||
|
||
public static final int MAX_CPU = 4; | ||
|
||
|
||
public static Caffeine<Object, Object> newBuilder() { | ||
final Caffeine<Object, Object> cacheBuilder = Caffeine.newBuilder(); | ||
cacheBuilder.executor(MANAGER.executor()); | ||
return cacheBuilder; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.navercorp.pinpoint.profiler.cache; | ||
|
||
import com.navercorp.pinpoint.common.annotations.VisibleForTesting; | ||
import com.navercorp.pinpoint.common.profiler.concurrent.ExecutorFactory; | ||
import com.navercorp.pinpoint.common.util.CpuUtils; | ||
|
||
import java.util.concurrent.Executor; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class ExecutorManager { | ||
|
||
private volatile ExecutorService executor; | ||
|
||
@VisibleForTesting | ||
static ExecutorService cacheExecutor() { | ||
// TODO Cleanup the executor when the agent is shutdown | ||
final int nThreads = cpuCount(CpuUtils.cpuCount()); | ||
// Must set the same property as ForkJoinPool.commonPool() | ||
return ExecutorFactory.newFixedThreadPool(nThreads, Integer.MAX_VALUE, "Caffeine", true); | ||
} | ||
|
||
@VisibleForTesting | ||
static int cpuCount(int cpuCount) { | ||
cpuCount = cpuCount - 1; | ||
if (cpuCount <= 0) { | ||
return 1; | ||
} | ||
return Math.min(cpuCount, CaffeineBuilder.MAX_CPU); | ||
} | ||
|
||
@VisibleForTesting | ||
public Executor executor() { | ||
if (executor != null) { | ||
return executor; | ||
} | ||
synchronized (this) { | ||
if (executor != null) { | ||
return executor; | ||
Check warning on line 39 in agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/cache/ExecutorManager.java Codecov / codecov/patchagent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/cache/ExecutorManager.java#L39
|
||
} | ||
executor = cacheExecutor(); | ||
return executor; | ||
} | ||
} | ||
|
||
@VisibleForTesting | ||
void shutdown() { | ||
if (executor == null) { | ||
return; | ||
} | ||
synchronized (this) { | ||
if (executor == null) { | ||
return; | ||
Check warning on line 53 in agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/cache/ExecutorManager.java Codecov / codecov/patchagent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/cache/ExecutorManager.java#L53
|
||
} | ||
executor.shutdown(); | ||
try { | ||
executor.awaitTermination(3000, TimeUnit.MILLISECONDS); | ||
} catch (InterruptedException ignore) { | ||
Check warning on line 58 in agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/cache/ExecutorManager.java Codecov / codecov/patchagent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/cache/ExecutorManager.java#L58
|
||
} | ||
executor = null; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.navercorp.pinpoint.profiler.cache; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class CaffeineBuilderTest { | ||
|
||
|
||
@Test | ||
void executor_manager() { | ||
CaffeineBuilder.newBuilder(); | ||
CaffeineBuilder.MANAGER.shutdown(); | ||
} | ||
|
||
@Test | ||
void executor_manager_null_check() { | ||
CaffeineBuilder.newBuilder(); | ||
CaffeineBuilder.newBuilder(); | ||
CaffeineBuilder.MANAGER.shutdown(); | ||
|
||
CaffeineBuilder.newBuilder(); | ||
CaffeineBuilder.MANAGER.shutdown(); | ||
CaffeineBuilder.MANAGER.shutdown(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.navercorp.pinpoint.profiler.cache; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.concurrent.ThreadPoolExecutor; | ||
|
||
class ExecutorManagerTest { | ||
@Test | ||
void cpuCount() { | ||
Assertions.assertEquals(1, ExecutorManager.cpuCount(0)); | ||
|
||
Assertions.assertEquals(1, ExecutorManager.cpuCount(1)); | ||
Assertions.assertEquals(1, ExecutorManager.cpuCount(2)); | ||
Assertions.assertEquals(2, ExecutorManager.cpuCount(3)); | ||
Assertions.assertEquals(3, ExecutorManager.cpuCount(4)); | ||
|
||
Assertions.assertEquals(4, ExecutorManager.cpuCount(5)); | ||
Assertions.assertEquals(4, ExecutorManager.cpuCount(16)); | ||
} | ||
|
||
|
||
@Test | ||
void cacheExecutor() { | ||
ThreadPoolExecutor executor = (ThreadPoolExecutor) ExecutorManager.cacheExecutor(); | ||
executor.prestartAllCoreThreads(); | ||
|
||
executor.shutdown(); | ||
} | ||
|
||
} |