[6.2.0][技术优化] 添加主线程消息记录

This commit is contained in:
renwj
2023-11-14 20:37:24 +08:00
parent 2690957305
commit 82cd08460c
4 changed files with 54 additions and 41 deletions

View File

@@ -23,6 +23,8 @@ import kotlinx.coroutines.Runnable
import java.lang.ref.WeakReference
import java.util.LinkedList
import java.util.concurrent.*
import java.util.concurrent.locks.ReentrantReadWriteLock
import kotlin.concurrent.withLock
import com.mogo.eagle.core.block.runtime.message.Message as Holder
@Keep
@@ -37,14 +39,18 @@ class MainBlockCheck {
@JvmStatic
private val tokens = ConcurrentHashMap<Int, WeakReference<Holder>>(128)
private val pool by lazy { Executors.newFixedThreadPool(1) }
private val pool by lazy { Executors.newFixedThreadPool(2) }
val asyncHandlers by lazy { CopyOnWriteArraySet<WeakReference<Handler>>() }
private val asyncHandlersMap by lazy { ConcurrentHashMap<Int, Boolean>() }
private val handlersMap by lazy { ConcurrentHashMap<String, Boolean>() }
private val whats by lazy { ConcurrentHashMap<Int, LinkedList<WeakReference<Holder>>>() }
private val actionPool by lazy { Pools.SimplePool<PoolAction>(500) }
private val lock by lazy { ReentrantReadWriteLock() }
private data class PoolAction(var type: Int, var what: Int, var holder: Holder?, var elapsedTime: Long = -1, var duration:Long = -1): Runnable {
override fun run() {
@@ -71,7 +77,9 @@ class MainBlockCheck {
}
} finally {
reset()
actionPool.release(this)
lock.writeLock().withLock {
actionPool.release(this)
}
}
}
@@ -84,9 +92,9 @@ class MainBlockCheck {
}
}
private val actionPool by lazy { Pools.SynchronizedPool<PoolAction>(500) }
private fun getAction(): PoolAction = actionPool.acquire() ?: PoolAction(-1, 0, null)
private fun getAction(): PoolAction = lock.readLock().withLock { actionPool.acquire() ?: PoolAction(-1, 0, null) }
@JvmStatic
fun getRecorder(): IMessageRecorder? {
@@ -126,6 +134,9 @@ class MainBlockCheck {
h?.dispatchMessage(oldMsg)
}
}
} catch (t: Throwable) {
Log.e(TAG, "handler: $h -> ${handlersMap[h.toString()]} -> msg: $enclose", t)
throw t
} finally {
val duration = now() - now
recycle(enclose, elapsedTime, duration)
@@ -150,7 +161,7 @@ class MainBlockCheck {
if (Looper.getMainLooper() != handler.looper) {
return handler.post(action)
}
val isAsync = asyncHandlersMap.getOrPut(handler.hashCode()) { computeIsAsyncHandler(handler) }
val isAsync = handlersMap.getOrPut(handler.toString()) { computeIsAsyncHandler(handler) }
val newHandler = getHandler(isAsync)
val msg = Message.obtain(newHandler)
val what = ObjectHashCodeUtils.getHashCodeIfNeed(newHandler, action)
@@ -172,7 +183,7 @@ class MainBlockCheck {
if (Looper.getMainLooper() != handler.looper) {
return handler.postDelayed(action, delayMillis)
}
val isAsync = asyncHandlersMap.getOrPut(handler.hashCode()) { computeIsAsyncHandler(handler) }
val isAsync = handlersMap.getOrPut(handler.toString()) { computeIsAsyncHandler(handler) }
val newHandler = getHandler(isAsync)
val msg = Message.obtain(newHandler)
val what = ObjectHashCodeUtils.getHashCodeIfNeed(handler, action)
@@ -194,7 +205,7 @@ class MainBlockCheck {
if (Looper.getMainLooper() != handler.looper) {
return HandlerCompat.postDelayed(handler, action, token, delayMillis)
}
val isAsync = asyncHandlersMap.getOrPut(handler.hashCode()) { computeIsAsyncHandler(handler) }
val isAsync = handlersMap.getOrPut(handler.toString()) { computeIsAsyncHandler(handler) }
val newHandler = getHandler(isAsync)
val msg = Message.obtain(newHandler)
val what = ObjectHashCodeUtils.getHashCodeIfNeed(handler, action)
@@ -221,7 +232,7 @@ class MainBlockCheck {
if (Looper.getMainLooper() != handler.looper) {
return handler.postAtFrontOfQueue(action)
}
val isAsync = asyncHandlersMap.getOrPut(handler.hashCode()) { computeIsAsyncHandler(handler) }
val isAsync = handlersMap.getOrPut(handler.toString()) { computeIsAsyncHandler(handler) }
val newHandler = getHandler(isAsync)
val msg = Message.obtain(newHandler)
val what = ObjectHashCodeUtils.getHashCodeIfNeed(handler, action)
@@ -243,7 +254,7 @@ class MainBlockCheck {
if (Looper.getMainLooper() != handler.looper) {
return handler.postAtTime(action, uptimeMillis)
}
val isAsync = asyncHandlersMap.getOrPut(handler.hashCode()) { computeIsAsyncHandler(handler) }
val isAsync = handlersMap.getOrPut(handler.toString()) { computeIsAsyncHandler(handler) }
val newHandler = getHandler(isAsync)
val msg = Message.obtain(newHandler)
val what = ObjectHashCodeUtils.getHashCodeIfNeed(handler, action)
@@ -266,7 +277,7 @@ class MainBlockCheck {
if (Looper.getMainLooper() != handler.looper) {
return handler.postAtTime(action, uptimeMillis)
}
val isAsync = asyncHandlersMap.getOrPut(handler.hashCode()) { computeIsAsyncHandler(handler) }
val isAsync = handlersMap.getOrPut(handler.toString() ) { computeIsAsyncHandler(handler) }
val newHandler = getHandler(isAsync)
val msg = Message.obtain(newHandler)
val what = ObjectHashCodeUtils.getHashCodeIfNeed(handler, action)
@@ -294,7 +305,7 @@ class MainBlockCheck {
if (Looper.getMainLooper() != handler.looper) {
return handler.sendEmptyMessage(what)
}
val isAsync = asyncHandlersMap.getOrPut(handler.hashCode()) { computeIsAsyncHandler(handler) }
val isAsync = handlersMap.getOrPut(handler.toString()) { computeIsAsyncHandler(handler) }
val newHandler = getHandler(isAsync)
val msg = Message.obtain(newHandler, what)
val holder = Holder.acquire(handler, what = what, isAsync = isAsync)
@@ -314,7 +325,7 @@ class MainBlockCheck {
if (Looper.getMainLooper() != handler.looper) {
return handler.sendEmptyMessageAtTime(what, uptimeMillis)
}
val isAsync = asyncHandlersMap.getOrPut(handler.hashCode()) { computeIsAsyncHandler(handler) }
val isAsync = handlersMap.getOrPut(handler.toString()) { computeIsAsyncHandler(handler) }
val newHandler = getHandler(isAsync)
val msg = Message.obtain(newHandler, what)
val delta = uptimeMillis - SystemClock.uptimeMillis()
@@ -324,7 +335,6 @@ class MainBlockCheck {
if (inserted) {
insert(what, holder)
}
return inserted
}
@@ -336,7 +346,7 @@ class MainBlockCheck {
if (Looper.getMainLooper() != handler.looper) {
return handler.sendEmptyMessageDelayed(what, delayMillis)
}
val isAsync = asyncHandlersMap.getOrPut(handler.hashCode()) { computeIsAsyncHandler(handler) }
val isAsync = handlersMap.getOrPut(handler.toString()) { computeIsAsyncHandler(handler) }
val newHandler = getHandler(isAsync)
val msg = Message.obtain(newHandler, what)
val holder = Holder.acquire(handler, what = what, delay = delayMillis, isAsync = isAsync)
@@ -356,7 +366,7 @@ class MainBlockCheck {
if (Looper.getMainLooper() != handler.looper) {
return handler.sendMessage(msg)
}
val isAsync = asyncHandlersMap.getOrPut(handler.hashCode()) { computeIsAsyncHandler(handler) }
val isAsync = handlersMap.getOrPut(handler.toString()) { computeIsAsyncHandler(handler) }
val newHandler = getHandler(isAsync)
var newMsg = msg
val action = msg.callback
@@ -387,7 +397,7 @@ class MainBlockCheck {
if (Looper.getMainLooper() != handler.looper) {
return handler.sendMessageAtTime(msg, uptimeMillis)
}
val isAsync = asyncHandlersMap.getOrPut(handler.hashCode()) { computeIsAsyncHandler(handler) }
val isAsync = handlersMap.getOrPut(handler.toString()) { computeIsAsyncHandler(handler) }
val newHandler = getHandler(isAsync)
var newMsg = msg
val action = msg.callback
@@ -419,7 +429,7 @@ class MainBlockCheck {
if (Looper.getMainLooper() != handler.looper) {
return handler.sendMessageAtFrontOfQueue(msg)
}
val isAsync = asyncHandlersMap.getOrPut(handler.hashCode()) { computeIsAsyncHandler(handler) }
val isAsync = handlersMap.getOrPut(handler.toString()) { computeIsAsyncHandler(handler) }
val newHandler = getHandler(isAsync)
var newMsg = msg
val action = msg.callback
@@ -502,8 +512,11 @@ class MainBlockCheck {
return handler.removeCallbacks(action)
}
val what = ObjectHashCodeUtils.getHashCodeIfNeed(handler, action)
getHandler(asyncHandlersMap.getOrPut(handler.hashCode()) { computeIsAsyncHandler(handler) }).removeMessages(what)
remove(what)
try {
return getHandler(handlersMap.getOrPut(handler.toString()) { computeIsAsyncHandler(handler) }).removeMessages(what)
} finally {
remove(what)
}
}
@JvmStatic
@@ -515,7 +528,7 @@ class MainBlockCheck {
return handler.removeCallbacks(action, token)
}
val what = ObjectHashCodeUtils.getHashCodeIfNeed(handler, action)
getHandler(asyncHandlersMap.getOrPut(handler.hashCode()) { computeIsAsyncHandler(handler) }).removeMessages(what, if (token != null) { synchronized(
getHandler(handlersMap.getOrPut(handler.toString()) { computeIsAsyncHandler(handler) }).removeMessages(what, if (token != null) { synchronized(
tokens
) { tokens.remove(token.hashCode())?.get() } } else null)
remove(what)
@@ -529,7 +542,7 @@ class MainBlockCheck {
if (Looper.getMainLooper() != handler.looper) {
return handler.removeMessages(what)
}
getHandler(asyncHandlersMap.getOrPut(handler.hashCode()) { computeIsAsyncHandler(handler) }).removeMessages(what)
getHandler(handlersMap.getOrPut(handler.toString()) { computeIsAsyncHandler(handler) }).removeMessages(what)
remove(what)
}
@@ -552,7 +565,7 @@ class MainBlockCheck {
if (Looper.getMainLooper() != handler.looper) {
return handler.removeMessages(what, token)
}
getHandler(asyncHandlersMap.getOrPut(handler.hashCode()) { computeIsAsyncHandler(handler) }).removeMessages(what, if (token != null) synchronized(
getHandler(handlersMap.getOrPut(handler.toString()) { computeIsAsyncHandler(handler) }).removeMessages(what, if (token != null) synchronized(
tokens
) { tokens.remove(token.hashCode())?.get() } else null)
remove(what)
@@ -563,27 +576,27 @@ class MainBlockCheck {
}
fun insert(what: Int, holder: Holder) {
// val action = getAction()
// action.type = 0
// action.what = what
// action.holder = holder
// pool.execute(action)
val action = getAction()
action.type = 0
action.what = what
action.holder = holder
pool.execute(action)
}
private fun remove(what: Int) {
// val action = getAction()
// action.type = 1
// action.what = what
// pool.execute(action)
val action = getAction()
action.type = 1
action.what = what
pool.execute(action)
}
private fun recycle(holder: Holder, elapsedTime: Long, duration: Long) {
// val action = getAction()
// action.type = 2
// action.holder = holder
// action.elapsedTime = elapsedTime
// action.duration = duration
// pool.execute(action)
val action = getAction()
action.type = 2
action.holder = holder
action.elapsedTime = elapsedTime
action.duration = duration
pool.execute(action)
}
private fun getHandler(isAsync: Boolean): Handler {

View File

@@ -229,7 +229,7 @@ ext {
passport_secret : "com.zhidaoauto:sdk-java:1.0.5-SNAPSHOT",
// 主线程卡顿监测
block_detector : "com.mogo.eagle.core.block:runtime:10.0.21",
block_detector : "com.mogo.eagle.core.block:runtime:10.20.0",
//======================== google auto-service ===============
google_auto_service : "com.google.auto.service:auto-service:1.0-rc7"

View File

@@ -393,5 +393,5 @@ class DevaToolsProvider : IDevaToolsProvider {
override fun logRecord(): IMoGoLogRecordProvider = logRecordProvider
override fun block(): IMoGoBlockProvider? = null
override fun block(): IMoGoBlockProvider? = block
}

View File

@@ -27,7 +27,7 @@ internal class MoGoBlockProviderImpl: IMoGoBlockProvider, IBlockListener {
.isDebug(false)
.period(5, SECONDS)
.junkRateThreshold(0.8f)
.recorder(null, 1000, 512)
.recorder(null, 500, 500)
.build())
hasInit = true
}