Unity 之 Profiler 模块

CPU Usage Profiler module

CPU Usage Profiler 模块展示在应用程序中花费的时间,比如 renderingscriptsanimation.

Chart categories

CPU Usage Profiler模块的chart跟踪应用程序(main thread)主线程上花费的时间。一共有九种,你可以上下拖动他们改变他们的顺序,也可以点击关闭它

CategoryDescription
Rendering您的应用程序在呈现图形上花费的时间。
Scripts您的应用程序在运行脚本上花费的时间
Physics在physics engine花费的时间
.
Animation在animating SkinnedMeshRenderers, GameObjects
and other components in your application花费的时间,这其中也包括计算  Animation and Animator components使用过程中的时间
utilize.
GarbageCollector在arbage Collector上花费的时间
VSyncHow much time is spent in a frame waiting for the targetFrameRate or the next VBlank to sync with. This is according to the QualitySettings.vSyncCount value, or the target framerate, or the VSync setting that is the default or enforced maximum of the platform your application is running on. For more information about VSync, see the section in this documentation on Rendering and VSync samples.
Global Illumination在应用程序的照明上花费了多少时间。.
UI UI.显示应用程序花费了多少时间
Others在不属于任何其他类别的代码中花费了多少时间,例如整个EditorLoop,或者在编辑器中分析Playmode时的分析开销

Module details pane

选中某一帧后会呈现应有的细节,这些细节有三种呈现方式,默认是Timeline

ViewFunction
TimelineDisplays a breakdown of the timings for a particular frame alongside a time axis of the frame’s length. 这是您可以用来查看除主线程之外的其他线程上的计时以及跨线程关联计时的唯一视图模式
Hierarchy按层级分组,
Raw HierarchyDisplays the timing data in a hierarchical structure that is similar to the call stacks where the timing occurred. Unity lists each call stack separately in this mode instead of merging them, as it does in Hierarchy view.

Timeline view

分析所有线程上的数据, Hierarchy views,只分析 main thread.的数据

你可以用该视图查看不同线程之间的执行数据.你可以看到你使用了多少不同的线程, 例如作业系统的工作线程,线程上的工作如何排队,以及是否有线程正在空闲(“Idle”示例)或正在等待另一个线程或作业完成(“Wait for x”示例)。

 

Navigating and selecting items

放大时间轴:滚轮放大或者按住alt键的同时滑动鼠标右键T.  A 键重置缩放比例,鼠标中键来移动视图

 

Hierarchy and Raw Hierarchy view

当您切换到Hierarchy或Raw Hierarchy视图时,会显示主线程的数据,按F键聚焦。

Hierarchy视图列出了所有已分析的样本,并根据它们的共享调用堆栈和profilermarker的层次结构将它们分组在一起. Raw Hierarchy 视图不会将样本分组在一起,这使得它非常适合在粒度级别上查看样本:

PropertyFunction
Total在一个特定函数上花费的总时间百分比。
Self在一个特定函数上花费的总时间百分比,不包括Unity调用子函数的时间。

例如,在截图中,41.7%的时间花在了相机渲染功能上。这是因为它调用了大量的绘图和筛选函数,但是当您排除它调用的函数时,只有3.5%的时间花在相机上。渲染函数本身。
Calls在这一帧中对这个函数的调用次数,在Raw Hierarchy视图中,这一列的值总是1,因为profiler不会合并样本的层次结构。
GC Alloc

当前帧中分配了多少脚本堆内存?脚本堆内存由垃圾收集器管理(garbage collector.)

无论何时调用GC.Collect(),或者存在不适合堆当前大小的脚本堆分配,垃圾收集器都会被触发. It marks all allocations that have no more references to them and collects them. This process shows up as GC.Collect samples in the Profiler.

 

当您在堆上分配更多内存时,Unity会更频繁地运行垃圾收集器

