Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Clocks from std::chrono be different on different cores

I like to compare time_points from std::chrono::high_resolution_clock that were measured in threads running on different cores in a processor. Can there be significant differences, such as lags or from faster/slower clocks? What does the standard specify?

like image 689
max Avatar asked Jun 05 '17 13:06

max


2 Answers

The std clocks don't have a processor affinity, in fact, most threads don't either. Two clock measurements from the same thread could well have been made on different cores.

On the lead and lag associated with the measurements - this would be no different from the execution of any other instruction; the usual pipelines etc. may have an impact, but I would not consider it detrimental. If it could be detrimental, the you may need to consider other tools for the job, such as some specialized platform timing tools or CPU specific instructions of some sort.

like image 62
Niall Avatar answered Nov 02 '22 02:11

Niall


I am going to answer this specifically based on your comment that you are running this on a 'Modern' Intel CPU.

I am also going to highlight that this can be a highly contentious discussion since this is very much dependent on the CPU and motherboard. 'Modern' can be a very fluid concept :)

Generally Speaking

The 'Modern' Intel CPU's have incorporated a few mechanisms to ensure uniform ticks across cores. However, it also matters what OS you are running on. For example, some Linux kernels will use the Time Stamp Counter as its source due to less overhead and fall back on the (modern) High Precision Event Timer that attempts to keep the clock tick uniform across cores.

Also, bear in mind that Intel CPUs have numerous power consumption utilities that can affect the CPU Clock frequency.

Best Case Assumption

I would check for the constant_tsc flag as part of the /proc/cpuinfo on Linux as it can then be assumed that the TSC runs in fixed frequency across cores. But...remember the variations above.

There are machine state events like power saving and machine hibernate that can cause the CPU clock frequency to change. In this event, a recalibration of the TSC is required and some documentation suggests this is only at reboot.

Recommendation:

Target a specific chipset and OS and study the specific documentation. Most OS families will have detailed information on the specifics of its clock source.

Depending on your time precision requirements you might want to switch to use

clock_gettime(CLOCK_MONOTONIC, ...); 
like image 3
IsakBosman Avatar answered Nov 02 '22 02:11

IsakBosman