1 Star 0 Fork 0

萌小卖 / LuaProfiler-For-Unity

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

Lua Profiler For Unity


中文说明

Outline

Deploy and Install Tutors Datas Descriptions Record mode DIFF two different periods of Lua variables On-device Profiler Contact

Purpose

Unity + Lua script is now most popular incremental update frameWork for mobile game in China,However, since there haven't been good tool to monitor the cpu and memory usage of lua vm, lots of developers have no idea to optimize their code,so there are many potential risks in lua codes.
this tool is designed to support an easy-to-use profiler for Unity that help finding bottleneck and make your game more fast and stable.

Use renderings

Deploy and Install

Lua Profiler For Unity supports XLuaSLuaToLua and also a remote profiler tool so it supports WindowsAndroidIOS On-device Profiler.

  • You must open two Unity projects,one for game client ,one for editor server
  • Open LuaProfiler folder
  • Copy LuaProfilerClient folder to you game project content,if your C# Lua script is in Plugins folder,Copy LuaProfilerClient to Plugins.This Tool must make sure That code must in the same DLL which has C# lua codes.
  • Use Unity5.6 or newer version Unity version to open LuaProfilerServer as a Unity project
  • If your Unity version is below 5,call the following code before start the game.
MikuLuaProfiler.HookLuaSetup.OnStartGame();

Theory

It use mono.ceil's IL inject feature(also use in XLua),inject the profiler code to game code

Tutors

Config your client

Open windows by "Window->Lua Profiler Window", toggle profiler's feature and configure the server ip address.


Select the kind of code you want profiler,C# code color is green,and lua code color is blue.

Open server

Also open windows by "Window->Lua Profiler Window", then click OpenService,wait for client connects

Datas Descriptions

Name Descriptions
Overview function name
totalLuaMemory The sum of all Lua GCs produced by this function If GC happens then the value will not be very accurate
self The amount of GC produced by the function itself
totalMonoMemory The sum of all Mono GCs produced by this function If GC happens then the value will not be very accurate
self The amount of GC produced by the function itself
currentTime The time it takes for the function to run in current frame
averageTime Count the average value of the time spent on the function
totalTime All the time consumed by this function
LuaGC Lua GC generated by the current frame
MonoGC Mono GC generated by the current frame
totalCalls The number of runs of this function after the game starts
Calls The number of executions of the current frame of the function

Menu in Game

Press DEL or touch four fingers in screen to show this menu,you can change ip to connect and show more details of profiler info

Monitor registry

Programmers tend to forget to release Lua objects that are cached by C#.For example, when XLua calls LuaEnv.Dispose, it throws the exception "try to dispose a LuaEnv with C# callback!"

The reason is often caused by the following code

LuaTable tb = ...
Action action = tb.rawget<Action>("action");
a();
tb.Dispose(); //This line of code tends to forget to call
a= null;      //This line of code tends to forget to call

in Lua

// The lua table tends to forget to call dispose
CS.UIBehaviour.luauiTable = {}
// The callback tends to Unregister
CS.UIButton.OnClick = function() print("test") end

They can be released by C#'s destructor, but because in C# is just an index, the memory footprint is small then C# will not be GC immediately, but the memory usage on Lua is very large. This tool will provide real-time detection of the registry to help locate memory leaks quickly.


You can search history in record line

And the addremove history

You can set index '__name' to the lua table,monitor will replace the table code by the value of this index

CS.UIBehaviour.loginUI = { __name = "LoginUI" }

Charts

  • Toggle LuaChart to open lua memory chart,line color is blue.
  • Toggle MonoChart to open mono memory chart,line color is green.
  • Toggle FpsChart to open fps chart,line color is orange.
  • Toggle PssChart to open pss chart,line color is red.
  • Toggle PowerChart to open power chart,line color is brown.

Record mode

Click Record button, when game connect to server, toggle StartRecord to start or stop record.

