forked from rainycape/gondola
-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile.go
236 lines (213 loc) · 5.53 KB
/
profile.go
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package profile
import (
"fmt"
"sync"
"time"
"gondola/log"
)
const On = profileIsOn
// Timed represents an ongoing timed event.
// Use Start or Startf to start a timed
// event.
type Timed struct {
name string
started time.Time
ended time.Time
autoend bool
notes []*Note
}
// Note adds a note regarding this timed event (e.g. the
// SQL query that was executed, the URL that was fetched, etc...).
func (t *Timed) Note(title string, text string) *Timed {
t.notes = append(t.notes, &Note{Title: title, Text: text})
return t
}
// Notef works like Note(), but accepts a format string.
func (t *Timed) Notef(title string, format string, args ...interface{}) *Timed {
t.notes = append(t.notes, &Note{Title: title, Text: fmt.Sprintf(format, args...)})
return t
}
// End ends the timed event.
func (t *Timed) End() {
t.ended = time.Now()
}
// Ended returns wheter the timed event has
// finished.
func (t *Timed) Ended() bool {
return !t.ended.IsZero()
}
// AutoEnd causes the timed event to be ended when
// the timings are requested.
func (e *Timed) AutoEnd() *Timed {
e.autoend = true
return e
}
type context struct {
sync.Mutex
events []*Timed
}
var contexts struct {
sync.RWMutex
data map[int32]*context
}
func currentEvent() *Timed {
contexts.RLock()
ctx := contexts.data[goroutineId()]
contexts.RUnlock()
if ctx != nil {
ctx.Lock()
defer ctx.Unlock()
if len(ctx.events) > 0 {
return ctx.events[len(ctx.events)-1]
}
}
return nil
}
// ID returns the profiling ID for the current goroutine.
func ID() int {
return int(goroutineId())
}
// Begin enables profiling for the current goroutine.
// This function is idempotent. Any goroutine which
// calls Begin must also call End to avoid leaking
// resources.
func Begin() {
gid := goroutineId()
if gid >= 0 {
contexts.Lock()
if _, ok := contexts.data[gid]; !ok {
contexts.data[gid] = &context{}
}
contexts.Unlock()
}
}
// End removes any profiling data for this goroutine. It must
// called before the goroutine ends for each goroutine which
// called Begin(). If parent is non-zero, the events in the
// ending goroutine are added to the goroutine with the given ID.
func End(parent int) {
gid := goroutineId()
contexts.Lock()
if parent > 0 {
cur := contexts.data[gid]
if cur != nil {
p := contexts.data[int32(parent)]
if p != nil {
// We're assumming well behaved clients, which
// won't call End() with arguments creating cycles,
// so there should be no risk of deadlock here.
p.Lock()
cur.Lock()
p.events = append(p.events, cur.events...)
cur.Unlock()
p.Unlock()
}
}
}
delete(contexts.data, gid)
contexts.Unlock()
}
// Profiling returns wheter profiling has been enabled for
// this goroutine.
func Profiling() bool {
contexts.RLock()
_, ok := contexts.data[goroutineId()]
contexts.RUnlock()
return ok
}
// Start starts a timed event. Use Timed.End to terminate the
// event or Timed.AutoEnd to finish it when the request finishes
// processing. Note that if profiling is not enabled for the current
// goroutine, this function does nothing and returns an empty event.
func Start(name string) *Timed {
contexts.RLock()
ctx := contexts.data[goroutineId()]
contexts.RUnlock()
if ctx == nil {
return &Timed{}
}
ev := &Timed{name: name, started: time.Now()}
ctx.Lock()
ctx.events = append(ctx.events, ev)
ctx.Unlock()
return ev
}
// Startf is a shorthand function for calling Start and then
// Timed.Notef on the resulting Ev.
func Startf(name string, title string, format string, args ...interface{}) *Timed {
e := Start(name)
e.Notef(title, format, args...)
return e
}
// HasEvent returns true iff there's current an ongoing
// timed event for the current goroutine.
func HasEvent() bool {
return currentEvent() != nil
}
// Notef adds a note to the current Timed, as started by Start
// or Startf.
func Notef(title string, format string, args ...interface{}) {
ev := currentEvent()
if ev == nil {
log.Warningln("can't note, no ongoing event")
return
}
ev.Notef(title, format, args...)
}
// Profile is a shorthand function for calling Start(),
// executing f and the calling Timed.End() on the resulting
// Timed.
func Profile(f func(), name string) {
ev := Start(name)
f()
ev.End()
}
// Profilef is a shorthand function for calling Startf(),
// executing f and the calling Timed.End() on the resulting
// Timed.
func Profilef(f func(), title string, name string, format string, args ...interface{}) {
ev := Startf(name, title, format, args...)
f()
ev.End()
}
// Timings returns the available timings for the current
// goroutine. Note that calling Timings will end any events
// which have been set to automatically end (with Timed.AutoEnd)
// so this function should only be called at the end of the
// request lifecycle.
func Timings() []*Timing {
var timings map[string]*Timing
contexts.RLock()
ctx := contexts.data[goroutineId()]
if ctx != nil {
timings = make(map[string]*Timing)
ctx.Lock()
for _, v := range ctx.events {
if v.ended.IsZero() {
if v.autoend {
v.End()
} else {
if len(v.notes) > 0 {
log.Warningf("unfinished %q event (%s)", v.name, v.notes)
} else {
log.Warningf("unfinished %q event", v.name)
}
continue
}
}
timing := timings[v.name]
if timing == nil {
timing = &Timing{Name: v.name}
timings[v.name] = timing
}
timing.Events = append(timing.Events, &Event{v.started, v.ended, v.notes})
}
ctx.Unlock()
}
contexts.RUnlock()
ret := make([]*Timing, 0, len(timings))
for _, v := range timings {
ret = append(ret, v)
}
return ret
}