|
| 1 | +/* |
| 2 | +Copyright 2020 The Kubernetes Authors All rights reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package systemstatsmonitor |
| 18 | + |
| 19 | +import ( |
| 20 | + "github.com/golang/glog" |
| 21 | + "github.com/shirou/gopsutil/cpu" |
| 22 | + "github.com/shirou/gopsutil/load" |
| 23 | + |
| 24 | + ssmtypes "k8s.io/node-problem-detector/pkg/systemstatsmonitor/types" |
| 25 | + "k8s.io/node-problem-detector/pkg/util/metrics" |
| 26 | +) |
| 27 | + |
| 28 | +// clockTick is the ratio between 1 second and 1 USER_HZ (a clock tick). |
| 29 | +// |
| 30 | +// CLK_TCK is 100 in most architectures. If NPD ever runs on a super special architecture, |
| 31 | +// we can work out a way to detect the clock tick on that architecture (might require |
| 32 | +// cross-compilation with C library or parsing kernel ABIs). For now, it's not worth the |
| 33 | +// complexity. |
| 34 | +// |
| 35 | +// See documentation at http://man7.org/linux/man-pages/man5/proc.5.html |
| 36 | +const clockTick float64 = 100.0 |
| 37 | + |
| 38 | +type cpuCollector struct { |
| 39 | + mRunnableTaskCount *metrics.Float64Metric |
| 40 | + mUsageTime *metrics.Float64Metric |
| 41 | + |
| 42 | + config *ssmtypes.CPUStatsConfig |
| 43 | + |
| 44 | + lastUsageTime map[string]float64 |
| 45 | +} |
| 46 | + |
| 47 | +func NewCPUCollectorOrDie(cpuConfig *ssmtypes.CPUStatsConfig) *cpuCollector { |
| 48 | + cc := cpuCollector{config: cpuConfig} |
| 49 | + |
| 50 | + var err error |
| 51 | + |
| 52 | + cc.mRunnableTaskCount, err = metrics.NewFloat64Metric( |
| 53 | + metrics.CPURunnableTaskCountID, |
| 54 | + cpuConfig.MetricsConfigs[string(metrics.CPURunnableTaskCountID)].DisplayName, |
| 55 | + "The average number of runnable tasks in the run-queue during the last minute", |
| 56 | + "1", |
| 57 | + metrics.LastValue, |
| 58 | + []string{}) |
| 59 | + if err != nil { |
| 60 | + glog.Fatalf("Error initializing metric for %q: %v", metrics.CPURunnableTaskCountID, err) |
| 61 | + } |
| 62 | + |
| 63 | + cc.mUsageTime, err = metrics.NewFloat64Metric( |
| 64 | + metrics.CPUUsageTimeID, |
| 65 | + cpuConfig.MetricsConfigs[string(metrics.CPUUsageTimeID)].DisplayName, |
| 66 | + "CPU usage, in seconds", |
| 67 | + "s", |
| 68 | + metrics.Sum, |
| 69 | + []string{stateLabel}) |
| 70 | + if err != nil { |
| 71 | + glog.Fatalf("Error initializing metric for %q: %v", metrics.CPUUsageTimeID, err) |
| 72 | + } |
| 73 | + |
| 74 | + cc.lastUsageTime = make(map[string]float64) |
| 75 | + |
| 76 | + return &cc |
| 77 | +} |
| 78 | + |
| 79 | +func (cc *cpuCollector) recordLoad() { |
| 80 | + if cc.mRunnableTaskCount == nil { |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + loadAvg, err := load.Avg() |
| 85 | + if err != nil { |
| 86 | + glog.Errorf("Failed to retrieve average CPU load: %v", err) |
| 87 | + return |
| 88 | + } |
| 89 | + |
| 90 | + cc.mRunnableTaskCount.Record(map[string]string{}, loadAvg.Load1) |
| 91 | +} |
| 92 | + |
| 93 | +func (cc *cpuCollector) recordUsage() { |
| 94 | + if cc.mUsageTime == nil { |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + // Set percpu=false to get aggregated usage from all CPUs. |
| 99 | + timersStats, err := cpu.Times(false) |
| 100 | + if err != nil { |
| 101 | + glog.Errorf("Failed to retrieve CPU timers stat: %v", err) |
| 102 | + return |
| 103 | + } |
| 104 | + timersStat := timersStats[0] |
| 105 | + |
| 106 | + cc.mUsageTime.Record(map[string]string{stateLabel: "user"}, clockTick*timersStat.User-cc.lastUsageTime["user"]) |
| 107 | + cc.lastUsageTime["user"] = clockTick * timersStat.User |
| 108 | + |
| 109 | + cc.mUsageTime.Record(map[string]string{stateLabel: "system"}, clockTick*timersStat.System-cc.lastUsageTime["system"]) |
| 110 | + cc.lastUsageTime["system"] = clockTick * timersStat.System |
| 111 | + |
| 112 | + cc.mUsageTime.Record(map[string]string{stateLabel: "idle"}, clockTick*timersStat.Idle-cc.lastUsageTime["idle"]) |
| 113 | + cc.lastUsageTime["idle"] = clockTick * timersStat.Idle |
| 114 | + |
| 115 | + cc.mUsageTime.Record(map[string]string{stateLabel: "nice"}, clockTick*timersStat.Nice-cc.lastUsageTime["nice"]) |
| 116 | + cc.lastUsageTime["nice"] = clockTick * timersStat.Nice |
| 117 | + |
| 118 | + cc.mUsageTime.Record(map[string]string{stateLabel: "iowait"}, clockTick*timersStat.Iowait-cc.lastUsageTime["iowait"]) |
| 119 | + cc.lastUsageTime["iowait"] = clockTick * timersStat.Iowait |
| 120 | + |
| 121 | + cc.mUsageTime.Record(map[string]string{stateLabel: "irq"}, clockTick*timersStat.Irq-cc.lastUsageTime["irq"]) |
| 122 | + cc.lastUsageTime["irq"] = clockTick * timersStat.Irq |
| 123 | + |
| 124 | + cc.mUsageTime.Record(map[string]string{stateLabel: "softirq"}, clockTick*timersStat.Softirq-cc.lastUsageTime["softirq"]) |
| 125 | + cc.lastUsageTime["softirq"] = clockTick * timersStat.Softirq |
| 126 | + |
| 127 | + cc.mUsageTime.Record(map[string]string{stateLabel: "steal"}, clockTick*timersStat.Steal-cc.lastUsageTime["steal"]) |
| 128 | + cc.lastUsageTime["steal"] = clockTick * timersStat.Steal |
| 129 | + |
| 130 | + cc.mUsageTime.Record(map[string]string{stateLabel: "guest"}, clockTick*timersStat.Guest-cc.lastUsageTime["guest"]) |
| 131 | + cc.lastUsageTime["guest"] = clockTick * timersStat.Guest |
| 132 | + |
| 133 | + cc.mUsageTime.Record(map[string]string{stateLabel: "guest_nice"}, clockTick*timersStat.GuestNice-cc.lastUsageTime["guest_nice"]) |
| 134 | + cc.lastUsageTime["guest_nice"] = clockTick * timersStat.GuestNice |
| 135 | +} |
| 136 | + |
| 137 | +func (cc *cpuCollector) collect() { |
| 138 | + if cc == nil { |
| 139 | + return |
| 140 | + } |
| 141 | + |
| 142 | + cc.recordLoad() |
| 143 | + cc.recordUsage() |
| 144 | +} |
0 commit comments