3 Ydk@s~ddlmZddlTddlZddlmZddljZddl Z ddl Z ddl Z ddl Z ddl Z ejjZdZGdddejZdS) )hotplug)*N)commandsz$/sys/devices/system/cpu/cpu0/cpuidlecseZdZdZfddZddZddZedd Zd d Z d d Z ddZ ddZ ddZ ddZddZddZddZddZddZd d!Zfd"d#Zejffd$d% Zd&d'Zd(d)Zd*d+Zd,d-Zd.d/Zdkd1d2Zdld3d4Zd5d6Z d7d8Z!d9d:Z"e#d;dd?Z$e%d;dmd@dAZ&dndCdDZ'e#dEdd|_t|_x"|jjdD]}|jj|jqWt|_dS)NTcpu)Z_devices_supportedsetZ _free_devices_hardware_inventoryZ get_devicesaddZsys_nameZ_assigned_devices)rdevicerrr _init_devicess zCPULatencyPlugin._init_devicescsfdd|DS)Ncsg|]}jjd|qS)r)rZ get_device).0x)rrr sz8CPULatencyPlugin._get_device_objects..r)rZdevicesr)rr_get_device_objectssz$CPULatencyPlugin._get_device_objectsc Csddddddddddddd S)Ng?di) load_threshold latency_low latency_high force_latencygovernorsampling_down_factorenergy_perf_bias min_perf_pct max_perf_pctno_turbopm_qos_resume_latency_usenergy_performance_preferencer)rrrr_get_config_optionssz$CPULatencyPlugin._get_config_optionscCsdddddg}tj|_|j|krttj}|jjd}|dkrFd|_n|d ksV|d kr^d|_nd|_t j d |nt j d |j|jdkr|j |j dS) NrZi686Zi585Zi486Zi386Z vendor_idZ GenuineIntelTZ AuthenticAMDZ HygonGenuinez$We are running on an x86 %s platformzWe are running on %s (non x86)) platformmachiner procfscpuinfotagsgetr r loginfo_check_energy_perf_bias_check_intel_pstate)rZ intel_archsrZvendorrrr _check_archs    zCPULatencyPlugin._check_archcCsbd|_d}|jjddgtj|gd\}}|dkr@|dkr@d|_n|dkrTtjd n tjd dS) NFrx86_energy_perf_policyz-r)Z no_errorsrTzgunable to run x86_energy_perf_policy tool, ignoring CPU energy performance bias, is the tool installed?zXyour CPU doesn't support MSR_IA32_ENERGY_PERF_BIAS, ignoring CPU energy performance bias)rrexecuteerrnoENOENTr;warning)rZretcode_unsupportedretcodeoutrrrr= s z(CPULatencyPlugin._check_energy_perf_biascCs"tjjd|_|jrtjddS)Nz$/sys/devices/system/cpu/intel_pstatezintel_pstate detected)ospathexistsrr;r<)rrrrr>sz$CPULatencyPlugin._check_intel_pstatecCs$|jdkrtjjjdg|_|jS)Nflags)rr7r8r9r:)rrrr_get_cpuinfo_flagss z#CPULatencyPlugin._get_cpuinfo_flagscCs t|}|jjt|jddS)NrrA)strrZ is_cpu_onlinereplace)rr!Zsdrrr_is_cpu_online!szCPULatencyPlugin._is_cpu_onlinecCstjjd|S)Nz3/sys/devices/system/cpu/%s/cpufreq/scaling_governor)rHrIrJ)rr!rrr_cpu_has_scaling_governor%sz*CPULatencyPlugin._cpu_has_scaling_governorcCs<|j|stjd|dS|j|s8tjd|dSdS)Nz'%s' is not online, skippingFz.there is no scaling governor fo '%s', skippingT)rOr;debugrP)rr!rrr_check_cpu_can_change_governor(s  z/CPULatencyPlugin._check_cpu_can_change_governorcCsd|_d|_t|jjd|krd|_ytjtj tj |_ Wn*t k rht jdtj d|_YnXd|_|jddkr|jddkr|jjdd|_d|_nd|_|jnd|_t jd|jyt|jd|_Wntk rd|_YnXdS) NTFrz-Unable to open '%s', disabling PM_QoS controlr+r2loadzILatency settings from non-first CPU plugin instance '%s' will be ignored.)Z_has_static_tuningZ_has_dynamic_tuninglistZ _instancesvalues_first_instancerHopenconstsZPATH_CPU_DMA_LATENCYO_WRONLY_cpu_latency_fdOSErrorr;errorr _latencyoptions_monitors_repositoryZcreate _load_monitorr?r<nameZassigned_devices _first_device IndexError)rinstancerrr_instance_init1s*  zCPULatencyPlugin._instance_initcCs4|jr0|jrtj|j|jdk r0|jj|jdS)N)rVr rHcloserZr`r_delete)rrdrrr_instance_cleanupOs   z"CPULatencyPlugin._instance_cleanupcCs|jjd|djS)Nz'/sys/devices/system/cpu/intel_pstate/%s)r read_filestrip)rattrrrr_get_intel_pstate_attrVsz'CPULatencyPlugin._get_intel_pstate_attrcCs|dk r|jjd||dS)Nz'/sys/devices/system/cpu/intel_pstate/%s)r write_to_file)rrkvalrrr_set_intel_pstate_attrYsz'CPULatencyPlugin._set_intel_pstate_attrcCs&|dkr dS|j|}|j|||S)N)rlro)rrkvaluevrrr_getset_intel_pstate_attr]s   z*CPULatencyPlugin._getset_intel_pstate_attrcstt|j||jsdS|jj|jd}|dk r>|j||jr|jj|jd}|j d||_ |jj|jd}|j d||_ |jj|jd}|j d||_ dS)Nr+r/r0r1) rr_instance_apply_staticrVZ _variablesexpandr^ _set_latencyrrrrrr)rrdZforce_latency_valueZ new_value)rrrrsds(       z'CPULatencyPlugin._instance_apply_staticcsLtt|j|||jrH|jrH|jd|j|jd|j|jd|jdS)Nr/r0r1) rr_instance_unapply_staticrVrrorrr)rrdZrollback)rrrrv|s  z)CPULatencyPlugin._instance_unapply_staticcCs|j||dS)N)_instance_update_dynamic)rrdr!rrr_instance_apply_dynamicsz(CPULatencyPlugin._instance_apply_dynamiccCsZ|js t||jkrdS|jjd}||jdkrF|j|jdn|j|jddS)Nsystemr(r*r))rVAssertionErrorrbr`Zget_loadr^ru)rrdr!rSrrrrws  z)CPULatencyPlugin._instance_update_dynamiccCsdS)Nr)rrdr!rrr_instance_unapply_dynamicsz*CPULatencyPlugin._instance_unapply_dynamicc Cs&yt|Sttfk r dSXdS)N)int ValueError TypeError)rsrrr_str2intszCPULatencyPlugin._str2intcCsi|_xztjtD]l}td|}|jj|dddd}|jj|dddd}|dk r|dk r|j|}|dk r||j|j<qWdS)Nz/%s/raT)err_retno_errorlatency)cstates_latencyrHlistdircpuidle_states_pathrrirrj)rdZ cstate_pathrarrrr_read_cstates_latencys  z&CPULatencyPlugin._read_cstates_latencyFcCshtjd||jdkr*tjd|j|jj|d}|rR|dkrRtjddStjdt||S)Nz)getting latency for cstate with name '%s'zreading cstates latency tablerz"skipping latency 0 as set by paramz!cstate name mapped to latency: %s)r;rQrrr:rM)rrano_zerorrrr_get_latency_by_cstate_names    z,CPULatencyPlugin._get_latency_by_cstate_namecCstjdt||j|}|dkr2tjddStdd|}|j|jj|ddd}|rt|dkrttjddStjd t||S) Nz'getting latency for cstate with ID '%s'zcstate ID is invalidz /%s/latencyzstate%dT)rrrz"skipping latency 0 as set by paramzcstate ID mapped to latency: %s)r;rQrMrrrri)rZlidrZ latency_pathrrrr_get_latency_by_cstate_ids    z*CPULatencyPlugin._get_latency_by_cstate_idc Cs:d|_t|jd}tjdx|D]}yt|}tjd|Wntk r"|dddkr|j|dddd}n|dd d kr|j|d d}nz|dd d kr|j|d ddd}nR|dd dkr|j|d d}n.|dkrtjddSd}tjdt|YnX|dk r&Pq&W|dfS)N|zparsing latencyz+parsed directly specified latency value: %drzcstate.id_no_zero:T)r z cstate.id:zcstate.name_no_zero: z cstate.name:noneNonezlatency 'none' specifiedzinvalid latency specified: '%s'F)rr)NT) rrMsplitr;rQr|r}rr)rrZ latenciesrrr_parse_latencys0   zCPULatencyPlugin._parse_latencycCsp|j|\}}| rl|jrl|dkr4tjdd|_n8|j|krltjd|tjd|}tj |j |||_dS)Nztunable to evaluate latency value (probably wrong settings in the 'cpu' section of current profile), disabling PM QoSFzsetting new cpu latency %di) rr r;r\r]r<structpackrHwriterZ)rrskipZ latency_binrrrrus    zCPULatencyPlugin._set_latencycCs|jjd|jjS)Nz>/sys/devices/system/cpu/%s/cpufreq/scaling_available_governors)rrirjr)rr!rrr_get_available_governorssz)CPULatencyPlugin._get_available_governorsr,T) per_devicecCs|j|sdSt|}|jd}dd|D}x&|D]}t|dkr4tjddSq4W|j|}xn|D]N}||kr|stjd||f|jj d||Pqf|sftj d||fqfWtj d d j |d}|S) NrcSsg|] }|jqSr)rj)r#r,rrrr%sz2CPULatencyPlugin._set_governor..rz.The 'governor' option contains an empty value.z!setting governor '%s' on cpu '%s'z3/sys/devices/system/cpu/%s/cpufreq/scaling_governorz7Ignoring governor '%s' on cpu '%s', it is not supportedz.None of the scaling governors is supported: %sz, ) rRrMrlenr;r\rr<rrmrQwarnjoin)rZ governorsr!simr,Zavailable_governorsrrr _set_governors2         zCPULatencyPlugin._set_governorcCsTd}|j|sdS|jjd||dj}t|dkr:|}|dkrPtjd||S)Nz3/sys/devices/system/cpu/%s/cpufreq/scaling_governor)rrz*could not get current governor on cpu '%s')rRrrirjrr;r\)rr!ignore_missingr,datarrr _get_governors  zCPULatencyPlugin._get_governorondemandcCsd|S)Nz7/sys/devices/system/cpu/cpufreq/%s/sampling_down_factorr)rr,rrr_sampling_down_factor_pathsz+CPULatencyPlugin._sampling_down_factor_pathr-r)rZprioritycCsd}||jkr|jjd|j|<|j|}|dkrFtjd|dS|t|jjkr||j|<|j|}tj j |stjd||fdSt |}|stj d||f|j j|||S)NzIignoring sampling_down_factor setting for CPU '%s', cannot match governorzTignoring sampling_down_factor setting for CPU '%s', governor '%s' doesn't support itz6setting sampling_down_factor to '%s' for governor '%s')rclearrr;rQrTrUrrHrIrJrMr<rrm)rr-r!rrnr,rIrrr_set_sampling_down_factors&       z*CPULatencyPlugin._set_sampling_down_factorcCsD|j||d}|dkrdS|j|}tjj|s4dS|jj|jS)N)r)rrrHrIrJrrirj)rr!rr,rIrrr_get_sampling_down_factor4s  z*CPULatencyPlugin._get_sampling_down_factorcCs*|jjdd|t|gdd\}}}||fS)Nr@z-cT)Z return_err)rrBrM)rcpu_idrprFrGerr_msgrrr_try_set_energy_perf_bias>s z*CPULatencyPlugin._try_set_energy_perf_biascCsd||r dndfS)Nz>/sys/devices/system/cpu/cpufreq/policy%s/energy_performance_%sZavailable_preferencesZ preferencer)rrZ availablerrr_intel_preference_pathGsz'CPULatencyPlugin._intel_preference_pathcCsd|S)Nz4/sys/devices/system/cpu/cpu%s/power/energy_perf_biasr)rrrrr_energy_perf_bias_pathJsz'CPULatencyPlugin._energy_perf_bias_pathr.c Csl|j|stjd|dS|jd}|jd}tj|jkr|j|}t j j |r|sxD|D].}|j }|j j||r^tjd||fPq^Wtjd|t|Stjd|dSn|jrd|s\x|D]|}|j }tjd||f|j||\}} |dkrtjd||fPq|dkr8tjd | Pqtjd ||fqWtjd|t|SdSdS) Nz%s is not online, skippingrrz5energy_perf_bias successfully set to '%s' on cpu '%s'zPFailed to set energy_perf_bias on cpu '%s'. Is the value in the profile correct?zXFailed to set energy_perf_bias on cpu '%s' because energy_perf_bias file does not exist.z2Trying to set energy_perf_bias to '%s' on cpu '%s'rz"Failed to set energy_perf_bias: %szHCould not set energy_perf_bias to '%s' on cpu '%s', trying another value)rOr;rQlstriprrXCFG_CPU_EPP_FLAGrLrrHrIrJrjrrmr<r\rMrr) rr.r!rrvalsenergy_perf_bias_pathrnrFrrrr_set_energy_perf_biasMsV             z&CPULatencyPlugin._set_energy_perf_biascCsjy t|}WnXtk rd}zrLrOrPrRrerhrlrorrrsrXZ ROLLBACK_SOFTrvrxrwr{rrrrrrurZ command_setrZ command_getrrrrrrrrrrrrrrrrrr __classcell__rr)rrrsl+           7   r)rArZ decoratorsZ tuned.logsZtunedZtuned.utils.commandsrZ tuned.constsrXrHrrCr5r7Zlogsr:r;rZPluginrrrrrs