OpenHFT / Java-Thread-Affinity

Bind a java thread to a given core
http://chronicle.software/products/thread-affinity/
Apache License 2.0
1.78k stars 361 forks source link

Issue with AffinityThreadFactory in netty. Use only one cpu #35

Closed caoli5288 closed 6 years ago

caoli5288 commented 7 years ago

Env netty-4.1.9 and newest java-thread-affinity

ThreadFactory threadFactory = new AffinityThreadFactory("atf_wrk", AffinityStrategies.DIFFERENT_CORE);
EventLoopGroup workerGroup = new NioEventLoopGroup(nThread, threadFactory);
...

qq20170508121629

peter-lawrey commented 7 years ago

Can you clarify what the issue is? Are you saying all threads are using the same cpu?

On 8 May 2017 05:18, "Zhang" notifications@github.com wrote:

Env netty-4.1.9 and newest java-thread-affinity

ThreadFactory threadFactory = new AffinityThreadFactory("atf_wrk", AffinityStrategies.DIFFERENT_CORE);EventLoopGroup workerGroup = new NioEventLoopGroup(nThread, threadFactory);...

[image: qq20170508121629] https://cloud.githubusercontent.com/assets/3120587/25790054/588da402-33e8-11e7-96c3-143af455976e.png

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/OpenHFT/Java-Thread-Affinity/issues/35, or mute the thread https://github.com/notifications/unsubscribe-auth/ABBU8QHnd_qfWytk02HsLlgusTCI6MH1ks5r3pd3gaJpZM4NTd0n .

peter-lawrey commented 7 years ago

Can you clarify what the issue is? Are you saying all threads are using the same cpu?

On 8 May 2017 05:18, "Zhang" notifications@github.com wrote:

Env netty-4.1.9 and newest java-thread-affinity

ThreadFactory threadFactory = new AffinityThreadFactory("atf_wrk", AffinityStrategies.DIFFERENT_CORE);EventLoopGroup workerGroup = new NioEventLoopGroup(nThread, threadFactory);...

[image: qq20170508121629] https://cloud.githubusercontent.com/assets/3120587/25790054/588da402-33e8-11e7-96c3-143af455976e.png

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/OpenHFT/Java-Thread-Affinity/issues/35, or mute the thread https://github.com/notifications/unsubscribe-auth/ABBU8QHnd_qfWytk02HsLlgusTCI6MH1ks5r3pd3gaJpZM4NTd0n .

caoli5288 commented 7 years ago

Yes. All threads use the same cpu.

caoli5288 commented 7 years ago

I change the factory name to "netty-io" and dump the threads. All theads seems bind the same core that only one core had load in system top view.

peter-lawrey commented 7 years ago

Can you run AffinityThreadFactoryMain

On my machine I get the following showing different cpus.

[bg] INFO net.openhft.affinity.AffinityLock - No isolated CPUs found, so assuming CPUs 1 to 7 available. [bg] INFO net.openhft.affinity.AffinityLock - Assigning cpu 6 to Thread[bg,5,main] [bg-3] INFO net.openhft.affinity.AffinityLock - Assigning cpu 5 to Thread[bg-3,5,main] [bg-4] INFO net.openhft.affinity.AffinityLock - Assigning cpu 4 to Thread[bg-4,5,main]

[bg-2] INFO net.openhft.affinity.AffinityLock - Assigning cpu 3 to Thread[bg-2,5,main] The assignment of CPUs is 0: General use CPU 1: Reserved for this application 2: Reserved for this application 3: Reserved for this application 4: Thread[bg-4,5,main] alive=true 5: Thread[bg-3,5,main] alive=true 6: Thread[bg,5,main] alive=true 7: General use CPU

[bg-4] INFO net.openhft.affinity.LockInventory - Releasing cpu 4 from Thread[bg-4,5,main] [bg-2] INFO net.openhft.affinity.LockInventory - Releasing cpu 3 from Thread[bg-2,5,main] [bg] INFO net.openhft.affinity.LockInventory - Releasing cpu 6 from Thread[bg,5,main] [bg-3] INFO net.openhft.affinity.LockInventory - Releasing cpu 5 from Thread[bg-3,5,main]

This is the case even after I changed the following to match what you have.

private static final ExecutorService ES = Executors.newFixedThreadPool(4, new AffinityThreadFactory("bg", DIFFERENT_CORE)); ᐧ

On 8 May 2017 at 16:26, Zhang notifications@github.com wrote:

I change the factory name to "netty-io" and dump the threads.

https://cloud.githubusercontent.com/assets/3120587/25810949/37705be6-3444-11e7-9f90-fecb18baa905.png All theads seems bind the same core that only one core had load in system top view.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/OpenHFT/Java-Thread-Affinity/issues/35#issuecomment-299899816, or mute the thread https://github.com/notifications/unsubscribe-auth/ABBU8ZHpU6OF0WEMLGVfmQj4Y4I3IGQIks5r3zQmgaJpZM4NTd0n .

caoli5288 commented 7 years ago

@peter-lawrey Yes. I have made a custom ThreadFactory extends yours to log which cpu they used.

 +    public synchronized Thread newThread(final Runnable r) {
 +        final val n = this.name + "-" + (++next);
 +        val t = new Thread(new Runnable() {
 +            public void run() {
 +                val lock = lastLock == null ? AffinityLock.acquireLock() : lastLock.acquireLock(st);
 +                try {
 +                    int i = lock.cpuId();
 +                    if (i > -1) lastLock = lock;
 +                    logger.info(n + " affinity on cpu " + i);
(etc..)

I got those log output. seems in diff cpus.

03:01:07 [INFO]: netty-io-1 affinity on cpu 6
03:01:11 [INFO]: netty-io-2 affinity on cpu 5
03:01:17 [INFO]: netty-io-3 affinity on cpu 4
(etc..)

But load still in a same cpu core in top view :( what happen?

caoli5288 commented 7 years ago

@peter-lawrey Use AffinityThreadFactory in only worker group got balanced cpu usage. Puzzling. This may be related to netty's impl. Once it is applied to the boss group, cpu load will focus on the first boss thread. Puzzling.