Microprofile
UNIGINE has support for Microprofile, an advanced external embeddable CPU/GPU profiler with support for per-frame inspection. UNIGINE支持Microprofile,这是一种先进的外部可嵌入CPU / GPU分析器,支持逐帧检查。
The profiler features the following:探查器具有以下特点:
- Profiling operations performed by the engine on CPU and GPU引擎在CPU和GPU上执行的分析操作
- Profiling the engine threads分析引擎线程
- Profiling up to 1000 frames最多分析1000帧
- The performance data output to a local web server or to an HTML file性能数据输出到本地Web服务器或HTML文件
Running Microprofile运行Microprofile#
The Microprofile tool is available only for the Development builds of UNIGINE Engine: it won't be compiled for the Debug and Release ones. You can use the microprofile_info console command to check if the Microprofile is compiled. Microprofile工具仅适用于UNIGINE引擎的Development版本:不会针对Debug和Release版本进行编译。您可以使用 microprofile_info 控制台命令来检查Microprofile是否已编译。
The performance data obtained by the Microprofile can be output to a local web server or to an HTML file.Microprofile可以输出到本地Web服务器或HTML文件。
Enabling and Disabling Microprofile启用和禁用Microprofile#
Microprofile is performance-consuming, that's why we recommend you to enable it only when you work with it and keep disabled otherwise.Microprofile消耗性能,这就是为什么我们建议您仅在使用时启用它,否则保持禁用。
One way to disable Microprofile is to untick the corresponding option before running UnigineEditor or the application:禁用Microprofile的一种方法是在运行UnigineEditor或应用程序之前取消选中相应的选项:
Another way is to use the microprofile_enabled console command.另一种方法是使用microprofile_enabled控制台命令。
Saving Performance Data to File将性能数据保存到文件#
Microprofile allows saving the performance data to an HTML file and then analyzing it offline.Microprofile允许将性能数据保存到HTML文件,然后离线分析。
To output the performance data to the HTML file, perform the following:要将性能数据输出到HTML文件,请执行以下操作:
- In the console, set the the number of frames to be profiled via the microprofile_dump_frames console command. You can skip this step: by default, 500 frames will be profiled.在控制台中,通过microprofile_dump_frames控制台命令设置要分析的帧数。 您可以跳过此步骤:默认情况下,将对500帧进行分析。
- Run the microprofile_dump_html console command.运行microprofile_dump_html控制台命令。
The performance data will be saved to the specified HTML file.性能数据将保存到指定的HTML文件中。
Using profiling dumps simplifies capturing performance "spikes": the engine continues rendering the application in the background even when it is out of focus, so the frame where the spike has occurred can be overwritten in case of using the Web server for performance data visualization. Also it can be used to estimate the optimization results: you can dump frames before and after optimization and compare them.使用分析转储简化了捕获性能“峰值”:引擎在后台继续呈现应用程序,即使它不在焦点上,因此在使用Web服务器进行性能数据可视化的情况下,可以覆盖发生峰值的帧。它还可以用来估计优化结果:您可以转储优化前后的帧并对它们进行比较。
Visualization Using Built-In Server使用内置服务器进行可视化#
To visualize the performance data using the local web server, perform the following:要使用本地Web服务器可视化性能数据,请执行以下操作:
- In the console, set the the number of frames to be profiled via the microprofile_webserver_frames console command. You can skip this step: by default, 200 frames will be profiled.在控制台中,通过 microprofile_webserver_frames 控制台命令设置要分析的帧数。您可以跳过此步骤:默认情况下,将分析 200 个帧。
-
On the Menu Bar of UnigineEditor, choose Tools -> Microprofile.在UnigineEditor的菜单栏上,选择Tools -> Microprofile。
The performance data will be displayed in your Web browser.性能数据将显示在您的Web浏览器中。
- You can display only a part of the profiled frames: in the Web browser address bar, add /<number_of_frames> to the current URL.您只能显示部分分析帧:在Web浏览器地址栏中,将/<number_of_frames>添加到当前URL。
- Don't forget to refresh (F5) the page in the Web browser while the profiling data is collected as it is not performed automatically.在收集分析数据时,不要忘记在Web浏览器中刷新页面(F5),因为它不是自动执行的。
For example, if you specify localhost:1337/100, only the first 100 frames will be displayed.例如,如果指定localhost:1337/100,则仅显示前100帧。
Performance Data性能数据#
The Microprofile visualizes the detailed per frame performance data on the operations performed by the engine on CPU and GPU and on the engine threads. In the Microprofile main menu, you can change the visualization mode: click Mode and choose the required one. By default, the Detailed mode is set. Microprofile可视化引擎在CPU和GPU 以及引擎线程上执行的操作的每帧详细性能数据。在Microprofile主菜单中,您可以更改可视化模式:单击Mode并选择所需的模式。默认情况下,设置为Detailed模式。
In the Detailed mode, each operation (function) and thread is displayed as a separate colored region. The regions are hierarchical: the function called by the other function is displayed under the last one. The size of the region is determined by the time the corresponding operation takes.在Detailed模式下,每个操作(功能)和线程均显示为单独的彩色区域。区域是分层的:另一个功能调用的功能显示在最后一个功能下方。区域的大小取决于相应操作所花费的时间。
In the picture below, the Engine::do_render() function calls the RenderRenderer::renderWorld() functions and so on:在下图中,Engine::do_render()函数调用RenderRenderer::renderWorld()函数,依此类推:
To view the data on a certain operation or a thread, point to the corresponding region. To zoom in/out the displayed regions, scroll the mouse wheel.要在某个操作或线程上查看数据,请指向相应的区域。要放大/缩小显示的区域,请滚动鼠标滚轮。
CPU DataCPU数据#
In the Main group of the performance data, the call stack of the operations (e.g., update, rendering, etc.) performed by the engine on CPU is displayed.在性能数据的Main组中,显示引擎在CPU上执行的操作的调用堆栈(例如update, rendering等)。
GPU DataGPU数据#
In the GPU group of the performance data, the call stack of the operations performed by the engine on GPU is displayed. In addition to the main performance data, for each function (e.g. environment rendering, post materials rendering and so on), the number of DIP calls and rendered triangles is shown. Also there can be the number of surfaces, lights, shadows rendered by this function, the number of materials and shaders used; the information on the node or material for which the function is called (identifier, name, etc.).性能数据的 GPU 组中,显示了引擎在GPU上执行的操作的调用堆栈。除了主要性能数据以外,对于每个功能(例如环境渲染,后期材质渲染等),还会显示DIP调用和渲染的三角形的数量。还可以有此功能渲染的表面,灯光,阴影的数量,所使用的材质和着色器的数量。有关调用该函数的节点或材质的信息(标识符,名称等)。
When the region that corresponds to the certain function is pointed, the Microprofile displays when this function is called on CPU and how much time is spent on its performing. 指向与某个功能对应的区域时,将在CPU上调用此功能以及执行该功能所花费的时间时显示Microprofile。
Vulkan or DirectX commands can be combined into GPU Debug groups that are created automatically when defining a profiling scope. All graphic resources loaded from external files, such as textures, shaders, static or skinned meshes, as well as the Engine's internal resources, have their own debug names to simplify identification. This information can be useful when using Graphics API debuggers, such as NVIDIA Nsight or RenderDoc. Vulkan或DirectX命令可以组合到在定义概要分析范围时自动创建的GPU调试组。从外部文件加载的所有图形资源(例如纹理,着色器,静态或蒙皮的网格物体)以及引擎的内部资源均具有自己的调试名称,以简化识别。使用Graphics API调试器(例如NVIDIA Nsight或RenderDoc)时,此信息可能很有用。
Engine Threads Data引擎线程数据#
The performance data on the engine threads is visualized in the CPUThread, SoundThread, AsyncQueueThread, WorldSpawnMeshClutterThread, WorldSpawnGrassThread groups. 引擎线程上的性能数据在CPUThread, SoundThread, AsyncQueueThread, WorldSpawnMeshClutterThread, WorldSpawnGrassThread组中可视化。
Using Microprofile For Application Logic将Microprofile用于应用逻辑#
You can use the Microprofile to inspect performance of your application logic. 您可以使用Microprofile来检查应用程序逻辑的性能。
- Open the source code of your application.打开应用程序的源代码。
Include the UnigineProfiler.h file to get access to methods of the Profiler class.包含UnigineProfiler.h文件以访问Profiler类的方法。
#include <UnigineProfiler.h> using namespace Unigine;
- Find a function or a scope in the code you want to inspect.在要检查的代码中查找函数或作用域。
-
In the beginning of the function (or the scope), call Profiler::begin(). Specify a name for the capture to be displayed in the Microprofile.在函数(或作用域)的开头,调用Profiler::begin()。 指定要在Microprofile中显示的捕获的名称。
You can specify the __FUNCTION__ macro to automatically use the class and the function name as the capture.您可以指定__FUNCTION__宏以自动使用类和函数名作为捕获。 -
In the end of the function (scope), call Profiler::end().在函数(作用域)的末尾,调用Profiler::end()。
int AppWorldLogic::update() { // start profiling Profiler::begin("AppWorldLogic::update"); float time = Game::getTime(); float angle_0 = time * 1.0f; float angle_1 = time * 1.5f; float angle_2 = time * 2.0f; light_0->setWorldTransform(translate(Unigine::Math::Vec3(sinf(angle_0), cosf(angle_0), 1.0f) * 20.0f)); light_1->setWorldTransform(translate(Unigine::Math::Vec3(sinf(angle_1), cosf(angle_1), 1.0f) * 20.0f)); light_2->setWorldTransform(translate(Unigine::Math::Vec3(sinf(angle_2), cosf(angle_2), 1.0f) * 20.0f)); // stop profiling Profiler::end(); return 1; }
- Run the project.运行项目。
- Open the console and run the microprofile_enabled 1 command.打开控制台并运行microprofile_enabled 1命令。
- Go to the Microprofile and find the region with the specified name.转到Microprofile并找到具有指定名称的区域。
The begin() and end() functions make the profiling scope available for both the Profiler and Microprofile. begin()和end()函数使分析作用域可用于分析器和Microprofile。
If you want to use Microprofile only, create the counter via the beginMicro() and endMicro() functions.如果您只想使用Microprofile,请通过beginMicro()和endMicro()函数创建计数器。
int AppWorldLogic::update()
{
// start profiling
int id = Profiler::beginMicro("AppWorldLogic::update");
float time = Game::getTime();
float angle_0 = time * 1.0f;
float angle_1 = time * 1.5f;
float angle_2 = time * 2.0f;
light_0->setWorldTransform(translate(Unigine::Math::Vec3(sinf(angle_0), cosf(angle_0), 1.0f) * 20.0f));
light_1->setWorldTransform(translate(Unigine::Math::Vec3(sinf(angle_1), cosf(angle_1), 1.0f) * 20.0f));
light_2->setWorldTransform(translate(Unigine::Math::Vec3(sinf(angle_2), cosf(angle_2), 1.0f) * 20.0f));
// stop profiling
Profiler::endMicro(id);
return 1;
}
Creating Nested Counters for Profiling创建用于分析的嵌套计数器#
To create several nested counters, you should use the beginMicro() and endMicro() functions. beginMicro() returns an identifier that should be passed to endMicro(). This enables to create nested and intersecting profiler counters.要创建多个嵌套计数器,应使用beginMicro()和endMicro()函数。 beginMicro()返回应传递给endMicro()的标识符。 这样就可以创建嵌套和相交的分析器计数器。
In the following example, combination of begin()/end() and beginMicro()/endMicro() is used. So, profiling of the AppWorldLogic::update() function will be available in both the Profiler and the Microprofile.在以下示例中,使用begin()/end()和beginMicro()/endMicro()的组合。 因此,AppWorldLogic::update()函数的分析将在分析器和Microprofile中都可用。
int AppWorldLogic::update()
{
// start profiling
Profiler::begin("AppWorldLogic::update");
float time = Game::getTime();
float angle_0 = time * 1.0f;
float angle_1 = time * 1.5f;
float angle_2 = time * 2.0f;
// start profiling of the first light source (available in the Microprofile only)
int id_0 = Profiler::beginMicro("1st Light Update");
light_0->setWorldTransform(translate(Unigine::Math::Vec3(sinf(angle_0), cosf(angle_0), 1.0f) * 20.0f));
// stop profiling of the first light source
Profiler::endMicro(id_0);
// start profiling of the second light source (available in the Microprofile only)
int id_1 = Profiler::beginMicro("2nd Light Update");
light_1->setWorldTransform(translate(Unigine::Math::Vec3(sinf(angle_1), cosf(angle_1), 1.0f) * 20.0f));
// stop profiling of the second light source
Profiler::endMicro(id_1);
light_2->setWorldTransform(translate(Unigine::Math::Vec3(sinf(angle_2), cosf(angle_2), 1.0f) * 20.0f));
// stop profiling
Profiler::end();
return 1;
}
Profiling Logic Using Macros使用宏分析逻辑#
There are special macros to mark fragments of your code that should be inspected. They can be used instead of the beginMicro() and endMicro() functions:有特殊的宏来标记应该检查的代码片段。 它们可以用来代替beginMicro()和endMicro()函数:
-
UNIGINE_PROFILER_FUNCTION to inspect performance of the function.UNIGINE_PROFILER_FUNCTION检查功能的性能。
Using Macro Using Functions void myFunction() { UNIGINE_PROFILER_FUNCTION; // your function code }
void myFunction() { int id = Profiler::beginMicro(__FUNCTION__); // your function code Profiler::endMicro(id); }
-
UNIGINE_PROFILER_SCOPED to inspect only a certain part of the code.UNIGINE_PROFILER_SCOPED只检查代码的某一部分。
Using Macro Using Functions void myFunction() { // your function code { UNIGINE_PROFILER_SCOPED("Scope name"); // your function code } }
void myFunction() { // your function code { int id = Profiler::beginMicro("Scope name"); // your function code Profiler::endMicro(id); } }