sigar_interface.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package gosigar
  2. import (
  3. "time"
  4. )
  5. type ErrNotImplemented struct {
  6. OS string
  7. }
  8. func (e ErrNotImplemented) Error() string {
  9. return "not implemented on " + e.OS
  10. }
  11. func IsNotImplemented(err error) bool {
  12. switch err.(type) {
  13. case ErrNotImplemented, *ErrNotImplemented:
  14. return true
  15. default:
  16. return false
  17. }
  18. }
  19. type Sigar interface {
  20. CollectCpuStats(collectionInterval time.Duration) (<-chan Cpu, chan<- struct{})
  21. GetLoadAverage() (LoadAverage, error)
  22. GetMem() (Mem, error)
  23. GetSwap() (Swap, error)
  24. GetHugeTLBPages(HugeTLBPages, error)
  25. GetFileSystemUsage(string) (FileSystemUsage, error)
  26. GetFDUsage() (FDUsage, error)
  27. GetRusage(who int) (Rusage, error)
  28. }
  29. type Cpu struct {
  30. User uint64
  31. Nice uint64
  32. Sys uint64
  33. Idle uint64
  34. Wait uint64
  35. Irq uint64
  36. SoftIrq uint64
  37. Stolen uint64
  38. }
  39. func (cpu *Cpu) Total() uint64 {
  40. return cpu.User + cpu.Nice + cpu.Sys + cpu.Idle +
  41. cpu.Wait + cpu.Irq + cpu.SoftIrq + cpu.Stolen
  42. }
  43. func (cpu Cpu) Delta(other Cpu) Cpu {
  44. return Cpu{
  45. User: cpu.User - other.User,
  46. Nice: cpu.Nice - other.Nice,
  47. Sys: cpu.Sys - other.Sys,
  48. Idle: cpu.Idle - other.Idle,
  49. Wait: cpu.Wait - other.Wait,
  50. Irq: cpu.Irq - other.Irq,
  51. SoftIrq: cpu.SoftIrq - other.SoftIrq,
  52. Stolen: cpu.Stolen - other.Stolen,
  53. }
  54. }
  55. type LoadAverage struct {
  56. One, Five, Fifteen float64
  57. }
  58. type Uptime struct {
  59. Length float64
  60. }
  61. type Mem struct {
  62. Total uint64
  63. Used uint64
  64. Free uint64
  65. ActualFree uint64
  66. ActualUsed uint64
  67. }
  68. type Swap struct {
  69. Total uint64
  70. Used uint64
  71. Free uint64
  72. }
  73. type HugeTLBPages struct {
  74. Total uint64
  75. Free uint64
  76. Reserved uint64
  77. Surplus uint64
  78. DefaultSize uint64
  79. TotalAllocatedSize uint64
  80. }
  81. type CpuList struct {
  82. List []Cpu
  83. }
  84. type FDUsage struct {
  85. Open uint64
  86. Unused uint64
  87. Max uint64
  88. }
  89. type FileSystem struct {
  90. DirName string
  91. DevName string
  92. TypeName string
  93. SysTypeName string
  94. Options string
  95. Flags uint32
  96. }
  97. type FileSystemList struct {
  98. List []FileSystem
  99. }
  100. type FileSystemUsage struct {
  101. Total uint64
  102. Used uint64
  103. Free uint64
  104. Avail uint64
  105. Files uint64
  106. FreeFiles uint64
  107. }
  108. type ProcList struct {
  109. List []int
  110. }
  111. type RunState byte
  112. const (
  113. RunStateSleep = 'S'
  114. RunStateRun = 'R'
  115. RunStateStop = 'T'
  116. RunStateZombie = 'Z'
  117. RunStateIdle = 'D'
  118. RunStateUnknown = '?'
  119. )
  120. type ProcState struct {
  121. Name string
  122. Username string
  123. State RunState
  124. Ppid int
  125. Pgid int
  126. Tty int
  127. Priority int
  128. Nice int
  129. Processor int
  130. }
  131. type ProcMem struct {
  132. Size uint64
  133. Resident uint64
  134. Share uint64
  135. MinorFaults uint64
  136. MajorFaults uint64
  137. PageFaults uint64
  138. }
  139. type ProcTime struct {
  140. StartTime uint64
  141. User uint64
  142. Sys uint64
  143. Total uint64
  144. }
  145. type ProcArgs struct {
  146. List []string
  147. }
  148. type ProcEnv struct {
  149. Vars map[string]string
  150. }
  151. type ProcExe struct {
  152. Name string
  153. Cwd string
  154. Root string
  155. }
  156. type ProcFDUsage struct {
  157. Open uint64
  158. SoftLimit uint64
  159. HardLimit uint64
  160. }
  161. type Rusage struct {
  162. Utime time.Duration
  163. Stime time.Duration
  164. Maxrss int64
  165. Ixrss int64
  166. Idrss int64
  167. Isrss int64
  168. Minflt int64
  169. Majflt int64
  170. Nswap int64
  171. Inblock int64
  172. Oublock int64
  173. Msgsnd int64
  174. Msgrcv int64
  175. Nsignals int64
  176. Nvcsw int64
  177. Nivcsw int64
  178. }