3 bfb@szddlmZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl m Z mZmZmZmZddl mZddlmZddlmZddlmZdd d d d d dgZddZddZddZddZddZeZGddde Z!Gddde Z"Gddde Z#Gdd d e Z$Gd!d"d"e Z%Gd#d$d$e&Z'd%d&Z(d'd(Z)d)d*Z*d+e j+dZ,d,d-Z-dS).)print_functionN) find_uidsTaskStatsNetlink ProcessListStatssysctl_task_delayacct) ThreadInfo)VERSION)ioprio)IoprioSetErrorBKMGTPEcCs`|dkrd}n|dkr"d}| }ndSttj|ddd}d|t|dd|>t|fS) Nr-z0.00 B z %s%.2f %s)intmathlogfloatUNITS)sizeZsignZexpor/usr/lib/python3.6/ui.py human_size-sr cCs|jrd|dSt|S)Nz%.2f Kg@) kilobytesr )optionsbytesrrr format_size9s r$cCst||ot||dS)Nz/s)r$r)r"rdurationrrrformat_bandwidth>sr&c sfdd}jr4|j}fdd}tj|jn|j}fdd}||j}||j}||j}|j|j } t d| } || } |||| fS)Ncsdtd|dS)Nz%.2f %%g(\X@gcA)min)Zdelay)r%rr delay2percentDsz#format_stats..delay2percentcs t|S)N)r$)rr%)r"rrHszformat_stats..cs t||S)N)r&)rr%)r"rrr)Lsr) accumulated stats_accumtimeZstats_accum_timestamp stats_deltablkio_delay_totalswapin_delay_total read_bytes write_bytescancelled_write_bytesmax) r"Zprocessr%r(statsZdisplay_formatio_delay swapin_delayr0Z written_bytesr1r)r%r"r format_statsAs         r7cCsByttdjjStk r<}z t|dSd}~XnXdS)Nz/proc/sys/kernel/pid_max)lenopenreadstrip Exceptionprint)errrget_max_pid_widthVs r@c@s eZdZdS) CancelInputN)__name__ __module__ __qualname__rrrrrAdsrAc@s eZdZdS) InvalidIntN)rBrCrDrrrrrEesrEc@s eZdZdS) InvalidPidN)rBrCrDrrrrrFfsrFc@s eZdZdS) InvalidTidN)rBrCrDrrrrrGgsrGc@s eZdZdS)InvalidIoprioDataN)rBrCrDrrrrrHhsrHc @seZdZdddfdddfdddfdddfdddfd ddfd ddfd ddfgZd d ZddZddZddZddZd0ddZ d1ddZ ddZ ddZ dd Z d!d"Zd2d#d$Zd3d&d'Zd(d)Zd*d+Zd,d-Zd.d/ZdS)4IOTopUIcCs|jS)N)pid)psrrrr)qszIOTopUI.FcCs|jS)N)Zioprio_sort_key)rKrLrrrr)rscCs|jS)N)get_user)rKrLrrrr)sscCs|jS)N)r0)rKrLrrrr)tsTcCs |j|jS)N)r1r2)rKrLrrrr)uscCs|jS)N)r/)rKrLrrrr)vscCs|jpt|jp|j S)N)r.rr0r1)rKrLrrrr)yscCs|jS)N) get_cmdline)rKrLrrrr){sc Cst||_||_d|_tj|jd|_|jjsp||_|jyt j t j t j dWnt j k rnYnXdS)Nrr) process_listr" sorting_keyrI sorting_keyssorting_reversebatchwinresizecursesZuse_default_colorsZ start_colorcurs_seterror)selfrUrPr"rrr__init__~szIOTopUI.__init__cCs|jj\|_|_dS)N)rUZgetmaxyxheightwidth)rZrrrrVszIOTopUI.resizec Cs^d}tj}|jjs.|jtjjtjtj Bx(|jj dksL||jj krX|j j \}}|j |dk|||j j|jj dk r|d7}||jj krPn |dkrd}y|j|jjd}WnBtjk r}z$|jr|jdtjkrg}nWYdd}~XnXx.|D]&\}}|tjtjB@rtjdqW|jjs>|j|r2|jj} |j| q2WdS)Nrrg@@)selectpollr"rTregistersysstdinfilenoZPOLLINZPOLLPRI iterationsrPrefresh_processesrefresh_displayr% delay_secondsrYargserrnoZEINTRZPOLLERRZPOLLHUPexitrVrUgetch handle_key) rZrdr_totalactualZeventsr?fdZeventkeyrrrruns:     z IOTopUI.runcCs|j |_dS)N)rS)rZrrrreverse_sortingszIOTopUI.reverse_sortingcCs|j}|j|7_td|j|_tttjd|j|_|jsh|jdkrh|dkrZd|_n|dkrhd|_||jkrtj|jd|_dS)Nrrr8rO)r8rO)rQr3r'r9rIrR has_swapin_iorS)rZZdeltaZorig_sorting_keyrrradjust_sorting_keys  zIOTopUI.adjust_sorting_keyNcCs|jjddtdtjB|j|jjdd|tj|jjtj tj d|jj dt |}tj dtj |dkr~|S|rt|S)Nrr r)Nr)rUhlineordrWA_NORMALr]addstrA_BOLDrefreshZechorXZgetstrr9ZnoechorA)rZpromptdefaultempty_is_cancelinprrr prompt_strs   zIOTopUI.prompt_strc Cs6|j|||}yt|Stk r0tYnXdS)N)rr ValueErrorrE)rZr~rrrrrr prompt_ints zIOTopUI.prompt_intc Cs>y |jdStk r$tYntk r8YnXdS)NzPID to ionice: )rrErFrA)rZrrr prompt_pids   zIOTopUI.prompt_pidc Cs>y |jdStk r$tYntk r8YnXdS)NzTID to ionice: )rrErGrA)rZrrr prompt_tids   zIOTopUI.prompt_tidc Csdy.|dk r|jd||d}n|jddd}Wntk rHtYnX|dksZ|dkr`t|S)Nz'I/O priority data (0-7, currently %s): FzI/O priority data (0-7): rrt)rrErH)rZ ioprio_datarrrr prompt_datas zIOTopUI.prompt_datac Csvy|j|}Wntk r&d }YnXt|d}x:|jjddtdtjB|j|jj dd|tj t|}xPt |D]D\}}d|} ||krtj } ntj} |jj d|| | |t| 7}q|Wx|jj } | tjtdfkr|dkr|d8}Pq| tjtdfkr ||kr |d7}Pq| tjtdtdfkrB||S| d tjtjtjtd td fkrtqWq8WdS) Nrrrwz %s lr  qQ)indexrr9rUrxryrWrzr]insstrr| enumerate A_REVERSErkKEY_LEFT KEY_RIGHTZ KEY_ENTERZ KEY_CANCELZ KEY_CLOSEZKEY_EXITrA) rZr~Z display_listZret_listZselectedZset_lenoffsetiitemZdisplayattrrprrr prompt_sets:    zIOTopUI.prompt_setcCs8d}dddg}dddg}|dkr$d}|j||||}|S) NzI/O priority class: z Real-timez Best-effortZIdleZrtZbeidler)r)rZ ioprio_classr~Zclasses_promptZ classes_retrrrr prompt_classs  zIOTopUI.prompt_classError!cCsN|jjddtdtjB|j|jjddd|tj|jjt j ddS)Nrrrwz %s ) rUrxryrWrzr]rrr}r,Zsleep)rZrYrrr prompt_error"s zIOTopUI.prompt_errorcCs,|jjddtdtjB|j|jjdS)Nrrrw)rUrxryrWrzr]r})rZrrr prompt_clear(szIOTopUI.prompt_clearc"sfdd}fdd}fdd}fdd}td d d td d d tdfdd tdfdd td|td|td|td|td|td|td|td|tjfdd tjfdd tjfdd tjfdd i}|j|dd }|dS)NcsjjdN_dS)NT)r"r*r)rZrrtoggle_accumulated-sz.IOTopUI.handle_key..toggle_accumulatedcsjjdN_dS)NT)r"onlyr)rZrrtoggle_only_io/sz*IOTopUI.handle_key..toggle_only_iocs(jjdN_jjjjdS)NT)r" processesrPclearrer)rZrrtoggle_processes1s z,IOTopUI.handle_key..toggle_processescs`yjjr j}jj|}nj}t|jj}|j}t j |\}}j |}|dkrdd}n j |}|j ||jjjjWntk r}zjd|jWYdd}~Xntk rjdYn~tk rjdYnbtk rjdYnDtk r6jdYn&tk rRjYn XjdS)NrrzError setting I/O priority: %szInvalid process id!zInvalid thread id!zInvalid I/O priority data!zInvalid integer!)r"rrrPZ get_processrrtaskstats_connection get_ioprior Zto_class_and_datarrZ set_iopriorrer rerrrFrGrHrErAr)rJZ exec_unittidZ ioprio_valuerrr?)rZrrionice5s:      z"IOTopUI.handle_key..ionicercSs tjdS)Nr)rarjrrrrr)Zsz$IOTopUI.handle_key..rcSs tjdS)Nr)rarjrrrrr)\srcsjS)N)rrr)rZrrr)^sRcsjS)N)rrr)rZrrr)`saAoOrKrrIcs jdS)Nrr)rvr)rZrrr)rscs jdS)Nr)rvr)rZrrr)tscsjttj S)N)rvr9rIrRr)rZrrr)vscsjttjS)N)rvr9rIrRr)rZrrr)xscSsdS)Nrrrrrr){s)ryrWrrZKEY_HOMEZKEY_ENDget)rZrprrrrZ key_bindingsactionr)rZrrl,sL    $     zIOTopUI.handle_keycsfdd}fdd}tt|jjj}tjjdjj rPddndd|j fd dj d jj s|j d d=tt||S) Nc sRtj|jj}|\}}}}dt}|jf}|d7}||jf7}|d7}||jddf7}|d7}|||f7}jr|d7}|||f7}njj r|d7}|d 7}|d 7}||}|j } jj s.j t |} d | kot | knr.| d d } | | d  } | d| d | | d} || 7}jj sN|dj }|S)Nz%%%ddz %4sz %-8sz %11s %11sz %7s %7sz %s ?unavailable?rwrr~)r) r7r"rPr% MAX_PID_WIDTHrJrrMrurTrNr]r9) rKr4r5r6r0r1formatZparamslineZcmdlineZremaining_lengthZlen1Zoffset2)rZrrrs:     z IOTopUI.get_data..formatcsjj p|jjjS)N)r"rZ did_some_ior*)rK)rZrr should_formats z'IOTopUI.get_data..should_formatrcSs|jS)N)r+)rKrrrr)sz"IOTopUI.get_data..cSs|jS)N)r-)rKrrrr)scs||S)Nr)rK)rp stats_lambdarrr)s)rpreverser)listfilterrPrvaluesrIrRrQr"r*sortrSrTr\map)rZrrrr)rprZrrget_data~s    zIOTopUI.get_datacsdtj|d|jdtj|d|jdfdtj|d|jdtj|d|jdfgtdtdd}jjr|d7}n|d 7}|d d d d dddg}tj_jrt dkrd_j dj }jj rdg|}t j dfdd|D}fddDjjrjjdkrjxD]}t|q8Wjjt|krjtdj|x|D]} t| jdqpWtjjnjjx0tD]$\}jjd|djqWjjtdtdtj Bjj} xt!t|D]j r$d!kr$qtj } |} j"krJ| dd} j"krv| tj#O} | j$rpdprd7} | d| } | t| 8} jj| | qWjdk rd} nd} t%t|j&dtt'| }xzt!|D]nyNfdd }y||Wn(t(k r0||jdYnXWntj)k rLYnXqW| rxjj*j&td| tj#jj+dS)"Nz,Total DISK READ : %s | Total DISK WRITE : %srrz,Actual DISK READ: %s | Actual DISK WRITE: %srwPIDZTIDz PRIOz USERz DISK READz DISK WRITEz SWAPINz IOz COMMANDFz TIMEz %H:%M:%S csg|] }|qSrr).0r) current_timerr sz+IOTopUI.refresh_display..csg|] }|qSrr)rrL)rrrrsrrzutf-8r8rO>.print_line)r8rO),r&r"rjustr3rrrZhas_blkio_delay_totalrurrvrr,ZstrftimerTquietr>rjoinencoderastdoutflushrUZeraserr{r]rxr9ryrWrrangerQr|rSr'r\boolUnicodeEncodeErrorrYrr})rZZ first_timermrnr%rJZtitleslinesrLrZremaining_colsrtitleZ status_msgZ num_linesrr)rrrZrrrfs                      zIOTopUI.refresh_display)NT)NT)N)r)rBrCrDrRr[rVrqrrrvrrrrrrrrrrlrrfrrrrrIns4      !    R1rIcCsf|jrtjtjtjn$dd}tjtj|tjtj|t|}t||}t|||}|j dS)Nc_stjddS)Nr)rarj)rhkwargsrrr clean_exit sz$run_iotop_window..clean_exit) rTsignalSIGPIPESIG_DFLSIGINTSIGTERMrrrIrq)rUr"rrrPZuirrrrun_iotop_windows  rcCsy |jrtd|Stjt|SWntk rx}z<|jtjkrft|tj dtdtj dtj dnWYdd}~XnZtj k r}zrastderrrjrYstrfind)r"r?Zstrerrr run_iotops"    rc Csd}y:ddl}ddl}td|jdtt||j|}WnXtk rddl}ddl }|j |dd}td|j ||j |j j|}YnX|j|jdd |jd |jd tj|dS) Nz iotop.profrzProfiling using cProfilezcontinuation()r)Z lineeventszProfiling using hotshotr,Zcalls2)cProfilepstatsr>Zrunctxglobalslocalsr ImportErrorhotshotZ hotshot.statsZProfileZruncallcloser4loadZ strip_dirsZ sort_statsZ print_statsZ print_calleesosremove)Z continuationZ prof_filerrr4rZprofrrr_profile6s(    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.csytjtjdWntjk r0tdYnXtjtdtd}|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 \}|rt|j dEdFj |tjpg_jpjpj_fdGdHjṙfdIdJ}t|ndS)KNrz8unable to set locale, falling back to the default localeziotop )Zusageversionz-oz--only store_truerFz1only show processes or threads actually doing I/O)rdestrhelpz-bz--batchrTznon-interactive mode)rrrz-nz--iterrrdZNUMz-number of iterations before ending [infinite])typermetavarrz-dz--delayrrgz#delay between iterations [1 second]ZSECr)rrrrrz-pz--pidpidsappendz"processes/threads to monitor [all]r)rrrrrz-uz--userrZuserszusers to monitor [all]ZUSERz-Pz --processesrz$only show processes, not all threadsz-az --accumulatedr*z)show accumulated I/O instead of bandwidthz-kz --kilobytesr!z.use kilobytes instead of a human friendly unitz-tz--timer,z.add a timestamp on each line (implies --batch)z-qz--quietcountrrz/suppress some lines of header (implies --batch)z --profileprofilezUnexpected arguments: rwcstS)N)rr)r"rrr)szmain..c sy Wn YnXdS)Nrr) main_looprrsafe_main_loops zmain..safe_main_loop)locale setlocaleLC_ALLErrorr>optparseZ OptionParserUSAGEr Z add_optionZ SUPPRESS_HELP parse_argsrYrrrrTr,rrr)parserrhrr)rr"rmain\s\                 r).Z __future__rrWrirrrrr^rrar,Z iotop.datarrrrrrZ iotop.versionr Ziotopr Z iotop.ioprior rr r$r&r7r@rr=rArErFrGrHobjectrIrrrargvrrrrrrsH       $$