3 @sddlmZddlZddlmZddlmZddl m Z ddl m Z ddl jZddlmZddlZddlZddlZddlZddlZddlZGd d d eZdS) )print_functionN)commands)Locator)TunedAdminDBusException)TunedException)ProfileRecommenderc@seZdZdddejejfddZddZddZ d d Z d d Z d dZ ddZ ddZddZddZddZddZddZddZdfd d!Zdgd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-Zd.d/Zd0d1Zd2d3Zd4d5Zd6d7Z d8d9Z!d:d;Z"dd?Z$d@dAZ%dBdCZ&dDdEZ'dFdGZ(dHdIZ)dJdKZ*dLdMZ+dNdOZ,dhdQdRZ-didSdTZ.djdUdVZ/dkdWdXZ0dYdZZ1d[d\Z2d]d^Z3d_d`Z4dadbZ5dcddZ6deS)lAdminTFcCs||_||_||_||_t||_ttj|_ t j |_ d|_ d|_d|_d|_d|_||_t|_|jrtjjtjtjtj||_y|jjtj|jWn2tk r}z|j|d|_WYdd}~XnXdS)NTF) _dbusZ_debug_async_timeoutr_cmdprofiles_locatorconstsZLOAD_DIRECTORIES_profiles_locator threadingZEvent_daemon_action_finished_daemon_action_profile_daemon_action_result_daemon_action_errstr _controller _log_token _log_levelr_profile_recommendertunedZadminZDBusControllerZDBUS_BUSZDBUS_INTERFACEZ DBUS_OBJECTZset_signal_handlerZSIGNAL_PROFILE_CHANGED_signal_profile_changed_cbr_error)selfZdbusdebugZasyncotimeoutZ log_leveler"/usr/lib/python3.6/admin.py__init__s*    zAdmin.__init__cCst|tjddS)N)file)printsysstderr)rmessager"r"r#r+sz Admin._errorcCs*|jjs&||_||_||_|jjdS)N)rZis_setrrrset)r profile_nameresultZerrstrr"r"r#r.s  z Admin._signal_profile_changed_cbcCsnytjt|jjtjdWnJtk rF}z |jtj kSd}~Xn$t t fk rh}zdSd}~XnXdS)NrFT) oskillintrZ read_filerZPID_FILEOSErrorerrnoZEPERM ValueErrorIOError)rr!r"r"r#_tuned_is_running6szAdmin._tuned_is_runningc-Os6|dks|dkrdSd}d}d}yt|d|}Wn(tk rZ}z d|_WYdd}~XnXyt|d|}WnBtk r}z&|js|jt|d|dSWYdd}~XnX|jr y0|jj|j|jj|f|||jj }Wn4t k r}z|j|d|_WYdd}~XnX|js2|||}|S)Nr FZ _action_dbus_Z_action_z , action '%s' is not implemented) getattrAttributeErrorr rstrrZset_on_exit_action_log_capture_finish set_actionZrunr)rZ action_nameargskwargsactionZ action_dbusresr!r"r"r#r<@s6  z Admin.actioncCshtdxZ|D]R}|ddk rP|ddkrPt|jjd|ddd|dqtd|dqWdS)NzAvailable profiles:rr z- %sr)r&rZ align_str)r profile_namesprofiler"r"r#_print_profiles^s  &zAdmin._print_profilescCsdy|jj}Wn6tk rD}zdd|jjD}WYdd}~XnX|j||j|jjdS)NcSsg|] }|dfqS)r r").0r@r"r"r# ksz4Admin._action_dbus_list_profiles..T)rZ profiles2rprofilesrA_action_dbus_activeexit)rr?r!r"r"r#_action_dbus_list_profilesfs& z Admin._action_dbus_list_profilescCs|j|jj|jdS)NT)rArZget_known_names_summary_action_active)rr"r"r#_action_list_profilespszAdmin._action_list_profilescCs&|jj}|dkrd}|jjd|S)Nr T)ractive_profilerF)rr+r"r"r#_dbus_get_active_profileus   zAdmin._dbus_get_active_profilecCs|jj\}}|S)N)rget_active_profile)rr+manualr"r"r#_get_active_profile|szAdmin._get_active_profilecCs.|jj\}}|dkr|dk }|r(tjStjS)N)rrLrZACTIVE_PROFILE_MANUALZACTIVE_PROFILE_AUTO)rr@rMr"r"r#_get_profile_modeszAdmin._get_profile_modecCs|jj}|dkrd}|S)Nr )rpost_loaded_profile)rr+r"r"r#_dbus_get_post_loaded_profiles z#Admin._dbus_get_post_loaded_profilecCs|jj}|S)N)rZget_post_loaded_profile)rr+r"r"r#_get_post_loaded_profiles zAdmin._get_post_loaded_profilecCsl|ddkrXtdt|dttdt|dttdt|ddStd |d SdS) NrTz Profile name:rzProfile summary:zProfile description:z,Unable to get information about profile '%s'F)r&)rr@ profile_infor"r"r#_print_profile_infos     zAdmin._print_profile_infor cCsB|dkr|j}|r*|j||jj|}n tdd}|jj|S)Nr zNo current active profile.F)rKrVrrUr&rF)rr@r=r"r"r#_action_dbus_profile_infoszAdmin._action_dbus_profile_infocCs||dkrXy |j}|dkr&tddSWn.tk rV}z|jt|dSd}~XnX|j||jj|tj tj gddgS)Nr zNo current active profile.F) rNr&rrr7rVrZget_profile_attrsrZPROFILE_ATTR_SUMMARYZPROFILE_ATTR_DESCRIPTION)rr@r!r"r"r#_action_profile_infoszAdmin._action_profile_infocCs$|dkrtddStd|dS)NzNo current active profile.FzCurrent active profile: %sT)r&)rr+r"r"r#_print_profile_names  zAdmin._print_profile_namecCs|rtd|dS)NzCurrent post-loaded profile: %s)r&)rr+r"r"r#_print_post_loaded_profilesz Admin._print_post_loaded_profilecCs4|j}|j|}|r(|j}|j||jj|S)N)rKrYrQrZrrF)rrJr=rPr"r"r#rEs   zAdmin._action_dbus_activecCsy2|j}|j}|r0|r$|d7}nd}||7}Wn.tk r`}z|jt|dSd}~XnX|dk r|j rtdtd||rtd|dS|j|}|j||S)N r FzKIt seems that tuned daemon is not running, preset profile is not activated.zPreset profile: %szPreset post-loaded profile: %sT) rNrRrrr7r4r&rYrZ)rr+rPr!r=r"r"r#rHs(      zAdmin._action_activecCstd|dS)NzProfile selection mode: )r&)rmoder"r"r#_print_profile_modeszAdmin._print_profile_modecCsB|jj\}}|j||dkr6|j||jjdS|jjdS)Nr FT)rZ profile_moder]rrF)rr\errorr"r"r#_action_dbus_profile_modes    zAdmin._action_dbus_profile_modecCsJy|j}|j|dStk rD}z|jt|dSd}~XnXdS)NTF)rOr]rrr7)rr\r!r"r"r#_action_profile_modes zAdmin._action_profile_modecCs>|r,|jj r:|jj r:|jdd}n|jd||S)NzCannot enable the tuning.FzUnable to switch profile: %s)rZ is_runningstartr)rretmsgr"r"r#_profile_print_statuss  zAdmin._profile_print_statuscCsrtj|j|jkr.td|j|jjdS|jjrn|j|krn|j sbtd|j |jjdS|jjdSdS)NzOperation timed out after waiting %d seconds(s), you may try to increase timeout by using --timeout command line option or using --async.FzError changing profile: %sT) time _timestampr r&rrFrZisSetrrr)rr+r"r"r#_action_dbus_wait_profiles     zAdmin._action_dbus_wait_profilecCs||jdks|jdkrdSy2|jj|j}d|_t|dtjdtjjWn,tk rv}z|jdWYdd}~XnXdS)Nr )endr%zUError: Failed to stop log capture. Restart the TuneD daemon to prevent a memory leak.) rrZlog_capture_finishr&r'r(flushrr)rZlog_msgsr!r"r"r#r8 szAdmin._log_capture_finishcCst|dkr|jSdj|}|dkr2|jjdS|jj|j rn|jdk rn|j dd}|jj |j||_ |jj |\}}|js| r|jj|j ||Stj|_|jj|j||j ||S)Nrr[r F)len_action_dbus_listjoinrrFrclearr rr log_capture_startrZswitch_profilerdrerfr9rg)rrDr+r rbrcr"r"r#_action_dbus_profiles        zAdmin._action_dbus_profilecCs<td|jjdddg\}}|dkr0tdntddS)NzTrying to (re)start tuned...ZservicerZrestartrz#TuneD (re)started, changes applied.zQTuneD (re)start failed, you need to (re)start TuneD by hand for changes to apply.)r&rZexecute)rrbrcr"r"r#_restart_tuned+s  zAdmin._restart_tunedcCsz||jjkrdy|jj|||jdStk r`}z|jt||jddSd}~XqvXn|jd|dSdS)NTzUnable to switch profile.Fz%Requested profile '%s' doesn't exist.)rZget_known_namesrZsave_active_profilerrrrr7)rr+rMr!r"r"r# _set_profile3s zAdmin._set_profilecCs6t|dkr|jSdj|}|dkr*dS|j|dS)Nrr[r FT)rlrIrnrs)rrDr+r"r"r#_action_profileAs   zAdmin._action_profilecCs|jj}|jj|j rF|jdk rF|jdd}|jj|j||_|jj \}}|js`| rt|jj |j ||St j |_ |jj|j||j ||S)Nrjrk)rrecommend_profilerror rr rprZ auto_profilerFrdrerfr9rg)rr+r rbrcr"r"r#_action_dbus_auto_profileIs     zAdmin._action_dbus_auto_profilecCs|jj}|j|dS)NF)r recommendrs)rr+r"r"r#_action_auto_profileYs zAdmin._action_auto_profilecCst|jj|jjdS)NT)r&rrurF)rr"r"r#_action_dbus_recommend_profile]sz$Admin._action_dbus_recommend_profilecCst|jjdS)NT)r&rrw)rr"r"r#_action_recommend_profileaszAdmin._action_recommend_profilecCsr|r|jj}n |jj}|r(tdn0tdtdtdtdtdtdtdtj|jj|S) NzIVerification succeeded, current system settings match the preset profile.zLVerification failed, current system settings differ from the preset profile.z=You can mostly fix this by restarting the TuneD daemon, e.g.:z systemctl restart tunedorz service tuned restartzNSometimes (if some plugins like bootloader are used) a reboot may be required.z&See TuneD log file ('%s') for details.)rZverify_profile_ignore_missingZverify_profiler&rZLOG_FILErF)rignore_missingrbr"r"r#_action_dbus_verify_profilees   z!Admin._action_dbus_verify_profilecCs tddS)Nz Not supported in no_daemon mode.F)r&)rr|r"r"r#_action_verify_profilevszAdmin._action_verify_profilecCs:d}|jj|j||_|jj}|s.|jd|jj|S)NrjrkzCannot disable active profile.r>)rrprrZoffrrF)rr rbr"r"r#_action_dbus_offzs   zAdmin._action_dbus_offcCs tddS)Nz Not supported in no_daemon mode.F)r&)rr"r"r# _action_offszAdmin._action_offrDcCs(|dkr|jS|dkr$|j|dSdS)aDPrint accessible profiles or plugins got from TuneD dbus api Keyword arguments: list_choice -- argument from command line deciding what will be listed verbose -- if True then list plugin's config options and their hints if possible. Functional only with plugin listing, with profiles this argument is omitted rDplugins)verboseN)rG_action_dbus_list_plugins)r list_choicerr"r"r#rms zAdmin._action_dbus_listcCs(|dkr|jS|dkr$|j|dSdS)aaPrint accessible profiles or plugins with no daemon mode Keyword arguments: list_choice -- argument from command line deciding what will be listed verbose -- Plugins cannot be listed in this mode, so verbose argument is here only because argparse module always supplies verbose option and if verbose was not here it would result in error rDr)rN)rI_action_list_plugins)rrrr"r"r# _action_lists zAdmin._action_listcCs|jj}xv|jD]j}t|| st||dkr8q|jj|}x8||D],}td||j|d}|rNtd|qNWqW|jjdS)zvPrint accessible plugins Keyword arguments: verbose -- if is set to True then parameters and hints are printed rz %sNz %sT)rZ get_pluginskeysr&rlZget_plugin_hintsgetrF)rrrpluginZhintsZ parameterZhintr"r"r#rs    zAdmin._action_dbus_list_pluginscCs tddS)Nz Not supported in no_daemon mode.F)r&)rrr"r"r#rszAdmin._action_list_pluginscCs0|jj||\}}|s$|jd||jj|S)NzUnable to acquire devices: %s)rZinstance_acquire_devicesrrF)rdevicesinstancerbrcr"r"r#%_action_dbus_instance_acquire_devicessz+Admin._action_dbus_instance_acquire_devicescCs tddS)Nz Not supported in no_daemon mode.F)r&)rrrr"r"r# _action_instance_acquire_devicessz&Admin._action_instance_acquire_devicescCs^|jj|\}}}|s0|jd||jjdSx |D]\}}td||fq6W|jjdS)NzUnable to list instances: %sFz%s (%s)T)rZ get_instancesrrFr&)r plugin_namerbrcZpairsrrr"r"r#_action_dbus_get_instancess z Admin._action_dbus_get_instancescCs tddS)Nz Not supported in no_daemon mode.F)r&)rrr"r"r#_action_get_instancesszAdmin._action_get_instancescCsR|jj|\}}}|s0|jd||jjdSx|D] }t|q6W|jjdS)NzUnable to list devices: %sFT)rZinstance_get_devicesrrFr&)rrrbrcrZdevicer"r"r#!_action_dbus_instance_get_devicess   z'Admin._action_dbus_instance_get_devicescCs tddS)Nz Not supported in no_daemon mode.F)r&)rrr"r"r#_action_instance_get_devicessz"Admin._action_instance_get_devicesN)r )r )rDF)rDF)F)F)7__name__ __module__ __qualname__rZ ADMIN_TIMEOUTloggingZERRORr$rrr4r<rArGrIrKrNrOrQrRrVrWrXrYrZrErHr]r_r`rdrgr8rqrrrsrtrvrxryrzr}r~rrrmrrrrrrrrrr"r"r"r#r sd              r )Z __future__rZ tuned.adminrZtuned.utils.commandsrZtuned.profilesrr exceptionsrZtuned.exceptionsrZ tuned.constsrZtuned.utils.profile_recommenderrr-r'r1rerrobjectr r"r"r"r#s