Skip to content

Commit ea00461

Browse files
committed
internal/trace: make Value follow reflect conventions
A previous change renamed Value.Uint64 to Value.ToUint64 to accomodate string values. The method for a string value is then Value.ToString, while the method for a debug string (for example, for fmt) is just called String, as per fmt.Stringer. This change follows a request from Dominik Honnef, maintainer of gotraceui, to make Value follow the conventions of the reflect package. The Value type there has a method String which fulfills both purposes: getting the string for a String Value, and as fmt.Stringer. It's not exactly pretty, but it does make sense to just stick to convention. Change-Id: I55b364be88088d2121527bffc833ef03dbdb9764 Reviewed-on: https://go-review.googlesource.com/c/go/+/680978 Reviewed-by: Florian Lehner <lehner.florian86@gmail.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Carlos Amedee <carlos@golang.org>
1 parent 96a6e14 commit ea00461

File tree

5 files changed

+23
-26
lines changed

5 files changed

+23
-26
lines changed

src/cmd/trace/gen.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,11 @@ func (g *globalMetricGenerator) GlobalMetric(ctx *traceContext, ev *trace.Event)
283283
m := ev.Metric()
284284
switch m.Name {
285285
case "/memory/classes/heap/objects:bytes":
286-
ctx.HeapAlloc(ctx.elapsed(ev.Time()), m.Value.ToUint64())
286+
ctx.HeapAlloc(ctx.elapsed(ev.Time()), m.Value.Uint64())
287287
case "/gc/heap/goal:bytes":
288-
ctx.HeapGoal(ctx.elapsed(ev.Time()), m.Value.ToUint64())
288+
ctx.HeapGoal(ctx.elapsed(ev.Time()), m.Value.Uint64())
289289
case "/sched/gomaxprocs:threads":
290-
ctx.Gomaxprocs(m.Value.ToUint64())
290+
ctx.Gomaxprocs(m.Value.Uint64())
291291
}
292292
}
293293

src/internal/trace/event.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"iter"
1010
"math"
11+
"strconv"
1112
"strings"
1213
"time"
1314

@@ -812,6 +813,10 @@ func (e Event) String() string {
812813
switch kind := e.Kind(); kind {
813814
case EventMetric:
814815
m := e.Metric()
816+
v := m.Value.String()
817+
if m.Value.Kind() == ValueString {
818+
v = strconv.Quote(v)
819+
}
815820
fmt.Fprintf(&sb, " Name=%q Value=%s", m.Name, m.Value)
816821
case EventLabel:
817822
l := e.Label()

src/internal/trace/gc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func MutatorUtilizationV2(events []Event, flags UtilFlags) [][]MutatorUtil {
103103
if m.Name != "/sched/gomaxprocs:threads" {
104104
break
105105
}
106-
gomaxprocs := int(m.Value.ToUint64())
106+
gomaxprocs := int(m.Value.Uint64())
107107
if len(ps) > gomaxprocs {
108108
if flags&UtilPerProc != 0 {
109109
// End each P's series.

src/internal/trace/testtrace/validation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func (v *Validator) Event(ev trace.Event) error {
135135
switch m.Value.Kind() {
136136
case trace.ValueUint64:
137137
// Just make sure it doesn't panic.
138-
_ = m.Value.ToUint64()
138+
_ = m.Value.Uint64()
139139
}
140140
case trace.EventLabel:
141141
l := ev.Label()

src/internal/trace/value.go

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,27 @@ func (v Value) Kind() ValueKind {
3535
return v.kind
3636
}
3737

38-
// ToUint64 returns the uint64 value for a ValueUint64.
38+
// Uint64 returns the uint64 value for a ValueUint64.
3939
//
4040
// Panics if this Value's Kind is not ValueUint64.
41-
func (v Value) ToUint64() uint64 {
41+
func (v Value) Uint64() uint64 {
4242
if v.kind != ValueUint64 {
43-
panic("ToUint64 called on Value of a different Kind")
43+
panic("Uint64 called on Value of a different Kind")
4444
}
4545
return v.scalar
4646
}
4747

48-
// ToString returns the uint64 value for a ValueString.
49-
//
50-
// Panics if this Value's Kind is not ValueString.
51-
func (v Value) ToString() string {
52-
if v.kind != ValueString {
53-
panic("ToString called on Value of a different Kind")
48+
// String returns the string value for a ValueString, and otherwise
49+
// a string representation of the value for other kinds of values.
50+
func (v Value) String() string {
51+
if v.kind == ValueString {
52+
return unsafe.String((*byte)(v.pointer), int(v.scalar))
53+
}
54+
switch v.kind {
55+
case ValueUint64:
56+
return fmt.Sprintf("Value{Uint64(%d)}", v.Uint64())
5457
}
55-
return unsafe.String((*byte)(v.pointer), int(v.scalar))
58+
return "Value{Bad}"
5659
}
5760

5861
func uint64Value(x uint64) Value {
@@ -62,14 +65,3 @@ func uint64Value(x uint64) Value {
6265
func stringValue(s string) Value {
6366
return Value{kind: ValueString, scalar: uint64(len(s)), pointer: unsafe.Pointer(unsafe.StringData(s))}
6467
}
65-
66-
// String returns the string representation of the value.
67-
func (v Value) String() string {
68-
switch v.Kind() {
69-
case ValueUint64:
70-
return fmt.Sprintf("Value{Uint64(%d)}", v.ToUint64())
71-
case ValueString:
72-
return fmt.Sprintf("Value{String(%s)}", v.ToString())
73-
}
74-
return "Value{Bad}"
75-
}

0 commit comments

Comments
 (0)