Skip to content

Commit

Permalink
Improvements by PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Boev committed Oct 26, 2019
1 parent c09bd72 commit 1aa3fff
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 166 deletions.
30 changes: 11 additions & 19 deletions plugins/inputs/cpufreq/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,21 @@ The CPUFreq plugin collects the current CPU's frequency. This plugin work only w
## Path for sysfs filesystem.
## See https://www.kernel.org/doc/Documentation/filesystems/sysfs.txt
## Defaults:
# path_sysfs = "/sys"
## Gather CPU throttles per socker
# host_sys = "/sys"
## Gather CPU throttles per core
## Defaults:
# throttles_per_socket = false
## Gather CPU throttles per physical core
## Defaults:
# throttles_per_core = false
# gather_throttles = false
```

## Example output

```
> cpufreq,cpu=0,host=server01 cur_freq=3756293000,max_freq=3900000000,min_freq=800000000 1527789803000000000
> cpufreq,cpu=1,host=server01 cur_freq=3735119000,max_freq=3900000000,min_freq=800000000 1527789803000000000
> cpufreq,cpu=2,host=server01 cur_freq=3786381000,max_freq=3900000000,min_freq=800000000 1527789803000000000
> cpufreq,cpu=3,host=server01 cur_freq=3823190000,max_freq=3900000000,min_freq=800000000 1527789803000000000
> cpufreq,cpu=4,host=server01 cur_freq=3780804000,max_freq=3900000000,min_freq=800000000 1527789803000000000
> cpufreq,cpu=5,host=server01 cur_freq=3801758000,max_freq=3900000000,min_freq=800000000 1527789803000000000
> cpufreq,cpu=6,host=server01 cur_freq=3839194000,max_freq=3900000000,min_freq=800000000 1527789803000000000
> cpufreq,cpu=7,host=server01 cur_freq=3877989000,max_freq=3900000000,min_freq=800000000 1527789803000000000
> cpufreq_cpu_throttles,cpu=0,host=server01 count=0 1527789803000000000
> cpufreq_core_throttles,core=0,cpu=0,host=server01 count=0 1527789803000000000
> cpufreq_core_throttles,core=1,cpu=0,host=server01 count=0 1527789803000000000
> cpufreq_core_throttles,core=2,cpu=0,host=server01 count=0 1527789803000000000
> cpufreq_core_throttles,core=3,cpu=0,host=server01 count=0 1527789803000000000
> cpufreq,cpu=0,host=server01 cur_freq=3756293000i,max_freq=3900000000i,min_freq=800000000i,throttle_count=0i 1527789803000000000
> cpufreq,cpu=1,host=server01 cur_freq=3735119000i,max_freq=3900000000i,min_freq=800000000i,throttle_count=0i 1527789803000000000
> cpufreq,cpu=2,host=server01 cur_freq=3786381000i,max_freq=3900000000i,min_freq=800000000i,throttle_count=0i 1527789803000000000
> cpufreq,cpu=3,host=server01 cur_freq=3823190000i,max_freq=3900000000i,min_freq=800000000i,throttle_count=0i 1527789803000000000
> cpufreq,cpu=4,host=server01 cur_freq=3780804000i,max_freq=3900000000i,min_freq=800000000i,throttle_count=0i 1527789803000000000
> cpufreq,cpu=5,host=server01 cur_freq=3801758000i,max_freq=3900000000i,min_freq=800000000i,throttle_count=0i 1527789803000000000
> cpufreq,cpu=6,host=server01 cur_freq=3839194000i,max_freq=3900000000i,min_freq=800000000i,throttle_count=0i 1527789803000000000
> cpufreq,cpu=7,host=server01 cur_freq=3877989000i,max_freq=3900000000i,min_freq=800000000i,throttle_count=0i 1527789803000000000
```
102 changes: 19 additions & 83 deletions plugins/inputs/cpufreq/cpufreq.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,21 @@ import (
"github.com/influxdata/telegraf/plugins/inputs"
)

const defaultPathSysfs = "/sys"
const defaultHostSys = "/sys"

type CPUFreq struct {
PathSysfs string `toml:"path_sysfs"`
ThrottlesPerSocket bool `toml:"throttles_per_socket"`
ThrottlesPerCore bool `toml:"throttles_per_core"`
PathSysfs string `toml:"host_sys"`
GatherThrottles bool `toml:"gather_throttles"`
}

var sampleConfig = `
## Path for sysfs filesystem.
## See https://www.kernel.org/doc/Documentation/filesystems/sysfs.txt
## Defaults:
# path_sysfs = "/sys"
## Gather CPU throttles per socker
# host_sys = "/sys"
## Gather CPU throttles per core
## Defaults:
# throttles_per_socket = false
## Gather CPU throttles per physical core
## Defaults:
# throttles_per_core = false
# gather_throttles = false
`

func (g *CPUFreq) SampleConfig() string {
Expand All @@ -44,7 +40,11 @@ func (g *CPUFreq) Description() string {
func (g *CPUFreq) Gather(acc telegraf.Accumulator) error {

if g.PathSysfs == "" {
g.PathSysfs = defaultPathSysfs
if os.Getenv("HOST_SYS") != "" {
g.PathSysfs = os.Getenv("HOST_SYS")
} else {
g.PathSysfs = defaultHostSys
}
}

cpus, err := filepath.Glob(path.Join(g.PathSysfs, "devices/system/cpu/cpu[0-9]*"))
Expand All @@ -53,9 +53,6 @@ func (g *CPUFreq) Gather(acc telegraf.Accumulator) error {
}

var value uint64
packageThrottles := make(map[uint64]uint64)
packageCoreThrottles := make(map[uint64]map[uint64]uint64)

// cpu loop
for _, cpu := range cpus {
fileds := make(map[string]interface{})
Expand All @@ -74,90 +71,29 @@ func (g *CPUFreq) Gather(acc telegraf.Accumulator) error {
acc.AddError(err)
continue
} else {
fileds["cur_freq"] = float64(value) * 1000.0
fileds["cur_freq"] = uint64(value) * 1000
}
if value, err = readUintFromFile(filepath.Join(cpu, "cpufreq", "scaling_min_freq")); err != nil {
acc.AddError(err)
} else {
fileds["min_freq"] = float64(value) * 1000.0
fileds["min_freq"] = uint64(value) * 1000
}
if value, err = readUintFromFile(filepath.Join(cpu, "cpufreq", "scaling_max_freq")); err != nil {
acc.AddError(err)
} else {
fileds["max_freq"] = float64(value) * 1000.0
fileds["max_freq"] = uint64(value) * 1000
}

acc.AddFields("cpufreq", fileds, tags)
}

if g.ThrottlesPerSocket || g.ThrottlesPerCore {
var physicalPackageID, coreID uint64

// topology/physical_package_id
if physicalPackageID, err = readUintFromFile(filepath.Join(cpu, "topology", "physical_package_id")); err != nil {
if g.GatherThrottles {
if value, err := readUintFromFile(filepath.Join(cpu, "thermal_throttle", "core_throttle_count")); err != nil {
acc.AddError(err)
continue
}
// topology/core_id
if coreID, err = readUintFromFile(filepath.Join(cpu, "topology", "core_id")); err != nil {
acc.AddError(err)
continue
}

// core_throttles
if _, present := packageCoreThrottles[physicalPackageID]; !present {
packageCoreThrottles[physicalPackageID] = make(map[uint64]uint64)
}
if _, present := packageCoreThrottles[physicalPackageID][coreID]; !present {
// Read thermal_throttle/core_throttle_count only once
if coreThrottleCount, err := readUintFromFile(filepath.Join(cpu, "thermal_throttle", "core_throttle_count")); err == nil {
packageCoreThrottles[physicalPackageID][coreID] = coreThrottleCount
} else {
acc.AddError(err)
}
}

// cpu_package_throttles
if _, present := packageThrottles[physicalPackageID]; !present {
// Read thermal_throttle/package_throttle_count only once
if packageThrottleCount, err := readUintFromFile(filepath.Join(cpu, "thermal_throttle", "package_throttle_count")); err == nil {
packageThrottles[physicalPackageID] = packageThrottleCount
} else {
acc.AddError(err)
}
} else {
fileds["throttle_count"] = uint64(value)
}
}
}

if g.ThrottlesPerSocket {
for physicalPackageID, packageThrottleCount := range packageThrottles {
acc.AddFields(
"cpufreq_cpu_throttles",
map[string]interface{}{
"count": float64(packageThrottleCount),
},
map[string]string{
"cpu": strconv.FormatUint(physicalPackageID, 10),
},
)
}
}

if g.ThrottlesPerCore {
for physicalPackageID, coreMap := range packageCoreThrottles {
for coreID, coreThrottleCount := range coreMap {
acc.AddFields(
"cpufreq_core_throttles",
map[string]interface{}{
"count": float64(coreThrottleCount),
},
map[string]string{
"cpu": strconv.FormatUint(physicalPackageID, 10),
"core": strconv.FormatUint(coreID, 10),
},
)
}
}
acc.AddFields("cpufreq", fileds, tags)
}

return nil
Expand Down
102 changes: 39 additions & 63 deletions plugins/inputs/cpufreq/cpufreq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ func TestCPUFreq_NoThrottles(t *testing.T) {
acc.AssertContainsTaggedFields(t,
"cpufreq",
map[string]interface{}{
"cur_freq": float64(2597101000),
"max_freq": float64(3400000000),
"min_freq": float64(1200000000),
"cur_freq": uint64(2597101000),
"max_freq": uint64(3400000000),
"min_freq": uint64(1200000000),
},
map[string]string{
"cpu": "0",
Expand All @@ -32,9 +32,9 @@ func TestCPUFreq_NoThrottles(t *testing.T) {
acc.AssertContainsTaggedFields(t,
"cpufreq",
map[string]interface{}{
"cur_freq": float64(2597027000),
"max_freq": float64(3400000000),
"min_freq": float64(1200000000),
"cur_freq": uint64(2597027000),
"max_freq": uint64(3400000000),
"min_freq": uint64(1200000000),
},
map[string]string{
"cpu": "1",
Expand All @@ -44,9 +44,9 @@ func TestCPUFreq_NoThrottles(t *testing.T) {
acc.AssertContainsTaggedFields(t,
"cpufreq",
map[string]interface{}{
"cur_freq": float64(2597328000),
"max_freq": float64(3400000000),
"min_freq": float64(1200000000),
"cur_freq": uint64(2597328000),
"max_freq": uint64(3400000000),
"min_freq": uint64(1200000000),
},
map[string]string{
"cpu": "2",
Expand All @@ -56,100 +56,76 @@ func TestCPUFreq_NoThrottles(t *testing.T) {
acc.AssertContainsTaggedFields(t,
"cpufreq",
map[string]interface{}{
"cur_freq": float64(2597176000),
"max_freq": float64(3400000000),
"min_freq": float64(1200000000),
"cur_freq": uint64(2597176000),
"max_freq": uint64(3400000000),
"min_freq": uint64(1200000000),
},
map[string]string{
"cpu": "3",
},
)
}

func TestCPUFreq_SocketThrottles(t *testing.T) {
func TestCPUFreq_WithThrottles(t *testing.T) {
var acc testutil.Accumulator
var cpufreq = &CPUFreq{
PathSysfs: "testdata",
ThrottlesPerSocket: true,
PathSysfs: "testdata",
GatherThrottles: true,
}

err := acc.GatherError(cpufreq.Gather)
require.NoError(t, err)

// CPU 0
// CPU 0 Core 0
acc.AssertContainsTaggedFields(t,
"cpufreq_cpu_throttles",
"cpufreq",
map[string]interface{}{
"count": float64(0),
"cur_freq": uint64(2597101000),
"max_freq": uint64(3400000000),
"min_freq": uint64(1200000000),
"throttle_count": uint64(0),
},
map[string]string{
"cpu": "0",
},
)
// CPU 1
// CPU 1 Core 0
acc.AssertContainsTaggedFields(t,
"cpufreq_cpu_throttles",
"cpufreq",
map[string]interface{}{
"count": float64(0),
"cur_freq": uint64(2597027000),
"max_freq": uint64(3400000000),
"min_freq": uint64(1200000000),
"throttle_count": uint64(0),
},
map[string]string{
"cpu": "1",
},
)
}

func TestCPUFreq_CoreThrottles(t *testing.T) {
var acc testutil.Accumulator
var cpufreq = &CPUFreq{
PathSysfs: "testdata",
ThrottlesPerCore: true,
}

err := acc.GatherError(cpufreq.Gather)
require.NoError(t, err)

// CPU 0 Core 0
acc.AssertContainsTaggedFields(t,
"cpufreq_core_throttles",
map[string]interface{}{
"count": float64(0),
},
map[string]string{
"cpu": "0",
"core": "0",
},
)
// CPU 0 Core 1
acc.AssertContainsTaggedFields(t,
"cpufreq_core_throttles",
map[string]interface{}{
"count": float64(0),
},
map[string]string{
"cpu": "0",
"core": "1",
},
)
// CPU 1 Core 0
acc.AssertContainsTaggedFields(t,
"cpufreq_core_throttles",
"cpufreq",
map[string]interface{}{
"count": float64(0),
"cur_freq": uint64(2597328000),
"max_freq": uint64(3400000000),
"min_freq": uint64(1200000000),
"throttle_count": uint64(0),
},
map[string]string{
"cpu": "1",
"core": "0",
"cpu": "2",
},
)
// CPU 1 Core 1
acc.AssertContainsTaggedFields(t,
"cpufreq_core_throttles",
"cpufreq",
map[string]interface{}{
"count": float64(0),
"cur_freq": uint64(2597176000),
"max_freq": uint64(3400000000),
"min_freq": uint64(1200000000),
"throttle_count": uint64(100),
},
map[string]string{
"cpu": "1",
"core": "1",
"cpu": "3",
},
)
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0
100

0 comments on commit 1aa3fff

Please sign in to comment.