-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtotaltimetracer.go
73 lines (61 loc) · 1.56 KB
/
totaltimetracer.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
package tracing
import (
"sync"
"github.com/sarchlab/akita/v3/sim"
)
// TotalTimeTracer can collect the total time of executing a certain type of
// task. If the execution of two tasks overlaps, this tracer will simply add
// the two task processing time together.
type TotalTimeTracer struct {
timeTeller sim.TimeTeller
filter TaskFilter
lock sync.Mutex
totalTime sim.VTimeInSec
inflightTasks map[string]Task
}
// NewTotalTimeTracer creates a new TotalTimeTracer
func NewTotalTimeTracer(
timeTeller sim.TimeTeller,
filter TaskFilter,
) *TotalTimeTracer {
t := &TotalTimeTracer{
timeTeller: timeTeller,
filter: filter,
inflightTasks: make(map[string]Task),
}
return t
}
// TotalTime returns the total time has been spent on a certain type of tasks.
func (t *TotalTimeTracer) TotalTime() sim.VTimeInSec {
t.lock.Lock()
time := t.totalTime
t.lock.Unlock()
return time
}
// StartTask records the task start time
func (t *TotalTimeTracer) StartTask(task Task) {
task.StartTime = t.timeTeller.CurrentTime()
if !t.filter(task) {
return
}
t.lock.Lock()
t.inflightTasks[task.ID] = task
t.lock.Unlock()
}
// StepTask does nothing
func (t *TotalTimeTracer) StepTask(_ Task) {
// Do nothing
}
// EndTask records the end of the task
func (t *TotalTimeTracer) EndTask(task Task) {
task.EndTime = t.timeTeller.CurrentTime()
t.lock.Lock()
originalTask, ok := t.inflightTasks[task.ID]
if !ok {
t.lock.Unlock()
return
}
t.totalTime += task.EndTime - originalTask.StartTime
delete(t.inflightTasks, task.ID)
t.lock.Unlock()
}