返回Golang中的CPU插槽数、核心数和线程数。

huangapple go评论120阅读模式
英文:

Return number of CPU sockets, Cores, and Threads in Golang

问题

使用Golang,我正在尝试找到/编写一个函数,该函数将返回Linux系统的CPU插槽数、每个插槽的核心数和每个核心的线程数。例如,一个服务器可能有2个CPU,每个CPU有4个核心,并且支持超线程,每个CPU可以处理8个线程。

示例输出:

{
"CPUSockets": "2",
"CoresPerSocket": "4",
"ThreadsPerCore": "2"
}

问题:你知道有哪些Go包或伪代码可以提供这些信息吗?

注意:我已经查看了各种Go实现的psutil,但我找不到一个返回插槽数或区分核心和线程的实现。我想要的数据可以通过运行lscpu命令轻松获取,但我不知道如何使用Go访问它。

英文:

Using Golang I'm trying to find/write a function that would return the number of CPU Sockets, Cores per socket, and Threads per core a Linux system. For example, a server might have 2 CPUs each with 4 cores, with hyperthreading, it could handle 8 threads per CPU.

Sample Output:

  1. {
  2. "CPUSockets": "2",
  3. "CoresPerSocket": "4",
  4. "ThreadsPerCore": "2"
  5. }

Question: Do you know of any Go Package or pseudo code that could provide this information?

Note: I've looked at various go implementations of psutil but I can't find one that returns the number of sockets or distinguishes between Cores and Threads. The data I want is very easily accessible by running lscpu but I don't know how to access that using Go.

答案1

得分: 3

我已经弄清楚了,下面是翻译好的代码部分:

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os/exec"
  6. "strconv"
  7. "strings"
  8. )
  9. type CPUInfo struct {
  10. Sockets int32 `json:"sockets"`
  11. CoresPerSocket int32 `json:"cores_per_socket"`
  12. ThreadsPerCore int32 `json:"threads_per_core"`
  13. }
  14. func main() {
  15. out, _ := exec.Command("lscpu").Output()
  16. outstring := strings.TrimSpace(string(out))
  17. lines := strings.Split(outstring, "\n")
  18. c := CPUInfo{}
  19. for _, line := range lines {
  20. fields := strings.Split(line, ":")
  21. if len(fields) < 2 {
  22. continue
  23. }
  24. key := strings.TrimSpace(fields[0])
  25. value := strings.TrimSpace(fields[1])
  26. switch key {
  27. case "Socket(s)":
  28. t, _ := strconv.Atoi(value)
  29. c.Sockets = int32(t)
  30. case "Core(s) per socket":
  31. t, _ := strconv.Atoi(value)
  32. c.CoresPerSocket = int32(t)
  33. case "Thread(s) per core":
  34. t, _ := strconv.Atoi(value)
  35. c.ThreadsPerCore = int32(t)
  36. }
  37. }
  38. CPUInfoJSON, _ := json.MarshalIndent(c, "", " ")
  39. fmt.Println(string(CPUInfoJSON))
  40. }

输出结果:

  1. tbenz9@ubuntu-dev: go run socket.go
  2. {
  3. "sockets": 1,
  4. "cores_per_socket": 2,
  5. "threads_per_core": 2
  6. }
英文:

I ended up figuring it out, see below:

  1. package main
  2. import (
  3. &quot;encoding/json&quot;
  4. &quot;fmt&quot;
  5. &quot;os/exec&quot;
  6. &quot;strconv&quot;
  7. &quot;strings&quot;
  8. )
  9. type CPUInfo struct {
  10. Sockets int32 `json:&quot;sockets&quot;`
  11. CoresPerSocket int32 `json:&quot;cores_per_socket&quot;`
  12. ThreadsPerCore int32 `json:&quot;threads_per_core&quot;`
  13. }
  14. func main() {
  15. out, _ := exec.Command(&quot;lscpu&quot;).Output()
  16. outstring := strings.TrimSpace(string(out))
  17. lines := strings.Split(outstring, &quot;\n&quot;)
  18. c := CPUInfo{}
  19. for _, line := range lines {
  20. fields := strings.Split(line, &quot;:&quot;)
  21. if len(fields) &lt; 2 {
  22. continue
  23. }
  24. key := strings.TrimSpace(fields[0])
  25. value := strings.TrimSpace(fields[1])
  26. switch key {
  27. case &quot;Socket(s)&quot;:
  28. t, _ := strconv.Atoi(value)
  29. c.Sockets = int32(t)
  30. case &quot;Core(s) per socket&quot;:
  31. t, _ := strconv.Atoi(value)
  32. c.CoresPerSocket = int32(t)
  33. case &quot;Thread(s) per core&quot;:
  34. t, _ := strconv.Atoi(value)
  35. c.ThreadsPerCore = int32(t)
  36. }
  37. }
  38. CPUInfoJSON, _ := json.MarshalIndent(c, &quot;&quot;, &quot; &quot;)
  39. fmt.Println(string(CPUInfoJSON))
  40. }

Output:

  1. tbenz9@ubuntu-dev: go run socket.go
  2. {
  3. &quot;sockets&quot;: 1,
  4. &quot;cores_per_socket&quot;: 2,
  5. &quot;threads_per_core&quot;: 2
  6. }

huangapple
  • 本文由 发表于 2016年2月24日 08:36:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/35591107.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定