-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathaveragetimetracer.go
86 lines (72 loc) · 1.9 KB
/
averagetimetracer.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
package tracing
import (
"sync"
"github.com/sarchlab/akita/v3/sim"
)
// AverageTimeTracer 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 AverageTimeTracer struct {
timeTeller sim.TimeTeller
filter TaskFilter
lock sync.Mutex
averageTime sim.VTimeInSec
inflightTasks map[string]Task
taskCount uint64
}
// NewAverageTimeTracer creates a new AverageTimeTracer
func NewAverageTimeTracer(
timeTeller sim.TimeTeller,
filter TaskFilter,
) *AverageTimeTracer {
t := &AverageTimeTracer{
timeTeller: timeTeller,
filter: filter,
inflightTasks: make(map[string]Task),
}
return t
}
// AverageTime returns the total time has been spent on a certain type of tasks.
func (t *AverageTimeTracer) AverageTime() sim.VTimeInSec {
t.lock.Lock()
time := t.averageTime
t.lock.Unlock()
return time
}
// TotalCount returns the total number of tasks.
func (t *AverageTimeTracer) TotalCount() uint64 {
t.lock.Lock()
defer t.lock.Unlock()
return t.taskCount
}
// StartTask records the task start time
func (t *AverageTimeTracer) 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 *AverageTimeTracer) StepTask(_ Task) {
// Do nothing
}
// EndTask records the end of the task
func (t *AverageTimeTracer) EndTask(task Task) {
task.EndTime = t.timeTeller.CurrentTime()
t.lock.Lock()
originalTask, ok := t.inflightTasks[task.ID]
if !ok {
t.lock.Unlock()
return
}
taskTime := task.EndTime - originalTask.StartTime
t.averageTime = sim.VTimeInSec(
(float64(t.averageTime)*float64(t.taskCount) + float64(taskTime)) /
float64(t.taskCount+1))
delete(t.inflightTasks, task.ID)
t.taskCount++
t.lock.Unlock()
}