-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdbtracer.go
118 lines (91 loc) · 2.37 KB
/
dbtracer.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
package tracing
import (
"github.com/sarchlab/akita/v3/sim"
"github.com/tebeka/atexit"
)
// TracerBackend is a backend that can store tasks.
type TracerBackend interface {
// Write writes a task to the storage.
Write(task Task)
// Flush flushes the tasks to the storage, in case if the backend buffers
// the tasks.
Flush()
}
// DBTracer is a tracer that can store tasks into a database.
// DBTracers can connect with different backends so that the tasks can be stored
// in different types of databases (e.g., CSV files, SQL databases, etc.)
type DBTracer struct {
timeTeller sim.TimeTeller
backend TracerBackend
startTime, endTime sim.VTimeInSec
tracingTasks map[string]Task
}
// StartTask marks the start of a task.
func (t *DBTracer) StartTask(task Task) {
t.startingTaskMustBeValid(task)
task.StartTime = t.timeTeller.CurrentTime()
if t.endTime > 0 && task.StartTime > t.endTime {
return
}
t.tracingTasks[task.ID] = task
}
func (t *DBTracer) startingTaskMustBeValid(task Task) {
if task.ID == "" {
panic("task ID must be set")
}
if task.Kind == "" {
panic("task kind must be set")
}
if task.What == "" {
panic("task what must be set")
}
if task.Where == "" {
panic("task where must be set")
}
}
// StepTask marks a step of a task.
func (t *DBTracer) StepTask(_ Task) {
// Do nothing for now.
}
// EndTask marks the end of a task.
func (t *DBTracer) EndTask(task Task) {
task.EndTime = t.timeTeller.CurrentTime()
if t.startTime > 0 && task.EndTime < t.startTime {
delete(t.tracingTasks, task.ID)
return
}
originalTask, ok := t.tracingTasks[task.ID]
if !ok {
return
}
originalTask.EndTime = task.EndTime
delete(t.tracingTasks, task.ID)
t.backend.Write(originalTask)
}
// Terminate terminates the tracer.
func (t *DBTracer) Terminate() {
for _, task := range t.tracingTasks {
task.EndTime = t.timeTeller.CurrentTime()
t.backend.Write(task)
}
t.tracingTasks = nil
t.backend.Flush()
}
// NewDBTracer creates a new DBTracer.
func NewDBTracer(
timeTeller sim.TimeTeller,
backend TracerBackend,
) *DBTracer {
t := &DBTracer{
timeTeller: timeTeller,
backend: backend,
tracingTasks: make(map[string]Task),
}
atexit.Register(func() { t.Terminate() })
return t
}
// SetTimeRange sets the time range of the tracer.
func (t *DBTracer) SetTimeRange(startTime, endTime sim.VTimeInSec) {
t.startTime = startTime
t.endTime = endTime
}