Giter VIP home page Giter VIP logo

plua's Introduction

pLua

Lua 性能分析工具

简介

类似于gperftools,可分析Lua程序的热点及内存分配情况

特性

  • 简单,只需几行代码,即可输出结果,或通过hookso注入,不用修改代码
  • 准确,相比lua hook,定时采样的方式更能准确捕获lua执行的热点,使用ITIMER_PROF剔除sleep等堆栈
  • 轻量,因为是采样的,相比直接按行lua hook,能最小程度影响宿主程序的运行
  • 直观,输出调用图,能直观的看到热点和调用关系,可兼容gperftools的pprof工具,可生成火焰图

编译

  • 安装lua
  • 编译插件libplua.so及tools
# ./build.sh
  • 安装火焰图的依赖项
# yum install perl-open.noarch

使用

获取CPU采样数据

  • 修改Lua code
-- 引入libplua.so
local p = require "libplua"
-- 开启采样
-- 参数1:采样时间(秒),0表示一直采样
-- 参数2:采样结果文件
p.start(0, "call.pro")

do_some_thing()

-- 结束采样,输出结果文件
p.stop()
a) 首先获取进程中的Lua_State指针,比如进程的xxx.so调用了lua_settop(L)函数,那么就取第一个参数
# ./hookso arg $PID xxx.so lua_settop 1 
123456

b) 加载libplua.so
# ./hookso dlopen $PID ./libplua.so

c) 执行libplua.so的lrealstart手动开启,等价于lrealstart(L, 0, "./call.pro")
# ./hookso call $PID libplua.so lrealstart i=123456 i=0 s="./call.pro"

c) 执行libclua.so的lrealstop手动关闭,等价于lrealstop(L)
# ./hookso call $PID libplua.so lrealstop i=123456

获取内存采样数据

  • 修改Lua code
-- 引入libplua.so
local p = require "libplua"
-- 开启采样
-- 参数1:采样时间(秒),0表示一直采样
-- 参数2:采样结果文件名,会生成2个采样文件,分别代表内存分配大小、内存占用大小
p.start_mem(0, "mem.pro")

do_some_thing()

-- 结束采样,输出结果文件
p.stop_mem()
a) 首先获取进程中的Lua_State指针,比如进程的xxx.so调用了lua_settop(L)函数,那么就取第一个参数
# ./hookso arg $PID xxx.so lua_settop 1 
123456

b) 加载libplua.so
# ./hookso dlopen $PID ./libplua.so

c) 执行libplua.so的lrealstartmem手动开启,等价于lrealstartmem(L, 0, "./call.pro")
# ./hookso call $PID libplua.so lrealstartmem i=123456 i=0 s="./call.pro"

c) 执行libclua.so的lrealstopmem手动关闭,等价于lrealstopmem(L)
# ./hookso call $PID libplua.so lrealstopmem i=123456

示例

运行test目录下的lua

# lua test_cpu.lua
# lua test_mem.lua

生成可视化结果

使用tools目录下的show.sh脚本生成gperftools风格png及火焰图

# cd tools
# ./show.sh ../test

查看test_cpu.lua的热点

image

查看test_mem.lua的内存采样

  • 内存分配大小 image

其他

lua全家桶

plua's People

Contributors

esrrhs avatar patrick08t avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

plua's Issues

是否支持多个平台

ubuntu 20.04下可以编辑成功,windows 10下编译遇到问题(使用visual studio community 2017),能否提供windows10下的makefile或者release版本

# runtime/cgo ccache: invalid option -- 'E'

系统信息:
Linux version 3.10.0-1160.11.1.el7.x86_64 ([email protected]) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) ) #1 SMP Fri Dec 18 16:34:56 UTC 2020
go版本:
go version go1.14 linux/amd64
go env:
GO111MODULE="" GOARCH="amd64" GOBIN="" GOCACHE="/data/ptt/.cache/go-build" GOENV="/data/ptt/.config/go/env" GOEXE="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOINSECURE="" GONOPROXY="" GONOSUMDB="" GOOS="linux" GOPATH="/data/ptt/private/Server/src/go" GOPRIVATE="" GOPROXY="https://proxy.golang.org,direct" GOROOT="/opt/go" GOSUMDB="sum.golang.org" GOTMPDIR="" GOTOOLDIR="/opt/go/pkg/tool/linux_amd64" GCCGO="gccgo" AR="ar" CC="ccache" CXX="ccache" CGO_ENABLED="1" GOMOD="" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config" GOGCCFLAGS="/opt/rh/devtoolset-8/root/usr/bin/gcc -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build559118463=/tmp/go-build -gno-record-gcc-switches"
cmake版本:
cmake version 3.20.0

执行 go get github.com/goccy/go-graphviz
报错:
`# runtime/cgo
ccache: invalid option -- 'E'
Usage:
ccache [options]
ccache compiler [compiler options]
compiler [compiler options] (via symbolic link)

Common options:
-c, --cleanup delete old files and recalculate size counters
(normally not needed as this is done
automatically)
-C, --clear clear the cache completely (except configuration)
-F, --max-files=N set maximum number of files in cache to N (use 0
for no limit)
-M, --max-size=SIZE set maximum size of cache to SIZE (use 0 for no
limit); available suffixes: k, M, G, T (decimal)
and Ki, Mi, Gi, Ti (binary); default suffix: G
-p, --show-config show current configuration options in
human-readable format
-s, --show-stats show summary of configuration and statistics
counters in human-readable format
-z, --zero-stats zero statistics counters

-h, --help                print this help text
-V, --version             print version and copyright information

Options for scripting or debugging:
--dump-manifest=PATH dump manifest file at PATH in text format
-k, --get-config=K print the value of configuration key K
--hash-file=PATH print the hash (-) of the file at PATH
--print-stats print statistics counter IDs and corresponding
values in machine-parsable format
-o, --set-config=K=V set configuration item K to value V

See also https://ccache.dev.`

不知道什么原因

接入到lua 服务器,崩溃

打开性能采集,会再此处崩溃

#0 settraps (ci=0x5f7476655b5d7465) at lua/ldebug.c:116
#1 lua_sethook (func=0x7f6eb4e46b50 <SignalHandlerHook(lua_State*, lua_Debug*)>, mask=, count=1, L=0x33b7e0d3728) at lua/ldebug.c:141
#2 lua_sethook (L=0x33b7e0d3728, func=0x7f6eb4e46b50 <SignalHandlerHook(lua_State*, lua_Debug*)>, mask=, count=1) at lua/ldebug.c:131

gdb 调试 ci 对象不合法, lua_state 对象是合法的
(gdb) p *ci
Cannot access memory at address 0x5f7476655b5d7465

只支持lua-5.3.4及以上的版本

执行: g++ -g3 -shared -o libplua.so plua.cpp -fPIC -std=c++11 -I ../lua-5.3.1/include/

报错如下:
plua.cpp: 在函数‘int pushglobalfuncname(lua_State*, lua_Debug*)’中:
plua.cpp:117:40: 错误:‘LUA_LOADED_TABLE’在此作用域中尚未声明
lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);

请问大佬后续会做低版本兼容吗?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.