propeller.rst 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. .. SPDX-License-Identifier: GPL-2.0
  2. =====================================
  3. Using Propeller with the Linux kernel
  4. =====================================
  5. This enables Propeller build support for the kernel when using Clang
  6. compiler. Propeller is a profile-guided optimization (PGO) method used
  7. to optimize binary executables. Like AutoFDO, it utilizes hardware
  8. sampling to gather information about the frequency of execution of
  9. different code paths within a binary. Unlike AutoFDO, this information
  10. is then used right before linking phase to optimize (among others)
  11. block layout within and across functions.
  12. A few important notes about adopting Propeller optimization:
  13. #. Although it can be used as a standalone optimization step, it is
  14. strongly recommended to apply Propeller on top of AutoFDO,
  15. AutoFDO+ThinLTO or Instrument FDO. The rest of this document
  16. assumes this paradigm.
  17. #. Propeller uses another round of profiling on top of
  18. AutoFDO/AutoFDO+ThinLTO/iFDO. The whole build process involves
  19. "build-afdo - train-afdo - build-propeller - train-propeller -
  20. build-optimized".
  21. #. Propeller requires LLVM 19 release or later for Clang/Clang++
  22. and the linker(ld.lld).
  23. #. In addition to LLVM toolchain, Propeller requires a profiling
  24. conversion tool: https://github.com/google/autofdo with a release
  25. after v0.30.1: https://github.com/google/autofdo/releases/tag/v0.30.1.
  26. The Propeller optimization process involves the following steps:
  27. #. Initial building: Build the AutoFDO or AutoFDO+ThinLTO binary as
  28. you would normally do, but with a set of compile-time / link-time
  29. flags, so that a special metadata section is created within the
  30. kernel binary. The special section is only intend to be used by the
  31. profiling tool, it is not part of the runtime image, nor does it
  32. change kernel run time text sections.
  33. #. Profiling: The above kernel is then run with a representative
  34. workload to gather execution frequency data. This data is collected
  35. using hardware sampling, via perf. Propeller is most effective on
  36. platforms supporting advanced PMU features like LBR on Intel
  37. machines. This step is the same as profiling the kernel for AutoFDO
  38. (the exact perf parameters can be different).
  39. #. Propeller profile generation: Perf output file is converted to a
  40. pair of Propeller profiles via an offline tool.
  41. #. Optimized build: Build the AutoFDO or AutoFDO+ThinLTO optimized
  42. binary as you would normally do, but with a compile-time /
  43. link-time flag to pick up the Propeller compile time and link time
  44. profiles. This build step uses 3 profiles - the AutoFDO profile,
  45. the Propeller compile-time profile and the Propeller link-time
  46. profile.
  47. #. Deployment: The optimized kernel binary is deployed and used
  48. in production environments, providing improved performance
  49. and reduced latency.
  50. Preparation
  51. ===========
  52. Configure the kernel with::
  53. CONFIG_AUTOFDO_CLANG=y
  54. CONFIG_PROPELLER_CLANG=y
  55. Customization
  56. =============
  57. The default CONFIG_PROPELLER_CLANG setting covers kernel space objects
  58. for Propeller builds. One can, however, enable or disable Propeller build
  59. for individual files and directories by adding a line similar to the
  60. following to the respective kernel Makefile:
  61. - For enabling a single file (e.g. foo.o)::
  62. PROPELLER_PROFILE_foo.o := y
  63. - For enabling all files in one directory::
  64. PROPELLER_PROFILE := y
  65. - For disabling one file::
  66. PROPELLER_PROFILE_foo.o := n
  67. - For disabling all files in one directory::
  68. PROPELLER__PROFILE := n
  69. Workflow
  70. ========
  71. Here is an example workflow for building an AutoFDO+Propeller kernel:
  72. 1) Assuming an AutoFDO profile is already collected following
  73. instructions in the AutoFDO document, build the kernel on the host
  74. machine, with AutoFDO and Propeller build configs ::
  75. CONFIG_AUTOFDO_CLANG=y
  76. CONFIG_PROPELLER_CLANG=y
  77. and ::
  78. $ make LLVM=1 CLANG_AUTOFDO_PROFILE=<autofdo-profile-name>
  79. 2) Install the kernel on the test machine.
  80. 3) Run the load tests. The '-c' option in perf specifies the sample
  81. event period. We suggest using a suitable prime number, like 500009,
  82. for this purpose.
  83. - For Intel platforms::
  84. $ perf record -e BR_INST_RETIRED.NEAR_TAKEN:k -a -N -b -c <count> -o <perf_file> -- <loadtest>
  85. - For AMD platforms::
  86. $ perf record --pfm-event RETIRED_TAKEN_BRANCH_INSTRUCTIONS:k -a -N -b -c <count> -o <perf_file> -- <loadtest>
  87. Note you can repeat the above steps to collect multiple <perf_file>s.
  88. 4) (Optional) Download the raw perf file(s) to the host machine.
  89. 5) Use the create_llvm_prof tool (https://github.com/google/autofdo) to
  90. generate Propeller profile. ::
  91. $ create_llvm_prof --binary=<vmlinux> --profile=<perf_file>
  92. --format=propeller --propeller_output_module_name
  93. --out=<propeller_profile_prefix>_cc_profile.txt
  94. --propeller_symorder=<propeller_profile_prefix>_ld_profile.txt
  95. "<propeller_profile_prefix>" can be something like "/home/user/dir/any_string".
  96. This command generates a pair of Propeller profiles:
  97. "<propeller_profile_prefix>_cc_profile.txt" and
  98. "<propeller_profile_prefix>_ld_profile.txt".
  99. If there are more than 1 perf_file collected in the previous step,
  100. you can create a temp list file "<perf_file_list>" with each line
  101. containing one perf file name and run::
  102. $ create_llvm_prof --binary=<vmlinux> --profile=@<perf_file_list>
  103. --format=propeller --propeller_output_module_name
  104. --out=<propeller_profile_prefix>_cc_profile.txt
  105. --propeller_symorder=<propeller_profile_prefix>_ld_profile.txt
  106. 6) Rebuild the kernel using the AutoFDO and Propeller
  107. profiles. ::
  108. CONFIG_AUTOFDO_CLANG=y
  109. CONFIG_PROPELLER_CLANG=y
  110. and ::
  111. $ make LLVM=1 CLANG_AUTOFDO_PROFILE=<profile_file> CLANG_PROPELLER_PROFILE_PREFIX=<propeller_profile_prefix>