Sleep thread issues on Xeon Phi

Sleep thread issues on Xeon Phi

Jonathan J.'s picture

I am attempting to use the nanosleep function (defined here: http://linux.die.net/man/2/nanosleep) on the Xeon Phi. It is very similar to the standard (and by standard I just mean widely-used, I know there is no true standard) sleep function, but it is higher-resolution, a requirement for our software.

I know that no sleep function can be perfect, except maybe on embedded software, so I came in expecting some margin of error. However, I found that asking it to sleep for 5 ms (or 5000000 nanoseconds) resulting in it sleeping for approx 20 ms on the Xeon Phi. Did I discover a hardware bug, or is this a known issue? Is there a known better way to "sleep" a thread on a Xeon Phi? 

Code for reproducing below:

#include <stdint.h>
#include <time.h>
#include <iostream>
inline int64_t picosecondTime() {
    timespec systemtime;
    clock_gettime(CLOCK_REALTIME, &systemtime);
    return (int64_t) ((systemtime.tv_sec * 1000000000000UL) + (systemtime.tv_nsec * 1000UL));
}
int main(int argc, char *argv[]) {
    struct timespec tim;
    tim.tv_sec = 0;
    //time is picosecond granularity in software
    int64_t picosToSleep = 5000000000;
    //convert to nanoseconds, lowest granularity of sleep
    tim.tv_nsec = picosToSleep / 1000;
    int64_t currentTime = picosecondTime();
    int err = nanosleep(&tim, NULL);
    if (err == -1) {
        std::cout << "sleep error, disregard this test" << std::endl;
    }
    int64_t awokeTime = picosecondTime();
    int64_t shouldAwakeTime = (currentTime + picosToSleep);
    std::cout << "Went to sleep at: " << currentTime << std::endl;
    std::cout << "Should awake at: " << shouldAwakeTime << std::endl;
    std::cout << "Actually awoke at: " << awokeTime << std::endl;
    std::cout << "Is difference of: " << (awokeTime - shouldAwakeTime) << std::endl;
    return 0;
}

3 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.
loc-nguyen (Intel)'s picture

Hi Jonathan,

Let me investigate the issue you found. Regards.

loc-nguyen (Intel)'s picture

Hi Jonathan,

I confirmed that using nanosleep() and usleep() for 5 msec on Intel(R) Xeon Phi(TM), it is not accurate as it is in Xeon (the difference is ~5 msec on Xeon Phi and ~0.1 msec on Xeon). I will file a problem tracking for this. 

Thank you and regards.

Login to leave a comment.