Record button feature


  • drag slider to modify samples
  • click '<''>' to increase or discrease frames one by one
  • click '<<''>>' to fast locate the frames control by Capture Lua GCCapture Mono GCFrame Count
  • stop record and press left or right arrow keybord to increase or discrease frames one by one

DIFF two different periods of Lua variables

Choose an appropriate time, such as you are ready to open a new UI after the configuration table is loaded, click MarkLuaRecord button


Open the UI then close and unload the UI resources. Click DiffRecord. The tool will compare the difference between the Lua variable at the time of Mark and the variable when you click DiffRecord.


Clicking the ShowLog button will save the type of the variable and the reference path after the file is saved. Note that _G represents the global table and _R represents the object referenced by C#.


Destroy null values for Unity has released resources, while Lua still references variables, this is the main resource leak to focus on.



On-device Profiler

Set macro USE_LUA_PROFILER to inject profiler code in you App.

Android

set IP:127.0.0.1 port:2333. Connect your android phone with a USB cable.Execute the following instructions in cmd windows.

adb reverse tcp:2333 tcp:2333

Execute your app.

If you want to use luac code or luajit bytecode ,use InjectLua.exe in folder tools To inject the lua profiler code.

InjectLua.exe "inpath" "outpath"

Contact

If you find any bug or have any suggests join the QQ group:882425563 to contact us


大纲

部署和安装 使用教程 数据说明 数据报表导出 记录模式 DIFF 两个不同时期的Lua变量 自定义Profiler点 真机profiler 联系我们

目的

Unity + Lua 脚本现在是中国最流行的增量更新框架,但是,由于没有很好的工具来监控lua vm的cpu和内存使用情况,很多开发人员都不知道如何优化他们的代码,所以在lua代码中存在许多潜在的风险。 此工具旨给Unity 提供一个易于使用的lua性能分析器用于查找代码中的性能瓶颈并使您的游戏更快速、稳定。

使用示意图

部署和安装

Lua Profiler For Unity支持 XLuaSLuaToLua ,本工具是基于远程Socket的Profiler工具,因此它支持Android,IOS 的真机 Profiler。

  • 您必须打开两个Unity项目,一个放进游戏客户端,一个用于展示数据
  • 打开 LuaProfiler 文件夹
  • LuaProfilerClient 文件夹复制到游戏项目内容,如果您的C#Lua脚本位于Plugins文件夹中,则将 LuaProfilerClient 复制到插件。此工具必须确保该代码必须位于具有C#lua代码的同一DLL中。
  • 使用 Unity5.6 or newer version Unity版本将 LuaProfilerServer 作为Unity项目打开
  • 如果Unity版本低于5,请在开始游戏前调用以下代码。
MikuLuaProfiler.HookLuaSetup.OnStartGame();

注意!!! 不要在 static变量声明里面 启动lua流程(比如xlua的demo),请在Awake或者Start里面进行调用。

理论

它使用mono.ceil的IL注入功能(也用于XLua),将profiler代码注入游戏代码

使用教程

配置客户端

通过**"Window->Lua Profiler Window"**打开设置窗口


选择想要分析器的代码类型,C#代码颜色为绿色,lua代码颜色为蓝色。

打开展示Editor的监听端口

同理可以通过 Window->Lua Profiler Window打开窗口,然后单击 OpenService,等待客户端连接

数据说明

Name Descriptions
Overview 函数名称
totalLuaMemory 此函数生成的所有Lua GC的总和
self 函数本身产生的GC量
totalMonoMemory 此函数生成的所有Mono GC的总和
self 函数本身产生的GC量
currentTime 函数在当前帧中运行所需的时间
averageTime 计算在函数上花费的时间的平均值
totalTime 此功能消耗的所有时间
LuaGC 由当前帧生成的Lua GC
MonoGC 由当前帧生成的Mono GC
totalCalls 游戏开始后此功能的运行次数
Calls 函数当前帧的执行次数

