"Fossies" - the Fresh Open Source Software Archive

Member "dstat-0.7.4/plugins/dstat_top_cpu_adv.py" (17 Jan 2019, 3177 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_cpu_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 cpu process'
    9         self.vars = ('process              pid  cpu read write',)
   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['cpu_usage'] = 0
   24         for pid in proc_pidlist():
   25             try:
   26                 ### Reset values
   27                 if pid not in self.pidset2:
   28                     self.pidset2[pid] = {'rchar:': 0, 'wchar:': 0, 'cputime:': 0, 'cpuper:': 0}
   29                 if pid not in self.pidset1:
   30                     self.pidset1[pid] = {'rchar:': 0, 'wchar:': 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.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]['rchar:'] - self.pidset1[pid]['rchar:']) * 1.0 / elapsed
   56             write_usage = (self.pidset2[pid]['wchar:'] - self.pidset1[pid]['wchar:']) * 1.0 / elapsed
   57 
   58             ### Get the process that spends the most jiffies
   59             if cpu_usage > self.val['cpu_usage']:
   60                 self.val['read_usage'] = read_usage
   61                 self.val['write_usage'] = write_usage
   62                 self.val['pid'] = pid
   63                 self.val['name'] = getnamebypid(pid, name)
   64                 self.val['cpu_usage'] = cpu_usage
   65 
   66         if step == op.delay:
   67             self.pidset1 = self.pidset2
   68 
   69         if self.val['cpu_usage'] != 0.0:
   70             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['cpu_usage'], 'f', 3, 34), color['darkgray'],cprint(self.val['read_usage'], 'd', 5, 1024), cprint(self.val['write_usage'], 'd', 5, 1024))
   71 
   72 
   73     def showcsv(self):
   74         return 'Top: %s\t%s\t%s\t%s' % (self.val['name'][0:self.width-20], self.val['cpu_usage'], self.val['read_usage'], self.val['write_usage'])