-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathapi.go
209 lines (187 loc) · 3.87 KB
/
api.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
package tracing
import (
"fmt"
"reflect"
"github.com/sarchlab/akita/v3/sim"
)
// NamedHookable represent something both have a name and can be hooked
type NamedHookable interface {
sim.Named
sim.Hookable
InvokeHook(sim.HookCtx)
}
// A list of hook poses for the hooks to apply to
var (
HookPosTaskStart = &sim.HookPos{Name: "HookPosTaskStart"}
HookPosTaskStep = &sim.HookPos{Name: "HookPosTaskStep"}
HookPosTaskEnd = &sim.HookPos{Name: "HookPosTaskEnd"}
)
// StartTask notifies the hooks that hook to the domain about the start of a
// task.
func StartTask(
id string,
parentID string,
domain NamedHookable,
kind string,
what string,
detail interface{},
) {
StartTaskWithSpecificLocation(
id,
parentID,
domain,
kind,
what,
domain.Name(),
detail,
)
}
func allRequiredFieldsMustBeNotEmpty(
id string,
domain NamedHookable,
kind string,
what string,
) {
if id == "" {
panic("id must not be empty")
}
if domain == nil {
panic("domain must not be nil")
}
if kind == "" {
panic("kind must not be empty")
}
if what == "" {
panic("what must not be empty")
}
}
func domainMustHaveName(domain NamedHookable) {
if domain.Name() == "" {
panic("domain must have a name")
}
}
// StartTaskWithSpecificLocation notifies the hooks that hook to the domain
// about the start of a task, and is able to customize `where` field of a task,
// especially for network tracing.
func StartTaskWithSpecificLocation(
id string,
parentID string,
domain NamedHookable,
kind string,
what string,
location string,
detail interface{},
) {
if domain.NumHooks() == 0 {
return
}
allRequiredFieldsMustBeNotEmpty(id, domain, kind, what)
domainMustHaveName(domain)
task := Task{
ID: id,
ParentID: parentID,
Kind: kind,
What: what,
Where: location,
Detail: detail,
}
ctx := sim.HookCtx{
Domain: domain,
Item: task,
Pos: HookPosTaskStart,
}
domain.InvokeHook(ctx)
}
// AddTaskStep marks that a milestone has been reached when processing a task.
func AddTaskStep(
id string,
domain NamedHookable,
what string,
) {
if domain.NumHooks() == 0 {
return
}
step := TaskStep{
What: what,
}
task := Task{
ID: id,
Steps: []TaskStep{step},
}
ctx := sim.HookCtx{
Domain: domain,
Item: task,
Pos: HookPosTaskStep,
}
domain.InvokeHook(ctx)
}
// EndTask notifies the hooks about the end of a task.
func EndTask(
id string,
domain NamedHookable,
) {
if domain.NumHooks() == 0 {
return
}
task := Task{
ID: id,
}
ctx := sim.HookCtx{
Domain: domain,
Item: task,
Pos: HookPosTaskEnd,
}
domain.InvokeHook(ctx)
}
// MsgIDAtReceiver generates a standard ID for the message task at the
// message receiver.
func MsgIDAtReceiver(msg sim.Msg, domain NamedHookable) string {
return fmt.Sprintf("%s@%s", msg.Meta().ID, domain.Name())
}
// TraceReqInitiate generatse a new task. The new task has Type="req_out",
// What=[the type name of the message]. This function is to be called by the
// sender of the message.
func TraceReqInitiate(
msg sim.Msg,
domain NamedHookable,
taskParentID string,
) {
StartTask(
msg.Meta().ID+"_req_out",
taskParentID,
domain,
"req_out",
reflect.TypeOf(msg).String(),
msg,
)
}
// TraceReqReceive generates a new task for the message handling. The kind of
// the task is always "req_in".
func TraceReqReceive(
msg sim.Msg,
domain NamedHookable,
) {
StartTask(
MsgIDAtReceiver(msg, domain),
msg.Meta().ID+"_req_out",
domain,
"req_in",
reflect.TypeOf(msg).String(),
msg,
)
}
// TraceReqComplete terminates the message handling task.
func TraceReqComplete(
msg sim.Msg,
domain NamedHookable,
) {
EndTask(MsgIDAtReceiver(msg, domain), domain)
}
// TraceReqFinalize terminates the message task. This function should be called
// when the sender receives the response.
func TraceReqFinalize(
msg sim.Msg,
domain NamedHookable,
) {
EndTask(msg.Meta().ID+"_req_out", domain)
}