[6.2.0][技术优化] 更改Handler消息记录方式,对编译生成的类方法集合进行过滤
This commit is contained in:
@@ -52,13 +52,13 @@ internal class MoGoBlockProviderImpl: IMoGoBlockProvider, IBlockListener {
|
||||
map["pending"] = data[1] ?: emptyList()
|
||||
val cpu = CallerDevaToolsManager.usage()?.dump()
|
||||
if (!cpu.isNullOrEmpty()) {
|
||||
val mainThreadCpuUsage = cpu.remove("MainThreadCpuUsage")
|
||||
val processLaunchedTime = cpu.remove("ProcessLaunchedTime")
|
||||
if (mainThreadCpuUsage != null && processLaunchedTime != null) {
|
||||
linkedLog.recordCpuUsage(LinkedHashMap<String, String>().also {
|
||||
it["MainThread"] = "${ mainThreadCpuUsage * 1.0f * 100 / processLaunchedTime }%"
|
||||
val mainThreadUsage = cpu.remove("MainThreadUsage")
|
||||
val processUsage = cpu.remove("ProcessUsage")
|
||||
if (mainThreadUsage != null && processUsage != null) {
|
||||
linkedLog.recordCpuUsage(LinkedHashMap<String, String>().also { itx ->
|
||||
itx["MainThread"] = "${ "%.2f".format(mainThreadUsage * 1.0f * 100 / processUsage) }% ($mainThreadUsage, $processUsage)"
|
||||
cpu.entries.sortedByDescending { it.value }.forEach { sorted ->
|
||||
it[sorted.key] = "${ sorted.value * 1.0f * 100 / processLaunchedTime }%"
|
||||
itx[sorted.key] = "${ "%.2f".format(sorted.value * 1.0f * 100 / processUsage) }% (${sorted.value}, $processUsage)"
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.zhjt.mogo_core_function_devatools.perf
|
||||
|
||||
import android.os.Debug
|
||||
import android.os.Looper
|
||||
import android.os.SystemClock
|
||||
import com.mogo.eagle.core.function.api.devatools.perf.IMoGoCpuUsageProvider
|
||||
import java.util.concurrent.locks.ReentrantLock
|
||||
@@ -11,53 +13,67 @@ internal class MoGoCpuUsageProviderImpl: IMoGoCpuUsageProvider {
|
||||
|
||||
private var mainThreadStartTime : Long = 0L
|
||||
|
||||
private var mainThreadLastTime: Long = 0L
|
||||
|
||||
private var mainThreadCpuUsage: Long = 0L
|
||||
|
||||
private val maxSize = 500
|
||||
|
||||
private val map by lazy { LinkedHashMap<String, Long>(0, 0.75f, true) }
|
||||
private val map = LinkedHashMap<String, Long>(0, 0.75f, true)
|
||||
|
||||
private val lock by lazy { ReentrantLock() }
|
||||
private val lock = ReentrantLock()
|
||||
|
||||
private val builder by lazy { ThreadLocal<StringBuilder>() }
|
||||
private val builder = ThreadLocal<StringBuilder>()
|
||||
|
||||
override fun onProcessLaunched(time: Long) {
|
||||
processLaunchTime = time
|
||||
private val otherThreadLastTime = ThreadLocal<Long>()
|
||||
|
||||
private val mainLooper = Looper.getMainLooper()
|
||||
|
||||
override fun onProcessLaunched() {
|
||||
processLaunchTime = SystemClock.elapsedRealtime()
|
||||
}
|
||||
|
||||
override fun startMainThreadTime(time: Long) {
|
||||
mainThreadStartTime = time
|
||||
override fun onMainThreadLaunched() {
|
||||
mainThreadStartTime = Debug.threadCpuTimeNanos() / 1000_000
|
||||
}
|
||||
|
||||
override fun updateMainThreadTime(time: Long) {
|
||||
if (mainThreadLastTime == 0L) {
|
||||
mainThreadLastTime = mainThreadStartTime
|
||||
override fun updateMainThreadTime() {
|
||||
if (Looper.myLooper() != mainLooper) {
|
||||
return
|
||||
}
|
||||
val delta = time - mainThreadLastTime
|
||||
if (delta > 0) {
|
||||
mainThreadCpuUsage += delta
|
||||
if (mainThreadStartTime > 0) {
|
||||
mainThreadCpuUsage = (Debug.threadCpuTimeNanos() / 1000_000 - mainThreadStartTime)
|
||||
}
|
||||
mainThreadLastTime = time
|
||||
}
|
||||
|
||||
override fun incrementOtherThreadUsage(group: String, t: Thread, usage: Long) {
|
||||
var builder = builder.get()
|
||||
if (builder == null) {
|
||||
builder = StringBuilder(128)
|
||||
this.builder.set(builder)
|
||||
}
|
||||
if (builder.isNotEmpty()) {
|
||||
builder.setLength(0)
|
||||
}
|
||||
builder.append(group).append("@@").append(t.name)
|
||||
lock.withLock {
|
||||
while (map.size > maxSize) {
|
||||
val toEvict = map.entries.iterator().next()
|
||||
map.remove(toEvict.key)
|
||||
override fun updateOtherThreadTime() {
|
||||
var last = otherThreadLastTime.get()
|
||||
val now = Debug.threadCpuTimeNanos() / 1000_000
|
||||
try {
|
||||
if (last == null) {
|
||||
last = now
|
||||
otherThreadLastTime.set(last)
|
||||
}
|
||||
map[builder.toString()] = map.getOrPut(builder.toString()) { 0 } + usage
|
||||
val duration = now - last
|
||||
if (duration < 1) {
|
||||
return
|
||||
}
|
||||
var builder = builder.get()
|
||||
if (builder == null) {
|
||||
builder = StringBuilder(128)
|
||||
this.builder.set(builder)
|
||||
}
|
||||
if (builder.isNotEmpty()) {
|
||||
builder.setLength(0)
|
||||
}
|
||||
builder.append(Thread.currentThread().name)
|
||||
lock.withLock {
|
||||
while (map.size > maxSize) {
|
||||
val toEvict = map.entries.iterator().next()
|
||||
map.remove(toEvict.key)
|
||||
}
|
||||
map[builder.toString()] = map.getOrPut(builder.toString()) { 0 } + duration
|
||||
}
|
||||
} finally {
|
||||
otherThreadLastTime.set(now)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,8 +82,8 @@ internal class MoGoCpuUsageProviderImpl: IMoGoCpuUsageProvider {
|
||||
if (map.isNotEmpty()) {
|
||||
val iterator = map.entries.iterator()
|
||||
val rst = LinkedHashMap<String, Long>()
|
||||
rst["MainThreadCpuUsage"] = mainThreadCpuUsage
|
||||
rst["ProcessLaunchedTime"] = SystemClock.elapsedRealtimeNanos() - processLaunchTime
|
||||
rst["MainThreadUsage"] = mainThreadCpuUsage
|
||||
rst["ProcessUsage"] = SystemClock.elapsedRealtime() - processLaunchTime
|
||||
while (iterator.hasNext()) {
|
||||
val next = iterator.next()
|
||||
rst[next.key] = next.value
|
||||
|
||||
Reference in New Issue
Block a user