Android perfetto性能分析工具总结

1、使用perfetto抓取trace

atrace --list_categories  列出抓取的trace策略

示例: 
1)perfetto抓trace文件
adb shell perfetto gfx view wm am sm hal res rs ss  -o /data/misc/perfetto-traces/trace -t 5s
或
adb shell perfetto gfx view wm am hal rs ss aidl power -o /data/misc/perfetto-traces/trace --app app包名 -t 5s 
或
adb shell perfetto gfx input view wm am sm hal res rs ss  aidl sched irq freq load workq binder_driver thermal -o /data/misc/perfetto-traces/trace -t 10s
 
2)导出trace文件
adb pull /data/misc/perfetto-traces/trace .

2、分析trace文件工具
Google Chrome浏览器打开https://ui.perfetto.dev
1)打开https://ui.perfetto.dev/网页后,点击open trace file,选择刚才抓取的trace文件。
2) 搜索trace关键字 -> f -> shift + m标记
例如搜索:activityConfigChanged。
或者搜索自己添加的RootWindowContainer_onDisplayChanged等等等

示例:
1)在搜索框输入:RootWindowContainer_onDisplayChanged
点击左右箭头跳到上一处或下一处该关键字出现的地方。
2)点击f键会滚动并放大到刚才选择关键字的地方
3)点击m临时标记或者shift + m永久标记
添加自己的trace方法:
1,framework层:
import android.os.Trace;
Trace.traceBegin(long traceTag, String methodName)
Trace.traceEnd(long traceTag)
在代码中必须成对出现,一般将traceEnd放入到finally语句块,另外,必须在同一个线程。

例如:
Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastReceiveReg");
...
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);

2,app层
import android.os.Trace;
android.os.Trace.beginSection("lqy111");
Log.d("systrace_log_for_app_lqy111");
android.os.Trace.endSection();

3,native framework层
#define ATRACE_TAG xxx // xxx和当前相关代码有关
#define ATRACE_TAG ATRACE_TAG_GRAPHICS
#include
ATRACE_CALL();
ATRACE_NAME("lqy111");    //#define ATRACE_CALL() ATRACE_NAME(__FUNCTION__)ATRACE_BEGIN("lqy111");
...
ATRACE_END();#include 
ATRACE_FORMAT("onMessageInvalidate %s", "hahaha");

示例:AOSP在SurfaceFlinger.cpp添加的trace
http://aospxref.com/android-12.0.0_r3/xref/frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp
23  #define ATRACE_TAG ATRACE_TAG_GRAPHICS
1894      ATRACE_FORMAT("onMessageInvalidate %" PRId64 " vsyncIn %.2fms%s", vsyncId, vsyncIn,
1895                    mExpectedPresentTime == expectedVSyncTime ? "" : " (adjusted)");

示例:AOSP在ActivityConfigurationChangeItem.java添加的trace
http://aospxref.com/android-12.0.0_r3/xref/frameworks/base/core/java/android/app/servertransaction/ActivityConfigurationChangeItem.java
51      public void execute(ClientTransactionHandler client, ActivityClientRecord r,
52              PendingTransactionActions pendingActions) {
53          // TODO(lifecycler): detect if PIP or multi-window mode changed and report it here.
54          Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "activityConfigChanged");
55          client.handleActivityConfigurationChanged(r, mConfiguration, INVALID_DISPLAY);
56          Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
57      }

参考代码:
void debug_timestamp(char *msg)
{
	struct timespec64 t;

	ktime_get_ts64(&t);
	pr_err("**%s: %lld.%9.9ld\n", msg, t.tv_sec, t.tv_nsec);
}

时间相关:timekeeping.h
ktime_get_real_ns()

printk(KERN_ERR "lqy111 drm_mode_atomic_ioctl start: real_ns:%ld", ktime_get_real_ns());


打印java层堆栈log:
        String st = android.util.Log.getStackTraceString(new RuntimeException());
        android.util.Log.e("lqy111", " lqy111 === " + st);

String st = android.util.Log.getStackTraceString(new RuntimeException());
android.util.Log.e("lqy111", " elapsedRealtime(): " + SystemClock.elapsedRealtime() + " === " + st);
    
        
打印C++层堆栈log:
     shared_libs: [
         "libSurfaceFlingerProp",
+        "libutils",
+        "libutilscallstack",
     ],

Android.pb的shared_libs中增加:
        "libutils",
        "libutilscallstack",

#include 
android::CallStack::logStack("lqy111 ~Display");

    

你可能感兴趣的:(工具使用技巧,Android,Framework,Android,android)