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

后端 未结 9 1258
情话喂你
情话喂你 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:30

    The principal point of my question was not the accuracy of the result, but the fact that I am getting negative values every now and then (first call to rdstc gives bigger value than second call). Doing more research (and reading other questions on this website), I found out that a way for getting things work when using rdtsc is to put a cpuid command just before it. This command serializes the code. This is how I am doing things now:

    static inline uint64_t get_cycles()
    {
      uint64_t t;          
    
       volatile int dont_remove __attribute__((unused));
       unsigned tmp;
         __asm volatile ("cpuid" : "=a"(tmp), "=b"(tmp), "=c"(tmp), "=d"(tmp)
           : "a" (0));
    
       dont_remove = tmp; 
    
    
    
    
      __asm volatile ("rdtsc" : "=A"(t));
      return t;
    }
    

    I am still getting a NEGATIVE difference between second call and first call of the get_cycles function. WHY? I am not 100% sure about the syntax of the cpuid assembly inline code, this is what I found looking on the internet.

提交回复
热议问题