open-nomad/client/fingerprint/memory.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

65 lines
1.4 KiB
Go
Raw Normal View History

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package fingerprint
import (
"fmt"
log "github.com/hashicorp/go-hclog"
"github.com/hashicorp/nomad/nomad/structs"
2021-03-30 18:47:33 +00:00
"github.com/shirou/gopsutil/v3/mem"
)
const bytesInMB int64 = 1024 * 1024
2018-03-28 15:07:15 +00:00
// MemoryFingerprint is used to fingerprint the available memory on the node
type MemoryFingerprint struct {
2015-11-05 21:46:02 +00:00
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))
2018-09-30 00:23:41 +00:00
memoryMB := totalMemory / bytesInMB
// COMPAT(0.10): Unused since 0.9.
resp.Resources = &structs.Resources{
MemoryMB: int(memoryMB),
}
2018-09-30 00:23:41 +00:00
resp.NodeResources = &structs.NodeResources{
Memory: structs.NodeMemoryResources{
MemoryMB: memoryMB,
2018-09-30 00:23:41 +00:00
},
}
}
return nil
}