Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Mhz to current, min and max. #1517

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion cpu/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,18 @@ type InfoStat struct {
CoreID string `json:"coreId"`
Cores int32 `json:"cores"`
ModelName string `json:"modelName"`
Mhz float64 `json:"mhz"`
Mhz Mhz `json:"mhz"`
CacheSize int32 `json:"cacheSize"`
Flags []string `json:"flags"`
Microcode string `json:"microcode"`
}

type Mhz struct {
Current float64 `json:"current"` //from cpuinfo or cpufreq/cpuinfo_cur_freq
Max float64 `json:"max"` //from cpufreq/cpuinfo_max_freq
Min float64 `json:"min"` //from cpufreq/cpuinfo_min_freq
}

type lastPercent struct {
sync.Mutex
lastCPUTimes []TimesStat
Expand Down
8 changes: 6 additions & 2 deletions cpu/cpu_aix_cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
return nil, err
}
info := InfoStat{
CPU: 0,
Mhz: float64(c.ProcessorHz / 1000000),
CPU: 0,
Mhz: Mhz{
current: float64(c.ProcessorHz / 1000000),
max: 0,
min: 0,
},
Cores: int32(c.NCpusCfg),
}
result := []InfoStat{info}
Expand Down
10 changes: 6 additions & 4 deletions cpu/cpu_aix_nocgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,16 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
if t, err := strconv.ParseFloat(p[3], 64); err == nil {
switch strings.ToUpper(p[4]) {
case "MHZ":
ret.Mhz = t
ret.Mhz.Current = t
case "GHZ":
ret.Mhz = t * 1000.0
ret.Mhz.Current = t * 1000.0
case "KHZ":
ret.Mhz = t / 1000.0
ret.Mhz.Current = t / 1000.0
default:
ret.Mhz = t
ret.Mhz.Current = t
}
ret.Mhz.Min = 0
ret.Mhz.Max = 0
}
}
break
Expand Down
8 changes: 6 additions & 2 deletions cpu/cpu_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,17 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
c.VendorID, _ = unix.Sysctl("machdep.cpu.vendor")

if m1cpu.IsAppleSilicon() {
c.Mhz = float64(m1cpu.PCoreHz() / 1_000_000)
c.Mhz.Current = float64(m1cpu.PCoreHz() / 1_000_000)
c.Mhz.Max = 0
c.Mhz.Min = 0
} else {
// Use the rated frequency of the CPU. This is a static value and does not
// account for low power or Turbo Boost modes.
cpuFrequency, err := unix.SysctlUint64("hw.cpufrequency")
if err == nil {
c.Mhz = float64(cpuFrequency) / 1000000.0
c.Mhz.Current = float64(cpuFrequency) / 1000000.0
c.Mhz.Max = 0
c.Mhz.Min = 0
}
}

