def ps_fallback_usage(self): """Memory usage for the given PID using ps instead of proc.""" p = Popen(['ps', 'u', '-p', str(self.pid)], stdout=PIPE) output = p.stdout.read().split('\n') output = filter(None, output) process_line = output[-1].split() vsize_in_kb = process_line[5] + ' kB' return to_bytes(vsize_in_kb)
def proc_usage(self): """Memory usage for given PID.""" try: with open(self.procpath) as f: content = f.read() size = self.matcher.search(content).groups()[0] return to_bytes(size) except: return self.ps_fallback_usage()