← Documents Documentation/dev-tools/autofdo.rst GitHub 원문 ↗

Linux 6.18.37 · Dev Tools

Linux kernel AutoFDO

Clang AutoFDO profile 수집, architecture 간 재사용, file별 Makefile 설정과 perf 기반 build workflow를 설명합니다.

Source pathDocumentation/dev-tools/autofdo.rst
Source versionLinux v6.18.37
TranslationDUJINLABS 전문 번역 + 해설

요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.

1. 요약·해설

원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.

요약과 해설

autofdo.rst:1-168

AutoFDO는 hardware sampling으로 얻은 실행 빈도를 Clang 최적화에 반영하는 PGO 방식입니다. 대표 workload로 profile을 수집해야 하며, 같은 AutoFDO configuration의 kernel을 쓰는 것이 가장 안정적입니다.

실무 흐름은 profile 없는 초기 build, LBR 또는 BRS 기반 perf 수집, `llvm-profgen`이나 `create_llvm_prof` 변환, 필요 시 profile 병합, `CLANG_AUTOFDO_PROFILE`을 지정한 rebuild 순서입니다. LLVM 17 이상이 필요하고 profile 생성용 `llvm-profgen`은 LLVM 19 이상을 사용합니다.

2. 영어 원문 전체

번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.

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

3. 한국어 전문 번역

영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.

Linux kernel에서 AutoFDO 사용

1-32

SPDX 라이선스 식별자: GPL-2.0

Linux kernel에서 AutoFDO 사용

이 기능은 Clang compiler를 사용할 때 kernel의 AutoFDO build를 지원합니다. AutoFDO(Auto-Feedback-Directed Optimization)는 binary executable의 성능을 높이는 profile-guided optimization(PGO)의 한 종류입니다. hardware sampling으로 binary 안의 여러 code path가 실행되는 빈도 정보를 수집하고, 이 데이터를 compiler 최적화 결정에 사용해 더 효율적인 binary를 만듭니다. AutoFDO는 kernel 성능을 크게 개선할 수 있으며 특히 front-end stall의 영향을 받는 workload에 유용합니다.

non-FDO build와 달리 AutoFDO build에는 사용자가 profile을 제공해야 합니다. AutoFDO profile은 여러 방법으로 얻을 수 있으며, `perf` 도구의 hardware sampling 결과를 변환해 만듭니다. perf file 생성에 사용하는 workload는 최적화 대상 workload와 비슷한 runtime 특성을 가진 대표 workload여야 합니다. 그렇지 않으면 compiler가 잘못된 목표에 맞춰 최적화합니다.

AutoFDO profile에는 program의 동작이 담기는 경우가 많습니다. 성능에 중요한 code가 architecture-independent하다면 platform 사이에서 profile을 재사용해 성능 향상을 얻을 수 있습니다. 예를 들어 Intel architecture에서 만든 profile로 AMD architecture용 kernel을 build해도 성능이 향상될 수 있습니다.

대표 profile 수집과 생성

33-64

대표 profile을 얻는 방법은 두 가지입니다.

  • production environment에서 실제 workload를 sampling합니다.
  • 대표 load test로 profile을 생성합니다.

AutoFDO profile 없이 AutoFDO build configuration만 활성화하면 compiler는 runtime 성능에 영향을 주지 않고 kernel의 DWARF 정보만 수정합니다. perf profile을 수집할 때는 같은 AutoFDO configuration으로 build한 kernel binary를 사용하는 것이 좋습니다. 다른 option으로 build한 kernel도 사용할 수 있지만 성능이 떨어질 수 있습니다.

이전 kernel의 AutoFDO build를 사용해 profile을 수집할 수도 있습니다. AutoFDO는 상대 line number로 profile을 일치시키므로 source 변경을 어느 정도 허용합니다. production environment에서 흔히 사용하는 profile 수집 방식입니다.

load test 기반 AutoFDO 수집 과정은 다음 단계로 구성됩니다.

  • 초기 build: profile 없이 AutoFDO option으로 kernel을 build합니다.
  • profiling: 이 kernel에서 대표 workload를 실행하고 `perf` hardware sampling으로 실행 빈도 데이터를 수집합니다. AutoFDO는 Intel의 LBR처럼 고급 PMU 기능을 지원하는 platform에서 가장 효과적입니다.
  • AutoFDO profile 생성: offline 도구로 perf output file을 AutoFDO profile로 변환합니다.

