[6.2.0][技术优化] 优化synchrnized锁的处理

This commit is contained in:
renwj
2023-11-13 17:26:46 +08:00
parent f49a77cb39
commit ece5dca6fb
4 changed files with 62 additions and 8 deletions

View File

@@ -103,9 +103,7 @@ class JankPointAutoGenerator {
type = Type.SLEEP, type = Type.SLEEP,
scope = Scope.ALL, scope = Scope.ALL,
onlyHookMethodNames = { "sleep", "sleep"}, onlyHookMethodNames = { "sleep", "sleep"},
onlyHookMethodDescs = { "(J)V", "(JI)V"}, onlyHookMethodDescs = { "(J)V", "(JI)V"}
excludeHookMethodNames = { "toString", "hashCode" },
excludeHookMethodDescs = { "()Ljava/lang/String;", "()I" }
) )
private Thread thread; private Thread thread;

View File

@@ -21,18 +21,74 @@ public class HookHandler implements IHookInvoker {
private final AtomicLong startTime = new AtomicLong(0L); private final AtomicLong startTime = new AtomicLong(0L);
private final AtomicLong syncLockStartTime = new AtomicLong(0L);
private final ThreadLocal<StringBuilder> message = new ThreadLocal<>(); private final ThreadLocal<StringBuilder> message = new ThreadLocal<>();
@Override @Override
public void i(@NonNull Type type, Object caller, @NonNull String methodName, @NonNull Object... objects) { public void i(@NonNull Type type, Object caller, @NonNull String methodName, @NonNull Object... objects) {
if (Looper.myLooper() == mainLooper) { 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 @Override
public void o(@NonNull Type type, Object caller, @NonNull String methodName, @NonNull Object... objects) { 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(); long startTime = this.startTime.get();
if (startTime > 0) { if (startTime > 0) {
long delta = SystemClock.elapsedRealtime() - startTime; long delta = SystemClock.elapsedRealtime() - startTime;
@@ -66,7 +122,7 @@ public class HookHandler implements IHookInvoker {
builder.setLength(builder.length() - 1); builder.setLength(builder.length() - 1);
builder.append(")"); builder.append(")");
} }
if (type == Type.SYNCHRONIZED_LOCK || type == Type.AQS_LOCK) { if (type == Type.AQS_LOCK) {
builder.append("\n"); builder.append("\n");
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace(); StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
for (StackTraceElement trace: stackTrace) { for (StackTraceElement trace: stackTrace) {

View File

@@ -34,7 +34,7 @@ buildscript {
classpath "com.mogo.cloud:systrace:${plugin_version}" classpath "com.mogo.cloud:systrace:${plugin_version}"
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.18' classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.18'
classpath "com.mogo.sticky:service:${plugin_version}" 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 "io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.15.0"
classpath 'com.mogo.cloud:matrix:1.0.0' classpath 'com.mogo.cloud:matrix:1.0.0'
classpath 'org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:3.4.0.2513' classpath 'org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:3.4.0.2513'

View File

@@ -208,7 +208,7 @@ ext {
//========================= LancetX =================== //========================= 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", lancetx_compiler : "com.mogo.eagle.core.lancetx:compiler:1.0.0",