1. 普通使用
1 |
atrace -a com.jeoe.ebox gfx wm am sched view app res ss |
这样会把结果输出到控制台,对分析没什么意义,可以用于测试这个命令
2. 抓trace并保存到文件
1 |
atrace -a com.jeoe.ebox gfx wm am sched view app res ss -o /sdcard/a.trace |
这样会把分析文件输出到/sdcard/a.trace文件
将a.trace文件导出到电脑,然后使用sdk下的systrace.py程序将分析结果生成html文件,命令如下:
1 |
python systrace.py --from-file ~/a.trace -o ~/a.htm |
–from-file 参数表明从文件读入分析数据
~/a.trace 源数据文件
-o ~/a.html 指定输出文件路径
3. 循环抓取
4. atrace帮助内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
usage: atrace [options] [categories...] options include: -a appname enable app-level tracing for a comma separated list of cmdlines -b N use a trace buffer size of N KB -c trace into a circular buffer -f filename use the categories written in a file as space-separated values in a line -k fname,... trace the listed kernel functions -n ignore signals -s N sleep for N seconds before tracing [default 0] -t N trace for N seconds [default 5] -z compress the trace dump --async_start start circular trace and return immediately --async_dump dump the current contents of circular trace buffer --async_stop stop tracing and dump the current contents of circular trace buffer --stream stream trace to stdout as it enters the trace buffer Note: this can take significant CPU time, and is best used for measuring things that are not affected by CPU performance, like pagecache usage. --list_categories list the available tracing categories -o filename write the trace to the specified file instead of stdout. |
5. 允许的categories
执行下面命令可以看到atrace允许追踪的category
1 |
atrace --list_categories |
下面是笔者附上来的列表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
gfx - Graphics input - Input view - View System webview - WebView wm - Window Manager am - Activity Manager sm - Sync Manager audio - Audio video - Video camera - Camera hal - Hardware Modules app - Application res - Resource Loading dalvik - Dalvik VM rs - RenderScript bionic - Bionic C Library power - Power Management pm - Package Manager ss - System Server database - Database network - Network adb - ADB pdx - PDX services sched - CPU Scheduling irq - IRQ Events i2c - I2C Events freq - CPU Frequency idle - CPU Idle disk - Disk I/O mmc - eMMC commands workq - Kernel Workqueues regulators - Voltage and Current Regulators binder_driver - Binder Kernel driver binder_lock - Binder global lock trace pagecache - Page cache |
6. 在自己的程序中加入trace代码
系统版本大于等于Android 4.3 (API18)以上的才可以用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import android.content.Intent import android.os.Build import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.os.Trace class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { Trace.beginSection("aaabbbcccddd") }; //*************您的代码 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { Trace.endSection(); }; } } |