Question

I am trying to call a sampling function periodically within a C++ thread on my linux machine. I would like to restart my function after a very short period, ideally 1 msec, but I'm finding that the power (in Watts) consumed for a 1 msec period is prohibitively high: the system runs at double the power level as when my period is 5 msec. Keeping power low is a major concern for the functionality I want.

Specifically,

void* loop_and_sample(void* arg) {
    while(1) {
      sample();
      nanosleep((struct timespec[]){{0,1000000}}, NULL);
    }
}

Takes 2x the power of:

void* loop_and_sample(void* arg) {
    while(1) {
      sample();
      nanosleep((struct timespec[]){{0,5000000}}, NULL);
    }
}

I have determined that the difference in power usage of my sampler at the 2 frequencies is negligible, and that the extra power consumption comes from the sleep call. That is, even if I comment out the sample() line in both snippets above, the second still takes half the power. Any ideas as to how I might be able to reduce the power consumed by the sleep call?

FYI, I'm running Ubuntu 3.2.0 on a 24-core Intel Xeon and a search of my /boot/config for frequency shows the following:

cat /boot/config-3.2.0-48-generic | egrep 'HZ'
CONFIG_RCU_FAST_NO_HZ=y
CONFIG_NO_HZ=y
# CONFIG_HZ_100 is not set
CONFIG_HZ_250=y
# CONFIG_HZ_300 is not set
# CONFIG_HZ_1000 is not set
CONFIG_HZ=250
CONFIG_MACHZ_WDT=m

However, running this script: http://www.advenage.com/topics/linux-timer-interrupt-frequency.php, I found that my kernel timer interrupt is at least 4016 Hz (which is 4x the frequency that I would like to sample at). Thanks for your help!

Was it helpful?

Solution

The problem is almost certainly NOT the sleep call at all, but the fact that your CPU can't power up and down that fast. The CPU almost certainly won't instantaneously use less power after you go to sleep so it needs some time to do so. When you go to sleep for a longer period of time the CPU is able to reduce power usage better.

I'm not sure there's anything you can do here, although potentially underclocking your CPU might help power usage when waking up more frequently.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top