sigar_linux.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright (c) 2012 VMware, Inc.
  2. package gosigar
  3. import (
  4. "io/ioutil"
  5. "strconv"
  6. "strings"
  7. "syscall"
  8. )
  9. func init() {
  10. system.ticks = 100 // C.sysconf(C._SC_CLK_TCK)
  11. Procd = "/proc"
  12. getLinuxBootTime()
  13. }
  14. func getMountTableFileName() string {
  15. return "/etc/mtab"
  16. }
  17. func (self *Uptime) Get() error {
  18. sysinfo := syscall.Sysinfo_t{}
  19. if err := syscall.Sysinfo(&sysinfo); err != nil {
  20. return err
  21. }
  22. self.Length = float64(sysinfo.Uptime)
  23. return nil
  24. }
  25. func (self *FDUsage) Get() error {
  26. return readFile(Procd+"/sys/fs/file-nr", func(line string) bool {
  27. fields := strings.Fields(line)
  28. if len(fields) == 3 {
  29. self.Open, _ = strconv.ParseUint(fields[0], 10, 64)
  30. self.Unused, _ = strconv.ParseUint(fields[1], 10, 64)
  31. self.Max, _ = strconv.ParseUint(fields[2], 10, 64)
  32. }
  33. return false
  34. })
  35. }
  36. func (self *HugeTLBPages) Get() error {
  37. table, err := parseMeminfo()
  38. if err != nil {
  39. return err
  40. }
  41. self.Total, _ = table["HugePages_Total"]
  42. self.Free, _ = table["HugePages_Free"]
  43. self.Reserved, _ = table["HugePages_Rsvd"]
  44. self.Surplus, _ = table["HugePages_Surp"]
  45. self.DefaultSize, _ = table["Hugepagesize"]
  46. if totalSize, found := table["Hugetlb"]; found {
  47. self.TotalAllocatedSize = totalSize
  48. } else {
  49. // If Hugetlb is not present, or huge pages of different sizes
  50. // are used, this figure can be unaccurate.
  51. // TODO (jsoriano): Extract information from /sys/kernel/mm/hugepages too
  52. self.TotalAllocatedSize = (self.Total - self.Free + self.Reserved) * self.DefaultSize
  53. }
  54. return nil
  55. }
  56. func (self *ProcFDUsage) Get(pid int) error {
  57. err := readFile(procFileName(pid, "limits"), func(line string) bool {
  58. if strings.HasPrefix(line, "Max open files") {
  59. fields := strings.Fields(line)
  60. if len(fields) == 6 {
  61. self.SoftLimit, _ = strconv.ParseUint(fields[3], 10, 64)
  62. self.HardLimit, _ = strconv.ParseUint(fields[4], 10, 64)
  63. }
  64. return false
  65. }
  66. return true
  67. })
  68. if err != nil {
  69. return err
  70. }
  71. fds, err := ioutil.ReadDir(procFileName(pid, "fd"))
  72. if err != nil {
  73. return err
  74. }
  75. self.Open = uint64(len(fds))
  76. return nil
  77. }
  78. func parseCpuStat(self *Cpu, line string) error {
  79. fields := strings.Fields(line)
  80. self.User, _ = strtoull(fields[1])
  81. self.Nice, _ = strtoull(fields[2])
  82. self.Sys, _ = strtoull(fields[3])
  83. self.Idle, _ = strtoull(fields[4])
  84. self.Wait, _ = strtoull(fields[5])
  85. self.Irq, _ = strtoull(fields[6])
  86. self.SoftIrq, _ = strtoull(fields[7])
  87. self.Stolen, _ = strtoull(fields[8])
  88. return nil
  89. }