cpu_freebsd.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package cpu
  2. import (
  3. "context"
  4. "fmt"
  5. "reflect"
  6. "regexp"
  7. "runtime"
  8. "strconv"
  9. "strings"
  10. "unsafe"
  11. "github.com/shirou/gopsutil/internal/common"
  12. "github.com/tklauser/go-sysconf"
  13. "golang.org/x/sys/unix"
  14. )
  15. var ClocksPerSec = float64(128)
  16. var cpuMatch = regexp.MustCompile(`^CPU:`)
  17. var originMatch = regexp.MustCompile(`Origin\s*=\s*"(.+)"\s+Id\s*=\s*(.+)\s+Family\s*=\s*(.+)\s+Model\s*=\s*(.+)\s+Stepping\s*=\s*(.+)`)
  18. var featuresMatch = regexp.MustCompile(`Features=.+<(.+)>`)
  19. var featuresMatch2 = regexp.MustCompile(`Features2=[a-f\dx]+<(.+)>`)
  20. var cpuEnd = regexp.MustCompile(`^Trying to mount root`)
  21. var cpuCores = regexp.MustCompile(`FreeBSD/SMP: (\d*) package\(s\) x (\d*) core\(s\)`)
  22. var cpuTimesSize int
  23. var emptyTimes cpuTimes
  24. func init() {
  25. clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
  26. // ignore errors
  27. if err == nil {
  28. ClocksPerSec = float64(clkTck)
  29. }
  30. }
  31. func timeStat(name string, t *cpuTimes) *TimesStat {
  32. return &TimesStat{
  33. User: float64(t.User) / ClocksPerSec,
  34. Nice: float64(t.Nice) / ClocksPerSec,
  35. System: float64(t.Sys) / ClocksPerSec,
  36. Idle: float64(t.Idle) / ClocksPerSec,
  37. Irq: float64(t.Intr) / ClocksPerSec,
  38. CPU: name,
  39. }
  40. }
  41. func Times(percpu bool) ([]TimesStat, error) {
  42. return TimesWithContext(context.Background(), percpu)
  43. }
  44. func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
  45. if percpu {
  46. buf, err := unix.SysctlRaw("kern.cp_times")
  47. if err != nil {
  48. return nil, err
  49. }
  50. // We can't do this in init due to the conflict with cpu.init()
  51. if cpuTimesSize == 0 {
  52. cpuTimesSize = int(reflect.TypeOf(cpuTimes{}).Size())
  53. }
  54. ncpus := len(buf) / cpuTimesSize
  55. ret := make([]TimesStat, 0, ncpus)
  56. for i := 0; i < ncpus; i++ {
  57. times := (*cpuTimes)(unsafe.Pointer(&buf[i*cpuTimesSize]))
  58. if *times == emptyTimes {
  59. // CPU not present
  60. continue
  61. }
  62. ret = append(ret, *timeStat(fmt.Sprintf("cpu%d", len(ret)), times))
  63. }
  64. return ret, nil
  65. }
  66. buf, err := unix.SysctlRaw("kern.cp_time")
  67. if err != nil {
  68. return nil, err
  69. }
  70. times := (*cpuTimes)(unsafe.Pointer(&buf[0]))
  71. return []TimesStat{*timeStat("cpu-total", times)}, nil
  72. }
  73. // Returns only one InfoStat on FreeBSD. The information regarding core
  74. // count, however is accurate and it is assumed that all InfoStat attributes
  75. // are the same across CPUs.
  76. func Info() ([]InfoStat, error) {
  77. return InfoWithContext(context.Background())
  78. }
  79. func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
  80. const dmesgBoot = "/var/run/dmesg.boot"
  81. c, num, err := parseDmesgBoot(dmesgBoot)
  82. if err != nil {
  83. return nil, err
  84. }
  85. var u32 uint32
  86. if u32, err = unix.SysctlUint32("hw.clockrate"); err != nil {
  87. return nil, err
  88. }
  89. c.Mhz = float64(u32)
  90. if u32, err = unix.SysctlUint32("hw.ncpu"); err != nil {
  91. return nil, err
  92. }
  93. c.Cores = int32(u32)
  94. if c.ModelName, err = unix.Sysctl("hw.model"); err != nil {
  95. return nil, err
  96. }
  97. ret := make([]InfoStat, num)
  98. for i := 0; i < num; i++ {
  99. ret[i] = c
  100. }
  101. return ret, nil
  102. }
  103. func parseDmesgBoot(fileName string) (InfoStat, int, error) {
  104. c := InfoStat{}
  105. lines, _ := common.ReadLines(fileName)
  106. cpuNum := 1 // default cpu num is 1
  107. for _, line := range lines {
  108. if matches := cpuEnd.FindStringSubmatch(line); matches != nil {
  109. break
  110. } else if matches := originMatch.FindStringSubmatch(line); matches != nil {
  111. c.VendorID = matches[1]
  112. c.Family = matches[3]
  113. c.Model = matches[4]
  114. t, err := strconv.ParseInt(matches[5], 10, 32)
  115. if err != nil {
  116. return c, 0, fmt.Errorf("unable to parse FreeBSD CPU stepping information from %q: %v", line, err)
  117. }
  118. c.Stepping = int32(t)
  119. } else if matches := featuresMatch.FindStringSubmatch(line); matches != nil {
  120. for _, v := range strings.Split(matches[1], ",") {
  121. c.Flags = append(c.Flags, strings.ToLower(v))
  122. }
  123. } else if matches := featuresMatch2.FindStringSubmatch(line); matches != nil {
  124. for _, v := range strings.Split(matches[1], ",") {
  125. c.Flags = append(c.Flags, strings.ToLower(v))
  126. }
  127. } else if matches := cpuCores.FindStringSubmatch(line); matches != nil {
  128. t, err := strconv.ParseInt(matches[1], 10, 32)
  129. if err != nil {
  130. return c, 0, fmt.Errorf("unable to parse FreeBSD CPU Nums from %q: %v", line, err)
  131. }
  132. cpuNum = int(t)
  133. t2, err := strconv.ParseInt(matches[2], 10, 32)
  134. if err != nil {
  135. return c, 0, fmt.Errorf("unable to parse FreeBSD CPU cores from %q: %v", line, err)
  136. }
  137. c.Cores = int32(t2)
  138. }
  139. }
  140. return c, cpuNum, nil
  141. }
  142. func CountsWithContext(ctx context.Context, logical bool) (int, error) {
  143. return runtime.NumCPU(), nil
  144. }