a ”ˆ«hJNã@s(ddlmZmZddlmZddlmZmZmZddl m Z m Z m Z m Z ddlmZddlmZddlZddlZddlZddlZddlZe ¡ZdZd Zd Zd Zd Zd Zd ZdZ e e e dœZ!Gdd„deƒZ"Gdd„dej#ƒZ$Gdd„dej#ƒZ%Gdd„de&ƒZ'Gdd„de&ƒZ(Gdd„dej)j*ƒZ+dS)é)ÚexportsÚlogs)Úcommands)ÚPPD_CONFIG_FILEÚPPD_BASE_PROFILE_FILEÚPPD_API_COMPATIBILITY)Ú PPDConfigÚPPD_PERFORMANCEÚ PPD_BALANCEDÚPPD_POWER_SAVER)ÚEnum)ÚRandomNÚtunedz-/sys/devices/system/cpu/intel_pstate/no_turboz4/sys/bus/platform/devices/thinkpad_acpi/dytc_lapmodeÚunknownzorg.freedesktop.UPowerz/org/freedesktop/UPowerz#/sys/firmware/acpi/platform_profile)z low-powerZbalancedZ performancec@seZdZdZdZdZdZdS)ÚPerformanceDegradedz7 Possible reasons for performance degradation. Úz lap-detectedzhigh-operating-temperatureN)Ú__name__Ú __module__Ú __qualname__Ú__doc__ÚNONEÚ LAP_DETECTEDÚHIGH_OPERATING_TEMPERATURE©rrú8/usr/lib/python3.9/site-packages/tuned/ppd/controller.pyr!srcs(eZdZdZ‡fdd„Zdd„Z‡ZS)ÚPerformanceDegradedEventHandlerz= Event handler for checking performance degradation. cstt|ƒ ¡||_||_dS©N)ÚsuperrÚ__init__Ú _controllerÚ_path)ÚselfÚ controllerÚpath©Ú __class__rrr.sz(PerformanceDegradedEventHandler.__init__cCs|j|jkrdS|j ¡dSr)Úpathnamer rÚcheck_performance_degraded©r!ZeventrrrÚprocess_IN_MODIFY3s z1PerformanceDegradedEventHandler.process_IN_MODIFY)rrrrrr)Ú __classcell__rrr$rr*s rcsDeZdZdZdZ‡fdd„Zdd„Zdd„Zd d „Zd d „Z ‡Z S) ÚPlatformProfileEventHandlera Event handler for switching PPD profiles based on the ACPI platform profile This handler should only invoke a PPD profile change if the change of the file at PLATFORM_PROFILE_PATH comes from within the kernel (e.g., when the user presses Fn-L on a Thinkpad laptop). This is currently detected as the file being modified without being opened before. gš™™™™™¹?cs$tt|ƒ ¡||_d|_d|_dS)NFr)rr+rrÚ _file_openÚ _last_close©r!r"r$rrrFsz$PlatformProfileEventHandler.__init__cCs|jtkrdSd|_d|_dS)NTr)r&ÚPLATFORM_PROFILE_PATHr,r-r(rrrÚprocess_IN_OPENLs z+PlatformProfileEventHandler.process_IN_OPENcCs"|jtkrdSd|_t ¡|_dS©NF)r&r/r,Útimer-r(rrrÚprocess_IN_CLOSE_WRITERs z2PlatformProfileEventHandler.process_IN_CLOSE_WRITEcCs|jtkrdSd|_dSr1)r&r/r,r(rrrÚprocess_IN_CLOSE_NOWRITEXs z4PlatformProfileEventHandler.process_IN_CLOSE_NOWRITEcCs6|jtks$|js$|j|jt ¡kr(dS|j ¡dSr)r&r/r,r-ÚCLOSE_MODIFY_BUFFERr2rÚcheck_platform_profiler(rrrr)]s$z-PlatformProfileEventHandler.process_IN_MODIFY) rrrrr5rr0r3r4r)r*rrr$rr+9s  r+c@s eZdZdZdd„Zdd„ZdS)Ú ProfileHoldz} Class holding information about a single profile hold, i.e., a temporary profile switch requested by a process. cCs"||_||_||_||_||_dSr)ÚprofileÚreasonÚapp_idÚcallerÚwatch)r!r8r9r:r;r<rrrrks zProfileHold.__init__cCs|j|j|jdœS)zF Returns the hold information as a Python dictionary. )ÚProfileZReasonZ ApplicationId)r8r9r:©r!rrrÚas_dictrsýzProfileHold.as_dictN)rrrrrr?rrrrr7fsr7c@s`eZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z dd„Z dd„Z dd„Z dd„Z dS)ÚProfileHoldManagerzÙ Manager of profile holds responsible for their creation/deletion and for choosing the effective one. Holds are identified using integer cookies which are distributed to the hold-requesting processes. cCsi|_tƒ|_||_dSr)Ú_holdsr Ú_cookie_generatorrr.rrrrƒszProfileHoldManager.__init__cs‡‡‡fdd„}|S)z– Returns the callback to invoke when the process with the given ID (which requested a hold with the given cookie) disappears. cs(|dkr$t dˆˆf¡ˆ ˆ¡dS)Nrz1Application '%s' disappeared, releasing hold '%s')ÚlogÚinfoÚremove)Úname©r:Úcookier!rrÚcallbacksz6ProfileHoldManager._removal_callback..callbackr)r!rHr:rIrrGrÚ_removal_callbackˆsz$ProfileHoldManager._removal_callbackcCs tdd„|j ¡DƒƒrtStS)zJ Returns the hold to use from the set of all active ones. css|]}|jtkVqdSr)r8r ©Ú.0ÚholdrrrÚ ˜óz=ProfileHoldManager._effective_hold_profile..)ÚanyrAÚvaluesr r r>rrrÚ_effective_hold_profile”sz*ProfileHoldManager._effective_hold_profilecCs\||jvrdS|j |¡}|j ¡t d|¡t d| ¡¡t  d||j |j f¡dS)zC Cancels the hold saved under the provided cookie. NÚProfileReleasedÚActiveProfileHoldsz5Releasing hold '%s': profile '%s' by application '%s') rAÚpopr<ÚcancelrZ send_signalÚproperty_changedÚ as_dbus_arrayrCrDr8r:)r!rHrMrrrÚ_cancelœs    zProfileHoldManager._cancelcCstjdd„|j ¡DƒddS)zY Returns the information about current holds as a DBus-compatible array. cSsg|] }| ¡‘qSr)r?rKrrrÚ ¬rOz4ProfileHoldManager.as_dbus_array..úa{sv}©Z signature)ÚdbusÚArrayrArQr>rrrrX¨sz ProfileHoldManager.as_dbus_arraycCsŒd}|dks||jvr&|j dd¡}q|jj || ||¡¡}t d|||f¡t |||||ƒ|j|<t   d|  ¡¡|j  | ¡¡|S)z* Adds a new profile hold. rlÿÿz2Adding hold '%s': profile '%s' by application '%s'rT)rArBZrandintrÚbusZwatch_name_ownerrJrCrDr7rrWrXÚswitch_profilerR)r!r8r9r:r;rHr<rrrÚadd®szProfileHoldManager.addcCs ||jvS)zI Returns True if there is a hold under the given cookie. )rA©r!rHrrrÚhas¼szProfileHoldManager.hascCs:| |¡t|jƒdkr"| ¡}n|jj}|j |¡dS)zf Releases the hold saved under the provided cookie and sets the next profile. rN)rYÚlenrArRrÚ base_profiler`)r!rHÚ new_profilerrrrEÂs   zProfileHoldManager.removecCs"t|j ¡ƒD]}| |¡qdS)z- Releases all profile holds. N)ÚlistrAÚkeysrYrbrrrÚclearÎszProfileHoldManager.clearcCs||jvo|j|j|kSr)rAr;©r!rHr;rrrÚ check_callerÕszProfileHoldManager.check_callerN)rrrrrrJrRrYrXrarcrErirkrrrrr@}s   r@cs^eZdZdZ‡fdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z dd„Z dd„Z dd„Z dd„Z dd„Zdd„Zedd„ƒZedd„ƒZdd„Zd d!„Ze d"d#d$¡d%d&„ƒZe d#d'd(¡d)d*„ƒZe d#¡d+d,„ƒZe d-d.¡d/d0„ƒZe d-¡d1d2„ƒZe d3¡d4d5„ƒZe d6¡d7d8„ƒZ e d9¡d:d;„ƒZ!e d<¡d=d>„ƒZ"e d?¡d@dA„ƒZ#‡Z$S)BÚ ControllerzF The main tuned-ppd controller, exporting its DBus interface. cs¢tt|ƒ ¡||_||_tƒ|_t ¡|_ d|_ d|_ t   ¡|_t  |j¡|_i|_g|_tj t¡|_tj t¡|_tj t¡|_|j d|j¡| ¡dS)NFZprofile_changed)rrlrÚ_busÚ_tuned_interfacerÚ_cmdÚ threadingÚEventÚ _terminateÚ_battery_handlerÚ _on_batteryÚ pyinotifyZ WatchManagerÚ_watch_managerZThreadedNotifierÚ _notifierÚ_inotify_watchesÚ_pinned_virtual_filesÚosr#Úisfiler/Ú_platform_profile_supportedÚ NO_TURBO_PATHÚ_no_turbo_supportedÚ LAP_MODE_PATHÚ_lap_mode_supportedÚconnect_to_signalÚ_tuned_profile_changedÚ initialize)r!r_Ztuned_interfacer$rrrÝs   zController.__init__cCs<t|j td¡ƒ|_t d|jr$dnd¡| |j¡dS)zG The callback to invoke when the power supply changes. Ú OnBatteryzBattery status changed: z DC (battery)z AC (charging)N) ÚboolÚ_upower_propertiesÚGetÚUPOWER_DBUS_INTERFACErtrCrDr`Ú_active_profile)r!Z interfaceZchangedZ invalidatedrrrÚ_upower_changedïszController._upower_changedcCs²|sdS||j ¡kr$t d¡dSz|jj ||j¡}Wn$ty^t }t  d|¡Yn0|j |kr®t  d|¡|j  ¡||_ t d|j ¡|t kr®||_| |¡dS)zM The callback to invoke when TuneD signals a profile change. NzLReceived a profile change signal from TuneD, but it is not relevant anymore.z0TuneD profile changed to an unknown profile '%s'zProfile changed to '%s'Ú ActiveProfile)rnÚactive_profilerCÚdebugÚ_configZ tuned_to_ppdÚgetrtÚKeyErrorÚUNKNOWN_PROFILEZwarningr‰rDÚ_profile_holdsrirrWÚ _base_profileÚ_save_base_profile)r!Ú tuned_profileÚresultZerrstrZ ppd_profilerrrr‚÷s$    z!Controller._tuned_profile_changedc Cs´d|_|jjs,|jdur(|j ¡d|_dSzP|jdurf|j tt¡}t   |t j ¡|_ |  d|j¡|_t|j  td¡ƒ|_Wn2t jjy®}zt |¡WYd}~n d}~00dS)z; Sets up handling of power supply changes. FNZPropertiesChangedr„)rtrŽZbattery_detectionrsrErmZ get_objectÚUPOWER_DBUS_NAMEÚUPOWER_DBUS_PATHr]Z InterfaceZPROPERTIES_IFACEr†rrŠr…r‡rˆÚ exceptionsÚ DBusExceptionrCr)r!Z upower_proxyÚerrorrrrÚ_setup_battery_signalings   z#Controller._setup_battery_signalingcCs |jD] }| ¡q|j ¡|j t|j ¡ƒ¡|jrx|j  t t dƒ¡|j|jj t j t ¡tjt|t ƒdO_|jr¼|j  t tdƒ¡|j|jj t j t¡tjt|tƒdO_|jr|jjr|j  t tdƒ¡|j|jj t j t¡tjtjBtjBtjBt|ƒdO_dS)z/ Sets up inotify file watches. Úr)r#ÚmaskZproc_funN)ryÚcloserirvÚrm_watchrgrxrQr~ÚappendÚopenr}Z add_watchrzr#ÚdirnameruZ IN_MODIFYrr€rr|rŽZsysfs_acpi_monitorr/ZIN_OPENZIN_CLOSE_WRITEZIN_CLOSE_NOWRITEr+©r!ÚfrrrÚ_setup_inotify!s,   þ þ þzController._setup_inotifycCs„tj}tj t¡r,|j t¡ ¡dkr,tj }tj t ¡rR|j t ¡ ¡dkrRtj }||j kr€t  d|j¡||_ t d|j¡dS)ze Checks the current performance degradation status and sends a signal if it changed. Ú1zPerformance degraded: %srN)rrrzr#Úexistsr}roÚ read_fileÚstriprrrÚ_performance_degradedrCrDÚvaluerrW)r!Zperformance_degradedrrrr'9s   z%Controller.check_performance_degradedcCsZ|j t¡ ¡}|tvrdSt d|¡t|}|j ¡|  |¡||_ |  |¡dS)z` Sets the active PPD profile based on the content of the ACPI platform profile. NzPlatform profile changed: %s) ror©r/rªÚPLATFORM_PROFILE_MAPPINGrCrr’rir`r“r”)r!Zplatform_profilerfrrrr6Gs  z!Controller.check_platform_profilecCs|jjtdd ¡pdS)z? Loads and returns the saved PPD base profile. T)Zno_errorN)ror©rrªr>rrrÚ_load_base_profileUszController._load_base_profilecCs|j t|d¡dS)zI Saves the given PPD profile into the base profile file. Ú N)roZ write_to_filer©r!r8rrrr”[szController._save_base_profilecCsN|j ¡}||krdSt d|¡|j |¡\}}|sFt t|ƒ¡t|ƒS)zM Sets the TuneD profile to the given one if not already set. TzSetting TuneD profile to '%s')rnrŒrCrDr`r›Ústrr…)r!r•Zactive_tuned_profileÚokZ error_msgrrrÚ_set_tuned_profileas zController._set_tuned_profilecCsld|_t|ƒ|_tj|_| ¡tt|j ƒ|_ |  ¡|  ¡pD|j j |_| |j¡| |j¡| ¡dS)z- Initializes the controller. N)r‰r@r’rrr«r'rrrnrŽrœr®Zdefault_profiler“r`r”r¦r>rrrrƒns   zController.initializecCsnt ¡|j ¡|j |jd¡s$q|jD] }| ¡q*|j ¡|j   t |j   ¡ƒ¡|j ¡t ¡dS)zK Exports the DBus interface and runs the main daemon loop. éN)rÚstartrwroÚwaitrrryrŸrirvr rgrxrQÚstopr¤rrrÚrun}s     zController.runcCs|jS)zG DBus interface for communication with other services. )rmr>rrrr_ŒszController.buscCs|jS)zÏ The base PPD profile. This is the profile to restore when all profile holds are released or when tuned-ppd is restarted. It may not be equal to the currently active profile. )r“r>rrrre“szController.base_profilecCs|j ¡dS)z4 Stops the main loop of the daemon. N)rrÚsetr>rrrÚ terminateœszController.terminatecCs<| |jj ||j¡¡sdS|j|kr8t d|¡||_dS)zƒ Sets the currently active profile to the given one, if not already set. Does not change the base profile. Fr‹T)r³rŽÚ ppd_to_tunedrrtr‰rrWr°rrrr`¢s   zController.switch_profileZsssÚuz hold-profilecCs6|tkr$|tkr$tj dttf¡‚|j ||||¡S)zT Initiates a profile hold and returns a cookie for referring to it. z'Only '%s' and '%s' profiles may be held)r r r]r™ršr’ra)r!r8r9r:r;rrrÚ HoldProfile®s  ÿzController.HoldProfilerzrelease-profilecCsF|j |¡stj d|¡‚|j ||¡s6tj d¡‚|j |¡dS)z@ Releases a held profile with the given cookie. zNo active hold for cookie '%s'z.r[r\)r]r^rŽr»rhrtrÀrrrÚ get_profilesásþzController.get_profilesZActionscCstjgddS)z[ Returns a DBus array of all available actions (currently there are none). Úsr\)r]r^rÀrrrÚ get_actionsëszController.get_actionsrcCs|jjS)zE Returns the current performance degradation status. )r«r¬rÀrrrÚget_performance_degradedòsz#Controller.get_performance_degradedrTcCs |j ¡S)z? Returns a DBus array of active profile holds. )r’rXrÀrrrÚget_active_profile_holdsùsz#Controller.get_active_profile_holdsZVersioncCstSr)rrÀrrrÚversionszController.version)%rrrrrrŠr‚rœr¦r'r6r®r”r³rƒr¸Úpropertyr_rerºr`rZexportr½r¾ÚsignalrSZproperty_setterr¿Zproperty_getterrÁrÃrÅrÆrÇrÈr*rrr$rrlÙsN               rl),rrrZtuned.utils.commandsrZ tuned.constsrrrZtuned.ppd.configrr r r Úenumr Zrandomr rurpr]rzr2rrCrÂr}rr‘r—r˜rˆr/r­rZ ProcessEventrr+Úobjectr7r@Z interfacesZExportableInterfacerlrrrrÚs:   ý -\