Titus
Titus

Reputation: 43

Need some help in C code for optimization (Poll + delay/sleep)

Currently I'm polling the register to get the expected value and now I want reduce the CPU usage and increase the performance. So, I think, if we do polling for particular time (Say for 10ms) and if we didn't get expected value then wait for some time (like udelay(10*1000) or usleep(10*1000) delay/sleep in ms) then continue to do polling for more more extra time (Say 100ms) and still if you didn't get the expected value then do sleep/delay for 100ms.....vice versa... need to do till it reach to maximum timeout value.

Please let me know if anything.

This is the old code:

#include <sys/time.h>       /* for setitimer */
#include <unistd.h>     /* for pause */
#include <signal.h>     /* for signal */

#define INTERVAL    500 //timeout in ms
static int timedout = 0;  
struct itimerval it_val;    /* for setting itimer */
char temp_reg[2];

int main(void)
{
  /* Upon SIGALRM, call DoStuff().
   * Set interval timer.  We want frequency in ms, 
   * but the setitimer call needs seconds and useconds. */
  if (signal(SIGALRM, (void (*)(int)) DoStuff) == SIG_ERR) 
  {
    perror("Unable to catch SIGALRM");
    exit(1);
  }
  it_val.it_value.tv_sec =     INTERVAL/1000;
  it_val.it_value.tv_usec =    (INTERVAL*1000) % 1000000;   
  it_val.it_interval = it_val.it_value;


  if (setitimer(ITIMER_REAL, &it_val, NULL) == -1) 
  {
    perror("error calling setitimer()");
    exit(1);
  }

    do 
    {
        temp_reg[0] = read_reg();
        //Read the register here and copy the value into char array (temp_reg

        if (timedout == 1 )
            return -1;//Timedout

    } while (temp_reg[0] != 0 );//Check the value and if not try to read the register again (poll)



}

/*
 * DoStuff
 */
void DoStuff(void) 
{
  timedout = 1;
  printf("Timer went off.\n");
}

Now I want to optimize and reduce the CPU usage and want to improve the performance.

Can any one help me on this issue ?

Thanks for your help on this.

Upvotes: 0

Views: 818

Answers (2)

4386427
4386427

Reputation: 44274

Here are some pseudo code that might help:

do 
{
    // Pseudo code
    start_time = get_current_time(); 

    do 
    {
        temp_reg[0] = read_reg();
        //Read the register here and copy the value into char array (temp_reg

        if (timedout == 1 )
            return -1;//Timedout

        // Pseudo code
        stop_time = get_current_time();
        if (stop_time - start_time > some_limit) break;

    } while (temp_reg[0] != 0 );

    if (temp_reg[0] != 0) 
    {
         usleep(some_time);
         start_time = get_current_time(); 
     }    
} while (temp_reg[0] != 0 );

To turn the pseudo code into real code, see https://stackoverflow.com/a/2150334/4386427

Upvotes: 0

Mike Nakis
Mike Nakis

Reputation: 61984

Currently I'm polling the register to get the expected value [...]

wow wow wow, hold on a moment here, there is a huge story hidden behind this sentence; what is "the register"? what is "the expected value"? What does read_reg() do? are you polling some external hardware? Well then, it all depends on how your hardware behaves.

There are two possibilities:

  1. Your hardware buffers the values that it produces. This means that the hardware will keep each value available until you read it; it will detect when you have read the value, and then it will provide the next value.

  2. Your hardware does not buffer values. This means that values are being made available in real time, for an unknown length of time each, and they are replaced by new values at a rate that only your hardware knows.

If your hardware is buffering, then you do not need to be afraid that some values might be lost, so there is no need to poll at all: just try reading the next value once and only once, and if it is not what you expect, sleep for a while. Each value will be there when you get around to reading it.

If your hardware is not buffering, then there is no strategy of polling and sleeping that will work for you. Your hardware must provide an interrupt, and you must write an interrupt-handling routine that will read every single new value as quickly as possible from the moment that it has been made available.

Upvotes: 1

Related Questions