分析 SUMO

引言#

可以使用不同的方法来分析 SUMO。对于 C++ 部分,我们传统上使用 gprof。较新的方法使用 perf Linux 工具或 valgrind。所有这些工具都仅在 Linux 上测试过。

对于 Python,使用的工具是 cProfile。

分析 C++#

这部分主要是 https://stackoverflow.com/questions/375913/how-do-i-profile-c-code-running-on-linux/60265409#60265409 的总结。

perf#

这是首选工具。它速度快,不需要特殊构建,而且火焰图看起来很漂亮。唯一的缺点是烦人的设置会干扰 sysctl。

过程如下:

  • 安装 perf 工具 sudo apt install linux-tools-$(uname -r)
  • 运行 perf record 检查是否允许记录内核事件(可能需要使用 Ctrl + c 中止调用)。
    • 如果不允许,并且希望永久启用,请执行 sudo sh -c 'echo "kernel.perf_event_paranoid = -1\nkernel.kptr_restrict = 0" >> /etc/sysctl.conf'
  • 运行 perf record --call-graph dwarf sumo -c test.sumocfg。这将生成一个名为 perf.data 的文件。
  • 使用 perf report 进行交互式检查。
  • 可选地生成火焰图(使用 Web 浏览器查看生成的 svg)
    • git clone https://github.com/brendangregg/FlameGraph
    • perf script | FlameGraph/stackcollapse-perf.pl | FlameGraph/flamegraph.pl > flamegraph.svg

gprof#

如果不想或无法使用上述方法,这是次优选择。它需要重新编译 sumo,但通常不需要 root 权限,因为大部分工具(除了可视化部分)已经随编译器安装。

过程如下:

  • 安装 gprof sudo apt install binutils
  • 通过在 VS Code 中选择 "Profiling" 构建变体,或在运行 cmake 时启用 -DPROFILING=ON 来启用代码插桩。这默认会在所有可执行文件名后添加后缀 P
  • 运行代码,例如 $SUMO_HOME/bin/sumoP -c test.sumocfg。这将生成一个名为 gmon.out 的文件。
  • 生成文本输出,例如 gprof $SUMO_HOME/bin/sumoP gmon.out > gprof.log
  • 可选地使用可视化工具
    • python3 -m pip install gprof2dot
    • gprof2dot < gprof.log > gprof.dot
    • sudo apt install xdot
    • xdot gprof.dot

valgrind#

待定

Python 分析#

待定