Files
nomad/client/fingerprint/memory.go
Seth Hoenig 83720740f5 core: plumbing to support numa aware scheduling (#18681)
* core: plumbing to support numa aware scheduling

* core: apply node resources compatibility upon fsm rstore

Handle the case where an upgraded server dequeus an evaluation before
a client triggers a new fingerprint - which would be needed to cause
the compatibility fix to run. By running the compat fix on restore the
server will immediately have the compatible pseudo topology to use.

* lint: learn how to spell pseudo
2023-10-19 15:09:30 -05:00

59 lines
1.3 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package fingerprint
import (
"fmt"
log "github.com/hashicorp/go-hclog"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/shirou/gopsutil/v3/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
resp.NodeResources = &structs.NodeResources{
Memory: structs.NodeMemoryResources{
MemoryMB: memoryMB,
},
}
}
return nil
}