utils.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package systemstat
  2. import (
  3. "log"
  4. "runtime"
  5. "strconv"
  6. )
  7. func notImplemented(fn string) {
  8. log.Printf("systemstat/%s is not implemented for this OS: %s\n", fn, runtime.GOOS)
  9. }
  10. func getSimpleCPUAverage(first CPUSample, second CPUSample) (avg SimpleCPUAverage) {
  11. //walltimediff := second.Time.Sub(first.Time)
  12. //dT := float64(first.Total - second.Total)
  13. dI := float64(second.Idle - first.Idle)
  14. dTot := float64(second.Total - first.Total)
  15. avg.IdlePct = dI / dTot * 100
  16. avg.BusyPct = (dTot - dI) * 100 / dTot
  17. //log.Printf("cpu idle ticks %f, total ticks %f, idle pct %f, busy pct %f\n", dI, dTot, avg.IdlePct, avg.BusyPct)
  18. return
  19. }
  20. func subtractAndConvertTicks(first uint64, second uint64) float64 {
  21. return float64(first - second)
  22. }
  23. func getCPUAverage(first CPUSample, second CPUSample) (avg CPUAverage) {
  24. dTot := float64(second.Total - first.Total)
  25. invQuotient := 100.00 / dTot
  26. avg.UserPct = subtractAndConvertTicks(second.User, first.User) * invQuotient
  27. avg.NicePct = subtractAndConvertTicks(second.Nice, first.Nice) * invQuotient
  28. avg.SystemPct = subtractAndConvertTicks(second.System, first.System) * invQuotient
  29. avg.IdlePct = subtractAndConvertTicks(second.Idle, first.Idle) * invQuotient
  30. avg.IowaitPct = subtractAndConvertTicks(second.Iowait, first.Iowait) * invQuotient
  31. avg.IrqPct = subtractAndConvertTicks(second.Irq, first.Irq) * invQuotient
  32. avg.SoftIrqPct = subtractAndConvertTicks(second.SoftIrq, first.SoftIrq) * invQuotient
  33. avg.StealPct = subtractAndConvertTicks(second.Steal, first.Steal) * invQuotient
  34. avg.GuestPct = subtractAndConvertTicks(second.Guest, first.Guest) * invQuotient
  35. avg.Time = second.Time
  36. avg.Seconds = second.Time.Sub(first.Time).Seconds()
  37. return
  38. }
  39. func getProcCPUAverage(first ProcCPUSample, second ProcCPUSample, procUptime float64) (avg ProcCPUAverage) {
  40. dT := second.Time.Sub(first.Time).Seconds()
  41. avg.UserPct = 100 * (second.User - first.User) / dT
  42. avg.SystemPct = 100 * (second.System - first.System) / dT
  43. avg.TotalPct = 100 * (second.Total - first.Total) / dT
  44. avg.PossiblePct = 100.0 * float64(runtime.NumCPU())
  45. avg.CumulativeTotalPct = 100 * second.Total / procUptime
  46. avg.Time = second.Time
  47. avg.Seconds = dT
  48. return
  49. }
  50. func parseCPUFields(fields []string, stat *CPUSample) {
  51. numFields := len(fields)
  52. stat.Name = fields[0]
  53. for i := 1; i < numFields; i++ {
  54. val, numerr := strconv.ParseUint(fields[i], 10, 64)
  55. if numerr != nil {
  56. log.Println("systemstat.parseCPUFields(): Error parsing (field, value): ", i, fields[i])
  57. }
  58. stat.Total += val
  59. switch i {
  60. case 1:
  61. stat.User = val
  62. case 2:
  63. stat.Nice = val
  64. case 3:
  65. stat.System = val
  66. case 4:
  67. stat.Idle = val
  68. case 5:
  69. stat.Iowait = val
  70. case 6:
  71. stat.Irq = val
  72. case 7:
  73. stat.SoftIrq = val
  74. case 8:
  75. stat.Steal = val
  76. case 9:
  77. stat.Guest = val
  78. }
  79. }
  80. }