1 /*
2  * Copyright 2019 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.analyzer.startup
18 
19 import getStartupEvents
20 import org.junit.Test
21 import trebuchet.io.BufferProducer
22 import trebuchet.io.DataSlice
23 import trebuchet.io.asSlice
24 import trebuchet.model.Model
25 import trebuchet.task.ImportTask
26 import trebuchet.util.PrintlnImportFeedback
27 
parseStringnull28 fun parseString(source: String): Model {
29     val task = ImportTask(PrintlnImportFeedback())
30     return task.import(object : BufferProducer {
31         var hasRead = false
32         override fun next(): DataSlice? {
33             if (hasRead) return null
34             hasRead = true
35             return source.asSlice()
36         }
37     })
38 }
39 
40 class StartupCommonTest {
41     @Test
testEmptyThreadStartnull42     fun testEmptyThreadStart() {
43         // This is a reduced trace based on one seen in the wild that used to
44         // crash because it creates a thread with no slices assigned to it.
45         val test = """
46 TRACE:
47 # tracer: nop
48 #
49 # entries-in-buffer/entries-written: 289801/289801   #P:4
50 #
51 #                                      _-----=> irqs-off
52 #                                     / _----=> need-resched
53 #                                    | / _---=> hardirq/softirq
54 #                                    || / _--=> preempt-depth
55 #                                    ||| /     delay
56 #           TASK-PID    TGID   CPU#  ||||    TIMESTAMP  FUNCTION
57 #              | |        |      |   ||||       |         |
58    Binder:980_13-3951  (  980) [002] ...1  1628.269667: tracing_mark_write: E|980
59    Binder:980_13-3951  (  980) [002] ...2  1628.270068: tracing_mark_write: S|980|launching: com.example.eholk.myfirstapp|0
60            <...>-1104  (-----) [000] ...1  1628.292040: tracing_mark_write: B|980|Start proc: com.example.eholk.myfirstapp
61            <...>-1103  (-----) [002] d..4  1628.300748: sched_waking: comm=system_server pid=980 prio=118 target_cpu=001
62     RenderThread-9817  ( 9793) [003] d..3  1628.457017: sched_waking: comm=holk.myfirstapp pid=9793 prio=110 target_cpu=002
63     RenderThread-9817  ( 9793) [003] ...1  1628.457191: tracing_mark_write: B|9793|Thread birth
64         """
65 
66         val trace = parseString(test)
67         trace.getStartupEvents()
68     }
69 }
70