Skip to content

Commit b5bd5bf

Browse files
committed
cmd/trace: fix overlappingDuration
Update #24081 Change-Id: Ieccfb03c51e86f35d4629a42959c80570bd93c33 Reviewed-on: https://go-review.googlesource.com/97555 Reviewed-by: Brad Fitzpatrick <[email protected]> Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent f8973fc commit b5bd5bf

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

src/cmd/trace/annotations.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -463,10 +463,10 @@ func overlappingDuration(start1, end1, start2, end2 int64) time.Duration {
463463
return 0
464464
}
465465

466-
if start1 > start2 {
466+
if start1 < start2 { // choose the later one
467467
start1 = start2
468468
}
469-
if end1 > end2 {
469+
if end1 > end2 { // choose the earlier one
470470
end1 = end2
471471
}
472472
return time.Duration(end1 - start1)

src/cmd/trace/annotations_test.go

+35-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,33 @@ import (
1717

1818
var saveTraces = flag.Bool("savetraces", false, "save traces collected by tests")
1919

20+
func TestOverlappingDuration(t *testing.T) {
21+
cases := []struct {
22+
start0, end0, start1, end1 int64
23+
want time.Duration
24+
}{
25+
{
26+
1, 10, 11, 20, 0,
27+
},
28+
{
29+
1, 10, 5, 20, 5 * time.Nanosecond,
30+
},
31+
{
32+
1, 10, 2, 8, 6 * time.Nanosecond,
33+
},
34+
}
35+
36+
for _, tc := range cases {
37+
s0, e0, s1, e1 := tc.start0, tc.end0, tc.start1, tc.end1
38+
if got := overlappingDuration(s0, e0, s1, e1); got != tc.want {
39+
t.Errorf("overlappingDuration(%d, %d, %d, %d)=%v; want %v", s0, e0, s1, e1, got, tc.want)
40+
}
41+
if got := overlappingDuration(s1, e1, s0, e0); got != tc.want {
42+
t.Errorf("overlappingDuration(%d, %d, %d, %d)=%v; want %v", s1, e1, s0, e0, got, tc.want)
43+
}
44+
}
45+
}
46+
2047
// prog0 starts three goroutines.
2148
//
2249
// goroutine 1: taskless span
@@ -247,14 +274,18 @@ func TestAnalyzeAnnotationGC(t *testing.T) {
247274
switch task.name {
248275
case "taskWithGC":
249276
if got <= 0 || got >= gcTime {
250-
t.Errorf("%s reported %v as overlapping GC time; want (0, %v): %v", task.name, got, gcTime, task)
277+
t.Errorf("%s reported %v as overlapping GC time; want (0, %v):\n%v", task.name, got, gcTime, task)
251278
buf := new(bytes.Buffer)
252279
fmt.Fprintln(buf, "GC Events")
253280
for _, ev := range res.gcEvents {
254-
fmt.Fprintf(buf, " %s\n", ev)
281+
fmt.Fprintf(buf, " %s -> %s\n", ev, ev.Link)
282+
}
283+
fmt.Fprintln(buf, "Events in Task")
284+
for i, ev := range task.events {
285+
fmt.Fprintf(buf, " %d: %s\n", i, ev)
255286
}
256-
fmt.Fprintf(buf, "%s\n", task)
257-
t.Logf("%s", buf)
287+
288+
t.Logf("\n%s", buf)
258289
}
259290
case "taskWithoutGC":
260291
if got != 0 {

0 commit comments

Comments
 (0)