concrete_sigar.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package gosigar
  2. import (
  3. "time"
  4. )
  5. type ConcreteSigar struct{}
  6. func (c *ConcreteSigar) CollectCpuStats(collectionInterval time.Duration) (<-chan Cpu, chan<- struct{}) {
  7. // samplesCh is buffered to 1 value to immediately return first CPU sample
  8. samplesCh := make(chan Cpu, 1)
  9. stopCh := make(chan struct{})
  10. go func() {
  11. var cpuUsage Cpu
  12. // Immediately provide non-delta value.
  13. // samplesCh is buffered to 1 value, so it will not block.
  14. cpuUsage.Get()
  15. samplesCh <- cpuUsage
  16. ticker := time.NewTicker(collectionInterval)
  17. for {
  18. select {
  19. case <-ticker.C:
  20. previousCpuUsage := cpuUsage
  21. cpuUsage.Get()
  22. select {
  23. case samplesCh <- cpuUsage.Delta(previousCpuUsage):
  24. default:
  25. // Include default to avoid channel blocking
  26. }
  27. case <-stopCh:
  28. return
  29. }
  30. }
  31. }()
  32. return samplesCh, stopCh
  33. }
  34. func (c *ConcreteSigar) GetLoadAverage() (LoadAverage, error) {
  35. l := LoadAverage{}
  36. err := l.Get()
  37. return l, err
  38. }
  39. func (c *ConcreteSigar) GetMem() (Mem, error) {
  40. m := Mem{}
  41. err := m.Get()
  42. return m, err
  43. }
  44. func (c *ConcreteSigar) GetSwap() (Swap, error) {
  45. s := Swap{}
  46. err := s.Get()
  47. return s, err
  48. }
  49. func (c *ConcreteSigar) GetHugeTLBPages() (HugeTLBPages, error) {
  50. p := HugeTLBPages{}
  51. err := p.Get()
  52. return p, err
  53. }
  54. func (c *ConcreteSigar) GetFileSystemUsage(path string) (FileSystemUsage, error) {
  55. f := FileSystemUsage{}
  56. err := f.Get(path)
  57. return f, err
  58. }
  59. func (c *ConcreteSigar) GetFDUsage() (FDUsage, error) {
  60. fd := FDUsage{}
  61. err := fd.Get()
  62. return fd, err
  63. }
  64. // GetRusage return the resource usage of the process
  65. // Possible params: 0 = RUSAGE_SELF, 1 = RUSAGE_CHILDREN, 2 = RUSAGE_THREAD
  66. func (c *ConcreteSigar) GetRusage(who int) (Rusage, error) {
  67. r := Rusage{}
  68. err := r.Get(who)
  69. return r, err
  70. }