[6.2.0][技术优化] 优化synchrnized锁的处理
This commit is contained in:
@@ -103,9 +103,7 @@ class JankPointAutoGenerator {
|
||||
type = Type.SLEEP,
|
||||
scope = Scope.ALL,
|
||||
onlyHookMethodNames = { "sleep", "sleep"},
|
||||
onlyHookMethodDescs = { "(J)V", "(JI)V"},
|
||||
excludeHookMethodNames = { "toString", "hashCode" },
|
||||
excludeHookMethodDescs = { "()Ljava/lang/String;", "()I" }
|
||||
onlyHookMethodDescs = { "(J)V", "(JI)V"}
|
||||
)
|
||||
private Thread thread;
|
||||
|
||||
|
||||
@@ -21,18 +21,74 @@ public class HookHandler implements IHookInvoker {
|
||||
|
||||
private final AtomicLong startTime = new AtomicLong(0L);
|
||||
|
||||
private final AtomicLong syncLockStartTime = new AtomicLong(0L);
|
||||
|
||||
private final ThreadLocal<StringBuilder> message = new ThreadLocal<>();
|
||||
|
||||
@Override
|
||||
public void i(@NonNull Type type, Object caller, @NonNull String methodName, @NonNull Object... objects) {
|
||||
if (Looper.myLooper() == mainLooper) {
|
||||
startTime.set(SystemClock.elapsedRealtime());
|
||||
if (type == Type.SYNCHRONIZED_LOCK) {
|
||||
if ("onMonitorBefore".equals(methodName)) {
|
||||
syncLockStartTime.set(SystemClock.elapsedRealtime());
|
||||
}
|
||||
if ("onMonitorEnter".equals(methodName)) {
|
||||
long elapsedTime = SystemClock.elapsedRealtime() - syncLockStartTime.get();
|
||||
if (elapsedTime >= 10) {
|
||||
StringBuilder builder = message.get();
|
||||
if (builder == null) {
|
||||
builder = new StringBuilder();
|
||||
message.set(builder);
|
||||
}
|
||||
if (builder.length() > 0) {
|
||||
builder.setLength(0);
|
||||
}
|
||||
builder
|
||||
.append(type)
|
||||
.append("#")
|
||||
.append(caller == null ? "caller is null" : caller.getClass().getSimpleName())
|
||||
.append("#")
|
||||
.append(methodName);
|
||||
if (objects.length > 0) {
|
||||
builder.append("#(");
|
||||
}
|
||||
for (Object o : objects) {
|
||||
if (o == null) {
|
||||
continue;
|
||||
}
|
||||
builder.append(o.getClass().getSimpleName())
|
||||
.append(",");
|
||||
}
|
||||
|
||||
if (objects.length > 0) {
|
||||
builder.setLength(builder.length() - 1);
|
||||
builder.append(")");
|
||||
}
|
||||
|
||||
builder.append("\n");
|
||||
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
|
||||
for (StackTraceElement trace: stackTrace) {
|
||||
if ("dalvik.system.VMStack".equals(trace.getClassName()) && "getThreadStackTrace".equals(trace.getMethodName())) {
|
||||
continue;
|
||||
}
|
||||
if ("java.lang.Thread".equals(trace.getClassName()) && "getStackTrace".equals(trace.getMethodName())) {
|
||||
continue;
|
||||
}
|
||||
builder.append(trace.getClassName()).append("#").append(trace.getMethodName()).append("#").append(trace.getLineNumber()).append("\n");
|
||||
}
|
||||
builder.setLength(builder.length() - 1);
|
||||
Log.w("HookHandler", "jank detected:" + builder);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
startTime.set(SystemClock.elapsedRealtime());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void o(@NonNull Type type, Object caller, @NonNull String methodName, @NonNull Object... objects) {
|
||||
if (Looper.myLooper() == mainLooper) {
|
||||
if (type != Type.SYNCHRONIZED_LOCK && Looper.myLooper() == mainLooper) {
|
||||
long startTime = this.startTime.get();
|
||||
if (startTime > 0) {
|
||||
long delta = SystemClock.elapsedRealtime() - startTime;
|
||||
@@ -66,7 +122,7 @@ public class HookHandler implements IHookInvoker {
|
||||
builder.setLength(builder.length() - 1);
|
||||
builder.append(")");
|
||||
}
|
||||
if (type == Type.SYNCHRONIZED_LOCK || type == Type.AQS_LOCK) {
|
||||
if (type == Type.AQS_LOCK) {
|
||||
builder.append("\n");
|
||||
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
|
||||
for (StackTraceElement trace: stackTrace) {
|
||||
|
||||
@@ -34,7 +34,7 @@ buildscript {
|
||||
classpath "com.mogo.cloud:systrace:${plugin_version}"
|
||||
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.18'
|
||||
classpath "com.mogo.sticky:service:${plugin_version}"
|
||||
classpath "io.github.knight-zxw:lancet-plugin:10.1.0"
|
||||
classpath "io.github.knight-zxw:lancet-plugin:10.3.0"
|
||||
classpath "io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.15.0"
|
||||
classpath 'com.mogo.cloud:matrix:1.0.0'
|
||||
classpath 'org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:3.4.0.2513'
|
||||
|
||||
@@ -208,7 +208,7 @@ ext {
|
||||
|
||||
|
||||
//========================= LancetX ===================
|
||||
lancetx_runtime : "io.github.knight-zxw:lancet-runtime:10.2.0",
|
||||
lancetx_runtime : "io.github.knight-zxw:lancet-runtime:10.3.0",
|
||||
|
||||
lancetx_compiler : "com.mogo.eagle.core.lancetx:compiler:1.0.0",
|
||||
|
||||
|
||||
Reference in New Issue
Block a user