随着托管堆的增长,Unity需要更长的时间来标记和收集内存。因此,在应用程序运行时,应该将GC Alloc值保持为0,以防止垃圾收集器影响您的帧速率,并保持总体堆大小较小

For more details about the managed heap see the Understanding Automatic Memory Management documentation.
Time ms在一个特定函数上花费的总时间(以毫秒为单位)。这个信息可能会产生误导,因为它只包含在主线程上花费的时间。如果您的应用程序使用作业系统或多线程呈现,您应该注意这一点。
Self ms在一个特定函数上花费的总时间(以毫秒为单位),不包括Unity调用子函数的时间。
Warning由警告图标指示,它显示在当前帧中触发警告的次数

 你也可以通过右上角的下拉菜单来看具体的信息:

 Show Related Objects 视图显示UnityEngine与profiler样本关联的对象,使用UnityEngine.Object的Begin()重载获取对象物体. 一些Unity报告的样本已经内置了这些关联, 就像 Camera.Render 关联的是Camera 物体g. 这些对象通过它们的instance ID进行报告,并在Profiler窗口中解析为一个名称。

当你点击这些对象之一,Unity试图在场景中找到该对象.

 在GC.Alloc 样例中 ,此视图显示一个“N/A”项列表,每个项对应于此层次结构级别上的每个分配,GCAlloc 中列出了分配的大小。 当你在编辑器中打开Allocation Callstacks设置时, 当你选择一个 GC.Alloc 样本时,将会显示您选择的已分配脚本对象的调用堆栈

 Show Calls 面板显示所选的样例从何处被调用,以及它调用的其他函数

Common samples

除了脚本代码生成的样例之外,Unity还提供了大量的样例,这些样例可以让您了解应用程序中哪些部分占用了时间.

主线程样本:

主线程基本示例清楚地区分了花在应用程序上的时间和花在Editor和Profiler上的时间,The Recorder can also use these samples to get the timing of a frame on the main thread.

SampleFunction
PlayerLoop 在主线程下生成的任何样例的父物体,当你打开 Profile Editor ,并运行unity时,它嵌套在,EditorLoop下.
EditorLoop在Editor下生成的任何样例的父物体. 这仅仅在编辑器下分析数据的时候出现. 当您禁用Profile Editor设置时,此示例将显示渲染rendering和运行包含播放器的编辑器所花费的帧时间。
Profiler.CollectEditorStats与为不同的Profiler模块收集统计信息相关的任何示例的根,在Profiler.CollectGlobalStats 下的子样本会增加unity的开销,其他的子样本仅仅影响Editor. 可以用Profiler.SetAreaEnabled().关闭特定模块

Script 中的样例

Unless you are using the Job System, most of your scripting code is nested underneath the following samples:

SampleFunction
Update.ScriptRunBehaviourUpdate这个示例包括对MonoBehaviour.Update的调用和对协同程序的更新和处理。.
BehaviourUpdate所有Update()方法.
CoroutinesDelayedCalls包含所有第一个yieldh后的coroutine 协程样本
PreLateUpdate.ScriptRunBehaviourLateUpdate所有LateUpdate() 方法
FixedBehaviourUpdate所有FixedUpdate() 方法.

Rendering and VSync samples

这些示例显示CPU在哪些地方为GPU处理数据花费时间,或者在哪些地方等待GPU完成.如果GPU Profiler不可用或它增加了太多的开销, 工具栏不显示此信息。这些示例可以让您了解自己是cpu绑定还是gpu绑定。

SampleFunction
WaitForTargetFPS等待目标fps的时间,比如一秒30帧,一帧结束后等待下一帧的时间,帧率由 Application.targetFrameRate 确定

如果这个样本是Gfx.WaitForPresent的子样本。它表示应用程序等待QualitySettings.vSyncCount中配置的VSync所花费的时间。

