From 976cd1b7d4a1c34e97fb20d6f504d970a342295f Mon Sep 17 00:00:00 2001 From: Tim Gross Date: Tue, 21 Dec 2021 11:37:03 -0500 Subject: [PATCH] scheduler: cache resource comparisons for each alloc During scheduling, a given allocation will potentially be compared for fit against a large number of nodes, particularly if the job has `spread` blocks or the cluster is large and few nodes meet the feasibility. The scheduler recalculates the `ComparableResources` struct on the allocation for each node compared against, but this data doesn't change for this evaluation. Cache this information to reduce allocations and CPU time. This patch was benchmarked on a large cluster with a `spread` job and reduced heap allocations by ~16MB (which only amounts to ~2% of CPU time, but can't hurt). --- nomad/structs/funcs.go | 6 +++++- nomad/structs/structs.go | 6 ++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/nomad/structs/funcs.go b/nomad/structs/funcs.go index 1820443a7b9..d61313b7220 100644 --- a/nomad/structs/funcs.go +++ b/nomad/structs/funcs.go @@ -158,7 +158,11 @@ func AllocsFit(node *Node, allocs []*Allocation, netIdx *NetworkIndex, checkDevi continue } - cr := alloc.ComparableResources() + if alloc.comparableResources == nil { + alloc.comparableResources = alloc.ComparableResources() + } + + cr := alloc.comparableResources used.Add(cr) // Adding the comparable resource unions reserved core sets, need to check if reserved cores overlap diff --git a/nomad/structs/structs.go b/nomad/structs/structs.go index b856b9ddb6c..cd707c58ecb 100644 --- a/nomad/structs/structs.go +++ b/nomad/structs/structs.go @@ -9341,6 +9341,12 @@ type Allocation struct { // ModifyTime is the time the allocation was last updated. ModifyTime int64 + + // comparableResources is a cache of calculated resources to + // reduce work done by the scheduler during node fit checking in + // AllocsFit. Note: this should never be updated to be visible + // outside this package, so that it's not serialized to disk. + comparableResources *ComparableResources } // ConsulNamespace returns the Consul namespace of the task group associated