1*9a8e740eSShrikanth Hegde.. SPDX-License-Identifier: GPL-2.0 2*9a8e740eSShrikanth Hegde 3*9a8e740eSShrikanth HegdeSteal Governor 4*9a8e740eSShrikanth Hegde============== 5*9a8e740eSShrikanth Hegde 6*9a8e740eSShrikanth Hegde:Author: Shrikanth Hegde <sshegde@linux.ibm.com> 7*9a8e740eSShrikanth Hegde 8*9a8e740eSShrikanth HegdeIntroduction 9*9a8e740eSShrikanth Hegde============ 10*9a8e740eSShrikanth Hegde 11*9a8e740eSShrikanth HegdeThe steal governor is aimed at mitigating the Noisy Neighbour problem 12*9a8e740eSShrikanth Hegdewhich occurs in paravirtualized environments with CPU overcommit. 13*9a8e740eSShrikanth HegdeThe performance of a workload running in one VM gets degraded by 14*9a8e740eSShrikanth Hegdethe activity of other VMs on the same host. As a result, all VMs 15*9a8e740eSShrikanth Hegdecollectively make slower forward progress. 16*9a8e740eSShrikanth Hegde 17*9a8e740eSShrikanth HegdeIn such systems, high utilization in all VMs causes the hypervisor to 18*9a8e740eSShrikanth Hegdefrequently preempt vCPUs. This vCPU preemption is expensive. 19*9a8e740eSShrikanth HegdeTo mitigate this, the kernel aims to restrict workloads to a subset of 20*9a8e740eSShrikanth HegdePreferred CPUs to reduce physical CPU contention. 21*9a8e740eSShrikanth HegdeA detailed explanation of Preferred CPUs is available in 22*9a8e740eSShrikanth Hegde``Documentation/scheduler/sched-paravirt.rst``. 23*9a8e740eSShrikanth Hegde 24*9a8e740eSShrikanth HegdeThe steal governor selects ``CONFIG_PREFERRED_CPU=y`` which enables the 25*9a8e740eSShrikanth Hegdescheduler core infrastructure to move the tasks to Preferred CPUs where 26*9a8e740eSShrikanth Hegdepossible. The driver controls the policy decisions regarding the state of 27*9a8e740eSShrikanth Hegdepreferred CPUs. That is, this driver decides which CPUs are preferred 28*9a8e740eSShrikanth Hegdeand which CPUs are non-preferred. 29*9a8e740eSShrikanth Hegde 30*9a8e740eSShrikanth HegdeThe driver code is available at ``drivers/virt/steal_governor.c``. 31*9a8e740eSShrikanth Hegde 32*9a8e740eSShrikanth HegdeCore idea 33*9a8e740eSShrikanth Hegde========= 34*9a8e740eSShrikanth Hegde 35*9a8e740eSShrikanth Hegdesteal time is an indication available today in Guest which shows contention 36*9a8e740eSShrikanth Hegdefor underlying physical CPU. Use it as a hint in the guest to fold the 37*9a8e740eSShrikanth Hegdeworkload to a reduced set of vCPUs. When there is contention, steal time 38*9a8e740eSShrikanth Hegdewill show up in all the guests. When each guest honors the hint and folds 39*9a8e740eSShrikanth Hegdethe workload to a smaller set of vCPUs (Preferred CPUs), it reduces the 40*9a8e740eSShrikanth Hegdecontention and thereby reduces vCPU preemption. 41*9a8e740eSShrikanth HegdeThis is achieved without any cross-guest communication. 42*9a8e740eSShrikanth Hegde 43*9a8e740eSShrikanth HegdeSteal governor driver effectively does: 44*9a8e740eSShrikanth Hegde 45*9a8e740eSShrikanth Hegde1. Periodically computes the steal ratio using accumulated steal time 46*9a8e740eSShrikanth Hegde across possible CPUs, normalized by the number of active CPUs. 47*9a8e740eSShrikanth Hegde 48*9a8e740eSShrikanth Hegde2. If steal ratio is greater than high threshold, reduce the number of 49*9a8e740eSShrikanth Hegde preferred CPUs by 1 core. Ensure at least one core is left always. 50*9a8e740eSShrikanth Hegde Skip changing the state of offline CPUs in that core. 51*9a8e740eSShrikanth Hegde 52*9a8e740eSShrikanth Hegde3. If steal ratio is less than or equal to low threshold, increase the 53*9a8e740eSShrikanth Hegde number of preferred CPUs by 1 core. If preferred is same as active, 54*9a8e740eSShrikanth Hegde nothing to be done. Skip changing the state of offline CPUs. 55*9a8e740eSShrikanth Hegde This helps to handle cases where few CPUs are offline in a core and 56*9a8e740eSShrikanth Hegde those offline CPUs will not be marked as preferred. 57*9a8e740eSShrikanth Hegde 58*9a8e740eSShrikanth Hegde4. Ensure preferred CPUs is always subset of active CPUs. 59*9a8e740eSShrikanth Hegde On feature disable it is same as active CPUs. 60*9a8e740eSShrikanth Hegde 61*9a8e740eSShrikanth HegdeThis feature works best only when all the VMs enable the feature as 62*9a8e740eSShrikanth Hegdeit is a co-operative scheme. If a specific VM doesn't enable this feature 63*9a8e740eSShrikanth Hegdeit may end up with more CPUs than others, still should lead to better 64*9a8e740eSShrikanth Hegdeperformance when seen from system view. Those who enable this driver must 65*9a8e740eSShrikanth Hegdeensure it is enabled in all VMs. 66*9a8e740eSShrikanth Hegde 67*9a8e740eSShrikanth HegdeNote that this driver is strictly intended for actual guests; for example, 68*9a8e740eSShrikanth Hegdeloading this module in a privileged VM like Xen Dom0 is blocked. 69*9a8e740eSShrikanth Hegde 70*9a8e740eSShrikanth HegdeWorkload considerations 71*9a8e740eSShrikanth Hegde======================= 72*9a8e740eSShrikanth Hegde 73*9a8e740eSShrikanth HegdeThe steal governor is useful for workloads where vCPU preemption has 74*9a8e740eSShrikanth Hegdecosts beyond the lost CPU time, such as lock-holder preemption, critical 75*9a8e740eSShrikanth Hegdesections, communicating threads, and cache or TLB disruption. 76*9a8e740eSShrikanth Hegde 77*9a8e740eSShrikanth HegdePure CPU-time workloads with independent workers may not benefit and 78*9a8e740eSShrikanth Hegdecould see a small regression due to additional guest scheduling overhead. 79*9a8e740eSShrikanth Hegde 80*9a8e740eSShrikanth HegdeModule Parameters 81*9a8e740eSShrikanth Hegde================= 82*9a8e740eSShrikanth Hegde 83*9a8e740eSShrikanth Hegdeinterval_ms 84*9a8e740eSShrikanth Hegde----------- 85*9a8e740eSShrikanth Hegde 86*9a8e740eSShrikanth HegdeHow often steal governor checks for steal time. 87*9a8e740eSShrikanth HegdeDefault: 1000 i.e. 1 second. Value should be in between 100ms to 100sec. 88*9a8e740eSShrikanth Hegde 89*9a8e740eSShrikanth HegdeThis controls how fast steal governor driver reacts to changes to the 90*9a8e740eSShrikanth Hegdecontention of physical CPUs. Since it does a fair amount of work, setting 91*9a8e740eSShrikanth Hegdetoo low may have overhead. Setting it too high might render it ineffective. 92*9a8e740eSShrikanth Hegde 93*9a8e740eSShrikanth Hegdelow_threshold 94*9a8e740eSShrikanth Hegde------------- 95*9a8e740eSShrikanth Hegde 96*9a8e740eSShrikanth Hegdelower threshold value in percentage * 100. 97*9a8e740eSShrikanth HegdeDefault: 200, i.e. 2% steal is considered as low threshold. 98*9a8e740eSShrikanth HegdeCan't be higher than high_threshold. 99*9a8e740eSShrikanth Hegde 100*9a8e740eSShrikanth HegdeThis determines what values should be considered as nil/no steal values. 101*9a8e740eSShrikanth HegdeWhen steal governor sees steal ratio is less than or equal to this value, 102*9a8e740eSShrikanth Hegdeit will increase the preferred CPUs by 1 core. 103*9a8e740eSShrikanth HegdeUsing zero might cause oscillations. 104*9a8e740eSShrikanth Hegde 105*9a8e740eSShrikanth Hegdehigh_threshold 106*9a8e740eSShrikanth Hegde-------------- 107*9a8e740eSShrikanth Hegde 108*9a8e740eSShrikanth Hegdehigher threshold value in percentage * 100 109*9a8e740eSShrikanth HegdeDefault: 500, i.e. 5% steal is considered as high threshold. 110*9a8e740eSShrikanth HegdeCan't be lower than low_threshold. Must be less than 10000. 111*9a8e740eSShrikanth Hegde 112*9a8e740eSShrikanth HegdeThis determines what values should be considered as high steal values. 113*9a8e740eSShrikanth HegdeWhen steal governor sees steal ratio is higher than this value, it will 114*9a8e740eSShrikanth Hegdereduce the preferred CPUs by 1 core. 115*9a8e740eSShrikanth Hegde 116*9a8e740eSShrikanth HegdeLimitations of default values 117*9a8e740eSShrikanth Hegde----------------------------- 118*9a8e740eSShrikanth Hegde 119*9a8e740eSShrikanth HegdeBecause of the vast diversity in VM configurations and different 120*9a8e740eSShrikanth Hegdearchitectures, the default thresholds may not be optimal for all systems. 121*9a8e740eSShrikanth HegdeUsers may need to tune these parameters based on the system under 122*9a8e740eSShrikanth Hegdetest to achieve the best results. 123*9a8e740eSShrikanth Hegde 124*9a8e740eSShrikanth HegdeThe governor sums the steal time across all possible CPUs, which ensures 125*9a8e740eSShrikanth Hegdethe accumulated steal time remains a monotonically increasing value. 126*9a8e740eSShrikanth HegdeHowever, to calculate the effective steal ratio, it divides this sum 127*9a8e740eSShrikanth Hegdeby the number of active CPUs. Because only active CPUs contribute to 128*9a8e740eSShrikanth Hegdethe steal time delta, this prevents threshold dilution on sparsely 129*9a8e740eSShrikanth Hegdepopulated systems. 130*9a8e740eSShrikanth Hegde 131*9a8e740eSShrikanth HegdeThe driver reduces/increases preferred CPUs by core-level. This could provide 132*9a8e740eSShrikanth Hegdefaster convergence for hypervisors such as powerVM. But on KVM and Xen 133*9a8e740eSShrikanth Hegdeconvergence could be slower depending on the configuration. 134*9a8e740eSShrikanth HegdeUsing a smaller interval_ms could help one to expedite it. 135*9a8e740eSShrikanth Hegde 136*9a8e740eSShrikanth HegdeReasons for CONFIG_STEAL_GOVERNOR=m 137*9a8e740eSShrikanth Hegde=================================== 138*9a8e740eSShrikanth Hegde 139*9a8e740eSShrikanth HegdeSelecting this driver makes CONFIG_PREFERRED_CPU=y. That makes configs 140*9a8e740eSShrikanth Hegdedriven by user preference. Though one can have CONFIG_STEAL_GOVERNOR=y, 141*9a8e740eSShrikanth HegdeIt is recommended to build CONFIG_STEAL_GOVERNOR=m due to below reasons: 142*9a8e740eSShrikanth Hegde 143*9a8e740eSShrikanth Hegde1. Doing periodic work has additional overheads. Enabling this driver 144*9a8e740eSShrikanth Hegde in systems where steal time cannot happen is of no use. There is no 145*9a8e740eSShrikanth Hegde benefit with additional overheads in such systems. 146*9a8e740eSShrikanth Hegde 147*9a8e740eSShrikanth Hegde2. This works well when all VMs work in a co-operative manner. When an 148*9a8e740eSShrikanth Hegde administrative user enables it in one VM, he/she will likely enable 149*9a8e740eSShrikanth Hegde it in all VMs. 150*9a8e740eSShrikanth Hegde 151*9a8e740eSShrikanth Hegde3. User can tweak the module parameters by reloading the module. 152