aboutsummaryrefslogtreecommitdiff
path: root/internal/speed/cpuinfo.go
blob: df3177d429f1d7231509d687d67e4cff7f77c9ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package speed

import (
	"io/ioutil"
	"os"
	"runtime"
	"strings"
)

// cpuModelName returns the "model name" acc. to /proc/cpuinfo, or ""
// on error.
//
// Examples: On my desktop PC:
//
//	$ grep "model name" /proc/cpuinfo
//	model name	: Intel(R) Core(TM) i5-3470 CPU @ 3.20GHz
//
// --> Returns "Intel(R) Core(TM) i5-3470 CPU @ 3.20GHz".
//
// On a Raspberry Pi 4:
//
//	$ grep "model name" /proc/cpuinfo
//	(empty)
//	$ grep Hardware /proc/cpuinfo
//	Hardware	: BCM2835
//
// --> Returns "BCM2835"
func cpuModelName() string {
	if runtime.GOOS != "linux" {
		return ""
	}
	f, err := os.Open("/proc/cpuinfo")
	if err != nil {
		return ""
	}
	content, err := ioutil.ReadAll(f)
	if err != nil {
		return ""
	}
	lines := strings.Split(string(content), "\n")
	// Look for "model name", then for "Hardware" (arm devices don't have "model name")
	for _, want := range []string{"model name", "Hardware"} {
		for _, line := range lines {
			if strings.HasPrefix(line, want) {
				parts := strings.SplitN(line, ":", 2)
				if len(parts) != 2 {
					continue
				}
				return strings.TrimSpace(parts[1])
			}
		}
	}
	return ""
}