简体   繁体   English

Android - 通过DDMS分析特定线程(UI线程)

[英]Android - profiling a specific thread (UI Thread) via DDMS


I'm trying to find a cause of freezes on UI thread in my app in certain conditions. 我试图在某些条件下在我的应用程序中找到UI线程冻结的原因。
I have run the app through DDMS profiling. 我通过DDMS分析运行了应用程序。 But in the bottom tree-table view I see all the methods called on all threads, whereas I would like to focus only on main thread. 但是在底部的树表视图中,我看到所有线程都调用了所有方法,而我只想关注主线程。

Is there a way to filter the bottom tree-table list of methods in DDMS, to include only statistics for a chosen thread (UI thread in my case) ? 有没有办法过滤DDMS中底层树表的方法列表,只包括所选线程的统计信息(在我的情况下是UI线程)?

A sub-question: is there some good Android profiler out there? 一个子问题:那里有一些好的Android分析器吗?

The UI doesn't provide a way to do this -- you just have to skim the timeline and click on busy-looking sections. 用户界面没有提供这样做的方法 - 你只需要浏览时间线并点击看起来很忙的部分。

There's a rough approach that may do what you want. 有一种粗略的方法可以做你想要的。

The SDK includes a tool called dmtracedump that can be used to generate reports from the .trace file. SDK包含一个名为dmtracedump的工具,可用于从.trace文件生成报告。 When you grab the trace with DDMS, note the file's location (mine was /tmp/ddms4176182990461128308.trace ). 当您使用DDMS获取跟踪时,请记下文件的位置(我的是/tmp/ddms4176182990461128308.trace )。 On Linux, you would run: 在Linux上,您将运行:

dmtracedump -o <filename> > trace.txt

This will give you a file that looks like: 这将为您提供如下文件:

VERSION: 3
Threads (13):
 1 main
 2 GC
 3 Signal Catcher
 4 JDWP
...
Trace (threadID action usecs class.method signature):
 4 xit         0 ..dalvik/system/VMDebug.startMethodTracingDdms (IIZI)V
 4 xit         0 .android/os/Debug.startMethodTracingDdms (IIZI)V
 4 xit         0 android/ddm/DdmHandleProfiling.handleMPSS (Lorg/apache/harmony/dalvik/ddmc/Chunk;)Lorg/apache/harmony/dalvik/ddmc/Chunk;
...

As noted in the output, each line begins with a thread ID that corresponds to the table at the top (so thread ID 4 is the JDWP thread, which handles DDMS traffic). 如输出中所述,每行以对应于顶部表的线程ID开头(因此线程ID 4是处理DDMS流量的JDWP线程)。 This is followed by an action code: ent for method entry, xit for method exit, or unr for stack unroll due to an exception. 接下来是一个操作代码: ent表示方法入口, xit表示方法退出,或者unr表示由于异常导致的堆栈展开。 After that comes the per-thread CPU time stamp (ie the cumulative CPU time used by this thread), then the method name and signature. 之后是每线程CPU时间戳(即该线程使用的累计CPU时间),然后是方法名称和签名。

If you just want to see the main UI thread (thread ID 1), you can use: 如果您只想查看主UI线程(线程ID 1),您可以使用:

grep "^ 1 " trace.txt

Now it's a simple matter of parsing the output. 现在解析输出很简单。 :-) :-)

It would be easier to handle if you could strip the data for the uninteresting threads out of the trace file, then just open the new file with the traceview tool (included with the SDK). 如果您可以从跟踪文件中删除不感兴趣的线程的数据,那么将更容易处理,然后使用traceview工具(包含在SDK中)打开新文件。 If you open a .trace file you can see that it's a chunk of text followed by a chunk of binary data. 如果你打开一个.trace文件,你会发现它是一大块文本,后跟一大块二进制数据。 You would need to write a program that passes the text through, then parses the binary part and drops anything with a thread ID other than the one you're interested in. The format is simple -- a collection of fixed-size records -- described in a comment near the top of the profiling code . 您需要编写一个程序来传递文本,然后解析二进制部分并删除任何线程ID而不是您感兴趣的线程ID。格式很简单 - 一组固定大小的记录 - 在分析代码顶部附近的注释中描述

(I don't know if somebody has written this already.) (我不知道是否有人写过这个。)

To view your code activity (hotspots with call-stacks) on certain thread (UI for example) I propose to use Intel VTune from System Studio. 要在某个线程(例如UI)上查看代码活动(带有调用堆栈的热点),我建议使用System Studio中的Intel VTune。 You could collect Basic hotspots profile on your Android device, open Bottom-up tab in and select Grouping : Thread / Function / Callstack. 您可以在Android设备上收集Basic热点配置文件,打开Bottom-up选项卡并选择Grouping:Thread / Function / Callstack。 After that you see per-thread hotspot-profile of your application. 之后,您会看到应用程序的每个线程的热点配置文件。 (see details in attached screenshot) (详见附页截图) 在此输入图像描述

Here is tutorial how to use VTune: http://software.intel.com/en-us/articles/using-intel-vtune-amplifier-on-non-rooted-android-devices 以下是如何使用VTune的教程: http//software.intel.com/en-us/articles/using-intel-vtune-amplifier-on-non-rooted-android-devices

The best android profiler is certainly "Eclipse Memory Analyzer". 最好的android分析器肯定是“Eclipse Memory Analyzer”。 It allow you to take a snapshot of your memory and look in deep what's happening on it. 它允许您拍摄记忆的快照,并深入了解其中发生的事情。 Please take a look on the link below to learn more about it. 请查看下面的链接以了解有关它的更多信息。

http://eclipsesource.com/blogs/2013/01/21/10-tips-for-using-the-eclipse-memory-analyzer/ http://eclipsesource.com/blogs/2013/01/21/10-tips-for-using-the-eclipse-memory-analyzer/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM