Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solver: allow debugging specific builder steps #4410

Merged
merged 1 commit into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
32 changes: 28 additions & 4 deletions solver/edge.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package solver

import (
"context"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -33,6 +34,28 @@ func newEdge(ed Edge, op activeOp, index *edgeIndex) *edge {
cacheRecords: map[string]*CacheRecord{},
cacheRecordsLoaded: map[string]struct{}{},
index: index,
debug: debugScheduler,
}
if !e.debug && len(debugSchedulerSteps) > 0 {
withParents := strings.HasSuffix(debugSchedulerSteps[0], "^")
name := strings.TrimSuffix(debugSchedulerSteps[0], "^")
for _, v := range debugSchedulerSteps {
if strings.Contains(name, v) {
e.debug = true
break
}
}
if !e.debug && withParents {
for _, vtx := range ed.Vertex.Inputs() {
name := strings.TrimSuffix(vtx.Vertex.Name(), "^")
for _, v := range debugSchedulerSteps {
if strings.Contains(name, v) {
e.debug = true
break
}
}
}
}
Comment on lines +39 to +58
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have some examples?

iiuc it could smth like BUILDKIT_SCHEDULER_DEBUG_STEPS=unpark,^status?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BUILDKIT_SCHEDULER_DEBUG_STEPS=COPY foo bar^

}
return e
}
Expand Down Expand Up @@ -71,6 +94,7 @@ type edge struct {
secondaryExporters []expDep

failedOnce sync.Once
debug bool
}

// dep holds state for a dependant edge
Expand Down Expand Up @@ -179,15 +203,15 @@ func (e *edge) finishIncoming(req pipe.Sender) {
if req.Request().Canceled && err == nil {
err = context.Canceled
}
if debugScheduler {
if e.debug {
bklog.G(context.TODO()).Debugf("finishIncoming %s %v %#v desired=%s", e.edge.Vertex.Name(), err, e.edgeState, req.Request().Payload.(*edgeRequest).desiredState)
}
req.Finalize(&e.edgeState, err)
}

// updateIncoming updates the current value of incoming pipe request
func (e *edge) updateIncoming(req pipe.Sender) {
if debugScheduler {
if e.debug {
bklog.G(context.TODO()).Debugf("updateIncoming %s %#v desired=%s", e.edge.Vertex.Name(), e.edgeState, req.Request().Payload.(*edgeRequest).desiredState)
}
req.Update(&e.edgeState)
Expand Down Expand Up @@ -683,7 +707,7 @@ func (e *edge) recalcCurrentState() {
}
if len(openKeys) == 0 {
e.state = edgeStatusCacheSlow
if debugScheduler {
if e.debug {
bklog.G(context.TODO()).Debugf("upgrade to cache-slow because no open keys")
}
}
Expand Down Expand Up @@ -722,7 +746,7 @@ func (e *edge) respondToIncoming(incoming []pipe.Sender, allPipes []pipe.Receive
allIncomingCanComplete = false
}

if debugScheduler {
if e.debug {
bklog.G(context.TODO()).Debugf("status state=%s cancomplete=%v hasouts=%v noPossibleCache=%v depsCacheFast=%v keys=%d cacheRecords=%d", e.state, allIncomingCanComplete, e.hasActiveOutgoing, e.noCacheMatchPossible, e.allDepsCompletedCacheFast, len(e.keys), len(e.cacheRecords))
}

Expand Down
22 changes: 18 additions & 4 deletions solver/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package solver

import (
"context"
"encoding/csv"
"os"
"strings"
"sync"

"github.com/moby/buildkit/solver/internal/pipe"
Expand All @@ -12,6 +14,8 @@ import (
)

var debugScheduler = false // TODO: replace with logs in build trace
var debugSchedulerSteps []string
var debugSchedulerStepsParseOnce sync.Once

func init() {
if os.Getenv("BUILDKIT_SCHEDULER_DEBUG") == "1" {
Expand Down Expand Up @@ -68,6 +72,16 @@ func (s *scheduler) Stop() {
}

func (s *scheduler) loop() {
debugSchedulerStepsParseOnce.Do(func() {
if s := os.Getenv("BUILDKIT_SCHEDULER_DEBUG_STEPS"); s != "" {
fields, err := csv.NewReader(strings.NewReader(s)).Read()
if err != nil {
return
}
debugSchedulerSteps = fields
}
})

defer func() {
close(s.closed)
}()
Expand Down Expand Up @@ -130,11 +144,11 @@ func (s *scheduler) dispatch(e *edge) {
pf := &pipeFactory{s: s, e: e}

// unpark the edge
if debugScheduler {
if e.debug {
debugSchedulerPreUnpark(e, inc, updates, out)
}
e.unpark(inc, updates, out, pf)
if debugScheduler {
if e.debug {
debugSchedulerPostUnpark(e, inc)
}

Expand Down Expand Up @@ -352,15 +366,15 @@ func (pf *pipeFactory) NewInputRequest(ee Edge, req *edgeRequest) pipe.Receiver
})
}
p := pf.s.newPipe(target, pf.e, pipe.Request{Payload: req})
if debugScheduler {
if pf.e.debug {
bklog.G(context.TODO()).Debugf("> newPipe %s %p desiredState=%s", ee.Vertex.Name(), p, req.desiredState)
}
return p.Receiver
}

func (pf *pipeFactory) NewFuncRequest(f func(context.Context) (interface{}, error)) pipe.Receiver {
p := pf.s.newRequestWithFunc(pf.e, f)
if debugScheduler {
if pf.e.debug {
bklog.G(context.TODO()).Debugf("> newFunc %p", p)
}
return p
Expand Down