cpu_dragonfly.go 3.9 KB

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