Skip to content

Commit 64e1c1f

Browse files
changelcaitxfelixzhou
authored andcommitted
Feature/dev changelcai (Tencent#274)
* Fix syscall 0xf0002 is blocked by seccomp filter in android o/p armv8-64 so (Tencent#230) * update version to 0.5.2.43. * bugfix 空指针保护 * fix maskIndex npe * fix maskIndex npe * add log * add log * add log * modify LooperMonitor and add log * add test listener in LooperMonitor * add evilThresholdMs modify method * bug fix:耗电+ANR,fib 的复杂度很高 * fix MemoryCanaryCore 耗电 * add log * modify getNextDelay * fix bug * add log * bug fix: getappmemory过于耗时的情况下关闭 * fix memory cost too long * add log * add log * fix bug * fix matrix calculate data bug * fix bug * fix TraceDataUtils bug * fix TraceDataUtils bug * fix TraceDataUtils bug * fix TraceDataUtils bug * fix TraceDataUtils bug * fix TraceDataUtils bug * fix stop不完整
1 parent 0e1e708 commit 64e1c1f

File tree

12 files changed

+101
-38
lines changed

12 files changed

+101
-38
lines changed

matrix/matrix-android/gradle.properties

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryErro
1010
#
1111
# When configured, Gradle will run in incubating parallel mode.
1212
# This option should only be used with decoupled projects. More details, visit
13-
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13+
# http://www.gradle.org/docs/current/userguide/multifa_project_builds.html#sec:decoupled_projects
1414
# org.gradle.parallel=true
1515
#Tue Jun 20 10:24:33 CST 2017
1616

17-
VERSION_NAME_PREFIX=0.5.3.3
17+
18+
VERSION_NAME_PREFIX=0.5.2.61
1819
VERSION_NAME_SUFFIX=

matrix/matrix-android/matrix-android-commons/src/main/cpp/elf_hook/elf_hook.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ static int locate_symbol(const loaded_soinfo *soinfo, const char *name, Elf_Sym
498498
static void clear_cache(void *addr) {
499499
unsigned long soff = ((unsigned long) addr) & PAGE_MASK;
500500
unsigned long eoff = soff + getpagesize();
501-
syscall(0xf0002, soff, eoff);
501+
__builtin___clear_cache(soff, eoff);
502502
}
503503

504504
static int replace_elf_rel_segment(const loaded_soinfo *soinfo, Elf_Addr rel_addr, size_t rel_count,

matrix/matrix-android/matrix-android-lib/src/main/java/com/tencent/matrix/report/Issue.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ public void setContent(JSONObject content) {
6060

6161
@Override
6262
public String toString() {
63-
return String.format("tag[%s]type[%d];key[%s];content[%s]", tag, type, key, content);
63+
String strContent = "";
64+
if(null != content) strContent = content.toString();
65+
return String.format("tag[%s]type[%d];key[%s];content[%s]", tag, type, key, strContent);
6466
}
6567

6668
public void setKey(String key) {

matrix/matrix-android/matrix-memory-canary/src/main/java/com/tencent/matrix/memorycanary/core/MemoryCanaryCore.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public static class MatrixMemoryInfo {
8484
private static final int NATIVE_HEAP_LIMIT = 500 * 1024;
8585
private static final int TRIM_MEMORY_SPAN = 10 * 60 * 1000;
8686
private static final int VMSIZE_LIMIT = 4 * 1024 * 1024; //4GB
87+
private static final int MAX_COST = 3000;
8788

8889
private final MemoryCanaryPlugin mPlugin;
8990
private boolean mIsOpen = false;
@@ -229,6 +230,7 @@ public void start() {
229230
public void stop() {
230231
((Application) mContext).unregisterActivityLifecycleCallbacks(mActivityLifecycleCallback);
231232
mContext.unregisterComponentCallbacks(mComponentCallback);
233+
mIsOpen = false;
232234
}
233235

234236
@Override
@@ -251,6 +253,9 @@ private void onShow(final Activity activity) {
251253
}
252254

253255
private void detectAppMemoryInfo(boolean bDetectAll, int flag) {
256+
if(!mIsOpen)
257+
return;
258+
254259
if (!bDetectAll) {
255260
detectRuntimeMemoryInfo();
256261
} else {
@@ -304,6 +309,10 @@ private void detectRuntimeMemoryInfo() {
304309
if (memoryInfo != null) {
305310
long cost = System.currentTimeMillis() - start;
306311
MatrixLog.i(TAG, "get app memory cost:" + cost);
312+
if(cost > MAX_COST) {
313+
mIsOpen = false;
314+
return;
315+
}
307316
MatrixMemoryInfo appInfo = new MatrixMemoryInfo(mShowingActivity);
308317
makeMatrixMemoryInfo(memoryInfo, appInfo);
309318
fillMemoryInfo(json, appInfo, SharePluginInfo.ISSUE_APP_MEM, mShowingActivity);
@@ -342,6 +351,10 @@ private void detectAppMemoryInfoImpl(int flag) {
342351

343352
long cost = System.currentTimeMillis() - start;
344353
MatrixLog.i(TAG, "get app memory cost:" + cost);
354+
if(cost > MAX_COST) {
355+
mIsOpen = false;
356+
return;
357+
}
345358

346359
//ontrimmemory or use too much memory
347360
MatrixMemoryInfo matrixMemoryInfo = new MatrixMemoryInfo(mShowingActivity);
@@ -435,7 +448,17 @@ private void makeMatrixMemoryInfo(final Debug.MemoryInfo memoryInfo, MatrixMemor
435448
}
436449

437450
private long getNextDelay() {
438-
return (getFib(mNextReportFactor) - getFib(mNextReportFactor - 1)) * STEP_FACTOR;
451+
if (mNextReportFactor >= 8) {
452+
return 30 * STEP_FACTOR;
453+
}
454+
455+
long start = System.currentTimeMillis();
456+
long delay = (getFib(mNextReportFactor) - getFib(mNextReportFactor - 1)) * STEP_FACTOR;
457+
long cost = System.currentTimeMillis() - start;
458+
if (cost > 1000) {
459+
MatrixLog.e(TAG, "[getNextDelay] cost time[%s] too long!", cost);
460+
}
461+
return delay;
439462
}
440463

441464
private int getFib(int n) {
@@ -445,6 +468,8 @@ private int getFib(int n) {
445468
return 1;
446469
} else if (n == 2) {
447470
return 2;
471+
} else if(n >= 8) {
472+
return 30;
448473
} else {
449474
return getFib(n - 1) + getFib(n - 2);
450475
}

matrix/matrix-android/matrix-trace-canary/src/main/java/com/tencent/matrix/trace/constants/Constants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class Constants {
2727
public static final int FILTER_STACK_MAX_COUNT = 60;
2828
public static final float FILTER_STACK_KEY_ALL_PERCENT = .3F;
2929
public static final float FILTER_STACK_KEY_PATENT_PERCENT = .8F;
30-
public static final int DEFAULT_EVIL_METHOD_THRESHOLD_MS = 1000;
30+
public static final int DEFAULT_EVIL_METHOD_THRESHOLD_MS = 700;
3131
public static final int DEFAULT_FPS_TIME_SLICE_ALIVE_MS = 10 * 1000;
3232
public static final int TIME_MILLIS_TO_NANO = 1000000;
3333
public static final int DEFAULT_ANR = 5 * 1000;

matrix/matrix-android/matrix-trace-canary/src/main/java/com/tencent/matrix/trace/core/AppMethodBeat.java

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
public class AppMethodBeat implements BeatLifecycle {
2121

2222
private static final String TAG = "Matrix.AppMethodBeat";
23+
public static boolean isDev = false;
2324
private static AppMethodBeat sInstance = new AppMethodBeat();
2425
private static final int STATUS_DEFAULT = Integer.MAX_VALUE;
2526
private static final int STATUS_STARTED = 2;
@@ -47,6 +48,24 @@ public class AppMethodBeat implements BeatLifecycle {
4748
private static Object updateTimeLock = new Object();
4849
private static boolean isPauseUpdateTime = false;
4950
private static Runnable checkStartExpiredRunnable = null;
51+
private static LooperMonitor.LooperDispatchListener looperMonitorListener = new LooperMonitor.LooperDispatchListener() {
52+
@Override
53+
public boolean isValid() {
54+
return status >= STATUS_READY;
55+
}
56+
57+
@Override
58+
public void dispatchStart() {
59+
super.dispatchStart();
60+
AppMethodBeat.dispatchBegin();
61+
}
62+
63+
@Override
64+
public void dispatchEnd() {
65+
super.dispatchEnd();
66+
AppMethodBeat.dispatchEnd();
67+
}
68+
};
5069

5170
static {
5271
sHandler.postDelayed(new Runnable() {
@@ -126,6 +145,7 @@ private static void realRelease() {
126145
if (status == STATUS_DEFAULT) {
127146
MatrixLog.i(TAG, "[realRelease] timestamp:%s", System.currentTimeMillis());
128147
sHandler.removeCallbacksAndMessages(null);
148+
LooperMonitor.unregister(looperMonitorListener);
129149
sTimerUpdateThread.quit();
130150
sBuffer = null;
131151
status = STATUS_OUT_RELEASE;
@@ -144,6 +164,7 @@ private static void realExecute() {
144164
@Override
145165
public void run() {
146166
synchronized (statusLock) {
167+
MatrixLog.i(TAG, "[startExpired] timestamp:%s status:%s", System.currentTimeMillis(), status);
147168
if (status == STATUS_DEFAULT || status == STATUS_READY) {
148169
status = STATUS_EXPIRED_START;
149170
}
@@ -152,24 +173,7 @@ public void run() {
152173
}, Constants.DEFAULT_RELEASE_BUFFER_DELAY);
153174

154175
ActivityThreadHacker.hackSysHandlerCallback();
155-
LooperMonitor.register(new LooperMonitor.LooperDispatchListener() {
156-
@Override
157-
public boolean isValid() {
158-
return status >= STATUS_READY;
159-
}
160-
161-
@Override
162-
public void dispatchStart() {
163-
super.dispatchStart();
164-
AppMethodBeat.dispatchBegin();
165-
}
166-
167-
@Override
168-
public void dispatchEnd() {
169-
super.dispatchEnd();
170-
AppMethodBeat.dispatchEnd();
171-
}
172-
});
176+
LooperMonitor.register(looperMonitorListener);
173177
}
174178

175179
private static void dispatchBegin() {
@@ -208,7 +212,7 @@ public static void i(int methodId) {
208212
}
209213
}
210214

211-
if (Thread.currentThread() == sMainThread) {
215+
if (Thread.currentThread().getId() == sMainThread.getId()) {
212216
if (assertIn) {
213217
android.util.Log.e(TAG, "ERROR!!! AppMethodBeat.i Recursive calls!!!");
214218
return;
@@ -237,7 +241,7 @@ public static void o(int methodId) {
237241
if (methodId >= METHOD_ID_MAX) {
238242
return;
239243
}
240-
if (Thread.currentThread() == sMainThread) {
244+
if (Thread.currentThread().getId() == sMainThread.getId()) {
241245
if (sIndex < Constants.BUFFER_SIZE) {
242246
mergeData(methodId, sIndex, false);
243247
} else {
@@ -329,7 +333,9 @@ public IndexRecord maskIndex(String source) {
329333
indexRecord.next = tmp;
330334
} else {
331335
IndexRecord tmp = last.next;
332-
last.next = indexRecord;
336+
if (null != last.next) {
337+
last.next = indexRecord;
338+
}
333339
indexRecord.next = tmp;
334340
}
335341
return indexRecord;

matrix/matrix-android/matrix-trace-canary/src/main/java/com/tencent/matrix/trace/core/LooperMonitor.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class LooperMonitor implements MessageQueue.IdleHandler {
1616
private static final HashSet<LooperDispatchListener> listeners = new HashSet<>();
1717
private static final String TAG = "Matrix.LooperMonitor";
1818
private static Printer printer;
19-
19+
public static Printer testPrinter = null;
2020

2121
public abstract static class LooperDispatchListener {
2222

@@ -85,6 +85,9 @@ public void println(String x) {
8585

8686
if (isValid) {
8787
dispatch(x.charAt(0) == '>');
88+
if (null != testPrinter) {
89+
testPrinter.println(x);
90+
}
8891
}
8992

9093
}
@@ -97,6 +100,15 @@ public static void register(LooperDispatchListener listener) {
97100
}
98101
}
99102

103+
public static void unregister(LooperDispatchListener listener) {
104+
if (null == listener) {
105+
return;
106+
}
107+
synchronized (listeners) {
108+
listeners.remove(listener);
109+
}
110+
}
111+
100112

101113
private static void dispatch(boolean isBegin) {
102114

matrix/matrix-android/matrix-trace-canary/src/main/java/com/tencent/matrix/trace/tracer/EvilMethodTracer.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ public void dispatchEnd(long beginMs, long cpuBeginMs, long endMs, long cpuEndMs
9595
}
9696
}
9797

98+
public void modifyEvilThresholdMs(long evilThresholdMs) {
99+
this.evilThresholdMs = evilThresholdMs;
100+
}
101+
98102
private class AnalyseTask implements Runnable {
99103
long[] queueCost;
100104
long[] data;

matrix/matrix-android/matrix-trace-canary/src/main/java/com/tencent/matrix/trace/util/TraceDataUtils.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package com.tencent.matrix.trace.util;
22

3+
import android.util.Log;
4+
35
import com.tencent.matrix.trace.constants.Constants;
46
import com.tencent.matrix.trace.core.AppMethodBeat;
57
import com.tencent.matrix.trace.items.MethodItem;
68
import com.tencent.matrix.util.MatrixLog;
79

8-
import java.util.ArrayList;
910
import java.util.Collections;
1011
import java.util.Comparator;
1112
import java.util.Iterator;
@@ -54,18 +55,27 @@ public static void structuredDataToStack(long[] buffer, LinkedList<MethodItem> r
5455
depth++;
5556
rawData.push(trueId);
5657
} else {
57-
int methodId = getMethodId(trueId); // out
58+
int outMethodId = getMethodId(trueId);
5859
if (!rawData.isEmpty()) {
5960
long in = rawData.pop();
6061
depth--;
61-
while (getMethodId(in) != methodId && !rawData.isEmpty()) {
62-
MatrixLog.w(TAG, "[structuredDataToStack] method[%s] not match in! pop[%s] to continue find!", in, methodId);
62+
int inMethodId;
63+
LinkedList<Long> tmp = new LinkedList<>();
64+
tmp.add(in);
65+
while ((inMethodId = getMethodId(in)) != outMethodId && !rawData.isEmpty()) {
66+
MatrixLog.w(TAG, "pop inMethodId[%s] to continue match ouMethodId[%s]", inMethodId, outMethodId);
6367
in = rawData.pop();
6468
depth--;
69+
tmp.add(in);
6570
}
66-
if (lastInId == methodId && methodId == AppMethodBeat.METHOD_ID_DISPATCH) {
71+
72+
if (inMethodId != outMethodId && inMethodId == AppMethodBeat.METHOD_ID_DISPATCH) {
73+
MatrixLog.e(TAG, "inMethodId[%s] != outMethodId[%s] throw this outMethodId!", inMethodId, outMethodId);
74+
rawData.addAll(tmp);
75+
depth += rawData.size();
6776
continue;
6877
}
78+
6979
long outTime = getTime(trueId);
7080
long inTime = getTime(in);
7181
long during = outTime - inTime;
@@ -75,10 +85,10 @@ public static void structuredDataToStack(long[] buffer, LinkedList<MethodItem> r
7585
result.clear();
7686
return;
7787
}
78-
MethodItem methodItem = new MethodItem(methodId, (int) during, depth);
88+
MethodItem methodItem = new MethodItem(outMethodId, (int) during, depth);
7989
addMethodItem(result, methodItem);
8090
} else {
81-
MatrixLog.w(TAG, "[structuredDataToStack] method[%s] not found in! ", methodId);
91+
MatrixLog.w(TAG, "[structuredDataToStack] method[%s] not found in! ", outMethodId);
8292
}
8393
}
8494
}
@@ -116,6 +126,9 @@ private static int getMethodId(long trueId) {
116126
}
117127

118128
private static int addMethodItem(LinkedList<MethodItem> resultStack, MethodItem item) {
129+
if (AppMethodBeat.isDev) {
130+
Log.v(TAG, "method:" + item);
131+
}
119132
MethodItem last = null;
120133
if (!resultStack.isEmpty()) {
121134
last = resultStack.peek();

samples/sample-android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ buildscript {
99
// }
1010
}
1111
dependencies {
12-
classpath 'com.android.tools.build:gradle:3.0.0'
12+
classpath 'com.android.tools.build:gradle:3.1.4'
1313
classpath 'com.google.code.gson:gson:2.7'
1414
classpath ("com.tencent.matrix:matrix-gradle-plugin:${MATRIX_VERSION}") { changing = true }
1515
}

0 commit comments

Comments
 (0)