Skip to content

Commit 9075933

Browse files
committed
1.实现无侵入性集成MethodTraceMan 2.优化MethodTraceMan内存占用问题
1 parent e910697 commit 9075933

File tree

8 files changed

+97
-26
lines changed

8 files changed

+97
-26
lines changed

.idea/gradle.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 12 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ dependencies {
3636
androidTestImplementation 'com.android.support.test:runner:1.0.2'
3737
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
3838

39-
debugImplementation 'com.github.zhengcx:MethodTraceMan:1.0.6'
40-
releaseImplementation 'com.github.zhengcx:MethodTraceMan:1.0.5-noop'
39+
// debugImplementation 'com.github.zhengcx:MethodTraceMan:1.0.6'
40+
// releaseImplementation 'com.github.zhengcx:MethodTraceMan:1.0.5-noop'
4141

4242
// releaseImplementation project(':tracemanui-noop')
43-
// implementation project(':tracemanui')
43+
implementation project(':tracemanui')
4444

4545
}
4646

app/src/main/java/cn/cxzheng/asmtraceman/MainActivity.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ class MainActivity : AppCompatActivity() {
1414
private val random by lazy { Random() }
1515
override fun onCreate(savedInstanceState: Bundle?) {
1616
super.onCreate(savedInstanceState)
17-
MethodTraceServerManager.startService(applicationContext)
1817
MethodTraceServerManager.logLevel = MethodTraceServerManager.MTM_LOG_DETAIL
1918

2019
setContentView(R.layout.activity_main)

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
include ':app'
1+
include ':app', ':tracemanui'
22
//include ':app', ':tracemanui',':tracemanplugin', ':tracemanui-noop'
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2-
package="cn.cxzheng.tracemanui"/>
2+
package="cn.cxzheng.tracemanui">
3+
4+
<application>
5+
<provider
6+
android:name=".TraceManProvider"
7+
android:authorities="${applicationId}.contentprovider"
8+
android:exported="false" />
9+
</application>
10+
</manifest>

tracemanui/src/main/java/cn/cxzheng/tracemanui/TraceMan.java

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44
import android.os.Looper;
55
import android.os.Trace;
66
import android.support.annotation.RequiresApi;
7-
import android.util.Log;
87

98
import com.ctrip.ibu.hotel.debug.server.producer.module.methodcost.MethodInfo;
109

1110
import java.util.ArrayList;
11+
import java.util.LinkedList;
1212
import java.util.List;
13-
import java.util.concurrent.CopyOnWriteArrayList;
1413

1514
import cn.cxzheng.tracemanui.utils.LogUtil;
1615

@@ -21,13 +20,15 @@
2120
*/
2221
public class TraceMan {
2322

24-
private static CopyOnWriteArrayList<Entity> methodList = new CopyOnWriteArrayList();
23+
private static List<Entity> methodList = new LinkedList<>();
2524

2625
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
2726
public static void start(String name) {
2827
if (isOpenTraceMethod()) {
2928
Trace.beginSection(name);
30-
methodList.add(new Entity(name, System.currentTimeMillis(), true, isInMainThread()));
29+
synchronized (methodList) {
30+
methodList.add(new Entity(name, System.currentTimeMillis(), true, isInMainThread()));
31+
}
3132
}
3233
}
3334

@@ -36,7 +37,9 @@ public static void end(String name) {
3637
if (isOpenTraceMethod()) {
3738
LogUtil.detail("执行了方法:" + name);
3839
Trace.endSection();
39-
methodList.add(new Entity(name, System.currentTimeMillis(), false, isInMainThread()));
40+
synchronized (methodList) {
41+
methodList.add(new Entity(name, System.currentTimeMillis(), false, isInMainThread()));
42+
}
4043
}
4144
}
4245

@@ -53,28 +56,31 @@ public static List<MethodInfo> endCollectMethodCost() {
5356

5457

5558
public static void resetTraceManData() {
56-
methodList.clear();
59+
synchronized (methodList) {
60+
methodList.clear();
61+
}
5762
}
5863

5964
/**
6065
* 处理插桩数据,按顺序获取所有方法耗时
6166
*/
6267
public static List<MethodInfo> obtainMethodCostData() {
63-
List<MethodInfo> resultList = new ArrayList();
64-
for (int i = 0; i < methodList.size(); i++) {
65-
Entity startEntity = methodList.get(i);
66-
if (!startEntity.isStart) {
67-
continue;
68-
}
69-
startEntity.pos = i;
70-
Entity endEntity = findEndEntity(startEntity.name, i + 1);
68+
synchronized (methodList) {
69+
List<MethodInfo> resultList = new ArrayList();
70+
for (int i = 0; i < methodList.size(); i++) {
71+
Entity startEntity = methodList.get(i);
72+
if (!startEntity.isStart) {
73+
continue;
74+
}
75+
startEntity.pos = i;
76+
Entity endEntity = findEndEntity(startEntity.name, i + 1);
7177

72-
if (startEntity != null && endEntity != null && endEntity.time - startEntity.time > 0) {
73-
resultList.add(createMethodInfo(startEntity, endEntity));
78+
if (startEntity != null && endEntity != null && endEntity.time - startEntity.time > 0) {
79+
resultList.add(createMethodInfo(startEntity, endEntity));
80+
}
7481
}
82+
return resultList;
7583
}
76-
77-
return resultList;
7884
}
7985

8086
/**
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package cn.cxzheng.tracemanui
2+
3+
import android.content.ContentProvider
4+
import android.content.ContentValues
5+
import android.database.Cursor
6+
import android.net.Uri
7+
8+
/**
9+
* Create by cxzheng on 2019-12-08
10+
*/
11+
class TraceManProvider : ContentProvider() {
12+
override fun insert(uri: Uri, values: ContentValues?): Uri? {
13+
return null
14+
}
15+
16+
override fun query(
17+
uri: Uri,
18+
projection: Array<String>?,
19+
selection: String?,
20+
selectionArgs: Array<String>?,
21+
sortOrder: String?
22+
): Cursor? {
23+
return null
24+
}
25+
26+
override fun update(
27+
uri: Uri,
28+
values: ContentValues?,
29+
selection: String?,
30+
selectionArgs: Array<String>?
31+
): Int {
32+
return 0
33+
}
34+
35+
override fun delete(uri: Uri, selection: String?, selectionArgs: Array<String>?): Int {
36+
return 0
37+
}
38+
39+
override fun getType(uri: Uri): String? {
40+
return null
41+
}
42+
43+
override fun onCreate(): Boolean {
44+
MethodTraceServerManager.startService(context)
45+
return true
46+
}
47+
}

0 commit comments

Comments
 (0)