Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 23 additions & 66 deletions pkg/device/nvidia/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -748,90 +748,47 @@ func (nv *NvidiaGPUDevices) Fit(devices []*device.DeviceUsage, request device.Co
originReq := k.Nums
prevnuma := -1
klog.InfoS("Allocating device for container request", "pod", klog.KObj(pod), "card request", k)
var tmpDevs map[string]device.ContainerDevices
tmpDevs = make(map[string]device.ContainerDevices)
reason := make(map[string]int)
tmpDevs := make(map[string]device.ContainerDevices)
reasons := make(map[string]int)
needTopology := util.GetGPUSchedulerPolicyByPod(device.GPUSchedulerPolicy, pod) == util.GPUSchedulerPolicyTopology.String()
for i := len(devices) - 1; i >= 0; i-- {
dev := devices[i]
klog.V(4).InfoS("scoring pod", "pod", klog.KObj(pod), "device", dev.ID, "Memreq", k.Memreq, "MemPercentagereq", k.MemPercentagereq, "Coresreq", k.Coresreq, "Nums", k.Nums, "device index", i)
if !dev.Health {
reason[common.CardNotHealth]++
klog.V(5).InfoS(common.CardNotHealth, "pod", klog.KObj(pod), "device", dev.ID, "health", dev.Health)
if reason := checkCardHealth(dev, pod); reason != "" {
reasons[reason]++
continue
}
found, numa := nv.checkType(pod.GetAnnotations(), *dev, k)
if !found {
reason[common.CardTypeMismatch]++
reasons[common.CardTypeMismatch]++
klog.V(5).InfoS(common.CardTypeMismatch, "pod", klog.KObj(pod), "device", dev.ID, dev.Type, k.Type)
continue
}
if numa && prevnuma != dev.Numa {
if k.Nums != originReq {
reason[common.NumaNotFit] += len(tmpDevs)
reasons[common.NumaNotFit] += len(tmpDevs)
klog.V(5).InfoS(common.NumaNotFit, "pod", klog.KObj(pod), "device", dev.ID, "k.nums", k.Nums, "numa", numa, "prevnuma", prevnuma, "device numa", dev.Numa)
}
k.Nums = originReq
prevnuma = dev.Numa
tmpDevs = make(map[string]device.ContainerDevices)
}
if !device.CheckUUID(pod.GetAnnotations(), dev.ID, GPUUseUUID, GPUNoUseUUID, nv.CommonWord()) {
reason[common.CardUUIDMismatch]++
klog.V(5).InfoS(common.CardUUIDMismatch, "pod", klog.KObj(pod), "device", dev.ID, "current device info is:", *dev)
normalizeCoresreq(&k, pod, dev)
ctx := &cardCheckCtx{
request: &k,
pod: pod,
allocated: allocated,
tmpDevsMap: tmpDevs,
deviceType: k.Type,
commonWord: nv.CommonWord(),
nv: nv,
memreq: computeMemreq(k, dev),
deviceIndex: i,
}
if reason := runCardChecks(dev, ctx); reason != "" {
reasons[reason]++
continue
}

memreq := int32(0)
if dev.Count <= dev.Used {
reason[common.CardTimeSlicingExhausted]++
klog.V(5).InfoS(common.CardTimeSlicingExhausted, "pod", klog.KObj(pod), "device", dev.ID, "count", dev.Count, "used", dev.Used)
continue
}
if k.Coresreq > 100 {
klog.ErrorS(nil, "core limit can't exceed 100", "pod", klog.KObj(pod), "device", dev.ID)
k.Coresreq = 100
//return false, tmpDevs
}
if k.Memreq > 0 {
memreq = k.Memreq
}
if k.MemPercentagereq != 101 && k.Memreq == 0 {
//This incurs an issue
memreq = dev.Totalmem * k.MemPercentagereq / 100
}
if !fitQuota(tmpDevs, allocated, pod.Namespace, int64(memreq), int64(k.Coresreq)) {
reason[common.ResourceQuotaNotFit]++
klog.V(3).InfoS(common.ResourceQuotaNotFit, "pod", pod.Name, "memreq", memreq, "coresreq", k.Coresreq)
continue
}
if dev.Totalmem-dev.Usedmem < memreq {
reason[common.CardInsufficientMemory]++
klog.V(5).InfoS(common.CardInsufficientMemory, "pod", klog.KObj(pod), "device", dev.ID, "device index", i, "device total memory", dev.Totalmem, "device used memory", dev.Usedmem, "request memory", memreq)
continue
}
if dev.Totalcore-dev.Usedcores < k.Coresreq {
reason[common.CardInsufficientCore]++
klog.V(5).InfoS(common.CardInsufficientCore, "pod", klog.KObj(pod), "device", dev.ID, "device index", i, "device total core", dev.Totalcore, "device used core", dev.Usedcores, "request cores", k.Coresreq)
continue
}
// Coresreq=100 indicates it want this card exclusively
if dev.Totalcore == 100 && k.Coresreq == 100 && dev.Used > 0 {
reason[common.ExclusiveDeviceAllocateConflict]++
klog.V(5).InfoS(common.ExclusiveDeviceAllocateConflict, "pod", klog.KObj(pod), "device", dev.ID, "device index", i, "used", dev.Used)
continue
}
// You can't allocate core=0 job to an already full GPU
if dev.Totalcore != 0 && dev.Usedcores == dev.Totalcore && k.Coresreq == 0 {
reason[common.CardComputeUnitsExhausted]++
klog.V(5).InfoS(common.CardComputeUnitsExhausted, "pod", klog.KObj(pod), "device", dev.ID, "device index", i)
continue
}
if !nv.CustomFilterRule(allocated, request, tmpDevs[k.Type], dev) {
reason[common.CardNotFoundCustomFilterRule]++
klog.V(5).InfoS(common.CardNotFoundCustomFilterRule, "pod", klog.KObj(pod), "device", dev.ID, "device index", i)
continue
}

if k.Nums > 0 {
klog.V(5).InfoS("find fit device", "pod", klog.KObj(pod), "device", dev.ID)
if !needTopology {
Expand All @@ -841,7 +798,7 @@ func (nv *NvidiaGPUDevices) Fit(devices []*device.DeviceUsage, request device.Co
Idx: int(dev.Index),
UUID: dev.ID,
Type: k.Type,
Usedmem: memreq,
Usedmem: ctx.memreq,
Usedcores: k.Coresreq,
})
}
Expand Down Expand Up @@ -875,10 +832,10 @@ func (nv *NvidiaGPUDevices) Fit(devices []*device.DeviceUsage, request device.Co
}
}
if len(tmpDevs) > 0 {
reason[common.AllocatedCardsInsufficientRequest] = len(tmpDevs)
reasons[common.AllocatedCardsInsufficientRequest] = len(tmpDevs)
klog.V(5).InfoS(common.AllocatedCardsInsufficientRequest, "pod", klog.KObj(pod), "request", originReq, "allocated", len(tmpDevs))
}
return false, tmpDevs, common.GenReason(reason, len(devices))
return false, tmpDevs, common.GenReason(reasons, len(devices))
}

func (dev *NvidiaGPUDevices) GetResourceNames() device.ResourceNames {
Expand Down
139 changes: 139 additions & 0 deletions pkg/device/nvidia/fit_bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
Copyright 2026 The HAMi Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package nvidia

import (
"strconv"
"testing"

corev1 "k8s.io/api/core/v1"

"github.com/Project-HAMi/HAMi/pkg/device"
)

func makeBenchmarkDevices(n int) []*device.DeviceUsage {
devices := make([]*device.DeviceUsage, n)
for i := range n {
devices[i] = &device.DeviceUsage{
ID: "dev-" + strconv.Itoa(i),
Index: uint(i),
Used: 0,
Count: 10,
Usedmem: 0,
Totalmem: 32768,
Usedcores: 0,
Totalcore: 100,
Type: NvidiaGPUDevice,
Health: true,
}
}
return devices
}

// registerBenchmarkDevice installs nv into the global DevicesMap so that the
// fitQuota path takes its real branch (rather than the early-return when the
// device is unregistered). This makes benchmark numbers reflect production.
func registerBenchmarkDevice(nv *NvidiaGPUDevices) {
device.DevicesMap = map[string]device.Devices{NvidiaGPUDevice: nv}
}

func BenchmarkFit_SingleCardFromEight(b *testing.B) {
nv := InitNvidiaDevice(NvidiaConfig{
ResourceCountName: "nvidia.com/gpu",
ResourceMemoryName: "nvidia.com/gpumem",
ResourceCoreName: "nvidia.com/gpucores",
})
registerBenchmarkDevice(nv)
devices := makeBenchmarkDevices(8)
req := device.ContainerDeviceRequest{Nums: 1, Memreq: 1024, Coresreq: 10, Type: NvidiaGPUDevice}
pod := &corev1.Pod{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _, _ = nv.Fit(devices, req, pod, &device.NodeInfo{}, &device.PodDevices{})
}
}

func BenchmarkFit_FourCardsFromEight(b *testing.B) {
nv := InitNvidiaDevice(NvidiaConfig{
ResourceCountName: "nvidia.com/gpu",
ResourceMemoryName: "nvidia.com/gpumem",
ResourceCoreName: "nvidia.com/gpucores",
})
registerBenchmarkDevice(nv)
devices := makeBenchmarkDevices(8)
req := device.ContainerDeviceRequest{Nums: 4, Memreq: 1024, Coresreq: 10, Type: NvidiaGPUDevice}
pod := &corev1.Pod{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _, _ = nv.Fit(devices, req, pod, &device.NodeInfo{}, &device.PodDevices{})
}
}

func BenchmarkFit_AllCardsFail(b *testing.B) {
nv := InitNvidiaDevice(NvidiaConfig{
ResourceCountName: "nvidia.com/gpu",
ResourceMemoryName: "nvidia.com/gpumem",
ResourceCoreName: "nvidia.com/gpucores",
})
registerBenchmarkDevice(nv)
devices := makeBenchmarkDevices(8)
for _, d := range devices {
d.Totalmem = 100
d.Usedmem = 100
}
req := device.ContainerDeviceRequest{Nums: 1, Memreq: 1024, Coresreq: 10, Type: NvidiaGPUDevice}
pod := &corev1.Pod{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _, _ = nv.Fit(devices, req, pod, &device.NodeInfo{}, &device.PodDevices{})
}
}

func BenchmarkRunCardChecks_AllPass(b *testing.B) {
nv := InitNvidiaDevice(NvidiaConfig{
ResourceCountName: "nvidia.com/gpu",
ResourceMemoryName: "nvidia.com/gpumem",
ResourceCoreName: "nvidia.com/gpucores",
})
registerBenchmarkDevice(nv)
dev := &device.DeviceUsage{
ID: "dev-0",
Health: true,
Count: 10,
Used: 0,
Totalmem: 32768,
Usedmem: 0,
Totalcore: 100,
Usedcores: 0,
Type: NvidiaGPUDevice,
}
req := device.ContainerDeviceRequest{Type: NvidiaGPUDevice, Memreq: 1024, Coresreq: 10}
tmpDevs := make(map[string]device.ContainerDevices)
ctx := &cardCheckCtx{
request: &req,
pod: &corev1.Pod{},
tmpDevsMap: tmpDevs,
deviceType: NvidiaGPUDevice,
commonWord: nv.CommonWord(),
nv: nv,
memreq: 1024,
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = runCardChecks(dev, ctx)
}
}
Loading
Loading