Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "regexp"
- "strconv"
- "github.com/davecgh/go-spew/spew"
- )
- func main() {
- 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`)
- d := func(s string) {
- type cpuInfo struct {
- numCores int64
- numHT int64
- proc string
- family string
- model string
- step string
- clock string
- }
- matches := expression.FindAllStringSubmatch(s, -1)
- fmt.Printf(s)
- cpus := make([]cpuInfo, 0, len(matches))
- for _, cpuMatch := range matches {
- spew.Dump(cpuMatch)
- var err error
- var numCores int64
- var numHT int64
- switch {
- case cpuMatch[1] != "":
- numCores, err = strconv.ParseInt(cpuMatch[1], 10, 64)
- if err != nil {
- panic(err)
- }
- case cpuMatch[3] != "":
- numCores, err = strconv.ParseInt(cpuMatch[3], 10, 64)
- if err != nil {
- panic(err)
- }
- numHT, err = strconv.ParseInt(cpuMatch[4], 10, 64)
- if err != nil {
- panic(err)
- }
- }
- cpus = append(cpus, cpuInfo{
- numCores: numCores,
- numHT: numHT,
- proc: cpuMatch[5],
- family: cpuMatch[6],
- model: cpuMatch[7],
- step: cpuMatch[8],
- clock: cpuMatch[9],
- })
- }
- spew.Dump(cpus)
- }
- d(inputSinglecore)
- d(inputMulticore)
- }
- const inputSinglecore = `The physical processor has 1 virtual processor (0)
- x86 (GenuineIntel 406E3 family 6 model 78 step 3 clock 3312 MHz)
- Intel(r) Core(tm) i7-6567U CPU @ 3.30GHz
- The physical processor has 1 virtual processor (1)
- x86 (GenuineIntel 406E3 family 6 model 78 step 3 clock 3312 MHz)
- Intel(r) Core(tm) i7-6567U CPU @ 3.30GHz
- `
- const inputMulticore = `The physical processor has 8 cores and 16 virtual processors (0-7 16-23)
- The core has 2 virtual processors (0 16)
- The core has 2 virtual processors (1 17)
- The core has 2 virtual processors (2 18)
- The core has 2 virtual processors (3 19)
- The core has 2 virtual processors (4 20)
- The core has 2 virtual processors (5 21)
- The core has 2 virtual processors (6 22)
- The core has 2 virtual processors (7 23)
- x86 (GenuineIntel 206D7 family 6 model 45 step 7 clock 2600 MHz)
- Intel(r) Xeon(r) CPU E5-2670 0 @ 2.60GHz
- The physical processor has 8 cores and 16 virtual processors (8-15 24-31)
- The core has 2 virtual processors (8 24)
- The core has 2 virtual processors (9 25)
- The core has 2 virtual processors (10 26)
- The core has 2 virtual processors (11 27)
- The core has 2 virtual processors (12 28)
- The core has 2 virtual processors (13 29)
- The core has 2 virtual processors (14 30)
- The core has 2 virtual processors (15 31)
- x86 (GenuineIntel 206D7 family 6 model 45 step 7 clock 2600 MHz)
- Intel(r) Xeon(r) CPU E5-2670 0 @ 2.60GHz
- `
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement