天天看点

Instruments User Guide编程指南3-Navigate Collected DataAbout Data AnalysisNavigate  the Timeline Pane Navigate the Detail Pane  操作详情面板Map Data to Source Code 映射数据到源码See Your App's Source Code 查看对应APP源码

About Data Analysis

Instrument并不帮助我们解决代码问题。它帮助我们更好的理解我们的代码做了什么,当APP运行的时候,通过捕捉,统计详细的数据并呈现数据给我们进行分析。由于每个APP是不一样的,所以实际发现和处理问题的步骤也是不一样的。因此,我们必须学习怎样去理解Instrument为我们收集的数据,过滤不需要的数据,研究与APP相关的数据。我们必须执行一些检测工作,对于任意相关数据识别APP程序中的代码,以至于能够对代码进行改善。

在执行trace之后,我们可以检查在时间轴(timeline)面板,详情面板(detail pane),监督面板中扩展详情区域(extended detail area (

Instruments User Guide编程指南3-Navigate Collected DataAbout Data AnalysisNavigate  the Timeline Pane Navigate the Detail Pane  操作详情面板Map Data to Source Code 映射数据到源码See Your App's Source Code 查看对应APP源码

) of the inspector pane),中收集的数据,如Figure 10-1:

Figure 10-1A trace document containing collected data

Instruments User Guide编程指南3-Navigate Collected DataAbout Data AnalysisNavigate  the Timeline Pane Navigate the Detail Pane  操作详情面板Map Data to Source Code 映射数据到源码See Your App's Source Code 查看对应APP源码

建议:许多的instrument,当它们监控APP的时候,会周期性的捕捉堆栈踪迹。当我们确定了具体的数据点想要进一步研究,我们可以浏览堆栈踪迹来确定对应的代码部分。如果我们正在对Xcode打开的APP进行性能分析,我们经常可以在Instrument的右边查看源码或者调到Xcode中进一步检查。

Navigate  the Timeline Pane 

跟踪文件(trace document)中最突出的部分就是时间轴面板(timeline pane),它显示每个instrument基于时间收集的可视数据(Figure 11-1)。该面板非常容易识别APP中动态趋势和潜在的问题。例如:在内存使用图表中像钉子一样突出的部分表明了在该一段时间内你的APP比正常情况下分配了更多的内存。突出的部分可能正常,或者也表明了我们的代码创建了更多的对象,或者内存缓冲区超过了我们的预期。对于工具(Instrument)像Leaks能确定我们APP中哪个地方并没有合理的处理内存。如果Leaks的时间轴面板是稀疏构成,我们就明白我们APP的行为是合理的。相反,如果track不为空,而是变化很大,我们可能想要查找原因了。

Figure 11-1The timeline pane of a trace document

Instruments User Guide编程指南3-Navigate Collected DataAbout Data AnalysisNavigate  the Timeline Pane Navigate the Detail Pane  操作详情面板Map Data to Source Code 映射数据到源码See Your App's Source Code 查看对应APP源码

Zoom In and Out

如果我们已经使用trace获取了数据,我们能够在时间轴面板放大或缩小被显示的详情,允许我们集中于具体部分更详情的内容(Figure 11-2)

Figure 11-2Zooming into the timeline pane of a trace document

Instruments User Guide编程指南3-Navigate Collected DataAbout Data AnalysisNavigate  the Timeline Pane Navigate the Detail Pane  操作详情面板Map Data to Source Code 映射数据到源码See Your App's Source Code 查看对应APP源码

To zoom in and out of your data 为了放大和缩小数据,可以使用以下方法:

1:To zoom in or out,pinch the trackpad.  为了放大和缩小,触摸触控板,使用2根指头至于触摸板,2指向内缩小,相反放大

2:To zoom in or out, use the scroll wheel while pressing the Option key with the pointer positioned over the track view 为了放大和缩小,按住Option建通过滚动滚轮与指针定位跟踪视图。即按住Option,使用2指在触摸板上像左滑动缩小,向右滑动放大

3:To zoom in, press the Option key, and drag across the section of data you wish to isolate. 为了放大,按住Option键,并拖动想要放大的区域

4:To zoom out, press the Control key, and drag across a section of data  为了缩小,按住Control键,并拖动想要缩小的区域

To fit all recorded data in the timeline   在时间面板上显示所有的记录数据

1:Choose View > Snap Track To Fit (or press Control-Command-Z).

Instruments User Guide编程指南3-Navigate Collected DataAbout Data AnalysisNavigate  the Timeline Pane Navigate the Detail Pane  操作详情面板Map Data to Source Code 映射数据到源码See Your App's Source Code 查看对应APP源码

To increase the size of the data displayed in the timeline 增加在时间面板上显示数据的大小

1:Choose View > Increase Deck Size (or press Command-Plus Sign)

Instruments User Guide编程指南3-Navigate Collected DataAbout Data AnalysisNavigate  the Timeline Pane Navigate the Detail Pane  操作详情面板Map Data to Source Code 映射数据到源码See Your App's Source Code 查看对应APP源码

跟踪当前选中的instrument,增加显示view的高度:

Instruments User Guide编程指南3-Navigate Collected DataAbout Data AnalysisNavigate  the Timeline Pane Navigate the Detail Pane  操作详情面板Map Data to Source Code 映射数据到源码See Your App's Source Code 查看对应APP源码

To decrease the size of the data displayed in the timeline 

1:Choose View > Decrease Deck Size (or press Command-Minus Sign).

Instruments User Guide编程指南3-Navigate Collected DataAbout Data AnalysisNavigate  the Timeline Pane Navigate the Detail Pane  操作详情面板Map Data to Source Code 映射数据到源码See Your App's Source Code 查看对应APP源码

Filter a Range of Data

当进行性能分析,instrument收集了大量的信息,我们可以进行设置详情面板快速过滤数据,只显示发生在具体时间段内的数据。如:Figure 11-3

Figure 11-3Selecting a range of data in the timeline pane of a trace document

Instruments User Guide编程指南3-Navigate Collected DataAbout Data AnalysisNavigate  the Timeline Pane Navigate the Detail Pane  操作详情面板Map Data to Source Code 映射数据到源码See Your App's Source Code 查看对应APP源码

To select a time range for inspection

1:Drag across a section of data without using any modifier keys. Only the data you drag across is displayed. 拖过一段数据而不使用任何修改键,只有你拖动的数据显示。

Instruments User Guide编程指南3-Navigate Collected DataAbout Data AnalysisNavigate  the Timeline Pane Navigate the Detail Pane  操作详情面板Map Data to Source Code 映射数据到源码See Your App's Source Code 查看对应APP源码

instrument高亮了时间面板上的内容,那部分就是我们所指定的范围

To clear an inspection range

1:Click outside the selected range in the timeline pane 点击事件面板之外的区域

2:Choose View > Clear Inspection Range

Instruments User Guide编程指南3-Navigate Collected DataAbout Data AnalysisNavigate  the Timeline Pane Navigate the Detail Pane  操作详情面板Map Data to Source Code 映射数据到源码See Your App's Source Code 查看对应APP源码

Set Flags

旗帜标志允许我们迅速范围时间面板上的兴趣点(Figure 11-4),我们能够为每一个flag添加名字和描述。

Figure 11-4Flags in the timeline pane of a trace document

Instruments User Guide编程指南3-Navigate Collected DataAbout Data AnalysisNavigate  the Timeline Pane Navigate the Detail Pane  操作详情面板Map Data to Source Code 映射数据到源码See Your App's Source Code 查看对应APP源码

To set a flag at the current playhead position in the timeline pane  在当前时间面板播放头位置设置旗帜

1:Choose Edit > Add Flag (or press Command-Down Arrow).

Instruments User Guide编程指南3-Navigate Collected DataAbout Data AnalysisNavigate  the Timeline Pane Navigate the Detail Pane  操作详情面板Map Data to Source Code 映射数据到源码See Your App's Source Code 查看对应APP源码

To navigate between your timeline flags  操作时间面板中的旗帜

1:Choose Window > Manage Flags (or press Shift-Command-T) to display the Flags palette.

Instruments User Guide编程指南3-Navigate Collected DataAbout Data AnalysisNavigate  the Timeline Pane Navigate the Detail Pane  操作详情面板Map Data to Source Code 映射数据到源码See Your App's Source Code 查看对应APP源码

2:Click a flag in the Flags palette to navigate to it in the timeline pane。点击弹出款中可以看到时间面板上所有的旗帜并可以进行相关操作

To delete a flag 删除flag

1:Drag the flag out of the timeline navigation bar

To hide a flag

1:Choose Window > Manage Flags (or press Shift-Command-T) to display the Flags palette

Instruments User Guide编程指南3-Navigate Collected DataAbout Data AnalysisNavigate  the Timeline Pane Navigate the Detail Pane  操作详情面板Map Data to Source Code 映射数据到源码See Your App's Source Code 查看对应APP源码

