autofdo.rst 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ===================================
  3. Using AutoFDO with the Linux kernel
  4. ===================================
  5. This enables AutoFDO build support for the kernel when using
  6. the Clang compiler. AutoFDO (Auto-Feedback-Directed Optimization)
  7. is a type of profile-guided optimization (PGO) used to enhance the
  8. performance of binary executables. It gathers information about the
  9. frequency of execution of various code paths within a binary using
  10. hardware sampling. This data is then used to guide the compiler's
  11. optimization decisions, resulting in a more efficient binary. AutoFDO
  12. is a powerful optimization technique, and data indicates that it can
  13. significantly improve kernel performance. It's especially beneficial
  14. for workloads affected by front-end stalls.
  15. For AutoFDO builds, unlike non-FDO builds, the user must supply a
  16. profile. Acquiring an AutoFDO profile can be done in several ways.
  17. AutoFDO profiles are created by converting hardware sampling using
  18. the "perf" tool. It is crucial that the workload used to create these
  19. perf files is representative; they must exhibit runtime
  20. characteristics similar to the workloads that are intended to be
  21. optimized. Failure to do so will result in the compiler optimizing
  22. for the wrong objective.
  23. The AutoFDO profile often encapsulates the program's behavior. If the
  24. performance-critical codes are architecture-independent, the profile
  25. can be applied across platforms to achieve performance gains. For
  26. instance, using the profile generated on Intel architecture to build
  27. a kernel for AMD architecture can also yield performance improvements.
  28. There are two methods for acquiring a representative profile:
  29. (1) Sample real workloads using a production environment.
  30. (2) Generate the profile using a representative load test.
  31. When enabling the AutoFDO build configuration without providing an
  32. AutoFDO profile, the compiler only modifies the dwarf information in
  33. the kernel without impacting runtime performance. It's advisable to
  34. use a kernel binary built with the same AutoFDO configuration to
  35. collect the perf profile. While it's possible to use a kernel built
  36. with different options, it may result in inferior performance.
  37. One can collect profiles using AutoFDO build for the previous kernel.
  38. AutoFDO employs relative line numbers to match the profiles, offering
  39. some tolerance for source changes. This mode is commonly used in a
  40. production environment for profile collection.
  41. In a profile collection based on a load test, the AutoFDO collection
  42. process consists of the following steps:
  43. #. Initial build: The kernel is built with AutoFDO options
  44. without a profile.
  45. #. Profiling: The above kernel is then run with a representative
  46. workload to gather execution frequency data. This data is
  47. collected using hardware sampling, via perf. AutoFDO is most
  48. effective on platforms supporting advanced PMU features like
  49. LBR on Intel machines.
  50. #. AutoFDO profile generation: Perf output file is converted to
  51. the AutoFDO profile via offline tools.
  52. The support requires a Clang compiler LLVM 17 or later.
  53. Preparation
  54. ===========
  55. Configure the kernel with::
  56. CONFIG_AUTOFDO_CLANG=y
  57. Customization
  58. =============
  59. The default CONFIG_AUTOFDO_CLANG setting covers kernel space objects for
  60. AutoFDO builds. One can, however, enable or disable AutoFDO build for
  61. individual files and directories by adding a line similar to the following
  62. to the respective kernel Makefile:
  63. - For enabling a single file (e.g. foo.o) ::
  64. AUTOFDO_PROFILE_foo.o := y
  65. - For enabling all files in one directory ::
  66. AUTOFDO_PROFILE := y
  67. - For disabling one file ::
  68. AUTOFDO_PROFILE_foo.o := n
  69. - For disabling all files in one directory ::
  70. AUTOFDO_PROFILE := n
  71. Workflow
  72. ========
  73. Here is an example workflow for AutoFDO kernel:
  74. 1) Build the kernel on the host machine with LLVM enabled,
  75. for example, ::
  76. $ make menuconfig LLVM=1
  77. Turn on AutoFDO build config::
  78. CONFIG_AUTOFDO_CLANG=y
  79. With a configuration that with LLVM enabled, use the following command::
  80. $ scripts/config -e AUTOFDO_CLANG
  81. After getting the config, build with ::
  82. $ make LLVM=1
  83. 2) Install the kernel on the test machine.
  84. 3) Run the load tests. The '-c' option in perf specifies the sample
  85. event period. We suggest using a suitable prime number, like 500009,
  86. for this purpose.
  87. - For Intel platforms::
  88. $ perf record -e BR_INST_RETIRED.NEAR_TAKEN:k -a -N -b -c <count> -o <perf_file> -- <loadtest>
  89. - For AMD platforms:
  90. The supported systems are: Zen3 with BRS, or Zen4 with amd_lbr_v2. To check,
  91. For Zen3::
  92. $ cat /proc/cpuinfo | grep " brs"
  93. For Zen4::
  94. $ cat /proc/cpuinfo | grep amd_lbr_v2
  95. The following command generated the perf data file::
  96. $ perf record --pfm-events RETIRED_TAKEN_BRANCH_INSTRUCTIONS:k -a -N -b -c <count> -o <perf_file> -- <loadtest>
  97. 4) (Optional) Download the raw perf file to the host machine.
  98. 5) To generate an AutoFDO profile, two offline tools are available:
  99. create_llvm_prof and llvm_profgen. The create_llvm_prof tool is part
  100. of the AutoFDO project and can be found on GitHub
  101. (https://github.com/google/autofdo), version v0.30.1 or later.
  102. The llvm_profgen tool is included in the LLVM compiler itself. It's
  103. important to note that the version of llvm_profgen doesn't need to match
  104. the version of Clang. It needs to be the LLVM 19 release of Clang
  105. or later, or just from the LLVM trunk. ::
  106. $ llvm-profgen --kernel --binary=<vmlinux> --perfdata=<perf_file> -o <profile_file>
  107. or ::
  108. $ create_llvm_prof --binary=<vmlinux> --profile=<perf_file> --format=extbinary --out=<profile_file>
  109. Note that multiple AutoFDO profile files can be merged into one via::
  110. $ llvm-profdata merge -o <profile_file> <profile_1> <profile_2> ... <profile_n>
  111. 6) Rebuild the kernel using the AutoFDO profile file with the same config as step 1,
  112. (Note CONFIG_AUTOFDO_CLANG needs to be enabled)::
  113. $ make LLVM=1 CLANG_AUTOFDO_PROFILE=<profile_file>