-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathmemory.go
61 lines (50 loc) · 1.33 KB
/
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package fingerprint
import (
"fmt"
log "github.com/hashicorp/go-hclog"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/shirou/gopsutil/mem"
)
const bytesInMB int64 = 1024 * 1024
// MemoryFingerprint is used to fingerprint the available memory on the node
type MemoryFingerprint struct {
StaticFingerprinter
logger log.Logger
}
// NewMemoryFingerprint is used to create a Memory fingerprint
func NewMemoryFingerprint(logger log.Logger) Fingerprint {
f := &MemoryFingerprint{
logger: logger.Named("memory"),
}
return f
}
func (f *MemoryFingerprint) Fingerprint(req *FingerprintRequest, resp *FingerprintResponse) error {
var totalMemory int64
cfg := req.Config
if cfg.MemoryMB != 0 {
totalMemory = int64(cfg.MemoryMB) * bytesInMB
} else {
memInfo, err := mem.VirtualMemory()
if err != nil {
f.logger.Warn("error reading memory information", "error", err)
return err
}
if memInfo.Total > 0 {
totalMemory = int64(memInfo.Total)
}
}
if totalMemory > 0 {
resp.AddAttribute("memory.totalbytes", fmt.Sprintf("%d", totalMemory))
memoryMB := totalMemory / bytesInMB
// COMPAT(0.10): Unused since 0.9.
resp.Resources = &structs.Resources{
MemoryMB: int(memoryMB),
}
resp.NodeResources = &structs.NodeResources{
Memory: structs.NodeMemoryResources{
MemoryMB: memoryMB,
},
}
}
return nil
}