aboutsummaryrefslogtreecommitdiff
path: root/internal/stack/process.go
blob: 8812de9521cb3faa0be882eefa8d953becdedd8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package stack

import (
	"bytes"
	"fmt"
	"io"
	"runtime"
	"sort"
)

// Capture get the current stack traces from the runtime.
func Capture() Dump {
	buf := make([]byte, 2<<20)
	buf = buf[:runtime.Stack(buf, true)]
	scanner := NewScanner(bytes.NewReader(buf))
	dump, _ := Parse(scanner)
	return dump
}

// Summarize a dump for easier consumption.
// This collates goroutines with equivalent stacks.
func Summarize(dump Dump) Summary {
	s := Summary{
		Total: len(dump),
	}
	for _, gr := range dump {
		s.addGoroutine(gr)
	}
	return s
}

// Process and input stream to an output stream, summarizing any stacks that
// are detected in place.
func Process(out io.Writer, in io.Reader) error {
	scanner := NewScanner(in)
	for {
		dump, err := Parse(scanner)
		summary := Summarize(dump)
		switch {
		case len(dump) > 0:
			fmt.Fprintf(out, "%+v\n\n", summary)
		case err != nil:
			return err
		case scanner.Done():
			return scanner.Err()
		default:
			// must have been a line that is not part of a dump
			fmt.Fprintln(out, scanner.Next())
		}
	}
}

// Diff calculates the delta between two dumps.
func Diff(before, after Dump) Delta {
	result := Delta{}
	processed := make(map[int]bool)
	for _, gr := range before {
		processed[gr.ID] = false
	}
	for _, gr := range after {
		if _, found := processed[gr.ID]; found {
			result.Shared = append(result.Shared, gr)
		} else {
			result.After = append(result.After, gr)
		}
		processed[gr.ID] = true
	}
	for _, gr := range before {
		if done := processed[gr.ID]; !done {
			result.Before = append(result.Before, gr)
		}
	}
	return result
}

// TODO: do we want to allow contraction of stacks before comparison?
func (s *Summary) addGoroutine(gr Goroutine) {
	index := sort.Search(len(s.Calls), func(i int) bool {
		return !s.Calls[i].Stack.less(gr.Stack)
	})
	if index >= len(s.Calls) || !s.Calls[index].Stack.equal(gr.Stack) {
		// insert new stack, first increase the length
		s.Calls = append(s.Calls, Call{})
		// move the top part upward to make space
		copy(s.Calls[index+1:], s.Calls[index:])
		// insert the new call
		s.Calls[index] = Call{
			Stack: gr.Stack,
		}
	}
	// merge the goroutine into the matched call
	s.Calls[index].merge(gr)
}

// TODO: do we want other grouping strategies?
func (c *Call) merge(gr Goroutine) {
	for i := range c.Groups {
		canditate := &c.Groups[i]
		if canditate.State == gr.State {
			canditate.Goroutines = append(canditate.Goroutines, gr)
			return
		}
	}
	c.Groups = append(c.Groups, Group{
		State:      gr.State,
		Goroutines: []Goroutine{gr},
	})
}