]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/trace: fix overlappingDuration
authorHana Kim <hakim@google.com>
Mon, 26 Feb 2018 21:40:25 +0000 (16:40 -0500)
committerHyang-Ah Hana Kim <hyangah@gmail.com>
Wed, 28 Feb 2018 02:42:15 +0000 (02:42 +0000)
Update #24081

Change-Id: Ieccfb03c51e86f35d4629a42959c80570bd93c33
Reviewed-on: https://go-review.googlesource.com/97555
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/cmd/trace/annotations.go
src/cmd/trace/annotations_test.go

index f2c4440a9af7460fff102f64a56c34da279ed217..91cdd4d1980c6013372b01797f210c104f066079 100644 (file)
@@ -463,10 +463,10 @@ func overlappingDuration(start1, end1, start2, end2 int64) time.Duration {
                return 0
        }
 
-       if start1 > start2 {
+       if start1 < start2 { // choose the later one
                start1 = start2
        }
-       if end1 > end2 {
+       if end1 > end2 { // choose the earlier one
                end1 = end2
        }
        return time.Duration(end1 - start1)
index 539ad81ecbdc236a18ecec189eada25bf59005e0..c9432846e25960336d25de5404ad921b21beff79 100644 (file)
@@ -17,6 +17,33 @@ import (
 
 var saveTraces = flag.Bool("savetraces", false, "save traces collected by tests")
 
+func TestOverlappingDuration(t *testing.T) {
+       cases := []struct {
+               start0, end0, start1, end1 int64
+               want                       time.Duration
+       }{
+               {
+                       1, 10, 11, 20, 0,
+               },
+               {
+                       1, 10, 5, 20, 5 * time.Nanosecond,
+               },
+               {
+                       1, 10, 2, 8, 6 * time.Nanosecond,
+               },
+       }
+
+       for _, tc := range cases {
+               s0, e0, s1, e1 := tc.start0, tc.end0, tc.start1, tc.end1
+               if got := overlappingDuration(s0, e0, s1, e1); got != tc.want {
+                       t.Errorf("overlappingDuration(%d, %d, %d, %d)=%v; want %v", s0, e0, s1, e1, got, tc.want)
+               }
+               if got := overlappingDuration(s1, e1, s0, e0); got != tc.want {
+                       t.Errorf("overlappingDuration(%d, %d, %d, %d)=%v; want %v", s1, e1, s0, e0, got, tc.want)
+               }
+       }
+}
+
 // prog0 starts three goroutines.
 //
 //   goroutine 1: taskless span
@@ -247,14 +274,18 @@ func TestAnalyzeAnnotationGC(t *testing.T) {
                switch task.name {
                case "taskWithGC":
                        if got <= 0 || got >= gcTime {
-                               t.Errorf("%s reported %v as overlapping GC time; want (0, %v): %v", task.name, got, gcTime, task)
+                               t.Errorf("%s reported %v as overlapping GC time; want (0, %v):\n%v", task.name, got, gcTime, task)
                                buf := new(bytes.Buffer)
                                fmt.Fprintln(buf, "GC Events")
                                for _, ev := range res.gcEvents {
-                                       fmt.Fprintf(buf, " %s\n", ev)
+                                       fmt.Fprintf(buf, " %s -> %s\n", ev, ev.Link)
+                               }
+                               fmt.Fprintln(buf, "Events in Task")
+                               for i, ev := range task.events {
+                                       fmt.Fprintf(buf, " %d: %s\n", i, ev)
                                }
-                               fmt.Fprintf(buf, "%s\n", task)
-                               t.Logf("%s", buf)
+
+                               t.Logf("\n%s", buf)
                        }
                case "taskWithoutGC":
                        if got != 0 {