def test_memory_maps(self): src = textwrap.dedent(""" import time with open("%s", "w") as f: time.sleep(10) """ % TESTFN) sproc = pyrun(src) self.addCleanup(reap_children) call_until(lambda: os.listdir('.'), "'%s' not in ret" % TESTFN) p = psutil.Process(sproc.pid) time.sleep(.1) maps = p.memory_maps(grouped=False) pmap = sh('pmap -x %s' % p.pid).split('\n') # get rid of header del pmap[0] del pmap[0] while maps and pmap: this = maps.pop(0) other = pmap.pop(0) addr, _, rss, dirty, mode, path = other.split(None, 5) if not path.startswith('[') and not path.endswith(']'): self.assertEqual(path, os.path.basename(this.path)) self.assertEqual(int(rss) * 1024, this.rss) # test only rwx chars, ignore 's' and 'p' self.assertEqual(mode[:3], this.perms[:3])
def test_memory_addrspace_info(self): src = textwrap.dedent(""" import time with open("%s", "w") as f: time.sleep(10) """ % TESTFN) sproc = pyrun(src) self.addCleanup(reap_children) call_until(lambda: os.listdir('.'), "'%s' not in ret" % TESTFN) p = psutil.Process(sproc.pid) time.sleep(.1) mem = p.memory_addrspace_info() maps = p.memory_maps(grouped=False) self.assertEqual( mem.uss, sum([x.private_dirty + x.private_clean for x in maps])) self.assertEqual(mem.pss, sum([x.pss for x in maps])) self.assertEqual(mem.swap, sum([x.swap for x in maps]))
def test_proc_open_files_file_gone(self): # simulates a file which gets deleted during open_files() # execution p = psutil.Process() files = p.open_files() with tempfile.NamedTemporaryFile(): # give the kernel some time to see the new file call_until(p.open_files, "len(ret) != %i" % len(files)) with mock.patch('psutil._pslinux.os.readlink', side_effect=OSError(errno.ENOENT, "")) as m: files = p.open_files() assert not files assert m.called # also simulate the case where os.readlink() returns EINVAL # in which case psutil is supposed to 'continue' with mock.patch('psutil._pslinux.os.readlink', side_effect=OSError(errno.EINVAL, "")) as m: self.assertEqual(p.open_files(), []) assert m.called