cpu_dragonfly.go 3.9 KB

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