Closed jvanlangen closed 5 months ago
I've found something that might be causing hi-cpu load.
The problem is, that when there is no data, it will directly continue the while loop. There is no sleep/wait event here. So it acks like a shortcircuit. I just added a sleep(1) which brings the cpu usage down dramatically.
Any thoughts?
This is my breakage, I didn't notice that it's caused high CPU. What I should have probably done is do the ReadAsync() and a TimerAsync (driven by the NeedsPulse), await either.. check if the Read is complete, if it is, go and do the read stuff.. otherwise if the NeedsPulse is ready, go and do the timer stuff (then reset timer task.. or just set it as autoresetting) then go back to waiting for them..
But short term... you could just check out the previous revision 990a7ac514ff5ad4834e0cc00d68fc853d851c33
I'll try to fix this up in the next week or so.
Reverted back to the previous version. Try the latest master and let us know if you still see high CPU usage.
I also found some small points in the code, which are not quite nice in terms of code structure.
For example (stream as NetworkStream).DataAvailable
, now you can be sure that stream
is a NetworkStream
, but if this ever changes you will get a NullReferenceException
here. NullReferenceExceptions are generally seen as sloppiness.
It's better to do a hard cast here. ((NetworkStream)stream).DataAvailable
, if the stream is not NetworkStream
, you will get a InvalidCastException
which explains the error better.
Would you mind if I contributed something?
Sure, feel free.
At the moment we suffer from high cpu usage when we activate the LibUA server. Any known issues with that?
My plan: