|
| 1 | +package com.github.grishberg.profiler.analyzer |
| 2 | + |
| 3 | +import com.android.tools.profilers.cpu.CaptureNode |
| 4 | +import com.android.tools.profilers.cpu.CpuCapture |
| 5 | +import com.android.tools.profilers.cpu.CpuThreadInfo |
| 6 | +import com.android.tools.profilers.cpu.nodemodel.CppFunctionModel |
| 7 | +import com.android.tools.profilers.cpu.nodemodel.JavaMethodModel |
| 8 | +import com.android.tools.profilers.cpu.nodemodel.NoSymbolModel |
| 9 | +import com.android.tools.profilers.cpu.nodemodel.SyscallModel |
| 10 | +import com.android.tools.profilers.cpu.simpleperf.SimpleperfTraceParser |
| 11 | +import com.github.grishberg.profiler.analyzer.converter.NameConverter |
| 12 | +import com.github.grishberg.profiler.common.AppLogger |
| 13 | +import com.github.grishberg.profiler.core.AnalyzerResult |
| 14 | +import com.github.grishberg.profiler.core.ExtendedData |
| 15 | +import java.io.File |
| 16 | +import kotlin.math.max |
| 17 | +import kotlin.math.min |
| 18 | +import kotlin.random.Random |
| 19 | + |
| 20 | +class SimplePerfAnalyzer( |
| 21 | + private val log: AppLogger, |
| 22 | + private val nameConverter: NameConverter, |
| 23 | +) { |
| 24 | + |
| 25 | + fun analyze(traceFile: File): AnalyzerResult { |
| 26 | + val parser = SimpleperfTraceParser() |
| 27 | + val traceId = Random.nextLong() |
| 28 | + try { |
| 29 | + return captureToAnalyzerResult(parser.parse(traceFile, traceId)) |
| 30 | + } catch (e: Exception) { |
| 31 | + throw WrongFormatException() |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + private fun captureToAnalyzerResult(capture: CpuCapture): AnalyzerResult { |
| 36 | + val threads = mapThreads(capture.threads) |
| 37 | + |
| 38 | + val data = mutableMapOf<Int, List<ProfileDataImpl>>() |
| 39 | + var maxLevel = 0 |
| 40 | + val threadTimeBounds = mutableMapOf<Int, ThreadTimeBoundsImpl>() |
| 41 | + val globalTimeBounds = mutableMapOf<Int, ThreadTimeBoundsImpl>() |
| 42 | + |
| 43 | + var minThreadTime = Long.MAX_VALUE |
| 44 | + var minGlobalTime = Long.MAX_VALUE |
| 45 | + |
| 46 | + for (thread in threads) { |
| 47 | + val currentThreadRoot = capture.getCaptureNode(thread.threadId) ?: continue |
| 48 | + val profileDataList = mutableListOf<ProfileDataImpl>() |
| 49 | + for (threadChild in currentThreadRoot.children) { |
| 50 | + val newRoot = mapCpuNode(threadChild) |
| 51 | + val level = processChildren(profileDataList, threadChild, newRoot) |
| 52 | + maxLevel = max(maxLevel, level) |
| 53 | + } |
| 54 | + threadTimeBounds[thread.threadId] = ThreadTimeBoundsImpl( |
| 55 | + minTime = convertTime(currentThreadRoot.startThread), maxTime = convertTime(currentThreadRoot.endThread) |
| 56 | + ) |
| 57 | + globalTimeBounds[thread.threadId] = ThreadTimeBoundsImpl( |
| 58 | + minTime = convertTime(currentThreadRoot.startGlobal), maxTime = convertTime(currentThreadRoot.endGlobal) |
| 59 | + ) |
| 60 | + |
| 61 | + minThreadTime = min(minThreadTime, currentThreadRoot.startThread) |
| 62 | + minGlobalTime = min(minGlobalTime, currentThreadRoot.startGlobal) |
| 63 | + data[thread.threadId] = profileDataList |
| 64 | + } |
| 65 | + |
| 66 | + return AnalyzerResultImpl( |
| 67 | + threadTimeBounds = threadTimeBounds, |
| 68 | + globalTimeBounds = globalTimeBounds, |
| 69 | + maxLevel = maxLevel, |
| 70 | + mutableData = data, |
| 71 | + threads = threads, |
| 72 | + mainThreadId = capture.mainThreadId, |
| 73 | + startTimeUs = -1, |
| 74 | + convertTime(minThreadTime), |
| 75 | + convertTime(minGlobalTime), |
| 76 | + ) |
| 77 | + } |
| 78 | + |
| 79 | + private fun processChildren(allNodes: MutableList<ProfileDataImpl>, node: CaptureNode, newRoot: ProfileDataImpl): Int { |
| 80 | + var maxLevel = node.depth - 1 |
| 81 | + node.children.forEachIndexed { index, captureNode -> |
| 82 | + val profileDataChild = mapCpuNode(captureNode) |
| 83 | + newRoot.addChild(profileDataChild) |
| 84 | + allNodes.add(newRoot) |
| 85 | + |
| 86 | + val level = processChildren(allNodes, captureNode, profileDataChild) |
| 87 | + maxLevel = max(maxLevel, level) |
| 88 | + |
| 89 | + } |
| 90 | + return maxLevel |
| 91 | + } |
| 92 | + |
| 93 | + private fun mapThreads(threads: Set<CpuThreadInfo>): List<ThreadItemImpl> { |
| 94 | + return threads.map { ThreadItemImpl(it.name, it.id) }.sortedBy { it.threadId } |
| 95 | + } |
| 96 | + |
| 97 | + private fun mapCpuNode(cpuNode: CaptureNode): ProfileDataImpl { |
| 98 | + val data = cpuNode.data |
| 99 | + val profileData = ProfileDataImpl( |
| 100 | + name = data.fullName, |
| 101 | + level = cpuNode.depth - THREAD_DEPTH_OFFSET, |
| 102 | + threadStartTimeInMillisecond = convertTime(cpuNode.startThread), |
| 103 | + threadEndTimeInMillisecond = convertTime(cpuNode.endThread), |
| 104 | + globalStartTimeInMillisecond = convertTime(cpuNode.startGlobal), |
| 105 | + globalEndTimeInMillisecond = convertTime(cpuNode.endGlobal), |
| 106 | + ) |
| 107 | + when (data) { |
| 108 | + is CppFunctionModel -> { |
| 109 | + profileData.extendedData = ExtendedData.CppFunctionData( |
| 110 | + tag = data.tag, |
| 111 | + id = data.id, |
| 112 | + fullName = data.fullName, |
| 113 | + classOrNamespace = data.classOrNamespace, |
| 114 | + parameters = data.parameters, |
| 115 | + isUserCode = data.isUserCode, |
| 116 | + fileName = data.fileName, |
| 117 | + vAddress = data.vAddress, |
| 118 | + ) |
| 119 | + } |
| 120 | + |
| 121 | + is JavaMethodModel -> { |
| 122 | + profileData.extendedData = ExtendedData.JavaMethodData( |
| 123 | + tag = data.tag, |
| 124 | + id = data.id, |
| 125 | + fullName = data.fullName, |
| 126 | + className = data.className, |
| 127 | + signature = data.signature, |
| 128 | + ) |
| 129 | + } |
| 130 | + |
| 131 | + is NoSymbolModel -> { |
| 132 | + profileData.extendedData = ExtendedData.NoSymbolData( |
| 133 | + tag = data.tag, |
| 134 | + id = data.id, |
| 135 | + fullName = data.fullName, |
| 136 | + isKernel = data.isKernel, |
| 137 | + ) |
| 138 | + } |
| 139 | + |
| 140 | + is SyscallModel -> { |
| 141 | + profileData.extendedData = ExtendedData.SyscallData( |
| 142 | + tag = data.tag, |
| 143 | + id = data.id, |
| 144 | + fullName = data.fullName, |
| 145 | + ) |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + return profileData |
| 150 | + } |
| 151 | + |
| 152 | + private fun convertTime(time: Long): Double { |
| 153 | + return time.toDouble() / 1000.0 |
| 154 | + } |
| 155 | + |
| 156 | + private companion object { |
| 157 | + private const val THREAD_DEPTH_OFFSET = 1 |
| 158 | + } |
| 159 | +} |
0 commit comments