[3.4.0][Block] 主线程卡顿检测

This commit is contained in:
renwj
2023-07-25 16:34:23 +08:00
parent c52d83596e
commit f7afd759e8
15 changed files with 748 additions and 3 deletions

View File

@@ -27,6 +27,8 @@ class CrashFix {
@TargetMethod(methodName = "startService")
@ReplaceInvoke
fun fixStartServiceCrash(context: Context, intent: Intent): ComponentName? {
return try {
context.startService(intent)
} catch (t: Throwable) {

View File

@@ -0,0 +1,574 @@
package com.mogo.launcher.lancet
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.Canvas
import android.os.*
import android.util.*
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.*
import androidx.collection.ArrayMap
import com.knightboost.lancet.api.*
import com.knightboost.lancet.api.Scope.ALL
import com.knightboost.lancet.api.Scope.LEAF
import com.knightboost.lancet.api.annotations.*
import com.knightboost.lancet.api.annotations.Weaver
import com.mogo.eagle.core.block.runtime.config.recorder.*
import com.mogo.eagle.core.block.runtime.utils.TimeUtils.Companion.now
import com.mogo.eagle.core.function.call.devatools.*
import com.mogo.eagle.core.utilcode.util.*
import kotlinx.coroutines.*
import kotlinx.coroutines.Runnable
import java.util.LinkedList
import java.util.concurrent.ConcurrentHashMap
import com.mogo.eagle.core.block.runtime.message.Message as Holder
@Keep
@Weaver
@Group("main_block_check")
class MainBlockCheck {
companion object {
@JvmStatic
val tokens = ArrayMap<Any, Holder>(128)
@JvmStatic
val whats by lazy { ConcurrentHashMap<Int, LinkedList<Holder>>() }
@JvmStatic
fun getRecorder(): IMessageRecorder? {
val block = CallerDevaToolsManager.block()
if (block == null || !block.hasInit()) {
return null
}
return block.recorder()
}
@JvmStatic val HANDLER by lazy {
Handler(Looper.getMainLooper()) { msg ->
val enclose = msg.obj as? Holder ?: throw AssertionError()
enclose.what?.also {
whats[it]?.remove(enclose)
}
val h = enclose.handler
val action = enclose.action
val oldMsg = enclose.origin
val oldObj = enclose.obj
val now = now()
if (enclose.enqueueTime <= 0) {
throw AssertionError("illegal: $enclose")
}
val elapsedTime = now - enclose.enqueueTime
try {
if (action != null) {
action.run()
} else {
if (oldMsg == null) {
h?.dispatchMessage(msg)
} else {
oldMsg.obj = oldObj
h?.dispatchMessage(oldMsg)
}
}
} finally {
val duration = now() - now
getRecorder()?.recycle(enclose.id, elapsedTime, duration)
}
true
}
}
@JvmStatic
@TargetClass(value = "android.os.Handler", scope = ALL)
@TargetMethod(methodName = "post")
@ReplaceInvoke
fun handlerPostProxy(handler: Handler, action: Runnable): Boolean {
if (Looper.getMainLooper() != handler.looper) {
return handler.post(action)
}
val what = ObjectHashCodeUtils.getHashCodeIfNeed(action)
val msg = Message.obtain(handler, action)
msg.what = what
val holder = Holder.acquire(handler, action = action, what = what)
msg.obj = holder
val inserted = HANDLER.sendMessage(msg)
if (inserted) {
getRecorder()?.insert(holder)
whats.getOrPut(what) { LinkedList() }.add(holder)
}
return inserted
}
@JvmStatic
@TargetClass(value = "android.os.Handler", scope = ALL)
@TargetMethod(methodName = "sendEmptyMessage")
@ReplaceInvoke
fun handlerSendEmptyMessage(handler: Handler, what: Int): Boolean {
if (Looper.getMainLooper() != handler.looper) {
return handler.sendEmptyMessage(what)
}
val msg = Message.obtain(handler, what)
val holder = Holder.acquire(handler, what = what)
msg.obj = holder
val inserted = HANDLER.sendMessage(msg)
if (inserted) {
getRecorder()?.insert(holder)
whats.getOrPut(what) { LinkedList() }.add(holder)
}
return inserted
}
@JvmStatic
@TargetClass(value = "android.os.Handler", scope = ALL)
@TargetMethod(methodName = "sendEmptyMessageAtTime")
@ReplaceInvoke
fun handlerSendEmptyMessageAtTime(handler: Handler, what: Int, uptimeMillis: Long): Boolean {
if (Looper.getMainLooper() != handler.looper) {
return handler.sendEmptyMessageAtTime(what, uptimeMillis)
}
val msg = Message.obtain(handler, what)
val holder = Holder.acquire(handler, what = what, delay = uptimeMillis - SystemClock.uptimeMillis())
msg.obj = holder
val inserted = HANDLER.sendMessageAtTime(msg, uptimeMillis)
if (inserted) {
getRecorder()?.insert(holder)
whats.getOrPut(what) { LinkedList() }.add(holder)
}
return inserted
}
@JvmStatic
@TargetClass(value = "android.os.Handler", scope = ALL)
@TargetMethod(methodName = "sendEmptyMessageDelayed")
@ReplaceInvoke
fun handlerSendEmptyMessageDelayed(handler: Handler, what: Int, delayMillis: Long): Boolean {
if (Looper.getMainLooper() != handler.looper) {
return handler.sendEmptyMessageDelayed(what, delayMillis)
}
val msg = Message.obtain(handler, what)
val holder = Holder.acquire(handler, what = what, delay = delayMillis)
msg.obj = holder
val inserted = HANDLER.sendMessageDelayed(msg, delayMillis)
if (inserted) {
getRecorder()?.insert(holder)
whats.getOrPut(what) { LinkedList() }.add(holder)
}
return inserted
}
@JvmStatic
@TargetClass(value = "android.os.Handler", scope = ALL)
@TargetMethod(methodName = "sendMessage")
@ReplaceInvoke
fun handlerSendMessage(handler: Handler, msg: Message): Boolean {
if (Looper.getMainLooper() != handler.looper) {
return handler.sendMessage(msg)
}
val obj: Holder = Holder.acquire(handler, msg, obj = msg.obj, what = msg.what)
val oldObj = msg.obj
msg.obj = obj
if (oldObj != null) {
synchronized(tokens) {
tokens[oldObj] = obj
}
}
val inserted = HANDLER.sendMessage(msg)
if (inserted) {
getRecorder()?.insert(obj)
whats.getOrPut(msg.what) { LinkedList() }.add(obj)
}
return inserted
}
@JvmStatic
@TargetClass(value = "android.os.Handler", scope = ALL)
@TargetMethod(methodName = "sendMessageAtTime")
@ReplaceInvoke
fun handlerSendMessageAtTime(handler: Handler, msg: Message, uptimeMillis: Long): Boolean {
if (Looper.getMainLooper() != handler.looper) {
return handler.sendMessageAtTime(msg, uptimeMillis)
}
val obj= Holder.acquire(handler, msg, obj = msg.obj, what = msg.what, delay = uptimeMillis - SystemClock.uptimeMillis())
val oldObj = msg.obj
msg.obj = obj
if (oldObj != null) {
synchronized(tokens) {
tokens[oldObj] = obj
}
}
val inserted = HANDLER.sendMessageAtTime(msg, uptimeMillis)
if (inserted) {
whats.getOrPut(msg.what) { LinkedList() }.add(obj)
getRecorder()?.insert(obj)
}
return inserted
}
@JvmStatic
@TargetClass(value = "android.os.Handler", scope = ALL)
@TargetMethod(methodName = "sendMessageAtFrontOfQueue")
@ReplaceInvoke
fun handlerSendMessageAtFrontOfQueue(handler: Handler, msg: Message): Boolean {
if (Looper.getMainLooper() != handler.looper) {
return handler.sendMessageAtFrontOfQueue(msg)
}
val obj = Holder.acquire(handler, msg, obj = msg.obj, what = msg.what)
val old = msg.obj
msg.obj = obj
if (old != null) {
synchronized(tokens) {
tokens[old] = obj
}
}
val inserted = HANDLER.sendMessageAtFrontOfQueue(msg)
if (inserted) {
whats.getOrPut(msg.what) { LinkedList() }.add(obj)
getRecorder()?.insert(obj)
}
return inserted
}
@JvmStatic
@TargetClass(value = "android.os.Handler", scope = ALL)
@TargetMethod(methodName = "postDelayed")
@ReplaceInvoke
fun handlerPostDelayProxy(handler: Handler, action: Runnable, delayMillis: Long): Boolean {
if (Looper.getMainLooper() != handler.looper) {
return handler.postDelayed(action, delayMillis)
}
val what = ObjectHashCodeUtils.getHashCodeIfNeed(action)
val msg = Message.obtain(handler, action)
msg.what = what
val holder = Holder.acquire(handler, action = action, what = what, delay = delayMillis)
msg.obj = holder
val inserted = HANDLER.sendMessageDelayed(msg, delayMillis)
if (inserted) {
getRecorder()?.insert(holder)
whats.getOrPut(what) { LinkedList() }.add(holder)
}
return inserted
}
@JvmStatic
@TargetClass(value = "android.app.Activity", scope = ALL)
@TargetMethod(methodName = "post")
@ReplaceInvoke
fun runOnUiThreadOfActivityProxy(activity: Activity, action: Runnable) {
val what = ObjectHashCodeUtils.getHashCodeIfNeed(action)
val msg = Message.obtain()
msg.what = what
val holder = Holder.acquire(null, action = action, what = what)
msg.obj = holder
val inserted = HANDLER.sendMessage(msg)
if (inserted) {
getRecorder()?.insert(holder)
whats.getOrPut(what) { LinkedList() }.add(holder)
}
}
@JvmStatic
@TargetClass(value = "android.view.View", scope = ALL)
@TargetMethod(methodName = "post")
@ReplaceInvoke
fun postOfViewProxy(view: View, action: Runnable): Boolean {
val what = ObjectHashCodeUtils.getHashCodeIfNeed(action)
val msg = Message.obtain()
msg.what = what
val holder = Holder.acquire(null, action = action, what = what)
msg.obj = holder
val inserted = HANDLER.sendMessage(msg)
if (inserted) {
getRecorder()?.insert(holder)
whats.getOrPut(what) { LinkedList() }.add(holder)
}
return inserted
}
@JvmStatic
@TargetClass(value = "android.view.View", scope = ALL)
@TargetMethod(methodName = "postDelayed")
@ReplaceInvoke
fun postDelayedOfViewProxy(view: View, action: Runnable, delayMillis: Long): Boolean {
val what = ObjectHashCodeUtils.getHashCodeIfNeed(action)
val msg = Message.obtain()
msg.what = what
val holder = Holder.acquire(null, action = action, what = what, delay = delayMillis)
msg.obj = holder
val inserted = HANDLER.sendMessageDelayed(msg, delayMillis)
if (inserted) {
getRecorder()?.insert(holder)
whats.getOrPut(what) { LinkedList() }.add(holder)
}
return inserted
}
@JvmStatic
@TargetClass(value = "android.os.Handler", scope = ALL)
@TargetMethod(methodName = "removeCallbacks")
@ReplaceInvoke
fun handlerRemoveCallbacks(handler: Handler, action: Runnable) {
if (Looper.getMainLooper() != handler.looper) {
return handler.removeCallbacks(action)
}
val what = ObjectHashCodeUtils.getHashCodeIfNeed(action)
HANDLER.removeMessages(what)
val holders = whats.remove(what)
if (!holders.isNullOrEmpty()) {
getRecorder()?.remove(holders)
}
}
@JvmStatic
@TargetClass(value = "android.os.Handler", scope = ALL)
@TargetMethod(methodName = "removeCallbacks")
@ReplaceInvoke
fun handlerRemoveCallbacksWithToken(handler: Handler, action: Runnable, token: Any?) {
if (Looper.getMainLooper() != handler.looper) {
return handler.removeCallbacks(action, token)
}
val what = ObjectHashCodeUtils.getHashCodeIfNeed(action)
val holders = whats.remove(what)
if (!holders.isNullOrEmpty()) {
getRecorder()?.remove(holders)
}
HANDLER.removeMessages(what, if (token != null) { synchronized(tokens) { tokens.remove(token) } } else null)
}
@JvmStatic
@TargetClass(value = "android.os.Handler", scope = ALL)
@TargetMethod(methodName = "removeMessages")
@ReplaceInvoke
fun handlerRemoveMessages(handler: Handler, what: Int) {
if (Looper.getMainLooper() != handler.looper) {
return handler.removeMessages(what)
}
val holders = whats.remove(what)
if (!holders.isNullOrEmpty()) {
getRecorder()?.remove(holders)
}
HANDLER.removeMessages(what)
}
@JvmStatic
@TargetClass(value = "android.os.Handler", scope = ALL)
@TargetMethod(methodName = "removeMessages")
@ReplaceInvoke
fun handlerRemoveMessagesWithToken(handler: Handler, what: Int, token: Any?) {
if (Looper.getMainLooper() != handler.looper) {
return handler.removeMessages(what, token)
}
val holders = whats.remove(what)
if (!holders.isNullOrEmpty()) {
getRecorder()?.remove(holders)
}
HANDLER.removeMessages(what, if (token != null) synchronized(tokens) { tokens.remove(token) } else null)
}
}
@TargetClass(value = "android.view.View", scope = LEAF)
@Insert(mayCreateSuper = true)
@TargetMethod(methodName = "onMeasure")
fun proxyViewOnMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
val now = now()
try {
Origin.callVoid()
} catch (t: Throwable) {
throw t
} finally {
val spend = now() - now
getRecorder()?.insert(Holder.acquire(spend, "View[${This.get().javaClass.name}]:onMeasure"))
}
}
@TargetClass(value = "android.view.View", scope = LEAF)
@Insert(mayCreateSuper = true)
@TargetMethod(methodName = "onLayout")
fun proxyViewOnLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
val holder = Holder.acquire(handler = null, obj = "View[${This.get().javaClass.name}]:onLayout")
getRecorder()?.insert(holder)
val now = now()
try {
Origin.callVoid()
} catch (t: Throwable) {
throw t
} finally {
val spend = now() - now
getRecorder()?.recycle(holder.id, 0, spend)
}
}
@TargetClass(value = "android.view.View", scope = LEAF)
@Insert(mayCreateSuper = true)
@TargetMethod(methodName = "onDraw")
fun proxyViewOnDraw(canvas: Canvas) {
val holder = Holder.acquire(handler = null, obj = "View[${This.get().javaClass.name}]:onDraw")
getRecorder()?.insert(holder)
val now = now()
try {
Origin.callVoid()
} catch (t: Throwable) {
throw t
} finally {
val spend = now() - now
getRecorder()?.recycle(holder.id, 0, spend)
}
}
@TargetClass(value = "android.app.Activity", scope = LEAF)
@Insert(mayCreateSuper = true)
@TargetMethod(methodName = "onCreate")
fun proxyActivityOnCreate(savedInstanceState: Bundle?) {
val holder = Holder.acquire(handler = null, obj = "Activity[${This.get().javaClass.name}]:onCreate")
getRecorder()?.insert(holder)
val now = now()
try {
Origin.callVoid()
} catch (t: Throwable) {
throw t
} finally {
val spend = now() - now
CallerDevaToolsManager.block()?.monitor((This.get() as Activity).window)
getRecorder()?.recycle(holder.id, 0, spend)
}
}
@TargetClass(value = "android.app.Activity", scope = LEAF)
@Insert(mayCreateSuper = true)
@TargetMethod(methodName = "onStart")
fun proxyActivityOnStart() {
val holder = Holder.acquire(handler = null, obj = "Activity[${This.get().javaClass.name}]:onStart")
getRecorder()?.insert(holder)
val now = now()
try {
Origin.callVoid()
} catch (t: Throwable) {
throw t
} finally {
val spend = now() - now
getRecorder()?.recycle(holder.id, 0, spend)
}
}
@TargetClass(value = "android.app.Activity", scope = LEAF)
@Insert(mayCreateSuper = true)
@TargetMethod(methodName = "onResume")
fun proxyActivityOnResume() {
val holder = Holder.acquire(handler = null, obj = "Activity[${This.get().javaClass.name}]:onResume")
getRecorder()?.insert(holder)
val now = now()
CallerDevaToolsManager.block()?.resume((This.get() as Activity).window)
try {
Origin.callVoid()
} catch (t: Throwable) {
throw t
} finally {
val spend = now() - now
getRecorder()?.recycle(holder.id, 0, spend)
}
}
@TargetClass(value = "android.app.Activity", scope = LEAF)
@Insert(mayCreateSuper = true)
@TargetMethod(methodName = "onPause")
fun proxyActivityOnPause() {
val holder = Holder.acquire(handler = null, obj = "Activity[${This.get().javaClass.name}]:onPause")
getRecorder()?.insert(holder)
CallerDevaToolsManager.block()?.pause((This.get() as Activity).window)
val now = now()
try {
Origin.callVoid()
} catch (t: Throwable) {
throw t
} finally {
val spend = now() - now
getRecorder()?.recycle(holder.id, 0, spend)
}
}
@TargetClass(value = "android.app.Activity", scope = LEAF)
@Insert(mayCreateSuper = true)
@TargetMethod(methodName = "onDestroy")
fun proxyActivityOnDestroy() {
CallerDevaToolsManager.block()?.pop((This.get() as Activity).window)
try {
Origin.callVoid()
} catch (t: Throwable) {
throw t
}
}
@TargetClass(value = "androidx.fragment.app.Fragment", scope = LEAF)
@Insert(mayCreateSuper = true)
@TargetMethod(methodName = "onCreateView")
fun proxyFragmentOnCreateView(inflater: LayoutInflater, container: ViewGroup, savedInstanceState: Bundle?) {
val holder = Holder.acquire(handler = null, obj = "Fragment[${This.get().javaClass.name}]:onCreateView")
getRecorder()?.insert(holder)
val startTime = now()
try {
Origin.callVoid()
} catch (t: Throwable) {
throw t
} finally {
val spend = now() - startTime
getRecorder()?.recycle(holder.id, 0, spend)
}
}
@TargetClass(value = "androidx.fragment.app.Fragment", scope = LEAF)
@Insert(mayCreateSuper = true)
@TargetMethod(methodName = "onCreate")
fun proxyFragmentOnCreate(savedInstanceState: Bundle?) {
val holder = Holder.acquire(handler = null, obj = "Fragment[${This.get().javaClass.name}]:onCreate")
getRecorder()?.insert(holder)
val startTime = now()
try {
Origin.callVoid()
} catch (t: Throwable) {
throw t
} finally {
val spend = now() - startTime
getRecorder()?.recycle(holder.id, 0, spend)
}
}
@TargetClass(value = "androidx.fragment.app.Fragment", scope = LEAF)
@Insert(mayCreateSuper = true)
@TargetMethod(methodName = "onViewCreated")
fun proxyFragmentOnViewCreated(view: View, savedInstanceState: Bundle?) {
val holder = Holder.acquire(handler = null, obj = "Fragment[${This.get().javaClass.name}]:onViewCreated")
getRecorder()?.insert(holder)
val startTime = now()
try {
Origin.callVoid()
} catch (t: Throwable) {
throw t
} finally {
val spend = now() - startTime
getRecorder()?.recycle(holder.id, 0, spend)
}
}
@TargetClass(value = "android.content.BroadcastReceiver", scope = LEAF)
@Insert(mayCreateSuper = true)
@TargetMethod(methodName = "onReceive")
fun proxyBroadcastReceiver(ctx: Context?, intent: Intent?) {
val holder = Holder.acquire(handler = null, obj = "Receiver[${This.get().javaClass.name}]:onReceive")
getRecorder()?.insert(holder)
val startTime = now()
try {
Origin.callVoid()
} catch (t: Throwable) {
throw t
} finally {
val spend = now() - startTime
getRecorder()?.recycle(holder.id, 0, spend)
}
}
}