MCPcopy Create free account
hub / github.com/prometheus/procfs / parseCPUStat

Function parseCPUStat

stat.go:86–124  ·  view source on GitHub ↗

Parse a cpu statistics line and returns the CPUStat struct plus the cpu id (or -1 for the overall sum).

(line string)

Source from the content-addressed store, hash-verified

84
85// Parse a cpu statistics line and returns the CPUStat struct plus the cpu id (or -1 for the overall sum).
86func parseCPUStat(line string) (CPUStat, int64, error) {
87 cpuStat := CPUStat{}
88 var cpu string
89
90 count, err := fmt.Sscanf(line, "%s %f %f %f %f %f %f %f %f %f %f",
91 &cpu,
92 &cpuStat.User, &cpuStat.Nice, &cpuStat.System, &cpuStat.Idle,
93 &cpuStat.Iowait, &cpuStat.IRQ, &cpuStat.SoftIRQ, &cpuStat.Steal,
94 &cpuStat.Guest, &cpuStat.GuestNice)
95
96 if err != nil && !errors.Is(err, io.EOF) {
97 return CPUStat{}, -1, fmt.Errorf("%w: couldn't parse %q (cpu): %w", ErrFileParse, line, err)
98 }
99 if count == 0 {
100 return CPUStat{}, -1, fmt.Errorf("%w: couldn't parse %q (cpu): 0 elements parsed", ErrFileParse, line)
101 }
102
103 cpuStat.User /= userHZ
104 cpuStat.Nice /= userHZ
105 cpuStat.System /= userHZ
106 cpuStat.Idle /= userHZ
107 cpuStat.Iowait /= userHZ
108 cpuStat.IRQ /= userHZ
109 cpuStat.SoftIRQ /= userHZ
110 cpuStat.Steal /= userHZ
111 cpuStat.Guest /= userHZ
112 cpuStat.GuestNice /= userHZ
113
114 if cpu == "cpu" {
115 return cpuStat, -1, nil
116 }
117
118 cpuID, err := strconv.ParseInt(cpu[3:], 10, 64)
119 if err != nil {
120 return CPUStat{}, -1, fmt.Errorf("%w: couldn't parse %q (cpu/cpuid): %w", ErrFileParse, line, err)
121 }
122
123 return cpuStat, cpuID, nil
124}
125
126// Parse a softirq line.
127func parseSoftIRQStat(line string) (SoftIRQStat, uint64, error) {

Callers 1

parseStatFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…