diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-16 19:25:22 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-16 19:25:22 +0000 |
commit | f6ad4dcef54c5ce997a4bad5a6d86de229015700 (patch) | |
tree | 7cfa4e31ace5c2bd95c72b154d15af494b2bcbef /src/internal/sysinfo/cpuinfo_linux.go | |
parent | Initial commit. (diff) | |
download | golang-1.22-f6ad4dcef54c5ce997a4bad5a6d86de229015700.tar.xz golang-1.22-f6ad4dcef54c5ce997a4bad5a6d86de229015700.zip |
Adding upstream version 1.22.1.upstream/1.22.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/internal/sysinfo/cpuinfo_linux.go')
-rw-r--r-- | src/internal/sysinfo/cpuinfo_linux.go | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/src/internal/sysinfo/cpuinfo_linux.go b/src/internal/sysinfo/cpuinfo_linux.go new file mode 100644 index 0000000..aff63b3 --- /dev/null +++ b/src/internal/sysinfo/cpuinfo_linux.go @@ -0,0 +1,77 @@ +// Copyright 2023 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package sysinfo + +import ( + "bufio" + "bytes" + "io" + "os" + "strings" +) + +func readLinuxProcCPUInfo(buf []byte) error { + f, err := os.Open("/proc/cpuinfo") + if err != nil { + return err + } + defer f.Close() + + _, err = io.ReadFull(f, buf) + if err != nil && err != io.ErrUnexpectedEOF { + return err + } + + return nil +} + +func osCpuInfoName() string { + modelName := "" + cpuMHz := "" + + // The 512-byte buffer is enough to hold the contents of CPU0 + buf := make([]byte, 512) + err := readLinuxProcCPUInfo(buf) + if err != nil { + return "" + } + + scanner := bufio.NewScanner(bytes.NewReader(buf)) + for scanner.Scan() { + line := scanner.Text() + if !strings.Contains(line, ":") { + continue + } + + field := strings.SplitN(line, ": ", 2) + switch strings.TrimSpace(field[0]) { + case "Model Name", "model name": + modelName = field[1] + case "CPU MHz", "cpu MHz": + cpuMHz = field[1] + } + } + + if modelName == "" { + return "" + } + + if cpuMHz == "" { + return modelName + } + + // The modelName field already contains the frequency information, + // so the cpuMHz field information is not needed. + // modelName filed example: + // Intel(R) Core(TM) i7-10700 CPU @ 2.90GHz + f := [...]string{"GHz", "MHz"} + for _, v := range f { + if strings.Contains(modelName, v) { + return modelName + } + } + + return modelName + " @ " + cpuMHz + "MHz" +} |