# CPU turbo freq



## JohnnySorocil (Apr 1, 2021)

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

```
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?


----------



## George (Apr 1, 2021)

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).


----------



## JAW (Apr 1, 2021)

JohnnySorocil said:


> How to get only current frequency of fastest running CPU?



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?


----------



## zirias@ (Apr 1, 2021)

JohnnySorocil said:


> To get turbo freq `pmcstat` command can be used


Uhm, "turbo"? A blast from the past 






JohnnySorocil said:


> which shows turbo frequency.


Seriously, these counters are for measuring performance of applications etc…


----------



## Mjölnir (Apr 1, 2021)

JohnnySorocil said:


> 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.


JohnnySorocil said:


> 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! 

```
Uptime: 2d6h48m39s
Dumping 2409 out of 12118 MB:..1%..11%..22%..
```



JohnnySorocil said:


> 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.


----------



## zirias@ (Apr 1, 2021)

Mjölnir said:


> `make -C /usr/ports/sysutils/intel-pcm fetch extract patch` then RTSL.


FWIW, `patch` is enough, it depends on `extract` which depends on `fetch`


----------



## trev (Apr 2, 2021)

JohnnySorocil said:


> 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.


----------



## kpedersen (Apr 2, 2021)

Zirias said:


> 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.


----------



## Snurg (Apr 2, 2021)

kpedersen said:


> ...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.


----------



## kpedersen (Apr 2, 2021)

Snurg said:


> Actually this was much more needed when the first 8088 PC clones >4.77MHz arrived. Usually for games.


Hah, yeah, i386 was far too modern. My bad!

I mostly saw this button *on* an i386 (and my first 286) to emulate much older machines.


----------



## Phishfry (Apr 2, 2021)

JAW said:


> I'm not 100% sure but I think all cores in a package run at the same frequency


For the last dozen years the frequency is dynamic per core.





						Intel Turbo Boost - Wikipedia
					






					en.wikipedia.org
				








						Intel's Core i7 870 & i5 750, Lynnfield: Harder, Better, Faster Stronger
					






					www.anandtech.com


----------



## JohnnySorocil (Apr 5, 2021)

Elazar said:


> 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).





JAW said:


> 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:

```
% 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".


Mjölnir said:


> `kldload hwpmc`
> `pmcstat -s unhalted-core-cycles -w 1 | sed -E 's/ {15}//g'`
> Booom!
> 
> ...


Haha, you still have that problem  What kernel dump says, what is the reason for panic?



Mjölnir said:


> 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.


Zirias said:


> Seriously, these counters are for measuring performance of applications etc…




```
% 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.

```
% 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?


----------



## Snurg (Apr 5, 2021)

JohnnySorocil said:


> 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.


----------



## Mjölnir (Apr 5, 2021)

JohnnySorocil said:


> Haha, you still have that problem


Which problem?  Is it s/th that I missed to fix?


JohnnySorocil said:


> What kernel dump says, what is the reason for panic?


`fgrep -i panic /var/crash/info.last`: `Panic String: page fault`
Sorry... I `sysrc dumpon_flags+=" -k /etc/ssl/public.pem"` but I don't know how to decrypt the dump...


----------



## JohnnySorocil (Apr 5, 2021)

Mjölnir said:


> Which problem?  Is it s/th that I missed to fix?


I remembered it from your post when I was duckduckgo-ing my problem


Mjölnir said:


> `fgrep -i panic /var/crash/info.last`: `Panic String: page fault`
> Sorry... I `sysrc dumpon_flags+=" -k /etc/ssl/public.pem"` but I don't know how to decrypt the dump...


Didn't know that is possible to encrypt dumps. Do you have /var/crash/core.txt.[some number]?


----------

