cpuinfo.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package linux
  2. import (
  3. "io/ioutil"
  4. "regexp"
  5. "strconv"
  6. "strings"
  7. )
  8. type CPUInfo struct {
  9. Processors []Processor `json:"processors"`
  10. }
  11. func (self *CPUInfo) NumCPU() int {
  12. return len(self.Processors)
  13. }
  14. func (self *CPUInfo) NumCore() int {
  15. core := make(map[string]bool)
  16. for _, p := range self.Processors {
  17. pid := p.PhysicalId
  18. cid := p.CoreId
  19. if pid == -1 {
  20. return self.NumCPU()
  21. } else {
  22. // to avoid fmt import
  23. key := strconv.FormatInt(int64(pid), 10) + ":" + strconv.FormatInt(int64(cid), 10)
  24. core[key] = true
  25. }
  26. }
  27. return len(core)
  28. }
  29. func (self *CPUInfo) NumPhysicalCPU() int {
  30. pcpu := make(map[string]bool)
  31. for _, p := range self.Processors {
  32. pid := p.PhysicalId
  33. if pid == -1 {
  34. return self.NumCPU()
  35. } else {
  36. // to avoid fmt import
  37. key := strconv.FormatInt(int64(pid), 10)
  38. pcpu[key] = true
  39. }
  40. }
  41. return len(pcpu)
  42. }
  43. type Processor struct {
  44. Id int64 `json:"id"`
  45. VendorId string `json:"vendor_id"`
  46. Model int64 `json:"model"`
  47. ModelName string `json:"model_name"`
  48. Flags []string `json:"flags"`
  49. Cores int64 `json:"cores"`
  50. MHz float64 `json:"mhz"`
  51. PhysicalId int64 `json:"physical_id"`
  52. CoreId int64 `json:"core_id"`
  53. }
  54. var cpuinfoRegExp = regexp.MustCompile("([^:]*?)\\s*:\\s*(.*)$")
  55. func ReadCPUInfo(path string) (*CPUInfo, error) {
  56. b, err := ioutil.ReadFile(path)
  57. if err != nil {
  58. return nil, err
  59. }
  60. content := string(b)
  61. lines := strings.Split(content, "\n")
  62. var cpuinfo = CPUInfo{}
  63. var processor = &Processor{CoreId: -1, PhysicalId: -1}
  64. for i, line := range lines {
  65. var key string
  66. var value string
  67. if len(line) == 0 && i != len(lines)-1 {
  68. // end of processor
  69. cpuinfo.Processors = append(cpuinfo.Processors, *processor)
  70. processor = &Processor{}
  71. continue
  72. } else if i == len(lines)-1 {
  73. continue
  74. }
  75. submatches := cpuinfoRegExp.FindStringSubmatch(line)
  76. key = submatches[1]
  77. value = submatches[2]
  78. switch key {
  79. case "processor":
  80. processor.Id, _ = strconv.ParseInt(value, 10, 32)
  81. case "vendor_id":
  82. processor.VendorId = value
  83. case "model":
  84. processor.Model, _ = strconv.ParseInt(value, 10, 32)
  85. case "model name":
  86. processor.ModelName = value
  87. case "flags":
  88. processor.Flags = strings.Fields(value)
  89. case "cpu cores":
  90. processor.Cores, _ = strconv.ParseInt(value, 10, 32)
  91. case "cpu MHz":
  92. processor.MHz, _ = strconv.ParseFloat(value, 64)
  93. case "physical id":
  94. processor.PhysicalId, _ = strconv.ParseInt(value, 10, 32)
  95. case "core id":
  96. processor.CoreId, _ = strconv.ParseInt(value, 10, 32)
  97. }
  98. /*
  99. processor : 0
  100. vendor_id : GenuineIntel
  101. cpu family : 6
  102. model : 26
  103. model name : Intel(R) Xeon(R) CPU L5520 @ 2.27GHz
  104. */
  105. }
  106. return &cpuinfo, nil
  107. }