profiling.rst 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. .. _ray-core-internal-profiling:
  2. Profiling for Ray Developers
  3. ============================
  4. This guide helps contributors to the Ray project analyze Ray performance.
  5. Getting a stack trace of Ray C++ processes
  6. ------------------------------------------
  7. You can use the following GDB command to view the current stack trace of any
  8. running Ray process (e.g., raylet). This can be useful for debugging 100% CPU
  9. utilization or infinite loops (simply run the command a few times to see what
  10. the process is stuck on).
  11. .. code-block:: shell
  12. sudo gdb -batch -ex "thread apply all bt" -p <pid>
  13. Note that you can find the pid of the raylet with ``pgrep raylet``.
  14. Installation
  15. ------------
  16. These instructions are for Ubuntu only. Attempts to get ``pprof`` to correctly
  17. symbolize on Mac OS have failed.
  18. .. code-block:: bash
  19. sudo apt-get install google-perftools libgoogle-perftools-dev
  20. Launching the to-profile binary
  21. -------------------------------
  22. If you want to launch Ray in profiling mode, define the following variables:
  23. .. code-block:: bash
  24. export PERFTOOLS_PATH=/usr/lib/x86_64-linux-gnu/libprofiler.so
  25. export PERFTOOLS_LOGFILE=/tmp/pprof.out
  26. The file ``/tmp/pprof.out`` will be empty until you let the binary run the
  27. target workload for a while and then ``kill`` it via ``ray stop`` or by
  28. letting the driver exit.
  29. Memory Profiling
  30. ----------------
  31. If you want to run memory profiling on Ray core components, you can use Jemalloc (https://github.com/jemalloc/jemalloc).
  32. Ray supports environment variables to override LD_PRELOAD on core components.
  33. You can find the component name from `ray_constants.py`. For example, if you'd like to profile gcs_server,
  34. search `PROCESS_TYPE_GCS_SERVER` in `ray_constants.py`. You can see the value is `gcs_server`.
  35. Users are supposed to provide 3 env vars for memory profiling.
  36. - RAY_JEMALLOC_LIB_PATH: The path to the jemalloc shared library `.so`.
  37. - RAY_JEMALLOC_CONF: The MALLOC_CONF of jemalloc (comma separated).
  38. - RAY_JEMALLOC_PROFILE: Comma separated Ray components to run Jemalloc `.so`. e.g., ("raylet,gcs_server"). Note that the components should match the process type in `ray_constants.py`. (It means "RAYLET,GCS_SERVER" won't work).
  39. .. code-block:: bash
  40. # Install jemalloc
  41. wget https://github.com/jemalloc/jemalloc/releases/download/5.2.1/jemalloc-5.2.1.tar.bz2
  42. tar -xf jemalloc-5.2.1.tar.bz2
  43. cd jemalloc-5.2.1
  44. ./configure --enable-prof --enable-prof-libunwind
  45. make
  46. # set jemalloc configs through MALLOC_CONF env variable
  47. # read http://jemalloc.net/jemalloc.3.html#opt.lg_prof_interval
  48. # for all jemalloc configs
  49. # Ray start will profile the GCS server component.
  50. RAY_JEMALLOC_CONF=prof:true,lg_prof_interval:33,lg_prof_sample:17,prof_final:true,prof_leak:true \
  51. RAY_JEMALLOC_LIB_PATH=~/jemalloc-5.2.1/lib/libjemalloc.so \
  52. RAY_JEMALLOC_PROFILE=gcs_server \
  53. ray start --head
  54. # You should be able to see the following logs.
  55. 2021-10-20 19:45:08,175 INFO services.py:622 -- Jemalloc profiling will be used for gcs_server. env vars: {'LD_PRELOAD': '/Users/sangbincho/jemalloc-5.2.1/lib/libjemalloc.so', 'MALLOC_CONF': 'prof:true,lg_prof_interval:33,lg_prof_sample:17,prof_final:true,prof_leak:true'}
  56. Visualizing the CPU profile
  57. ---------------------------
  58. The output of ``pprof`` can be visualized in many ways. Here we output it as a
  59. zoomable ``.svg`` image displaying the call graph annotated with hot paths.
  60. .. code-block:: bash
  61. # Use the appropriate path.
  62. RAYLET=ray/python/ray/core/src/ray/raylet/raylet
  63. google-pprof -svg $RAYLET /tmp/pprof.out > /tmp/pprof.svg
  64. # Then open the .svg file with Chrome.
  65. # If you realize the call graph is too large, use -focus=<some function> to zoom
  66. # into subtrees.
  67. google-pprof -focus=epoll_wait -svg $RAYLET /tmp/pprof.out > /tmp/pprof.svg
  68. Here's a snapshot of an example svg output, taken from the official
  69. documentation:
  70. .. image:: http://goog-perftools.sourceforge.net/doc/pprof-test-big.gif
  71. Running Microbenchmarks
  72. -----------------------
  73. To run a set of single-node Ray microbenchmarks, use:
  74. .. code-block:: bash
  75. ray microbenchmark
  76. You can find the microbenchmark results for Ray releases in the `GitHub release logs <https://github.com/ray-project/ray/tree/master/release/release_logs>`__.
  77. References
  78. ----------
  79. - The `pprof documentation <http://goog-perftools.sourceforge.net/doc/cpu_profiler.html>`_.
  80. - A `Go version of pprof <https://github.com/google/pprof>`_.
  81. - The `gperftools <https://github.com/gperftools/gperftools>`_, including libprofiler, tcmalloc, and other goodies.