"Fossies" - the Fresh Open Source Software Archive

Member "dstat-0.7.4/plugins/dstat_top_bio_adv.py" (17 Jan 2019, 3299 Bytes) of package /linux/privat/dstat-0.7.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "dstat_top_bio_adv.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 0.7.3_vs_0.7.4.

A hint: This file contains one or more very long lines, so maybe it is better readable using the pure text view mode that shows the contents as wrapped lines within the browser window.


    1 ### Dstat all I/O process plugin
    2 ### Displays all processes' I/O read/write stats and CPU usage
    3 ###
    4 ### Authority: Guillermo Cantu Luna
    5 
    6 class dstat_plugin(dstat):
    7     def __init__(self):
    8         self.name = 'most expensive block i/o process'
    9         self.vars = ('process              pid  read write cpu',)
   10         self.type = 's'
   11         self.width = 40
   12         self.scale = 0
   13         self.pidset1 = {}
   14 
   15     def check(self):
   16         if not os.access('/proc/self/io', os.R_OK):
   17             raise Exception('Kernel has no per-process I/O accounting [CONFIG_TASK_IO_ACCOUNTING], use at least 2.6.20')
   18         return True
   19 
   20     def extract(self):
   21         self.output = ''
   22         self.pidset2 = {}
   23         self.val['usage'] = 0.0
   24         for pid in proc_pidlist():
   25             try:
   26                 ### Reset values
   27                 if pid not in self.pidset2:
   28                     self.pidset2[pid] = {'read_bytes:': 0, 'write_bytes:': 0, 'cputime:': 0, 'cpuper:': 0}
   29                 if pid not in self.pidset1:
   30                     self.pidset1[pid] = {'read_bytes:': 0, 'write_bytes:': 0, 'cputime:': 0, 'cpuper:': 0}
   31 
   32                 ### Extract name
   33                 name = proc_splitline('/proc/%s/stat' % pid)[1][1:-1]
   34 
   35                 ### Extract counters
   36                 for l in proc_splitlines('/proc/%s/io' % pid):
   37                     if len(l) != 2: continue
   38                     self.pidset2[pid][l[0]] = int(l[1])
   39 
   40                 ### Get CPU usage
   41                 l = proc_splitline('/proc/%s/stat' % pid)
   42                 if len(l) < 15:
   43                     cpu_usage = 0
   44                 else:
   45                     self.pidset2[pid]['cputime:'] = int(l[13]) + int(l[14])
   46                     cpu_usage = (self.pidset2[pid]['cputime:'] - self.pidset1[pid]['cputime:']) * 1.0 / elapsed / cpunr
   47 
   48             except ValueError:
   49                 continue
   50             except IOError:
   51                 continue
   52             except IndexError:
   53                 continue
   54 
   55             read_usage = (self.pidset2[pid]['read_bytes:'] - self.pidset1[pid]['read_bytes:']) * 1.0 / elapsed
   56             write_usage = (self.pidset2[pid]['write_bytes:'] - self.pidset1[pid]['write_bytes:']) * 1.0 / elapsed
   57             usage = read_usage + write_usage
   58 
   59             ### Get the process that spends the most jiffies
   60             if usage > self.val['usage']:
   61                 self.val['usage'] = usage
   62                 self.val['read_usage'] = read_usage
   63                 self.val['write_usage'] = write_usage
   64                 self.val['pid'] = pid
   65                 self.val['name'] = getnamebypid(pid, name)
   66                 self.val['cpu_usage'] = cpu_usage
   67 
   68         if step == op.delay:
   69             self.pidset1 = self.pidset2
   70 
   71         if self.val['usage'] != 0.0:
   72             self.output = '%-*s%s%-5s%s%s%s%s%%' % (self.width-14-len(pid), self.val['name'][0:self.width-14-len(pid)], color['darkblue'], self.val['pid'], cprint(self.val['read_usage'], 'd', 5, 1024), cprint(self.val['write_usage'], 'd', 5, 1024), cprint(self.val['cpu_usage'], 'f', 3, 34), color['darkgray'])
   73 
   74     def showcsv(self):
   75         return 'Top: %s\t%s\t%s\t%s' % (self.val['name'][0:self.width-20], self.val['read_usage'], self.val['write_usage'], self.val['cpu_usage'])