task- fix deadlock and mac gpu ct

This commit is contained in:
Andrew Jackson (Ajax)
2023-10-31 17:13:16 -05:00
parent 1ff0d61adb
commit e37c874004
5 changed files with 45 additions and 84 deletions
+1 -22
View File
@@ -4,7 +4,6 @@ import (
"context"
"fmt"
"strconv"
"sync"
"sync/atomic"
"time"
@@ -100,7 +99,6 @@ type TaskEngine struct {
ctx context.Context
handlers []*taskTypeHandler
db *harmonydb.DB
workAdderMutex sync.Mutex
reg *resources.Reg
grace context.CancelFunc
taskMap map[string]*taskTypeHandler
@@ -289,33 +287,14 @@ func (e *TaskEngine) pollerTryAllWork() {
}
}
// ResourcesAvailable determines what resources are still unassigned.
func (e *TaskEngine) ResourcesAvailable() resources.Resources {
e.workAdderMutex.Lock()
defer e.workAdderMutex.Unlock()
return e.resoourcesAvailable()
}
// resoourcesAvailable requires workAdderMutex to be already locked.
func (e *TaskEngine) resoourcesAvailable() resources.Resources {
tmp := e.reg.Resources
copy(tmp.GpuRam, e.reg.Resources.GpuRam)
for _, t := range e.handlers {
ct := t.Count.Load()
tmp.Cpu -= int(ct) * t.Cost.Cpu
tmp.Gpu -= float64(ct) * t.Cost.Gpu
tmp.Ram -= uint64(ct) * t.Cost.Ram
if len(t.Cost.GpuRam) == 0 {
continue
}
for i := int32(0); i < ct; i++ {
for grIdx, j := range tmp.GpuRam {
if j > t.Cost.GpuRam[0] {
tmp.GpuRam[grIdx] = 0 // Only 1 per GPU. j - t.Cost.GpuRam[0]
break
}
}
log.Warn("We should never get out of gpuram for what's consumed.")
}
}
return tmp
}
+8 -17
View File
@@ -10,7 +10,6 @@ import (
"time"
logging "github.com/ipfs/go-log/v2"
"github.com/samber/lo"
"github.com/filecoin-project/lotus/lib/harmony/harmonydb"
)
@@ -50,6 +49,11 @@ func (h *taskTypeHandler) AddTask(extra func(TaskID, *harmonydb.Tx) (bool, error
}
}
// considerWork is called to attempt to start work on a task-id of this task type.
// It presumes single-threaded calling, so there should not be a multi-threaded re-entry.
// The only caller should be the one work poller thread. This does spin off other threads,
// but those should not considerWork. Work completing may lower the resource numbers
// unexpectedly, but that will not invalidate work being already able to fit.
func (h *taskTypeHandler) considerWork(from string, ids []TaskID) (workAccepted bool) {
top:
if len(ids) == 0 {
@@ -64,10 +68,8 @@ top:
return false
}
h.TaskEngine.workAdderMutex.Lock()
defer h.TaskEngine.workAdderMutex.Unlock()
// 2. Can we do any more work?
// 2. Can we do any more work? From here onward, we presume the resource
// story will not change, so single-threaded calling is best.
err := h.AssertMachineHasCapacity()
if err != nil {
log.Debugw("did not accept task", "name", h.Name, "reason", "at capacity already: "+err.Error())
@@ -213,7 +215,7 @@ func (h *taskTypeHandler) recordCompletion(tID TaskID, workStart time.Time, done
}
func (h *taskTypeHandler) AssertMachineHasCapacity() error {
r := h.TaskEngine.resoourcesAvailable()
r := h.TaskEngine.ResourcesAvailable()
if r.Cpu-h.Cost.Cpu < 0 {
return errors.New("Did not accept " + h.Name + " task: out of cpu")
@@ -224,16 +226,5 @@ func (h *taskTypeHandler) AssertMachineHasCapacity() error {
if r.Gpu-h.Cost.Gpu < 0 {
return errors.New("Did not accept " + h.Name + " task: out of available GPU")
}
gpuRamSum := lo.Sum(h.Cost.GpuRam)
if gpuRamSum == 0 {
goto enoughGpuRam
}
for _, u := range r.GpuRam {
if u >= gpuRamSum {
goto enoughGpuRam
}
}
return errors.New("Did not accept " + h.Name + " task: out of GPURam")
enoughGpuRam:
return nil
}