updateCPUfreq reads /sys/devices/system/cpu/cpu* and expose cpu frequency statistics.
(ch chan<- prometheus.Metric)
| 86 | |
| 87 | // updateCPUfreq reads /sys/devices/system/cpu/cpu* and expose cpu frequency statistics. |
| 88 | func (c *cpuCollector) updateCPUfreq(ch chan<- prometheus.Metric) error { |
| 89 | cpus, err := filepath.Glob(sysFilePath("devices/system/cpu/cpu[0-9]*")) |
| 90 | if err != nil { |
| 91 | return err |
| 92 | } |
| 93 | |
| 94 | var value uint64 |
| 95 | packageThrottles := make(map[uint64]uint64) |
| 96 | packageCoreThrottles := make(map[uint64]map[uint64]uint64) |
| 97 | |
| 98 | // cpu loop |
| 99 | for _, cpu := range cpus { |
| 100 | _, cpuName := filepath.Split(cpu) |
| 101 | cpuNum := strings.TrimPrefix(cpuName, "cpu") |
| 102 | |
| 103 | if _, err := os.Stat(filepath.Join(cpu, "cpufreq")); os.IsNotExist(err) { |
| 104 | log.Debugf("CPU %v is missing cpufreq", cpu) |
| 105 | } else { |
| 106 | // sysfs cpufreq values are kHz, thus multiply by 1000 to export base units (hz). |
| 107 | // See https://www.kernel.org/doc/Documentation/cpu-freq/user-guide.txt |
| 108 | if value, err = readUintFromFile(filepath.Join(cpu, "cpufreq", "scaling_cur_freq")); err != nil { |
| 109 | return err |
| 110 | } |
| 111 | ch <- prometheus.MustNewConstMetric(c.cpuFreq, prometheus.GaugeValue, float64(value)*1000.0, cpuNum) |
| 112 | |
| 113 | if value, err = readUintFromFile(filepath.Join(cpu, "cpufreq", "scaling_min_freq")); err != nil { |
| 114 | return err |
| 115 | } |
| 116 | ch <- prometheus.MustNewConstMetric(c.cpuFreqMin, prometheus.GaugeValue, float64(value)*1000.0, cpuNum) |
| 117 | |
| 118 | if value, err = readUintFromFile(filepath.Join(cpu, "cpufreq", "scaling_max_freq")); err != nil { |
| 119 | return err |
| 120 | } |
| 121 | ch <- prometheus.MustNewConstMetric(c.cpuFreqMax, prometheus.GaugeValue, float64(value)*1000.0, cpuNum) |
| 122 | } |
| 123 | |
| 124 | // See |
| 125 | // https://www.kernel.org/doc/Documentation/x86/topology.txt |
| 126 | // https://www.kernel.org/doc/Documentation/cputopology.txt |
| 127 | // https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-devices-system-cpu |
| 128 | var err error |
| 129 | var physicalPackageID, coreID uint64 |
| 130 | |
| 131 | // topology/physical_package_id |
| 132 | if physicalPackageID, err = readUintFromFile(filepath.Join(cpu, "topology", "physical_package_id")); err != nil { |
| 133 | log.Debugf("CPU %v is missing physical_package_id", cpu) |
| 134 | continue |
| 135 | } |
| 136 | // topology/core_id |
| 137 | if coreID, err = readUintFromFile(filepath.Join(cpu, "topology", "core_id")); err != nil { |
| 138 | log.Debugf("CPU %v is missing core_id", cpu) |
| 139 | continue |
| 140 | } |
| 141 | |
| 142 | // metric node_cpu_core_throttles_total |
| 143 | // |
| 144 | // We process this metric before the package throttles as there |
| 145 | // are cpu+kernel combinations that only present core throttles |
no test coverage detected