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