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_full_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_full_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_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_multi_sockets_procs(self): # Creates multiple sub processes, each creating different # sockets. For each process check that proc.connections() # and net_connections() return the same results. # This is done mainly to check whether net_connections()'s # pid is properly set, see: # https://github.com/giampaolo/psutil/issues/1013 with create_sockets() as socks: expected = len(socks) pids = [] times = 10 fnames = [] for i in range(times): fname = get_testfn() fnames.append(fname) src = textwrap.dedent("""\ import time, os from psutil.tests import create_sockets, cleanup_test_files with create_sockets(): with open(r'%s', 'w') as f: f.write("hello") # 2 UNIX test socket files are created cleanup_test_files() time.sleep(60) """ % fname) sproc = pyrun(src) pids.append(sproc.pid) # sync for fname in fnames: wait_for_file(fname) syscons = [ x for x in psutil.net_connections(kind='all') if x.pid in pids ] for pid in pids: self.assertEqual(len([x for x in syscons if x.pid == pid]), expected) p = psutil.Process(pid) self.assertEqual(len(p.connections('all')), expected)
def test_multi_sockets_procs(self): # Creates multiple sub processes, each creating different # sockets. For each process check that proc.connections() # and net_connections() return the same results. # This is done mainly to check whether net_connections()'s # pid is properly set, see: # https://github.com/giampaolo/psutil/issues/1013 with create_sockets() as socks: expected = len(socks) pids = [] times = 10 for i in range(times): fname = os.path.realpath(TESTFN) + str(i) src = textwrap.dedent("""\ import time, os from psutil.tests import create_sockets with create_sockets(): with open('%s', 'w') as f: f.write(str(os.getpid())) time.sleep(60) """ % fname) sproc = pyrun(src) pids.append(sproc.pid) self.addCleanup(safe_rmpath, fname) # sync for i in range(times): fname = TESTFN + str(i) wait_for_file(fname) syscons = [ x for x in psutil.net_connections(kind='all') if x.pid in pids ] for pid in pids: self.assertEqual(len([x for x in syscons if x.pid == pid]), expected) p = psutil.Process(pid) self.assertEqual(len(p.connections('all')), expected)
def test_multi_sockets_procs(self): # Creates multiple sub processes, each creating different # sockets. For each process check that proc.connections() # and net_connections() return the same results. # This is done mainly to check whether net_connections()'s # pid is properly set, see: # https://github.com/giampaolo/psutil/issues/1013 with create_sockets() as socks: expected = len(socks) pids = [] times = 10 for i in range(times): fname = os.path.realpath(TESTFN) + str(i) src = textwrap.dedent("""\ import time, os from psutil.tests import create_sockets with create_sockets(): with open(r'%s', 'w') as f: f.write(str(os.getpid())) time.sleep(60) """ % fname) sproc = pyrun(src) pids.append(sproc.pid) self.addCleanup(safe_rmpath, fname) # sync for i in range(times): fname = TESTFN + str(i) wait_for_file(fname) syscons = [x for x in psutil.net_connections(kind='all') if x.pid in pids] for pid in pids: self.assertEqual(len([x for x in syscons if x.pid == pid]), expected) p = psutil.Process(pid) self.assertEqual(len(p.connections('all')), expected)
def test_combos(self): def check_conn(proc, conn, family, type, laddr, raddr, status, kinds): all_kinds = ("all", "inet", "inet4", "inet6", "tcp", "tcp4", "tcp6", "udp", "udp4", "udp6") check_connection_ntuple(conn) self.assertEqual(conn.family, family) self.assertEqual(conn.type, type) self.assertEqual(conn.laddr, laddr) self.assertEqual(conn.raddr, raddr) self.assertEqual(conn.status, status) for kind in all_kinds: cons = proc.connections(kind=kind) if kind in kinds: assert cons else: assert not cons, cons # compare against system-wide connections # XXX Solaris can't retrieve system-wide UNIX # sockets. if not SUNOS: self.compare_procsys_connections(proc.pid, [conn]) tcp_template = textwrap.dedent(""" import socket, time s = socket.socket($family, socket.SOCK_STREAM) s.bind(('$addr', 0)) s.listen(1) with open('$testfn', 'w') as f: f.write(str(s.getsockname()[:2])) time.sleep(60) """) udp_template = textwrap.dedent(""" import socket, time s = socket.socket($family, socket.SOCK_DGRAM) s.bind(('$addr', 0)) with open('$testfn', 'w') as f: f.write(str(s.getsockname()[:2])) time.sleep(60) """) from string import Template testfile = os.path.basename(TESTFN) tcp4_template = Template(tcp_template).substitute(family=int(AF_INET), addr="127.0.0.1", testfn=testfile) udp4_template = Template(udp_template).substitute(family=int(AF_INET), addr="127.0.0.1", testfn=testfile) tcp6_template = Template(tcp_template).substitute(family=int(AF_INET6), addr="::1", testfn=testfile) udp6_template = Template(udp_template).substitute(family=int(AF_INET6), addr="::1", testfn=testfile) # launch various subprocess instantiating a socket of various # families and types to enrich psutil results tcp4_proc = pyrun(tcp4_template) tcp4_addr = eval(wait_for_file(testfile)) udp4_proc = pyrun(udp4_template) udp4_addr = eval(wait_for_file(testfile)) if supports_ipv6(): tcp6_proc = pyrun(tcp6_template) tcp6_addr = eval(wait_for_file(testfile)) udp6_proc = pyrun(udp6_template) udp6_addr = eval(wait_for_file(testfile)) else: tcp6_proc = None udp6_proc = None tcp6_addr = None udp6_addr = None for p in thisproc.children(): cons = p.connections() self.assertEqual(len(cons), 1) for conn in cons: # TCP v4 if p.pid == tcp4_proc.pid: check_conn(p, conn, AF_INET, SOCK_STREAM, tcp4_addr, (), psutil.CONN_LISTEN, ("all", "inet", "inet4", "tcp", "tcp4")) # UDP v4 elif p.pid == udp4_proc.pid: check_conn(p, conn, AF_INET, SOCK_DGRAM, udp4_addr, (), psutil.CONN_NONE, ("all", "inet", "inet4", "udp", "udp4")) # TCP v6 elif p.pid == getattr(tcp6_proc, "pid", None): check_conn(p, conn, AF_INET6, SOCK_STREAM, tcp6_addr, (), psutil.CONN_LISTEN, ("all", "inet", "inet6", "tcp", "tcp6")) # UDP v6 elif p.pid == getattr(udp6_proc, "pid", None): check_conn(p, conn, AF_INET6, SOCK_DGRAM, udp6_addr, (), psutil.CONN_NONE, ("all", "inet", "inet6", "udp", "udp6")) # err self.assertRaises(ValueError, p.connections, kind='???')
def test_combos(self): def check_conn(proc, conn, family, type, laddr, raddr, status, kinds): all_kinds = ("all", "inet", "inet4", "inet6", "tcp", "tcp4", "tcp6", "udp", "udp4", "udp6") check_connection_ntuple(conn) self.assertEqual(conn.family, family) self.assertEqual(conn.type, type) self.assertEqual(conn.laddr, laddr) self.assertEqual(conn.raddr, raddr) self.assertEqual(conn.status, status) for kind in all_kinds: cons = proc.connections(kind=kind) if kind in kinds: assert cons else: assert not cons, cons # compare against system-wide connections # XXX Solaris can't retrieve system-wide UNIX # sockets. if HAS_CONNECTIONS_UNIX: self.compare_procsys_connections(proc.pid, [conn]) tcp_template = textwrap.dedent(""" import socket, time s = socket.socket($family, socket.SOCK_STREAM) s.bind(('$addr', 0)) s.listen(1) with open('$testfn', 'w') as f: f.write(str(s.getsockname()[:2])) time.sleep(60) """) udp_template = textwrap.dedent(""" import socket, time s = socket.socket($family, socket.SOCK_DGRAM) s.bind(('$addr', 0)) with open('$testfn', 'w') as f: f.write(str(s.getsockname()[:2])) time.sleep(60) """) from string import Template testfile = os.path.basename(TESTFN) tcp4_template = Template(tcp_template).substitute( family=int(AF_INET), addr="127.0.0.1", testfn=testfile) udp4_template = Template(udp_template).substitute( family=int(AF_INET), addr="127.0.0.1", testfn=testfile) tcp6_template = Template(tcp_template).substitute( family=int(AF_INET6), addr="::1", testfn=testfile) udp6_template = Template(udp_template).substitute( family=int(AF_INET6), addr="::1", testfn=testfile) # launch various subprocess instantiating a socket of various # families and types to enrich psutil results tcp4_proc = pyrun(tcp4_template) tcp4_addr = eval(wait_for_file(testfile)) udp4_proc = pyrun(udp4_template) udp4_addr = eval(wait_for_file(testfile)) if supports_ipv6(): tcp6_proc = pyrun(tcp6_template) tcp6_addr = eval(wait_for_file(testfile)) udp6_proc = pyrun(udp6_template) udp6_addr = eval(wait_for_file(testfile)) else: tcp6_proc = None udp6_proc = None tcp6_addr = None udp6_addr = None for p in thisproc.children(): cons = p.connections() self.assertEqual(len(cons), 1) for conn in cons: # TCP v4 if p.pid == tcp4_proc.pid: check_conn(p, conn, AF_INET, SOCK_STREAM, tcp4_addr, (), psutil.CONN_LISTEN, ("all", "inet", "inet4", "tcp", "tcp4")) # UDP v4 elif p.pid == udp4_proc.pid: check_conn(p, conn, AF_INET, SOCK_DGRAM, udp4_addr, (), psutil.CONN_NONE, ("all", "inet", "inet4", "udp", "udp4")) # TCP v6 elif p.pid == getattr(tcp6_proc, "pid", None): check_conn(p, conn, AF_INET6, SOCK_STREAM, tcp6_addr, (), psutil.CONN_LISTEN, ("all", "inet", "inet6", "tcp", "tcp6")) # UDP v6 elif p.pid == getattr(udp6_proc, "pid", None): check_conn(p, conn, AF_INET6, SOCK_DGRAM, udp6_addr, (), psutil.CONN_NONE, ("all", "inet", "inet6", "udp", "udp6")) # err self.assertRaises(ValueError, p.connections, kind='???')
def test_pyrun(self): with open(TESTFN, 'w'): pass subp = pyrun("import os; os.remove('%s')" % TESTFN) subp.communicate() assert not os.path.exists(TESTFN)