[ 
https://issues.apache.org/jira/browse/DIRMINA-934?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14371499#comment-14371499
 ] 

Chris Kistner commented on DIRMINA-934:
---------------------------------------

Thanks [~quhw]!

With MINA 2.0.7, we used to create the acceptor with 20 NIO processors and it 
worked pretty well:
{code:java}acceptor = new NioSocketAcceptor(NIO_ACCEPTOR_THREADS);
acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new 
OurProtocolCodecFactory()));{code}

However with MINA 2.0.8/2.0.9, the performance degraded considerably, where all 
20 of our MINA threads were in a waiting state:
{noformat}"NioProcessor-5" - Thread t@1091
   java.lang.Thread.State: WAITING
        at sun.misc.Unsafe.park(Native Method)
        - parking to wait for <1562a344> (a 
java.util.concurrent.Semaphore$FairSync)
        at java.util.concurrent.locks.LockSupport.park(LockSupport.java:186)
        at 
java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:834)
        at 
java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireSharedInterruptibly(AbstractQueuedSynchronizer.java:994)
        at 
java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly(AbstractQueuedSynchronizer.java:1303)
        at java.util.concurrent.Semaphore.acquire(Semaphore.java:317)
        at 
org.apache.mina.filter.codec.ProtocolCodecFilter.messageReceived(ProtocolCodecFilter.java:231)
        at 
org.apache.mina.core.filterchain.DefaultIoFilterChain.callNextMessageReceived(DefaultIoFilterChain.java:542)
        at 
org.apache.mina.core.filterchain.DefaultIoFilterChain.access$1300(DefaultIoFilterChain.java:48)
        at 
org.apache.mina.core.filterchain.DefaultIoFilterChain$EntryImpl$1.messageReceived(DefaultIoFilterChain.java:943)
        at 
org.apache.mina.core.filterchain.IoFilterAdapter.messageReceived(IoFilterAdapter.java:109)
        at 
org.apache.mina.core.filterchain.DefaultIoFilterChain.callNextMessageReceived(DefaultIoFilterChain.java:542)
        at 
org.apache.mina.core.filterchain.DefaultIoFilterChain.fireMessageReceived(DefaultIoFilterChain.java:535)
        at 
org.apache.mina.core.polling.AbstractPollingIoProcessor.read(AbstractPollingIoProcessor.java:714)
        at 
org.apache.mina.core.polling.AbstractPollingIoProcessor.process(AbstractPollingIoProcessor.java:668)
        at 
org.apache.mina.core.polling.AbstractPollingIoProcessor.process(AbstractPollingIoProcessor.java:657)
        at 
org.apache.mina.core.polling.AbstractPollingIoProcessor.access$600(AbstractPollingIoProcessor.java:67)
        at 
org.apache.mina.core.polling.AbstractPollingIoProcessor$Processor.run(AbstractPollingIoProcessor.java:1121)
        at 
org.apache.mina.util.NamePreservingRunnable.run(NamePreservingRunnable.java:64)
        at 
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at 
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:724)

   Locked ownable synchronizers:
        - locked <72562fdf> (a 
java.util.concurrent.ThreadPoolExecutor$Worker){noformat}

Now I've changed our code to add the threadpool to the filter chain to get the 
performance like we had with MINA 2.0.7:
{code:java}minaWorkers = new OrderedThreadPoolExecutor(1, nioProcessorCount, 
60L, TimeUnit.SECONDS,
        new OurLablingDefaultThreadFactory("minaWorker"), null);

acceptor = new NioSocketAcceptor(2);
acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new 
OurProtocolCodecFactory()));
acceptor.getFilterChain().addLast("minaWorkers", new 
ExecutorFilter(minaWorkers));{code}

Alsod take note that if you don't specify the processorCount argument for the 
NioSocketAcceptor and you have more than 1 CPU core/thread (like if you have a 
Quad Core Xeon CPU with hyperthreading enabled), you may also run into this 
semaphore lock issue under high network loads! We haven't experimented yet with 
other values for the processorCount to see where the threshold/optimum 
performance is.

I hope this helps other people struggling with the same issue.

> Replace synchronized with a Semaphore for better performance
> ------------------------------------------------------------
>
>                 Key: DIRMINA-934
>                 URL: https://issues.apache.org/jira/browse/DIRMINA-934
>             Project: MINA
>          Issue Type: Improvement
>          Components: Core
>    Affects Versions: 2.0.7, 2.0.8
>         Environment: Window 8 Pro x64, JDK 7
>            Reporter: Paul Gregoire
>              Labels: patch
>             Fix For: 2.0.8
>
>         Attachments: ProtocolCodecFilterWithSemaphoreAndMore.diff
>
>   Original Estimate: 2h
>  Remaining Estimate: 2h
>
> Replacing the synchronized block with a Semaphore in the ProtocolCodecFilter 
> provides a lot of benefit in terms of locking and also reduces CPU 
> utilization. See attached git diff.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to