about summary refs log tree commit diff
path: root/vendor/modernc.org/libc/probes.go
blob: b92cfa1b9ab96d3e184dfaead2a853620ac6b5fb (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2022 The Libc 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 libc // import "modernc.org/libc"

import (
	"bytes"
	"fmt"
	"math"
	"runtime/debug"
	"sort"
	"strings"
	"sync"
	"sync/atomic"

	"github.com/dustin/go-humanize"
)

type PerfCounter struct {
	a      []int32
	labels []string

	enabled bool
}

func NewPerfCounter(labels []string) *PerfCounter {
	return &PerfCounter{
		a:       make([]int32, len(labels)),
		labels:  labels,
		enabled: true,
	}
}

func (c *PerfCounter) Disable() { c.enabled = false }
func (c *PerfCounter) Enable()  { c.enabled = true }

func (c *PerfCounter) Clear() {
	for i := range c.a {
		c.a[i] = 0
	}
}

func (c *PerfCounter) Inc(n int) {
	if c.enabled {
		atomic.AddInt32(&c.a[n], 1)
	}
}

func (c *PerfCounter) IncN(n, m int) {
	if c.enabled {
		atomic.AddInt32(&c.a[n], int32(m))
	}
}

func (c *PerfCounter) String() string {
	sv := c.enabled

	defer func() { c.enabled = sv }()

	c.enabled = false

	var a []string
	for i, v := range c.a {
		if v != 0 {
			a = append(a, fmt.Sprintf("%q: %s", c.labels[i], h(v)))
		}
	}
	return fmt.Sprint(a)
}

func h(v interface{}) string {
	switch x := v.(type) {
	case int:
		return humanize.Comma(int64(x))
	case int32:
		return humanize.Comma(int64(x))
	case int64:
		return humanize.Comma(x)
	case uint32:
		return humanize.Comma(int64(x))
	case uint64:
		if x <= math.MaxInt64 {
			return humanize.Comma(int64(x))
		}

		return "-" + humanize.Comma(-int64(x))
	}
	return fmt.Sprint(v)
}

type StackCapture struct {
	sync.Mutex
	m map[string]int

	depth int

	enabled bool
}

func NewStackCapture(depth int) *StackCapture {
	return &StackCapture{
		m:       map[string]int{},
		depth:   depth,
		enabled: true,
	}
}

func (c *StackCapture) Disable() { c.enabled = false }
func (c *StackCapture) Enable()  { c.enabled = true }

func (c *StackCapture) Clear() {
	c.Lock()

	defer c.Unlock()
	c.m = map[string]int{}
}

var (
	stackCapturePrefix = []byte("\n\t")
)

func (c *StackCapture) Record() {
	if !c.enabled {
		return
	}

	b := debug.Stack()
	var s strings.Builder
out:
	for i := 0; len(b) > 0 && i < c.depth+2; i++ {
		l := bytes.Index(b, stackCapturePrefix)
		if l < 0 {
			break out
		}

		b = b[l+len(stackCapturePrefix):]
		h := bytes.IndexByte(b, '\n')
		if h < 0 {
			h = len(b)
		}
		if i > 1 {
			fmt.Fprintf(&s, "\n\t%s", b[:h])
		}
		b = b[h:]
	}
	c.Lock()

	defer c.Unlock()

	c.m[s.String()]++
}

func (c *StackCapture) String() string {
	c.Lock()

	defer c.Unlock()

	var b strings.Builder
	var a []string
	for k := range c.m {
		a = append(a, k)
	}
	sort.Slice(a, func(i, j int) bool { return c.m[a[i]] < c.m[a[j]] })
	for _, k := range a {
		fmt.Fprintf(&b, "%d%s\n", c.m[k], k)
	}
	return b.String()
}