2:Deselect the checkbox next to a flag in the Flags palette to hide it in the timeline pane. 反选就可以隐藏flag

Instruments User Guide编程指南3-Navigate Collected DataAbout Data AnalysisNavigate  the Timeline Pane Navigate the Detail Pane  操作详情面板Map Data to Source Code 映射数据到源码See Your App's Source Code 查看对应APP源码

Access Trace Data for Multiple Runs

trace document在APP运行时能够记录多部分运行数据,如Figure 11-5.通过这样做,那么就不会抛弃之前的记录数据。

Figure 11-5Viewing multiple runs in the timeline pane of a trace document

Instruments User Guide编程指南3-Navigate Collected DataAbout Data AnalysisNavigate  the Timeline Pane Navigate the Detail Pane  操作详情面板Map Data to Source Code 映射数据到源码See Your App's Source Code 查看对应APP源码

To view a list of runs for a selected instrument

1:Click the disclosure triangle to the left of the instrument in the strategy pane. 点击左侧的三角符号

Instruments User Guide编程指南3-Navigate Collected DataAbout Data AnalysisNavigate  the Timeline Pane Navigate the Detail Pane  操作详情面板Map Data to Source Code 映射数据到源码See Your App's Source Code 查看对应APP源码

To view the run data for a selected instrument

1:Click the disclosure triangle to the left of the instrument in the strategy pane 点击左侧三角符号

2:Select the desired run’s track in the timeline pane  在时间轴面板上选择所需的运行轨道

To move to the next run for the selected instrument

