cpu_freebsd.go 4.5 KB

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