I've poked around a little, but I only get the definition of clock speed if I google it. How does the software determine the current clock speed of the CPU? Is there a specific instruction for it?

EDIT it seems that this question already has an answer, I just didn't look deep enough.

1 Answers

Brendan On Best Solutions

As far as I know; for all existing CPUs, a CPU's instantaneous clock speed can't be measured.

The closest you can get (depending on which CPU it is) is to find the average clock speed over a very short period of time. For example, for modern 80x86 CPUs you could set up a performance monitoring counter to measure "unhalted (CPU) clock cycles" and then use the TSC (which runs at a "nominal" fixed frequency and has nothing to do with CPU cycles) to calculate "CPU cycles / time". Of course this involves an "accuracy vs. duration" compromise (a longer duration will be more accurate, and a very short duration will be less accurate).

The other problem is that the information will be obsolete before you can use it. For example, the CPU's speed can change immediately before determining the CPU speed, and can change again immediately after determining the CPU speed; and if you could know the exact CPU speed at one point in time (while measuring) the information would be worthless because it doesn't imply anything about any other point in time (while not measuring).

To guard against this what you really want is log of CPU speed changes (e.g. when CPU speed changes, add an entry to the log containing a timestamp and the new speed) so that you can use the log to determine what the CPU speed was at any point in time in the past. For some cases this may be possible (e.g. if software changes CPU speed itself, or if the local APIC is configured to generate a "thermal monitor IRQ", then software can add relevant entries to the log). Sadly, for other cases ("turbo-boost") it's not possible (software isn't notified of speed changes).

The other problem is that you're assuming a CPU has one clock speed. While this was true in the past, it's not really true anymore, and it's reasonable to expect that it will get "even less true" in the future. For example; modern 80x86 CPUs typically have one clock for general purpose instructions and a different clock for SIMD (e.g. AVX), where (when not using AVX instructions) the CPU can/will decrease the "AVX clock" to reduce power consumption (and then it'll take several thousand cycles to get the "AVX clock" back to full speed when you do start using AVX instructions). In theory, a CPU can be split into an arbitrary number of domains each with its own independent clock.

Of course there are also asynchronous CPU designs (see https://en.wikipedia.org/wiki/Asynchronous_circuit#Asynchronous_CPU ) that simply don't have a clock to start with.