1:Choose Instrument > Next Run, or press Command-Single Quotation Mark ('). 

Instruments User Guide编程指南3-Navigate Collected DataAbout Data AnalysisNavigate  the Timeline Pane Navigate the Detail Pane  操作详情面板Map Data to Source Code 映射数据到源码See Your App's Source Code 查看对应APP源码

To move to the previous run for the selected instrument

1:Choose Instrument > Previous Run, or press Command-Quotation Mark (").

Instruments User Guide编程指南3-Navigate Collected DataAbout Data AnalysisNavigate  the Timeline Pane Navigate the Detail Pane  操作详情面板Map Data to Source Code 映射数据到源码See Your App's Source Code 查看对应APP源码

Navigate the Detail Pane  操作详情面板

在我们确认了时间轴面板上潜在的问题区域之后,使用详情面板来检测数据,如Figure 12-1所示。详情面板显示了与当前选中instrument的trace相关联的数据。instruments每次只能够显示一个在详情面板,所以我们需要自己切换不同的工具(instrument)来看不同的详情内容。

Figure 12-1Viewing detail data for an instrument

Instruments User Guide编程指南3-Navigate Collected DataAbout Data AnalysisNavigate  the Timeline Pane Navigate the Detail Pane  操作详情面板Map Data to Source Code 映射数据到源码See Your App's Source Code 查看对应APP源码

To open or close the detail pane

1:Choose View > Detail

Instruments User Guide编程指南3-Navigate Collected DataAbout Data AnalysisNavigate  the Timeline Pane Navigate the Detail Pane  操作详情面板Map Data to Source Code 映射数据到源码See Your App's Source Code 查看对应APP源码

2:Click the Detail View button (

Instruments User Guide编程指南3-Navigate Collected DataAbout Data AnalysisNavigate  the Timeline Pane Navigate the Detail Pane  操作详情面板Map Data to Source Code 映射数据到源码See Your App's Source Code 查看对应APP源码

) at the right end of the toolbar 点击导航条右侧底部的详情视图按钮

To view data in the detail pane for a specific instrument

1:Click the instrument in the strategy pane.

Instruments User Guide编程指南3-Navigate Collected DataAbout Data AnalysisNavigate  the Timeline Pane Navigate the Detail Pane  操作详情面板Map Data to Source Code 映射数据到源码See Your App's Source Code 查看对应APP源码

View Different Types of Data in the Detail Pane 在详情模板看不同类型数据

对于有些instrument,我们能够使用不止一种的格式进行显示。例如: Activity Monitor instrument 允许我们看数据的概要,父子视图信息,和取样列表。

To switch between detail types

1:Choose the appropriate mode from the detail type list in the navigation bar of the detail pane. 选择合适的模式

Instruments User Guide编程指南3-Navigate Collected DataAbout Data AnalysisNavigate  the Timeline Pane Navigate the Detail Pane  操作详情面板Map Data to Source Code 映射数据到源码See Your App's Source Code 查看对应APP源码

     instrument所支持的每一个模式取决于使用instrument收集数据的类型。

对于有些详情类型,像Allocations instrument 调用树模式(call tree mode),我们打开三角符号(Figure 12-2)在确定的详情部分进一步显示深层次的内容。点击对应的三角符号可以打开和关闭所给定的部分。

Figure 12-2Navigating profiling data in the detail pane

Instruments User Guide编程指南3-Navigate Collected DataAbout Data AnalysisNavigate  the Timeline Pane Navigate the Detail Pane  操作详情面板Map Data to Source Code 映射数据到源码See Your App's Source Code 查看对应APP源码

建议:为了展开行和它的所有子内容部分,当点击三角符号时按住Option建即可,一般的点击只会展开下级内容。当看一个call tree,许多的instrument包括了隐藏系统调用(hide system call)和反转系统调用树(invert the call tree).这些选项可以在监控面板(inspector pane)中的显示选项(Display Options)中的call tree 区域发现.隐藏系统调用允许我们迅速过滤APP的调用,反转调用树允许我们首先看最严重的调用。

Sort in the Detail Pane 

为了对在详情面板中显示的信息进行分类,可以根据数据所在的具体列,点击合理的列头(Figure 12-3)。每一个instrument所对应的详情面板中的列都不一样。

Figure 12-3Sorting profiling data in the detail pane

Instruments User Guide编程指南3-Navigate Collected DataAbout Data AnalysisNavigate  the Timeline Pane Navigate the Detail Pane  操作详情面板Map Data to Source Code 映射数据到源码See Your App's Source Code 查看对应APP源码

Use the Extended Detail Pane

对于有些instrument,在监控面板中的扩展详情区域显示了当前选中item的额外信息,扩展详情信息能够包括调查描述、事件的记录、堆栈轨迹、信息记录的相关时间学习等。

To open or close the extended detail area of the inspector

1:Choose View > Inspectors > Show Extended Detail or Hide Extended Detail (or press Command-3).

Instruments User Guide编程指南3-Navigate Collected DataAbout Data AnalysisNavigate  the Timeline Pane Navigate the Detail Pane  操作详情面板Map Data to Source Code 映射数据到源码See Your App's Source Code 查看对应APP源码

  Or:Click the Extended Detail button (

Instruments User Guide编程指南3-Navigate Collected DataAbout Data AnalysisNavigate  the Timeline Pane Navigate the Detail Pane  操作详情面板Map Data to Source Code 映射数据到源码See Your App's Source Code 查看对应APP源码

) in the navigation bar at the top of the inspector pane.

Instruments User Guide编程指南3-Navigate Collected DataAbout Data AnalysisNavigate  the Timeline Pane Navigate the Detail Pane  操作详情面板Map Data to Source Code 映射数据到源码See Your App's Source Code 查看对应APP源码

在堆栈轨迹中我们可以隐藏系统调用通过点击按钮

Instruments User Guide编程指南3-Navigate Collected DataAbout Data AnalysisNavigate  the Timeline Pane Navigate the Detail Pane  操作详情面板Map Data to Source Code 映射数据到源码See Your App's Source Code 查看对应APP源码

 在扩展详情区域中堆栈轨迹的顶部:

Figure 12-4Hiding system calls in the extended detail area of the inspector pane

Instruments User Guide编程指南3-Navigate Collected DataAbout Data AnalysisNavigate  the Timeline Pane Navigate the Detail Pane  操作详情面板Map Data to Source Code 映射数据到源码See Your App's Source Code 查看对应APP源码

Map Data to Source Code 映射数据到源码

Instrument需要工程(project)的精准信息来提供最好的结果。如果系统能够看到所有与工程相关的symbols,我们将获取最完整的信息。当在跟踪文件(trace document)中通过instrument工具生成的是地址(addresses)而不是symbol,我们能够手动提供缺少的信息。映射地址到symbol是被包含在dSYM文件中。很显然,instrument自动找到dSYM文件,基于我们在工具参数( Instruments preferences)中设置的Spotlight indexes 和路径(path).当instrument不能够自动找到dSYM文件,我们可以自己手动指定instrument正确的方向。一旦我们这样做,instrument将能够映射地址到他们相关联的对象和具体的行信息。

To locate the dSYM path for an executable or framework

1:Choose File > Symbols.     注意:该命令只在我们运行了trace或者加载了之前保存过的trace 才会出现。

Instruments User Guide编程指南3-Navigate Collected DataAbout Data AnalysisNavigate  the Timeline Pane Navigate the Detail Pane  操作详情面板Map Data to Source Code 映射数据到源码See Your App's Source Code 查看对应APP源码

2:Select the executable (binary) or framework that is missing symbols.

Instruments User Guide编程指南3-Navigate Collected DataAbout Data AnalysisNavigate  the Timeline Pane Navigate the Detail Pane  操作详情面板Map Data to Source Code 映射数据到源码See Your App's Source Code 查看对应APP源码

3:Click the Locate button.

Instruments User Guide编程指南3-Navigate Collected DataAbout Data AnalysisNavigate  the Timeline Pane Navigate the Detail Pane  操作详情面板Map Data to Source Code 映射数据到源码See Your App's Source Code 查看对应APP源码

4:In the dialog that appears, select your symbol file or the folder that contains it.

5:Click Open.

为了正确的在trace document中显示symbol,instrument需要访问具体的symbol 文件,该文件是当我们进行测试所生成的。因此,当使用一个电脑创建需要在其他电脑上进行性能测试时我们需要手动定位dSYM文件。

在Symbol对话框中的过滤条中能够让我们对可执行(executable)和不完整的二级制(incomplete binaries)、框架(framework)进行相应的过滤.我们也可以通过输入文本内容来搜索具体的 binary、framework。

笔记:默契情况下,Xcode在Build folder中保存symbols到dSYM bundles。Xcode通过添加扩展名.dSYM到可执行名(executable name)创建默认的 dSYM bundle path.为了在Xcode生成dSYM bundles,进入工程中的Build Settings区域,在Build Options部分选择调试(Debug)信息部分Format > Dwarf with dSYM File.

注意:当显示地址(address)而不是symbol,地址显示在左边空白部分并且用于的库显示在详情面板的右侧。玩能够通过确认这些地址来定位合理的dSYM文件,如果地址显示的灰色并且没有自己用于的库,我们不能够显示symbolic name 。灰色的地址并没有库名意味着那时候trace被记录了,我们被没有被许可查看相关的过程。很显然发生在build setting中Code Signing Identity设置为release/distribution标识。发布版本(Release build)应该跟调试版本(Debug build)设置相同(如:“iOS Developer”)。

See Your App's Source Code 查看对应APP源码

如果有一个Xcode project有源码对应堆栈轨迹中的symbol list,instrument能够显示源码甚至能够打开Xcode工程让我们做一想要的操作。使用堆栈轨迹符号(stack trace symbols )来定位我们所写的源码。用户源码堆栈轨迹符号显示在详情面板,是一个小的正方形用户icon有着黑色的背景。为了更容易为源码找到symbol,我们能够使用在监控区域(inspector pane)的显示设置区域(display setting area)来过滤隐藏symbol和系统库。

To see your app’s source code

1:Find an icon for your code (denoted by a user (

Instruments User Guide编程指南3-Navigate Collected DataAbout Data AnalysisNavigate  the Timeline Pane Navigate the Detail Pane  操作详情面板Map Data to Source Code 映射数据到源码See Your App's Source Code 查看对应APP源码

) symbol) in either the detail pane or extended detail area of the inspector pane. 在详情面板或监控面板中的者扩展详情区域找到symbol图标

Instruments User Guide编程指南3-Navigate Collected DataAbout Data AnalysisNavigate  the Timeline Pane Navigate the Detail Pane  操作详情面板Map Data to Source Code 映射数据到源码See Your App's Source Code 查看对应APP源码

2:Double-click the icon to display your source code where that symbol is used. 双击icon所对应的symbol将会显示对应的源码,即Symbol  Name下对应的内容

Instruments User Guide编程指南3-Navigate Collected DataAbout Data AnalysisNavigate  the Timeline Pane Navigate the Detail Pane  操作详情面板Map Data to Source Code 映射数据到源码See Your App's Source Code 查看对应APP源码

       3:Click the Xcode button (image: ../Art/inline_xcode_2x.png) at the top right of the detail pane to open Xcode and display your source code. The following screenshot shows the location of the Xcode button that appears at the top of the detail pane. It appears there after youdouble-click a symbol or iconin the detail pane or the extended detail area of the inspector panein order to display codein the detail pane.   在双击了symbol之后,我们可以看到源码页面右侧有如下的图标部分,点击在详情面板中顶部的Xcode按钮打开Xcode并显示源码。

Instruments User Guide编程指南3-Navigate Collected DataAbout Data AnalysisNavigate  the Timeline Pane Navigate the Detail Pane  操作详情面板Map Data to Source Code 映射数据到源码See Your App's Source Code 查看对应APP源码