Reference/traceview

From Android中文网

Android中文网(androidcn.net) 版权申明 : creativecommons licenses
Jump to: navigation, search

Feature Preview Only: The Traceview and dmtracedump utilities will be available in an upcoming release of the SDK.

提示:The Traceview及dmtracedump工具要在即将发布的下一版本SDK中才能使用。

Traceview is a graphical viewer for execution logs saved by your application. The sections below describe how to use the program.

Traceview是一个查看分析程序运行日志的图形化工具。以下几节将描述这个工具的用法。

目录

[编辑] Creating Trace Files 建立跟踪文件

To use Traceview, you need to generate log files containing the trace information you want to analyze. To do that, you include the Debug class in your code and call its methods to start and stop logging of trace information to disk. When your application quits, you can then use Traceview to examine the log files for useful run-time information such as method calls and run times.

要使用Traceview进行分析,首先必须要生成你想要分析的跟踪信息日志文件。你可以这样生成跟踪信息日志文件,在你的代码中直接调用Debug类的方法来记录跟踪信息并输出到日志文件中。当应用程序退出后,你就可以使用Traceview来对日志文件进行分析程序运行时的方法调用及运行次数等情况了。

To create the trace files, include the Debug class and call one of the startMethodTracing() methods. In the call, you specify a base name for the trace files that the system generates. To stop tracing, call stopMethodTracing(). These methods start and stop method tracing across the entire virtual machine. For example, you could call startMethodTracing() in your activity's onCreate() method, and call stopMethodTracing() in that activity's onDestroy() method.

为了生成跟踪日志文件,你应该在程序中包含Debug类并调用startMethodTracing()方法来开始进行日志跟踪,在这个调用方法中,你应该为跟踪日志文件指定一个名字。调用stopMethodTracing()方法来停止跟踪。 这些方法可以在虚拟机上任意开始和停止方法跟踪。比如:你在activity's onCreate()方法中调用startMethodTracing()开始跟踪,并在activity's onDestroy()方法中调用stopMethodTracing()来结束跟踪。

   // start tracing to "/tmp/calc"
   Debug.startMethodTracing("/tmp/calc");
   // ...
   // stop tracing
   Debug.stopMethodTracing();

When your application calls startMethodTracing(), the system creates two files:

当你调用startMethodTracing(),系统会生成以下两个文件:

  • <trace-base-name>.data -- contains binary method trace data
  • <trace-base-name>.data --方法跟踪数据的二进制文件.
  • <trace-base-name>.key -- a plain text file that contains mappings from binary identifiers to thread and method names
  • <trace-base-name>.key --一个映射二进制文件中线程和方法名的纯文本文件.

The system then begins buffering the generated trace data, until your application calls stopMethodTracing(), at which time it writes the buffered data to the files. If the system reaches the maximum buffer size before stopMethodTracing() is called, the system stops tracing and sends a notification to the console.

跟踪开始后,系统对生成的跟踪数据是先进行缓存的,直到程序调用了stopMethodTracing()方法时才把缓存的数据写到文件中。如果在调用stopMethodTracing()前,系统已经达到了缓存最大值时,则系统就停止跟踪并发一个通知到控制台。

The format of the trace files is described later in this document.

跟踪文件的格式下面会详细进行说明.

[编辑] Copying Trace Files to a Host Machine 拷贝跟踪文件到主机

After your application has run and the system has created your trace files <trace-base-name>.data and <trace-base-name>.key. on a device or emulator, you must copy those files to your development computer. You can use adp pull to copy the files. Here's an example that shows how to copy two example files, calc.data and calc.key, from the default location on the emulator to the /tmp directory on the emulator host machine:

当程序运行并在手机设备或者模拟器上生成了跟踪文件<trace-base-name>.data 和 <trace-base-name>.key后,你必须把这些文件拷贝到开发环境的主机上。使用adp命令进行拷贝,以下例子说明如何从模拟器默认位置拷贝calc.data 和 calc.key到模拟器的主机的/tmp目录下的。


   adb pull /tmp/calc.data /tmp 
   adb pull /tmp/calc.key /tmp

[编辑] Viewing Trace Files in Traceview 使用Traceview查看跟踪文件

To run traceview and view the trace files, enter traceview <trace-base-name>. For example, to run Traceview on the example files copied in the previous section, you would use:

输入traceview <trace-base-name>命令运行traceview工具来查看跟踪文件。例如:通过Traceview工具运行上一节中用到的例子文件,你会看到:

   traceview /tmp/calc

Traceview loads the log files and displays their data in a window that has two panels:

  • A timeline panel -- describes when each thread and method started and stopped
  • A profile panel -- provides a summary of what happened inside a method

The sections below provide addition information about the traceview output panes.

Traceview载入日志文件并显示数据在两个面板上:

  • 时间轴面板—描述每个线程和方法的开始和终止。
  • Profile面板—提供一个方法中发生了什么的摘要。