游戏中的菜单

DEL 或 四手指触碰屏幕 以显示此菜单,您可以在游戏中随时中断或者重启连接 以及更换IP地址

监控注册表

程序员往往忘记释放由C#缓存的Lua对象。例如,当XLua调用LuaEnv.Dispose时,它会抛出异常 "try to dispose a LuaEnv with C# callback!"

原因通常是由以下代码引起的

LuaTable tb = ...
Action action = tb.rawget<Action>("action");
a();
tb.Dispose(); // 忘记Dipose了
a= null;      //忘记置空变量了

在Lua

// 忘记置空变量了
CS.UIBehaviour.luauiTable = {}
// 忘记取消注册函数了
CS.UIButton.OnClick = function() print("test") end

它们可以被C#的析构函数释放,但是因为在C#中只是一个索引,内存占用很小,所以C#不会立即成为GC,但是Lua上的内存使用量非常大。此工具将提供对注册表的实时检测,以帮助快速定位内存泄漏。


您可以在记录行中搜索历史记录

以及 addremove 的变化历史

您可以将索引'__name'设置为lua表,monitor将使用此索引的值替换表代码

CS.UIBehaviour.loginUI = { __name = "LoginUI" }

图表

  • 点击 LuaChart 打开lua内存图表,线条颜色为蓝色。
  • 点击 MonoChart 打开单声道内存图表,线条颜色为绿色。
  • 点击 FpsChart 打开fps图表,线条颜色为橙色。
  • 点击 PssChart 打开pss图表,线条颜色为红色。
  • 点击 PowerChart 打开电量图表,线条颜色为棕色。

记录模式

单击 Record 按钮,当游戏连接到服务器时,切换 StartRecord 以开始或停止录制。


  • 拖动滑块以修改样本
  • 单击 '<''>' 逐个增加或减少帧
  • 单击 '<<''>>' 以快速定位 Capture Lua GCCapture Mono GCFrame Count
  • 停止记录并按左或右箭头键盘逐个增加或减少帧

DIFF 两个不同时期的Lua变量

选取一个适当的时机,比如配置表加载完后,准备打开一个新的UI的时候点击MarkLuaRecord按钮


打开UI然后关闭并卸载掉UI资源,点击DiffRecord,工具将会对Mark时候的Lua变量与DiffRecord时候的变量进行差异比较


点击ShowLog按钮,将会把文件存盘打开之后将把对于变量的类型以及引用路径打印出来。 注意_G表示全局表,_R表示被C#引用的对象


Destroy null values为Unity已经将资源释放,而Lua仍然引用的变量,这个是主要的资源泄漏要重点关注。



自定义Profiler点

MikuLuaProfiler.LuaProfiler.BeginSampleCustom("profiler name")
-- your code
MikuLuaProfiler.LuaProfiler.EndSampleCustom()

真机Profiler

设置宏 USE_LUA_PROFILER 以在App中注入探查器代码。

Android

设置IP:127.0.0.1 port:2333. 。使用USB线连接Android手机。在cmd窗口中执行以下指令

adb reverse tcp:2333 tcp:2333

执行你的应用程序。

如果要使用luac代码或luajit字节码,请在文件夹工具中使用InjectLua.exe注入lua profiler代码。

InjectLua.exe "inpath" "outpath"

Use Case

Thanks

easy66
Xavier
Jay
ZhangDi
and all members in qq group LuaProfiler

联系

如果您发现任何错误或有任何建议加入QQ群:882425563 与我们联系

觉得插件好用,想支持作者继续开发这款插件可以选择投食

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

暂无描述 展开 收起
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/MengXiaoMai/LuaProfiler-For-Unity.git
git@gitee.com:MengXiaoMai/LuaProfiler-For-Unity.git
MengXiaoMai
LuaProfiler-For-Unity
LuaProfiler-For-Unity
master

搜索帮助