Commit 241ad43f authored by xiajingze's avatar xiajingze
Browse files

Compiler: Add clang's PGO support for kernel.

clang inclusion
category: feature
bugzilla: https://gitee.com/openeuler/kernel/issues/IA9FKR



--------------------------

Provides clang's Profile-Guided Optimization(PGO) for kernel.
When the option is opened, a representative workload is run, the raw
profile data can be collected from /sys/kernel/debug/pgo/vmlinux.profraw.
The raw profile data must be processed by clang's "llvm-profdata" tool
to be used during recompilation.

Collect and process the raw profile data

$ cp -a /sys/kernel/debug/pgo/vmlinux.profraw /tmp/vmlinux.profraw
$ llvm-profdata merge --output=vmlinux.profdata vmlinux.profraw

Rebuild the kernel using the processed profile data (PGO disabled)

$ make ... KCLAGS=-fprofile-use=vmlinux.profdata

kernel option(default is n):
CONFIG_PGO_CLANG=y

Signed-off-by: default avatarxiajingze <xiajingze1@huawei.com>
parent 79e1c0e4
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@ Documentation/dev-tools/testing-overview.rst
   kselftest
   kunit/index
   ktap
   pgo


.. only::  subproject and html
+127 −0
Original line number Diff line number Diff line
.. SPDX-License-Identifier: GPL-2.0

===============================
Using PGO with the Linux kernel
===============================

Clang's profiling kernel support (PGO_) enables profiling of the Linux kernel
when building with Clang. The profiling data is exported via the ``pgo``
debugfs directory.

.. _PGO: https://clang.llvm.org/docs/UsersManual.html#profile-guided-optimization


Preparation
===========

Configure the kernel with:

.. code-block:: make

   CONFIG_DEBUG_FS=y
   CONFIG_PGO_CLANG=y

Note that kernels compiled with profiling flags will be significantly larger
and run slower.

Profiling data will only become accessible once debugfs has been mounted:

.. code-block:: sh

   mount -t debugfs none /sys/kernel/debug


Customization
=============

You can enable or disable profiling for individual file and directories by
adding a line similar to the following to the respective kernel Makefile:

- For a single file (e.g. main.o)

  .. code-block:: make

     PGO_PROFILE_main.o := y

- For all files in one directory

  .. code-block:: make

     PGO_PROFILE := y

To exclude files from being profiled use

  .. code-block:: make

     PGO_PROFILE_main.o := n

and

  .. code-block:: make

     PGO_PROFILE := n

Only files which are linked to the main kernel image or are compiled as kernel
modules are supported by this mechanism.


Files
=====

The PGO kernel support creates the following files in debugfs:

``/sys/kernel/debug/pgo``
	Parent directory for all PGO-related files.

``/sys/kernel/debug/pgo/reset``
	Global reset file: resets all coverage data to zero when written to.

``/sys/kernel/debug/pgo/vmlinux.profraw``
	The raw PGO data that must be processed with ``llvm_profdata``.


Workflow
========

The PGO kernel can be run on the host or test machines. The data though should
be analyzed with Clang's tools from the same Clang version as the kernel was
compiled. Clang's tolerant of version skew, but it's easier to use the same
Clang version.

The profiling data is useful for optimizing the kernel, analyzing coverage,
etc. Clang offers tools to perform these tasks.

Here is an example workflow for profiling an instrumented kernel with PGO and
using the result to optimize the kernel:

1) Install the kernel on the TEST machine.

2) Reset the data counters right before running the load tests

   .. code-block:: sh

      echo 1 > /sys/kernel/debug/pgo/reset

3) Run the load tests.

4) Collect the raw profile data

   .. code-block:: sh

      cp -a /sys/kernel/debug/pgo/vmlinux.profraw /tmp/vmlinux.profraw

5) (Optional) Download the raw profile data to the HOST machine.

6) Process the raw profile data

   .. code-block:: sh

      llvm-profdata merge --output=vmlinux.profdata vmlinux.profraw

   Note that multiple raw profile data files can be merged during this step.

7) Rebuild the kernel using the processed profile data (PGO disabled)

   .. code-block:: sh

      make ... KCLAGS=-fprofile-use=vmlinux.profdata
+11 −0
Original line number Diff line number Diff line
@@ -16862,6 +16862,17 @@ S: Maintained
F:	include/linux/personality.h
F:	include/uapi/linux/personality.h
PGO BASED KERNEL PROFILING
M:	Sami Tolvanen <samitolvanen@google.com>
M:	Bill Wendling <wcw@google.com>
M:	Jingze Xia <xiajingze1@huawei.com>
M:	Haibo Jiang <jianghaibo9@huawei.com>
R:	Nathan Chancellor <natechancellor@gmail.com>
R:	Nick Desaulniers <ndesaulniers@google.com>
S:	Supported
F:	Documentation/dev-tools/pgo.rst
F:	kernel/pgo
PHOENIX RC FLIGHT CONTROLLER ADAPTER
M:	Marcus Folkesson <marcus.folkesson@gmail.com>
L:	linux-input@vger.kernel.org
+2 −0
Original line number Diff line number Diff line
@@ -753,6 +753,8 @@ endif # KBUILD_EXTMOD
# This allow a user to issue only 'make' to build a kernel including modules
# Defaults to vmlinux, but the arch makefile usually adds further targets
all: vmlinux
CFLAGS_PGO_CLANG := -fprofile-generate
export CFLAGS_PGO_CLANG

CFLAGS_GCOV	:= -fprofile-arcs -ftest-coverage
ifdef CONFIG_CC_IS_GCC
+1 −0
Original line number Diff line number Diff line
@@ -1502,6 +1502,7 @@ config ARCH_HAS_NONLEAF_PMD_YOUNG
	  may use this capability to reduce their search space.

source "kernel/gcov/Kconfig"
source "kernel/pgo/Kconfig"

source "scripts/gcc-plugins/Kconfig"

Loading