o $DYOk @sddlmZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ze e ds=ddl m Z e e _ WneefyMe j e _ YnwddlmZddlmZmZmZmZddlmZddlmZdd lmZdd lmZgd Zd d ZddZ ddZ!ddZ"ddZ#e#Z$Gddde%Z&Gddde%Z'Gddde%Z(Gddde%Z)Gddde%Z*Gd d!d!e+Z,d"d#Z-d$d%Z.d&d'Z/d(e j0dZ1d)d*Z2dS)+)print_functionN monotonic)r) OrderedDict) find_uidsTaskStatsNetlink ProcessListStats) ThreadInfo)VERSION)ioprio)IoprioSetError)BKMGTPEcCs`|dkrd}n |dkrd}| }ndStt|ddd}d|t|dd|>t|fS) Nr-z0.00 B z %s%.2f %s)intmathlogfloatUNITS)sizesignexpor!*/usr/lib/python3/dist-packages/iotop/ui.py human_size;sr#cCs|jr d|dSt|S)Nz%.2f Kg@) kilobytesr#)optionsbytesr!r!r" format_sizeIs r'cCst||o t||dS)Nz/s)r'r)r%rdurationr!r!r"format_bandwidthOsr)c sfdd}jr|j}fdd}t|jn |j}fdd}||j}||j}||j}|j |j } t d| } || } |||| fS)Ncsdtd|dS)Nz%.2f %%g(\X@gcA)min)delay)r(r!r" delay2percentVz#format_stats..delay2percentcs t|SN)r'rr(r%r!r"Z zformat_stats..cs t||Sr.)r)r/r0r!r"r1^sr) accumulated stats_accumtimerstats_accum_timestamp stats_deltablkio_delay_totalswapin_delay_total read_bytes write_bytescancelled_write_bytesmax) r%processr(r,statsdisplay_formatio_delay swapin_delayr: written_bytesr;r!)r(r%r" format_statsSs          rDc CsFz ttdWSty"}z t|WYd}~dSd}~ww)Nz/proc/sys/kernel/pid_max)lenopenreadstrip Exceptionprint)er!r!r"get_max_pid_widthisrMc@ eZdZdS) CancelInputN__name__ __module__ __qualname__r!r!r!r"rOxrOc@rN) InvalidIntNrPr!r!r!r"rU|rTrUc@rN) InvalidPidNrPr!r!r!r"rVrTrVc@rN) InvalidTidNrPr!r!r!r"rWrTrWc@rN)InvalidIoprioDataNrPr!r!r!r"rXrTrXc @seZdZdddfdddfdddfdddfdddfd ddfd ddfd ddfgZd d ZddZddZddZddZddZ d2ddZ d2ddZ ddZ dd Z d!d"Zd#d$Zd3d%d&Zd4d(d)Zd*d+Zd,d-Zd.d/Zd0d1ZdS)5IOTopUIcC|jSr.)pidpsr!r!r"r1zIOTopUI.FcC|Sr.)ioprio_sort_keyr\r!r!r"r1cCr`r.)get_userr\r!r!r"r1rbcC|jSr.)r:r\r!r!r"r1r_TcCs |j|jSr.)r;r<r\r!r!r"r1s cCrdr.)r9r\r!r!r"r1r_cCs|jp t|jp |j Sr.)r8rr:r;r\r!r!r"r1scCr`r.) get_cmdliner\r!r!r"r1rbcCsv||_||_d|_tj|jd|_|jjs9||_|zt t t dWdSt j y8YdSwdS)Nrr) process_listr% sorting_keyrY sorting_keyssorting_reversebatchwinresizecursesuse_default_colors start_colorcurs_seterror)selfrlrgr%r!r!r"__init__szIOTopUI.__init__cCs|j\|_|_dSr.)rlgetmaxyxheightwidthrsr!r!r"rmr-zIOTopUI.resizec Csjd}t}|jjs|tjtjtj B|jj dus#||jj kr|j \}}| |dk|||j j|jj durI|d7}||jj krHdSn|dkrOd}z ||jjd}Wn"tjy|}z|jrq|jdtjkrqg}nWYd}~nd}~ww|D]\}}|tjtjB@rtdq|jjs||r|j} || |jj dus#||jj ks#dSdS)Nrrg@@)selectpollr%rkregistersysstdinfilenoPOLLINPOLLPRI iterationsrgrefresh_processesrefresh_displayr( delay_secondsrrargserrnoEINTRPOLLERRPOLLHUPexitrmrlgetch handle_key) rsrrztotalcurrenteventsrLfdeventkeyr!r!r"runsJ          z IOTopUI.runcCs|j |_dSr.)rjrxr!r!r"reverse_sortingzIOTopUI.reverse_sortingcCs6|j}|||_||jkrtj|jd|_dSdSNr)rhget_sorting_keyrYrirj)rsdeltaorig_sorting_keyr!r!r"adjust_sorting_keys   zIOTopUI.adjust_sorting_keycCs0|j}||7}td|}tttjd|}|S)Nrr)rhr=r*rFrYri)rsrnew_sorting_keyr!r!r"rs  zIOTopUI.get_sorting_keyNcCs|jddtdtjB|j|jdd|tj|jt t d|j dt |}t dt |dvr?|S|rDt|S)Nrr )Nr)rlhlineordrnA_NORMALrwaddstrA_BOLDrefreshechorqgetstrrFnoechorOrspromptdefaultempty_is_cancelinpr!r!r" prompt_strs   zIOTopUI.prompt_strcCs.||||}zt|WStytwr.)rr ValueErrorrUrr!r!r" prompt_ints   zIOTopUI.prompt_intcC0z|dWStyttyw)NzPID to ionice: )rrUrVrOrxr!r!r" prompt_pid   zIOTopUI.prompt_pidcCr)NzTID to ionice: )rrUrWrOrxr!r!r" prompt_tidrzIOTopUI.prompt_tidcCs^z|dur|d||d}n|ddd}Wn ty!tw|dks*|dkr-t|S)Nz'I/O priority data (0-7, currently %s): FzI/O priority data (0-7): r)rrUrX)rs ioprio_datarr!r!r" prompt_data s zIOTopUI.prompt_datac Cs\z||}Wn tyd}Ynwt|d} |jddtdtjB|j|j dd|tj t|}t |D]"\}}d|} ||urKtj } ntj} |j d|| | |t| 7}q; |j } | tjtdfvrv|dkrv|d8}n7| tjtdfvr||kr|d7}n%| tjtd td fvr||S| d tjtjtjtd td fvrtq_q)NrTrrz %s lr  qQ)indexrrFrlrrrnrrwinsstrr enumerate A_REVERSErKEY_LEFT KEY_RIGHT KEY_ENTER KEY_CANCEL KEY_CLOSEKEY_EXITrO) rsr display_listret_listselectedset_lenoffsetiitemdisplayattrrr!r!r" prompt_setsD    zIOTopUI.prompt_setcCs4d}gd}gd}|durd}|||||}|S)NzI/O priority class: )z Real-timez Best-effortIdle)rtbeidler)r)rs ioprio_classrclasses_prompt classes_retrr!r!r" prompt_class;szIOTopUI.prompt_classError!cCsN|jddtdtjB|j|jddd|tj|jt ddS)Nrrrz %s ) rlrrrnrrwrrrr5sleep)rsrrr!r!r" prompt_errorEs zIOTopUI.prompt_errorcCs,|jddtdtjB|j|jdS)Nrrr)rlrrrnrrwrrxr!r!r" prompt_clearKszIOTopUI.prompt_clearcsfdd}fdd}fdd}fdd}itd d d td d d tdfdd tdfdd td|td|td|td|td|td|td|td|tjfdd tjfdd tjfdd tjfdd }||dd }|dS)NcjjdN_dSNT)r%r3r!rxr!r"toggle_accumulatedPz.IOTopUI.handle_key..toggle_accumulatedcrr)r%onlyr!rxr!r"toggle_only_ioSrz*IOTopUI.handle_key..toggle_only_iocs(jjdN_jjdSr)r% processesrgclearrr!rxr!r"toggle_processesVs z,IOTopUI.handle_key..toggle_processesc s^zHjjr}j|}n }t|jj}|}t |\}} |}|dkr2d}n |}| ||jjWn`tyc}zd|jWYd}~dSd}~wtyqdYdStydYdStydYdStydYdStyYdSwdS)NrrzError setting I/O priority: %szInvalid process id!zInvalid thread id!zInvalid I/O priority data!zInvalid integer!)r%rrrg get_processrr taskstats_connection get_ioprior to_class_and_datarr set_iopriorrr rerrrVrWrXrUrOr)r[ exec_unittid ioprio_valuerrrLrxr!r"ionice[sB          z"IOTopUI.handle_key..ionicercS tdSNrr|rr!r!r!r"r1r2z$IOTopUI.handle_key..rcSrrrr!r!r!r"r1r2rcSr.rr!rxr!r"r1rbRcrr.rr!rxr!r"r1rbaAoOr]rrIc dS)Nrrr!rxr!r"r1r2crrrr!rxr!r"r1r2csttj Sr.rrFrYrir!rxr!r"r1scsttjSr.rr!rxr!r"r1scSsdSr.r!r!r!r!r"r1s)rrnrrKEY_HOMEKEY_ENDget)rsrrrrr key_bindingsactionr!rxr"rOsn    #         # zIOTopUI.handle_keycsfdd}fdd}tt|jj}tjjdjj r(ddndd|j fd dj d tt ||S) Nc stj|jj}|\}}}}tjrd||f}nd}dt}|d|j|| dd|||f}| } jj slj t |} d| krOt | krlnn| dd} | | d } | d| d| | d} || 7}jj s{|dj }|S) Nz%7s %7s z ?unavailable? z%%%ddz %4s %-8s %11s %11s %srr~)rDr%rgr(rhas_blkio_delay_total MAX_PID_WIDTHr[rrcrerkrwrF) r]r?rArBr:r; delay_stats pid_formatlinecmdlineremaining_lengthlen1offset2rxr!r"formats*  z IOTopUI.get_data..formatcsjj p |jjSr.)r%r did_some_ior3r]rxr!r" should_formats  z'IOTopUI.get_data..should_formatrcSrZr.)r4rr!r!r"r1r_z"IOTopUI.get_data..cSrZr.)r7rr!r!r"r1r_cs||Sr.r!r)r stats_lambdar!r"r1s)rreverse) listfilterrgrvaluesrYrirhr%r3sortrjmap)rsrrrr!)rrsrr"get_datas    zIOTopUI.get_datac sdtj|d|dtj|d|dfdtj|d|dtj|d|dfg}tdtdd}jjrF|d7}n|d 7}|d d d d dddg}}jjrxdg|}tdfdd|D}fdd|D}jj rjj dkr|D]} t | qjj t |krt d ||D]} t | qtjdSjtjrd} nd} g} g} jjrwd}d}tdddddjjrdnd fd!jjrd"nd#fd$jjrd%nd&fd'd(jrd)nd*fd+||fd,||fd-|dfd.|dfg }d}|D]^\}}|r4d/|d0|g}d|d1kr-dntjddg}n d/|d2g}gd3}| rU|rUtd | |d |j kre| !|| !||d7}q| ||7<| ||7<qt|t t"|t t"| }t| }j#||}|d4krg}d|d5krg}d|d6krd} d}|d7krg} g} d}j#||}t$t||}t%|D]\} j&d| dj q|rj'dt(dtj)Bj d}j }t*t|D]Jtj)}|}j+kr|dd}j+kr-|tj,O}|jr*d8p+d97}|d|}|t|8}|rEj&||||t|7}qt*|D]6z(fd:d;}z||Wnt-yx||.d<YnwWqQtj/yYqQwt*|D]G}j#|||}j'|dt(dtj)Bj d}t*t| |D]!j0||| |tj)| |B|t| |7}qq| rj0j#dd| tj,j1dS)=Nz.Total DISK READ: %s | Total DISK WRITE: %srrz.Current DISK READ: %s | Current DISK WRITE: %srPIDTIDz PRIOz USERz DISK READz DISK WRITEz SWAPINz IOz COMMANDz TIMEz %H:%M:%S cg|]}|qSr!r!).0r current_timer!r" z+IOTopUI.refresh_display..crr!r!)rr^r r!r"r"r#rrzNCONFIG_TASK_DELAY_ACCT not enabled in kernel, cannot determine SWAPIN and IO %r)keysr)anyr)rquit)rrrallactiver]threadsprocsr bandwidthaccum)rrrascdescleftrighthomeendz z: r%:)rrrrErfrr>.print_linezutf-8)2r)r%rjustr=rrrr5strftimerkquietrKrjoinr|stdoutflushrleraserrhelprrrr3rjrIitemsrn A_UNDERLINErFrwappendboolrvr*rrrrrrangerhrUnicodeEncodeErrorencoderrrr) rs first_timerrr(summaryr[titleslinesr^r status_msg help_lines help_attrsprevnextr@ help_liner help_item help_attrlen_status_msglen_help max_lines num_linesposremaining_colsrtitler8lnr r!)r!rr6r7rsr"rs              0                 (zIOTopUI.refresh_displayrr.)r)rQrRrSrirtrmrrrrrrrrrrrrrrrrr!r!r!r"rYs<       !  ! U 'rYcCsf|jr ttjtjndd}ttj|ttj|t|}t||}t|||}| dS)Nc_stddSrr)rkwargsr!r!r" clean_exit]rz$run_iotop_window..clean_exit) rksignalSIGPIPESIG_DFLSIGINTSIGTERMrrrYr)rlr%r]rrguir!r!r"run_iotop_windowYs   rdc Cs~z|jr td|WStt|WSty>}z"|jtjkr2t|tj dtdtj dt dnWYd}~dSd}~ww)N)filea The Linux kernel interfaces that iotop relies on now require root privileges or the NET_ADMIN capability. This change occurred because a security issue (CVE-2011-2494) was found that allows leakage of sensitive data across user boundaries. If you require the ability to run iotop as a non-root user, please configure sudo to allow you to run iotop as root. Please do not file bugs on iotop about this.r) rkrdrnwrapperOSErrorrEPERMrKr|stderrr)r%rLr!r!r" run_iotopgs   rjcCsd}zddl}ddl}td|dtt|||}Wn+tyJddl}ddl }|j |dd}td| || |j |}Ynw||dd |d |d t|dS) Nz iotop.profrzProfiling using cProfilezcontinuation()r) lineeventszProfiling using hotshotr5calls2)cProfilepstatsrKrunctxglobalslocalsr ImportErrorhotshot hotshot.statsProfileruncallcloser?load strip_dirs sort_stats print_stats print_calleesosremove) continuation prof_filernror?rtprofr!r!r"_profiles*     raF%s [OPTIONS] DISK READ and DISK WRITE are the block I/O bandwidth used during the sampling period. SWAPIN and IO are the percentages of time the thread spent respectively while swapping in and waiting on I/O more generally. PRIO is the I/O priority at which the thread is running (set using the ionice command). Controls: left and right arrows to change the sorting column, r to invert the sorting order, o to toggle the --only option, p to toggle the --processes option, a to toggle the --accumulated option, i to change I/O priority, q to quit, any other key to force a refresh.c sz ttjdWntjytdYnwtjtdtd}|j ddddd d d |j d d dddd|j ddddddd|j dddddddd|j d d!dd"d#d$d%d&|j d'd(d)d*d#d+d,d&|j d-d.dd/d d0d |j d1d2dd3d d4d |j d5d6dd7d d8d |j d9d:dd;dd?d@dAdBd |j dCddDd tj d |j dEdFdGdHdId | \}|r| dJdK |tjpg_jpԈjpԈj_fdLdMjrfdNdO}t|dSdS)PNrz8unable to set locale, falling back to the default localeziotop )usageversionz-oz--only store_truerFz1only show processes or threads actually doing I/O)rdestrr@z-bz--batchrkznon-interactive mode)rrr@z-nz--iterrrNUMz-number of iterations before ending [infinite])typermetavarr@z-dz--delayrrz#delay between iterations [1 second]SECr)rrr@rrz-pz--pidpidsrCz"processes/threads to monitor [all]r)rrrr@rz-uz--userstruserszusers to monitor [all]USERz-Pz --processesrz$only show processes, not all threadsz-az --accumulatedr3z)show accumulated I/O instead of bandwidthz-kz --kilobytesr$z.use kilobytes instead of a human friendly unitz-tz--timer5z.add a timestamp on each line (implies --batch)z-qz--quietcountr;rz/suppress some lines of header (implies --batch)z --profileprofilez --no-help store_falser@Tzsuppress listing of shortcutszUnexpected arguments: rcstSr.)rjr!r0r!r"r1rbzmain..cs"zWdStyYdSwr.)rJr!) main_loopr!r"safe_main_loops   zmain..safe_main_loop)locale setlocaleLC_ALLErrorrKoptparse OptionParserUSAGEr add_option SUPPRESS_HELP parse_argsrrr<rrrkr5r;rr)parserrrr!)rr%r"mains~                  r)3 __future__rrnrrrrr~ryr^r|r5hasattrrrs RuntimeError collectionsr iotop.datarrrrr iotop.versionr iotopr iotop.ioprior rr#r'r)rDrMrrJrOrUrVrWrXobjectrYrdrjrargvrrr!r!r!r"s`         L