Skip to content

Commit

Permalink
rename variables in cpuusage for linux
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Oct 24, 2017
1 parent 2b9c045 commit d9184af
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions metrics/linux/cpuusage.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,33 @@ var cpuUsageLogger = logging.GetLogger("metrics.cpuUsage")

// Generate CPU metric values
func (g *CPUUsageGenerator) Generate() (metrics.Values, error) {
before, err := g.collectProcStatValues()
previous, err := g.collectProcStatValues()
if err != nil {
return nil, err
}

time.Sleep(g.Interval)

after, err := g.collectProcStatValues()
current, err := g.collectProcStatValues()
if err != nil {
return nil, err
}

totalDiff := float64(after.Total - before.Total)
cpuCount := float64(after.CPUCount)
totalDiff := float64(current.Total - previous.Total)
cpuCount := float64(current.CPUCount)

ret := map[string]float64{
"cpu.user.percentage": float64((after.User-after.Guest)-(before.User-before.Guest)) * cpuCount * 100.0 / totalDiff,
"cpu.nice.percentage": float64(after.Nice-before.Nice) * cpuCount * 100.0 / totalDiff,
"cpu.system.percentage": float64(after.System-before.System) * cpuCount * 100.0 / totalDiff,
"cpu.idle.percentage": float64(after.Idle-before.Idle) * cpuCount * 100.0 / totalDiff,
"cpu.iowait.percentage": float64(after.Iowait-before.Iowait) * cpuCount * 100.0 / totalDiff,
"cpu.irq.percentage": float64(after.Irq-before.Irq) * cpuCount * 100.0 / totalDiff,
"cpu.softirq.percentage": float64(after.Softirq-before.Softirq) * cpuCount * 100.0 / totalDiff,
"cpu.steal.percentage": float64(after.Steal-before.Steal) * cpuCount * 100.0 / totalDiff,
"cpu.guest.percentage": float64(after.Guest-before.Guest) * cpuCount * 100.0 / totalDiff,
// "cpu.guest_nice.percentage": float64(after.GuestNice - before.GuestNice) * cpuCount * 100.0 / totalDiff,
"cpu.user.percentage": float64((current.User-current.Guest)-(previous.User-previous.Guest)) * cpuCount * 100.0 / totalDiff,
"cpu.nice.percentage": float64(current.Nice-previous.Nice) * cpuCount * 100.0 / totalDiff,
"cpu.system.percentage": float64(current.System-previous.System) * cpuCount * 100.0 / totalDiff,
"cpu.idle.percentage": float64(current.Idle-previous.Idle) * cpuCount * 100.0 / totalDiff,
"cpu.iowait.percentage": float64(current.Iowait-previous.Iowait) * cpuCount * 100.0 / totalDiff,
"cpu.irq.percentage": float64(current.Irq-previous.Irq) * cpuCount * 100.0 / totalDiff,
"cpu.softirq.percentage": float64(current.Softirq-previous.Softirq) * cpuCount * 100.0 / totalDiff,
"cpu.steal.percentage": float64(current.Steal-previous.Steal) * cpuCount * 100.0 / totalDiff,
"cpu.guest.percentage": float64(current.Guest-previous.Guest) * cpuCount * 100.0 / totalDiff,
// guest_nice is not yet supported in Mackerel
// "cpu.guest_nice.percentage": float64(current.GuestNice - previous.GuestNice) * cpuCount * 100.0 / totalDiff,
}
return metrics.Values(ret), nil
}
Expand Down

0 comments on commit d9184af

Please sign in to comment.