Files
nomad/client/lib/cgroupslib/partition.go
Seth Hoenig 2e1974a574 client: refactor cpuset partitioning (#18371)
* client: refactor cpuset partitioning

This PR updates the way Nomad client manages the split between tasks
that make use of resources.cpus vs. resources.cores.

Previously, each task was explicitly assigned which CPU cores they were
able to run on. Every time a task was started or destroyed, all other
tasks' cpusets would need to be updated. This was inefficient and would
crush the Linux kernel when a client would try to run ~400 or so tasks.

Now, we make use of cgroup heirarchy and cpuset inheritence to efficiently
manage cpusets.

* cr: tweaks for feedback
2023-09-12 09:11:11 -05:00

64 lines
1.6 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package cgroupslib
import (
"github.com/hashicorp/nomad/client/lib/idset"
"github.com/hashicorp/nomad/client/lib/numalib/hw"
)
// A Partition is used to track reserved vs. shared cpu cores.
type Partition interface {
Restore(*idset.Set[hw.CoreID])
Reserve(*idset.Set[hw.CoreID]) error
Release(*idset.Set[hw.CoreID]) error
}
// SharePartition is the name of the cgroup containing cgroups for tasks
// making use of the 'cpu' resource, which pools and shares cpu cores.
func SharePartition() string {
switch GetMode() {
case CG1:
return "share"
case CG2:
return "share.slice"
default:
return ""
}
}
// ReservePartition is the name of the cgroup containing cgroups for tasks
// making use of the 'cores' resource, which reserves specific cpu cores.
func ReservePartition() string {
switch GetMode() {
case CG1:
return "reserve"
case CG2:
return "reserve.slice"
default:
return ""
}
}
// GetPartitionFromCores returns the name of the cgroup that should contain
// the cgroup of the task, which is determined by inspecting the cores
// parameter, which is a non-empty string if the task is using reserved
// cores.
func GetPartitionFromCores(cores string) string {
if cores == "" {
return SharePartition()
}
return ReservePartition()
}
// GetPartitionFromBool returns the name of the cgroup that should contain
// the cgroup of the task, which is determined from the cores parameter,
// which indicates whether the task is making use of reserved cores.
func GetPartitionFromBool(cores bool) string {
if cores {
return ReservePartition()
}
return SharePartition()
}