rdtsc accuracy across CPU cores

后端 未结 6 735
一生所求
一生所求 2020-11-28 22:53

I am sending network packets from one thread and receiving replies on a 2nd thread that runs on a different CPU core. My process measures the time between send & receiv

6条回答
  •  天涯浪人
    2020-11-28 23:43

    In fact, it seems that cores doesn´t share TSC, check this thread: http://software.intel.com/en-us/forums/topic/388964

    Summarizing, different cores does not share TSC, sometimes TSC can get out of synchronization if a core change to an specific energy state, but it depends on the kind of CPU, so you need to check the Intel documentation. It seems that most Operating Systems synchronize TSC on boot.
    I checked the differences between TSC on different cores, using an exciting-reacting algorithm, on a Linux Debian machine with core i5 processor. The exciter process (in one core) writed the TSC in a shared variable, when the reacting process detected a change in that variable it compares its value and compares it with its own TSC. This is an example output of my test program:

    TSC ping-pong test result:
    TSC cores (exciter-reactor): 0-1
    100 records, avrg: 159, range: 105-269
    Dispersion: 13
    TSC ping-pong test result:
    TSC cores (exciter-reactor): 1-0
    100 records, avrg: 167, range: 125-410
    Dispersion: 13
    

    The reaction time when the exciter CPU is 0 (159 tics on average) is almost the same than when the exciter CPU is 1 (167 tics). This indicates that they are pretty well synchronized (perhaps with a few tics of difference). On other core pairs, results were very similar.
    On the other hand, rdtscp assembly instruction return a value indicating the CPU in which the TSC was read. It is not your case but it can be useful when you want to measure time in a simple code segment and you want to ensure that the process was not moved of CPU in the middle of the code.

提交回复
热议问题