Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[Solved]running-at cpu frequency in /sys/ for any arch?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Kernel & Hardware
View previous topic :: View next topic  
Author Message
teika
Apprentice
Apprentice


Joined: 19 Feb 2011
Posts: 167
Location: YYYY-MM-DD, period. Have you ever used the Internet?

PostPosted: Sun Aug 29, 2021 7:21 am    Post subject: [Solved]running-at cpu frequency in /sys/ for any arch? Reply with quote

[Edit] See the conclusion below.

Hi. What’s the correct way to get the cpu(s) frequency from /sys/, in particular for non-x86?

I have a y2016 core-i5. For me, the frequency of each cpu core can be obtained from:
/sys/devices/system/cpu/cpufreq/policy<X>/scaling_cur_freq ,
where <X> is 0, 1,... The unit is kHz. (Wow.)

According to the kernel doc, admin-guide/pm/cpufreq, the file cpuinfo_cur_freq gives the "actual" value. OTOH scaling_cur_freq does not necessarily so, but in fact it is "more precise" for some architectures, including x86. The former does not exist when the actual frequency cannot be determined, and it is the case for mine.

There's yet another source, /proc/cpuinfo:
Code:
$ grep MHz /proc/cpuinfo
cpu MHz      : 2400.000
cpu MHz      : 600.031
cpu MHz      : 2400.000
cpu MHz      : 2400.000


The last one is not reliable for me; even if I limit the maximal frequency to 600MHz, it reports values like 2.4GHz.

How about non-x86 CPUs? (Or are there x86 cpus where the story is different?)

Motivation: x11-misc/xosview prints the value from /proc/cpuinfo. I'd like to write a patch, but not sure what's the correct way. In Gentoo, this package is limited to x86 and ppc (both 32 and 64 bits), but the upstream supports various Unixes. So an acceptable patch I think has to be general enough, (even though the part I'm relevant applies only to linux.)

If you want to write a patch yourself, this is the file (in particular after the line 134) to be modified. (Dunno what's c++, nor how to write sensible C codes. :p)

TIA. Regards.
_________________
Hack of easy Shift / Ctrl / AltGr etc; save your pinkies, type without drudgery: topic 865313

BTRFS - Biden & TRump, Fools Suck.


Last edited by teika on Sun Sep 05, 2021 1:29 am; edited 2 times in total
Back to top
View user's profile Send private message
mike155
Advocate
Advocate


Joined: 17 Sep 2010
Posts: 4438
Location: Frankfurt, Germany

PostPosted: Sun Aug 29, 2021 12:35 pm    Post subject: Reply with quote

Quote:
Code:
/sys/devices/system/cpu/cpufreq/policy<X>/scaling_cur_freq

'.../policy<X>/scaling_cur_freq' is probably not the right file. Try
Code:
/sys/devices/system/cpu/cpu<X>/cpufreq/cpuinfo_cur_freq

Hint: Look at the files 'cpupower frequency-info' reads:
Code:
# strace -f -o /tmp/strace.out cpupower -c all frequency-info
# grep open /tmp/strace.out
[...]
openat(AT_FDCWD, "/sys/devices/system/cpu/cpu6/online", O_RDONLY) = 3
openat(AT_FDCWD, "/sys/devices/system/cpu/cpu6/cpufreq/scaling_driver", O_RDONLY) = 3
openat(AT_FDCWD, "/sys/devices/system/cpu/cpu6/cpufreq/related_cpus", O_RDONLY) = 3
openat(AT_FDCWD, "/sys/devices/system/cpu/cpu6/cpufreq/affected_cpus", O_RDONLY) = 3
openat(AT_FDCWD, "/sys/devices/system/cpu/cpu6/cpufreq/cpuinfo_transition_latency", O_RDONLY) = 3
openat(AT_FDCWD, "/sys/devices/system/cpu/cpu6/cpufreq/cpuinfo_min_freq", O_RDONLY) = 3
openat(AT_FDCWD, "/sys/devices/system/cpu/cpu6/cpufreq/cpuinfo_max_freq", O_RDONLY) = 3
openat(AT_FDCWD, "/sys/devices/system/cpu/cpu6/cpufreq/scaling_available_frequencies", O_RDONLY) = 3
openat(AT_FDCWD, "/sys/devices/system/cpu/cpu6/cpufreq/scaling_available_governors", O_RDONLY) = 3
openat(AT_FDCWD, "/sys/devices/system/cpu/cpu6/cpufreq/scaling_governor", O_RDONLY) = 3
openat(AT_FDCWD, "/sys/devices/system/cpu/cpu6/cpufreq/scaling_min_freq", O_RDONLY) = 3
openat(AT_FDCWD, "/sys/devices/system/cpu/cpu6/cpufreq/scaling_max_freq", O_RDONLY) = 3
openat(AT_FDCWD, "/sys/devices/system/cpu/cpu6/cpufreq/cpuinfo_cur_freq", O_RDONLY) = 3
openat(AT_FDCWD, "/proc/cpuinfo", O_RDONLY) = 3
openat(AT_FDCWD, "/sys/bus/pci/devices", O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_DIRECTORY) = 3
openat(AT_FDCWD, "/sys/bus/pci/devices/0000:00:18.4/config", O_RDONLY) = 3
openat(AT_FDCWD, "/dev/cpu/6/msr", O_RDONLY) = 3
[...]
Back to top
View user's profile Send private message
teika
Apprentice
Apprentice


Joined: 19 Feb 2011
Posts: 167
Location: YYYY-MM-DD, period. Have you ever used the Internet?

PostPosted: Tue Aug 31, 2021 6:35 am    Post subject: Reply with quote

Thanks, but the two directories are the same ;-)

Code:
$ realpath /sys/devices/system/cpu/cpu0/cpufreq/
/sys/devices/system/cpu/cpufreq/policy0
$ realpath /sys/devices/system/cpu/cpufreq/policy0/
/sys/devices/system/cpu/cpufreq/policy0

So in fact I don't have that file:
Code:
$ find /sys -name cpuinfo_cur_freq
$ # None found.


[Moderator edit: added [code] tags to preserve output layout. -Hu]
Back to top
View user's profile Send private message
teika
Apprentice
Apprentice


Joined: 19 Feb 2011
Posts: 167
Location: YYYY-MM-DD, period. Have you ever used the Internet?

PostPosted: Sun Sep 05, 2021 1:27 am    Post subject: Reply with quote

Ok, the official solution, seemingly architecture-independent, is here:

1. Look at /sys/devices/system/cpu/cpu<N>/cpufreq/
2. If cpuinfo_cur_freq is there, use it.
3. If not, use scaling_cur_freq.

Proof:
The package sys-power/cpupower is a part of the kernel. It first tries cpuinfo_cur_freq, and it if fails, it falls back to scaling_cur_freq. See cpupower/utils/cpufreq-info.c, the function debug_output_one[1]:
Code:
if (get_freq_hardware(cpu, 1) < 0)
  get_freq_kernel(cpu, 1);

These two functions are wrappers, and their core functions can be found in cpupower/lib/cpufreq.c. Search therein for the words "get_freq_kernel" and "get_freq_hardware".

[1] Although the name "debug_output_one" sounds related to debug, but this is the default mode of $ cpupower frequency-info.

Thanks for reading. Regards.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Kernel & Hardware All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum