[6.2.0][技术优化] 更改Handler消息记录方式,对编译生成的类方法集合进行过滤

This commit is contained in:
renwj
2023-11-20 15:35:36 +08:00
parent c8efde952b
commit 59ac4d030b
20 changed files with 824 additions and 1067 deletions

View File

@@ -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)"
}
})
}

View File

@@ -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

View File

@@ -0,0 +1,12 @@
package com.mogo.eagle.core.function.main
import java.util.concurrent.atomic.AtomicBoolean
class ARouterUtils {
companion object {
@JvmField
var isInit = AtomicBoolean(false)
}
}

View File

@@ -1,26 +1,23 @@
package com.mogo.eagle.core.function.main.threadopt;
import android.os.Debug;
import android.os.Looper;
import android.os.SystemClock;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.os.HandlerCompat;
import com.mogo.eagle.core.function.api.devatools.perf.IMoGoCpuUsageProvider;
import com.mogo.eagle.core.function.call.devatools.CallerDevaToolsManager;
import com.mogo.eagle.core.function.main.ARouterUtils;
import com.mogo.thread.opt.core.ThreadManager;
import com.mogo.thread.opt.core.annotation.Keep;
import com.mogo.thread.opt.core.config.ThreadConfig;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
@Keep
public class ThreadOptInitializer {
public static final AtomicBoolean aRouterInit = new AtomicBoolean(false);
private static final ThreadConfig.TaskExecuteListener listener = new ThreadConfig.TaskExecuteListener() {
@@ -38,29 +35,29 @@ public class ThreadOptInitializer {
@Override
public void onExecutorBefore(@NonNull ThreadConfig.TaskType taskType, @Nullable Object task, @Nullable String desc) {
if (!recorded && aRouterInit.get()) {
if (!recorded && ARouterUtils.isInit.get()) {
IMoGoCpuUsageProvider usage = CallerDevaToolsManager.INSTANCE.usage();
if (usage != null) {
recorded = true;
HandlerCompat.createAsync(Looper.getMainLooper()).post(() -> usage.startMainThreadTime(Debug.threadCpuTimeNanos()));
usage.onProcessLaunched(SystemClock.elapsedRealtimeNanos());
HandlerCompat.createAsync(Looper.getMainLooper()).post(usage::onMainThreadLaunched);
usage.onProcessLaunched();
}
}
if (recorded) {
start.set(Debug.threadCpuTimeNanos());
IMoGoCpuUsageProvider usage = CallerDevaToolsManager.INSTANCE.usage();
if (usage != null) {
usage.updateOtherThreadTime();
}
}
}
@Override
public void onExecutorAfter(@NonNull ThreadConfig.TaskType taskType, @Nullable Object task, @Nullable String desc) {
if (recorded) {
Long start = this.start.get();
if (start != null && start > 0) {
long delta = Debug.threadCpuTimeNanos() - start;
IMoGoCpuUsageProvider usage = CallerDevaToolsManager.INSTANCE.usage();
if (usage != null && desc != null) {
usage.incrementOtherThreadUsage(desc, Thread.currentThread(), delta);
}
IMoGoCpuUsageProvider usage = CallerDevaToolsManager.INSTANCE.usage();
if (usage != null) {
usage.updateOtherThreadTime();
}
}
}