forked from jaypipes/ghw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.go
40 lines (36 loc) · 941 Bytes
/
memory.go
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
package ghw
import (
"fmt"
"math"
)
type MemoryInfo struct {
TotalPhysicalBytes int64
TotalUsableBytes int64
// An array of sizes, in bytes, of memory pages supported by the host
SupportedPageSizes []uint64
}
func Memory() (*MemoryInfo, error) {
info := &MemoryInfo{}
err := memFillInfo(info)
if err != nil {
return nil, err
}
return info, nil
}
func (i *MemoryInfo) String() string {
tpbs := "unknown"
if i.TotalPhysicalBytes > 0 {
tpb := i.TotalPhysicalBytes
unit, unitStr := unitWithString(tpb)
tpb = int64(math.Ceil(float64(i.TotalPhysicalBytes) / float64(unit)))
tpbs = fmt.Sprintf("%d%s", tpb, unitStr)
}
tubs := "unknown"
if i.TotalUsableBytes > 0 {
tub := i.TotalUsableBytes
unit, unitStr := unitWithString(tub)
tub = int64(math.Ceil(float64(i.TotalUsableBytes) / float64(unit)))
tubs = fmt.Sprintf("%d%s", tub, unitStr)
}
return fmt.Sprintf("memory (%s physical, %s usable)", tpbs, tubs)
}