DIY Profiler 1

关于Unity性能监控工具的开发练习1.

Posted by 秦浩凯(Haokai Qin) on 2023-01-28
Estimated Reading Time 4 Minutes
Words 983 In Total
Viewed Times

前言

性能监控工具一直是软件开发中的重要组成。然而,应该如何开发这种程序?如何为项目需求定制性能监控?

本文是实习工作内容,结合GAMES 104课程的实践。
VISUAL PROFILER也用于AR开发。

基础款:Visual Profiler解析

这是一款非常简单的性能监视工具,用于获取帧率和程序总体内存占用。

runtime可用。

可以通过这一工具熟悉相关API。

PF

帧率

帧率的统计

我们用帧率来监测卡顿情况。
在Visual Profiler中,程序使用FrameTimingManager来获取帧率相关的信息。

帧数通过计数器frameCount统计;
运行时间elapsedSeconds通过System.Diagnostics.stopwatch相关来获取(而非Time);
通过这两个参数计算出 FrameRate = 1.0f / (elapsedSecond / frameCount)

该程序的工作流可参考

https://imgtec.eetrend.com/blog/2022/100563328.html

扩展:帧率的获取

ILSpy中不能获取该类的实现。

注意到对于NVIDIA系,可以采用

https://www.zhihu.com/question/29680314

提到的 nvmlReturn_t DECLDIR nvmlDeviceGetUtilizationRates (nvmlDevice_t device, vmlUtilization_t *utilization)

获取。

帧率显示控制

我们用一组方块(进度条)反映帧率,不同范围的帧率映射为不同颜色。每帧使进度条向前移动一格.

使用 MaterialPropertyBlock 进行模型批处理以减小修改开销。

https://blog.csdn.net/Ling_SevoL_Y/article/details/122999996

使用Graphics.DrawMesh执行绘制。

其他

https://blog.unity.com/cn/technology/detecting-performance-bottlenecks-with-unity-frame-timing-manager

内存占用

对于UWP平台,可以使用
MemoryManager类获取当前程序的占用。

其他平台可以使用Profiler.GetTotalAllocatedMemoryLong来获取。

其他

https://www.656463.com/wenda/VirtualMemorySize64hPrivateMemor_404
https://blog.csdn.net/qq_42679415/article/details/126473614

猜想与扩展:如何获取不同资源的内存占用?

  1. 收集场景内使用的资源列表,根据已知信息计算。

这个想法在

https://stackoverflow.com/questions/56822948/estimate-an-assetbundle-size-in-ram

中也被提到。

  1. 一个API: ProfilerRecorder

    https://thegamedev.guru/unity-memory/profiler-module-metrics-programatically/

个人思路就是 Resource API获取列表再计算。

Append1: 内存占用获取

MemoryProfiler API

可以用来获取某一类资源总体的占用(当前所有纹理的内存、所有网格、GC回收等)

参考
https://thegamedev.guru/unity-memory/profiler-module-metrics-programatically/

文档
https://docs.unity3d.com/2020.2/Documentation/Manual/ProfilerMemory.html

获取指定类型的obj

Resource.FindObjectsOfTypeAll();

另见

https://docs.unity3d.com/ScriptReference/ResourcesAPI.html
https://github.com/handcircus/Unity-Resource-Checker/blob/master/ResourceChecker.cs

Texture系列( LTS 2019.4 or later)

Texture.targetTextureMemory
The total amount of Texture memory that Unity allocates to the Textures in the scene after it applies the memory budget and fin…

Texture.desiredTextureMemory
The total size of the Textures, in bytes, that Unity loads if there were no other constraints. Before Unity loads any Textures,…

Texture.currentTextureMemory
The amount of memory that all Textures in the scene use.

currentTextureMemory
The amount of memory that all Textures in the scene use.

desiredTextureMemory
The total size of the Textures, in bytes, that Unity loads if there were no other constraints. Before Unity loads any Textures, it applies the memory budget which reduces the loaded Texture resolution if the Texture sizes exceed its value. The desiredTextureMemory value takes into account the mipmap levels that Unity has requested or that you have set manually. For example, if Unity does not load a Texture at full resolution because it is far away or its requested mipmap level is greater than 0, Unity reduces the desiredTextureMemory value to match the total memory needed. The desiredTextureMemory value can be greater than the targetTextureMemory value.

targetTextureMemory
The total amount of Texture memory that Unity allocates to the Textures in the scene after it applies the memory budget and finishes loading Textures. targetTextureMemoryalso takes mipmap streaming settings into account. This value only includes instances of Texture2D and CubeMap Textures. This value does not include any other Texture types, or 2D and CubeMap Textures that Unity creates internally.

totalTextureMemory
The total amount of Texture memory that Unity would use if it loads all Textures at mipmap level 0. This is a theoretical value that does not take into account any input from the streaming system or any other input, for example when you set theTexture2D.requestedMipmapLevel manually. To see a Texture memory value that takes inputs into account, use desiredTextureMemory. totalTextureMemory only includes instances of Texture2D and CubeMap Textures. This value does not include any other Texture types, or 2D and CubeMap Textures that Unity creates internally.

DC

UnityEditor.UnityStats.batches

一些参数

https://blog.csdn.net/SHIYUEDYX/article/details/80928332

学习:ParticleEffectProfiler

https://github.com/sunbrando/ParticleEffectProfiler

除了熟悉相关API外,也可作为ANIMATONCURVE教程使用。

在UNITY INSPECTOR面板和SCENE(使用gizmos)生效。

PARTICLEEFFECTSCRIPT


If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !