Advertisement
sean-

Parsing CPU Info for Illumos

Mar 11th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.91 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "regexp"
  6.     "strconv"
  7.  
  8.     "github.com/davecgh/go-spew/spew"
  9. )
  10.  
  11. func main() {
  12.     expression := regexp.MustCompile(`The physical processor has (?:([\d]+) virtual processor \(([\d]+)\)|([\d]+) cores and ([\d]+) virtual processors[^\n]+)\n(?:\s+ The core has.+\n)*\s+.+ ([\S]+) family (.+) model (.+) step (.+) clock (.+) MHz`)
  13.  
  14.     d := func(s string) {
  15.         type cpuInfo struct {
  16.             numCores int64
  17.             numHT    int64
  18.             proc     string
  19.             family   string
  20.             model    string
  21.             step     string
  22.             clock    string
  23.         }
  24.  
  25.         matches := expression.FindAllStringSubmatch(s, -1)
  26.  
  27.         fmt.Printf(s)
  28.         cpus := make([]cpuInfo, 0, len(matches))
  29.         for _, cpuMatch := range matches {
  30.             spew.Dump(cpuMatch)
  31.  
  32.             var err error
  33.             var numCores int64
  34.             var numHT int64
  35.             switch {
  36.             case cpuMatch[1] != "":
  37.                 numCores, err = strconv.ParseInt(cpuMatch[1], 10, 64)
  38.                 if err != nil {
  39.                     panic(err)
  40.                 }
  41.             case cpuMatch[3] != "":
  42.                 numCores, err = strconv.ParseInt(cpuMatch[3], 10, 64)
  43.                 if err != nil {
  44.                     panic(err)
  45.                 }
  46.  
  47.                 numHT, err = strconv.ParseInt(cpuMatch[4], 10, 64)
  48.                 if err != nil {
  49.                     panic(err)
  50.                 }
  51.             }
  52.  
  53.             cpus = append(cpus, cpuInfo{
  54.                 numCores: numCores,
  55.                 numHT:    numHT,
  56.                 proc:     cpuMatch[5],
  57.                 family:   cpuMatch[6],
  58.                 model:    cpuMatch[7],
  59.                 step:     cpuMatch[8],
  60.                 clock:    cpuMatch[9],
  61.             })
  62.  
  63.         }
  64.         spew.Dump(cpus)
  65.     }
  66.  
  67.     d(inputSinglecore)
  68.     d(inputMulticore)
  69. }
  70.  
  71. const inputSinglecore = `The physical processor has 1 virtual processor (0)
  72.   x86 (GenuineIntel 406E3 family 6 model 78 step 3 clock 3312 MHz)
  73.         Intel(r) Core(tm) i7-6567U CPU @ 3.30GHz
  74. The physical processor has 1 virtual processor (1)
  75.   x86 (GenuineIntel 406E3 family 6 model 78 step 3 clock 3312 MHz)
  76.         Intel(r) Core(tm) i7-6567U CPU @ 3.30GHz
  77. `
  78.  
  79. const inputMulticore = `The physical processor has 8 cores and 16 virtual processors (0-7 16-23)
  80.   The core has 2 virtual processors (0 16)
  81.   The core has 2 virtual processors (1 17)
  82.   The core has 2 virtual processors (2 18)
  83.   The core has 2 virtual processors (3 19)
  84.   The core has 2 virtual processors (4 20)
  85.   The core has 2 virtual processors (5 21)
  86.   The core has 2 virtual processors (6 22)
  87.   The core has 2 virtual processors (7 23)
  88.     x86 (GenuineIntel 206D7 family 6 model 45 step 7 clock 2600 MHz)
  89.       Intel(r) Xeon(r) CPU E5-2670 0 @ 2.60GHz
  90. The physical processor has 8 cores and 16 virtual processors (8-15 24-31)
  91.   The core has 2 virtual processors (8 24)
  92.   The core has 2 virtual processors (9 25)
  93.   The core has 2 virtual processors (10 26)
  94.   The core has 2 virtual processors (11 27)
  95.   The core has 2 virtual processors (12 28)
  96.   The core has 2 virtual processors (13 29)
  97.   The core has 2 virtual processors (14 30)
  98.   The core has 2 virtual processors (15 31)
  99.     x86 (GenuineIntel 206D7 family 6 model 45 step 7 clock 2600 MHz)
  100.       Intel(r) Xeon(r) CPU E5-2670 0 @ 2.60GHz
  101. `
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement