forked from sarchlab/akita
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracehook.go
41 lines (35 loc) · 861 Bytes
/
tracehook.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
package tracing
import (
"fmt"
"reflect"
"github.com/sarchlab/akita/v3/sim"
)
// CollectTrace let the tracer to collect trace from a domain
func CollectTrace(domain NamedHookable, tracer Tracer) {
hooks := domain.Hooks()
for _, hook := range hooks {
hook, ok := hook.(*traceHook)
if ok && hook.t == tracer {
panic(fmt.Sprintf(
"domain %s already has tracer %s",
domain.Name(), reflect.TypeOf(tracer)))
}
}
h := traceHook{t: tracer}
domain.AcceptHook(&h)
}
// A traceHook is a hook that traces tasks
type traceHook struct {
t Tracer
}
// Func calls the tracer interfaces when the hook is triggered
func (h *traceHook) Func(ctx sim.HookCtx) {
switch ctx.Pos {
case HookPosTaskStart:
h.t.StartTask(ctx.Item.(Task))
case HookPosTaskStep:
h.t.StepTask(ctx.Item.(Task))
case HookPosTaskEnd:
h.t.EndTask(ctx.Item.(Task))
}
}