이 기능에는 LLVM 17 이상인 Clang compiler가 필요합니다.

준비

65-71

준비

다음 option으로 kernel을 구성합니다.

CONFIG_AUTOFDO_CLANG=y

file 및 directory별 설정

72-95

사용자 지정

기본 `CONFIG_AUTOFDO_CLANG` 설정은 AutoFDO build의 kernel space object를 대상으로 합니다. 해당 kernel Makefile에 다음과 같은 줄을 추가하면 개별 file과 directory에서 AutoFDO build를 활성화하거나 비활성화할 수 있습니다.

단일 file(예: `foo.o`) 활성화:

AUTOFDO_PROFILE_foo.o := y

한 directory의 모든 file 활성화:

AUTOFDO_PROFILE := y

단일 file 비활성화:

AUTOFDO_PROFILE_foo.o := n

한 directory의 모든 file 비활성화:

AUTOFDO_PROFILE := n

workflow: 초기 kernel build

96-117

Workflow

다음은 AutoFDO kernel의 예제 workflow입니다.

1. host machine에서 LLVM을 활성화해 kernel을 build합니다. 예:

$ make menuconfig LLVM=1

AutoFDO build config를 활성화합니다.

CONFIG_AUTOFDO_CLANG=y

LLVM이 활성화된 configuration에서는 다음 command를 사용할 수 있습니다.

$ scripts/config -e AUTOFDO_CLANG

config를 얻은 뒤 다음과 같이 build합니다.

$ make LLVM=1

workflow: 설치와 perf 수집

118-143

2. test machine에 kernel을 설치합니다.

3. load test를 실행합니다. `perf`의 `-c` option은 sample event period를 지정합니다. 500009처럼 적절한 prime number를 사용하는 것이 좋습니다.

Intel platform:

$ perf record -e BR_INST_RETIRED.NEAR_TAKEN:k -a -N -b -c <count> -o <perf_file> -- <loadtest>

AMD platform은 BRS를 갖춘 Zen3 또는 `amd_lbr_v2`를 갖춘 Zen4 system을 지원합니다. 다음과 같이 확인합니다.

Zen3:

$ cat /proc/cpuinfo | grep " brs"

Zen4:

$ cat /proc/cpuinfo | grep amd_lbr_v2

다음 command로 perf data file을 생성합니다.

$ perf record --pfm-events RETIRED_TAKEN_BRANCH_INSTRUCTIONS:k -a -N -b -c <count> -o <perf_file> -- <loadtest>

workflow: AutoFDO profile 생성과 병합

144-164

4. 선택적으로 raw perf file을 host machine에 내려받습니다.

5. AutoFDO profile을 생성하는 offline 도구로 `create_llvm_prof`와 `llvm_profgen`을 사용할 수 있습니다. `create_llvm_prof`는 AutoFDO project의 일부이며 GitHub(https://github.com/google/autofdo)에서 v0.30.1 이상을 구할 수 있습니다. `llvm_profgen`은 LLVM compiler에 포함됩니다. `llvm_profgen` version은 Clang version과 일치할 필요가 없지만 LLVM 19 release의 Clang 이상 또는 LLVM trunk version이어야 합니다.

$ llvm-profgen --kernel --binary=<vmlinux> --perfdata=<perf_file> -o <profile_file>

또는 다음 command를 사용합니다.

$ create_llvm_prof --binary=<vmlinux> --profile=<perf_file> --format=extbinary --out=<profile_file>

여러 AutoFDO profile file은 다음과 같이 하나로 병합할 수 있습니다.

$ llvm-profdata merge -o <profile_file> <profile_1> <profile_2> ... <profile_n>

workflow: profile을 적용한 rebuild

165-168

6. 1단계와 같은 config로 AutoFDO profile file을 사용해 kernel을 다시 build합니다. `CONFIG_AUTOFDO_CLANG`이 활성화되어 있어야 합니다.

$ make LLVM=1 CLANG_AUTOFDO_PROFILE=<profile_file>