concrete_sigar.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package sigar
  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) GetFileSystemUsage(path string) (FileSystemUsage, error) {
  50. f := FileSystemUsage{}
  51. err := f.Get(path)
  52. return f, err
  53. }