From 3119ca7a02d51a9ff239b1249a87623ebea666a9 Mon Sep 17 00:00:00 2001 From: Pavel Boev Date: Sun, 27 May 2018 23:21:16 +0300 Subject: [PATCH 1/4] add cpufreq plugin --- plugins/inputs/all/all.go | 1 + plugins/inputs/cpufreq/README.md | 3 + plugins/inputs/cpufreq/cpufreq.go | 165 ++++++++++++++++++++++++++++++ 3 files changed, 169 insertions(+) create mode 100644 plugins/inputs/cpufreq/README.md create mode 100644 plugins/inputs/cpufreq/cpufreq.go diff --git a/plugins/inputs/all/all.go b/plugins/inputs/all/all.go index 6934266429b2d..ea4c8720b4d5a 100644 --- a/plugins/inputs/all/all.go +++ b/plugins/inputs/all/all.go @@ -27,6 +27,7 @@ import ( _ "github.com/influxdata/telegraf/plugins/inputs/couchbase" _ "github.com/influxdata/telegraf/plugins/inputs/couchdb" _ "github.com/influxdata/telegraf/plugins/inputs/cpu" + _ "github.com/influxdata/telegraf/plugins/inputs/cpufreq" _ "github.com/influxdata/telegraf/plugins/inputs/dcos" _ "github.com/influxdata/telegraf/plugins/inputs/disk" _ "github.com/influxdata/telegraf/plugins/inputs/diskio" diff --git a/plugins/inputs/cpufreq/README.md b/plugins/inputs/cpufreq/README.md new file mode 100644 index 0000000000000..72a28104ad06f --- /dev/null +++ b/plugins/inputs/cpufreq/README.md @@ -0,0 +1,3 @@ +# CPUFreq Input Plugin For Telegraf Agent + +The CPUFreq plugin collects the current CPU's frequency \ No newline at end of file diff --git a/plugins/inputs/cpufreq/cpufreq.go b/plugins/inputs/cpufreq/cpufreq.go new file mode 100644 index 0000000000000..d806a8d6424a9 --- /dev/null +++ b/plugins/inputs/cpufreq/cpufreq.go @@ -0,0 +1,165 @@ +package cpufreq + +import ( + "io/ioutil" + "os" + "path" + "path/filepath" + "strconv" + "strings" + + "github.com/influxdata/telegraf" + "github.com/influxdata/telegraf/plugins/inputs" +) + +const defaultPathSysfs = "/sys" + +type CPUFreq struct { + PathSysfs string `toml:"path_sysfs"` +} + +var sampleConfig = ` + ## PathSysfs + # path_sysfs = "/sys" +` + +func (g *CPUFreq) SampleConfig() string { + return sampleConfig +} + +func (g *CPUFreq) Description() string { + return "Read specific statistics per cgroup" +} + +func (g *CPUFreq) Gather(acc telegraf.Accumulator) error { + + if g.PathSysfs == "" { + g.PathSysfs = defaultPathSysfs + } + + cpus, err := filepath.Glob(path.Join(g.PathSysfs, "devices/system/cpu/cpu[0-9]*")) + if err != nil { + acc.AddError(err) + } + + 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{}) + tags := make(map[string]string) + + _, cpuName := filepath.Split(cpu) + cpuNum := strings.TrimPrefix(cpuName, "cpu") + + tags["cpu"] = cpuNum + + // sysfs cpufreq values are kHz, thus multiply by 1000 to export base units (hz). + if _, err := os.Stat(filepath.Join(cpu, "cpufreq")); os.IsNotExist(err) { + acc.AddError(err) + } else { + if value, err = readUintFromFile(filepath.Join(cpu, "cpufreq", "scaling_cur_freq")); err != nil { + acc.AddError(err) + continue + } else { + fileds["cur_freq"] = float64(value) * 1000.0 + } + if value, err = readUintFromFile(filepath.Join(cpu, "cpufreq", "scaling_min_freq")); err != nil { + acc.AddError(err) + } else { + fileds["min_freq"] = float64(value) * 1000.0 + } + if value, err = readUintFromFile(filepath.Join(cpu, "cpufreq", "scaling_max_freq")); err != nil { + acc.AddError(err) + } else { + fileds["max_freq"] = float64(value) * 1000.0 + } + + acc.AddFields("cpufreq", fileds, tags) + } + + var err error + var physicalPackageID, coreID uint64 + + // topology/physical_package_id + if physicalPackageID, err = readUintFromFile(filepath.Join(cpu, "topology", "physical_package_id")); 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) + } + } + } + + for physicalPackageID, packageThrottleCount := range packageThrottles { + acc.AddFields( + "cpufreq_cpu_throttles", + map[string]interface{}{ + "count": float64(packageThrottleCount), + }, + map[string]string{ + "cpu": strconv.FormatUint(physicalPackageID, 10), + }, + ) + } + + 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), + }, + ) + } + } + + return nil +} + +func init() { + inputs.Add("cpufreq", func() telegraf.Input { return &CPUFreq{} }) +} + +func readUintFromFile(path string) (uint64, error) { + data, err := ioutil.ReadFile(path) + if err != nil { + return 0, err + } + value, err := strconv.ParseUint(strings.TrimSpace(string(data)), 10, 64) + if err != nil { + return 0, err + } + return value, nil +} From c3f4a6099b6fe9192d9fb844d537a222cb9c9236 Mon Sep 17 00:00:00 2001 From: Pavel Boev Date: Thu, 31 May 2018 07:46:21 +0300 Subject: [PATCH 2/4] add base tests --- plugins/inputs/cpufreq/cpufreq.go | 109 ++++++------ plugins/inputs/cpufreq/cpufreq_test.go | 155 ++++++++++++++++++ .../system/cpu/cpu0/cpufreq/scaling_cur_freq | 1 + .../system/cpu/cpu0/cpufreq/scaling_max_freq | 1 + .../system/cpu/cpu0/cpufreq/scaling_min_freq | 1 + .../cpu0/thermal_throttle/core_throttle_count | 1 + .../thermal_throttle/package_throttle_count | 1 + .../devices/system/cpu/cpu0/topology/core_id | 1 + .../cpu/cpu0/topology/physical_package_id | 1 + .../system/cpu/cpu1/cpufreq/scaling_cur_freq | 1 + .../system/cpu/cpu1/cpufreq/scaling_max_freq | 1 + .../system/cpu/cpu1/cpufreq/scaling_min_freq | 1 + .../cpu1/thermal_throttle/core_throttle_count | 1 + .../thermal_throttle/package_throttle_count | 1 + .../devices/system/cpu/cpu1/topology/core_id | 1 + .../cpu/cpu1/topology/physical_package_id | 1 + .../system/cpu/cpu2/cpufreq/scaling_cur_freq | 1 + .../system/cpu/cpu2/cpufreq/scaling_max_freq | 1 + .../system/cpu/cpu2/cpufreq/scaling_min_freq | 1 + .../cpu2/thermal_throttle/core_throttle_count | 1 + .../thermal_throttle/package_throttle_count | 1 + .../devices/system/cpu/cpu2/topology/core_id | 1 + .../cpu/cpu2/topology/physical_package_id | 1 + .../system/cpu/cpu3/cpufreq/scaling_cur_freq | 1 + .../system/cpu/cpu3/cpufreq/scaling_max_freq | 1 + .../system/cpu/cpu3/cpufreq/scaling_min_freq | 1 + .../cpu3/thermal_throttle/core_throttle_count | 1 + .../thermal_throttle/package_throttle_count | 1 + .../devices/system/cpu/cpu3/topology/core_id | 1 + .../cpu/cpu3/topology/physical_package_id | 1 + 30 files changed, 243 insertions(+), 49 deletions(-) create mode 100644 plugins/inputs/cpufreq/cpufreq_test.go create mode 100644 plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq create mode 100644 plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu0/cpufreq/scaling_max_freq create mode 100644 plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu0/cpufreq/scaling_min_freq create mode 100644 plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu0/thermal_throttle/core_throttle_count create mode 100644 plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu0/thermal_throttle/package_throttle_count create mode 100644 plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu0/topology/core_id create mode 100644 plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu0/topology/physical_package_id create mode 100644 plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu1/cpufreq/scaling_cur_freq create mode 100644 plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu1/cpufreq/scaling_max_freq create mode 100644 plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu1/cpufreq/scaling_min_freq create mode 100644 plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu1/thermal_throttle/core_throttle_count create mode 100644 plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu1/thermal_throttle/package_throttle_count create mode 100644 plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu1/topology/core_id create mode 100644 plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu1/topology/physical_package_id create mode 100644 plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu2/cpufreq/scaling_cur_freq create mode 100644 plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu2/cpufreq/scaling_max_freq create mode 100644 plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu2/cpufreq/scaling_min_freq create mode 100644 plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu2/thermal_throttle/core_throttle_count create mode 100644 plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu2/thermal_throttle/package_throttle_count create mode 100644 plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu2/topology/core_id create mode 100644 plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu2/topology/physical_package_id create mode 100644 plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu3/cpufreq/scaling_cur_freq create mode 100644 plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu3/cpufreq/scaling_max_freq create mode 100644 plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu3/cpufreq/scaling_min_freq create mode 100644 plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu3/thermal_throttle/core_throttle_count create mode 100644 plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu3/thermal_throttle/package_throttle_count create mode 100644 plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu3/topology/core_id create mode 100644 plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu3/topology/physical_package_id diff --git a/plugins/inputs/cpufreq/cpufreq.go b/plugins/inputs/cpufreq/cpufreq.go index d806a8d6424a9..4f6f73cd8a809 100644 --- a/plugins/inputs/cpufreq/cpufreq.go +++ b/plugins/inputs/cpufreq/cpufreq.go @@ -15,12 +15,18 @@ import ( const defaultPathSysfs = "/sys" type CPUFreq struct { - PathSysfs string `toml:"path_sysfs"` + PathSysfs string `toml:"path_sysfs"` + ThrottlesPerSocket bool `toml:"throttles_per_socket"` + ThrottlesPerCore bool `toml:"throttles_per_core"` } var sampleConfig = ` ## PathSysfs # path_sysfs = "/sys" + ## + # throttles_per_socket = false + ## + # throttles_per_core = false ` func (g *CPUFreq) SampleConfig() string { @@ -80,71 +86,76 @@ func (g *CPUFreq) Gather(acc telegraf.Accumulator) error { acc.AddFields("cpufreq", fileds, tags) } - var err error - var physicalPackageID, coreID uint64 + 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 { - 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 { + // topology/physical_package_id + if physicalPackageID, err = readUintFromFile(filepath.Join(cpu, "topology", "physical_package_id")); err != nil { acc.AddError(err) + continue } - } - - // 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 { + // 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) + } } - } - } - for physicalPackageID, packageThrottleCount := range packageThrottles { - acc.AddFields( - "cpufreq_cpu_throttles", - map[string]interface{}{ - "count": float64(packageThrottleCount), - }, - map[string]string{ - "cpu": strconv.FormatUint(physicalPackageID, 10), - }, - ) + // 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) + } + } + } } - for physicalPackageID, coreMap := range packageCoreThrottles { - for coreID, coreThrottleCount := range coreMap { + if g.ThrottlesPerSocket { + for physicalPackageID, packageThrottleCount := range packageThrottles { acc.AddFields( - "cpufreq_core_throttles", + "cpufreq_cpu_throttles", map[string]interface{}{ - "count": float64(coreThrottleCount), + "count": float64(packageThrottleCount), }, map[string]string{ - "cpu": strconv.FormatUint(physicalPackageID, 10), - "core": strconv.FormatUint(coreID, 10), + "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), + }, + ) + } + } + } + return nil } diff --git a/plugins/inputs/cpufreq/cpufreq_test.go b/plugins/inputs/cpufreq/cpufreq_test.go new file mode 100644 index 0000000000000..d5c2808bde716 --- /dev/null +++ b/plugins/inputs/cpufreq/cpufreq_test.go @@ -0,0 +1,155 @@ +package cpufreq + +import ( + "testing" + + "github.com/influxdata/telegraf/testutil" + "github.com/stretchr/testify/require" +) + +func TestCPUFreq_NoThrottles(t *testing.T) { + var acc testutil.Accumulator + var cpufreq = &CPUFreq{ + PathSysfs: "testdata", + } + + err := acc.GatherError(cpufreq.Gather) + require.NoError(t, err) + + // CPU 0 Core 0 + acc.AssertContainsTaggedFields(t, + "cpufreq", + map[string]interface{}{ + "cur_freq": float64(2597101000), + "max_freq": float64(3400000000), + "min_freq": float64(1200000000), + }, + map[string]string{ + "cpu": "0", + }, + ) + // CPU 1 Core 0 + acc.AssertContainsTaggedFields(t, + "cpufreq", + map[string]interface{}{ + "cur_freq": float64(2597027000), + "max_freq": float64(3400000000), + "min_freq": float64(1200000000), + }, + map[string]string{ + "cpu": "1", + }, + ) + // CPU 0 Core 1 + acc.AssertContainsTaggedFields(t, + "cpufreq", + map[string]interface{}{ + "cur_freq": float64(2597328000), + "max_freq": float64(3400000000), + "min_freq": float64(1200000000), + }, + map[string]string{ + "cpu": "2", + }, + ) + // CPU 1 Core 1 + acc.AssertContainsTaggedFields(t, + "cpufreq", + map[string]interface{}{ + "cur_freq": float64(2597176000), + "max_freq": float64(3400000000), + "min_freq": float64(1200000000), + }, + map[string]string{ + "cpu": "3", + }, + ) +} + +func TestCPUFreq_SocketThrottles(t *testing.T) { + var acc testutil.Accumulator + var cpufreq = &CPUFreq{ + PathSysfs: "testdata", + ThrottlesPerSocket: true, + } + + err := acc.GatherError(cpufreq.Gather) + require.NoError(t, err) + + // CPU 0 + acc.AssertContainsTaggedFields(t, + "cpufreq_cpu_throttles", + map[string]interface{}{ + "count": float64(0), + }, + map[string]string{ + "cpu": "0", + }, + ) + // CPU 1 + acc.AssertContainsTaggedFields(t, + "cpufreq_cpu_throttles", + map[string]interface{}{ + "count": float64(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", + map[string]interface{}{ + "count": float64(0), + }, + map[string]string{ + "cpu": "1", + "core": "0", + }, + ) + // CPU 1 Core 1 + acc.AssertContainsTaggedFields(t, + "cpufreq_core_throttles", + map[string]interface{}{ + "count": float64(0), + }, + map[string]string{ + "cpu": "1", + "core": "1", + }, + ) +} diff --git a/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq new file mode 100644 index 0000000000000..fb33186688280 --- /dev/null +++ b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq @@ -0,0 +1 @@ +2597101 \ No newline at end of file diff --git a/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu0/cpufreq/scaling_max_freq b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu0/cpufreq/scaling_max_freq new file mode 100644 index 0000000000000..d8d0e34a24bbb --- /dev/null +++ b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu0/cpufreq/scaling_max_freq @@ -0,0 +1 @@ +3400000 \ No newline at end of file diff --git a/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu0/cpufreq/scaling_min_freq b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu0/cpufreq/scaling_min_freq new file mode 100644 index 0000000000000..f4177d860dd79 --- /dev/null +++ b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu0/cpufreq/scaling_min_freq @@ -0,0 +1 @@ +1200000 \ No newline at end of file diff --git a/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu0/thermal_throttle/core_throttle_count b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu0/thermal_throttle/core_throttle_count new file mode 100644 index 0000000000000..573541ac9702d --- /dev/null +++ b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu0/thermal_throttle/core_throttle_count @@ -0,0 +1 @@ +0 diff --git a/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu0/thermal_throttle/package_throttle_count b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu0/thermal_throttle/package_throttle_count new file mode 100644 index 0000000000000..573541ac9702d --- /dev/null +++ b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu0/thermal_throttle/package_throttle_count @@ -0,0 +1 @@ +0 diff --git a/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu0/topology/core_id b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu0/topology/core_id new file mode 100644 index 0000000000000..573541ac9702d --- /dev/null +++ b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu0/topology/core_id @@ -0,0 +1 @@ +0 diff --git a/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu0/topology/physical_package_id b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu0/topology/physical_package_id new file mode 100644 index 0000000000000..573541ac9702d --- /dev/null +++ b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu0/topology/physical_package_id @@ -0,0 +1 @@ +0 diff --git a/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu1/cpufreq/scaling_cur_freq b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu1/cpufreq/scaling_cur_freq new file mode 100644 index 0000000000000..7ae989c67aeb9 --- /dev/null +++ b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu1/cpufreq/scaling_cur_freq @@ -0,0 +1 @@ +2597027 \ No newline at end of file diff --git a/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu1/cpufreq/scaling_max_freq b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu1/cpufreq/scaling_max_freq new file mode 100644 index 0000000000000..d8d0e34a24bbb --- /dev/null +++ b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu1/cpufreq/scaling_max_freq @@ -0,0 +1 @@ +3400000 \ No newline at end of file diff --git a/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu1/cpufreq/scaling_min_freq b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu1/cpufreq/scaling_min_freq new file mode 100644 index 0000000000000..f4177d860dd79 --- /dev/null +++ b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu1/cpufreq/scaling_min_freq @@ -0,0 +1 @@ +1200000 \ No newline at end of file diff --git a/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu1/thermal_throttle/core_throttle_count b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu1/thermal_throttle/core_throttle_count new file mode 100644 index 0000000000000..573541ac9702d --- /dev/null +++ b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu1/thermal_throttle/core_throttle_count @@ -0,0 +1 @@ +0 diff --git a/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu1/thermal_throttle/package_throttle_count b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu1/thermal_throttle/package_throttle_count new file mode 100644 index 0000000000000..573541ac9702d --- /dev/null +++ b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu1/thermal_throttle/package_throttle_count @@ -0,0 +1 @@ +0 diff --git a/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu1/topology/core_id b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu1/topology/core_id new file mode 100644 index 0000000000000..573541ac9702d --- /dev/null +++ b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu1/topology/core_id @@ -0,0 +1 @@ +0 diff --git a/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu1/topology/physical_package_id b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu1/topology/physical_package_id new file mode 100644 index 0000000000000..d00491fd7e5bb --- /dev/null +++ b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu1/topology/physical_package_id @@ -0,0 +1 @@ +1 diff --git a/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu2/cpufreq/scaling_cur_freq b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu2/cpufreq/scaling_cur_freq new file mode 100644 index 0000000000000..c355c2498aebd --- /dev/null +++ b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu2/cpufreq/scaling_cur_freq @@ -0,0 +1 @@ +2597328 \ No newline at end of file diff --git a/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu2/cpufreq/scaling_max_freq b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu2/cpufreq/scaling_max_freq new file mode 100644 index 0000000000000..d8d0e34a24bbb --- /dev/null +++ b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu2/cpufreq/scaling_max_freq @@ -0,0 +1 @@ +3400000 \ No newline at end of file diff --git a/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu2/cpufreq/scaling_min_freq b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu2/cpufreq/scaling_min_freq new file mode 100644 index 0000000000000..f4177d860dd79 --- /dev/null +++ b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu2/cpufreq/scaling_min_freq @@ -0,0 +1 @@ +1200000 \ No newline at end of file diff --git a/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu2/thermal_throttle/core_throttle_count b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu2/thermal_throttle/core_throttle_count new file mode 100644 index 0000000000000..573541ac9702d --- /dev/null +++ b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu2/thermal_throttle/core_throttle_count @@ -0,0 +1 @@ +0 diff --git a/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu2/thermal_throttle/package_throttle_count b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu2/thermal_throttle/package_throttle_count new file mode 100644 index 0000000000000..573541ac9702d --- /dev/null +++ b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu2/thermal_throttle/package_throttle_count @@ -0,0 +1 @@ +0 diff --git a/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu2/topology/core_id b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu2/topology/core_id new file mode 100644 index 0000000000000..d00491fd7e5bb --- /dev/null +++ b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu2/topology/core_id @@ -0,0 +1 @@ +1 diff --git a/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu2/topology/physical_package_id b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu2/topology/physical_package_id new file mode 100644 index 0000000000000..573541ac9702d --- /dev/null +++ b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu2/topology/physical_package_id @@ -0,0 +1 @@ +0 diff --git a/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu3/cpufreq/scaling_cur_freq b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu3/cpufreq/scaling_cur_freq new file mode 100644 index 0000000000000..ccd2f0dff952e --- /dev/null +++ b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu3/cpufreq/scaling_cur_freq @@ -0,0 +1 @@ +2597176 \ No newline at end of file diff --git a/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu3/cpufreq/scaling_max_freq b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu3/cpufreq/scaling_max_freq new file mode 100644 index 0000000000000..d8d0e34a24bbb --- /dev/null +++ b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu3/cpufreq/scaling_max_freq @@ -0,0 +1 @@ +3400000 \ No newline at end of file diff --git a/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu3/cpufreq/scaling_min_freq b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu3/cpufreq/scaling_min_freq new file mode 100644 index 0000000000000..f4177d860dd79 --- /dev/null +++ b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu3/cpufreq/scaling_min_freq @@ -0,0 +1 @@ +1200000 \ No newline at end of file diff --git a/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu3/thermal_throttle/core_throttle_count b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu3/thermal_throttle/core_throttle_count new file mode 100644 index 0000000000000..573541ac9702d --- /dev/null +++ b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu3/thermal_throttle/core_throttle_count @@ -0,0 +1 @@ +0 diff --git a/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu3/thermal_throttle/package_throttle_count b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu3/thermal_throttle/package_throttle_count new file mode 100644 index 0000000000000..573541ac9702d --- /dev/null +++ b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu3/thermal_throttle/package_throttle_count @@ -0,0 +1 @@ +0 diff --git a/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu3/topology/core_id b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu3/topology/core_id new file mode 100644 index 0000000000000..d00491fd7e5bb --- /dev/null +++ b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu3/topology/core_id @@ -0,0 +1 @@ +1 diff --git a/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu3/topology/physical_package_id b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu3/topology/physical_package_id new file mode 100644 index 0000000000000..d00491fd7e5bb --- /dev/null +++ b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu3/topology/physical_package_id @@ -0,0 +1 @@ +1 From cd70364ee827c447519c318fddf1bcf50fd31255 Mon Sep 17 00:00:00 2001 From: Pavel Boev Date: Thu, 31 May 2018 21:17:45 +0300 Subject: [PATCH 3/4] update Readme and sample config --- plugins/inputs/cpufreq/README.md | 36 ++++++++++++++++++++++++++++++- plugins/inputs/cpufreq/cpufreq.go | 10 ++++++--- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/plugins/inputs/cpufreq/README.md b/plugins/inputs/cpufreq/README.md index 72a28104ad06f..c944e47667c39 100644 --- a/plugins/inputs/cpufreq/README.md +++ b/plugins/inputs/cpufreq/README.md @@ -1,3 +1,37 @@ # CPUFreq Input Plugin For Telegraf Agent -The CPUFreq plugin collects the current CPU's frequency \ No newline at end of file +The CPUFreq plugin collects the current CPU's frequency. This plugin work only with linux. + +## Configuration + +```toml +[[inputs.cpufreq]] + ## Path for sysfs filesystem. + ## See https://www.kernel.org/doc/Documentation/filesystems/sysfs.txt + ## Defaults: + # path_sysfs = "/sys" + ## Gather CPU throttles per socker + ## Defaults: + # throttles_per_socket = false + ## Gather CPU throttles per physical core + ## Defaults: + # throttles_per_core = 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 +``` diff --git a/plugins/inputs/cpufreq/cpufreq.go b/plugins/inputs/cpufreq/cpufreq.go index 4f6f73cd8a809..67c3350829e72 100644 --- a/plugins/inputs/cpufreq/cpufreq.go +++ b/plugins/inputs/cpufreq/cpufreq.go @@ -21,11 +21,15 @@ type CPUFreq struct { } var sampleConfig = ` - ## PathSysfs + ## Path for sysfs filesystem. + ## See https://www.kernel.org/doc/Documentation/filesystems/sysfs.txt + ## Defaults: # path_sysfs = "/sys" - ## + ## Gather CPU throttles per socker + ## Defaults: # throttles_per_socket = false - ## + ## Gather CPU throttles per physical core + ## Defaults: # throttles_per_core = false ` From e4a523697158cac8050d694fead35e1c181a62c3 Mon Sep 17 00:00:00 2001 From: Pavel Boev Date: Sat, 26 Oct 2019 22:06:22 +0300 Subject: [PATCH 4/4] Improvements by PR comments --- plugins/inputs/cpufreq/README.md | 30 ++---- plugins/inputs/cpufreq/cpufreq.go | 102 ++++-------------- plugins/inputs/cpufreq/cpufreq_test.go | 102 +++++++----------- .../cpu3/thermal_throttle/core_throttle_count | 2 +- 4 files changed, 70 insertions(+), 166 deletions(-) diff --git a/plugins/inputs/cpufreq/README.md b/plugins/inputs/cpufreq/README.md index c944e47667c39..6f8085013bd03 100644 --- a/plugins/inputs/cpufreq/README.md +++ b/plugins/inputs/cpufreq/README.md @@ -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 ``` diff --git a/plugins/inputs/cpufreq/cpufreq.go b/plugins/inputs/cpufreq/cpufreq.go index 67c3350829e72..561149a01a092 100644 --- a/plugins/inputs/cpufreq/cpufreq.go +++ b/plugins/inputs/cpufreq/cpufreq.go @@ -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 { @@ -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]*")) @@ -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{}) @@ -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 diff --git a/plugins/inputs/cpufreq/cpufreq_test.go b/plugins/inputs/cpufreq/cpufreq_test.go index d5c2808bde716..d11234bd17ccd 100644 --- a/plugins/inputs/cpufreq/cpufreq_test.go +++ b/plugins/inputs/cpufreq/cpufreq_test.go @@ -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", @@ -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", @@ -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", @@ -56,9 +56,9 @@ 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", @@ -66,90 +66,66 @@ func TestCPUFreq_NoThrottles(t *testing.T) { ) } -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", }, ) } diff --git a/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu3/thermal_throttle/core_throttle_count b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu3/thermal_throttle/core_throttle_count index 573541ac9702d..29d6383b52c13 100644 --- a/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu3/thermal_throttle/core_throttle_count +++ b/plugins/inputs/cpufreq/testdata/devices/system/cpu/cpu3/thermal_throttle/core_throttle_count @@ -1 +1 @@ -0 +100