Expand Down
2 changes: 1 addition & 1 deletion cpu/cpu_darwin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func Test_CpuInfo_AppleSilicon(t *testing.T) {
if vv.ModelName == "" {
t.Errorf("could not get CPU info: %v", vv)
}
if vv.Mhz <= 0 {
if vv.Mhz.Current <= 0 {
t.Errorf("could not get frequency of: %s", vv.ModelName)
}
if vv.Mhz > 6000 {
Expand Down
5 changes: 3 additions & 2 deletions cpu/cpu_dragonfly.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,9 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
if u32, err = unix.SysctlUint32("hw.clockrate"); err != nil {
return nil, err
}
c.Mhz = float64(u32)

c.Mhz.Current = float64(u32)
c.Mhz.Min = 0
c.Mhz.Max = 0
var num int
var buf string
if buf, err = unix.Sysctl("hw.cpu_topology.tree"); err != nil {
Expand Down
4 changes: 3 additions & 1 deletion cpu/cpu_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
if u32, err = unix.SysctlUint32("hw.clockrate"); err != nil {
return nil, err
}
c.Mhz = float64(u32)
c.Mhz.Current = float64(u32)
c.Mhz.Min = 0
c.Mhz.Max = 0

if u32, err = unix.SysctlUint32("hw.ncpu"); err != nil {
return nil, err
Expand Down
47 changes: 29 additions & 18 deletions cpu/cpu_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ func sysCPUPath(ctx context.Context, cpu int32, relPath string) string {
func finishCPUInfo(ctx context.Context, c *InfoStat) {
var lines []string
var err error
var value float64

if len(c.CoreID) == 0 {
lines, err = common.ReadLines(sysCPUPath(ctx, c.CPU, "topology/core_id"))
Expand All @@ -142,23 +141,35 @@ func finishCPUInfo(ctx context.Context, c *InfoStat) {
}
}

// override the value of c.Mhz with cpufreq/cpuinfo_max_freq regardless
// of the value from /proc/cpuinfo because we want to report the maximum
// clock-speed of the CPU for c.Mhz, matching the behaviour of Windows
lines, err = common.ReadLines(sysCPUPath(ctx, c.CPU, "cpufreq/cpuinfo_max_freq"))
// if we encounter errors below such as there are no cpuinfo_max_freq file,
// we just ignore. so let Mhz is 0.
if err != nil || len(lines) == 0 {
return
}
value, err = strconv.ParseFloat(lines[0], 64)
if err != nil {
return
}
c.Mhz = value / 1000.0 // value is in kHz
if c.Mhz > 9999 {
c.Mhz = c.Mhz / 1000.0 // value in Hz
c.Mhz.Current = fillMhz(ctx, "cur", c)
c.Mhz.Min = fillMhz(ctx, "min", c)
c.Mhz.Max = fillMhz(ctx, "max", c)

}

func fillMhz(ctx context.Context, value string, c *InfoStat) float64 {
var lines []string
var err error
var line float64
var mhz float64 = 0

if value == "min" || value == "max" || value == "cur" {
lines, err = common.ReadLines(sysCPUPath(ctx, c.CPU, fmt.Sprintf("cpufreq/cpuinfo_%s_freq", value)))
// if we encounter errors below such as there are no cpuinfo_max_freq file,
// we just ignore. so let Mhz is 0.
if err != nil || len(lines) == 0 {
return mhz
}
line, err = strconv.ParseFloat(lines[0], 64)
if err != nil {
return mhz
}
mhz = line / 1000.0 // value is in kHz
if mhz > 9999 {
mhz = mhz / 1000.0 // value in Hz
}
}
return mhz
}

// CPUInfo on linux will return 1 item per physical thread.
Expand Down Expand Up @@ -279,7 +290,7 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
case "cpu MHz", "clock", "cpu MHz dynamic":
// treat this as the fallback value, thus we ignore error
if t, err := strconv.ParseFloat(strings.Replace(value, "MHz", "", 1), 64); err == nil {
c.Mhz = t
c.Mhz.Current = t
}
case "cache size":
t, err := strconv.ParseInt(strings.Replace(value, " KB", "", 1), 10, 64)
Expand Down
4 changes: 3 additions & 1 deletion cpu/cpu_openbsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
if err != nil {
return nil, err
}
c.Mhz = float64(mhz)
c.Mhz.Current = float64(mhz)
c.Mhz.Min = 0
c.Mhz.Max = 0

ncpu, err := unix.SysctlUint32("hw.ncpuonline")
if err != nil {
Expand Down
12 changes: 10 additions & 2 deletions cpu/cpu_solaris.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,11 @@ func parseProcessorInfo(cmdOutput string) ([]InfoStat, error) {
Family: physicalCPU[psrFamilyOffset],
Model: physicalCPU[psrModelOffset],
Stepping: step,
Mhz: clock,
Mhz: Mhz{
current: clock,
max: 0,
min: 0,
},
})
infoStatCount++
}
Expand All @@ -252,7 +256,11 @@ func parseProcessorInfo(cmdOutput string) ([]InfoStat, error) {
Family: physicalCPU[psrFamilyOffset],
Model: physicalCPU[psrModelOffset],
Stepping: step,
Mhz: clock,
Mhz: Mhz{
current: clock,
max: 0,
min: 0,
},
})
infoStatCount++
}
Expand Down
Loading