forked from sarchlab/akita
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbusytimetracer.go
197 lines (156 loc) · 3.88 KB
/
busytimetracer.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
package tracing
import (
"container/list"
"github.com/sarchlab/akita/v3/sim"
)
type taskTimeStartEnd struct {
start, end sim.VTimeInSec
completed bool
}
// BusyTimeTracer traces the that a domain is processing a kind of task. If the
// task processing time overlaps, this tracer only consider one instance of the
// overlapped time.
type BusyTimeTracer struct {
timeTeller sim.TimeTeller
filter TaskFilter
inflightTasks map[string]*list.Element
taskTimes *list.List
busyTime sim.VTimeInSec
}
// NewBusyTimeTracer creates a new BusyTimeTracer
func NewBusyTimeTracer(
timeTeller sim.TimeTeller,
filter TaskFilter,
) *BusyTimeTracer {
t := &BusyTimeTracer{
timeTeller: timeTeller,
filter: filter,
inflightTasks: make(map[string]*list.Element),
taskTimes: list.New(),
}
t.taskTimes.Init()
return t
}
// BusyTime returns the total time has been spent on a certain type of tasks.
func (t *BusyTimeTracer) BusyTime() sim.VTimeInSec {
return t.busyTime
}
// TerminateAllTasks will mark all the tasks as completed.
func (t *BusyTimeTracer) TerminateAllTasks(now sim.VTimeInSec) {
for e := t.taskTimes.Front(); e != nil; e = e.Next() {
task := e.Value.(*taskTimeStartEnd)
if !task.completed {
task.completed = true
task.end = now
}
}
t.collapse(now)
}
func (t *BusyTimeTracer) extendTaskTime(
base *taskTimeStartEnd,
t2 *taskTimeStartEnd,
) {
if t2.start < base.start {
base.start = t2.start
}
if t2.end > base.end {
base.end = t2.end
}
}
// StartTask records the task start time
func (t *BusyTimeTracer) StartTask(task Task) {
task.StartTime = t.timeTeller.CurrentTime()
if t.filter != nil && !t.filter(task) {
return
}
taskTime := &taskTimeStartEnd{start: task.StartTime}
elem := t.taskTimes.PushBack(taskTime)
t.inflightTasks[task.ID] = elem
}
// StepTask does nothing
func (t *BusyTimeTracer) StepTask(_ Task) {
// Do nothing
}
// EndTask records the end of the task
func (t *BusyTimeTracer) EndTask(task Task) {
task.EndTime = t.timeTeller.CurrentTime()
originalTask, ok := t.inflightTasks[task.ID]
if !ok {
return
}
time := originalTask.Value.(*taskTimeStartEnd)
time.end = task.EndTime
time.completed = true
delete(t.inflightTasks, task.ID)
t.collapse(task.EndTime)
}
func (t *BusyTimeTracer) collapse(now sim.VTimeInSec) {
time, found := t.startTimeOfFirstImcompleteTask()
if found && time < now {
return
}
finishedTasks := make([]*taskTimeStartEnd, 0)
var next *list.Element
for e := t.taskTimes.Front(); e != nil; e = next {
next = e.Next()
task := e.Value.(*taskTimeStartEnd)
if !task.completed {
break
}
if task.completed && task.end <= now {
finishedTasks = append(finishedTasks, task)
t.taskTimes.Remove(e)
}
}
t.busyTime += t.taskBusyTime(finishedTasks)
}
func (t *BusyTimeTracer) startTimeOfFirstImcompleteTask() (
sim.VTimeInSec, bool,
) {
for e := t.taskTimes.Front(); e != nil; e = e.Next() {
task := e.Value.(*taskTimeStartEnd)
if !task.completed {
return task.start, true
}
}
return 0, false
}
func (t *BusyTimeTracer) taskBusyTime(
tasks []*taskTimeStartEnd,
) sim.VTimeInSec {
busyTime := sim.VTimeInSec(0.0)
coveredMask := make(map[int]bool)
for i, t1 := range tasks {
if _, covered := coveredMask[i]; covered {
continue
}
coveredMask[i] = true
extTime := taskTimeStartEnd{
start: t1.start,
end: t1.end,
}
for j, t2 := range tasks {
if _, covered := coveredMask[j]; covered {
continue
}
if t.taskTimeOverlap(t1, t2) {
coveredMask[j] = true
t.extendTaskTime(&extTime, t2)
}
}
busyTime += extTime.end - extTime.start
}
return busyTime
}
func (t *BusyTimeTracer) taskTimeOverlap(t1, t2 *taskTimeStartEnd) bool {
if t1.start <= t2.start && t1.end >= t2.start {
return true
}
if t1.start <= t2.end && t1.end >= t2.end {
return true
}
if t1.start >= t2.start && t1.end <= t2.end {
return true
}
return false
}