How to reduce high CPU usage caused by receiver thread of CAN messages?

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Re: How to reduce high CPU usage caused by receiver thread of CAN messages?

Adding ::Sleep() to code doesn't "solve" anything.

At best it can give you a false perception that something is running "better" under the specific circumstances you're testing it on. On another computer, with another resource load, other cpu, memory... You may very well have made things even worse by adding that Sleep().

Re: How to reduce high CPU usage caused by receiver thread of CAN messages?

What "issue"?
CPU usage isn't necessarily an issue, as long as real work is getting done. If you are in a hard-spin waiting for data, then a blocking API would be better (as discussed in the other thread).

Re: How to reduce high CPU usage caused by receiver thread of CAN messages?

Originally Posted by Codeplug

What "issue"?
CPU usage isn't necessarily an issue, as long as real work is getting done. If you are in a hard-spin waiting for data, then a blocking API would be better (as discussed in the other thread).

What CAN library are you using - and how are you using it?

gg

But for my case its an issue,because of High CPU usage the performance of HMI is degrading.After 3 hours of continuous HMI Pc getting slower.

Re: How to reduce high CPU usage caused by receiver thread of CAN messages?

>> My code is taking almost 60% of CPU usage due to the Receiver thread used for receiving CAN messages.
Not sure how to help without seeing some real code - other than "don't call Sleep" and follow the advice in the other thread.

Re: How to reduce high CPU usage caused by receiver thread of CAN messages?

Just google'ing the documentation for xlReceive(), I can see that it isn't being used correctly (msgsrx is uninitialized). It also appears that xlReceive() is always non-blocking, so you're code will be in a hard spin doing a whole lot of nothing while XL_ERR_QUEUE_IS_EMPTY is returned.

Look into the xlSetNotification() API. This will give you a Win32 waitable handle that can be used to wait efficiently for messages to come in. Also consider calling xlReceive() to read more than just 1 event at a time.

Re: How to reduce high CPU usage caused by receiver thread of CAN messages?

Originally Posted by Codeplug

Just google'ing the documentation for xlReceive(), I can see that it isn't being used correctly (msgsrx is uninitialized). It also appears that xlReceive() is always non-blocking, so you're code will be in a hard spin doing a whole lot of nothing while XL_ERR_QUEUE_IS_EMPTY is returned.

Look into the xlSetNotification() API. This will give you a Win32 waitable handle that can be used to wait efficiently for messages to come in. Also consider calling xlReceive() to read more than just 1 event at a time.

Can you please tell me how to avoid the non-blocking of xlreceive().I tried adding xlSetNotification() API in my code.And how can I call xlreceive() to read more than 1 event at a time?
Please help me with this.I am nearing the deadline.

I have used this tool and I have seen that there is no memory leak.The CPU usage is mainly because of receiver thread which is continuously running,that is used for receiving messages from CAN Bus.The messages come at a rate of 40 milliseconds but this thread is running at a rate in microseconds so increasing CPU usage.So i wanted to know if anything else can be done in the code,which I attached earlier to reduce this fast loop?

Re: How to reduce high CPU usage caused by receiver thread of CAN messages?

If the new code is calling WaitForSingleObject(), then xlReceive() should efficiently return a message on every call - which is followed by the real CPU work of processing that message.

So assuming the CPU usage reflects real work, why do you want to reduce it?
Are you looking to "sample" less than 25 messages a second?
Or do you want to process all 25 messages per second, but use less CPU than you are using now to do so?

If it's the later, then you will have to profile your code to see where the "hot spots" are and optimize.

Re: How to reduce high CPU usage caused by receiver thread of CAN messages?

Originally Posted by Codeplug

If the new code is calling WaitForSingleObject(), then xlReceive() should efficiently return a message on every call - which is followed by the real CPU work of processing that message.

So assuming the CPU usage reflects real work, why do you want to reduce it?
Are you looking to "sample" less than 25 messages a second?
Or do you want to process all 25 messages per second, but use less CPU than you are using now to do so?

If it's the later, then you will have to profile your code to see where the "hot spots" are and optimize.

gg

I think new messages are coming before the previously received messages are parsed.As a result the buffer is overwritten.So can you tell me am I thinking in the correct way?If so by creating a buffer of larger size can solve this issue.The incoming messages are of 8 bytes and a total of 11 messages I am getting,so I need to store all 11 messages in the buffer and then parse the data accordingly.