以下部分提供traceview输出面板的详细信息。

[编辑] Timeline Panel

The image below shows a close up of the timeline panel. Each thread’s execution is shown in its own row, with time increasing to the right. Each method is shown in another color (colors are reused in a round-robin fashion starting with the methods that have the most inclusive time). The thin lines underneath the first row show the extent (entry to exit) of all the calls to the selected method. The method in this case is LoadListener.nativeFinished() and it was selected in the profile view.

下图显示了时间轴面板的一个结果。每个线程的执行都显示在随着时间渐增右移的各自行上。不同的方法用不同的颜色来表示。第一行下面的细线显示选中方法的调用时长(由进入到退出)。本例中的方法是LoadListener.nativeFinished(),它是在profile面板中选中的。

[编辑] Profile Panel

The image below shows the profile pane. The profile pane shows a summary of all the time spent in a method. The table shows both the inclusive and exclusive times (as well as the percentage of the total time). Exclusive time is the time spent in the method. Inclusive time is the time spent in the method plus the time spent in any called functions. We refer to calling methods as "parents" and called methods as "children." When a method is selected (by clicking on it), it expands to show the parents and children. Parents are shown with a purple background and children with a yellow background. The last column in the table shows the number of calls to this method plus the number of recursive calls. The last column shows the number of calls out of the total number of calls made to that method. In this view, we can see that there were 14 calls to LoadListener.nativeFinished(); looking at the timeline panel shows that one of those calls took an unusually long time.

下图是profile面板,面板显示了每一个方法的所花费时间的概要.包括inclusive和exclusive时间(同时用百分比表示)。Exclusive时间:方法执行所花费的时间。Inclusive时间: 方法执行所花费的时间+方法调用所花费的时间。通常调用方法为“父节点”,被调用方法为“子节点”。当一个方法被选中(单击),它就展开并显示父子节点,背景色为紫色的是父节点, 背景色为黄色的是子节点。表中最后一栏显示的是调用这个方法的次数+递归调用的次数。同时也表示的是调用次数/总调用次数。下图中,我们可以看出LoadListener.nativeFinished()调用了14次。此时看一下时间轴面板,面板显示表明每次调用都花费了很长的时间。

[编辑] Traceview File Format Traceview的文件格式

Tracefile creates two trace files: a .data file, which holds the trace data, and a .key file, which provides a mapping from binary identifiers to thread and method names. This section describes the format of both files.

Tracefile生成两个跟踪文件:文件a.data保存跟踪的数据,文件a.key提供从二进标识符到线程和方法名的映射。这部分描述这两种文件的格式。

[编辑] Data File Format Data文件格式

The data file is a binary file with the extension .data. It is structured as follows (all values are stored in little-endian order):

Data文件是以扩展名.data的二进制文件。它的结构如下:

   * File format:
   * header
   * record 0
   * record 1
   * ...
   *
   * Header format:
   * u4 magic 0x574f4c53 ('SLOW')
   * u2 version
   * u2 offset to data
   * u8 start date/time in usec
   *
   * Record format:
   * u1 thread ID
   * u4 method ID | method action
   * u4 time delta since start, in usec

The application is expected to parse all of the header fields, then seek to "offset to data" from the start of the file. From there it just reads 9-byte records until EOF is reached.

u8 start date/time in usec is the output from gettimeofday(). It's mainly there so that you can tell if the output was generated yesterday or three months ago.

method action sits in the two least-significant bits of the method word. The currently defined meanings are:

  • 0 - method entry
  • 1 - method exit
  • 2 - method "exited" when unrolled by exception handling
  • 3 - (reserved)

An unsigned 32-bit integer can hold about 70 minutes of time in microseconds.

程序从文件开始解析header字段,并查找“偏移数据”,每次只读9-byte,直到EOF结束。以u8开始表示来表示输出的日期/时间,这样,你可以知道是昨天还是三天以前输出的了。方法动作用2个字节来表示,定义如下:

  • 0 - 表示进行
  • 1 - 表示退出
  • 2 - 表示异常退出
  • 3 - (保留)

32位的无符号整数可以表示70分钟以微秒为单位的时长.

[编辑] Key File Format Key文件格式

The key file is a plain text file divided into three sections. Each section starts with a keyword that begins with '*'. If you see a '*' at the start of a line, you have found the start of a new section.

An example file might look like this:

Key文件是一个由三部分组成的纯文本文件,每一部分用关键字‘*’作为开始,如果你看到某一行以‘*’开始,则表示这是新部分的开始。

