-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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 | ||||||||||
Comment on lines
+106
to
+108
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please consider writing this
Suggested change
Abd everywhere you did the replacement |
||||||||||
|
||||||||||
Comment on lines
+106
to
109
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At some point if it's for test purpose that you are resetting it |
||||||||||
if u32, err = unix.SysctlUint32("hw.ncpu"); err != nil { | ||||||||||
return nil, err | ||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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")) | ||||||||||||||||||||||
|
@@ -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) | ||||||||||||||||||||||
Comment on lines
+144
to
+146
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would pass
Suggested change
And then I would use
Suggested change
|
||||||||||||||||||||||
|
||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
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" { | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a switch here and return 0 if the value are not the one you expect |
||||||||||||||||||||||
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. | ||||||||||||||||||||||
|
@@ -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) | ||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would use MHz
And while you are changing the type and so the output format, I would change the Mhz variable name in the struct
But here,I'm only a random Gopher.
The maintainers of this project might have another opinion