{"id":21071582,"url":"https://github.com/esrrhs/plua","last_synced_at":"2025-05-16T05:31:38.473Z","repository":{"id":45637260,"uuid":"206901934","full_name":"esrrhs/pLua","owner":"esrrhs","description":"Lua 性能分析工具 Lua profiler tool like gperftools","archived":false,"fork":false,"pushed_at":"2024-04-23T03:11:36.000Z","size":5776,"stargazers_count":99,"open_issues_count":1,"forks_count":23,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-03T20:51:14.191Z","etag":null,"topics":["gperftools","lua","lua-hook","profile"],"latest_commit_sha":null,"homepage":"","language":"Perl","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/esrrhs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-09-07T02:05:24.000Z","updated_at":"2025-04-03T06:46:02.000Z","dependencies_parsed_at":"2022-07-21T06:02:37.004Z","dependency_job_id":"30c6b57a-320b-450f-9d50-c8e07983d36d","html_url":"https://github.com/esrrhs/pLua","commit_stats":{"total_commits":99,"total_committers":3,"mean_commits":33.0,"dds":0.1515151515151515,"last_synced_commit":"c734adc80b9cbc18136a0aa6ae9b47dec9f13e11"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/esrrhs%2FpLua","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/esrrhs%2FpLua/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/esrrhs%2FpLua/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/esrrhs%2FpLua/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/esrrhs","download_url":"https://codeload.github.com/esrrhs/pLua/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254474255,"owners_count":22077247,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["gperftools","lua","lua-hook","profile"],"created_at":"2024-11-19T18:53:40.639Z","updated_at":"2025-05-16T05:31:36.206Z","avatar_url":"https://github.com/esrrhs.png","language":"Perl","funding_links":[],"categories":[],"sub_categories":[],"readme":"# pLua\n\n[\u003cimg src=\"https://img.shields.io/github/license/esrrhs/pLua\"\u003e](https://github.com/esrrhs/pLua)\n[\u003cimg src=\"https://img.shields.io/github/languages/top/esrrhs/pLua\"\u003e](https://github.com/esrrhs/pLua)\n[\u003cimg src=\"https://img.shields.io/github/actions/workflow/status/esrrhs/pLua/ccpp.yml?branch=master\"\u003e](https://github.com/esrrhs/pLua/actions)\n\nLua 性能分析工具\n\n## 简介\n类似于gperftools，可分析Lua程序的热点及内存分配情况\n\n\n## 特性\n- 简单，只需几行代码，即可输出结果，或通过[hookso](https://github.com/esrrhs/hookso)注入，不用修改代码\n- 准确，相比lua hook，定时采样的方式更能准确捕获lua执行的热点，使用ITIMER_PROF剔除sleep等堆栈\n- 轻量，因为是采样的，相比直接按行lua hook，能最小程度影响宿主程序的运行\n- 直观，输出调用图，能直观的看到热点和调用关系，可兼容[gperftools](https://github.com/gperftools/gperftools)的pprof工具，可生成[火焰图](https://github.com/brendangregg/FlameGraph)\n\n## 编译\n* 安装lua\n* 编译插件libplua.so及tools\n```shell\n# ./build.sh\n```\n* 安装火焰图的依赖项\n```shell\n# yum install perl-open.noarch\n```\n\n## 使用\n#### 获取CPU采样数据\n* 修改Lua code\n``` lua\n-- 引入libplua.so\nlocal p = require \"libplua\"\n-- 开启采样\n-- 参数1：采样时间（秒），0表示一直采样\n-- 参数2：采样结果文件\np.start(0, \"call.pro\")\n\ndo_some_thing()\n\n-- 结束采样，输出结果文件\np.stop()\n\n```\n* 或者用[hookso](https://github.com/esrrhs/hookso)注入\n```shell\na) 首先获取进程中的Lua_State指针，比如进程的xxx.so调用了lua_settop(L)函数，那么就取第一个参数\n# ./hookso arg $PID xxx.so lua_settop 1 \n123456\n\nb) 加载libplua.so\n# ./hookso dlopen $PID ./libplua.so\n\nc) 执行libplua.so的lrealstart手动开启，等价于lrealstart(L, 0, \"./call.pro\")\n# ./hookso call $PID libplua.so lrealstart i=123456 i=0 s=\"./call.pro\"\n\nc) 执行libclua.so的lrealstop手动关闭，等价于lrealstop(L)\n# ./hookso call $PID libplua.so lrealstop i=123456\n```\n#### 获取内存采样数据\n* 修改Lua code\n``` lua\n-- 引入libplua.so\nlocal p = require \"libplua\"\n-- 开启采样\n-- 参数1：采样时间（秒），0表示一直采样\n-- 参数2：采样结果文件名，会生成2个采样文件，分别代表内存分配大小、内存占用大小\np.start_mem(0, \"mem.pro\")\n\ndo_some_thing()\n\n-- 结束采样，输出结果文件\np.stop_mem()\n\n```\n* 或者用[hookso](https://github.com/esrrhs/hookso)注入\n```shell\na) 首先获取进程中的Lua_State指针，比如进程的xxx.so调用了lua_settop(L)函数，那么就取第一个参数\n# ./hookso arg $PID xxx.so lua_settop 1 \n123456\n\nb) 加载libplua.so\n# ./hookso dlopen $PID ./libplua.so\n\nc) 执行libplua.so的lrealstartmem手动开启，等价于lrealstartmem(L, 0, \"./call.pro\")\n# ./hookso call $PID libplua.so lrealstartmem i=123456 i=0 s=\"./call.pro\"\n\nc) 执行libclua.so的lrealstopmem手动关闭，等价于lrealstopmem(L)\n# ./hookso call $PID libplua.so lrealstopmem i=123456\n```\n\n## 示例\n#### 运行test目录下的lua\n```shell\n# lua test_cpu.lua\n# lua test_mem.lua\n```\n\n#### 生成可视化结果\n使用tools目录下的show.sh脚本生成gperftools风格png及火焰图\n```shell\n# cd tools\n# ./show.sh ../test\n```\n\n#### 查看test_cpu.lua的热点\n![image](test/call.png)\n\n#### 查看test_mem.lua的内存采样\n* 内存分配大小\n![image](test/mem_ALLOC_SIZE.png)\n\n## 其他\n[lua全家桶](https://github.com/esrrhs/lua-family-bucket)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fesrrhs%2Fplua","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fesrrhs%2Fplua","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fesrrhs%2Fplua/lists"}