-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresources.go
274 lines (241 loc) · 7.99 KB
/
resources.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Copyright 2023 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 trace
import "fmt"
// ThreadID is the runtime-internal M structure's ID. This is unique
// for each OS thread.
type ThreadID int64
// NoThread indicates that the relevant events don't correspond to any
// thread in particular.
const NoThread = ThreadID(-1)
// ProcID is the runtime-internal G structure's id field. This is unique
// for each P.
type ProcID int64
// NoProc indicates that the relevant events don't correspond to any
// P in particular.
const NoProc = ProcID(-1)
// GoID is the runtime-internal G structure's goid field. This is unique
// for each goroutine.
type GoID int64
// NoGoroutine indicates that the relevant events don't correspond to any
// goroutine in particular.
const NoGoroutine = GoID(-1)
// GoState represents the state of a goroutine.
//
// New GoStates may be added in the future. Users of this type must be robust
// to that possibility.
type GoState uint8
const (
GoUndetermined GoState = iota // No information is known about the goroutine.
GoNotExist // Goroutine does not exist.
GoRunnable // Goroutine is runnable but not running.
GoRunning // Goroutine is running.
GoWaiting // Goroutine is waiting on something to happen.
GoSyscall // Goroutine is in a system call.
)
// Executing returns true if the state indicates that the goroutine is executing
// and bound to its thread.
func (s GoState) Executing() bool {
return s == GoRunning || s == GoSyscall
}
// String returns a human-readable representation of a GoState.
//
// The format of the returned string is for debugging purposes and is subject to change.
func (s GoState) String() string {
switch s {
case GoUndetermined:
return "Undetermined"
case GoNotExist:
return "NotExist"
case GoRunnable:
return "Runnable"
case GoRunning:
return "Running"
case GoWaiting:
return "Waiting"
case GoSyscall:
return "Syscall"
}
return "Bad"
}
// ProcState represents the state of a proc.
//
// New ProcStates may be added in the future. Users of this type must be robust
// to that possibility.
type ProcState uint8
const (
ProcUndetermined ProcState = iota // No information is known about the proc.
ProcNotExist // Proc does not exist.
ProcRunning // Proc is running.
ProcIdle // Proc is idle.
)
// Executing returns true if the state indicates that the proc is executing
// and bound to its thread.
func (s ProcState) Executing() bool {
return s == ProcRunning
}
// String returns a human-readable representation of a ProcState.
//
// The format of the returned string is for debugging purposes and is subject to change.
func (s ProcState) String() string {
switch s {
case ProcUndetermined:
return "Undetermined"
case ProcNotExist:
return "NotExist"
case ProcRunning:
return "Running"
case ProcIdle:
return "Idle"
}
return "Bad"
}
// ResourceKind indicates a kind of resource that has a state machine.
//
// New ResourceKinds may be added in the future. Users of this type must be robust
// to that possibility.
type ResourceKind uint8
const (
ResourceNone ResourceKind = iota // No resource.
ResourceGoroutine // Goroutine.
ResourceProc // Proc.
ResourceThread // Thread.
)
// String returns a human-readable representation of a ResourceKind.
//
// The format of the returned string is for debugging purposes and is subject to change.
func (r ResourceKind) String() string {
switch r {
case ResourceNone:
return "None"
case ResourceGoroutine:
return "Goroutine"
case ResourceProc:
return "Proc"
case ResourceThread:
return "Thread"
}
return "Bad"
}
// ResourceID represents a generic resource ID.
type ResourceID struct {
// Kind is the kind of resource this ID is for.
Kind ResourceKind
id int64
}
// MakeResourceID creates a general resource ID from a specific resource's ID.
func MakeResourceID[T interface{ GoID | ProcID | ThreadID }](id T) ResourceID {
var rd ResourceID
var a any = id
switch a.(type) {
case GoID:
rd.Kind = ResourceGoroutine
case ProcID:
rd.Kind = ResourceProc
case ThreadID:
rd.Kind = ResourceThread
}
rd.id = int64(id)
return rd
}
// Goroutine obtains a GoID from the resource ID.
//
// r.Kind must be ResourceGoroutine or this function will panic.
func (r ResourceID) Goroutine() GoID {
if r.Kind != ResourceGoroutine {
panic(fmt.Sprintf("attempted to get GoID from %s resource ID", r.Kind))
}
return GoID(r.id)
}
// Proc obtains a ProcID from the resource ID.
//
// r.Kind must be ResourceProc or this function will panic.
func (r ResourceID) Proc() ProcID {
if r.Kind != ResourceProc {
panic(fmt.Sprintf("attempted to get ProcID from %s resource ID", r.Kind))
}
return ProcID(r.id)
}
// Thread obtains a ThreadID from the resource ID.
//
// r.Kind must be ResourceThread or this function will panic.
func (r ResourceID) Thread() ThreadID {
if r.Kind != ResourceThread {
panic(fmt.Sprintf("attempted to get ThreadID from %s resource ID", r.Kind))
}
return ThreadID(r.id)
}
// String returns a human-readable string representation of the ResourceID.
//
// This representation is subject to change and is intended primarily for debugging.
func (r ResourceID) String() string {
if r.Kind == ResourceNone {
return r.Kind.String()
}
return fmt.Sprintf("%s(%d)", r.Kind, r.id)
}
// StateTransition provides details about a StateTransition event.
type StateTransition struct {
// Resource is the resource this state transition is for.
Resource ResourceID
// Reason is a human-readable reason for the state transition.
Reason string
// Stack is the stack trace of the resource making the state transition.
//
// This is distinct from the result (Event).Stack because it pertains to
// the transitioning resource, not any of the ones executing the event
// this StateTransition came from.
//
// An example of this difference is the NotExist -> Runnable transition for
// goroutines, which indicates goroutine creation. In this particular case,
// a Stack here would refer to the starting stack of the new goroutine, and
// an (Event).Stack would refer to the stack trace of whoever created the
// goroutine.
Stack Stack
// The actual transition data. Stored in a neutral form so that
// we don't need fields for every kind of resource.
id int64
oldState uint8
newState uint8
}
func goStateTransition(id GoID, from, to GoState) StateTransition {
return StateTransition{
Resource: ResourceID{Kind: ResourceGoroutine, id: int64(id)},
oldState: uint8(from),
newState: uint8(to),
}
}
func procStateTransition(id ProcID, from, to ProcState) StateTransition {
return StateTransition{
Resource: ResourceID{Kind: ResourceProc, id: int64(id)},
oldState: uint8(from),
newState: uint8(to),
}
}
// Goroutine returns the state transition for a goroutine.
//
// Transitions to and from states that are Executing are special in that
// they change the future execution context. In other words, future events
// on the same thread will feature the same goroutine until it stops running.
//
// Panics if d.Resource.Kind is not ResourceGoroutine.
func (d StateTransition) Goroutine() (from, to GoState) {
if d.Resource.Kind != ResourceGoroutine {
panic("Goroutine called on non-Goroutine state transition")
}
return GoState(d.oldState), GoState(d.newState)
}
// Proc returns the state transition for a proc.
//
// Transitions to and from states that are Executing are special in that
// they change the future execution context. In other words, future events
// on the same thread will feature the same goroutine until it stops running.
//
// Panics if d.Resource.Kind is not ResourceProc.
func (d StateTransition) Proc() (from, to ProcState) {
if d.Resource.Kind != ResourceProc {
panic("Proc called on non-Proc state transition")
}
return ProcState(d.oldState), ProcState(d.newState)
}