CPU turbo freq

How to get CPU turbo frequency?

I want to write small statusbar widget in C which will show current CPU frequency (including turbo).
For example I can (in shell) run sysctl -n dev.cpu.0.freq and that return maximal base frequency (or in Intel case: max base frequency + 1 MHz).
To get turbo freq pmcstat command can be used
Code:
pmcstat -s unhalted-core-cycles -w 1 2>&1 | sed -E 's/ {15}//g'
which shows turbo frequency.
But how to do it in C? How to get only current frequency of fastest running CPU?
 
To get turbo freq pmcstat command can be used
Uhm, "turbo"? A blast from the past ?

1617310380745.png

which shows turbo frequency.
Seriously, these counters are for measuring performance of applications etc…
 
  • Thanks
Reactions: a6h
How to get CPU turbo frequency?
On the shell (max. enlarged horizontally) I use pcm.x from sysutils/intel-pcm. It reports the factor to multiply the nominal CPU freq to get the real freq.
To get turbo freq pmcstat command can be used pmcstat -s unhalted-core-cycles -w 1 2>&1 | sed -E 's/ {15}//g' which shows turbo frequency.
kldload hwpmc
pmcstat -s unhalted-core-cycles -w 1 | sed -E 's/ {15}//g'
Booom! :oops:
Code:
Uptime: 2d6h48m39s
Dumping 2409 out of 12118 MB:..1%..11%..22%..
But how to do it in C? How to get only current frequency of fastest running CPU?
make -C /usr/ports/sysutils/intel-pcm fetch extract patch then RTSL.
 
For example I can (in shell) run sysctl -n dev.cpu.0.freq and that return maximal base frequency (or in Intel case: max base frequency + 1 MHz).
If you're running powerd, it returns the current base frequency which may not be the maximal base frequency.
 
Uhm, "turbo"? A blast from the past ?
Ah the classic turbo button. If I recall that actually slowed down machines to emulate a much older generation of i386. That way, janky DOS software written in a naive way wouldn't run too fast to be usable.
 
...that actually slowed down machines to emulate a much older generation of i386. That way, janky DOS software written in a naive way wouldn't run too fast to be usable.
Actually this was much more needed when the first 8088 PC clones >4.77MHz arrived. Usually for games.
 
You could read the cpu related sysctls with sysctlbyname(3) or use cpufreq(4). Or run (is this lame?) the shell commands via popen(3).
I'm not 100% sure but I think all cores in a package run at the same frequency, so reading dev.cpu.0.freq should suffice?
dev.cpu.0.freq shows maximum base frequency, not maximum turbo frequency, eg:
Code:
% grep CPU: /var/run/dmesg.boot
CPU: Intel(R) Core(TM) i7-3520M CPU @ 2.90GHz (2893.48-MHz K8-class CPU)

% cpuset -l2 yes > /dev/null
% sysctl dev.cpu.2.freq
dev.cpu.2.freq: 2901
That shows maximum base frequency (2.9 GHz) + 1 MHz (indicating that CPU is in turbo mode). That CPU has maximum turbo freq up to 3.6 GHz and I want somehow to show "3600" instead of "2901".
kldload hwpmc
pmcstat -s unhalted-core-cycles -w 1 | sed -E 's/ {15}//g'
Booom! :oops:
Code:
Uptime: 2d6h48m39s
Dumping 2409 out of 12118 MB:..1%..11%..22%..
Haha, you still have that problem :rolleyes: What kernel dump says, what is the reason for panic?

On the shell (max. enlarged horizontally) I use pcm.x from sysutils/intel-pcm. It reports the factor to multiply the nominal CPU freq to get the real freq.
I am searching method which will work on Intel and AMD CPUs.
Seriously, these counters are for measuring performance of applications etc…
Code:
% service powerdxx stop
% sysctl dev.cpu.0.freq=1200
dev.cpu.0.freq: 2900 -> 1200

% pmcstat -s unhalted-core-cycles -w 1 2>&1 | sed -E 's/ {15}//g'
# s/00/unhalted-core-cycles s/01/unhalted-core-cycles s/02/unhalted-core-cycles s/03/unhalted-core-cycles 
   625224528  622224947 1093823474 1092840580 
  1065736076 1065754245 1188315957 1196444315 
   907394272  907401561 1197269104 1189144561 
   768661168  768646950 1197197753 1197251174 
  1081825135 1081976193 1197622792 1211841331 
   669283827  669127379 1196965021 1182762013 
   847763448  847791620 1197514721 1197590110
Last two columns shows values close to 1200 MHz which should be CPU frequency...

So, I am looking to show exact current CPU turbo/boost frequency (not the base frequency) on Intel and AMD CPU.
Code:
% pmcstat -s unhalted-core-cycles -w 1 2>&1 | sed -E 's/ {15}//g'
# s/00/unhalted-core-cycles s/01/unhalted-core-cycles s/02/unhalted-core-cycles s/03/unhalted-core-cycles  
  3392347466 3392239854 3392362303 3391561413 
  3392362034 3392464619 3392342770 3392302304 
  3392400350 3392302315 3392297352 3392276603 
  3392500313 3392499109 3392504132 3393433675 
  3398005049 3398767664 3401102916 3400195029
Last line shows 3.4 GHz for all 4 cores. I want somehow to extract that information in C program or shell script (which should print something like "3401102916" or "3.4 GHz"). How I can do that?
 
I want somehow to extract that information in C program or shell script (which should print something like "3401102916" or "3.4 GHz"). How I can do that?
Why don't you just directly read the CPUs' MSRs?
Then you get immediate values, not averaged ones.

All you need is described in the Intel software developer manuals.
To see how to read the CPU info, you can look into this cpupdate module that reads the CPU stats (among other things).
Most of the MSR stuff should be identical with AMD and Intel CPUs, except of microcode uploading.
Just read the clock/multiplier settings instead of the microcode data.
 
Back
Top