Negative clock cycle measurements with back-to-back rdtsc?

后端 未结 9 1240
情话喂你
情话喂你 2020-11-27 04:17

I am writing a C code for measuring the number of clock cycles needed to acquire a semaphore. I am using rdtsc, and before doing the measurement on the semaphore, I call rdt

9条回答
  •  半阙折子戏
    2020-11-27 04:28

    If you code starts off on one processor then swaps to another, the timestamp difference may be negative due to processors sleeping etc.

    Try setting the processor affinity before you start measuring.

    I can't see if you are running under Windows or Linux from the question, so I'll answer for both.

    Windows:

    DWORD affinityMask = 0x00000001L;
    SetProcessAffinityMask(GetCurrentProcessId(), affinityMask);
    

    Linux:

    cpu_set_t cpuset;
    CPU_ZERO(&cpuset);
    CPU_SET(0, &cpuset);
    sched_setaffinity (getpid(), sizeof(cpuset), &cpuset)
    

提交回复
热议问题