Home Swift UNIX C Assembly Go Web MCU Research Non-Tech

Get CPU Frequency on Raspbian in Go (No Assembly Required)

2025-01-23 | Go | #Words: 180 | 中文原版

This implementation does not use assembly because it is intended for use in Go, which does not support inline assembly—working with assembly in Go would be cumbersome. Additionally, performance is not a critical concern for the project, so we directly Get pre-computed values from the kernel via command-line access.

On Raspbian, CPU information is stored in the /sys/devices/system/cpu/ directory, which contains the following contents:

$ ls /sys/devices/system/cpu/
cpu0  cpu2  cpufreq  isolated    modalias  online    power    uevent
cpu1  cpu3  cpuidle  kernel_max  offline   possible  present  vulnerabilities

Retrieve Frequency for a Specific CPU Core

To get the frequency of a specific CPU core (e.g., the first core, cpu0), use this command:

$ sudo cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq 
600000

Retrieve a General CPU Frequency Reference

For an overall reference of the CPU frequency, use this method instead:

$ sudo cat /sys/devices/system/cpu/cpufreq/policy0/cpuinfo_cur_freq 
800000

The second path is a bit unusual and worth noting.

I hope these will help someone in need~