1.. SPDX-License-Identifier: BSD-3-Clause 2 Copyright(c) 2010-2014 Intel Corporation. 3 4L3 Forwarding with Power Management Sample Application 5====================================================== 6 7Introduction 8------------ 9 10The L3 Forwarding with Power Management application is an example of power-aware packet processing using the DPDK. 11The application is based on existing L3 Forwarding sample application, 12with the power management algorithms to control the P-states and 13C-states of the Intel processor via a power management library. 14 15Overview 16-------- 17 18The application demonstrates the use of the Power libraries in the DPDK to implement packet forwarding. 19The initialization and run-time paths are very similar to those of the :doc:`l3_forward`. 20The main difference from the L3 Forwarding sample application is that this application introduces power-aware optimization algorithms 21by leveraging the Power library to control P-state and C-state of processor based on packet load. 22 23The DPDK includes poll-mode drivers to configure Intel NIC devices and their receive (Rx) and transmit (Tx) queues. 24The design principle of this PMD is to access the Rx and Tx descriptors directly without any interrupts to quickly receive, 25process and deliver packets in the user space. 26 27In general, the DPDK executes an endless packet processing loop on dedicated IA cores that include the following steps: 28 29* Retrieve input packets through the PMD to poll Rx queue 30 31* Process each received packet or provide received packets to other processing cores through software queues 32 33* Send pending output packets to Tx queue through the PMD 34 35In this way, the PMD achieves better performance than a traditional interrupt-mode driver, 36at the cost of keeping cores active and running at the highest frequency, 37hence consuming the maximum power all the time. 38However, during the period of processing light network traffic, 39which happens regularly in communication infrastructure systems due to well-known "tidal effect", 40the PMD is still busy waiting for network packets, which wastes a lot of power. 41 42Processor performance states (P-states) are the capability of an Intel processor 43to switch between different supported operating frequencies and voltages. 44If configured correctly, according to system workload, this feature provides power savings. 45CPUFreq is the infrastructure provided by the Linux* kernel to control the processor performance state capability. 46CPUFreq supports a user space governor that enables setting frequency via manipulating the virtual file device from a user space application. 47The Power library in the DPDK provides a set of APIs for manipulating a virtual file device to allow user space application 48to set the CPUFreq governor and set the frequency of specific cores. 49 50This application includes a P-state power management algorithm to generate a frequency hint to be sent to CPUFreq. 51The algorithm uses the number of received and available Rx packets on recent polls to make a heuristic decision to scale frequency up/down. 52Specifically, some thresholds are checked to see whether a specific core running a DPDK polling thread needs to increase frequency 53a step up based on the near to full trend of polled Rx queues. 54Also, it decreases frequency a step if packet processed per loop is far less than the expected threshold 55or the thread's sleeping time exceeds a threshold. 56 57C-States are also known as sleep states. 58They allow software to put an Intel core into a low power idle state from which it is possible to exit via an event, such as an interrupt. 59However, there is a tradeoff between the power consumed in the idle state and the time required to wake up from the idle state (exit latency). 60Therefore, as you go into deeper C-states, the power consumed is lower but the exit latency is increased. Each C-state has a target residency. 61It is essential that when entering into a C-state, the core remains in this C-state for at least as long as the target residency in order 62to fully realize the benefits of entering the C-state. 63CPUIdle is the infrastructure provide by the Linux kernel to control the processor C-state capability. 64Unlike CPUFreq, CPUIdle does not provide a mechanism that allows the application to change C-state. 65It actually has its own heuristic algorithms in kernel space to select target C-state to enter by executing privileged instructions like HLT and MWAIT, 66based on the speculative sleep duration of the core. 67In this application, we introduce a heuristic algorithm that allows packet processing cores to sleep for a short period 68if there is no Rx packet received on recent polls. 69In this way, CPUIdle automatically forces the corresponding cores to enter deeper C-states 70instead of always running to the C0 state waiting for packets. 71 72.. note:: 73 74 To fully demonstrate the power saving capability of using C-states, 75 it is recommended to enable deeper C3 and C6 states in the BIOS during system boot up. 76 77Compiling the Application 78------------------------- 79 80To compile the sample application see :doc:`compiling`. 81 82The application is located in the ``l3fwd-power`` sub-directory. 83 84Running the Application 85----------------------- 86 87The application has a number of command line options: 88 89.. code-block:: console 90 91 ./<build_dir>/examples/dpdk-l3fwd_power [EAL options] -- -p PORTMASK [-P] --config(port,queue,lcore)[,(port,queue,lcore)] [--max-pkt-len PKTLEN] [--no-numa] 92 93where, 94 95* -p PORTMASK: Hexadecimal bitmask of ports to configure 96 97* -P: Sets all ports to promiscuous mode so that packets are accepted regardless of the packet's Ethernet MAC destination address. 98 Without this option, only packets with the Ethernet MAC destination address set to the Ethernet address of the port are accepted. 99 100* --config (port,queue,lcore)[,(port,queue,lcore)]: determines which queues from which ports are mapped to which cores. 101 102* --max-pkt-len: optional, maximum packet length in decimal (64-9600) 103 104* --no-numa: optional, disables numa awareness 105 106* --empty-poll: Traffic Aware power management. See below for details 107 108* --telemetry: Telemetry mode. 109 110* --pmd-mgmt: PMD power management mode. 111 112* --max-empty-polls : Number of empty polls to wait before entering sleep state. Applies to --pmd-mgmt mode only. 113 114* --pause-duration: Set the duration of the pause callback (microseconds). Applies to --pmd-mgmt mode only. 115 116* --scale-freq-min: Set minimum frequency for scaling. Applies to --pmd-mgmt mode only. 117 118* --scale-freq-max: Set maximum frequency for scaling. Applies to --pmd-mgmt mode only. 119 120See :doc:`l3_forward` for details. 121The L3fwd-power example reuses the L3fwd command line options. 122 123Explanation 124----------- 125 126The following sections provide some explanation of the sample application code. 127As mentioned in the overview section, 128the initialization and run-time paths are identical to those of the L3 forwarding application. 129The following sections describe aspects that are specific to the L3 Forwarding with Power Management sample application. 130 131Power Library Initialization 132~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 133 134The Power library is initialized in the main routine. 135It changes the P-state governor to userspace for specific cores that are under control. 136The Timer library is also initialized and several timers are created later on, 137responsible for checking if it needs to scale down frequency at run time by checking CPU utilization statistics. 138 139.. note:: 140 141 Only the power management related initialization is shown. 142 143.. literalinclude:: ../../../examples/l3fwd-power/main.c 144 :language: c 145 :start-after: Power library initialized in the main routine. 8< 146 :end-before: >8 End of power library initialization. 147 148Monitoring Loads of Rx Queues 149~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 150 151In general, the polling nature of the DPDK prevents the OS power management subsystem from knowing 152if the network load is actually heavy or light. 153In this sample, sampling network load work is done by monitoring received and 154available descriptors on NIC Rx queues in recent polls. 155Based on the number of returned and available Rx descriptors, 156this example implements algorithms to generate frequency scaling hints and speculative sleep duration, 157and use them to control P-state and C-state of processors via the power management library. 158Frequency (P-state) control and sleep state (C-state) control work individually for each logical core, 159and the combination of them contributes to a power efficient packet processing solution when serving light network loads. 160 161The rte_eth_rx_burst() function and the newly-added rte_eth_rx_queue_count() function are used in the endless packet processing loop 162to return the number of received and available Rx descriptors. 163And those numbers of specific queue are passed to P-state and C-state heuristic algorithms 164to generate hints based on recent network load trends. 165 166.. note:: 167 168 Only power control related code is shown. 169 170.. literalinclude:: ../../../examples/l3fwd-power/main.c 171 :language: c 172 :start-after: Main processing loop. 8< 173 :end-before: >8 End of main processing loop. 174 175P-State Heuristic Algorithm 176~~~~~~~~~~~~~~~~~~~~~~~~~~~ 177 178The power_freq_scaleup_heuristic() function is responsible for generating a frequency hint for the specified logical core 179according to available descriptor number returned from rte_eth_rx_queue_count(). 180On every poll for new packets, the length of available descriptor on an Rx queue is evaluated, 181and the algorithm used for frequency hinting is as follows: 182 183* If the size of available descriptors exceeds 96, the maximum frequency is hinted. 184 185* If the size of available descriptors exceeds 64, a trend counter is incremented by 100. 186 187* If the length of the ring exceeds 32, the trend counter is incremented by 1. 188 189* When the trend counter reached 10000 the frequency hint is changed to the next higher frequency. 190 191.. note:: 192 193 The assumption is that the Rx queue size is 128 and the thresholds specified above 194 must be adjusted accordingly based on actual hardware Rx queue size, 195 which are configured via the rte_eth_rx_queue_setup() function. 196 197In general, a thread needs to poll packets from multiple Rx queues. 198Most likely, different queue have different load, so they would return different frequency hints. 199The algorithm evaluates all the hints and then scales up frequency in an aggressive manner 200by scaling up to highest frequency as long as one Rx queue requires. 201In this way, we can minimize any negative performance impact. 202 203On the other hand, frequency scaling down is controlled in the timer callback function. 204Specifically, if the sleep times of a logical core indicate that it is sleeping more than 25% of the sampling period, 205or if the average packet per iteration is less than expectation, the frequency is decreased by one step. 206 207C-State Heuristic Algorithm 208~~~~~~~~~~~~~~~~~~~~~~~~~~~ 209 210Whenever recent rte_eth_rx_burst() polls return 5 consecutive zero packets, 211an idle counter begins incrementing for each successive zero poll. 212At the same time, the function power_idle_heuristic() is called to generate speculative sleep duration 213in order to force logical to enter deeper sleeping C-state. 214There is no way to control C- state directly, and the CPUIdle subsystem in OS is intelligent enough 215to select C-state to enter based on actual sleep period time of giving logical core. 216The algorithm has the following sleeping behavior depending on the idle counter: 217 218* If idle count less than 100, the counter value is used as a microsecond sleep value through rte_delay_us() 219 which execute pause instructions to avoid costly context switch but saving power at the same time. 220 221* If idle count is between 100 and 999, a fixed sleep interval of 100 μs is used. 222 A 100 μs sleep interval allows the core to enter the C1 state while keeping a fast response time in case new traffic arrives. 223 224* If idle count is greater than 1000, a fixed sleep value of 1 ms is used until the next timer expiration is used. 225 This allows the core to enter the C3/C6 states. 226 227.. note:: 228 229 The thresholds specified above need to be adjusted for different Intel processors and traffic profiles. 230 231If a thread polls multiple Rx queues and different queue returns different sleep duration values, 232the algorithm controls the sleep time in a conservative manner by sleeping for the least possible time 233in order to avoid a potential performance impact. 234 235Empty Poll Mode 236------------------------- 237Additionally, there is a traffic aware mode of operation called "Empty 238Poll" where the number of empty polls can be monitored to keep track 239of how busy the application is. Empty poll mode can be enabled by the 240command line option --empty-poll. 241 242See :doc:`Power Management<../prog_guide/power_man>` chapter in the DPDK Programmer's Guide for empty poll mode details. 243 244.. code-block:: console 245 246 ./<build_dir>/examples/dpdk-l3fwd-power -l xxx -n 4 -a 0000:xx:00.0 -a 0000:xx:00.1 \ 247 -- -p 0x3 -P --config="(0,0,xx),(1,0,xx)" --empty-poll="0,0,0" -l 14 -m 9 -h 1 248 249Where, 250 251--empty-poll: Enable the empty poll mode instead of original algorithm 252 253--empty-poll="training_flag, med_threshold, high_threshold" 254 255* ``training_flag`` : optional, enable/disable training mode. Default value is 0. If the training_flag is set as 1(true), then the application will start in training mode and print out the trained threshold values. If the training_flag is set as 0(false), the application will start in normal mode, and will use either the default thresholds or those supplied on the command line. The trained threshold values are specific to the user’s system, may give a better power profile when compared to the default threshold values. 256 257* ``med_threshold`` : optional, sets the empty poll threshold of a modestly busy system state. If this is not supplied, the application will apply the default value of 350000. 258 259* ``high_threshold`` : optional, sets the empty poll threshold of a busy system state. If this is not supplied, the application will apply the default value of 580000. 260 261* -l : optional, set up the LOW power state frequency index 262 263* -m : optional, set up the MED power state frequency index 264 265* -h : optional, set up the HIGH power state frequency index 266 267Empty Poll Mode Example Usage 268~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 269To initially obtain the ideal thresholds for the system, the training 270mode should be run first. This is achieved by running the l3fwd-power 271app with the training flag set to “1”, and the other parameters set to 2720. 273 274.. code-block:: console 275 276 ./<build_dir>/examples/dpdk-l3fwd-power -l 1-3 -- -p 0x0f --config="(0,0,2),(0,1,3)" --empty-poll "1,0,0" –P 277 278This will run the training algorithm for x seconds on each core (cores 2 279and 3), and then print out the recommended threshold values for those 280cores. The thresholds should be very similar for each core. 281 282.. code-block:: console 283 284 POWER: Bring up the Timer 285 POWER: set the power freq to MED 286 POWER: Low threshold is 230277 287 POWER: MED threshold is 335071 288 POWER: HIGH threshold is 523769 289 POWER: Training is Complete for 2 290 POWER: set the power freq to MED 291 POWER: Low threshold is 236814 292 POWER: MED threshold is 344567 293 POWER: HIGH threshold is 538580 294 POWER: Training is Complete for 3 295 296Once the values have been measured for a particular system, the app can 297then be started without the training mode so traffic can start immediately. 298 299.. code-block:: console 300 301 ./<build_dir>/examples/dpdk-l3fwd-power -l 1-3 -- -p 0x0f --config="(0,0,2),(0,1,3)" --empty-poll "0,340000,540000" –P 302 303Telemetry Mode 304-------------- 305 306The telemetry mode support for ``l3fwd-power`` is a standalone mode, in this mode 307``l3fwd-power`` does simple l3fwding along with calculating empty polls, full polls, 308and busy percentage for each forwarding core. The aggregation of these 309values of all cores is reported as application level telemetry to metric 310library for every 500ms from the main core. 311 312The busy percentage is calculated by recording the poll_count 313and when the count reaches a defined value the total 314cycles it took is measured and compared with minimum and maximum 315reference cycles and accordingly busy rate is set to either 0% or 31650% or 100%. 317 318.. code-block:: console 319 320 ./<build_dir>/examples/dpdk-l3fwd-power --telemetry -l 1-3 -- -p 0x0f --config="(0,0,2),(0,1,3)" --telemetry 321 322The new stats ``empty_poll`` , ``full_poll`` and ``busy_percent`` can be viewed by running the script 323``/usertools/dpdk-telemetry-client.py`` and selecting the menu option ``Send for global Metrics``. 324 325PMD power management Mode 326------------------------- 327 328The PMD power management mode support for ``l3fwd-power`` is a standalone mode. 329In this mode, ``l3fwd-power`` does simple l3fwding 330along with enabling the power saving scheme on specific port/queue/lcore. 331Main purpose for this mode is to demonstrate 332how to use the PMD power management API. 333 334.. code-block:: console 335 336 ./build/examples/dpdk-l3fwd-power -l 1-3 -- --pmd-mgmt -p 0x0f --config="(0,0,2),(0,1,3)" 337 338PMD Power Management Mode 339------------------------- 340 341There is also a traffic-aware operating mode that, 342instead of using explicit power management, 343will use automatic PMD power management. 344This mode is limited to one queue per core, 345and has three available power management schemes: 346 347``monitor`` 348 This will use ``rte_power_monitor()`` function to enter 349 a power-optimized state (subject to platform support). 350 351``pause`` 352 This will use ``rte_power_pause()`` or ``rte_pause()`` 353 to avoid busy looping when there is no traffic. 354 355``scale`` 356 This will use frequency scaling routines 357 available in the ``librte_power`` library. 358 The reaction time of the scale mode is longer 359 than the pause and monitor mode. 360 361See :doc:`Power Management<../prog_guide/power_man>` chapter 362in the DPDK Programmer's Guide for more details on PMD power management. 363 364.. code-block:: console 365 366 ./<build_dir>/examples/dpdk-l3fwd-power -l 1-3 -- -p 0x0f --config="(0,0,2),(0,1,3)" --pmd-mgmt=scale 367