Skip to content

Commit 273aeab

Browse files
committed
cache: add a few more fields to ref trace logs.
The trace logs now also include the pointer value of the ref, which serves as its "ID" amongst all the refs for the underlying record. They also now include equal{Mutable,Immutable} IDs, which are otherwise hard to determine with the Usage API. Finally, LazyStackTrace now has a MarshalText method which returns the same value as the existing String method. This allows it to work with custom logrus hooks that rely on marshaling the fields. Signed-off-by: Erik Sipsma <[email protected]>
1 parent d0ec935 commit 273aeab

File tree

2 files changed

+45
-24
lines changed

2 files changed

+45
-24
lines changed

cache/refs.go

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
digest "github.com/opencontainers/go-digest"
3636
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
3737
"github.com/pkg/errors"
38+
"github.com/sirupsen/logrus"
3839
"golang.org/x/sync/errgroup"
3940
)
4041

@@ -107,12 +108,7 @@ func (cr *cacheRecord) ref(triggerLastUsed bool, descHandlers DescHandlers, pg p
107108
progress: pg,
108109
}
109110
cr.refs[ref] = struct{}{}
110-
bklog.G(context.TODO()).WithFields(map[string]any{
111-
"id": cr.ID(),
112-
"newRefCount": len(cr.refs),
113-
"mutable": false,
114-
"stack": bklog.LazyStackTrace{},
115-
}).Trace("acquired cache ref")
111+
bklog.G(context.TODO()).WithFields(ref.traceLogFields()).Trace("acquired cache ref")
116112
return ref
117113
}
118114

@@ -124,12 +120,7 @@ func (cr *cacheRecord) mref(triggerLastUsed bool, descHandlers DescHandlers) *mu
124120
descHandlers: descHandlers,
125121
}
126122
cr.refs[ref] = struct{}{}
127-
bklog.G(context.TODO()).WithFields(map[string]any{
128-
"id": cr.ID(),
129-
"newRefCount": len(cr.refs),
130-
"mutable": true,
131-
"stack": bklog.LazyStackTrace{},
132-
}).Trace("acquired cache ref")
123+
bklog.G(context.TODO()).WithFields(ref.traceLogFields()).Trace("acquired cache ref")
133124
return ref
134125
}
135126

@@ -493,6 +484,24 @@ type immutableRef struct {
493484
progress progress.Controller
494485
}
495486

487+
// hold ref lock before calling
488+
func (sr *immutableRef) traceLogFields() logrus.Fields {
489+
m := map[string]any{
490+
"id": sr.ID(),
491+
"refID": fmt.Sprintf("%p", sr),
492+
"newRefCount": len(sr.refs),
493+
"mutable": false,
494+
"stack": bklog.LazyStackTrace{},
495+
}
496+
if sr.equalMutable != nil {
497+
m["equalMutableID"] = sr.equalMutable.ID()
498+
}
499+
if sr.equalImmutable != nil {
500+
m["equalImmutableID"] = sr.equalImmutable.ID()
501+
}
502+
return m
503+
}
504+
496505
// Order is from parent->child, sr will be at end of slice. Refs should not
497506
// be released as they are used internally in the underlying cacheRecords.
498507
func (sr *immutableRef) layerChain() []*immutableRef {
@@ -615,6 +624,24 @@ type mutableRef struct {
615624
descHandlers DescHandlers
616625
}
617626

627+
// hold ref lock before calling
628+
func (sr *mutableRef) traceLogFields() logrus.Fields {
629+
m := map[string]any{
630+
"id": sr.ID(),
631+
"refID": fmt.Sprintf("%p", sr),
632+
"newRefCount": len(sr.refs),
633+
"mutable": true,
634+
"stack": bklog.LazyStackTrace{},
635+
}
636+
if sr.equalMutable != nil {
637+
m["equalMutableID"] = sr.equalMutable.ID()
638+
}
639+
if sr.equalImmutable != nil {
640+
m["equalImmutableID"] = sr.equalImmutable.ID()
641+
}
642+
return m
643+
}
644+
618645
func (sr *mutableRef) DescHandler(dgst digest.Digest) *DescHandler {
619646
return sr.descHandlers[dgst]
620647
}
@@ -1320,12 +1347,7 @@ func (sr *immutableRef) updateLastUsedNow() bool {
13201347

13211348
func (sr *immutableRef) release(ctx context.Context) (rerr error) {
13221349
defer func() {
1323-
l := bklog.G(ctx).WithFields(map[string]any{
1324-
"id": sr.ID(),
1325-
"newRefCount": len(sr.refs),
1326-
"mutable": false,
1327-
"stack": bklog.LazyStackTrace{},
1328-
})
1350+
l := bklog.G(ctx).WithFields(sr.traceLogFields())
13291351
if rerr != nil {
13301352
l = l.WithError(rerr)
13311353
}
@@ -1514,12 +1536,7 @@ func (sr *mutableRef) Release(ctx context.Context) error {
15141536

15151537
func (sr *mutableRef) release(ctx context.Context) (rerr error) {
15161538
defer func() {
1517-
l := bklog.G(ctx).WithFields(map[string]any{
1518-
"id": sr.ID(),
1519-
"newRefCount": len(sr.refs),
1520-
"mutable": true,
1521-
"stack": bklog.LazyStackTrace{},
1522-
})
1539+
l := bklog.G(ctx).WithFields(sr.traceLogFields())
15231540
if rerr != nil {
15241541
l = l.WithError(rerr)
15251542
}

util/bklog/log.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,7 @@ type LazyStackTrace struct{}
7070
func (LazyStackTrace) String() string {
7171
return string(debug.Stack())
7272
}
73+
74+
func (LazyStackTrace) MarshalText() ([]byte, error) {
75+
return debug.Stack(), nil
76+
}

0 commit comments

Comments
 (0)