Refinitiv / Real-Time-SDK

Other
184 stars 129 forks source link

High CPU when running consumer (EMA C#) #284

Closed michalk-gc closed 1 week ago

michalk-gc commented 1 month ago

I'm running Cons300_NET6.0 project from RTSDK_NET6.0 solution which simply requests 1 symbol and after few responses unregisters the request handle. I've observed that even there are no active requests, the process is still consuming 5-10% of CPU, which is surprisingly high considering no job is actually in progress.

During debug, I realized that most likely it's caused by busy loop inside ReactorDispatchLoop (OmmBaseImpl.cs). At line 673, Socket.Select is called inside "while forever" with timeout = 0, which probably returns immediately. Unfortunately, the property of ConsumerConfig (DispatchTimeoutApiThread) which could help in setting the timeout to non-zero value is not exposed as public. I've managed to workaround that using reflection to access private members of consumer right after it's created:

            // get private member "m_OmmConsumerImpl" from consumer
            var consumerImpl = consumer.GetType().GetField("m_OmmConsumerImpl", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(consumer);
            // set protected field DispatchTimeoutApiThread on consumer impl
            consumerImpl.GetType().GetField("DispatchTimeoutApiThread", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).SetValue(consumerImpl, 10);

which solves the problem - even 10 microseconds timeout seems to be relief and CPU usage goes down to 0%.

Could you please expose this property as public or change default value to non-zero or advice what are other configuration options to avoid busy loop and high CPU usage.

soranat commented 1 month ago

DispatchTimeoutApiThread is a configurable parameter in EMA under the ConsumerGroup section which you can configure it in both EMAConfig.xml file or programmatic configuration (please see Cons421 as example).

Please take a look at EMA C# configuration guide for more details at https://github.com/Refinitiv/Real-Time-SDK/blob/master/CSharp/Ema/Docs/EMACSharp_ConfigGuide.pdf

vlevendel commented 1 week ago

@michalk-gc Please reopen if you have further questions. Closing.