How to avoid a tight real time loop
Hello. In my application I need to read data from an ADC card in blocks of a given number of samples. For efficiency I'm using an mmapped buffer to exchange data with the ADC driver. I'm setting up the acquisition, and then calling a loop like this:
unsigned int required = <compute required number of bytes>; unsigned int read = 0;
while (1) {
while (read < required) {
read = a4l_pool(...); }
process(buffer, ...);
}
However if I run this loop in a real time thread the tight while loop is an obvious cpu hog: the watchdog kicks in and kills the thread.
There is not an analogy API that can be used to require a certain amount of data to the ADC driver. How can I code this loop to let other processes to run?
Thanks. Cheers,
Signature · 1 line
Daniele
Quoted 14 lines · Daniele Nicolodi
Hello. In my application I need to read data from an ADC card in blocks of a given number of samples. For efficiency I'm using an mmapped buffer to exchange data with the ADC driver. I'm setting up the acquisition, and then calling a loop like this:
unsigned int required = <compute required number of bytes>; unsigned int read = 0;
while (1) {while (read < required) { read = a4l_pool(...);}
process(buffer, ...);
}
Quoted 6 lines
However if I run this loop in a real time thread the tight while loop is an obvious cpu hog: the watchdog kicks in and kills the thread.
There is not an analogy API that can be used to require a certain amount of data to the ADC driver. How can I code this loop to let other processes to run?
You should define a period, and how much data you want to process during this period. Then make your task periodic, and wait for the next period at the beginning of the loop.
Quoted 2 lines
Thanks. Cheers,
Signature · 1 line
Gilles.
Hi,
Quoted 14 lines · Daniele Nicolodi
Hello. In my application I need to read data from an ADC card in blocks of a given number of samples. For efficiency I'm using an mmapped buffer to exchange data with the ADC driver. I'm setting up the acquisition, and then calling a loop like this:
unsigned int required = <compute required number of bytes>; unsigned int read = 0;
while (1) {while (read < required) { read = a4l_pool(...);}
process(buffer, ...);
}
Quoted 6 lines
However if I run this loop in a real time thread the tight while loop is an obvious cpu hog: the watchdog kicks in and kills the thread.
There is not an analogy API that can be used to require a certain amount of data to the ADC driver. How can I code this loop to let other processes to run?
As a matter of fact, there is something in the API which could have helped you in an unmapped configuration. If you set the flag A4L_CMD_BULK in the field "flags" of the command structure, a call to a4l_async_read() only returns when the data size specified as argument is available.
Unfortunately, I have not done the same thing with a4l_poll, I might have added the requested size as an additional argument to a4l_poll.
Any idea on how to extend the API without breaking it ?
Quoted 9 lines
Thanks. Cheers,
Signature · 6 lines
Daniele _______________________________________________ Xenomai-help mailing list [email protected] https://mail.gna.org/listinfo/xenomai-help
Signature · 1 line
Alexis.
Quoted 9 lines · Alexis Berlemont
Quoted 3 lines
There is not an analogy API that can be used to require a certain amount of data to the ADC driver. How can I code this loop to let other processes to run?
As a matter of fact, there is something in the API which could have helped you in an unmapped configuration. If you set the flag
A4L_CMD_BULKin the field "flags" of the command structure, a call toa4l_async_read()only returns when the data size specified as argument is available.
Unfortunately copying memory data around would decrease too much the performance of my data acquisition code. By the way, I think that using the O_NONBLOCK fcntl flag on the device descriptor file descriptor would be the best way to activate this feature.
Quoted 4 lines
Unfortunately, I have not done the same thing with
a4l_poll, I might have added the requested size as an additional argument toa4l_poll.Any idea on how to extend the API without breaking it ?
I do not see any way rather than augmenting the function signature with an additional parameter. If you want to maintain API compatibility the only thing to do is to add a different function.
Cheers,
Signature · 1 line
Daniele
Quoted 30 lines · Alexis Berlemont
Hi,
Quoted 14 lines · Daniele Nicolodi
Hello. In my application I need to read data from an ADC card in blocks of a given number of samples. For efficiency I'm using an mmapped buffer to exchange data with the ADC driver. I'm setting up the acquisition, and then calling a loop like this:
unsigned int required = <compute required number of bytes>; unsigned int read = 0;
while (1) {while (read < required) { read = a4l_pool(...);}
process(buffer, ...);
}Quoted 6 lines
However if I run this loop in a real time thread the tight while loop is an obvious cpu hog: the watchdog kicks in and kills the thread.
There is not an analogy API that can be used to require a certain amount of data to the ADC driver. How can I code this loop to let other processes to run?
As a matter of fact, there is something in the API which could have helped you in an unmapped configuration. If you set the flag
A4L_CMD_BULKin the field "flags" of the command structure, a call toa4l_async_read()only returns when the data size specified as argument is available.
Quoted 4 lines
Unfortunately, I have not done the same thing with
a4l_poll, I might have added the requested size as an additional argument toa4l_poll.Any idea on how to extend the API without breaking it ?
Maybe it would be possible to use the standard "select" call, with a threshold configurable with an ioctl ?
Signature · 1 line
Gilles.
Quoted 31 lines · Gilles Chanteperdrix
Quoted 30 lines · Alexis Berlemont
Hi,
Quoted 14 lines · Daniele Nicolodi
Hello. In my application I need to read data from an ADC card in blocks of a given number of samples. For efficiency I'm using an mmapped buffer to exchange data with the ADC driver. I'm setting up the acquisition, and then calling a loop like this:
unsigned int required = <compute required number of bytes>; unsigned int read = 0;
while (1) {while (read < required) { read = a4l_pool(...);}
process(buffer, ...);
}Quoted 6 lines
However if I run this loop in a real time thread the tight while loop is an obvious cpu hog: the watchdog kicks in and kills the thread.
There is not an analogy API that can be used to require a certain amount of data to the ADC driver. How can I code this loop to let other processes to run?
As a matter of fact, there is something in the API which could have helped you in an unmapped configuration. If you set the flag
A4L_CMD_BULKin the field "flags" of the command structure, a call toa4l_async_read()only returns when the data size specified as argument is available.
Quoted 7 lines
Quoted 4 lines
Unfortunately, I have not done the same thing with
a4l_poll, I might have added the requested size as an additional argument toa4l_poll.Any idea on how to extend the API without breaking it ?
Maybe it would be possible to use the standard "select" call, with a threshold configurable with an ioctl ?
Thanks. I added your idea in my TODO list.
Quoted 4 lines
Signature · 2 lines
-- Gilles.
Signature · 1 line
Alexis.