cpu_windows.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. //go:build !linux
  2. // +build !linux
  3. // Use and distribution licensed under the Apache license version 2.
  4. //
  5. // See the COPYING file in the root project directory for full text.
  6. //
  7. package cpu
  8. import (
  9. "github.com/StackExchange/wmi"
  10. )
  11. const wmqlProcessor = "SELECT Manufacturer, Name, NumberOfLogicalProcessors, NumberOfCores FROM Win32_Processor"
  12. type win32Processor struct {
  13. Manufacturer *string
  14. Name *string
  15. NumberOfLogicalProcessors uint32
  16. NumberOfCores uint32
  17. }
  18. func (i *Info) load() error {
  19. // Getting info from WMI
  20. var win32descriptions []win32Processor
  21. if err := wmi.Query(wmqlProcessor, &win32descriptions); err != nil {
  22. return err
  23. }
  24. // Converting into standard structures
  25. i.Processors = processorsGet(win32descriptions)
  26. var totCores uint32
  27. var totThreads uint32
  28. for _, p := range i.Processors {
  29. totCores += p.NumCores
  30. totThreads += p.NumThreads
  31. }
  32. i.TotalCores = totCores
  33. i.TotalThreads = totThreads
  34. return nil
  35. }
  36. func processorsGet(win32descriptions []win32Processor) []*Processor {
  37. var procs []*Processor
  38. // Converting into standard structures
  39. for index, description := range win32descriptions {
  40. p := &Processor{
  41. ID: index,
  42. Model: *description.Name,
  43. Vendor: *description.Manufacturer,
  44. NumCores: description.NumberOfCores,
  45. NumThreads: description.NumberOfLogicalProcessors,
  46. }
  47. procs = append(procs, p)
  48. }
  49. return procs
  50. }