FlameScope 更高级全面的火焰图
网飞(Netflix)开发的火焰图工具能够更好得呈现出一段时间内的服务器on/off cpu 的热力图。
安装步骤
$ git clone https://github.com/Netflix/flamescope
$ cd flamescope
$ pip install -r requirements.txt
$ python3 run.py
如果安装成功,运行到最后一步会输出如下,flamescope会启动一个httpserver:
$ python3.9 run.py
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 888-173-631
如果某一步失败,一般是第三步和第四步 会出现python相关的错误,可以参考如下错误的解决办法。
安装问题fix
-
ImportError: cannot import name ‘Feature’ from ‘setuptools’
因为python3中的setuptools 版本默认升级到了46及以上,但是Feature 接口已经在46及以上的setuptools版本中移除了,所以使用会有问题
如果想要python3运行时使用Feature这个模块,需要安装小于46版本的setuptools
pip install --upgrade pip setuptools==45.2.0
即可解决 -
ImportError: failed to find libmagic.
libmagic 版本过高,python3运行时找不到对应的动态库降低这个版本的库即可:
pip uninstall python-magic pip install python-magic-bin==0.4.14
-
TypeError: required field “type_ignores” missing from Module
在
/usr/local/lib/python3.9/site-packages/werkzeug/routing.py
代码中要求一些函数运行必须携带参数# 将改行代码更换成如下即可,为函数ast.Module增加一个空参数 # module = ast.fix_missing_locations(ast.Module([func_ast],[])) module = ast.fix_missing_locations(ast.Module([func_ast])) code = compile(module, "<werkzeug routing>", "exec") globs, locs = {}, {} exec(code, globs, locs) return locs[func_ast.name]
接下来即可启动flame graph 的httpserver了
python3 run.py
使用方式
flamescope的介绍以及使用 官方也给了两个视频,而基本火焰图的查看这里不再多说,从底向上是函数层级的调用占用CPU的比例。
安装完成,并运行python3 run.py
成功之后,会成功启动一个flamescope的httpserver。
如果没有配置flamescope/app/config.py
文件中的ip和端口号,则可以通过http://127.0.0.1:5000
访问examples中的热力图。
点开一个perf数据
可以看到如上热力图,其中横轴是时间,总共采样了60s,每一个纵轴代表一秒内的1000ms cpu占用情况,可以看到单纵轴小方格内颜色越深的小方格,表示这段时间内的cpu消耗较多。
通过这个图能够非常清晰得看到一段时间内CPU的消耗情况,具体哪一部分消耗较多,可以选择对应区域的小方格看到这段区域的火焰图数据,非常直观。
如果想要看自己抓的热力图,可以通过perf命令采样自己应用进程的oncpu数据,flamescope会在当前文件自动识别到perf的结果数据:
$ sudo perf record -F 49 -p <pid> -g -- sleep 120
$ sudo perf script --header > stacks.oncpu
如果安装了bcc工具集,通过offcpu命令抓取offcpu的火焰图,同样能够看到offcpu的数据:
offcputime -df -p <pid> 60 > stacks.offcpu