xref: /aosp_15_r20/build/soong/ui/metrics/hostinfo.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1// Copyright 2024 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//	http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14package metrics
15
16// The hostinfo* files contain code to extract host information from
17// /proc/cpuinfo and /proc/meminfo relevant to machine performance
18
19import (
20	"strconv"
21	"strings"
22)
23
24// CpuInfo holds information regarding the host's CPU cores.
25type CpuInfo struct {
26	// The vendor id
27	VendorId string
28
29	// The model name
30	ModelName string
31
32	// The number of CPU cores
33	CpuCores int32
34
35	// The CPU flags
36	Flags string
37}
38
39// MemInfo holds information regarding the host's memory.
40// The memory size in each of the field is in bytes.
41type MemInfo struct {
42	// The total memory.
43	MemTotal uint64
44
45	// The amount of free memory.
46	MemFree uint64
47
48	// The amount of available memory.
49	MemAvailable uint64
50}
51
52// fillCpuInfo takes the key and value, converts the value
53// to the proper size unit and is stores it in CpuInfo.
54func (c *CpuInfo) fillInfo(key, value string) {
55	switch key {
56	case "vendor_id":
57		c.VendorId = value
58	case "model name":
59		c.ModelName = value
60	case "cpu cores":
61		v, err := strconv.ParseInt(value, 10, 32)
62		if err == nil {
63			c.CpuCores = int32(v)
64		}
65	case "flags":
66		c.Flags = value
67	default:
68		// Ignore unknown keys
69	}
70}
71
72// fillCpuInfo takes the key and value, converts the value
73// to the proper size unit and is stores it in CpuInfo.
74func (m *MemInfo) fillInfo(key, value string) {
75	v := strToUint64(value)
76	switch key {
77	case "MemTotal":
78		m.MemTotal = v
79	case "MemFree":
80		m.MemFree = v
81	case "MemAvailable":
82		m.MemAvailable = v
83	default:
84		// Ignore unknown keys
85	}
86}
87
88// strToUint64 takes the string and converts to unsigned 64-bit integer.
89// If the string contains a memory unit such as kB and is converted to
90// bytes.
91func strToUint64(v string) uint64 {
92	// v could be "1024 kB" so scan for the empty space and
93	// split between the value and the unit.
94	var separatorIndex int
95	if separatorIndex = strings.IndexAny(v, " "); separatorIndex < 0 {
96		separatorIndex = len(v)
97	}
98	value, err := strconv.ParseUint(v[:separatorIndex], 10, 64)
99	if err != nil {
100		return 0
101	}
102
103	var scale uint64 = 1
104	switch strings.TrimSpace(v[separatorIndex:]) {
105	case "kB", "KB":
106		scale = 1024
107	case "mB", "MB":
108		scale = 1024 * 1024
109	}
110	return value * scale
111}
112