mirror of
https://github.com/go-delve/delve.git
synced 2025-10-27 20:23:41 +08:00
cache computed labels
This commit is contained in:
committed by
Derek Parker
parent
ca3fe88899
commit
7fe81ca1d6
@ -2310,17 +2310,18 @@ func TestGoroutineLables(t *testing.T) {
|
||||
assertNoError(proc.Continue(p), t, "Continue()")
|
||||
g, err := proc.GetG(p.CurrentThread())
|
||||
assertNoError(err, t, "GetG()")
|
||||
if len(g.Labels) != 0 {
|
||||
if len(g.Labels()) != 0 {
|
||||
t.Fatalf("No labels expected")
|
||||
}
|
||||
|
||||
assertNoError(proc.Continue(p), t, "Continue()")
|
||||
g, err = proc.GetG(p.CurrentThread())
|
||||
assertNoError(err, t, "GetG()")
|
||||
if v := g.Labels["k1"]; v != "v1" {
|
||||
labels := g.Labels()
|
||||
if v := labels["k1"]; v != "v1" {
|
||||
t.Errorf("Unexpected label value k1=%v", v)
|
||||
}
|
||||
if v := g.Labels["k2"]; v != "v2" {
|
||||
if v := labels["k2"]; v != "v2" {
|
||||
t.Errorf("Unexpected label value k2=%v", v)
|
||||
}
|
||||
})
|
||||
|
||||
@ -211,7 +211,7 @@ type G struct {
|
||||
|
||||
Unreadable error // could not read the G struct
|
||||
|
||||
Labels map[string]string // G's pprof labels
|
||||
labels *map[string]string // G's pprof labels, computed on demand in Labels() method
|
||||
}
|
||||
|
||||
// Defer returns the top-most defer of the goroutine.
|
||||
@ -268,6 +268,32 @@ func (g *G) StartLoc() Location {
|
||||
return Location{PC: g.StartPC, File: f, Line: l, Fn: fn}
|
||||
}
|
||||
|
||||
func (g *G) Labels() map[string]string {
|
||||
if g.labels != nil {
|
||||
return *g.labels
|
||||
}
|
||||
var labels map[string]string
|
||||
if labelsVar := g.variable.loadFieldNamed("labels"); labelsVar != nil && len(labelsVar.Children) == 1 {
|
||||
if address := labelsVar.Children[0]; address.Addr != 0 {
|
||||
labelMapType, _ := g.variable.bi.findType("runtime/pprof.labelMap")
|
||||
if labelMapType != nil {
|
||||
labelMap := newVariable("", address.Addr, labelMapType, g.variable.bi, g.variable.mem)
|
||||
labelMap.loadValue(loadFullValue)
|
||||
labels = map[string]string{}
|
||||
for i := range labelMap.Children {
|
||||
if i%2 == 0 {
|
||||
k := labelMap.Children[i]
|
||||
v := labelMap.Children[i+1]
|
||||
labels[constant.StringVal(k.Value)] = constant.StringVal(v.Value)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
g.labels = &labels
|
||||
return *g.labels
|
||||
}
|
||||
|
||||
type Ancestor struct {
|
||||
ID int64 // Goroutine ID
|
||||
Unreadable error
|
||||
@ -561,26 +587,6 @@ func (v *Variable) parseG() (*G, error) {
|
||||
status, _ := constant.Int64Val(v.fieldVariable("atomicstatus").Value)
|
||||
f, l, fn := v.bi.PCToLine(uint64(pc))
|
||||
|
||||
var labels map[string]string
|
||||
if labelsVar := v.loadFieldNamed("labels"); labelsVar != nil && len(labelsVar.Children) == 1 {
|
||||
if address := labelsVar.Children[0]; address.Addr != 0 {
|
||||
labelMapType, _ := v.bi.findType("runtime/pprof.labelMap")
|
||||
if labelMapType != nil {
|
||||
labelMap := newVariable("", address.Addr, labelMapType, v.bi, v.mem)
|
||||
labelMap.loadValue(loadFullValue)
|
||||
labels = map[string]string{}
|
||||
// iterate through map as it is done in other places
|
||||
for i := range labelMap.Children {
|
||||
if i % 2 == 0 {
|
||||
k := labelMap.Children[i]
|
||||
v := labelMap.Children[i+1]
|
||||
labels[constant.StringVal(k.Value)] = constant.StringVal(v.Value)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
g := &G{
|
||||
ID: int(id),
|
||||
GoPC: uint64(gopc),
|
||||
@ -596,7 +602,6 @@ func (v *Variable) parseG() (*G, error) {
|
||||
stkbarPos: int(stkbarPos),
|
||||
stackhi: stackhi,
|
||||
stacklo: stacklo,
|
||||
Labels: labels,
|
||||
}
|
||||
return g, nil
|
||||
}
|
||||
|
||||
@ -263,7 +263,7 @@ func ConvertGoroutine(g *proc.G) *Goroutine {
|
||||
GoStatementLoc: ConvertLocation(g.Go()),
|
||||
StartLoc: ConvertLocation(g.StartLoc()),
|
||||
ThreadID: tid,
|
||||
Labels: g.Labels,
|
||||
Labels: g.Labels(),
|
||||
}
|
||||
if g.Unreadable != nil {
|
||||
r.Unreadable = g.Unreadable.Error()
|
||||
|
||||
Reference in New Issue
Block a user