Skip to content

Commit

Permalink
1.实现无侵入性集成MethodTraceMan 2.优化MethodTraceMan内存占用问题
Browse files Browse the repository at this point in the history
  • Loading branch information
cxzhengfont committed Dec 8, 2019
1 parent e910697 commit 9075933
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 26 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 12 additions & 2 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ dependencies {
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

debugImplementation 'com.github.zhengcx:MethodTraceMan:1.0.6'
releaseImplementation 'com.github.zhengcx:MethodTraceMan:1.0.5-noop'
// debugImplementation 'com.github.zhengcx:MethodTraceMan:1.0.6'
// releaseImplementation 'com.github.zhengcx:MethodTraceMan:1.0.5-noop'

// releaseImplementation project(':tracemanui-noop')
// implementation project(':tracemanui')
implementation project(':tracemanui')

}

Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/cn/cxzheng/asmtraceman/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class MainActivity : AppCompatActivity() {
private val random by lazy { Random() }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
MethodTraceServerManager.startService(applicationContext)
MethodTraceServerManager.logLevel = MethodTraceServerManager.MTM_LOG_DETAIL

setContentView(R.layout.activity_main)
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
include ':app'
include ':app', ':tracemanui'
//include ':app', ':tracemanui',':tracemanplugin', ':tracemanui-noop'
10 changes: 9 additions & 1 deletion tracemanui/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.cxzheng.tracemanui"/>
package="cn.cxzheng.tracemanui">

<application>
<provider
android:name=".TraceManProvider"
android:authorities="${applicationId}.contentprovider"
android:exported="false" />
</application>
</manifest>
42 changes: 24 additions & 18 deletions tracemanui/src/main/java/cn/cxzheng/tracemanui/TraceMan.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
import android.os.Looper;
import android.os.Trace;
import android.support.annotation.RequiresApi;
import android.util.Log;

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

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import cn.cxzheng.tracemanui.utils.LogUtil;

Expand All @@ -21,13 +20,15 @@
*/
public class TraceMan {

private static CopyOnWriteArrayList<Entity> methodList = new CopyOnWriteArrayList();
private static List<Entity> methodList = new LinkedList<>();

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public static void start(String name) {
if (isOpenTraceMethod()) {
Trace.beginSection(name);
methodList.add(new Entity(name, System.currentTimeMillis(), true, isInMainThread()));
synchronized (methodList) {
methodList.add(new Entity(name, System.currentTimeMillis(), true, isInMainThread()));
}
}
}

Expand All @@ -36,7 +37,9 @@ public static void end(String name) {
if (isOpenTraceMethod()) {
LogUtil.detail("执行了方法:" + name);
Trace.endSection();
methodList.add(new Entity(name, System.currentTimeMillis(), false, isInMainThread()));
synchronized (methodList) {
methodList.add(new Entity(name, System.currentTimeMillis(), false, isInMainThread()));
}
}
}

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


public static void resetTraceManData() {
methodList.clear();
synchronized (methodList) {
methodList.clear();
}
}

/**
* 处理插桩数据,按顺序获取所有方法耗时
*/
public static List<MethodInfo> obtainMethodCostData() {
List<MethodInfo> resultList = new ArrayList();
for (int i = 0; i < methodList.size(); i++) {
Entity startEntity = methodList.get(i);
if (!startEntity.isStart) {
continue;
}
startEntity.pos = i;
Entity endEntity = findEndEntity(startEntity.name, i + 1);
synchronized (methodList) {
List<MethodInfo> resultList = new ArrayList();
for (int i = 0; i < methodList.size(); i++) {
Entity startEntity = methodList.get(i);
if (!startEntity.isStart) {
continue;
}
startEntity.pos = i;
Entity endEntity = findEndEntity(startEntity.name, i + 1);

if (startEntity != null && endEntity != null && endEntity.time - startEntity.time > 0) {
resultList.add(createMethodInfo(startEntity, endEntity));
if (startEntity != null && endEntity != null && endEntity.time - startEntity.time > 0) {
resultList.add(createMethodInfo(startEntity, endEntity));
}
}
return resultList;
}

return resultList;
}

/**
Expand Down
47 changes: 47 additions & 0 deletions tracemanui/src/main/java/cn/cxzheng/tracemanui/TraceManProvider.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cn.cxzheng.tracemanui

import android.content.ContentProvider
import android.content.ContentValues
import android.database.Cursor
import android.net.Uri

/**
* Create by cxzheng on 2019-12-08
*/
class TraceManProvider : ContentProvider() {
override fun insert(uri: Uri, values: ContentValues?): Uri? {
return null
}

override fun query(
uri: Uri,
projection: Array<String>?,
selection: String?,
selectionArgs: Array<String>?,
sortOrder: String?
): Cursor? {
return null
}

override fun update(
uri: Uri,
values: ContentValues?,
selection: String?,
selectionArgs: Array<String>?
): Int {
return 0
}

override fun delete(uri: Uri, selection: String?, selectionArgs: Array<String>?): Int {
return 0
}

override fun getType(uri: Uri): String? {
return null
}

override fun onCreate(): Boolean {
MethodTraceServerManager.startService(context)
return true
}
}

0 comments on commit 9075933

Please sign in to comment.