Note: The Editor doesn’t VSync on the GPU and instead uses WaitForTargetFPS to simulate the delay for VSync. Some platforms, in particular Android and iOS, enforce VSync or have a default frame rate cap of 30 or 60.
Gfx.ProcessCommands包含 render thread上所有的 rendering commands . 其中一些时间可能会花在等待来自主线程的VSync或新命令上,您可以从它的子示例Gfx.WaitForPresent中看到这一点。
Gfx.WaitForCommands表示呈现线程已经为新的命令做好了准备,并且可能表示主线程上存在瓶颈。Indicates that the render thread is ready for new commands and might indicate a bottle neck on the main thread.
Gfx.PresentFrame表示应用程序等待GPU渲染和呈现帧的时间,这可能包括等待VSync

线程上的WaitForTargetFPS示例显示了等待VSync花费了多少时间。
Gfx.WaitForPresent表示主线程已经准备好开始渲染下一帧,但是渲染线程还没有完成对GPU渲染帧的等待。这可能表明您的应用程序是受gpu限制的。要查看呈现线程同时在什么地方花费时间,请检查时间轴视图。

如果渲染线程在Camera.Render中花费时间,你的应用程序是cpu绑定的,可能会花费太多时间向GPU发送绘制调用或纹理

如果渲染线程花费时间在 Gfx.PresentFrame上, 你的游戏是GPU绑定的,或者它可能在GPU上等待,一个GFX.WaitForPresent的子样本WaitForTargetFPS表示应用程序在当前阶段等待VSync所花费的时间

Physics samples

物理样本:

下表概述了一些高级物理分析器示例。FixedUpdate调用所有这些示例。

SampleFunction
Physics.Simulate 通过调用physics engine (PhysX)更新当前的状态从而运行它的模拟
Physics.Processing运行所有non-cloth physics线程. 展开此示例,以显示物理引擎内部完成的底层工作细节。
Physics.ProcessingCloth运行所有cloth physics jobs. Expand this sample to show the low-level detail of the work done internally in the physics engine.
Physics.FetchResults收集所有的物理引擎模拟的结果
Physics.UpdateBodies更新所有物理实体的位置和旋转. 此示例还包含在发送这些更新时进行通信的消息。
Physics.ProcessReports在FixedUpdate 运行结束后执行. 处理各个阶段对仿真结果的响应. Contacts, joint
breaks and triggers update and message in this sample.一共有四个阶段
 Physics.TriggerEnterExits处理OnTriggerEnter and OnTriggerExit events.
 Physics.TriggerStays处理OnTriggerStay events.
 Physics.Contacts处理nCollisionEnter, OnCollisionExit, and OnCollisionStay events.
 Physics.JointBreaks处理更新和关于断裂接头的信息
Physics.UpdateCloth包含有关 cloth 和 skinned meshes的更新.
Physics.InterpolationManages the interpolation of positions and rotations for all the physics objects.

Performance warnings

CPU分析器可以检测一些常见的性能问题,并发出警告

A Profiler warning indicating that Static Colliders have been moved

The specific issues the Profiler can detect are:

  • Rigidbody
    .SetKinematic:
    recreate non-convex MeshCollider for Rigidbody
  • Animation.DestroyAnimationClip: triggers RebuildInternalState
  • Animation.AddClip: triggers RebuildInternalState
  • Animation.RemoveClip: triggers RebuildInternalState
  • Animation.Clone: triggers RebuildInternalState
  • Animation.Deactivate: triggers RebuildInternalState

Allocation Callstacks

如果Editor下分析, 你可以看到完整的GC.Alloc的调用堆栈.打开 Managed Allocations .GC.Alloc 样本中会包含他们的调用堆栈

每个脚本堆分配都显示为一个GC。层次结构视图和时间轴视图中的Alloc示例。在时间轴视图中,它的颜色为明亮的洋红色. 要查看调用堆栈,请选择CPU Profiler并选择GC.Alloc

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TO_ZRG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值