요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.
1. 요약·해설
원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.
2. 영어 원문 전체
번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.
원문 전체 펼치기
.. SPDX-License-Identifier: GPL-2.0
.. include:: <isonum.txt>
.. |intel_pstate| replace:: :doc:`intel_pstate <intel_pstate>`
=======================
CPU Performance Scaling
=======================
:Copyright: |copy| 2017 Intel Corporation
:Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
The Concept of CPU Performance Scaling
======================================
The majority of modern processors are capable of operating in a number of
different clock frequency and voltage configurations, often referred to as
Operating Performance Points or P-states (in ACPI terminology). As a rule,
the higher the clock frequency and the higher the voltage, the more instructions
can be retired by the CPU over a unit of time, but also the higher the clock
frequency and the higher the voltage, the more energy is consumed over a unit of
time (or the more power is drawn) by the CPU in the given P-state. Therefore
there is a natural tradeoff between the CPU capacity (the number of instructions
that can be executed over a unit of time) and the power drawn by the CPU.
In some situations it is desirable or even necessary to run the program as fast
as possible and then there is no reason to use any P-states different from the
highest one (i.e. the highest-performance frequency/voltage configuration
available). In some other cases, however, it may not be necessary to execute
instructions so quickly and maintaining the highest available CPU capacity for a
relatively long time without utilizing it entirely may be regarded as wasteful.
It also may not be physically possible to maintain maximum CPU capacity for too
long for thermal or power supply capacity reasons or similar. To cover those
cases, there are hardware interfaces allowing CPUs to be switched between
different frequency/voltage configurations or (in the ACPI terminology) to be
put into different P-states.
Typically, they are used along with algorithms to estimate the required CPU
capacity, so as to decide which P-states to put the CPUs into. Of course, since
the utilization of the system generally changes over time, that has to be done
repeatedly on a regular basis. The activity by which this happens is referred
to as CPU performance scaling or CPU frequency scaling (because it involves
adjusting the CPU clock frequency).
CPU Performance Scaling in Linux
================================
The Linux kernel supports CPU performance scaling by means of the ``CPUFreq``
(CPU Frequency scaling) subsystem that consists of three layers of code: the
core, scaling governors and scaling drivers.
The ``CPUFreq`` core provides the common code infrastructure and user space
interfaces for all platforms that support CPU performance scaling. It defines
the basic framework in which the other components operate.
Scaling governors implement algorithms to estimate the required CPU capacity.
As a rule, each governor implements one, possibly parametrized, scaling
algorithm.
Scaling drivers talk to the hardware. They provide scaling governors with
information on the available P-states (or P-state ranges in some cases) and
access platform-specific hardware interfaces to change CPU P-states as requested
by scaling governors.
In principle, all available scaling governors can be used with every scaling
driver. That design is based on the observation that the information used by
performance scaling algorithms for P-state selection can be represented in a
platform-independent form in the majority of cases, so it should be possible
to use the same performance scaling algorithm implemented in exactly the same
way regardless of which scaling driver is used. Consequently, the same set of
scaling governors should be suitable for every supported platform.
However, that observation may not hold for performance scaling algorithms
based on information provided by the hardware itself, for example through
feedback registers, as that information is typically specific to the hardware
interface it comes from and may not be easily represented in an abstract,
platform-independent way. For this reason, ``CPUFreq`` allows scaling drivers
to bypass the governor layer and implement their own performance scaling
algorithms. That is done by the |intel_pstate| scaling driver.
``CPUFreq`` Policy Objects
==========================
In some cases the hardware interface for P-state control is shared by multiple
CPUs. That is, for example, the same register (or set of registers) is used to
control the P-state of multiple CPUs at the same time and writing to it affects
all of those CPUs simultaneously.
Sets of CPUs sharing hardware P-state control interfaces are represented by
``CPUFreq`` as struct cpufreq_policy objects. For consistency,
struct cpufreq_policy is also used when there is only one CPU in the given
set.
The ``CPUFreq`` core maintains a pointer to a struct cpufreq_policy object for
every CPU in the system, including CPUs that are currently offline. If multiple
CPUs share the same hardware P-state control interface, all of the pointers
corresponding to them point to the same struct cpufreq_policy object.
``CPUFreq`` uses struct cpufreq_policy as its basic data type and the design
of its user space interface is based on the policy concept.
CPU Initialization
==================
First of all, a scaling driver has to be registered for ``CPUFreq`` to work.
It is only possible to register one scaling driver at a time, so the scaling
driver is expected to be able to handle all CPUs in the system.
The scaling driver may be registered before or after CPU registration. If
CPUs are registered earlier, the driver core invokes the ``CPUFreq`` core to
take a note of all of the already registered CPUs during the registration of the
scaling driver. In turn, if any CPUs are registered after the registration of
the scaling driver, the ``CPUFreq`` core will be invoked to take note of them
at their registration time.
In any case, the ``CPUFreq`` core is invoked to take note of any logical CPU it
has not seen so far as soon as it is ready to handle that CPU. [Note that the
logical CPU may be a physical single-core processor, or a single core in a
multicore processor, or a hardware thread in a physical processor or processor
core. In what follows "CPU" always means "logical CPU" unless explicitly stated
otherwise and the word "processor" is used to refer to the physical part
possibly including multiple logical CPUs.]
Once invoked, the ``CPUFreq`` core checks if the policy pointer is already set
for the given CPU and if so, it skips the policy object creation. Otherwise,
a new policy object is created and initialized, which involves the creation of
a new policy directory in ``sysfs``, and the policy pointer corresponding to
the given CPU is set to the new policy object's address in memory.
Next, the scaling driver's ``->init()`` callback is invoked with the policy
pointer of the new CPU passed to it as the argument. That callback is expected
to initialize the performance scaling hardware interface for the given CPU (or,
more precisely, for the set of CPUs sharing the hardware interface it belongs
to, represented by its policy object) and, if the policy object it has been
called for is new, to set parameters of the policy, like the minimum and maximum
frequencies supported by the hardware, the table of available frequencies (if
the set of supported P-states is not a continuous range), and the mask of CPUs
that belong to the same policy (including both online and offline CPUs). That
mask is then used by the core to populate the policy pointers for all of the
CPUs in it.
The next major initialization step for a new policy object is to attach a
scaling governor to it (to begin with, that is the default scaling governor
determined by the kernel command line or configuration, but it may be changed
later via ``sysfs``). First, a pointer to the new policy object is passed to
the governor's ``->init()`` callback which is expected to initialize all of the
data structures necessary to handle the given policy and, possibly, to add
a governor ``sysfs`` interface to it. Next, the governor is started by
invoking its ``->start()`` callback.
That callback is expected to register per-CPU utilization update callbacks for
all of the online CPUs belonging to the given policy with the CPU scheduler.
The utilization update callbacks will be invoked by the CPU scheduler on
important events, like task enqueue and dequeue, on every iteration of the
scheduler tick or generally whenever the CPU utilization may change (from the
scheduler's perspective). They are expected to carry out computations needed
to determine the P-state to use for the given policy going forward and to
invoke the scaling driver to make changes to the hardware in accordance with
the P-state selection. The scaling driver may be invoked directly from
scheduler context or asynchronously, via a kernel thread or workqueue, depending
on the configuration and capabilities of the scaling driver and the governor.
Similar steps are taken for policy objects that are not new, but were "inactive"
previously, meaning that all of the CPUs belonging to them were offline. The
only practical difference in that case is that the ``CPUFreq`` core will attempt
to use the scaling governor previously used with the policy that became
"inactive" (and is re-initialized now) instead of the default governor.
In turn, if a previously offline CPU is being brought back online, but some
other CPUs sharing the policy object with it are online already, there is no
need to re-initialize the policy object at all. In that case, it only is
necessary to restart the scaling governor so that it can take the new online CPU
into account. That is achieved by invoking the governor's ``->stop`` and
``->start()`` callbacks, in this order, for the entire policy.
As mentioned before, the |intel_pstate| scaling driver bypasses the scaling
governor layer of ``CPUFreq`` and provides its own P-state selection algorithms.
Consequently, if |intel_pstate| is used, scaling governors are not attached to
new policy objects. Instead, the driver's ``->setpolicy()`` callback is invoked
to register per-CPU utilization update callbacks for each policy. These
callbacks are invoked by the CPU scheduler in the same way as for scaling
governors, but in the |intel_pstate| case they both determine the P-state to
use and change the hardware configuration accordingly in one go from scheduler
context.
The policy objects created during CPU initialization and other data structures
associated with them are torn down when the scaling driver is unregistered
(which happens when the kernel module containing it is unloaded, for example) or
when the last CPU belonging to the given policy in unregistered.
Policy Interface in ``sysfs``
=============================
During the initialization of the kernel, the ``CPUFreq`` core creates a
``sysfs`` directory (kobject) called ``cpufreq`` under
:file:`/sys/devices/system/cpu/`.
That directory contains a ``policyX`` subdirectory (where ``X`` represents an
integer number) for every policy object maintained by the ``CPUFreq`` core.
Each ``policyX`` directory is pointed to by ``cpufreq`` symbolic links
under :file:`/sys/devices/system/cpu/cpuY/` (where ``Y`` represents an integer
that may be different from the one represented by ``X``) for all of the CPUs
associated with (or belonging to) the given policy. The ``policyX`` directories
in :file:`/sys/devices/system/cpu/cpufreq` each contain policy-specific
attributes (files) to control ``CPUFreq`` behavior for the corresponding policy
objects (that is, for all of the CPUs associated with them).
Some of those attributes are generic. They are created by the ``CPUFreq`` core
and their behavior generally does not depend on what scaling driver is in use
and what scaling governor is attached to the given policy. Some scaling drivers
also add driver-specific attributes to the policy directories in ``sysfs`` to
control policy-specific aspects of driver behavior.
The generic attributes under :file:`/sys/devices/system/cpu/cpufreq/policyX/`
are the following:
``affected_cpus``
List of online CPUs belonging to this policy (i.e. sharing the hardware
performance scaling interface represented by the ``policyX`` policy
object).
``bios_limit``
If the platform firmware (BIOS) tells the OS to apply an upper limit to
CPU frequencies, that limit will be reported through this attribute (if
present).
The existence of the limit may be a result of some (often unintentional)
BIOS settings, restrictions coming from a service processor or other
BIOS/HW-based mechanisms.
This does not cover ACPI thermal limitations which can be discovered
through a generic thermal driver.
This attribute is not present if the scaling driver in use does not
support it.
``cpuinfo_cur_freq``
Current frequency of the CPUs belonging to this policy as obtained from
the hardware (in KHz).
This is expected to be the frequency the hardware actually runs at.
If that frequency cannot be determined, this attribute should not
be present.
``cpuinfo_avg_freq``
An average frequency (in KHz) of all CPUs belonging to a given policy,
derived from a hardware provided feedback and reported on a time frame
spanning at most few milliseconds.
This is expected to be based on the frequency the hardware actually runs
at and, as such, might require specialised hardware support (such as AMU
extension on ARM). If one cannot be determined, this attribute should
not be present.
Note that failed attempt to retrieve current frequency for a given
CPU(s) will result in an appropriate error, i.e.: EAGAIN for CPU that
remains idle (raised on ARM).
``cpuinfo_max_freq``
Maximum possible operating frequency the CPUs belonging to this policy
can run at (in kHz).
``cpuinfo_min_freq``
Minimum possible operating frequency the CPUs belonging to this policy
can run at (in kHz).
``cpuinfo_transition_latency``
The time it takes to switch the CPUs belonging to this policy from one
P-state to another, in nanoseconds.
``related_cpus``
List of all (online and offline) CPUs belonging to this policy.
``scaling_available_frequencies``
List of available frequencies of the CPUs belonging to this policy
(in kHz).
``scaling_available_governors``
List of ``CPUFreq`` scaling governors present in the kernel that can
be attached to this policy or (if the |intel_pstate| scaling driver is
in use) list of scaling algorithms provided by the driver that can be
applied to this policy.
[Note that some governors are modular and it may be necessary to load a
kernel module for the governor held by it to become available and be
listed by this attribute.]
``scaling_cur_freq``
Current frequency of all of the CPUs belonging to this policy (in kHz).
In the majority of cases, this is the frequency of the last P-state
requested by the scaling driver from the hardware using the scaling
interface provided by it, which may or may not reflect the frequency
the CPU is actually running at (due to hardware design and other
limitations).
Some architectures (e.g. ``x86``) may attempt to provide information
more precisely reflecting the current CPU frequency through this
attribute, but that still may not be the exact current CPU frequency as
seen by the hardware at the moment. This behavior though, is only
available via c:macro:``CPUFREQ_ARCH_CUR_FREQ`` option.
``scaling_driver``
The scaling driver currently in use.
``scaling_governor``
The scaling governor currently attached to this policy or (if the
|intel_pstate| scaling driver is in use) the scaling algorithm
provided by the driver that is currently applied to this policy.
This attribute is read-write and writing to it will cause a new scaling
governor to be attached to this policy or a new scaling algorithm
provided by the scaling driver to be applied to it (in the
|intel_pstate| case), as indicated by the string written to this
attribute (which must be one of the names listed by the
``scaling_available_governors`` attribute described above).
``scaling_max_freq``
Maximum frequency the CPUs belonging to this policy are allowed to be
running at (in kHz).
This attribute is read-write and writing a string representing an
integer to it will cause a new limit to be set (it must not be lower
than the value of the ``scaling_min_freq`` attribute).
``scaling_min_freq``
Minimum frequency the CPUs belonging to this policy are allowed to be
running at (in kHz).
This attribute is read-write and writing a string representing a
non-negative integer to it will cause a new limit to be set (it must not
be higher than the value of the ``scaling_max_freq`` attribute).
``scaling_setspeed``
This attribute is functional only if the `userspace`_ scaling governor
is attached to the given policy.
It returns the last frequency requested by the governor (in kHz) or can
be written to in order to set a new frequency for the policy.
Generic Scaling Governors
=========================
``CPUFreq`` provides generic scaling governors that can be used with all
scaling drivers. As stated before, each of them implements a single, possibly
parametrized, performance scaling algorithm.
Scaling governors are attached to policy objects and different policy objects
can be handled by different scaling governors at the same time (although that
may lead to suboptimal results in some cases).
The scaling governor for a given policy object can be changed at any time with
the help of the ``scaling_governor`` policy attribute in ``sysfs``.
Some governors expose ``sysfs`` attributes to control or fine-tune the scaling
algorithms implemented by them. Those attributes, referred to as governor
tunables, can be either global (system-wide) or per-policy, depending on the
scaling driver in use. If the driver requires governor tunables to be
per-policy, they are located in a subdirectory of each policy directory.
Otherwise, they are located in a subdirectory under
:file:`/sys/devices/system/cpu/cpufreq/`. In either case the name of the
subdirectory containing the governor tunables is the name of the governor
providing them.
``performance``
---------------
When attached to a policy object, this governor causes the highest frequency,
within the ``scaling_max_freq`` policy limit, to be requested for that policy.
The request is made once at that time the governor for the policy is set to
``performance`` and whenever the ``scaling_max_freq`` or ``scaling_min_freq``
policy limits change after that.
``powersave``
-------------
When attached to a policy object, this governor causes the lowest frequency,
within the ``scaling_min_freq`` policy limit, to be requested for that policy.
The request is made once at that time the governor for the policy is set to
``powersave`` and whenever the ``scaling_max_freq`` or ``scaling_min_freq``
policy limits change after that.
``userspace``
-------------
This governor does not do anything by itself. Instead, it allows user space
to set the CPU frequency for the policy it is attached to by writing to the
``scaling_setspeed`` attribute of that policy. Though the intention may be to
set an exact frequency for the policy, the actual frequency may vary depending
on hardware coordination, thermal and power limits, and other factors.
``schedutil``
-------------
This governor uses CPU utilization data available from the CPU scheduler. It
generally is regarded as a part of the CPU scheduler, so it can access the
scheduler's internal data structures directly.
It runs entirely in scheduler context, although in some cases it may need to
invoke the scaling driver asynchronously when it decides that the CPU frequency
should be changed for a given policy (that depends on whether or not the driver
is capable of changing the CPU frequency from scheduler context).
The actions of this governor for a particular CPU depend on the scheduling class
invoking its utilization update callback for that CPU. If it is invoked by the
RT or deadline scheduling classes, the governor will increase the frequency to
the allowed maximum (that is, the ``scaling_max_freq`` policy limit). In turn,
if it is invoked by the CFS scheduling class, the governor will use the
Per-Entity Load Tracking (PELT) metric for the root control group of the
given CPU as the CPU utilization estimate (see the *Per-entity load tracking*
LWN.net article [1]_ for a description of the PELT mechanism). Then, the new
CPU frequency to apply is computed in accordance with the formula
f = 1.25 * ``f_0`` * ``util`` / ``max``
where ``util`` is the PELT number, ``max`` is the theoretical maximum of
``util``, and ``f_0`` is either the maximum possible CPU frequency for the given
policy (if the PELT number is frequency-invariant), or the current CPU frequency
(otherwise).
This governor also employs a mechanism allowing it to temporarily bump up the
CPU frequency for tasks that have been waiting on I/O most recently, called
"IO-wait boosting". That happens when the :c:macro:`SCHED_CPUFREQ_IOWAIT` flag
is passed by the scheduler to the governor callback which causes the frequency
to go up to the allowed maximum immediately and then draw back to the value
returned by the above formula over time.
This governor exposes only one tunable:
``rate_limit_us``
Minimum time (in microseconds) that has to pass between two consecutive
runs of governor computations (default: 1.5 times the scaling driver's
transition latency or the maximum 2ms).
The purpose of this tunable is to reduce the scheduler context overhead
of the governor which might be excessive without it.
This governor generally is regarded as a replacement for the older `ondemand`_
and `conservative`_ governors (described below), as it is simpler and more
tightly integrated with the CPU scheduler, its overhead in terms of CPU context
switches and similar is less significant, and it uses the scheduler's own CPU
utilization metric, so in principle its decisions should not contradict the
decisions made by the other parts of the scheduler.
``ondemand``
------------
This governor uses CPU load as a CPU frequency selection metric.
In order to estimate the current CPU load, it measures the time elapsed between
consecutive invocations of its worker routine and computes the fraction of that
time in which the given CPU was not idle. The ratio of the non-idle (active)
time to the total CPU time is taken as an estimate of the load.
If this governor is attached to a policy shared by multiple CPUs, the load is
estimated for all of them and the greatest result is taken as the load estimate
for the entire policy.
The worker routine of this governor has to run in process context, so it is
invoked asynchronously (via a workqueue) and CPU P-states are updated from
there if necessary. As a result, the scheduler context overhead from this
governor is minimum, but it causes additional CPU context switches to happen
relatively often and the CPU P-state updates triggered by it can be relatively
irregular. Also, it affects its own CPU load metric by running code that
reduces the CPU idle time (even though the CPU idle time is only reduced very
slightly by it).
It generally selects CPU frequencies proportional to the estimated load, so that
the value of the ``cpuinfo_max_freq`` policy attribute corresponds to the load of
1 (or 100%), and the value of the ``cpuinfo_min_freq`` policy attribute
corresponds to the load of 0, unless when the load exceeds a (configurable)
speedup threshold, in which case it will go straight for the highest frequency
it is allowed to use (the ``scaling_max_freq`` policy limit).
This governor exposes the following tunables:
``sampling_rate``
This is how often the governor's worker routine should run, in
microseconds.
Typically, it is set to values of the order of 2000 (2 ms). Its
default value is to add a 50% breathing room
to ``cpuinfo_transition_latency`` on each policy this governor is
attached to. The minimum is typically the length of two scheduler
ticks.
If this tunable is per-policy, the following shell command sets the time
represented by it to be 1.5 times as high as the transition latency
(the default)::
# echo `$(($(cat cpuinfo_transition_latency) * 3 / 2))` > ondemand/sampling_rate
``up_threshold``
If the estimated CPU load is above this value (in percent), the governor
will set the frequency to the maximum value allowed for the policy.
Otherwise, the selected frequency will be proportional to the estimated
CPU load.
``ignore_nice_load``
If set to 1 (default 0), it will cause the CPU load estimation code to
treat the CPU time spent on executing tasks with "nice" levels greater
than 0 as CPU idle time.
This may be useful if there are tasks in the system that should not be
taken into account when deciding what frequency to run the CPUs at.
Then, to make that happen it is sufficient to increase the "nice" level
of those tasks above 0 and set this attribute to 1.
``sampling_down_factor``
Temporary multiplier, between 1 (default) and 100 inclusive, to apply to
the ``sampling_rate`` value if the CPU load goes above ``up_threshold``.
This causes the next execution of the governor's worker routine (after
setting the frequency to the allowed maximum) to be delayed, so the
frequency stays at the maximum level for a longer time.
Frequency fluctuations in some bursty workloads may be avoided this way
at the cost of additional energy spent on maintaining the maximum CPU
capacity.
``powersave_bias``
Reduction factor to apply to the original frequency target of the
governor (including the maximum value used when the ``up_threshold``
value is exceeded by the estimated CPU load) or sensitivity threshold
for the AMD frequency sensitivity powersave bias driver
(:file:`drivers/cpufreq/amd_freq_sensitivity.c`), between 0 and 1000
inclusive.
If the AMD frequency sensitivity powersave bias driver is not loaded,
the effective frequency to apply is given by
f * (1 - ``powersave_bias`` / 1000)
where f is the governor's original frequency target. The default value
of this attribute is 0 in that case.
If the AMD frequency sensitivity powersave bias driver is loaded, the
value of this attribute is 400 by default and it is used in a different
way.
On Family 16h (and later) AMD processors there is a mechanism to get a
measured workload sensitivity, between 0 and 100% inclusive, from the
hardware. That value can be used to estimate how the performance of the
workload running on a CPU will change in response to frequency changes.
The performance of a workload with the sensitivity of 0 (memory-bound or
IO-bound) is not expected to increase at all as a result of increasing
the CPU frequency, whereas workloads with the sensitivity of 100%
(CPU-bound) are expected to perform much better if the CPU frequency is
increased.
If the workload sensitivity is less than the threshold represented by
the ``powersave_bias`` value, the sensitivity powersave bias driver
will cause the governor to select a frequency lower than its original
target, so as to avoid over-provisioning workloads that will not benefit
from running at higher CPU frequencies.
``conservative``
----------------
This governor uses CPU load as a CPU frequency selection metric.
It estimates the CPU load in the same way as the `ondemand`_ governor described
above, but the CPU frequency selection algorithm implemented by it is different.
Namely, it avoids changing the frequency significantly over short time intervals
which may not be suitable for systems with limited power supply capacity (e.g.
battery-powered). To achieve that, it changes the frequency in relatively
small steps, one step at a time, up or down - depending on whether or not a
(configurable) threshold has been exceeded by the estimated CPU load.
This governor exposes the following tunables:
``freq_step``
Frequency step in percent of the maximum frequency the governor is
allowed to set (the ``scaling_max_freq`` policy limit), between 0 and
100 (5 by default).
This is how much the frequency is allowed to change in one go. Setting
it to 0 will cause the default frequency step (5 percent) to be used
and setting it to 100 effectively causes the governor to periodically
switch the frequency between the ``scaling_min_freq`` and
``scaling_max_freq`` policy limits.
``down_threshold``
Threshold value (in percent, 20 by default) used to determine the
frequency change direction.
If the estimated CPU load is greater than this value, the frequency will
go up (by ``freq_step``). If the load is less than this value (and the
``sampling_down_factor`` mechanism is not in effect), the frequency will
go down. Otherwise, the frequency will not be changed.
``sampling_down_factor``
Frequency decrease deferral factor, between 1 (default) and 10
inclusive.
It effectively causes the frequency to go down ``sampling_down_factor``
times slower than it ramps up.
Frequency Boost Support
=======================
Background
----------
Some processors support a mechanism to raise the operating frequency of some
cores in a multicore package temporarily (and above the sustainable frequency
threshold for the whole package) under certain conditions, for example if the
whole chip is not fully utilized and below its intended thermal or power budget.
Different names are used by different vendors to refer to this functionality.
For Intel processors it is referred to as "Turbo Boost", AMD calls it
"Turbo-Core" or (in technical documentation) "Core Performance Boost" and so on.
As a rule, it also is implemented differently by different vendors. The simple
term "frequency boost" is used here for brevity to refer to all of those
implementations.
The frequency boost mechanism may be either hardware-based or software-based.
If it is hardware-based (e.g. on x86), the decision to trigger the boosting is
made by the hardware (although in general it requires the hardware to be put
into a special state in which it can control the CPU frequency within certain
limits). If it is software-based (e.g. on ARM), the scaling driver decides
whether or not to trigger boosting and when to do that.
The ``boost`` File in ``sysfs``
-------------------------------
This file is located under :file:`/sys/devices/system/cpu/cpufreq/` and controls
the "boost" setting for the whole system. It is not present if the underlying
scaling driver does not support the frequency boost mechanism (or supports it,
but provides a driver-specific interface for controlling it, like
|intel_pstate|).
If the value in this file is 1, the frequency boost mechanism is enabled. This
means that either the hardware can be put into states in which it is able to
trigger boosting (in the hardware-based case), or the software is allowed to
trigger boosting (in the software-based case). It does not mean that boosting
is actually in use at the moment on any CPUs in the system. It only means a
permission to use the frequency boost mechanism (which still may never be used
for other reasons).
If the value in this file is 0, the frequency boost mechanism is disabled and
cannot be used at all.
The only values that can be written to this file are 0 and 1.
Rationale for Boost Control Knob
--------------------------------
The frequency boost mechanism is generally intended to help to achieve optimum
CPU performance on time scales below software resolution (e.g. below the
scheduler tick interval) and it is demonstrably suitable for many workloads, but
it may lead to problems in certain situations.
For this reason, many systems make it possible to disable the frequency boost
mechanism in the platform firmware (BIOS) setup, but that requires the system to
be restarted for the setting to be adjusted as desired, which may not be
practical at least in some cases. For example:
1. Boosting means overclocking the processor, although under controlled
conditions. Generally, the processor's energy consumption increases
as a result of increasing its frequency and voltage, even temporarily.
That may not be desirable on systems that switch to power sources of
limited capacity, such as batteries, so the ability to disable the boost
mechanism while the system is running may help there (but that depends on
the workload too).
2. In some situations deterministic behavior is more important than
performance or energy consumption (or both) and the ability to disable
boosting while the system is running may be useful then.
3. To examine the impact of the frequency boost mechanism itself, it is useful
to be able to run tests with and without boosting, preferably without
restarting the system in the meantime.
4. Reproducible results are important when running benchmarks. Since
the boosting functionality depends on the load of the whole package,
single-thread performance may vary because of it which may lead to
unreproducible results sometimes. That can be avoided by disabling the
frequency boost mechanism before running benchmarks sensitive to that
issue.
Legacy AMD ``cpb`` Knob
-----------------------
The AMD powernow-k8 scaling driver supports a ``sysfs`` knob very similar to
the global ``boost`` one. It is used for disabling/enabling the "Core
Performance Boost" feature of some AMD processors.
If present, that knob is located in every ``CPUFreq`` policy directory in
``sysfs`` (:file:`/sys/devices/system/cpu/cpufreq/policyX/`) and is called
``cpb``, which indicates a more fine grained control interface. The actual
implementation, however, works on the system-wide basis and setting that knob
for one policy causes the same value of it to be set for all of the other
policies at the same time.
That knob is still supported on AMD processors that support its underlying
hardware feature, but it may be configured out of the kernel (via the
:c:macro:`CONFIG_X86_ACPI_CPUFREQ_CPB` configuration option) and the global
``boost`` knob is present regardless. Thus it is always possible use the
``boost`` knob instead of the ``cpb`` one which is highly recommended, as that
is more consistent with what all of the other systems do (and the ``cpb`` knob
may not be supported any more in the future).
The ``cpb`` knob is never present for any processors without the underlying
hardware feature (e.g. all Intel ones), even if the
:c:macro:`CONFIG_X86_ACPI_CPUFREQ_CPB` configuration option is set.
References
==========
.. [1] Jonathan Corbet, *Per-entity load tracking*,
https://lwn.net/Articles/531853/
3. 한국어 전문 번역
영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.
문서 정보
1-14이 문서는 `SPDX-License-Identifier: GPL-2.0`을 따르고 2017 Intel Corporation 저작물이며, 작성자는 Rafael J. Wysocki `<rafael.j.wysocki@intel.com>`입니다.
문서 안의 `intel_pstate` 표기는 별도 `intel_pstate` 문서로 연결됩니다.
CPU performance scaling 개념
15-47현대 processor 대부분은 여러 clock frequency와 voltage 조합, 즉 Operating Performance Point 또는 ACPI P-state로 동작할 수 있습니다. Frequency와 voltage가 높을수록 단위 시간에 더 많은 instruction을 완료하지만 energy 소비와 power draw도 커집니다.
따라서 CPU capacity와 power 사이에는 자연스러운 절충이 있습니다. 최대한 빨리 끝내야 하는 작업은 최고 P-state가 적합하지만, capacity를 오래 사용하지 않으면서 최고 상태를 유지하면 낭비이고 thermal·power supply 한계 때문에 물리적으로 지속 불가능할 수도 있습니다.
Hardware interface는 CPU를 frequency/voltage 조합 사이에서 전환합니다. 필요한 capacity를 추정하는 algorithm이 P-state를 선택하고, utilization이 계속 바뀌므로 이를 정기적으로 반복합니다. 이 활동을 CPU performance scaling 또는 CPU frequency scaling이라 합니다.
Linux CPUFreq 세 계층
48-84Linux kernel의 `CPUFreq` subsystem은 core, scaling governor, scaling driver 세 계층으로 CPU performance scaling을 지원합니다.
| 계층 | 책임 |
|---|---|
| CPUFreq core | 공통 infrastructure와 userspace interface |
| Scaling governor | 필요 CPU capacity를 추정하는 algorithm |
| Scaling driver | P-state 정보 제공과 hardware interface 제어 |
원칙적으로 platform-independent 정보로 P-state 선택을 표현할 수 있어 모든 governor를 모든 driver와 조합할 수 있습니다. 같은 algorithm을 hardware driver와 무관하게 재사용하는 설계입니다.
다만 feedback register처럼 hardware 고유 정보에 기반한 algorithm은 추상화하기 어렵습니다. 그래서 CPUFreq는 driver가 governor 계층을 우회해 자체 algorithm을 구현할 수 있게 하며 `intel_pstate`가 이 방식을 사용합니다.
struct cpufreq_policy
85-106여러 CPU가 같은 P-state control register를 공유하면 하나를 쓰는 순간 모두가 동시에 영향을 받습니다. CPUFreq는 이런 CPU 집합을 `struct cpufreq_policy` object로 표현하며, CPU 하나뿐인 집합에도 일관되게 같은 type을 씁니다.
Core는 offline CPU까지 system의 모든 CPU에 policy pointer를 유지합니다. Hardware interface를 공유하는 CPU들의 pointer는 같은 object를 가리킵니다. 이 object가 CPUFreq의 기본 data type이며 userspace interface도 policy 개념을 기반으로 합니다.
CPU와 policy 초기화
107-196CPUFreq가 동작하려면 먼저 scaling driver 하나가 등록돼야 합니다. 한 번에 하나만 등록할 수 있으므로 system의 모든 CPU를 처리할 수 있어야 합니다. Driver와 CPU 중 어느 쪽이 먼저 등록돼도 core가 이미 등록된 CPU 또는 새 CPU를 발견해 기록합니다.
이 문서에서 CPU는 별도 언급이 없으면 logical CPU를 뜻하며 single-core processor, multicore의 core, hardware thread일 수 있습니다. Processor는 여러 logical CPU를 포함할 수 있는 물리 부품을 뜻합니다.
새 CPU에 policy pointer가 이미 있으면 object 생성을 건너뜁니다. 없으면 새 object와 sysfs policy directory를 만들고 CPU pointer를 설정합니다.
그다음 driver의 `->init()` callback이 호출됩니다. Callback은 공유 hardware interface를 초기화하고, 새 policy라면 hardware 최소·최대 frequency, discrete P-state의 frequency table, online/offline을 포함한 공유 CPU mask를 설정합니다. Core는 mask의 모든 CPU pointer를 채웁니다.
새 policy에는 kernel command line/configuration이 정한 기본 governor를 연결합니다. Governor `->init()`가 data structure와 선택적 sysfs interface를 준비하고 `->start()`가 시작합니다.
`->start()`는 online CPU마다 scheduler utilization update callback을 등록합니다. Task enqueue/dequeue, scheduler tick 등 utilization 변화 때 callback이 P-state 계산을 수행하고 driver에 hardware 변경을 요청합니다. Driver는 capability에 따라 scheduler context에서 직접 또는 kernel thread/workqueue로 비동기 호출됩니다.
모든 CPU가 offline이어서 inactive였다가 되살아난 policy는 기본 governor 대신 이전 governor를 재사용하려 합니다. 공유 policy의 다른 CPU가 이미 online이면 object 재초기화 없이 전체 policy에 governor `->stop()` 후 `->start()`를 호출해 새 CPU만 반영합니다.
`intel_pstate`는 governor를 연결하지 않고 driver `->setpolicy()`가 policy별 utilization callback을 등록합니다. Callback이 scheduler context에서 P-state 결정과 hardware 변경을 함께 수행합니다.
Scaling driver가 unregister되거나 policy의 마지막 CPU가 unregister되면 policy object와 연결 data structure를 해제합니다.
Policy sysfs ABI
197-347Kernel 초기화 때 CPUFreq core는 `/sys/devices/system/cpu/cpufreq/`를 만들고 policy마다 `policyX` subdirectory를 둡니다. 관련 CPU의 `/sys/devices/system/cpu/cpuY/cpufreq` symlink가 이 directory를 가리킵니다.
Policy directory의 generic attribute는 core가 만들며 driver/governor와 대체로 무관합니다. Driver는 policy 고유 동작을 위한 attribute를 추가할 수 있습니다.
| Attribute | 의미 |
|---|---|
| `affected_cpus` | Policy의 online CPU |
| `bios_limit` | Firmware가 요구한 frequency 상한 |
| `cpuinfo_cur_freq` | Hardware에서 얻은 실제 현재 frequency |
| `cpuinfo_avg_freq` | 수 ms 이하 구간의 hardware feedback 평균 |
| `cpuinfo_max_freq` | 가능한 최대 operating frequency |
| `cpuinfo_min_freq` | 가능한 최소 operating frequency |
| `cpuinfo_transition_latency` | P-state 전환 시간(ns) |
| `related_cpus` | Policy의 online/offline CPU 전체 |
| `scaling_available_frequencies` | 사용 가능한 frequency 목록 |
| `scaling_available_governors` | 연결 가능한 governor/driver algorithm |
| `scaling_cur_freq` | 마지막 요청값 또는 architecture가 추정한 현재값 |
| `scaling_driver` | 현재 scaling driver |
| `scaling_governor` | 현재 governor/driver algorithm; read-write |
| `scaling_max_freq` | 허용 최대 frequency; read-write |
| `scaling_min_freq` | 허용 최소 frequency; read-write |
| `scaling_setspeed` | `userspace` governor의 요청 frequency |
`bios_limit`은 BIOS setting, service processor 등 firmware/HW mechanism의 상한이며 generic thermal driver가 찾는 ACPI thermal limit은 포함하지 않습니다. Driver가 지원하지 않으면 파일도 없습니다.
`cpuinfo_avg_freq`는 AMU 같은 전용 hardware가 필요할 수 있고 current frequency 조회 실패 시 ARM idle CPU처럼 `EAGAIN`이 날 수 있습니다.
`scaling_cur_freq`는 보통 hardware에 마지막으로 요청한 P-state frequency여서 실제 값과 다를 수 있습니다. X86 등은 :c:macro:`CPUFREQ_ARCH_CUR_FREQ`로 더 가까운 값을 제공할 수 있지만 순간 hardware 값과 정확히 같다는 보장은 없습니다.
`scaling_governor`에는 `scaling_available_governors`의 이름만 쓸 수 있습니다. `scaling_max_freq`는 `scaling_min_freq`보다 낮을 수 없고, 최소값은 최대값보다 높을 수 없습니다. `scaling_setspeed`는 `userspace` governor에서만 동작합니다.
Generic governor와 tunable 위치
348-371CPUFreq의 generic governor는 모든 scaling driver와 사용할 수 있으며 하나의, 경우에 따라 parameter화된 algorithm을 구현합니다. Policy마다 다른 governor를 동시에 쓸 수 있지만 결과가 최적이 아닐 수 있습니다.
`scaling_governor`로 언제든 바꿀 수 있습니다. Driver가 per-policy tunable을 요구하면 각 policy 아래 governor 이름 directory에, 그렇지 않으면 `/sys/devices/system/cpu/cpufreq/<governor>/`에 system-wide tunable을 둡니다.
| Governor | 선택 방식 |
|---|---|
| `performance` | `scaling_max_freq` 안의 최고값 |
| `powersave` | `scaling_min_freq` 안의 최저값 |
| `userspace` | `scaling_setspeed`에 userspace가 쓰는 값 |
| `schedutil` | Scheduler utilization과 PELT |
| `ondemand` | 비동기 worker가 계산한 active-time load |
| `conservative` | Load threshold와 작은 단계의 점진적 변경 |
performance, powersave와 userspace
372-400`performance`는 policy 설정 시와 min/max limit 변경 때 `scaling_max_freq` 범위의 최고 frequency를 한 번 요청합니다.
`powersave`는 같은 시점에 `scaling_min_freq` 범위의 최저 frequency를 요청합니다.
`userspace`는 스스로 결정하지 않고 userspace가 `scaling_setspeed`에 써서 frequency를 지정하게 합니다. 정확한 값을 의도해도 hardware coordination, thermal·power limit 등으로 실제 frequency는 달라질 수 있습니다.
schedutil
401-453`schedutil`은 CPU scheduler의 utilization data를 사용하고 scheduler 내부 구조에 직접 접근합니다. 전부 scheduler context에서 실행하지만 driver가 그 context에서 frequency를 바꾸지 못하면 비동기로 호출합니다.
RT 또는 deadline class가 callback을 부르면 허용 최대 `scaling_max_freq`로 올립니다. CFS는 root control group의 Per-Entity Load Tracking(PELT) metric을 utilization 추정값으로 사용합니다.
f = 1.25 * ``f_0`` * ``util`` / ``max``
| 항목 | 수식에서의 의미 |
|---|---|
| `util` | Root control group의 PELT utilization |
| `max` | `util`의 이론적 최대값 |
| `f_0` | Frequency-invariant이면 policy 최대값, 아니면 현재 frequency |
| `rate_limit_us` | 연속 계산 사이 최소 시간; 기본 transition latency x 1.5 또는 최대 2 ms |
IO-wait boosting은 scheduler가 :c:macro:`SCHED_CPUFREQ_IOWAIT`를 넘길 때 즉시 최대 frequency로 올린 뒤 수식 값으로 서서히 되돌립니다.
`rate_limit_us`는 연속 계산 사이 최소 시간으로 scheduler context overhead를 줄입니다. `schedutil`은 단순하고 scheduler와 긴밀하며 context switch overhead가 작고 scheduler와 같은 utilization metric을 써서 `ondemand`와 `conservative`의 대체로 간주됩니다.
ondemand
454-567`ondemand`는 worker 연속 실행 사이의 전체 시간 중 non-idle 비율을 CPU load로 봅니다. 여러 CPU가 policy를 공유하면 가장 큰 load를 policy 추정값으로 사용합니다.
Worker는 process context의 workqueue에서 비동기로 실행됩니다. Scheduler overhead는 작지만 context switch가 자주 생기고 P-state update가 불규칙할 수 있으며, worker 자신도 idle time을 조금 줄여 metric에 영향을 줍니다.
일반적으로 load에 비례해 frequency를 선택하되 load가 `up_threshold`를 넘으면 즉시 `scaling_max_freq`로 갑니다.
| Tunable | 동작 |
|---|---|
| `sampling_rate` | Worker 실행 주기(us) |
| `up_threshold` | 초과 시 허용 최고 frequency로 이동하는 load 비율 |
| `ignore_nice_load` | 양수 nice task 시간을 idle로 취급 |
| `sampling_down_factor` | 최고 frequency 유지 시간을 늘리는 임시 배수 1-100 |
| `powersave_bias` | 목표 감소율 또는 AMD sensitivity threshold 0-1000 |
`sampling_rate` 기본값은 각 policy `cpuinfo_transition_latency`에 50% 여유를 더한 값이고 최소값은 보통 scheduler tick 두 번 길이입니다. Per-policy일 때 기본 비율로 설정하는 원문 명령입니다.
# echo `$(($(cat cpuinfo_transition_latency) * 3 / 2))` > ondemand/sampling_rate
`sampling_down_factor`는 bursty workload의 frequency fluctuation을 줄이는 대신 최고 capacity 유지 energy를 더 씁니다.
AMD frequency sensitivity driver가 없으면 원 목표 `f`에 다음 감소율을 적용하며 기본 bias는 0입니다.
f * (1 - ``powersave_bias`` / 1000)
`drivers/cpufreq/amd_freq_sensitivity.c` driver가 load되면 기본 bias는 400입니다. AMD Family 16h 이후 hardware의 workload sensitivity 0-100%를 이용합니다. 0인 memory/IO-bound workload는 frequency 증가 이득이 없고, 100% CPU-bound workload는 큰 이득이 예상됩니다. Sensitivity가 threshold보다 낮으면 목표를 내려 over-provisioning을 피합니다.
conservative
568-610`conservative`도 `ondemand`와 같은 방식으로 load를 추정하지만 짧은 시간에 큰 frequency 변화를 피합니다. Battery system처럼 power supply capacity가 제한된 환경을 위해 작은 step으로 한 번씩 올리거나 내립니다.
| Tunable | 동작 |
|---|---|
| `freq_step` | 한 번에 바꿀 수 있는 최대 frequency 비율; 기본 5% |
| `down_threshold` | 증가·감소 방향을 정하는 load threshold; 기본 20% |
| `sampling_down_factor` | 감소를 증가보다 1-10배 늦추는 factor |
`freq_step=0`은 기본 5%를 사용하고 100은 사실상 min/max 사이를 주기적으로 전환합니다. Load가 `down_threshold`보다 크면 한 step 올리고 작으면 감소하며, 중간이면 유지합니다. `sampling_down_factor`는 감소 속도를 증가보다 느리게 합니다.
Frequency boost와 전역 knob
611-694일부 multicore processor는 chip 전체가 충분히 idle이고 thermal/power budget 아래일 때 일부 core를 package 지속 가능 frequency보다 잠시 높입니다. Intel은 Turbo Boost, AMD는 Turbo-Core 또는 Core Performance Boost라 부르며 여기서는 frequency boost로 통칭합니다.
X86 같은 hardware-based boost는 hardware가 결정하고, ARM 같은 software-based boost는 scaling driver가 trigger 여부와 시점을 정합니다.
전역 `/sys/devices/system/cpu/cpufreq/boost`는 system 전체 boost를 제어합니다. Driver가 boost를 지원하지 않거나 `intel_pstate`처럼 별도 interface를 제공하면 파일이 없습니다.
| 값 | 의미 |
|---|---|
| `1` | Boost 사용 허용; 실제 boost 중이라는 뜻은 아님 |
| `0` | Boost 완전 비활성화 |
`1`은 boost 사용 권한일 뿐 현재 어떤 CPU가 실제 boosting 중이라는 뜻은 아닙니다. 쓸 수 있는 값은 `0`과 `1`뿐입니다.
Boost는 scheduler tick보다 짧은 시간 scale에서 성능을 최적화하지만 문제도 일으킬 수 있습니다. BIOS에서 끄려면 reboot가 필요하므로 runtime knob가 유용합니다.
이유는 네 가지입니다. 제한된 battery 전원에서 일시 overclock의 energy 증가를 피하고, 성능보다 deterministic behavior가 중요한 상황을 만들고, reboot 없이 boost 효과를 비교하며, package 전체 load에 따른 single-thread benchmark 변동을 제거해 재현성을 높일 수 있습니다.
Legacy AMD cpb와 참고 문헌
695-725AMD `powernow-k8`의 policy별 `cpb` sysfs knob는 Core Performance Boost를 켜고 끕니다. `/sys/devices/system/cpu/cpufreq/policyX/cpb`에 있지만 실제 구현은 system-wide라 한 policy를 바꾸면 모두 같은 값으로 바뀝니다.
Hardware 지원 AMD processor에서는 계속 제공되지만 :c:macro:`CONFIG_X86_ACPI_CPUFREQ_CPB`로 제외할 수 있고 전역 `boost`는 항상 있습니다. 다른 system과 일관되고 향후 `cpb`가 사라질 수 있으므로 `boost` 사용을 강하게 권장합니다.
기반 hardware 기능이 없는 Intel 등의 processor에는 config가 켜져 있어도 `cpb`가 나타나지 않습니다.
참고 문헌 [1]은 Jonathan Corbet의 `Per-entity load tracking`입니다: `https://lwn.net/Articles/531853/`.
운영 요약
cpufreq.rst:1-725CPUFreq는 공유 hardware interface를 policy object로 묶고 governor 결정과 driver 제어를 연결합니다. Policy sysfs 한계, governor tunable, boost permission을 함께 확인해야 실제 frequency 동작을 해석할 수 있습니다.