1 /*
2  * Copyright 2018 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     https://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package trebuchet.model.fragments
18 
19 import trebuchet.model.InvalidId
20 import trebuchet.model.base.Slice
21 
22 data class AsyncSlice(
23     override val startTime: Double,
24     override var endTime: Double,
25     override val name: String,
26     val cookie: Int,
27     override var didNotFinish: Boolean,
28     val startThreadId: Int,
29     var endThreadId: Int
30 ) : Slice
31 
32 class AsyncSlicesBuilder {
33     val openSlices = mutableMapOf<String, AsyncSlice>()
34     val asyncSlices = mutableListOf<AsyncSlice>()
35 
keynull36     private fun key(name: String, cookie: Int): String {
37         return "$name:$cookie"
38     }
39 
openAsyncSlicenull40     fun openAsyncSlice(pid: Int, name: String, cookie: Int, startTime: Double) {
41         openSlices[key(name, cookie)] = AsyncSlice(startTime, Double.NaN, name, cookie,
42             true, pid, InvalidId)
43     }
44 
closeAsyncSlicenull45     fun closeAsyncSlice(pid: Int, name: String, cookie: Int, endTime: Double) {
46         val slice = openSlices.remove(key(name, cookie)) ?: return
47         slice.endTime = endTime
48         slice.didNotFinish = false
49         slice.endThreadId = pid
50         asyncSlices.add(slice)
51     }
52 
autoCloseOpenSlicesnull53     fun autoCloseOpenSlices(maxTimestamp: Double) {
54         for (slice in openSlices.values) {
55             slice.endTime = maxTimestamp
56             slice.didNotFinish = true
57             slice.endThreadId = InvalidId
58             asyncSlices.add(slice)
59         }
60         openSlices.clear()
61     }
62 
63 }