forked from sarchlab/akita
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponentinfo.go
429 lines (359 loc) · 8.99 KB
/
componentinfo.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
package main
import (
"encoding/json"
"log"
"net/http"
"sort"
"strconv"
"github.com/sarchlab/akita/v3/tracing"
)
type TimeValue struct {
Time float64 `json:"time"`
Value float64 `json:"value"`
}
type ComponentInfo struct {
Name string `json:"name"`
InfoType string `json:"info_type"`
StartTime float64 `json:"start_time"`
EndTime float64 `json:"end_time"`
Data []TimeValue `json:"data"`
}
func httpComponentNames(w http.ResponseWriter, r *http.Request) {
componentNames := traceReader.ListComponents()
rsp, err := json.Marshal(componentNames)
dieOnErr(err)
_, err = w.Write(rsp)
dieOnErr(err)
}
func httpComponentInfo(w http.ResponseWriter, r *http.Request) {
compName := r.FormValue("where")
infoType := r.FormValue("info_type")
startTime, err := strconv.ParseFloat(r.FormValue("start_time"), 64)
dieOnErr(err)
endTime, err := strconv.ParseFloat(r.FormValue("end_time"), 64)
dieOnErr(err)
numDots, err := strconv.ParseInt(r.FormValue("num_dots"), 10, 32)
dieOnErr(err)
var compInfo *ComponentInfo
switch infoType {
case "ReqInCount":
compInfo = calculateReqIn(
compName, startTime, endTime, int(numDots))
case "ReqCompleteCount":
compInfo = calculateReqComplete(
compName, startTime, endTime, int(numDots))
case "AvgLatency":
compInfo = calculateAvgLatency(
compName, startTime, endTime, int(numDots))
case "ConcurrentTask":
compInfo = calculateTimeWeightedTaskCount(
compName, infoType,
startTime, endTime, int(numDots),
func(t tracing.Task) bool { return true },
func(t tracing.Task) float64 { return float64(t.StartTime) },
func(t tracing.Task) float64 { return float64(t.EndTime) },
)
case "BufferPressure":
compInfo = calculateTimeWeightedTaskCount(
compName, infoType,
startTime, endTime, int(numDots),
taskIsReqIn,
func(t tracing.Task) float64 {
return float64(t.ParentTask.StartTime)
},
func(t tracing.Task) float64 {
return float64(t.StartTime)
},
)
case "PendingReqOut":
compInfo = calculateTimeWeightedTaskCount(
compName, infoType,
startTime, endTime, int(numDots),
func(t tracing.Task) bool { return t.Kind == "req_out" },
func(t tracing.Task) float64 { return float64(t.StartTime) },
func(t tracing.Task) float64 { return float64(t.EndTime) },
)
default:
log.Panicf("unknown info_type %s\n", infoType)
}
rsp, err := json.Marshal(compInfo)
dieOnErr(err)
_, err = w.Write(rsp)
dieOnErr(err)
}
func taskIsReqIn(t tracing.Task) bool {
return t.Kind == "req_in" && t.ParentTask != nil
}
func calculateReqIn(
compName string,
startTime, endTime float64,
numDots int,
) *ComponentInfo {
info := &ComponentInfo{
Name: compName,
InfoType: "req_in",
StartTime: startTime,
EndTime: endTime,
}
query := tracing.TaskQuery{
Where: compName,
Kind: "req_in",
EnableTimeRange: true,
StartTime: startTime,
EndTime: endTime,
EnableParentTask: true,
}
reqs := traceReader.ListTasks(query)
totalDuration := endTime - startTime
binDuration := totalDuration / float64(numDots)
for i := 0; i < numDots; i++ {
binStartTime := float64(i)*binDuration + startTime
binEndTime := float64(i+1)*binDuration + startTime
reqCount := 0
for _, r := range reqs {
if float64(r.StartTime) > binStartTime &&
float64(r.StartTime) < binEndTime {
reqCount++
}
}
tv := TimeValue{
Time: binStartTime + 0.5*binDuration,
Value: float64(reqCount) / binDuration,
}
info.Data = append(info.Data, tv)
}
return info
}
func calculateReqComplete(
compName string,
startTime, endTime float64,
numDots int,
) *ComponentInfo {
info := &ComponentInfo{
Name: compName,
InfoType: "req_complete",
StartTime: startTime,
EndTime: endTime,
}
query := tracing.TaskQuery{
Where: compName,
Kind: "req_in",
EnableTimeRange: true,
StartTime: startTime,
EndTime: endTime,
EnableParentTask: true,
}
reqs := traceReader.ListTasks(query)
totalDuration := endTime - startTime
binDuration := totalDuration / float64(numDots)
for i := 0; i < numDots; i++ {
binStartTime := float64(i)*binDuration + startTime
binEndTime := float64(i+1)*binDuration + startTime
reqCount := 0
for _, r := range reqs {
if float64(r.EndTime) > binStartTime &&
float64(r.EndTime) < binEndTime {
reqCount++
}
}
tv := TimeValue{
Time: binStartTime + 0.5*binDuration,
Value: float64(reqCount) / binDuration,
}
info.Data = append(info.Data, tv)
}
return info
}
func calculateAvgLatency(
compName string,
startTime, endTime float64,
numDots int,
) *ComponentInfo {
info := &ComponentInfo{
Name: compName,
InfoType: "avg_latency",
StartTime: startTime,
EndTime: endTime,
}
query := tracing.TaskQuery{
Where: compName,
Kind: "req_in",
EnableTimeRange: true,
StartTime: startTime,
EndTime: endTime,
EnableParentTask: true,
}
reqs := traceReader.ListTasks(query)
totalDuration := endTime - startTime
binDuration := totalDuration / float64(numDots)
for i := 0; i < numDots; i++ {
binStartTime := float64(i)*binDuration + startTime
binEndTime := float64(i+1)*binDuration + startTime
sum := 0.0
reqCount := 0
for _, r := range reqs {
if float64(r.EndTime) > binStartTime &&
float64(r.EndTime) < binEndTime {
sum += float64(r.EndTime - r.StartTime)
reqCount++
}
}
value := 0.0
if reqCount > 0 {
value = sum / float64(reqCount)
}
tv := TimeValue{
Time: binStartTime + 0.5*binDuration,
Value: value,
}
info.Data = append(info.Data, tv)
}
return info
}
type timestamp struct {
time float64
isStart bool
}
type timestamps []timestamp
func (ts timestamps) Len() int {
return len(ts)
}
func (ts timestamps) Less(i, j int) bool {
return ts[i].time < ts[j].time
}
func (ts timestamps) Swap(i, j int) {
ts[i], ts[j] = ts[j], ts[i]
}
type taskFilter func(t tracing.Task) bool
type taskTime func(t tracing.Task) float64
func calculateTimeWeightedTaskCount(
compName, infoType string,
startTime, endTime float64,
numDots int,
filter taskFilter,
increaseTime, decreaseTime taskTime,
) *ComponentInfo {
info := &ComponentInfo{
Name: compName,
InfoType: infoType,
StartTime: startTime,
EndTime: endTime,
}
query := tracing.TaskQuery{
Where: compName,
EnableTimeRange: true,
StartTime: startTime,
EndTime: endTime,
EnableParentTask: true,
}
tasks := traceReader.ListTasks(query)
tasks = filterTask(tasks, filter)
totalDuration := endTime - startTime
binDuration := totalDuration / float64(numDots)
for i := 0; i < numDots; i++ {
binStartTime := float64(i)*binDuration + startTime
binEndTime := float64(i+1)*binDuration + startTime
tasksInBin := getTasksInBin(
tasks,
binStartTime, binEndTime,
increaseTime, decreaseTime,
)
timestamps := taskToTimeStamps(tasksInBin, increaseTime, decreaseTime)
avgCount := calculateAvgTaskCount(
timestamps, binStartTime, binEndTime)
tv := TimeValue{
Time: binStartTime + 0.5*binDuration,
Value: avgCount,
}
info.Data = append(info.Data, tv)
}
return info
}
func filterTask(tasks []tracing.Task, filter taskFilter) []tracing.Task {
filteredTasks := []tracing.Task{}
for _, t := range tasks {
if filter(t) {
filteredTasks = append(filteredTasks, t)
}
}
return filteredTasks
}
func calculateAvgTaskCount(
timestamps timestamps,
binStartTime, binEndTime float64,
) float64 {
var count int
var timeByCount float64
prevTime := binStartTime
for _, ts := range timestamps {
if ts.time < binStartTime {
if ts.isStart {
count++
} else {
count--
}
continue
} else if ts.time >= binEndTime {
break
} else {
duration := ts.time - prevTime
if duration < 0 {
panic("duration is smaller than 0")
}
timeByCount += duration * float64(count)
prevTime = ts.time
if ts.isStart {
count++
} else {
count--
}
}
}
duration := binEndTime - prevTime
timeByCount += duration * float64(count)
avgCount := timeByCount / (binEndTime - binStartTime)
return avgCount
}
func taskToTimeStamps(
tasks []tracing.Task,
taskStart, taskEnd taskTime,
) []timestamp {
timestampList := make(timestamps, 0, len(tasks)*2)
for _, t := range tasks {
timestampStart := timestamp{
time: taskStart(t),
isStart: true,
}
timestampEnd := timestamp{
time: taskEnd(t),
}
timestampList = append(timestampList, timestampStart, timestampEnd)
}
sort.Sort(timestampList)
return timestampList
}
func getTasksInBin(
tasks []tracing.Task,
binStart, binEnd float64,
taskStart, taskEnd taskTime,
) (tasksInBin []tracing.Task) {
for _, t := range tasks {
if isTaskOverlapsWithBin(t, binStart, binEnd, taskStart, taskEnd) {
tasksInBin = append(tasksInBin, t)
}
}
return tasksInBin
}
func isTaskOverlapsWithBin(
t tracing.Task,
binStart, binEnd float64,
taskStart, taskEnd taskTime,
) bool {
if taskEnd(t) < binStart {
return false
}
if taskStart(t) > binEnd {
return false
}
return true
}