Source file
src/cmd/trace/annotations.go
1
2
3
4
5 package main
6
7 import (
8 "bytes"
9 "fmt"
10 "html/template"
11 "internal/trace"
12 "log"
13 "math"
14 "net/http"
15 "net/url"
16 "reflect"
17 "sort"
18 "strconv"
19 "strings"
20 "time"
21 )
22
23 func init() {
24 http.HandleFunc("/usertasks", httpUserTasks)
25 http.HandleFunc("/usertask", httpUserTask)
26 http.HandleFunc("/userregions", httpUserRegions)
27 http.HandleFunc("/userregion", httpUserRegion)
28 }
29
30
31 func httpUserTasks(w http.ResponseWriter, r *http.Request) {
32 res, err := analyzeAnnotations()
33 if err != nil {
34 http.Error(w, err.Error(), http.StatusInternalServerError)
35 return
36 }
37
38 tasks := res.tasks
39 summary := make(map[string]taskStats)
40 for _, task := range tasks {
41 stats, ok := summary[task.name]
42 if !ok {
43 stats.Type = task.name
44 }
45
46 stats.add(task)
47 summary[task.name] = stats
48 }
49
50
51 userTasks := make([]taskStats, 0, len(summary))
52 for _, stats := range summary {
53 userTasks = append(userTasks, stats)
54 }
55 sort.Slice(userTasks, func(i, j int) bool {
56 return userTasks[i].Type < userTasks[j].Type
57 })
58
59
60 err = templUserTaskTypes.Execute(w, userTasks)
61 if err != nil {
62 http.Error(w, fmt.Sprintf("failed to execute template: %v", err), http.StatusInternalServerError)
63 return
64 }
65 }
66
67 func httpUserRegions(w http.ResponseWriter, r *http.Request) {
68 res, err := analyzeAnnotations()
69 if err != nil {
70 http.Error(w, err.Error(), http.StatusInternalServerError)
71 return
72 }
73 allRegions := res.regions
74
75 summary := make(map[regionTypeID]regionStats)
76 for id, regions := range allRegions {
77 stats, ok := summary[id]
78 if !ok {
79 stats.regionTypeID = id
80 }
81 for _, s := range regions {
82 stats.add(s)
83 }
84 summary[id] = stats
85 }
86
87 userRegions := make([]regionStats, 0, len(summary))
88 for _, stats := range summary {
89 userRegions = append(userRegions, stats)
90 }
91 sort.Slice(userRegions, func(i, j int) bool {
92 if userRegions[i].Type != userRegions[j].Type {
93 return userRegions[i].Type < userRegions[j].Type
94 }
95 return userRegions[i].Frame.PC < userRegions[j].Frame.PC
96 })
97
98 err = templUserRegionTypes.Execute(w, userRegions)
99 if err != nil {
100 http.Error(w, fmt.Sprintf("failed to execute template: %v", err), http.StatusInternalServerError)
101 return
102 }
103 }
104
105 func httpUserRegion(w http.ResponseWriter, r *http.Request) {
106 filter, err := newRegionFilter(r)
107 if err != nil {
108 http.Error(w, err.Error(), http.StatusBadRequest)
109 return
110 }
111 res, err := analyzeAnnotations()
112 if err != nil {
113 http.Error(w, err.Error(), http.StatusInternalServerError)
114 return
115 }
116 allRegions := res.regions
117
118 var data []regionDesc
119
120 var maxTotal int64
121 for id, regions := range allRegions {
122 for _, s := range regions {
123 if !filter.match(id, s) {
124 continue
125 }
126 data = append(data, s)
127 if maxTotal < s.TotalTime {
128 maxTotal = s.TotalTime
129 }
130 }
131 }
132
133 sortby := r.FormValue("sortby")
134 _, ok := reflect.TypeOf(regionDesc{}).FieldByNameFunc(func(s string) bool {
135 return s == sortby
136 })
137 if !ok {
138 sortby = "TotalTime"
139 }
140 sort.Slice(data, func(i, j int) bool {
141 ival := reflect.ValueOf(data[i]).FieldByName(sortby).Int()
142 jval := reflect.ValueOf(data[j]).FieldByName(sortby).Int()
143 return ival > jval
144 })
145
146 err = templUserRegionType.Execute(w, struct {
147 MaxTotal int64
148 Data []regionDesc
149 Name string
150 Filter *regionFilter
151 }{
152 MaxTotal: maxTotal,
153 Data: data,
154 Name: filter.name,
155 Filter: filter,
156 })
157 if err != nil {
158 http.Error(w, fmt.Sprintf("failed to execute template: %v", err), http.StatusInternalServerError)
159 return
160 }
161 }
162
163
164 func httpUserTask(w http.ResponseWriter, r *http.Request) {
165 filter, err := newTaskFilter(r)
166 if err != nil {
167 http.Error(w, err.Error(), http.StatusBadRequest)
168 return
169 }
170
171 res, err := analyzeAnnotations()
172 if err != nil {
173 http.Error(w, err.Error(), http.StatusInternalServerError)
174 return
175 }
176 tasks := res.tasks
177
178 type event struct {
179 WhenString string
180 Elapsed time.Duration
181 Go uint64
182 What string
183
184 }
185 type entry struct {
186 WhenString string
187 ID uint64
188 Duration time.Duration
189 Complete bool
190 Events []event
191 Start, End time.Duration
192 GCTime time.Duration
193 }
194
195 base := time.Duration(firstTimestamp()) * time.Nanosecond
196
197 var data []entry
198
199 for _, task := range tasks {
200 if !filter.match(task) {
201 continue
202 }
203
204 rawEvents := append([]*trace.Event{}, task.events...)
205 for _, s := range task.regions {
206 if s.Start != nil {
207 rawEvents = append(rawEvents, s.Start)
208 }
209 }
210 sort.SliceStable(rawEvents, func(i, j int) bool { return rawEvents[i].Ts < rawEvents[j].Ts })
211
212 var events []event
213 var last time.Duration
214 for i, ev := range rawEvents {
215 when := time.Duration(ev.Ts)*time.Nanosecond - base
216 elapsed := time.Duration(ev.Ts)*time.Nanosecond - last
217 if i == 0 {
218 elapsed = 0
219 }
220
221 what := describeEvent(ev)
222 if what != "" {
223 events = append(events, event{
224 WhenString: fmt.Sprintf("%2.9f", when.Seconds()),
225 Elapsed: elapsed,
226 What: what,
227 Go: ev.G,
228 })
229 last = time.Duration(ev.Ts) * time.Nanosecond
230 }
231 }
232
233 data = append(data, entry{
234 WhenString: fmt.Sprintf("%2.9fs", (time.Duration(task.firstTimestamp())*time.Nanosecond - base).Seconds()),
235 Duration: task.duration(),
236 ID: task.id,
237 Complete: task.complete(),
238 Events: events,
239 Start: time.Duration(task.firstTimestamp()) * time.Nanosecond,
240 End: time.Duration(task.endTimestamp()) * time.Nanosecond,
241 GCTime: task.overlappingGCDuration(res.gcEvents),
242 })
243 }
244 sort.Slice(data, func(i, j int) bool {
245 return data[i].Duration < data[j].Duration
246 })
247
248
249 err = templUserTaskType.Execute(w, struct {
250 Name string
251 Entry []entry
252 }{
253 Name: filter.name,
254 Entry: data,
255 })
256 if err != nil {
257 log.Printf("failed to execute template: %v", err)
258 http.Error(w, fmt.Sprintf("failed to execute template: %v", err), http.StatusInternalServerError)
259 return
260 }
261 }
262
263 type annotationAnalysisResult struct {
264 tasks map[uint64]*taskDesc
265 regions map[regionTypeID][]regionDesc
266 gcEvents []*trace.Event
267 }
268
269 type regionTypeID struct {
270 Frame trace.Frame
271 Type string
272 }
273
274
275
276 func analyzeAnnotations() (annotationAnalysisResult, error) {
277 res, err := parseTrace()
278 if err != nil {
279 return annotationAnalysisResult{}, fmt.Errorf("failed to parse trace: %v", err)
280 }
281
282 events := res.Events
283 if len(events) == 0 {
284 return annotationAnalysisResult{}, fmt.Errorf("empty trace")
285 }
286
287 tasks := allTasks{}
288 regions := map[regionTypeID][]regionDesc{}
289 var gcEvents []*trace.Event
290
291 for _, ev := range events {
292 switch typ := ev.Type; typ {
293 case trace.EvUserTaskCreate, trace.EvUserTaskEnd, trace.EvUserLog:
294 taskid := ev.Args[0]
295 task := tasks.task(taskid)
296 task.addEvent(ev)
297
298
299 if typ == trace.EvUserTaskCreate {
300 if parentID := ev.Args[1]; parentID != 0 {
301 parentTask := tasks.task(parentID)
302 task.parent = parentTask
303 if parentTask != nil {
304 parentTask.children = append(parentTask.children, task)
305 }
306 }
307 }
308
309 case trace.EvGCStart:
310 gcEvents = append(gcEvents, ev)
311 }
312 }
313
314 analyzeGoroutines(events)
315 for goid, stats := range gs {
316
317
318
319 for _, s := range stats.Regions {
320 if s.TaskID != 0 {
321 task := tasks.task(s.TaskID)
322 task.goroutines[goid] = struct{}{}
323 task.regions = append(task.regions, regionDesc{UserRegionDesc: s, G: goid})
324 }
325 var frame trace.Frame
326 if s.Start != nil {
327 frame = *s.Start.Stk[0]
328 }
329 id := regionTypeID{Frame: frame, Type: s.Name}
330 regions[id] = append(regions[id], regionDesc{UserRegionDesc: s, G: goid})
331 }
332 }
333
334
335 for _, task := range tasks {
336 sort.SliceStable(task.regions, func(i, j int) bool {
337 si, sj := task.regions[i].firstTimestamp(), task.regions[j].firstTimestamp()
338 if si != sj {
339 return si < sj
340 }
341 return task.regions[i].lastTimestamp() < task.regions[j].lastTimestamp()
342 })
343 }
344 return annotationAnalysisResult{tasks: tasks, regions: regions, gcEvents: gcEvents}, nil
345 }
346
347
348 type taskDesc struct {
349 name string
350 id uint64
351 events []*trace.Event
352 regions []regionDesc
353 goroutines map[uint64]struct{}
354
355 create *trace.Event
356 end *trace.Event
357
358 parent *taskDesc
359 children []*taskDesc
360 }
361
362 func newTaskDesc(id uint64) *taskDesc {
363 return &taskDesc{
364 id: id,
365 goroutines: make(map[uint64]struct{}),
366 }
367 }
368
369 func (task *taskDesc) String() string {
370 if task == nil {
371 return "task <nil>"
372 }
373 wb := new(bytes.Buffer)
374 fmt.Fprintf(wb, "task %d:\t%s\n", task.id, task.name)
375 fmt.Fprintf(wb, "\tstart: %v end: %v complete: %t\n", task.firstTimestamp(), task.endTimestamp(), task.complete())
376 fmt.Fprintf(wb, "\t%d goroutines\n", len(task.goroutines))
377 fmt.Fprintf(wb, "\t%d regions:\n", len(task.regions))
378 for _, s := range task.regions {
379 fmt.Fprintf(wb, "\t\t%s(goid=%d)\n", s.Name, s.G)
380 }
381 if task.parent != nil {
382 fmt.Fprintf(wb, "\tparent: %s\n", task.parent.name)
383 }
384 fmt.Fprintf(wb, "\t%d children:\n", len(task.children))
385 for _, c := range task.children {
386 fmt.Fprintf(wb, "\t\t%s\n", c.name)
387 }
388
389 return wb.String()
390 }
391
392
393 type regionDesc struct {
394 *trace.UserRegionDesc
395 G uint64
396 }
397
398 type allTasks map[uint64]*taskDesc
399
400 func (tasks allTasks) task(taskID uint64) *taskDesc {
401 if taskID == 0 {
402 return nil
403 }
404
405 t, ok := tasks[taskID]
406 if ok {
407 return t
408 }
409
410 t = newTaskDesc(taskID)
411 tasks[taskID] = t
412 return t
413 }
414
415 func (task *taskDesc) addEvent(ev *trace.Event) {
416 if task == nil {
417 return
418 }
419
420 task.events = append(task.events, ev)
421 task.goroutines[ev.G] = struct{}{}
422
423 switch typ := ev.Type; typ {
424 case trace.EvUserTaskCreate:
425 task.name = ev.SArgs[0]
426 task.create = ev
427 case trace.EvUserTaskEnd:
428 task.end = ev
429 }
430 }
431
432
433
434 func (task *taskDesc) complete() bool {
435 if task == nil {
436 return false
437 }
438 return task.create != nil && task.end != nil
439 }
440
441
442 func (task *taskDesc) descendants() []*taskDesc {
443 if task == nil {
444 return nil
445 }
446 res := []*taskDesc{task}
447 for i := 0; len(res[i:]) > 0; i++ {
448 t := res[i]
449 for _, c := range t.children {
450 res = append(res, c)
451 }
452 }
453 return res
454 }
455
456
457
458
459 func (task *taskDesc) firstTimestamp() int64 {
460 if task != nil && task.create != nil {
461 return task.create.Ts
462 }
463 return firstTimestamp()
464 }
465
466
467
468
469 func (task *taskDesc) lastTimestamp() int64 {
470 endTs := task.endTimestamp()
471 if last := task.lastEvent(); last != nil && last.Ts > endTs {
472 return last.Ts
473 }
474 return endTs
475 }
476
477
478
479
480 func (task *taskDesc) endTimestamp() int64 {
481 if task != nil && task.end != nil {
482 return task.end.Ts
483 }
484 return lastTimestamp()
485 }
486
487 func (task *taskDesc) duration() time.Duration {
488 return time.Duration(task.endTimestamp()-task.firstTimestamp()) * time.Nanosecond
489 }
490
491 func (region *regionDesc) duration() time.Duration {
492 return time.Duration(region.lastTimestamp()-region.firstTimestamp()) * time.Nanosecond
493 }
494
495
496 func (task *taskDesc) overlappingGCDuration(evs []*trace.Event) (overlapping time.Duration) {
497 for _, ev := range evs {
498
499 if typ := ev.Type; typ != trace.EvGCStart && typ != trace.EvGCSTWStart {
500 continue
501 }
502
503 if o, overlapped := task.overlappingDuration(ev); overlapped {
504 overlapping += o
505 }
506 }
507 return overlapping
508 }
509
510
511
512
513 func (task *taskDesc) overlappingInstant(ev *trace.Event) bool {
514 if _, ok := isUserAnnotationEvent(ev); ok && task.id != ev.Args[0] {
515 return false
516 }
517
518 ts := ev.Ts
519 taskStart := task.firstTimestamp()
520 taskEnd := task.endTimestamp()
521 if ts < taskStart || taskEnd < ts {
522 return false
523 }
524 if ev.P == trace.GCP {
525 return true
526 }
527
528
529 goid := ev.G
530 for _, region := range task.regions {
531 if region.G != goid {
532 continue
533 }
534 if region.firstTimestamp() <= ts && ts <= region.lastTimestamp() {
535 return true
536 }
537 }
538 return false
539 }
540
541
542
543
544
545 func (task *taskDesc) overlappingDuration(ev *trace.Event) (time.Duration, bool) {
546 start := ev.Ts
547 end := lastTimestamp()
548 if ev.Link != nil {
549 end = ev.Link.Ts
550 }
551
552 if start > end {
553 return 0, false
554 }
555
556 goid := ev.G
557 goid2 := ev.G
558 if ev.Link != nil {
559 goid2 = ev.Link.G
560 }
561
562
563 if ev.P == trace.GCP {
564 taskStart := task.firstTimestamp()
565 taskEnd := task.endTimestamp()
566 o := overlappingDuration(taskStart, taskEnd, start, end)
567 return o, o > 0
568 }
569
570
571 var overlapping time.Duration
572 var lastRegionEnd int64
573 for _, region := range task.regions {
574 if region.G != goid && region.G != goid2 {
575 continue
576 }
577 regionStart, regionEnd := region.firstTimestamp(), region.lastTimestamp()
578 if regionStart < lastRegionEnd {
579 continue
580 }
581
582 if o := overlappingDuration(regionStart, regionEnd, start, end); o > 0 {
583
584 lastRegionEnd = regionEnd
585 overlapping += o
586 }
587 }
588 return overlapping, overlapping > 0
589 }
590
591
592
593
594 func overlappingDuration(start1, end1, start2, end2 int64) time.Duration {
595
596 if end1 < start2 || end2 < start1 {
597 return 0
598 }
599
600 if start1 < start2 {
601 start1 = start2
602 }
603 if end1 > end2 {
604 end1 = end2
605 }
606 return time.Duration(end1 - start1)
607 }
608
609 func (task *taskDesc) lastEvent() *trace.Event {
610 if task == nil {
611 return nil
612 }
613
614 if n := len(task.events); n > 0 {
615 return task.events[n-1]
616 }
617 return nil
618 }
619
620
621
622
623 func (region *regionDesc) firstTimestamp() int64 {
624 if region.Start != nil {
625 return region.Start.Ts
626 }
627 return firstTimestamp()
628 }
629
630
631
632
633 func (region *regionDesc) lastTimestamp() int64 {
634 if region.End != nil {
635 return region.End.Ts
636 }
637 return lastTimestamp()
638 }
639
640
641
642
643
644 func (task *taskDesc) RelatedGoroutines(events []*trace.Event, depth int) map[uint64]bool {
645 start, end := task.firstTimestamp(), task.endTimestamp()
646
647 gmap := map[uint64]bool{}
648 for k := range task.goroutines {
649 gmap[k] = true
650 }
651
652 for i := 0; i < depth; i++ {
653 gmap1 := make(map[uint64]bool)
654 for g := range gmap {
655 gmap1[g] = true
656 }
657 for _, ev := range events {
658 if ev.Ts < start || ev.Ts > end {
659 continue
660 }
661 if ev.Type == trace.EvGoUnblock && gmap[ev.Args[0]] {
662 gmap1[ev.G] = true
663 }
664 gmap = gmap1
665 }
666 }
667 gmap[0] = true
668 return gmap
669 }
670
671 type taskFilter struct {
672 name string
673 cond []func(*taskDesc) bool
674 }
675
676 func (f *taskFilter) match(t *taskDesc) bool {
677 if t == nil {
678 return false
679 }
680 for _, c := range f.cond {
681 if !c(t) {
682 return false
683 }
684 }
685 return true
686 }
687
688 func newTaskFilter(r *http.Request) (*taskFilter, error) {
689 if err := r.ParseForm(); err != nil {
690 return nil, err
691 }
692
693 var name []string
694 var conditions []func(*taskDesc) bool
695
696 param := r.Form
697 if typ, ok := param["type"]; ok && len(typ) > 0 {
698 name = append(name, "type="+typ[0])
699 conditions = append(conditions, func(t *taskDesc) bool {
700 return t.name == typ[0]
701 })
702 }
703 if complete := r.FormValue("complete"); complete == "1" {
704 name = append(name, "complete")
705 conditions = append(conditions, func(t *taskDesc) bool {
706 return t.complete()
707 })
708 } else if complete == "0" {
709 name = append(name, "incomplete")
710 conditions = append(conditions, func(t *taskDesc) bool {
711 return !t.complete()
712 })
713 }
714 if lat, err := time.ParseDuration(r.FormValue("latmin")); err == nil {
715 name = append(name, fmt.Sprintf("latency >= %s", lat))
716 conditions = append(conditions, func(t *taskDesc) bool {
717 return t.complete() && t.duration() >= lat
718 })
719 }
720 if lat, err := time.ParseDuration(r.FormValue("latmax")); err == nil {
721 name = append(name, fmt.Sprintf("latency <= %s", lat))
722 conditions = append(conditions, func(t *taskDesc) bool {
723 return t.complete() && t.duration() <= lat
724 })
725 }
726 if text := r.FormValue("logtext"); text != "" {
727 name = append(name, fmt.Sprintf("log contains %q", text))
728 conditions = append(conditions, func(t *taskDesc) bool {
729 return taskMatches(t, text)
730 })
731 }
732
733 return &taskFilter{name: strings.Join(name, ","), cond: conditions}, nil
734 }
735
736 func taskMatches(t *taskDesc, text string) bool {
737 for _, ev := range t.events {
738 switch ev.Type {
739 case trace.EvUserTaskCreate, trace.EvUserRegion, trace.EvUserLog:
740 for _, s := range ev.SArgs {
741 if strings.Contains(s, text) {
742 return true
743 }
744 }
745 }
746 }
747 return false
748 }
749
750 type regionFilter struct {
751 name string
752 params url.Values
753 cond []func(regionTypeID, regionDesc) bool
754 }
755
756 func (f *regionFilter) match(id regionTypeID, s regionDesc) bool {
757 for _, c := range f.cond {
758 if !c(id, s) {
759 return false
760 }
761 }
762 return true
763 }
764
765 func newRegionFilter(r *http.Request) (*regionFilter, error) {
766 if err := r.ParseForm(); err != nil {
767 return nil, err
768 }
769
770 var name []string
771 var conditions []func(regionTypeID, regionDesc) bool
772 filterParams := make(url.Values)
773
774 param := r.Form
775 if typ, ok := param["type"]; ok && len(typ) > 0 {
776 name = append(name, "type="+typ[0])
777 conditions = append(conditions, func(id regionTypeID, s regionDesc) bool {
778 return id.Type == typ[0]
779 })
780 filterParams.Add("type", typ[0])
781 }
782 if pc, err := strconv.ParseUint(r.FormValue("pc"), 16, 64); err == nil {
783 encPC := fmt.Sprintf("%x", pc)
784 name = append(name, "pc="+encPC)
785 conditions = append(conditions, func(id regionTypeID, s regionDesc) bool {
786 return id.Frame.PC == pc
787 })
788 filterParams.Add("pc", encPC)
789 }
790
791 if lat, err := time.ParseDuration(r.FormValue("latmin")); err == nil {
792 name = append(name, fmt.Sprintf("latency >= %s", lat))
793 conditions = append(conditions, func(_ regionTypeID, s regionDesc) bool {
794 return s.duration() >= lat
795 })
796 filterParams.Add("latmin", lat.String())
797 }
798 if lat, err := time.ParseDuration(r.FormValue("latmax")); err == nil {
799 name = append(name, fmt.Sprintf("latency <= %s", lat))
800 conditions = append(conditions, func(_ regionTypeID, s regionDesc) bool {
801 return s.duration() <= lat
802 })
803 filterParams.Add("latmax", lat.String())
804 }
805
806 return ®ionFilter{
807 name: strings.Join(name, ","),
808 cond: conditions,
809 params: filterParams,
810 }, nil
811 }
812
813 type durationHistogram struct {
814 Count int
815 Buckets []int
816 MinBucket, MaxBucket int
817 }
818
819
820 var logDiv = math.Log(math.Pow(10, 1.0/5))
821
822 func (h *durationHistogram) add(d time.Duration) {
823 var bucket int
824 if d > 0 {
825 bucket = int(math.Log(float64(d)) / logDiv)
826 }
827 if len(h.Buckets) <= bucket {
828 h.Buckets = append(h.Buckets, make([]int, bucket-len(h.Buckets)+1)...)
829 h.Buckets = h.Buckets[:cap(h.Buckets)]
830 }
831 h.Buckets[bucket]++
832 if bucket < h.MinBucket || h.MaxBucket == 0 {
833 h.MinBucket = bucket
834 }
835 if bucket > h.MaxBucket {
836 h.MaxBucket = bucket
837 }
838 h.Count++
839 }
840
841 func (h *durationHistogram) BucketMin(bucket int) time.Duration {
842 return time.Duration(math.Exp(float64(bucket) * logDiv))
843 }
844
845 func niceDuration(d time.Duration) string {
846 var rnd time.Duration
847 var unit string
848 switch {
849 case d < 10*time.Microsecond:
850 rnd, unit = time.Nanosecond, "ns"
851 case d < 10*time.Millisecond:
852 rnd, unit = time.Microsecond, "µs"
853 case d < 10*time.Second:
854 rnd, unit = time.Millisecond, "ms"
855 default:
856 rnd, unit = time.Second, "s "
857 }
858 return fmt.Sprintf("%d%s", d/rnd, unit)
859 }
860
861 func (h *durationHistogram) ToHTML(urlmaker func(min, max time.Duration) string) template.HTML {
862 if h == nil || h.Count == 0 {
863 return template.HTML("")
864 }
865
866 const barWidth = 400
867
868 maxCount := 0
869 for _, count := range h.Buckets {
870 if count > maxCount {
871 maxCount = count
872 }
873 }
874
875 w := new(bytes.Buffer)
876 fmt.Fprintf(w, `<table>`)
877 for i := h.MinBucket; i <= h.MaxBucket; i++ {
878
879 if h.Buckets[i] > 0 {
880 fmt.Fprintf(w, `<tr><td class="histoTime" align="right"><a href=%s>%s</a></td>`, urlmaker(h.BucketMin(i), h.BucketMin(i+1)), niceDuration(h.BucketMin(i)))
881 } else {
882 fmt.Fprintf(w, `<tr><td class="histoTime" align="right">%s</td>`, niceDuration(h.BucketMin(i)))
883 }
884
885 width := h.Buckets[i] * barWidth / maxCount
886 fmt.Fprintf(w, `<td><div style="width:%dpx;background:blue;position:relative"> </div></td>`, width)
887
888 fmt.Fprintf(w, `<td align="right"><div style="position:relative">%d</div></td>`, h.Buckets[i])
889 fmt.Fprintf(w, "</tr>\n")
890
891 }
892
893 fmt.Fprintf(w, `<tr><td align="right">%s</td></tr>`, niceDuration(h.BucketMin(h.MaxBucket+1)))
894 fmt.Fprintf(w, `</table>`)
895 return template.HTML(w.String())
896 }
897
898 func (h *durationHistogram) String() string {
899 const barWidth = 40
900
901 labels := []string{}
902 maxLabel := 0
903 maxCount := 0
904 for i := h.MinBucket; i <= h.MaxBucket; i++ {
905
906 label := fmt.Sprintf("[%-12s%-11s)", h.BucketMin(i).String()+",", h.BucketMin(i+1))
907 labels = append(labels, label)
908 if len(label) > maxLabel {
909 maxLabel = len(label)
910 }
911 count := h.Buckets[i]
912 if count > maxCount {
913 maxCount = count
914 }
915 }
916
917 w := new(bytes.Buffer)
918 for i := h.MinBucket; i <= h.MaxBucket; i++ {
919 count := h.Buckets[i]
920 bar := count * barWidth / maxCount
921 fmt.Fprintf(w, "%*s %-*s %d\n", maxLabel, labels[i-h.MinBucket], barWidth, strings.Repeat("█", bar), count)
922 }
923 return w.String()
924 }
925
926 type regionStats struct {
927 regionTypeID
928 Histogram durationHistogram
929 }
930
931 func (s *regionStats) UserRegionURL() func(min, max time.Duration) string {
932 return func(min, max time.Duration) string {
933 return fmt.Sprintf("/userregion?type=%s&pc=%x&latmin=%v&latmax=%v", template.URLQueryEscaper(s.Type), s.Frame.PC, template.URLQueryEscaper(min), template.URLQueryEscaper(max))
934 }
935 }
936
937 func (s *regionStats) add(region regionDesc) {
938 s.Histogram.add(region.duration())
939 }
940
941 var templUserRegionTypes = template.Must(template.New("").Parse(`
942 <html>
943 <style type="text/css">
944 .histoTime {
945 width: 20%;
946 white-space:nowrap;
947 }
948
949 </style>
950 <body>
951 <table border="1" sortable="1">
952 <tr>
953 <th>Region type</th>
954 <th>Count</th>
955 <th>Duration distribution (complete tasks)</th>
956 </tr>
957 {{range $}}
958 <tr>
959 <td>{{.Type}}<br>{{.Frame.Fn}}<br>{{.Frame.File}}:{{.Frame.Line}}</td>
960 <td><a href="/userregion?type={{.Type}}&pc={{.Frame.PC | printf "%x"}}">{{.Histogram.Count}}</a></td>
961 <td>{{.Histogram.ToHTML (.UserRegionURL)}}</td>
962 </tr>
963 {{end}}
964 </table>
965 </body>
966 </html>
967 `))
968
969 type taskStats struct {
970 Type string
971 Count int
972 Histogram durationHistogram
973 }
974
975 func (s *taskStats) UserTaskURL(complete bool) func(min, max time.Duration) string {
976 return func(min, max time.Duration) string {
977 return fmt.Sprintf("/usertask?type=%s&complete=%v&latmin=%v&latmax=%v", template.URLQueryEscaper(s.Type), template.URLQueryEscaper(complete), template.URLQueryEscaper(min), template.URLQueryEscaper(max))
978 }
979 }
980
981 func (s *taskStats) add(task *taskDesc) {
982 s.Count++
983 if task.complete() {
984 s.Histogram.add(task.duration())
985 }
986 }
987
988 var templUserTaskTypes = template.Must(template.New("").Parse(`
989 <html>
990 <style type="text/css">
991 .histoTime {
992 width: 20%;
993 white-space:nowrap;
994 }
995
996 </style>
997 <body>
998 Search log text: <form action="/usertask"><input name="logtext" type="text"><input type="submit"></form><br>
999 <table border="1" sortable="1">
1000 <tr>
1001 <th>Task type</th>
1002 <th>Count</th>
1003 <th>Duration distribution (complete tasks)</th>
1004 </tr>
1005 {{range $}}
1006 <tr>
1007 <td>{{.Type}}</td>
1008 <td><a href="/usertask?type={{.Type}}">{{.Count}}</a></td>
1009 <td>{{.Histogram.ToHTML (.UserTaskURL true)}}</td>
1010 </tr>
1011 {{end}}
1012 </table>
1013 </body>
1014 </html>
1015 `))
1016
1017 var templUserTaskType = template.Must(template.New("userTask").Funcs(template.FuncMap{
1018 "elapsed": elapsed,
1019 "asMillisecond": asMillisecond,
1020 "trimSpace": strings.TrimSpace,
1021 }).Parse(`
1022 <html>
1023 <head> <title>User Task: {{.Name}} </title> </head>
1024 <style type="text/css">
1025 body {
1026 font-family: sans-serif;
1027 }
1028 table#req-status td.family {
1029 padding-right: 2em;
1030 }
1031 table#req-status td.active {
1032 padding-right: 1em;
1033 }
1034 table#req-status td.empty {
1035 color: #aaa;
1036 }
1037 table#reqs {
1038 margin-top: 1em;
1039 }
1040 table#reqs tr.first {
1041 font-weight: bold;
1042 }
1043 table#reqs td {
1044 font-family: monospace;
1045 }
1046 table#reqs td.when {
1047 text-align: right;
1048 white-space: nowrap;
1049 }
1050 table#reqs td.elapsed {
1051 padding: 0 0.5em;
1052 text-align: right;
1053 white-space: pre;
1054 width: 10em;
1055 }
1056 address {
1057 font-size: smaller;
1058 margin-top: 5em;
1059 }
1060 </style>
1061 <body>
1062
1063 <h2>User Task: {{.Name}}</h2>
1064
1065 Search log text: <form onsubmit="window.location.search+='&logtext='+window.logtextinput.value; return false">
1066 <input name="logtext" id="logtextinput" type="text"><input type="submit">
1067 </form><br>
1068
1069 <table id="reqs">
1070 <tr><th>When</th><th>Elapsed</th><th>Goroutine ID</th><th>Events</th></tr>
1071 {{range $el := $.Entry}}
1072 <tr class="first">
1073 <td class="when">{{$el.WhenString}}</td>
1074 <td class="elapsed">{{$el.Duration}}</td>
1075 <td></td>
1076 <td>
1077 <a href="/trace?focustask={{$el.ID}}#{{asMillisecond $el.Start}}:{{asMillisecond $el.End}}">Task {{$el.ID}}</a>
1078 <a href="/trace?taskid={{$el.ID}}#{{asMillisecond $el.Start}}:{{asMillisecond $el.End}}">(goroutine view)</a>
1079 ({{if .Complete}}complete{{else}}incomplete{{end}})</td>
1080 </tr>
1081 {{range $el.Events}}
1082 <tr>
1083 <td class="when">{{.WhenString}}</td>
1084 <td class="elapsed">{{elapsed .Elapsed}}</td>
1085 <td class="goid">{{.Go}}</td>
1086 <td>{{.What}}</td>
1087 </tr>
1088 {{end}}
1089 <tr>
1090 <td></td>
1091 <td></td>
1092 <td></td>
1093 <td>GC:{{$el.GCTime}}</td>
1094 {{end}}
1095 </body>
1096 </html>
1097 `))
1098
1099 func elapsed(d time.Duration) string {
1100 b := []byte(fmt.Sprintf("%.9f", d.Seconds()))
1101
1102
1103
1104 if d < time.Second {
1105 dot := bytes.IndexByte(b, '.')
1106 for i := 0; i < dot; i++ {
1107 b[i] = ' '
1108 }
1109 for i := dot + 1; i < len(b); i++ {
1110 if b[i] == '0' {
1111 b[i] = ' '
1112 } else {
1113 break
1114 }
1115 }
1116 }
1117
1118 return string(b)
1119 }
1120
1121 func asMillisecond(d time.Duration) float64 {
1122 return float64(d.Nanoseconds()) / 1e6
1123 }
1124
1125 func formatUserLog(ev *trace.Event) string {
1126 k, v := ev.SArgs[0], ev.SArgs[1]
1127 if k == "" {
1128 return v
1129 }
1130 if v == "" {
1131 return k
1132 }
1133 return fmt.Sprintf("%v=%v", k, v)
1134 }
1135
1136 func describeEvent(ev *trace.Event) string {
1137 switch ev.Type {
1138 case trace.EvGoCreate:
1139 goid := ev.Args[0]
1140 return fmt.Sprintf("new goroutine %d: %s", goid, gs[goid].Name)
1141 case trace.EvGoEnd, trace.EvGoStop:
1142 return "goroutine stopped"
1143 case trace.EvUserLog:
1144 return formatUserLog(ev)
1145 case trace.EvUserRegion:
1146 if ev.Args[1] == 0 {
1147 duration := "unknown"
1148 if ev.Link != nil {
1149 duration = (time.Duration(ev.Link.Ts-ev.Ts) * time.Nanosecond).String()
1150 }
1151 return fmt.Sprintf("region %s started (duration: %v)", ev.SArgs[0], duration)
1152 }
1153 return fmt.Sprintf("region %s ended", ev.SArgs[0])
1154 case trace.EvUserTaskCreate:
1155 return fmt.Sprintf("task %v (id %d, parent %d) created", ev.SArgs[0], ev.Args[0], ev.Args[1])
1156
1157 case trace.EvUserTaskEnd:
1158 return "task end"
1159 }
1160 return ""
1161 }
1162
1163 func isUserAnnotationEvent(ev *trace.Event) (taskID uint64, ok bool) {
1164 switch ev.Type {
1165 case trace.EvUserLog, trace.EvUserRegion, trace.EvUserTaskCreate, trace.EvUserTaskEnd:
1166 return ev.Args[0], true
1167 }
1168 return 0, false
1169 }
1170
1171 var templUserRegionType = template.Must(template.New("").Funcs(template.FuncMap{
1172 "prettyDuration": func(nsec int64) template.HTML {
1173 d := time.Duration(nsec) * time.Nanosecond
1174 return template.HTML(niceDuration(d))
1175 },
1176 "percent": func(dividend, divisor int64) template.HTML {
1177 if divisor == 0 {
1178 return ""
1179 }
1180 return template.HTML(fmt.Sprintf("(%.1f%%)", float64(dividend)/float64(divisor)*100))
1181 },
1182 "barLen": func(dividend, divisor int64) template.HTML {
1183 if divisor == 0 {
1184 return "0"
1185 }
1186 return template.HTML(fmt.Sprintf("%.2f%%", float64(dividend)/float64(divisor)*100))
1187 },
1188 "unknownTime": func(desc regionDesc) int64 {
1189 sum := desc.ExecTime + desc.IOTime + desc.BlockTime + desc.SyscallTime + desc.SchedWaitTime
1190 if sum < desc.TotalTime {
1191 return desc.TotalTime - sum
1192 }
1193 return 0
1194 },
1195 "filterParams": func(f *regionFilter) template.URL {
1196 return template.URL(f.params.Encode())
1197 },
1198 }).Parse(`
1199 <!DOCTYPE html>
1200 <title>User Region {{.Name}}</title>
1201 <style>
1202 th {
1203 background-color: #050505;
1204 color: #fff;
1205 }
1206 th.total-time,
1207 th.exec-time,
1208 th.io-time,
1209 th.block-time,
1210 th.syscall-time,
1211 th.sched-time,
1212 th.sweep-time,
1213 th.pause-time {
1214 cursor: pointer;
1215 }
1216 table {
1217 border-collapse: collapse;
1218 }
1219 .details tr:hover {
1220 background-color: #f2f2f2;
1221 }
1222 .details td {
1223 text-align: right;
1224 border: 1px solid #000;
1225 }
1226 .details td.id {
1227 text-align: left;
1228 }
1229 .stacked-bar-graph {
1230 width: 300px;
1231 height: 10px;
1232 color: #414042;
1233 white-space: nowrap;
1234 font-size: 5px;
1235 }
1236 .stacked-bar-graph span {
1237 display: inline-block;
1238 width: 100%;
1239 height: 100%;
1240 box-sizing: border-box;
1241 float: left;
1242 padding: 0;
1243 }
1244 .unknown-time { background-color: #636363; }
1245 .exec-time { background-color: #d7191c; }
1246 .io-time { background-color: #fdae61; }
1247 .block-time { background-color: #d01c8b; }
1248 .syscall-time { background-color: #7b3294; }
1249 .sched-time { background-color: #2c7bb6; }
1250 </style>
1251
1252 <script>
1253 function reloadTable(key, value) {
1254 let params = new URLSearchParams(window.location.search);
1255 params.set(key, value);
1256 window.location.search = params.toString();
1257 }
1258 </script>
1259
1260 <h2>{{.Name}}</h2>
1261
1262 {{ with $p := filterParams .Filter}}
1263 <table class="summary">
1264 <tr><td>Network Wait Time:</td><td> <a href="/regionio?{{$p}}">graph</a><a href="/regionio?{{$p}}&raw=1" download="io.profile">(download)</a></td></tr>
1265 <tr><td>Sync Block Time:</td><td> <a href="/regionblock?{{$p}}">graph</a><a href="/regionblock?{{$p}}&raw=1" download="block.profile">(download)</a></td></tr>
1266 <tr><td>Blocking Syscall Time:</td><td> <a href="/regionsyscall?{{$p}}">graph</a><a href="/regionsyscall?{{$p}}&raw=1" download="syscall.profile">(download)</a></td></tr>
1267 <tr><td>Scheduler Wait Time:</td><td> <a href="/regionsched?{{$p}}">graph</a><a href="/regionsched?{{$p}}&raw=1" download="sched.profile">(download)</a></td></tr>
1268 </table>
1269 {{ end }}
1270 <p>
1271 <table class="details">
1272 <tr>
1273 <th> Goroutine </th>
1274 <th> Task </th>
1275 <th onclick="reloadTable('sortby', 'TotalTime')" class="total-time"> Total</th>
1276 <th></th>
1277 <th onclick="reloadTable('sortby', 'ExecTime')" class="exec-time"> Execution</th>
1278 <th onclick="reloadTable('sortby', 'IOTime')" class="io-time"> Network wait</th>
1279 <th onclick="reloadTable('sortby', 'BlockTime')" class="block-time"> Sync block </th>
1280 <th onclick="reloadTable('sortby', 'SyscallTime')" class="syscall-time"> Blocking syscall</th>
1281 <th onclick="reloadTable('sortby', 'SchedWaitTime')" class="sched-time"> Scheduler wait</th>
1282 <th onclick="reloadTable('sortby', 'SweepTime')" class="sweep-time"> GC sweeping</th>
1283 <th onclick="reloadTable('sortby', 'GCTime')" class="pause-time"> GC pause</th>
1284 </tr>
1285 {{range .Data}}
1286 <tr>
1287 <td> <a href="/trace?goid={{.G}}">{{.G}}</a> </td>
1288 <td> {{if .TaskID}}<a href="/trace?focustask={{.TaskID}}">{{.TaskID}}</a>{{end}} </td>
1289 <td> {{prettyDuration .TotalTime}} </td>
1290 <td>
1291 <div class="stacked-bar-graph">
1292 {{if unknownTime .}}<span style="width:{{barLen (unknownTime .) $.MaxTotal}}" class="unknown-time"> </span>{{end}}
1293 {{if .ExecTime}}<span style="width:{{barLen .ExecTime $.MaxTotal}}" class="exec-time"> </span>{{end}}
1294 {{if .IOTime}}<span style="width:{{barLen .IOTime $.MaxTotal}}" class="io-time"> </span>{{end}}
1295 {{if .BlockTime}}<span style="width:{{barLen .BlockTime $.MaxTotal}}" class="block-time"> </span>{{end}}
1296 {{if .SyscallTime}}<span style="width:{{barLen .SyscallTime $.MaxTotal}}" class="syscall-time"> </span>{{end}}
1297 {{if .SchedWaitTime}}<span style="width:{{barLen .SchedWaitTime $.MaxTotal}}" class="sched-time"> </span>{{end}}
1298 </div>
1299 </td>
1300 <td> {{prettyDuration .ExecTime}}</td>
1301 <td> {{prettyDuration .IOTime}}</td>
1302 <td> {{prettyDuration .BlockTime}}</td>
1303 <td> {{prettyDuration .SyscallTime}}</td>
1304 <td> {{prettyDuration .SchedWaitTime}}</td>
1305 <td> {{prettyDuration .SweepTime}} {{percent .SweepTime .TotalTime}}</td>
1306 <td> {{prettyDuration .GCTime}} {{percent .GCTime .TotalTime}}</td>
1307 </tr>
1308 {{end}}
1309 </table>
1310 </p>
1311 `))
1312
View as plain text