systemstat.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright (c) 2013 Phillip Bond
  2. // Licensed under the MIT License
  3. // see file LICENSE
  4. package systemstat
  5. import (
  6. "time"
  7. )
  8. // CPUSample is an object that represents the breakdown of time spent by the
  9. // CPU in various types of tasks. Two CPUSamples are required to find the
  10. // average usage over time, represented by the CPUAverage object. The CPUSample
  11. // is taken from the line "cpu" from /proc/stat in the Linux kernel.
  12. //
  13. // Summarized from the proc(5) man page:
  14. // /proc/stat :
  15. // kernel/system statistics. Varies with architecture.
  16. type CPUSample struct {
  17. User uint64 // time spent in user mode
  18. Nice uint64 // time spent in user mode with low priority (nice)
  19. System uint64 // time spent in system mode
  20. Idle uint64 // time spent in the idle task
  21. Iowait uint64 // time spent waiting for I/O to complete (since Linux 2.5.41)
  22. Irq uint64 // time spent servicing interrupts (since 2.6.0-test4)
  23. SoftIrq uint64 // time spent servicing softirqs (since 2.6.0-test4)
  24. Steal uint64 // time spent in other OSes when running in a virtualized environment
  25. Guest uint64 // time spent running a virtual CPU for guest operating systems under the control of the Linux kernel.
  26. Name string // name of the line in /proc/stat; cpu, cpu1, etc
  27. Time time.Time // when the sample was taken
  28. Total uint64 // total of all time fields
  29. }
  30. type ProcCPUSample struct {
  31. User float64 // time spent in user mode
  32. System float64 // time spent in system mode
  33. Time time.Time // when the sample was taken
  34. Total float64 // total of all time fields
  35. ProcMemUsedK int64
  36. }
  37. type ProcCPUAverage struct {
  38. UserPct float64 // time spent in user mode
  39. SystemPct float64 // time spent in system mode
  40. TotalPct float64 // total of all time fields
  41. PossiblePct float64 // total of all time fields
  42. CumulativeTotalPct float64 // total of all time throughout process life
  43. Time time.Time // when the sample was taken
  44. Seconds float64 // how many seconds between the two samples
  45. }
  46. // SimpleCPUAverage is an object that represents the average cpu usage over a
  47. // time period. It is calculated by taking the difference between two
  48. // CPUSamples (whose units are clock ticks), dividing by the number of elapsed
  49. // ticks between the samples, and converting to a percent. It is a simplified version of the CPUAverage in that it only accounts for time in the Idle task and all other time (Busy).
  50. type SimpleCPUAverage struct {
  51. BusyPct float64 // percent of time spent by CPU performing all non-idle tasks
  52. IdlePct float64 // percent of time spent by CPU in the idle task
  53. }
  54. // CPUAverage is an object that represents the average cpu usage over a
  55. // time period. It is calculated by taking the difference between two
  56. // CPUSamples (whose units are clock ticks), dividing by the number of elapsed
  57. // ticks between the samples, and converting to a percent.
  58. type CPUAverage struct {
  59. UserPct float64
  60. NicePct float64
  61. SystemPct float64
  62. IdlePct float64
  63. IowaitPct float64
  64. IrqPct float64
  65. SoftIrqPct float64
  66. StealPct float64
  67. GuestPct float64
  68. Time time.Time
  69. Seconds float64 // how many seconds between the two samples
  70. }
  71. type MemSample struct {
  72. Buffers uint64
  73. Cached uint64
  74. MemTotal uint64
  75. MemUsed uint64
  76. MemFree uint64
  77. SwapTotal uint64
  78. SwapUsed uint64
  79. SwapFree uint64
  80. Time time.Time
  81. }
  82. type LoadAvgSample struct {
  83. One float64
  84. Five float64
  85. Fifteen float64
  86. Time time.Time
  87. }
  88. type UptimeSample struct {
  89. Uptime float64
  90. Time time.Time
  91. }
  92. // GetCPUAverage returns the average cpu usage between two CPUSamples.
  93. func GetCPUAverage(first CPUSample, second CPUSample) CPUAverage {
  94. return getCPUAverage(first, second)
  95. }
  96. // GetSimpleCPUAverage returns an aggregated average cpu usage between two CPUSamples.
  97. func GetSimpleCPUAverage(first CPUSample, second CPUSample) SimpleCPUAverage {
  98. return getSimpleCPUAverage(first, second)
  99. }
  100. // GetProcCPUAverage returns the average cpu usage of this running process
  101. func GetProcCPUAverage(first ProcCPUSample, second ProcCPUSample, procUptime float64) (avg ProcCPUAverage) {
  102. return getProcCPUAverage(first, second, procUptime)
  103. }
  104. // GetCPUSample takes a snapshot of kernel statistics from the /proc/stat file.
  105. func GetCPUSample() (samp CPUSample) {
  106. return getCPUSample("/proc/stat")
  107. }
  108. // GetProcCPUSample takes a snapshot of kernel statistics from the /proc/stat file.
  109. func GetProcCPUSample() (samp ProcCPUSample) {
  110. return getProcCPUSample()
  111. }
  112. // GetUptime takes a snapshot of load info from the /proc/loadavg file.
  113. func GetUptime() (samp UptimeSample) {
  114. return getUptime("/proc/uptime")
  115. }
  116. // GetLoadAvgSample takes a snapshot of load info from the /proc/loadavg file.
  117. func GetLoadAvgSample() (samp LoadAvgSample) {
  118. return getLoadAvgSample("/proc/loadavg")
  119. }
  120. // GetMemSample takes a snapshot of memory info from the /proc/meminfo file.
  121. func GetMemSample() (samp MemSample) {
  122. return getMemSample("/proc/meminfo")
  123. }