a h@s"ddlmZddlTddlZddlZddlTddlZz ddlZWne yRYn0ddl Z ddl m Z ddl Z ddlmZddlZddlZddlZddlZz ejWneyddlZYn0ejZGdddeZGdd d eZGd d d eZGd d d eZGdddejZ dS))base)*N)commandsc@s0eZdZdddZeddZejddZdS)SchedulerParamsNcCs(||_||_||_||_||_||_dSN)_cmdcmdline schedulerpriorityaffinitycgroup)selfcmdr r r r r rB/usr/lib/python3.9/site-packages/tuned/plugins/plugin_scheduler.py__init__"s zSchedulerParams.__init__cCs |jdurdS|j|jSdSr) _affinityrZbitmask2cpulistrrrrr +s zSchedulerParams.affinitycCs"|durd|_n|j||_dSr)rrZcpulist2bitmaskrvaluerrrr 2s)NNNNN)__name__ __module__ __qualname__rpropertyr setterrrrrr!s  rc@seZdZddZdS) IRQAffinitiescCsi|_d|_g|_dSr)irqsdefault unchangeablerrrrr:szIRQAffinities.__init__N)rrrrrrrrr9src@speZdZdZddddddZdd Zd d Zd d ZddZddZ ddZ ddZ ddZ ddZ ddZdS)SchedulerUtilsz= Class encapsulating scheduler implementation in os module SCHED_FIFO SCHED_BATCHSCHED_RR SCHED_OTHER SCHED_IDLE)fbroicCs8tdd|jD|_tdd|jD|_dS)Ncss |]\}}|tt|fVqdSrgetattros.0knamerrr Oz*SchedulerUtils.__init__..css|]}tt||fVqdSrr+r/r1rrrr2Qr3dict_dict_schedcfg2schedconstitems_dict_schedcfg2numvalues_dict_num2schedconstrrrrrMszSchedulerUtils.__init__cCs |j|Sr)r9get)r str_schedulerrrrsched_cfg_to_numSszSchedulerUtils.sched_cfg_to_numcCs |j|Sr)r;r<)rr rrrsched_num_to_constWsz!SchedulerUtils.sched_num_to_constcCs t|Sr)r-sched_getschedulerrpidrrr get_schedulerZszSchedulerUtils.get_schedulercCst||t|dSr)r-sched_setscheduler sched_paramrrBschedpriorrr set_scheduler]szSchedulerUtils.set_schedulercCs t|Sr)r-sched_getaffinityrArrr get_affinity`szSchedulerUtils.get_affinitycCst||dSr)r-sched_setaffinityrrBr rrr set_affinitycszSchedulerUtils.set_affinitycCs t|jSr)r-sched_getparamsched_priorityrArrr get_priorityfszSchedulerUtils.get_prioritycCs t|Sr)r-sched_get_priority_minrrGrrrget_priority_miniszSchedulerUtils.get_priority_mincCs t|Sr)r-sched_get_priority_maxrSrrrget_priority_maxlszSchedulerUtils.get_priority_maxN)rrr__doc__r7rr>r?rCrIrKrNrQrTrVrrrrr @s"r c@sPeZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ dS)SchedulerUtilsSchedutilszE Class encapsulating scheduler implementation in schedutils module cCs8tdd|jD|_tdd|jD|_dS)Ncss |]\}}|tt|fVqdSrr, schedutilsr.rrrr2ur3z4SchedulerUtilsSchedutils.__init__..css|]}tt||fVqdSrrYr4rrrr2wr3r5rrrrrssz!SchedulerUtilsSchedutils.__init__cCs t|Sr)rZrCrArrrrCysz&SchedulerUtilsSchedutils.get_schedulercCst|||dSr)rZrIrFrrrrI|sz&SchedulerUtilsSchedutils.set_schedulercCs t|Sr)rZrKrArrrrKsz%SchedulerUtilsSchedutils.get_affinitycCst||dSr)rZrNrMrrrrNsz%SchedulerUtilsSchedutils.set_affinitycCs t|Sr)rZrQrArrrrQsz%SchedulerUtilsSchedutils.get_prioritycCs t|Sr)rZrTrSrrrrTsz)SchedulerUtilsSchedutils.get_priority_mincCs t|Sr)rZrVrSrrrrVsz)SchedulerUtilsSchedutils.get_priority_maxN) rrrrWrrCrIrKrNrQrTrVrrrrrXosrXcsFeZdZdZddddZddZfddZd d Zd d Zd dZ e ddZ ddZ ddZ ddZddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zdd(d)Zd*d+Zd,d-Zd.d/Zdd0d1Zd2d3Zd4d5Zd6d7Zd8d9Zd:d;Z dd?Z"d@dAZ#dBdCZ$dDdEZ%ddFdGZ&dHdIZ'dJdKZ(fdLdMZ)dNdOZ*dPdQZ+dRdSZ,e-j.ffdTdU Z/dVdWZ0dXdYZ1fdZd[Z2d\d]Z3d^d_Z4d`daZ5e6dbd'dcdddeZ7e6dfd'dcdgdhZ8e6did'dcdjdkZ9e6dld'dcdmdnZ:e6dod'dcdpdqZ;e6drd'dcdsdtZdzd{Z?d|d}Z@dd~dZAddZBddZCddZDddZEddZFddZGddZHddZIddZJe6dd'ddddZKddZLddZMddZNddZOdddZPeQdddZReSdddZTeQdddZUeSdddZVeQdddZWeSdddZXeQdddZYeSdddZZeQdddZ[eSdddZ\eQdddZ]eSdddZ^eQdddZ_eSdddÄZ`eQdăddƄZaeSdăddȄZbeQdɃdd˄ZceSdɃdd̈́ZdeQd΃ddЄZeeSd΃dd҄ZfeQdӃddՄZgeSdӃddׄZhZiS)SchedulerPluginaX. Allows tuning of scheduling priorities, process/thread/IRQ affinities, and CPU isolation. To prevent processes/threads/IRQs from using certain CPUs, use the [option]`isolated_cores` option. It changes process/thread affinities, IRQs affinities and it sets `default_smp_affinity` for IRQs. The CPU affinity mask is adjusted for all processes and threads matching [option]`ps_whitelist` option subject to success of the `sched_setaffinity()` system call. The default setting of the [option]`ps_whitelist` regular expression is `.*` to match all processes and thread names. To exclude certain processes and threads use [option]`ps_blacklist` option. The value of this option is also interpreted as a regular expression and process/thread names (`ps -eo cmd`) are matched against that expression. Profile rollback allows all matching processes and threads to run on all CPUs and restores the IRQ settings prior to the profile application. Multiple regular expressions for [option]`ps_whitelist` and [option]`ps_blacklist` options are allowed and separated by `;`. Quoted semicolon `\;` is taken literally. .Isolate CPUs 2-4 ==== ---- [scheduler] isolated_cores=2-4 ps_blacklist=.*pmd.*;.*PMD.*;^DPDK;.*qemu-kvm.* ---- Isolate CPUs 2-4 while ignoring processes and threads matching `ps_blacklist` regular expressions. ==== The [option]`irq_process` option controls whether the scheduler plugin applies the `isolated_cores` parameter to IRQ affinities. The default value is `true`, which means that the scheduler plugin will move all possible IRQs away from the isolated cores. When `irq_process` is set to `false`, the plugin will not change any IRQ affinities. The [option]`default_irq_smp_affinity` option controls the values *TuneD* writes to `/proc/irq/default_smp_affinity`. The file specifies default affinity mask that applies to all non-active IRQs. Once an IRQ is allocated/activated its affinity bitmask will be set to the default mask. The following values are supported: * `calc` + The content of `/proc/irq/default_smp_affinity` will be calculated from the `isolated_cores` parameter. Non-isolated cores are calculated as an inversion of the `isolated_cores`. Then the intersection of the non-isolated cores and the previous content of `/proc/irq/default_smp_affinity` is written to `/proc/irq/default_smp_affinity`. If the intersection is an empty set, then just the non-isolated cores are written to `/proc/irq/default_smp_affinity`. This behavior is the default if the parameter `default_irq_smp_affinity` is omitted. * `ignore` + *TuneD* will not touch `/proc/irq/default_smp_affinity`. * an explicit cpulist + The cpulist (such as `1,3-4`) is unpacked and written directly to `/proc/irq/default_smp_affinity`. .An explicit CPU list to set the default IRQ smp affinity to CPUs 0 and 2 ==== ---- [scheduler] isolated_cores=1,3 default_irq_smp_affinity=0,2 ---- ==== To adjust scheduling policy, priority and affinity for a group of processes/threads, use the following syntax. [subs="quotes"] ---- group.__groupname__=__rule_prio__:__sched__:__prio__:__affinity__:__regex__ ---- Here, `__rule_prio__` defines internal *TuneD* priority of the rule. Rules are sorted based on priority. This is needed for inheritence to be able to reorder previously defined rules. Equal `__rule_prio__` rules should be processed in the order they were defined. However, this is Python interpreter dependant. To disable an inherited rule for `__groupname__` use: [subs="quotes"] ---- group.__groupname__= ---- `__sched__` must be one of: *`f`* for FIFO, *`b`* for batch, *`r`* for round robin, *`o`* for other, *`*`* do not change. `__affinity__` is CPU affinity in hexadecimal. Use `*` for no change. `__prio__` scheduling priority (see `chrt -m`). `__regex__` is Python regular expression. It is matched against the output of: [subs="quotes"] ---- ps -eo cmd ---- Any given process name may match more than one group. In such a case, the priority and scheduling policy are taken from the last matching `__regex__`. .Setting scheduling policy and priorities to kernel threads and watchdog ==== ---- [scheduler] group.kthreads=0:*:1:*:\[.*\]$ group.watchdog=0:f:99:*:\[watchdog.*\] ---- ==== The scheduler plug-in uses perf event loop to catch newly created processes. By default it listens to `perf.RECORD_COMM` and `perf.RECORD_EXIT` events. By setting [option]`perf_process_fork` option to `true`, `perf.RECORD_FORK` events will be also listened to. In other words, child processes created by the `fork()` system call will be processed. Since child processes inherit CPU affinity from their parents, the scheduler plug-in usually does not need to explicitly process these events. As processing perf events can pose a significant CPU overhead, the [option]`perf_process_fork` option parameter is set to `false` by default. Due to this, child processes are not processed by the scheduler plug-in. The CPU overhead of the scheduler plugin can be mitigated by using the scheduler [option]`runtime` option and setting it to `0`. This will completely disable the dynamic scheduler functionality and the perf events will not be monitored and acted upon. The disadvantage ot this approach is the procees/thread tuning will be done only at profile application. .Disabling the scheduler dynamic functionality ==== ---- [scheduler] runtime=0 isolated_cores=1,3 ---- ==== NOTE: For perf events, memory mapped buffer is used. Under heavy load the buffer may overflow. In such cases the `scheduler` plug-in may start missing events and failing to process some newly created processes. Increasing the buffer size may help. The buffer size can be set with the [option]`perf_mmap_pages` option. The value of this parameter has to expressed in powers of 2. If it is not the power of 2, the nearest higher power of 2 value is calculated from it and this calculated value used. If the [option]`perf_mmap_pages` option is omitted, the default kernel value is used. The scheduler plug-in supports process/thread confinement using cgroups v1. [option]`cgroup_mount_point` option specifies the path to mount the cgroup filesystem or where *TuneD* expects it to be mounted. If unset, `/sys/fs/cgroup/cpuset` is expected. If [option]`cgroup_groups_init` option is set to `1` *TuneD* will create (and remove) all cgroups defined with the `cgroup*` options. This is the default behavior. If it is set to `0` the cgroups need to be preset by other means. If [option]`cgroup_mount_point_init` option is set to `1`, *TuneD* will create (and remove) the cgroup mountpoint. It implies `cgroup_groups_init = 1`. If set to `0` the cgroups mount point needs to be preset by other means. This is the default behavior. The [option]`cgroup_for_isolated_cores` option is the cgroup name used for the [option]`isolated_cores` option functionality. For example, if a system has 4 CPUs, `isolated_cores=1` means that all processes/threads will be moved to CPUs 0,2-3. The scheduler plug-in will isolate the specified core by writing the calculated CPU affinity to the `cpuset.cpus` control file of the specified cgroup and move all the matching processes/threads to this group. If this option is unset, classic cpuset affinity using `sched_setaffinity()` will be used. [option]`cgroup.__cgroup_name__` option defines affinities for arbitrary cgroups. Even hierarchic cgroups can be used, but the hieararchy needs to be specified in the correct order. Also *TuneD* does not do any sanity checks here, with the exception that it forces the cgroup to be under [option]`cgroup_mount_point`. The syntax of the scheduler option starting with `group.` has been augmented to use `cgroup.__cgroup_name__` instead of the hexadecimal `__affinity__`. The matching processes will be moved to the cgroup `__cgroup_name__`. It is also possible to use cgroups which have not been defined by the [option]`cgroup.` option as described above, i.e. cgroups not managed by *TuneD*. All cgroup names are sanitized by replacing all all dots (`.`) with slashes (`/`). This is to prevent the plug-in from writing outside [option]`cgroup_mount_point`. .Using cgroups v1 with the scheduler plug-in ==== ---- [scheduler] cgroup_mount_point=/sys/fs/cgroup/cpuset cgroup_mount_point_init=1 cgroup_groups_init=1 cgroup_for_isolated_cores=group cgroup.group1=2 cgroup.group2=0,2 group.ksoftirqd=0:f:2:cgroup.group1:ksoftirqd.* ps_blacklist=ksoftirqd.*;rcuc.*;rcub.*;ktimersoftd.* isolated_cores=1 ---- Cgroup `group1` has the affinity set to CPU 2 and the cgroup `group2` to CPUs 0,2. Given a 4 CPU setup, the [option]`isolated_cores=1` option causes all processes/threads to be moved to CPU cores 0,2-3. Processes/threads that are blacklisted by the [option]`ps_blacklist` regular expression will not be moved. The scheduler plug-in will isolate the specified core by writing the CPU affinity 0,2-3 to the `cpuset.cpus` control file of the `group` and move all the matching processes/threads to this cgroup. ==== Option [option]`cgroup_ps_blacklist` allows excluding processes which belong to the blacklisted cgroups. The regular expression specified by this option is matched against cgroup hierarchies from `/proc/PID/cgroups`. Cgroups v1 hierarchies from `/proc/PID/cgroups` are separated by commas ',' prior to regular expression matching. The following is an example of content against which the regular expression is matched against: `10:hugetlb:/,9:perf_event:/,8:blkio:/` Multiple regular expressions can be separated by semicolon ';'. The semicolon represents a logical 'or' operator. .Cgroup-based exclusion of processes from the scheduler ==== ---- [scheduler] isolated_cores=1 cgroup_ps_blacklist=:/daemons\b ---- The scheduler plug-in will move all processes away from core 1 except processes which belong to cgroup '/daemons'. The '\b' is a regular expression metacharacter that matches a word boundary. ---- [scheduler] isolated_cores=1 cgroup_ps_blacklist=\b8:blkio: ---- The scheduler plug-in will exclude all processes which belong to a cgroup with hierarchy-ID 8 and controller-list blkio. ==== Kernels 5.13 and newer moved some `sched_` and `numa_balancing_` kernel run-time parameters from `/proc/sys/kernel`, managed by the `sysctl` utility, to `debugfs`, typically mounted under `/sys/kernel/debug`. TuneD provides an abstraction mechanism for the following parameters via the scheduler plug-in: [option]`sched_min_granularity_ns`, [option]`sched_latency_ns`, [option]`sched_wakeup_granularity_ns`, [option]`sched_tunable_scaling`, [option]`sched_migration_cost_ns`, [option]`sched_nr_migrate`, [option]`numa_balancing_scan_delay_ms`, [option]`numa_balancing_scan_period_min_ms`, [option]`numa_balancing_scan_period_max_ms` and [option]`numa_balancing_scan_size_mb`. Moreover in kernel 6.6 and newer support for the `sched_wakeup_granularity_ns` and `sched_latency_ns` were removed. The `sched_min_granularity_ns` was renamed to `sched_base_slice_ns`. Based on the kernel used, TuneD will write the specified value to the correct location or ignore it. For the compatibility the alias [option]`sched_base_slice_ns` was added, but the [option]`sched_min_granularity_ns` can be still used instead. .Set tasks' "cache hot" value for migration decisions. ==== ---- [scheduler] sched_migration_cost_ns=500000 ---- On the old kernels, this is equivalent to: ---- [sysctl] kernel.sched_migration_cost_ns=500000 ---- that is, value `500000` will be written to `/proc/sys/kernel/sched_migration_cost_ns`. However, on more recent kernels, the value `500000` will be written to `/sys/kernel/debug/sched/migration_cost_ns`. ==== Z base_slice_ns)wakeup_granularity_nsmin_granularity_ns latency_nscCstdd|_dS)Nzupython-perf unavailable, disabling perf support and runtime tuning, you can try to (re)install python(3)-perf packageF)logwarning_perf_availablerrrr _disable_perfs zSchedulerPlugin._disable_perfc s*tt|||||||||d|_tj|_ttj|_ |durh| tj tj|_t| tj tj|_ t|_d|_i|_d|_d|_d|_d|_d|_zt|_Wn*ttfy||j|_Yn0|jdd|_d|_ |jdd|_!d|_"z t#|_$Wnty$t%|_$Yn0dS)NTz.*r\r )Z command_nameirq)&superr[rZ_has_dynamic_optionsconstsZCFG_DEF_DAEMON_daemonintZCFG_DEF_SLEEP_INTERVAL_sleep_intervalget_boolZ CFG_DAEMONr<ZCFG_SLEEP_INTERVALrr_secure_boot_hint_sched_knob_paths_cache _ps_whitelist _ps_blacklist_kthread_process_cgroup_ps_blacklist_rerbperfZcpu_map_cpus NameErrorAttributeErrorrcZget_cpusZ _storage_key_scheduler_storage_key _irq_process_irq_storage_key_evlistr _scheduler_utilsrX) rZmonitor_repositoryZstorage_factoryZhardware_inventoryZdevice_matcherZdevice_matcher_udevZplugin_instance_factoryZ global_cfgZ variables __class__rrrs@  zSchedulerPlugin.__init__cCsT|dur dSz t|}Wnty,YdS0|dkr:dStdtt|dS)Nr)rh ValueErrormathZceilr`)rZ mmap_pagesZmprrr_calc_mmap_pagess  z SchedulerPlugin._calc_mmap_pagesc sd|_d|_d|_d|_jji_tjdkr^t d i_j jt _d_d_d_tfdd|jD_|j|_j|jd}|}|dkrt d|d}|durt||krt d ||f|jD]}j|j||j|<qj|jd d d krDd|_t |_!j"r|jrj#rzt$%|_&t$j't$j(t$j)d d ddd d t$j*t$j+Bd }|j,j-|j&dt$.j-|j&|_|j/||dur|j0n|jj0|dWn1Yn0j#sd|_dS)NFTrz0recovering scheduling settings from previous runcsJg|]B\}}|dddkrt|dkr|ddj|fqSNzcgroup.)len_sanitize_cgroup_path _variablesexpand)r/optionr rrr s"z2SchedulerPlugin._instance_init..perf_mmap_pageszKInvalid 'perf_mmap_pages' value specified: '%s', using default kernel valuezL'perf_mmap_pages' value has to be power of two, specified: '%s', using: '%d'Zruntimer0) typeZconfigZtaskcommmmapZfreqZ wakeup_eventsZ watermarkZ sample_type)Zcpusthreads)Zpages)2rxZ_has_dynamic_tuningZ_has_static_tuning_runtime_tuning_storager<ru_scheduler_originalrr`info_restore_ps_affinityunsetr6_cgroups_original_affinityr_cgroup_affinity_initialized_cgroup collections OrderedDictoptionsr8_cgroups _schedulerrrrerrorstrrrj threadingEvent _terminatergrbrqZ thread_mapZ_threadsevselZ TYPE_SOFTWAREZCOUNT_SW_DUMMYZ SAMPLE_TIDZ SAMPLE_CPUopenrrZevlistaddrrc)rinstanceZperf_mmap_pages_rawrr0rrrr_instance_initsj          zSchedulerPlugin._instance_initcCs&|jr"|jD]}t|jqdSr)rx get_pollfdr-closer1)rrfdrrr_instance_cleanup;sz!SchedulerPlugin._instance_cleanupcCs8dtjdddddddddddddddddddddddS)NFTcalcZfalse)isolated_corescgroup_mount_pointcgroup_mount_point_initcgroup_groups_initcgroup_for_isolated_corescgroup_ps_blacklist ps_whitelist ps_blacklistkthread_process irq_processdefault_irq_smp_affinityrperf_process_forksched_min_granularity_nssched_base_slice_nssched_latency_nssched_wakeup_granularity_nssched_tunable_scalingsched_migration_cost_nssched_nr_migratenuma_balancing_scan_delay_ms!numa_balancing_scan_period_min_ms!numa_balancing_scan_period_max_msnuma_balancing_scan_size_mb)rfZDEF_CGROUP_MOUNT_POINT)clsrrr_get_config_options@s2z#SchedulerPlugin._get_config_optionscCs|durt|ddSdS)N./)rreplacerrrrr]sz%SchedulerPlugin._sanitize_cgroup_pathcCs>t|tjs|}t|}t|}||r:d|d}|S)N[]) isinstanceprocfsprocessZprocess_cmdline _is_kthread)rrrBr rrr _get_cmdlineas     zSchedulerPlugin._get_cmdlinec Cst}|i}|D]}z^|js6||r6Wq||}|d}|||<d|vr||dD]}||}|||<qdWqtt fy}z4|j t j ks|j t j krWYd}~qnWYd}~qd}~00q|S)NrBr) rpidstatsreload_threadsr:rorrkeysOSErrorIOErrorerrnoENOENTESRCH)rps processesprocrrBerrr get_processesks*     zSchedulerPlugin.get_processescCs@|j|}|j|}|j|}td|||f||fS)Nz8Read scheduler policy '%s' and priority '%d' of PID '%d')ryrCr?rQr`debug)rrBr sched_strr rrr_get_rts   zSchedulerPlugin._get_rtc Cs|j|}td|||fzB|j|}|j|}||ksJ||kr`td||||fWn6ttfy}ztd|WYd}~n d}~00z|j |||Wnbttfy}zDt |dr|j t j krtd|ntd||fWYd}~n d}~00dS)NzBSetting scheduler policy to '%s' and priority to '%d' of PID '%d'.z9Priority for %s must be in range %d - %d. '%d' was given.z(Failed to get allowed priority range: %srzAFailed to set scheduling parameters of PID %d, the task vanished.z1Failed to set scheduling parameters of PID %d: %s) ryr?r`rrTrVr SystemErrorrrIhasattrrr)rrBrGrHrZprio_minZprio_maxrrrr_set_rts6    zSchedulerPlugin._set_rtcCs|ddtjj@dkS)Nstatflagsr)rZpidstatZ PF_KTHREADrrrrrrszSchedulerPlugin._is_kthreadcCs&|jdkrdSt|j||duS)Nr\F)rpresearch_get_stat_cgrouprrrr_process_in_blacklisted_cgroups z.SchedulerPlugin._process_in_blacklisted_cgroupc Cs|j}z|dddkr,td|WdS||rJtd|WdS|dr||rptd|ntd|WdStd |||ftd || |ftd |t |j |fWnt tfyH}zV|jtjks|jtjkr"td |WYd}~dStd ||fWYd}~nDd}~0ttfy}ztd ||fWYd}~n d}~00dS)NrstateZz9Affinity of zombie task with PID %d could not be changed.Tz`Affinity of task with PID %d could not be changed, the task was moved into a blacklisted cgroup.z[Affinity of kernel thread with PID %d cannot be changed, the task's affinity mask is fixed.zRAffinity of task with PID %d cannot be changed, the task's affinity mask is fixed.zTask %d cmdline: %szTask %d cgroup: %szTask %d affinity: %sz6Failed to get task info for PID %d, the task vanished.z&Failed to get task info for PID %d: %sF)rBr`rrZis_bound_to_cpurrarrrlistryrKrrrrrrrtKeyError)rrrBrrrr_ignore_set_affinity_errorsL   "z*SchedulerPlugin._ignore_set_affinity_errorcCsZz|j|}Wn&ty4t|j}||j|<Yn0|jdurV|jdurV||_||_dSr)rrrrr r )rrBr r paramsrrr_store_orig_process_rts  z&SchedulerPlugin._store_orig_process_rtc Csd}|dur|dur|Sz:||\}}|dur4|}||||||||Wnvttfy}zZt|dr|jtjkrt d|||j vr|j |=d}nt d||fWYd}~n d}~00|S)NTrz=Failed to read scheduler policy of PID %d, the task vanished.FzcRefusing to set scheduler and priority of PID %d, reading original scheduling parameters failed: %s) rrrrrrrrr`rrr)rrBrGrHcontZ prev_schedZ prev_priorrrr_tune_process_rts* z SchedulerPlugin._tune_process_rtcCst|dddkSr)r)rr rrr_is_cgroup_affinitysz#SchedulerPlugin._is_cgroup_affinityFcCs`z|j|}Wn&ty4t|j}||j|<Yn0|jdur\|jdur\|rV||_n||_dSr)rrrrr r )rrBr is_cgrouprrrr_store_orig_process_affinitys  z,SchedulerPlugin._store_orig_process_affinityc Csp|jjdtjt|dfdddD]D}z,|dddd}|dkrN|nd WStyhYq&0q&d S) N%s/%s/%sr Tno_error z:cpuset:rr\r)r read_filerfZPROCFS_MOUNT_POINTrsplit IndexError)rrBlr rrr_get_cgroup_affinitys* z$SchedulerPlugin._get_cgroup_affinitycCsB||}|j}|dkr$d||f}|jjd|t|dddS)Nr%s/%sz%s/tasksTr)r_cgroup_mount_pointr write_to_filer)rrBr pathrrr _set_cgroups   zSchedulerPlugin._set_cgroupcCs,|dd}t|t o"t|dk}||fS)Nrr)rrr)rr rrrr_parse_cgroup_affinitys z&SchedulerPlugin._parse_cgroup_affinityc Csd}|dur|Szd||\}}|r<||}|||n(||}|rX||||}|||||||Wnvttfy}zZt |dr|j t j krt d|||jvr|j|=d}nt d||fWYd}~n d}~00|S)NTrz5Failed to read affinity of PID %d, the task vanished.FzLRefusing to set CPU affinity of PID %d, reading original affinity failed: %s)rrr _get_affinity_get_intersect_affinity _set_affinityrrrrrrr`rrr) rrBr intersectrrr prev_affinityrrrr_tune_process_affinity s<    z&SchedulerPlugin._tune_process_affinitycCsD||||}|sdS|||}|r0||jvr4dS||j|_dSr)rrrr )rrBrrGrHr rrrr _tune_process?s zSchedulerPlugin._tune_processcCsf|j|}|dur.|dkr.td|dSz t|}Wn"ty\td|YdS0||fS)Nrz>Invalid scheduler: %s. Scheduler and priority will be ignored.)NNz=Invalid priority: %s. Scheduler and priority will be ignored.)ryr>r`rrhr})rr=Z str_priorityr r rrr_convert_sched_paramsHs   z%SchedulerPlugin._convert_sched_paramscCsD|dkrd}n2||r|}n"|j|}|s@td|d}|S)Nrz)Invalid affinity: %s. It will be ignored.)rr hex2cpulistr`r)rZ str_affinityr rrr_convert_affinityWs  z!SchedulerPlugin._convert_affinitycCs6|\}}}}}|||\}}||}|||||fSr)r r )rvals rule_prior r r regexrrr_convert_sched_cfgds  z"SchedulerPlugin._convert_sched_cfgc Csd|j|f}zt|tjWn6tyV}ztd||fWYd}~n d}~00|jj d|df|jj d|jdfddddstd|dS)Nrz Unable to create cgroup '%s': %sz cpuset.memsTrz3Unable to initialize 'cpuset.mems ' for cgroup '%s') rr-mkdirrfDEF_CGROUP_MODErr`rrrr)rr rrrrr_cgroup_create_groupks(z$SchedulerPlugin._cgroup_create_groupcCs<|jdur"|j|jvr"||j|jD]}||q(dSr)rrrrcgrrr_cgroup_initialize_groupsvs  z)SchedulerPlugin._cgroup_initialize_groupsc Cstdzt|jtjWn2tyP}ztd|WYd}~n d}~00|j dddddd|jg\}}|dkrtd |jdS) NzInitializing cgroups settingsz'Unable to create cgroup mount point: %sZmountz-tr z-oZcpusetrzUnable to mount '%s') r`rr-makedirsrrfrrrrexecute)rrretoutrrr_cgroup_initialize|s $ z"SchedulerPlugin._cgroup_initializec CsJzt|Wn6tyD}ztd||fWYd}~n d}~00dS)Nz#Unable to remove directory '%s': %s)r-rmdirrr`r)rr rrrr _remove_dirszSchedulerPlugin._remove_dircCsTt|jD]}|d|j|fq |jdurP|j|jvrP|d|j|jfdS)Nr)reversedrrrrrrrr_cgroup_finalize_groupssz'SchedulerPlugin._cgroup_finalize_groupscCsltd|jd|jg\}}|dkr.cs6g|].\}}td|rt|dkr||fqS)zgroup\.)rmatchrr)r/rr rrrrs  cSs |ddS)Nrrr)Z option_valsrrrr3z8SchedulerPlugin._instance_apply_static..)keyz(error compiling regular expression: '%s'cs(g|] \}}t|dur||fqSr)rrr/rBr)r(rrrr3c s$g|]\}}||ffqSrrr4)r rr rr rrrsz(?.(?.ra)rJrrrrmrbrrrrms zSchedulerPlugin._ps_whitelistrcCs:|rdS|r6|dur6dddtdt|D|_dS)Nr^cSsg|] }d|qSr_rr`rrrrr3z1SchedulerPlugin._ps_blacklist..ra)rJrrrrnrbrrrrns zSchedulerPlugin._ps_blacklistrcCs*|rdS|r&|dur&|j|dk|_dSNr,)rrjrorbrrrros z SchedulerPlugin._kthread_processrcCs*|rdS|r&|dur&|j|dk|_dSrf)rrjrvrbrrrrvs zSchedulerPlugin._irq_processrcCs6|rdS|r2|dur2|dvr$||_n|j||_dS)N)rignore)_default_irq_smp_affinity_valuercpulist_unpackrbrrr_default_irq_smp_affinitys  z)SchedulerPlugin._default_irq_smp_affinityrcCs*|rdS|r&|dur&|j|dk|_dSrf)rrjr[rbrrr_perf_process_forks z"SchedulerPlugin._perf_process_forkcCs"|j|}td||f|S)NzRead affinity '%s' of PID %d)ryrKr`r)rrBresrrrrs zSchedulerPlugin._get_affinityc Cst|}||r&td|dStd||fz|j||WnFttfy}z*| |s|t d|||fWYd}~n d}~00dS)NzMNot setting CPU affinity of PID %d, the task belongs to a blacklisted cgroup.z'Setting CPU affinity of PID %d to '%s'.z,Failed to set affinity of PID %d to '%s': %s) rrrr`rryrNrrrr)rrBr rrrrrrs   zSchedulerPlugin._set_affinitycCs"t|t|}|rt|S|Sr)r? intersectionr)rZ affinity1Z affinity2Z affinity3Zaffrrrrsz'SchedulerPlugin._get_intersect_affinityc sD|}jsfdd|D}fdd|D}jdkrJfdd|D}tdd|D}|D]}z||}Wnnttfy}zR|jtjks|jtjkrt d|nt d||fWYd}~q`WYd}~n d}~00j ||d d } | sq`|j vr|j |_|s`d ||vr`||d |d q`dS) Ncsg|]}|s|qSr)rr`rrrrr3z9SchedulerPlugin._set_all_obj_affinity..cs(g|] }tj|dur|qSr)rrrm_get_stat_commr`rrrrsr\cs(g|] }tj|dur|qSr)rrrnrnr`rrrrscSsg|]}|j|fqSr)rBr`rrrrr3rSzARefusing to set affinity of PID %d, failed to get its cmdline: %sT)rr)rornr6rrrrrrr`rrrrr _set_all_obj_affinityr:) rZobjsr rZpslZpsdrBrrrrrrrosB   "  z%SchedulerPlugin._set_all_obj_affinityc Cs*z |dWStttfy$YdS0dS)NZcgroupsr\rrrrr)rrrrs z SchedulerPlugin._get_stat_cgroupc Cs.z|ddWStttfy(YdS0dS)Nrrr\rprqrrrrn szSchedulerPlugin._get_stat_commc Csbz&t}||||dWn6ttfy\}ztd|WYd}~n d}~00dS)NFr-) rrrror:rrr`r)rr rrrrr_set_ps_affinitysz SchedulerPlugin._set_ps_affinityc Csz`|j|}td||fd|}t|d}||Wdn1sR0YWdSttfy}zdt|dr|j t j kr|std|WYd}~dSt d|||fWYd}~d SWYd}~n d}~00dS) Nz&Setting SMP affinity of IRQ %s to '%s'z/proc/irq/%s/smp_affinitywrrz/Setting SMP affinity of IRQ %s is not supportedz0Failed to set SMP affinity of IRQ %s to '%s': %s) r cpulist2hexr`rrwriterrrrZEIOr)rrdr Z restoring affinity_hexfilenamer&rrrr_set_irq_affinitys*  (z!SchedulerPlugin._set_irq_affinityc CszR|j|}td|tdd}||Wdn1sF0YWn:ttfy}ztd||fWYd}~n d}~00dS)Nz(Setting default SMP IRQ affinity to '%s'/proc/irq/default_smp_affinityrsz2Failed to set default SMP IRQ affinity to '%s': %s) rrvr`rrrwrrr)rr rxr&rrrr_set_default_irq_affinity6s  ,z)SchedulerPlugin._set_default_irq_affinityc Cst}t}|D]}z"||d}td||fWntyRYqYn0||||}t|t|krtq| ||d}|dkr||j |<q|dkr|j |q|j d}|j |}|jdkr||||}n|jdkr|j}|jdkr ||||_|j|j|dS) Nr zRead affinity of IRQ '%s': '%s'Frrtr{rrg)rr interruptsrr`rrrr?rzrrappendrrr rhr|rrrw) rr irq_originalrrdrrrlZprev_affinity_hexrrr_set_all_irq_affinityAs8            z%SchedulerPlugin._set_all_irq_affinitycCsj|j|jd}|durdS|jD]\}}|||dq&|jdkrX|j}|||j |jdS)NTrg) rr<rwrr8rzrhrr|r)rrrdr rrr_restore_all_irq_affinity`s  z)SchedulerPlugin._restore_all_irq_affinitycCsFt|t|}|r,ttj||fnttj|||f|Sr)r?issubsetr`rrfrLrrM)rirq_descriptioncorrect_affinityrNrlrrr_verify_irq_affinityksz$SchedulerPlugin._verify_irq_affinityc Cs|j|jd}t}d}|D]}||jvrP|rPd|}tt j |q$z<||d}t d||fd|} | | ||sd}Wq$t yYq$Yq$0q$|jd} |j| }|jdkr| d ||jd kr|n|jsd}|S) NTz-IRQ %s does not support changing SMP affinityr z#Read SMP affinity of IRQ '%s': '%s'zSMP affinity of IRQ %sFr{rgzdefault IRQ SMP affinityr)rr<rwrr}rrr`rrfZ STR_VERIFY_PROFILE_VALUE_MISSINGrrrrrr rh) rrrRrrrlrd descriptionrNrZcurrent_affinity_hexrrr_verify_all_irq_affinityws@     z(SchedulerPlugin._verify_all_irq_affinityr )r]r c Csd}d|_|durrt|j|}t|j}||rRt||}|j||_n |j|j} t d|| f|sz|r|durdS|r|j r| ||SdS|r|j r| d|j } n|} || |j r||n|j r|dS)NzJInvalid isolated_cores specified, '%s' does not match available cores '%s'Tz cgroup.%s)rr?rrirrrrrKr`rrvrrr*rrrr) rrcrrdrRrr isolatedZpresentZstr_cpusZ ps_affinityrrr_isolated_coress:       zSchedulerPlugin._isolated_corescCs,|dkrd||f}nd|||f}d|S)Nr\rrz/sys/kernel/debug/%sr)rprefix namespaceknobrrrr_sched_assembly_pathsz$SchedulerPlugin._sched_assembly_pathcCs<|}|dkr8|j|}|dur8|r4||||}nd}|S)NrGr\)_dict_sched_knob_mapr<r)rrrrrZlpathZlknobrrr_sched_assembly_path2s z%SchedulerPlugin._sched_assembly_path2cCsd|||f}|j|}|s&|dkr*|Sd||f}tj|s||||}tj|sl|||||}|dkr|jdurd|_||j|<|S)Nz%s_%s_%sr\z/proc/sys/kernel/%s_%sT)rlr<r-rexistsrrrk)rrrrr3rrrr_get_sched_knob_paths      z$SchedulerPlugin._get_sched_knob_pathcCsZd}||||}|dkrV|jj|dd}|durVtd||jrVtdd|_|S)Nr\)r&zError reading '%s'zUThis may not work with Secure Boot or kernel_lockdown (this hint is logged only once)F)rrrr`rrk)rrrrrFrrrr_get_sched_knobs zSchedulerPlugin._get_sched_knobcCsh|dur dS||||}|s0td|dS|sd|jj|||rJtjgnddsdtd||f|S)Nz(knob '%s' ignored, unsupported by kernelFrz Error writing value '%s' to '%s')rr`rrrrrr)rrrrrsimremoverrrr_set_sched_knobs zSchedulerPlugin._set_sched_knobrcCs|dddSNr\rGr^rrrrrr_get_sched_min_granularity_nssz-SchedulerPlugin._get_sched_min_granularity_nscCs|ddd|||Srrrrrrrrrr_set_sched_min_granularity_nssz-SchedulerPlugin._set_sched_min_granularity_nsrcCs ||Sr)rrrrr_get_sched_base_slice_nssz(SchedulerPlugin._get_sched_base_slice_nscCs|||||Sr)rrrrr_set_sched_base_slice_nssz(SchedulerPlugin._set_sched_base_slice_nsrcCs|dddSNr\rGr_rrrrr_get_sched_latency_nssz%SchedulerPlugin._get_sched_latency_nscCs|ddd|||Srrrrrr_set_sched_latency_ns sz%SchedulerPlugin._set_sched_latency_nsrcCs|dddSNr\rGr]rrrrr _get_sched_wakeup_granularity_nssz0SchedulerPlugin._get_sched_wakeup_granularity_nscCs|ddd|||Srrrrrr _set_sched_wakeup_granularity_nssz0SchedulerPlugin._set_sched_wakeup_granularity_nsrcCs|dddSNr\rGZtunable_scalingrrrrr_get_sched_tunable_scalingsz*SchedulerPlugin._get_sched_tunable_scalingcCs|ddd|||Srrrrrr_set_sched_tunable_scalingsz*SchedulerPlugin._set_sched_tunable_scalingrcCs|dddSNr\rGZmigration_cost_nsrrrrr_get_sched_migration_cost_nssz,SchedulerPlugin._get_sched_migration_cost_nscCs|ddd|||Srrrrrr_set_sched_migration_cost_ns#sz,SchedulerPlugin._set_sched_migration_cost_nsrcCs|dddSNr\rGZ nr_migraterrrrr_get_sched_nr_migrate'sz%SchedulerPlugin._get_sched_nr_migratecCs|ddd|||Srrrrrr_set_sched_nr_migrate+sz%SchedulerPlugin._set_sched_nr_migratercCs|dddSNrGnuma_balancingZ scan_delay_msrrrrr!_get_numa_balancing_scan_delay_ms/sz1SchedulerPlugin._get_numa_balancing_scan_delay_mscCs|ddd|||Srrrrrr!_set_numa_balancing_scan_delay_ms3sz1SchedulerPlugin._set_numa_balancing_scan_delay_msrcCs|dddSNrGrZscan_period_min_msrrrrr&_get_numa_balancing_scan_period_min_ms7sz6SchedulerPlugin._get_numa_balancing_scan_period_min_mscCs|ddd|||Srrrrrr&_set_numa_balancing_scan_period_min_ms;sz6SchedulerPlugin._set_numa_balancing_scan_period_min_msrcCs|dddSNrGrZscan_period_max_msrrrrr&_get_numa_balancing_scan_period_max_ms?sz6SchedulerPlugin._get_numa_balancing_scan_period_max_mscCs|ddd|||Srrrrrr&_set_numa_balancing_scan_period_max_msCsz6SchedulerPlugin._set_numa_balancing_scan_period_max_msrcCs|dddSNrGrZ scan_size_mbrrrrr _get_numa_balancing_scan_size_mbGsz0SchedulerPlugin._get_numa_balancing_scan_size_mbcCs|ddd|||Srrrrrr _set_numa_balancing_scan_size_mbKsz0SchedulerPlugin._set_numa_balancing_scan_size_mb)F)F)F)F)F)jrrrrWrrcrrrr classmethodrrrrrrrrrrrrrrrrrr r r rrrrrrr"r)r*r+r9rrGrHrfZ ROLLBACK_SOFTrIrOrPrQrUrVrAZcommand_customrermrnrorvrjrkrrrrorrnrrrzr|rrrrrrrrrrZ command_getrZ command_setrrrrrrrrrrrrrrrrrrrrr __classcell__rrrzrr[s0 ' A  "           <                 "      $                       r[)!r\rZ decoratorsZ tuned.logsZtunedr subprocessrrq ImportErrorrWZ tuned.constsrfrZtuned.utils.commandsrrr-rr~r!rtrZZlogsr<r`objectrrr rXZPluginr[rrrrs6        /