Sleep thread issues on Xeon Phi

Sleep thread issues on Xeon Phi

I am attempting to use the nanosleep function (defined here: 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;

5 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Hi Jonathan,

Let me investigate the issue you found. Regards.

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.


I know this is a quite old post but I've fallen into the same problem. Is there any newer information about this?

All I can say is that I can reproduce the issue with mpss 3.4, 3,5 and 3.6 ; the clocksource on my MICs is set to "tsc" yet a  


or even


result in an elapsed time of ~ 15 ms (instead of 5). This happens with both a 5120 and a 7120.

It does not depend on the version of ICC used - I can also reproduce it by building a binary using gcc-for-Phi.



Leave a Comment

Please sign in to add a comment. Not a member? Join today