文件可能如下:

   *version
   1
   clock=global
   *threads
   1 main
   6 JDWP Handler
   5 Async GC
   4 Reference Handler
   3 Finalizer
   2 Signal Handler
   *methods
   0x080f23f8 java/io/PrintStream write ([BII)V
   0x080f25d4 java/io/PrintStream print (Ljava/lang/String;)V
   0x080f27f4 java/io/PrintStream println (Ljava/lang/String;)V
   0x080da620 java/lang/RuntimeException	<init>	()V
   [...]
   0x080f630c android/os/Debug startMethodTracing ()V
   0x080f6350 android/os/Debug startMethodTracing (Ljava/lang/String;Ljava/lang/String;I)V
   *end

[编辑] version section

The first line is the file version number, currently 1. The second line, clock=global, indicates that we use a common clock across all threads. A future version may use per-thread CPU time counters that are independent for every thread.

[编辑] 版本部分

第一行是文件版本号,通常是1,第二行clock=global描述所有线程共用的时钟.以后版本可能会用每个独立线程的CPU时钟来表示。

[编辑] threads section

One line per thread. Each line consists of two parts: the thread ID, followed by a tab, followed by the thread name. There are few restrictions on what a valid thread name is, so include everything to the end of the line.

[编辑] 线程部分

每个线程一行,每行包括两部分:线程ID ,一个tab,线程名。线程名没有限制,到这行结尾都是线程名部分。

[编辑] methods section

One line per method entry or exit. A line consists of four pieces, separated by tab marks: method-ID [TAB] class-name [TAB] method-name [TAB] signature . Only the methods that were actually entered or exited are included in the list. Note that all three identifiers are required to uniquely identify a method.

[编辑] 方法部分

每行表示一个方法,一行由四部分组成,用tab标识进行分隔:方法ID [TAB]类名[TAB] 方法名[TAB]信号 。无论是方法进入还是退出都会记录在这个列表上,注意:三个标识符是必须的,它唯一表示了方法.

Neither the threads nor methods sections are sorted.

除线程、方法部分外的都是类别部分。

[编辑] Traceview Known Issues Traceview存在问题

Threads

Traceview logging does not handle threads well, resulting in these two problems:

  1. If a thread exits during profiling, the thread name is not emitted;
  2. The VM reuses thread IDs. If a thread stops and another starts, they may get the same ID.

Traceview日志对线程处理得不是很好,存在以下2个问题:

  1. 如果一个线程存在于作分析图期间,这个线程是不会被发表的。
  2. 虚拟机上线程ID重用问题,如果一个线程停止后另一个线程开始,它们可能使用了同一个线程ID。

[编辑] Using dmtracedump dmtracedump用法

The Android SDK includes dmtracedump, a tool that gives you an alternate way of generating graphical call-stack diagrams from trace log files. The tool uses the Graphviz Dot utility to create the graphical output, so you need to install Graphviz before running dmtracedump.

The dmtracedump tool generates the call stack data as a tree diagram, with each call represented as a node. It shows call flow (from parent node to child nodes) using arrows. The diagram below shows an example of dmtracedump output.

Android SDK有一个dmtracedump工具,这个工具可以让从跟踪日志中生图形化的方法调用图和堆栈图。工具使用Graphviz Dot组件来生成图形的,所以要运行dmtracedump就必须先安装Graphviz。

Dmtracedump用树状图来表示堆栈数据,每一个数据用一个节点来表示。用箭头来表示(从父节点到子节点的)调用。下图显示了dmtracedump输出的一个例子。

For each node, dmtracedump shows <ref> callname (<inc-ms>, <exc-ms>,<numcalls>), where

  • <ref> -- Call reference number, as used in trace logs
  • <inc-ms> -- Inclusive elapsed time (milliseconds spent in method, including all child methods)
  • <exc-ms> -- Exclusive elapsed time (milliseconds spent in method, not including any child methods)
  • <numcalls> -- Number of calls

The usage for dmtracedump is:

对于每个节点,dmtracedump显示格式:<ref> callname (<inc-ms>, <exc-ms>,<numcalls>),其中:

  • <ref> -- 编号
  • <inc-ms> -- Inclusive时间总和(微秒为单位,包括子方法时间)
  • <exc-ms> -- Exclusive时间总和(微秒为单位,不包括子方法时间)
  • <numcalls> -- 调用次数

dmtracedump用法:

   dmtracedump [-ho] [-s sortable] [-d trace-base-name] [-g outfile] <trace-base-name>

The tool then loads trace log data from <trace-base-name>.data and <trace-base-name>.key. The table below lists the options for dmtracedump.

这个工具从<trace-base-name>.data and <trace-base-name>.key加载日志数据。下表描述dmtracedump的选项列表:

Option Description
-d <trace-base-name> Diff with this trace name
-g <outfile> Generate output to <outfile>
-h Turn on HTML output
-o Dump the trace file instead of profiling
-d <trace-base-name> URL base to the location of the sortable javascript file
-t <percent> Minimum threshold for including child nodes in the graph (child's inclusive time as a percentage of parent inclusive time). If this option is not used, the default threshold is 20%.
Personal tools