test_process.py 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """Tests for psutil.Process class."""
  6. import collections
  7. import errno
  8. import getpass
  9. import itertools
  10. import os
  11. import signal
  12. import socket
  13. import stat
  14. import subprocess
  15. import sys
  16. import textwrap
  17. import time
  18. import types
  19. import psutil
  20. from psutil import AIX
  21. from psutil import BSD
  22. from psutil import LINUX
  23. from psutil import MACOS
  24. from psutil import NETBSD
  25. from psutil import OPENBSD
  26. from psutil import OSX
  27. from psutil import POSIX
  28. from psutil import SUNOS
  29. from psutil import WINDOWS
  30. from psutil._common import open_text
  31. from psutil._compat import FileNotFoundError
  32. from psutil._compat import long
  33. from psutil._compat import PY3
  34. from psutil._compat import super
  35. from psutil.tests import APPVEYOR
  36. from psutil.tests import call_until
  37. from psutil.tests import CI_TESTING
  38. from psutil.tests import CIRRUS
  39. from psutil.tests import copyload_shared_lib
  40. from psutil.tests import create_exe
  41. from psutil.tests import GITHUB_WHEELS
  42. from psutil.tests import GLOBAL_TIMEOUT
  43. from psutil.tests import HAS_CPU_AFFINITY
  44. from psutil.tests import HAS_ENVIRON
  45. from psutil.tests import HAS_IONICE
  46. from psutil.tests import HAS_MEMORY_MAPS
  47. from psutil.tests import HAS_PROC_CPU_NUM
  48. from psutil.tests import HAS_PROC_IO_COUNTERS
  49. from psutil.tests import HAS_RLIMIT
  50. from psutil.tests import HAS_THREADS
  51. from psutil.tests import mock
  52. from psutil.tests import process_namespace
  53. from psutil.tests import PsutilTestCase
  54. from psutil.tests import PYPY
  55. from psutil.tests import PYTHON_EXE
  56. from psutil.tests import reap_children
  57. from psutil.tests import retry_on_failure
  58. from psutil.tests import sh
  59. from psutil.tests import skip_on_access_denied
  60. from psutil.tests import skip_on_not_implemented
  61. from psutil.tests import ThreadTask
  62. from psutil.tests import TRAVIS
  63. from psutil.tests import unittest
  64. from psutil.tests import wait_for_pid
  65. # ===================================================================
  66. # --- psutil.Process class tests
  67. # ===================================================================
  68. class TestProcess(PsutilTestCase):
  69. """Tests for psutil.Process class."""
  70. def spawn_psproc(self, *args, **kwargs):
  71. sproc = self.spawn_testproc(*args, **kwargs)
  72. return psutil.Process(sproc.pid)
  73. # ---
  74. def test_pid(self):
  75. p = psutil.Process()
  76. self.assertEqual(p.pid, os.getpid())
  77. with self.assertRaises(AttributeError):
  78. p.pid = 33
  79. def test_kill(self):
  80. p = self.spawn_psproc()
  81. p.kill()
  82. code = p.wait()
  83. if WINDOWS:
  84. self.assertEqual(code, signal.SIGTERM)
  85. else:
  86. self.assertEqual(code, -signal.SIGKILL)
  87. self.assertProcessGone(p)
  88. def test_terminate(self):
  89. p = self.spawn_psproc()
  90. p.terminate()
  91. code = p.wait()
  92. if WINDOWS:
  93. self.assertEqual(code, signal.SIGTERM)
  94. else:
  95. self.assertEqual(code, -signal.SIGTERM)
  96. self.assertProcessGone(p)
  97. def test_send_signal(self):
  98. sig = signal.SIGKILL if POSIX else signal.SIGTERM
  99. p = self.spawn_psproc()
  100. p.send_signal(sig)
  101. code = p.wait()
  102. if WINDOWS:
  103. self.assertEqual(code, sig)
  104. else:
  105. self.assertEqual(code, -sig)
  106. self.assertProcessGone(p)
  107. @unittest.skipIf(not POSIX, "not POSIX")
  108. def test_send_signal_mocked(self):
  109. sig = signal.SIGTERM
  110. p = self.spawn_psproc()
  111. with mock.patch('psutil.os.kill',
  112. side_effect=OSError(errno.ESRCH, "")):
  113. self.assertRaises(psutil.NoSuchProcess, p.send_signal, sig)
  114. p = self.spawn_psproc()
  115. with mock.patch('psutil.os.kill',
  116. side_effect=OSError(errno.EPERM, "")):
  117. self.assertRaises(psutil.AccessDenied, p.send_signal, sig)
  118. def test_wait_exited(self):
  119. # Test waitpid() + WIFEXITED -> WEXITSTATUS.
  120. # normal return, same as exit(0)
  121. cmd = [PYTHON_EXE, "-c", "pass"]
  122. p = self.spawn_psproc(cmd)
  123. code = p.wait()
  124. self.assertEqual(code, 0)
  125. self.assertProcessGone(p)
  126. # exit(1), implicit in case of error
  127. cmd = [PYTHON_EXE, "-c", "1 / 0"]
  128. p = self.spawn_psproc(cmd, stderr=subprocess.PIPE)
  129. code = p.wait()
  130. self.assertEqual(code, 1)
  131. self.assertProcessGone(p)
  132. # via sys.exit()
  133. cmd = [PYTHON_EXE, "-c", "import sys; sys.exit(5);"]
  134. p = self.spawn_psproc(cmd)
  135. code = p.wait()
  136. self.assertEqual(code, 5)
  137. self.assertProcessGone(p)
  138. # via os._exit()
  139. cmd = [PYTHON_EXE, "-c", "import os; os._exit(5);"]
  140. p = self.spawn_psproc(cmd)
  141. code = p.wait()
  142. self.assertEqual(code, 5)
  143. self.assertProcessGone(p)
  144. def test_wait_stopped(self):
  145. p = self.spawn_psproc()
  146. if POSIX:
  147. # Test waitpid() + WIFSTOPPED and WIFCONTINUED.
  148. # Note: if a process is stopped it ignores SIGTERM.
  149. p.send_signal(signal.SIGSTOP)
  150. self.assertRaises(psutil.TimeoutExpired, p.wait, timeout=0.001)
  151. p.send_signal(signal.SIGCONT)
  152. self.assertRaises(psutil.TimeoutExpired, p.wait, timeout=0.001)
  153. p.send_signal(signal.SIGTERM)
  154. self.assertEqual(p.wait(), -signal.SIGTERM)
  155. self.assertEqual(p.wait(), -signal.SIGTERM)
  156. else:
  157. p.suspend()
  158. self.assertRaises(psutil.TimeoutExpired, p.wait, timeout=0.001)
  159. p.resume()
  160. self.assertRaises(psutil.TimeoutExpired, p.wait, timeout=0.001)
  161. p.terminate()
  162. self.assertEqual(p.wait(), signal.SIGTERM)
  163. self.assertEqual(p.wait(), signal.SIGTERM)
  164. def test_wait_non_children(self):
  165. # Test wait() against a process which is not our direct
  166. # child.
  167. child, grandchild = self.spawn_children_pair()
  168. self.assertRaises(psutil.TimeoutExpired, child.wait, 0.01)
  169. self.assertRaises(psutil.TimeoutExpired, grandchild.wait, 0.01)
  170. # We also terminate the direct child otherwise the
  171. # grandchild will hang until the parent is gone.
  172. child.terminate()
  173. grandchild.terminate()
  174. child_ret = child.wait()
  175. grandchild_ret = grandchild.wait()
  176. if POSIX:
  177. self.assertEqual(child_ret, -signal.SIGTERM)
  178. # For processes which are not our children we're supposed
  179. # to get None.
  180. self.assertEqual(grandchild_ret, None)
  181. else:
  182. self.assertEqual(child_ret, signal.SIGTERM)
  183. self.assertEqual(child_ret, signal.SIGTERM)
  184. def test_wait_timeout(self):
  185. p = self.spawn_psproc()
  186. p.name()
  187. self.assertRaises(psutil.TimeoutExpired, p.wait, 0.01)
  188. self.assertRaises(psutil.TimeoutExpired, p.wait, 0)
  189. self.assertRaises(ValueError, p.wait, -1)
  190. def test_wait_timeout_nonblocking(self):
  191. p = self.spawn_psproc()
  192. self.assertRaises(psutil.TimeoutExpired, p.wait, 0)
  193. p.kill()
  194. stop_at = time.time() + GLOBAL_TIMEOUT
  195. while time.time() < stop_at:
  196. try:
  197. code = p.wait(0)
  198. break
  199. except psutil.TimeoutExpired:
  200. pass
  201. else:
  202. raise self.fail('timeout')
  203. if POSIX:
  204. self.assertEqual(code, -signal.SIGKILL)
  205. else:
  206. self.assertEqual(code, signal.SIGTERM)
  207. self.assertProcessGone(p)
  208. def test_cpu_percent(self):
  209. p = psutil.Process()
  210. p.cpu_percent(interval=0.001)
  211. p.cpu_percent(interval=0.001)
  212. for x in range(100):
  213. percent = p.cpu_percent(interval=None)
  214. self.assertIsInstance(percent, float)
  215. self.assertGreaterEqual(percent, 0.0)
  216. with self.assertRaises(ValueError):
  217. p.cpu_percent(interval=-1)
  218. def test_cpu_percent_numcpus_none(self):
  219. # See: https://github.com/giampaolo/psutil/issues/1087
  220. with mock.patch('psutil.cpu_count', return_value=None) as m:
  221. psutil.Process().cpu_percent()
  222. assert m.called
  223. def test_cpu_times(self):
  224. times = psutil.Process().cpu_times()
  225. assert (times.user > 0.0) or (times.system > 0.0), times
  226. assert (times.children_user >= 0.0), times
  227. assert (times.children_system >= 0.0), times
  228. if LINUX:
  229. assert times.iowait >= 0.0, times
  230. # make sure returned values can be pretty printed with strftime
  231. for name in times._fields:
  232. time.strftime("%H:%M:%S", time.localtime(getattr(times, name)))
  233. def test_cpu_times_2(self):
  234. user_time, kernel_time = psutil.Process().cpu_times()[:2]
  235. utime, ktime = os.times()[:2]
  236. # Use os.times()[:2] as base values to compare our results
  237. # using a tolerance of +/- 0.1 seconds.
  238. # It will fail if the difference between the values is > 0.1s.
  239. if (max([user_time, utime]) - min([user_time, utime])) > 0.1:
  240. self.fail("expected: %s, found: %s" % (utime, user_time))
  241. if (max([kernel_time, ktime]) - min([kernel_time, ktime])) > 0.1:
  242. self.fail("expected: %s, found: %s" % (ktime, kernel_time))
  243. @unittest.skipIf(not HAS_PROC_CPU_NUM, "not supported")
  244. def test_cpu_num(self):
  245. p = psutil.Process()
  246. num = p.cpu_num()
  247. self.assertGreaterEqual(num, 0)
  248. if psutil.cpu_count() == 1:
  249. self.assertEqual(num, 0)
  250. self.assertIn(p.cpu_num(), range(psutil.cpu_count()))
  251. def test_create_time(self):
  252. p = self.spawn_psproc()
  253. now = time.time()
  254. create_time = p.create_time()
  255. # Use time.time() as base value to compare our result using a
  256. # tolerance of +/- 1 second.
  257. # It will fail if the difference between the values is > 2s.
  258. difference = abs(create_time - now)
  259. if difference > 2:
  260. self.fail("expected: %s, found: %s, difference: %s"
  261. % (now, create_time, difference))
  262. # make sure returned value can be pretty printed with strftime
  263. time.strftime("%Y %m %d %H:%M:%S", time.localtime(p.create_time()))
  264. @unittest.skipIf(not POSIX, 'POSIX only')
  265. @unittest.skipIf(TRAVIS or CIRRUS, 'not reliable on TRAVIS/CIRRUS')
  266. def test_terminal(self):
  267. terminal = psutil.Process().terminal()
  268. if terminal is not None:
  269. tty = os.path.realpath(sh('tty'))
  270. self.assertEqual(terminal, tty)
  271. @unittest.skipIf(not HAS_PROC_IO_COUNTERS, 'not supported')
  272. @skip_on_not_implemented(only_if=LINUX)
  273. def test_io_counters(self):
  274. p = psutil.Process()
  275. # test reads
  276. io1 = p.io_counters()
  277. with open(PYTHON_EXE, 'rb') as f:
  278. f.read()
  279. io2 = p.io_counters()
  280. if not BSD and not AIX:
  281. self.assertGreater(io2.read_count, io1.read_count)
  282. self.assertEqual(io2.write_count, io1.write_count)
  283. if LINUX:
  284. self.assertGreater(io2.read_chars, io1.read_chars)
  285. self.assertEqual(io2.write_chars, io1.write_chars)
  286. else:
  287. self.assertGreaterEqual(io2.read_bytes, io1.read_bytes)
  288. self.assertGreaterEqual(io2.write_bytes, io1.write_bytes)
  289. # test writes
  290. io1 = p.io_counters()
  291. with open(self.get_testfn(), 'wb') as f:
  292. if PY3:
  293. f.write(bytes("x" * 1000000, 'ascii'))
  294. else:
  295. f.write("x" * 1000000)
  296. io2 = p.io_counters()
  297. self.assertGreaterEqual(io2.write_count, io1.write_count)
  298. self.assertGreaterEqual(io2.write_bytes, io1.write_bytes)
  299. self.assertGreaterEqual(io2.read_count, io1.read_count)
  300. self.assertGreaterEqual(io2.read_bytes, io1.read_bytes)
  301. if LINUX:
  302. self.assertGreater(io2.write_chars, io1.write_chars)
  303. self.assertGreaterEqual(io2.read_chars, io1.read_chars)
  304. # sanity check
  305. for i in range(len(io2)):
  306. if BSD and i >= 2:
  307. # On BSD read_bytes and write_bytes are always set to -1.
  308. continue
  309. self.assertGreaterEqual(io2[i], 0)
  310. self.assertGreaterEqual(io2[i], 0)
  311. @unittest.skipIf(not HAS_IONICE, "not supported")
  312. @unittest.skipIf(not LINUX, "linux only")
  313. def test_ionice_linux(self):
  314. p = psutil.Process()
  315. if not CI_TESTING:
  316. self.assertEqual(p.ionice()[0], psutil.IOPRIO_CLASS_NONE)
  317. self.assertEqual(psutil.IOPRIO_CLASS_NONE, 0)
  318. self.assertEqual(psutil.IOPRIO_CLASS_RT, 1) # high
  319. self.assertEqual(psutil.IOPRIO_CLASS_BE, 2) # normal
  320. self.assertEqual(psutil.IOPRIO_CLASS_IDLE, 3) # low
  321. init = p.ionice()
  322. try:
  323. # low
  324. p.ionice(psutil.IOPRIO_CLASS_IDLE)
  325. self.assertEqual(tuple(p.ionice()), (psutil.IOPRIO_CLASS_IDLE, 0))
  326. with self.assertRaises(ValueError): # accepts no value
  327. p.ionice(psutil.IOPRIO_CLASS_IDLE, value=7)
  328. # normal
  329. p.ionice(psutil.IOPRIO_CLASS_BE)
  330. self.assertEqual(tuple(p.ionice()), (psutil.IOPRIO_CLASS_BE, 0))
  331. p.ionice(psutil.IOPRIO_CLASS_BE, value=7)
  332. self.assertEqual(tuple(p.ionice()), (psutil.IOPRIO_CLASS_BE, 7))
  333. with self.assertRaises(ValueError):
  334. p.ionice(psutil.IOPRIO_CLASS_BE, value=8)
  335. try:
  336. p.ionice(psutil.IOPRIO_CLASS_RT, value=7)
  337. except psutil.AccessDenied:
  338. pass
  339. # errs
  340. self.assertRaisesRegex(
  341. ValueError, "ioclass accepts no value",
  342. p.ionice, psutil.IOPRIO_CLASS_NONE, 1)
  343. self.assertRaisesRegex(
  344. ValueError, "ioclass accepts no value",
  345. p.ionice, psutil.IOPRIO_CLASS_IDLE, 1)
  346. self.assertRaisesRegex(
  347. ValueError, "'ioclass' argument must be specified",
  348. p.ionice, value=1)
  349. finally:
  350. ioclass, value = init
  351. if ioclass == psutil.IOPRIO_CLASS_NONE:
  352. value = 0
  353. p.ionice(ioclass, value)
  354. @unittest.skipIf(not HAS_IONICE, "not supported")
  355. @unittest.skipIf(not WINDOWS, 'not supported on this win version')
  356. def test_ionice_win(self):
  357. p = psutil.Process()
  358. if not CI_TESTING:
  359. self.assertEqual(p.ionice(), psutil.IOPRIO_NORMAL)
  360. init = p.ionice()
  361. try:
  362. # base
  363. p.ionice(psutil.IOPRIO_VERYLOW)
  364. self.assertEqual(p.ionice(), psutil.IOPRIO_VERYLOW)
  365. p.ionice(psutil.IOPRIO_LOW)
  366. self.assertEqual(p.ionice(), psutil.IOPRIO_LOW)
  367. try:
  368. p.ionice(psutil.IOPRIO_HIGH)
  369. except psutil.AccessDenied:
  370. pass
  371. else:
  372. self.assertEqual(p.ionice(), psutil.IOPRIO_HIGH)
  373. # errs
  374. self.assertRaisesRegex(
  375. TypeError, "value argument not accepted on Windows",
  376. p.ionice, psutil.IOPRIO_NORMAL, value=1)
  377. self.assertRaisesRegex(
  378. ValueError, "is not a valid priority",
  379. p.ionice, psutil.IOPRIO_HIGH + 1)
  380. finally:
  381. p.ionice(init)
  382. @unittest.skipIf(not HAS_RLIMIT, "not supported")
  383. def test_rlimit_get(self):
  384. import resource
  385. p = psutil.Process(os.getpid())
  386. names = [x for x in dir(psutil) if x.startswith('RLIMIT')]
  387. assert names, names
  388. for name in names:
  389. value = getattr(psutil, name)
  390. self.assertGreaterEqual(value, 0)
  391. if name in dir(resource):
  392. self.assertEqual(value, getattr(resource, name))
  393. # XXX - On PyPy RLIMIT_INFINITY returned by
  394. # resource.getrlimit() is reported as a very big long
  395. # number instead of -1. It looks like a bug with PyPy.
  396. if PYPY:
  397. continue
  398. self.assertEqual(p.rlimit(value), resource.getrlimit(value))
  399. else:
  400. ret = p.rlimit(value)
  401. self.assertEqual(len(ret), 2)
  402. self.assertGreaterEqual(ret[0], -1)
  403. self.assertGreaterEqual(ret[1], -1)
  404. @unittest.skipIf(not HAS_RLIMIT, "not supported")
  405. def test_rlimit_set(self):
  406. p = self.spawn_psproc()
  407. p.rlimit(psutil.RLIMIT_NOFILE, (5, 5))
  408. self.assertEqual(p.rlimit(psutil.RLIMIT_NOFILE), (5, 5))
  409. # If pid is 0 prlimit() applies to the calling process and
  410. # we don't want that.
  411. if LINUX:
  412. with self.assertRaisesRegex(ValueError, "can't use prlimit"):
  413. psutil._psplatform.Process(0).rlimit(0)
  414. with self.assertRaises(ValueError):
  415. p.rlimit(psutil.RLIMIT_NOFILE, (5, 5, 5))
  416. @unittest.skipIf(not HAS_RLIMIT, "not supported")
  417. def test_rlimit(self):
  418. p = psutil.Process()
  419. testfn = self.get_testfn()
  420. soft, hard = p.rlimit(psutil.RLIMIT_FSIZE)
  421. try:
  422. p.rlimit(psutil.RLIMIT_FSIZE, (1024, hard))
  423. with open(testfn, "wb") as f:
  424. f.write(b"X" * 1024)
  425. # write() or flush() doesn't always cause the exception
  426. # but close() will.
  427. with self.assertRaises(IOError) as exc:
  428. with open(testfn, "wb") as f:
  429. f.write(b"X" * 1025)
  430. self.assertEqual(exc.exception.errno if PY3 else exc.exception[0],
  431. errno.EFBIG)
  432. finally:
  433. p.rlimit(psutil.RLIMIT_FSIZE, (soft, hard))
  434. self.assertEqual(p.rlimit(psutil.RLIMIT_FSIZE), (soft, hard))
  435. @unittest.skipIf(not HAS_RLIMIT, "not supported")
  436. def test_rlimit_infinity(self):
  437. # First set a limit, then re-set it by specifying INFINITY
  438. # and assume we overridden the previous limit.
  439. p = psutil.Process()
  440. soft, hard = p.rlimit(psutil.RLIMIT_FSIZE)
  441. try:
  442. p.rlimit(psutil.RLIMIT_FSIZE, (1024, hard))
  443. p.rlimit(psutil.RLIMIT_FSIZE, (psutil.RLIM_INFINITY, hard))
  444. with open(self.get_testfn(), "wb") as f:
  445. f.write(b"X" * 2048)
  446. finally:
  447. p.rlimit(psutil.RLIMIT_FSIZE, (soft, hard))
  448. self.assertEqual(p.rlimit(psutil.RLIMIT_FSIZE), (soft, hard))
  449. @unittest.skipIf(not HAS_RLIMIT, "not supported")
  450. def test_rlimit_infinity_value(self):
  451. # RLIMIT_FSIZE should be RLIM_INFINITY, which will be a really
  452. # big number on a platform with large file support. On these
  453. # platforms we need to test that the get/setrlimit functions
  454. # properly convert the number to a C long long and that the
  455. # conversion doesn't raise an error.
  456. p = psutil.Process()
  457. soft, hard = p.rlimit(psutil.RLIMIT_FSIZE)
  458. self.assertEqual(psutil.RLIM_INFINITY, hard)
  459. p.rlimit(psutil.RLIMIT_FSIZE, (soft, hard))
  460. def test_num_threads(self):
  461. # on certain platforms such as Linux we might test for exact
  462. # thread number, since we always have with 1 thread per process,
  463. # but this does not apply across all platforms (MACOS, Windows)
  464. p = psutil.Process()
  465. if OPENBSD:
  466. try:
  467. step1 = p.num_threads()
  468. except psutil.AccessDenied:
  469. raise unittest.SkipTest("on OpenBSD this requires root access")
  470. else:
  471. step1 = p.num_threads()
  472. with ThreadTask():
  473. step2 = p.num_threads()
  474. self.assertEqual(step2, step1 + 1)
  475. @unittest.skipIf(not WINDOWS, 'WINDOWS only')
  476. def test_num_handles(self):
  477. # a better test is done later into test/_windows.py
  478. p = psutil.Process()
  479. self.assertGreater(p.num_handles(), 0)
  480. @unittest.skipIf(not HAS_THREADS, 'not supported')
  481. def test_threads(self):
  482. p = psutil.Process()
  483. if OPENBSD:
  484. try:
  485. step1 = p.threads()
  486. except psutil.AccessDenied:
  487. raise unittest.SkipTest("on OpenBSD this requires root access")
  488. else:
  489. step1 = p.threads()
  490. with ThreadTask():
  491. step2 = p.threads()
  492. self.assertEqual(len(step2), len(step1) + 1)
  493. athread = step2[0]
  494. # test named tuple
  495. self.assertEqual(athread.id, athread[0])
  496. self.assertEqual(athread.user_time, athread[1])
  497. self.assertEqual(athread.system_time, athread[2])
  498. @retry_on_failure()
  499. @skip_on_access_denied(only_if=MACOS)
  500. @unittest.skipIf(not HAS_THREADS, 'not supported')
  501. def test_threads_2(self):
  502. p = self.spawn_psproc()
  503. if OPENBSD:
  504. try:
  505. p.threads()
  506. except psutil.AccessDenied:
  507. raise unittest.SkipTest(
  508. "on OpenBSD this requires root access")
  509. self.assertAlmostEqual(
  510. p.cpu_times().user,
  511. sum([x.user_time for x in p.threads()]), delta=0.1)
  512. self.assertAlmostEqual(
  513. p.cpu_times().system,
  514. sum([x.system_time for x in p.threads()]), delta=0.1)
  515. @retry_on_failure()
  516. def test_memory_info(self):
  517. p = psutil.Process()
  518. # step 1 - get a base value to compare our results
  519. rss1, vms1 = p.memory_info()[:2]
  520. percent1 = p.memory_percent()
  521. self.assertGreater(rss1, 0)
  522. self.assertGreater(vms1, 0)
  523. # step 2 - allocate some memory
  524. memarr = [None] * 1500000
  525. rss2, vms2 = p.memory_info()[:2]
  526. percent2 = p.memory_percent()
  527. # step 3 - make sure that the memory usage bumped up
  528. self.assertGreater(rss2, rss1)
  529. self.assertGreaterEqual(vms2, vms1) # vms might be equal
  530. self.assertGreater(percent2, percent1)
  531. del memarr
  532. if WINDOWS:
  533. mem = p.memory_info()
  534. self.assertEqual(mem.rss, mem.wset)
  535. self.assertEqual(mem.vms, mem.pagefile)
  536. mem = p.memory_info()
  537. for name in mem._fields:
  538. self.assertGreaterEqual(getattr(mem, name), 0)
  539. def test_memory_full_info(self):
  540. p = psutil.Process()
  541. total = psutil.virtual_memory().total
  542. mem = p.memory_full_info()
  543. for name in mem._fields:
  544. value = getattr(mem, name)
  545. self.assertGreaterEqual(value, 0, msg=(name, value))
  546. if name == 'vms' and OSX or LINUX:
  547. continue
  548. self.assertLessEqual(value, total, msg=(name, value, total))
  549. if LINUX or WINDOWS or MACOS:
  550. self.assertGreaterEqual(mem.uss, 0)
  551. if LINUX:
  552. self.assertGreaterEqual(mem.pss, 0)
  553. self.assertGreaterEqual(mem.swap, 0)
  554. @unittest.skipIf(not HAS_MEMORY_MAPS, "not supported")
  555. def test_memory_maps(self):
  556. p = psutil.Process()
  557. maps = p.memory_maps()
  558. paths = [x for x in maps]
  559. self.assertEqual(len(paths), len(set(paths)))
  560. ext_maps = p.memory_maps(grouped=False)
  561. for nt in maps:
  562. if not nt.path.startswith('['):
  563. assert os.path.isabs(nt.path), nt.path
  564. if POSIX:
  565. try:
  566. assert os.path.exists(nt.path) or \
  567. os.path.islink(nt.path), nt.path
  568. except AssertionError:
  569. if not LINUX:
  570. raise
  571. else:
  572. # https://github.com/giampaolo/psutil/issues/759
  573. with open_text('/proc/self/smaps') as f:
  574. data = f.read()
  575. if "%s (deleted)" % nt.path not in data:
  576. raise
  577. else:
  578. # XXX - On Windows we have this strange behavior with
  579. # 64 bit dlls: they are visible via explorer but cannot
  580. # be accessed via os.stat() (wtf?).
  581. if '64' not in os.path.basename(nt.path):
  582. try:
  583. st = os.stat(nt.path)
  584. except FileNotFoundError:
  585. pass
  586. else:
  587. assert stat.S_ISREG(st.st_mode), nt.path
  588. for nt in ext_maps:
  589. for fname in nt._fields:
  590. value = getattr(nt, fname)
  591. if fname == 'path':
  592. continue
  593. elif fname in ('addr', 'perms'):
  594. assert value, value
  595. else:
  596. self.assertIsInstance(value, (int, long))
  597. assert value >= 0, value
  598. @unittest.skipIf(not HAS_MEMORY_MAPS, "not supported")
  599. def test_memory_maps_lists_lib(self):
  600. # Make sure a newly loaded shared lib is listed.
  601. p = psutil.Process()
  602. with copyload_shared_lib() as path:
  603. def normpath(p):
  604. return os.path.realpath(os.path.normcase(p))
  605. libpaths = [normpath(x.path)
  606. for x in p.memory_maps()]
  607. self.assertIn(normpath(path), libpaths)
  608. def test_memory_percent(self):
  609. p = psutil.Process()
  610. p.memory_percent()
  611. self.assertRaises(ValueError, p.memory_percent, memtype="?!?")
  612. if LINUX or MACOS or WINDOWS:
  613. p.memory_percent(memtype='uss')
  614. def test_is_running(self):
  615. p = self.spawn_psproc()
  616. assert p.is_running()
  617. assert p.is_running()
  618. p.kill()
  619. p.wait()
  620. assert not p.is_running()
  621. assert not p.is_running()
  622. def test_exe(self):
  623. p = self.spawn_psproc()
  624. exe = p.exe()
  625. try:
  626. self.assertEqual(exe, PYTHON_EXE)
  627. except AssertionError:
  628. if WINDOWS and len(exe) == len(PYTHON_EXE):
  629. # on Windows we don't care about case sensitivity
  630. normcase = os.path.normcase
  631. self.assertEqual(normcase(exe), normcase(PYTHON_EXE))
  632. else:
  633. # certain platforms such as BSD are more accurate returning:
  634. # "/usr/local/bin/python2.7"
  635. # ...instead of:
  636. # "/usr/local/bin/python"
  637. # We do not want to consider this difference in accuracy
  638. # an error.
  639. ver = "%s.%s" % (sys.version_info[0], sys.version_info[1])
  640. try:
  641. self.assertEqual(exe.replace(ver, ''),
  642. PYTHON_EXE.replace(ver, ''))
  643. except AssertionError:
  644. # Tipically MACOS. Really not sure what to do here.
  645. pass
  646. out = sh([exe, "-c", "import os; print('hey')"])
  647. self.assertEqual(out, 'hey')
  648. def test_cmdline(self):
  649. cmdline = [PYTHON_EXE, "-c", "import time; time.sleep(60)"]
  650. p = self.spawn_psproc(cmdline)
  651. try:
  652. self.assertEqual(' '.join(p.cmdline()), ' '.join(cmdline))
  653. except AssertionError:
  654. # XXX - most of the times the underlying sysctl() call on Net
  655. # and Open BSD returns a truncated string.
  656. # Also /proc/pid/cmdline behaves the same so it looks
  657. # like this is a kernel bug.
  658. # XXX - AIX truncates long arguments in /proc/pid/cmdline
  659. if NETBSD or OPENBSD or AIX:
  660. self.assertEqual(p.cmdline()[0], PYTHON_EXE)
  661. else:
  662. raise
  663. @unittest.skipIf(PYPY, "broken on PYPY")
  664. def test_long_cmdline(self):
  665. testfn = self.get_testfn()
  666. create_exe(testfn)
  667. cmdline = [testfn] + (["0123456789"] * 20)
  668. p = self.spawn_psproc(cmdline)
  669. self.assertEqual(p.cmdline(), cmdline)
  670. def test_name(self):
  671. p = self.spawn_psproc(PYTHON_EXE)
  672. name = p.name().lower()
  673. pyexe = os.path.basename(os.path.realpath(sys.executable)).lower()
  674. assert pyexe.startswith(name), (pyexe, name)
  675. @unittest.skipIf(PYPY, "unreliable on PYPY")
  676. def test_long_name(self):
  677. testfn = self.get_testfn(suffix="0123456789" * 2)
  678. create_exe(testfn)
  679. p = self.spawn_psproc(testfn)
  680. self.assertEqual(p.name(), os.path.basename(testfn))
  681. # XXX
  682. @unittest.skipIf(SUNOS, "broken on SUNOS")
  683. @unittest.skipIf(AIX, "broken on AIX")
  684. @unittest.skipIf(PYPY, "broken on PYPY")
  685. def test_prog_w_funky_name(self):
  686. # Test that name(), exe() and cmdline() correctly handle programs
  687. # with funky chars such as spaces and ")", see:
  688. # https://github.com/giampaolo/psutil/issues/628
  689. funky_path = self.get_testfn(suffix='foo bar )')
  690. create_exe(funky_path)
  691. cmdline = [funky_path, "-c",
  692. "import time; [time.sleep(0.01) for x in range(3000)];"
  693. "arg1", "arg2", "", "arg3", ""]
  694. p = self.spawn_psproc(cmdline)
  695. # ...in order to try to prevent occasional failures on travis
  696. if TRAVIS:
  697. wait_for_pid(p.pid)
  698. self.assertEqual(p.cmdline(), cmdline)
  699. self.assertEqual(p.name(), os.path.basename(funky_path))
  700. self.assertEqual(os.path.normcase(p.exe()),
  701. os.path.normcase(funky_path))
  702. @unittest.skipIf(not POSIX, 'POSIX only')
  703. def test_uids(self):
  704. p = psutil.Process()
  705. real, effective, saved = p.uids()
  706. # os.getuid() refers to "real" uid
  707. self.assertEqual(real, os.getuid())
  708. # os.geteuid() refers to "effective" uid
  709. self.assertEqual(effective, os.geteuid())
  710. # No such thing as os.getsuid() ("saved" uid), but starting
  711. # from python 2.7 we have os.getresuid() which returns all
  712. # of them.
  713. if hasattr(os, "getresuid"):
  714. self.assertEqual(os.getresuid(), p.uids())
  715. @unittest.skipIf(not POSIX, 'POSIX only')
  716. def test_gids(self):
  717. p = psutil.Process()
  718. real, effective, saved = p.gids()
  719. # os.getuid() refers to "real" uid
  720. self.assertEqual(real, os.getgid())
  721. # os.geteuid() refers to "effective" uid
  722. self.assertEqual(effective, os.getegid())
  723. # No such thing as os.getsgid() ("saved" gid), but starting
  724. # from python 2.7 we have os.getresgid() which returns all
  725. # of them.
  726. if hasattr(os, "getresuid"):
  727. self.assertEqual(os.getresgid(), p.gids())
  728. def test_nice(self):
  729. p = psutil.Process()
  730. self.assertRaises(TypeError, p.nice, "str")
  731. init = p.nice()
  732. try:
  733. if WINDOWS:
  734. for prio in [psutil.NORMAL_PRIORITY_CLASS,
  735. psutil.IDLE_PRIORITY_CLASS,
  736. psutil.BELOW_NORMAL_PRIORITY_CLASS,
  737. psutil.REALTIME_PRIORITY_CLASS,
  738. psutil.HIGH_PRIORITY_CLASS,
  739. psutil.ABOVE_NORMAL_PRIORITY_CLASS]:
  740. with self.subTest(prio=prio):
  741. try:
  742. p.nice(prio)
  743. except psutil.AccessDenied:
  744. pass
  745. else:
  746. self.assertEqual(p.nice(), prio)
  747. else:
  748. try:
  749. if hasattr(os, "getpriority"):
  750. self.assertEqual(
  751. os.getpriority(os.PRIO_PROCESS, os.getpid()),
  752. p.nice())
  753. p.nice(1)
  754. self.assertEqual(p.nice(), 1)
  755. if hasattr(os, "getpriority"):
  756. self.assertEqual(
  757. os.getpriority(os.PRIO_PROCESS, os.getpid()),
  758. p.nice())
  759. # XXX - going back to previous nice value raises
  760. # AccessDenied on MACOS
  761. if not MACOS:
  762. p.nice(0)
  763. self.assertEqual(p.nice(), 0)
  764. except psutil.AccessDenied:
  765. pass
  766. finally:
  767. try:
  768. p.nice(init)
  769. except psutil.AccessDenied:
  770. pass
  771. def test_status(self):
  772. p = psutil.Process()
  773. self.assertEqual(p.status(), psutil.STATUS_RUNNING)
  774. def test_username(self):
  775. p = self.spawn_psproc()
  776. username = p.username()
  777. if WINDOWS:
  778. domain, username = username.split('\\')
  779. self.assertEqual(username, getpass.getuser())
  780. if 'USERDOMAIN' in os.environ:
  781. self.assertEqual(domain, os.environ['USERDOMAIN'])
  782. else:
  783. self.assertEqual(username, getpass.getuser())
  784. def test_cwd(self):
  785. p = self.spawn_psproc()
  786. self.assertEqual(p.cwd(), os.getcwd())
  787. def test_cwd_2(self):
  788. cmd = [PYTHON_EXE, "-c",
  789. "import os, time; os.chdir('..'); time.sleep(60)"]
  790. p = self.spawn_psproc(cmd)
  791. call_until(p.cwd, "ret == os.path.dirname(os.getcwd())")
  792. @unittest.skipIf(not HAS_CPU_AFFINITY, 'not supported')
  793. def test_cpu_affinity(self):
  794. p = psutil.Process()
  795. initial = p.cpu_affinity()
  796. assert initial, initial
  797. self.addCleanup(p.cpu_affinity, initial)
  798. if hasattr(os, "sched_getaffinity"):
  799. self.assertEqual(initial, list(os.sched_getaffinity(p.pid)))
  800. self.assertEqual(len(initial), len(set(initial)))
  801. all_cpus = list(range(len(psutil.cpu_percent(percpu=True))))
  802. # Work around travis failure:
  803. # https://travis-ci.org/giampaolo/psutil/builds/284173194
  804. for n in all_cpus if not TRAVIS else initial:
  805. p.cpu_affinity([n])
  806. self.assertEqual(p.cpu_affinity(), [n])
  807. if hasattr(os, "sched_getaffinity"):
  808. self.assertEqual(p.cpu_affinity(),
  809. list(os.sched_getaffinity(p.pid)))
  810. # also test num_cpu()
  811. if hasattr(p, "num_cpu"):
  812. self.assertEqual(p.cpu_affinity()[0], p.num_cpu())
  813. # [] is an alias for "all eligible CPUs"; on Linux this may
  814. # not be equal to all available CPUs, see:
  815. # https://github.com/giampaolo/psutil/issues/956
  816. p.cpu_affinity([])
  817. if LINUX:
  818. self.assertEqual(p.cpu_affinity(), p._proc._get_eligible_cpus())
  819. else:
  820. self.assertEqual(p.cpu_affinity(), all_cpus)
  821. if hasattr(os, "sched_getaffinity"):
  822. self.assertEqual(p.cpu_affinity(),
  823. list(os.sched_getaffinity(p.pid)))
  824. #
  825. self.assertRaises(TypeError, p.cpu_affinity, 1)
  826. p.cpu_affinity(initial)
  827. # it should work with all iterables, not only lists
  828. if not TRAVIS:
  829. p.cpu_affinity(set(all_cpus))
  830. p.cpu_affinity(tuple(all_cpus))
  831. @unittest.skipIf(not HAS_CPU_AFFINITY, 'not supported')
  832. def test_cpu_affinity_errs(self):
  833. p = self.spawn_psproc()
  834. invalid_cpu = [len(psutil.cpu_times(percpu=True)) + 10]
  835. self.assertRaises(ValueError, p.cpu_affinity, invalid_cpu)
  836. self.assertRaises(ValueError, p.cpu_affinity, range(10000, 11000))
  837. self.assertRaises(TypeError, p.cpu_affinity, [0, "1"])
  838. self.assertRaises(ValueError, p.cpu_affinity, [0, -1])
  839. @unittest.skipIf(not HAS_CPU_AFFINITY, 'not supported')
  840. def test_cpu_affinity_all_combinations(self):
  841. p = psutil.Process()
  842. initial = p.cpu_affinity()
  843. assert initial, initial
  844. self.addCleanup(p.cpu_affinity, initial)
  845. # All possible CPU set combinations.
  846. if len(initial) > 12:
  847. initial = initial[:12] # ...otherwise it will take forever
  848. combos = []
  849. for i in range(0, len(initial) + 1):
  850. for subset in itertools.combinations(initial, i):
  851. if subset:
  852. combos.append(list(subset))
  853. for combo in combos:
  854. p.cpu_affinity(combo)
  855. self.assertEqual(sorted(p.cpu_affinity()), sorted(combo))
  856. # TODO: #595
  857. @unittest.skipIf(BSD, "broken on BSD")
  858. # can't find any process file on Appveyor
  859. @unittest.skipIf(APPVEYOR, "unreliable on APPVEYOR")
  860. def test_open_files(self):
  861. p = psutil.Process()
  862. testfn = self.get_testfn()
  863. files = p.open_files()
  864. self.assertNotIn(testfn, files)
  865. with open(testfn, 'wb') as f:
  866. f.write(b'x' * 1024)
  867. f.flush()
  868. # give the kernel some time to see the new file
  869. files = call_until(p.open_files, "len(ret) != %i" % len(files))
  870. filenames = [os.path.normcase(x.path) for x in files]
  871. self.assertIn(os.path.normcase(testfn), filenames)
  872. if LINUX:
  873. for file in files:
  874. if file.path == testfn:
  875. self.assertEqual(file.position, 1024)
  876. for file in files:
  877. assert os.path.isfile(file.path), file
  878. # another process
  879. cmdline = "import time; f = open(r'%s', 'r'); time.sleep(60);" % testfn
  880. p = self.spawn_psproc([PYTHON_EXE, "-c", cmdline])
  881. for x in range(100):
  882. filenames = [os.path.normcase(x.path) for x in p.open_files()]
  883. if testfn in filenames:
  884. break
  885. time.sleep(.01)
  886. else:
  887. self.assertIn(os.path.normcase(testfn), filenames)
  888. for file in filenames:
  889. assert os.path.isfile(file), file
  890. # TODO: #595
  891. @unittest.skipIf(BSD, "broken on BSD")
  892. # can't find any process file on Appveyor
  893. @unittest.skipIf(APPVEYOR, "unreliable on APPVEYOR")
  894. def test_open_files_2(self):
  895. # test fd and path fields
  896. p = psutil.Process()
  897. normcase = os.path.normcase
  898. testfn = self.get_testfn()
  899. with open(testfn, 'w') as fileobj:
  900. for file in p.open_files():
  901. if normcase(file.path) == normcase(fileobj.name) or \
  902. file.fd == fileobj.fileno():
  903. break
  904. else:
  905. self.fail("no file found; files=%s" % repr(p.open_files()))
  906. self.assertEqual(normcase(file.path), normcase(fileobj.name))
  907. if WINDOWS:
  908. self.assertEqual(file.fd, -1)
  909. else:
  910. self.assertEqual(file.fd, fileobj.fileno())
  911. # test positions
  912. ntuple = p.open_files()[0]
  913. self.assertEqual(ntuple[0], ntuple.path)
  914. self.assertEqual(ntuple[1], ntuple.fd)
  915. # test file is gone
  916. self.assertNotIn(fileobj.name, p.open_files())
  917. @unittest.skipIf(not POSIX, 'POSIX only')
  918. def test_num_fds(self):
  919. p = psutil.Process()
  920. testfn = self.get_testfn()
  921. start = p.num_fds()
  922. file = open(testfn, 'w')
  923. self.addCleanup(file.close)
  924. self.assertEqual(p.num_fds(), start + 1)
  925. sock = socket.socket()
  926. self.addCleanup(sock.close)
  927. self.assertEqual(p.num_fds(), start + 2)
  928. file.close()
  929. sock.close()
  930. self.assertEqual(p.num_fds(), start)
  931. @skip_on_not_implemented(only_if=LINUX)
  932. @unittest.skipIf(OPENBSD or NETBSD, "not reliable on OPENBSD & NETBSD")
  933. def test_num_ctx_switches(self):
  934. p = psutil.Process()
  935. before = sum(p.num_ctx_switches())
  936. for x in range(500000):
  937. after = sum(p.num_ctx_switches())
  938. if after > before:
  939. return
  940. self.fail("num ctx switches still the same after 50.000 iterations")
  941. def test_ppid(self):
  942. p = psutil.Process()
  943. if hasattr(os, 'getppid'):
  944. self.assertEqual(p.ppid(), os.getppid())
  945. p = self.spawn_psproc()
  946. self.assertEqual(p.ppid(), os.getpid())
  947. if APPVEYOR:
  948. # Occasional failures, see:
  949. # https://ci.appveyor.com/project/giampaolo/psutil/build/
  950. # job/0hs623nenj7w4m33
  951. return
  952. def test_parent(self):
  953. p = self.spawn_psproc()
  954. self.assertEqual(p.parent().pid, os.getpid())
  955. lowest_pid = psutil.pids()[0]
  956. self.assertIsNone(psutil.Process(lowest_pid).parent())
  957. def test_parent_multi(self):
  958. parent = psutil.Process()
  959. child, grandchild = self.spawn_children_pair()
  960. self.assertEqual(grandchild.parent(), child)
  961. self.assertEqual(child.parent(), parent)
  962. def test_parent_disappeared(self):
  963. # Emulate a case where the parent process disappeared.
  964. p = self.spawn_psproc()
  965. with mock.patch("psutil.Process",
  966. side_effect=psutil.NoSuchProcess(0, 'foo')):
  967. self.assertIsNone(p.parent())
  968. @retry_on_failure()
  969. def test_parents(self):
  970. parent = psutil.Process()
  971. assert parent.parents()
  972. child, grandchild = self.spawn_children_pair()
  973. self.assertEqual(child.parents()[0], parent)
  974. self.assertEqual(grandchild.parents()[0], child)
  975. self.assertEqual(grandchild.parents()[1], parent)
  976. def test_children(self):
  977. parent = psutil.Process()
  978. self.assertEqual(parent.children(), [])
  979. self.assertEqual(parent.children(recursive=True), [])
  980. # On Windows we set the flag to 0 in order to cancel out the
  981. # CREATE_NO_WINDOW flag (enabled by default) which creates
  982. # an extra "conhost.exe" child.
  983. child = self.spawn_psproc(creationflags=0)
  984. children1 = parent.children()
  985. children2 = parent.children(recursive=True)
  986. for children in (children1, children2):
  987. self.assertEqual(len(children), 1)
  988. self.assertEqual(children[0].pid, child.pid)
  989. self.assertEqual(children[0].ppid(), parent.pid)
  990. def test_children_recursive(self):
  991. # Test children() against two sub processes, p1 and p2, where
  992. # p1 (our child) spawned p2 (our grandchild).
  993. parent = psutil.Process()
  994. child, grandchild = self.spawn_children_pair()
  995. self.assertEqual(parent.children(), [child])
  996. self.assertEqual(parent.children(recursive=True), [child, grandchild])
  997. # If the intermediate process is gone there's no way for
  998. # children() to recursively find it.
  999. child.terminate()
  1000. child.wait()
  1001. self.assertEqual(parent.children(recursive=True), [])
  1002. def test_children_duplicates(self):
  1003. # find the process which has the highest number of children
  1004. table = collections.defaultdict(int)
  1005. for p in psutil.process_iter():
  1006. try:
  1007. table[p.ppid()] += 1
  1008. except psutil.Error:
  1009. pass
  1010. # this is the one, now let's make sure there are no duplicates
  1011. pid = sorted(table.items(), key=lambda x: x[1])[-1][0]
  1012. if LINUX and pid == 0:
  1013. raise self.skipTest("PID 0")
  1014. p = psutil.Process(pid)
  1015. try:
  1016. c = p.children(recursive=True)
  1017. except psutil.AccessDenied: # windows
  1018. pass
  1019. else:
  1020. self.assertEqual(len(c), len(set(c)))
  1021. def test_parents_and_children(self):
  1022. parent = psutil.Process()
  1023. child, grandchild = self.spawn_children_pair()
  1024. # forward
  1025. children = parent.children(recursive=True)
  1026. self.assertEqual(len(children), 2)
  1027. self.assertEqual(children[0], child)
  1028. self.assertEqual(children[1], grandchild)
  1029. # backward
  1030. parents = grandchild.parents()
  1031. self.assertEqual(parents[0], child)
  1032. self.assertEqual(parents[1], parent)
  1033. def test_suspend_resume(self):
  1034. p = self.spawn_psproc()
  1035. p.suspend()
  1036. for x in range(100):
  1037. if p.status() == psutil.STATUS_STOPPED:
  1038. break
  1039. time.sleep(0.01)
  1040. p.resume()
  1041. self.assertNotEqual(p.status(), psutil.STATUS_STOPPED)
  1042. def test_invalid_pid(self):
  1043. self.assertRaises(TypeError, psutil.Process, "1")
  1044. self.assertRaises(ValueError, psutil.Process, -1)
  1045. def test_as_dict(self):
  1046. p = psutil.Process()
  1047. d = p.as_dict(attrs=['exe', 'name'])
  1048. self.assertEqual(sorted(d.keys()), ['exe', 'name'])
  1049. p = psutil.Process(min(psutil.pids()))
  1050. d = p.as_dict(attrs=['connections'], ad_value='foo')
  1051. if not isinstance(d['connections'], list):
  1052. self.assertEqual(d['connections'], 'foo')
  1053. # Test ad_value is set on AccessDenied.
  1054. with mock.patch('psutil.Process.nice', create=True,
  1055. side_effect=psutil.AccessDenied):
  1056. self.assertEqual(
  1057. p.as_dict(attrs=["nice"], ad_value=1), {"nice": 1})
  1058. # Test that NoSuchProcess bubbles up.
  1059. with mock.patch('psutil.Process.nice', create=True,
  1060. side_effect=psutil.NoSuchProcess(p.pid, "name")):
  1061. self.assertRaises(
  1062. psutil.NoSuchProcess, p.as_dict, attrs=["nice"])
  1063. # Test that ZombieProcess is swallowed.
  1064. with mock.patch('psutil.Process.nice', create=True,
  1065. side_effect=psutil.ZombieProcess(p.pid, "name")):
  1066. self.assertEqual(
  1067. p.as_dict(attrs=["nice"], ad_value="foo"), {"nice": "foo"})
  1068. # By default APIs raising NotImplementedError are
  1069. # supposed to be skipped.
  1070. with mock.patch('psutil.Process.nice', create=True,
  1071. side_effect=NotImplementedError):
  1072. d = p.as_dict()
  1073. self.assertNotIn('nice', list(d.keys()))
  1074. # ...unless the user explicitly asked for some attr.
  1075. with self.assertRaises(NotImplementedError):
  1076. p.as_dict(attrs=["nice"])
  1077. # errors
  1078. with self.assertRaises(TypeError):
  1079. p.as_dict('name')
  1080. with self.assertRaises(ValueError):
  1081. p.as_dict(['foo'])
  1082. with self.assertRaises(ValueError):
  1083. p.as_dict(['foo', 'bar'])
  1084. def test_oneshot(self):
  1085. p = psutil.Process()
  1086. with mock.patch("psutil._psplatform.Process.cpu_times") as m:
  1087. with p.oneshot():
  1088. p.cpu_times()
  1089. p.cpu_times()
  1090. self.assertEqual(m.call_count, 1)
  1091. with mock.patch("psutil._psplatform.Process.cpu_times") as m:
  1092. p.cpu_times()
  1093. p.cpu_times()
  1094. self.assertEqual(m.call_count, 2)
  1095. def test_oneshot_twice(self):
  1096. # Test the case where the ctx manager is __enter__ed twice.
  1097. # The second __enter__ is supposed to resut in a NOOP.
  1098. p = psutil.Process()
  1099. with mock.patch("psutil._psplatform.Process.cpu_times") as m1:
  1100. with mock.patch("psutil._psplatform.Process.oneshot_enter") as m2:
  1101. with p.oneshot():
  1102. p.cpu_times()
  1103. p.cpu_times()
  1104. with p.oneshot():
  1105. p.cpu_times()
  1106. p.cpu_times()
  1107. self.assertEqual(m1.call_count, 1)
  1108. self.assertEqual(m2.call_count, 1)
  1109. with mock.patch("psutil._psplatform.Process.cpu_times") as m:
  1110. p.cpu_times()
  1111. p.cpu_times()
  1112. self.assertEqual(m.call_count, 2)
  1113. def test_oneshot_cache(self):
  1114. # Make sure oneshot() cache is nonglobal. Instead it's
  1115. # supposed to be bound to the Process instance, see:
  1116. # https://github.com/giampaolo/psutil/issues/1373
  1117. p1, p2 = self.spawn_children_pair()
  1118. p1_ppid = p1.ppid()
  1119. p2_ppid = p2.ppid()
  1120. self.assertNotEqual(p1_ppid, p2_ppid)
  1121. with p1.oneshot():
  1122. self.assertEqual(p1.ppid(), p1_ppid)
  1123. self.assertEqual(p2.ppid(), p2_ppid)
  1124. with p2.oneshot():
  1125. self.assertEqual(p1.ppid(), p1_ppid)
  1126. self.assertEqual(p2.ppid(), p2_ppid)
  1127. def test_halfway_terminated_process(self):
  1128. # Test that NoSuchProcess exception gets raised in case the
  1129. # process dies after we create the Process object.
  1130. # Example:
  1131. # >>> proc = Process(1234)
  1132. # >>> time.sleep(2) # time-consuming task, process dies in meantime
  1133. # >>> proc.name()
  1134. # Refers to Issue #15
  1135. def assert_raises_nsp(fun, fun_name):
  1136. try:
  1137. ret = fun()
  1138. except psutil.ZombieProcess: # differentiate from NSP
  1139. raise
  1140. except psutil.NoSuchProcess:
  1141. pass
  1142. except psutil.AccessDenied:
  1143. if OPENBSD and fun_name in ('threads', 'num_threads'):
  1144. return
  1145. raise
  1146. else:
  1147. # NtQuerySystemInformation succeeds even if process is gone.
  1148. if WINDOWS and fun_name in ('exe', 'name'):
  1149. return
  1150. raise self.fail("%r didn't raise NSP and returned %r "
  1151. "instead" % (fun, ret))
  1152. p = self.spawn_psproc()
  1153. p.terminate()
  1154. p.wait()
  1155. if WINDOWS: # XXX
  1156. call_until(psutil.pids, "%s not in ret" % p.pid)
  1157. self.assertProcessGone(p)
  1158. ns = process_namespace(p)
  1159. for fun, name in ns.iter(ns.all):
  1160. assert_raises_nsp(fun, name)
  1161. # NtQuerySystemInformation succeeds even if process is gone.
  1162. if WINDOWS and not GITHUB_WHEELS:
  1163. normcase = os.path.normcase
  1164. self.assertEqual(normcase(p.exe()), normcase(PYTHON_EXE))
  1165. @unittest.skipIf(not POSIX, 'POSIX only')
  1166. def test_zombie_process(self):
  1167. def succeed_or_zombie_p_exc(fun):
  1168. try:
  1169. return fun()
  1170. except (psutil.ZombieProcess, psutil.AccessDenied):
  1171. pass
  1172. parent, zombie = self.spawn_zombie()
  1173. # A zombie process should always be instantiable
  1174. zproc = psutil.Process(zombie.pid)
  1175. # ...and at least its status always be querable
  1176. self.assertEqual(zproc.status(), psutil.STATUS_ZOMBIE)
  1177. # ...and it should be considered 'running'
  1178. assert zproc.is_running()
  1179. # ...and as_dict() shouldn't crash
  1180. zproc.as_dict()
  1181. # ...its parent should 'see' it (edit: not true on BSD and MACOS
  1182. # descendants = [x.pid for x in psutil.Process().children(
  1183. # recursive=True)]
  1184. # self.assertIn(zpid, descendants)
  1185. # XXX should we also assume ppid be usable? Note: this
  1186. # would be an important use case as the only way to get
  1187. # rid of a zombie is to kill its parent.
  1188. # self.assertEqual(zpid.ppid(), os.getpid())
  1189. # ...and all other APIs should be able to deal with it
  1190. ns = process_namespace(zproc)
  1191. for fun, name in ns.iter(ns.all):
  1192. succeed_or_zombie_p_exc(fun)
  1193. assert psutil.pid_exists(zproc.pid)
  1194. if not TRAVIS and MACOS:
  1195. # For some reason this started failing all of the sudden.
  1196. # Maybe they upgraded MACOS version?
  1197. # https://travis-ci.org/giampaolo/psutil/jobs/310896404
  1198. self.assertIn(zproc.pid, psutil.pids())
  1199. self.assertIn(zproc.pid, [x.pid for x in psutil.process_iter()])
  1200. psutil._pmap = {}
  1201. self.assertIn(zproc.pid, [x.pid for x in psutil.process_iter()])
  1202. @unittest.skipIf(not POSIX, 'POSIX only')
  1203. def test_zombie_process_is_running_w_exc(self):
  1204. # Emulate a case where internally is_running() raises
  1205. # ZombieProcess.
  1206. p = psutil.Process()
  1207. with mock.patch("psutil.Process",
  1208. side_effect=psutil.ZombieProcess(0)) as m:
  1209. assert p.is_running()
  1210. assert m.called
  1211. @unittest.skipIf(not POSIX, 'POSIX only')
  1212. def test_zombie_process_status_w_exc(self):
  1213. # Emulate a case where internally status() raises
  1214. # ZombieProcess.
  1215. p = psutil.Process()
  1216. with mock.patch("psutil._psplatform.Process.status",
  1217. side_effect=psutil.ZombieProcess(0)) as m:
  1218. self.assertEqual(p.status(), psutil.STATUS_ZOMBIE)
  1219. assert m.called
  1220. def test_pid_0(self):
  1221. # Process(0) is supposed to work on all platforms except Linux
  1222. if 0 not in psutil.pids():
  1223. self.assertRaises(psutil.NoSuchProcess, psutil.Process, 0)
  1224. # These 2 are a contradiction, but "ps" says PID 1's parent
  1225. # is PID 0.
  1226. assert not psutil.pid_exists(0)
  1227. self.assertEqual(psutil.Process(1).ppid(), 0)
  1228. return
  1229. p = psutil.Process(0)
  1230. exc = psutil.AccessDenied if WINDOWS else ValueError
  1231. self.assertRaises(exc, p.wait)
  1232. self.assertRaises(exc, p.terminate)
  1233. self.assertRaises(exc, p.suspend)
  1234. self.assertRaises(exc, p.resume)
  1235. self.assertRaises(exc, p.kill)
  1236. self.assertRaises(exc, p.send_signal, signal.SIGTERM)
  1237. # test all methods
  1238. ns = process_namespace(p)
  1239. for fun, name in ns.iter(ns.getters + ns.setters):
  1240. try:
  1241. ret = fun()
  1242. except psutil.AccessDenied:
  1243. pass
  1244. else:
  1245. if name in ("uids", "gids"):
  1246. self.assertEqual(ret.real, 0)
  1247. elif name == "username":
  1248. user = 'NT AUTHORITY\\SYSTEM' if WINDOWS else 'root'
  1249. self.assertEqual(p.username(), user)
  1250. elif name == "name":
  1251. assert name, name
  1252. if not OPENBSD:
  1253. self.assertIn(0, psutil.pids())
  1254. assert psutil.pid_exists(0)
  1255. @unittest.skipIf(not HAS_ENVIRON, "not supported")
  1256. def test_environ(self):
  1257. def clean_dict(d):
  1258. # Most of these are problematic on Travis.
  1259. d.pop("PSUTIL_TESTING", None)
  1260. d.pop("PLAT", None)
  1261. d.pop("HOME", None)
  1262. if MACOS:
  1263. d.pop("__CF_USER_TEXT_ENCODING", None)
  1264. d.pop("VERSIONER_PYTHON_PREFER_32_BIT", None)
  1265. d.pop("VERSIONER_PYTHON_VERSION", None)
  1266. return dict(
  1267. [(k.replace("\r", "").replace("\n", ""),
  1268. v.replace("\r", "").replace("\n", ""))
  1269. for k, v in d.items()])
  1270. self.maxDiff = None
  1271. p = psutil.Process()
  1272. d1 = clean_dict(p.environ())
  1273. d2 = clean_dict(os.environ.copy())
  1274. if not OSX and GITHUB_WHEELS:
  1275. self.assertEqual(d1, d2)
  1276. @unittest.skipIf(not HAS_ENVIRON, "not supported")
  1277. @unittest.skipIf(not POSIX, "POSIX only")
  1278. def test_weird_environ(self):
  1279. # environment variables can contain values without an equals sign
  1280. code = textwrap.dedent("""
  1281. #include <unistd.h>
  1282. #include <fcntl.h>
  1283. char * const argv[] = {"cat", 0};
  1284. char * const envp[] = {"A=1", "X", "C=3", 0};
  1285. int main(void) {
  1286. /* Close stderr on exec so parent can wait for the execve to
  1287. * finish. */
  1288. if (fcntl(2, F_SETFD, FD_CLOEXEC) != 0)
  1289. return 0;
  1290. return execve("/bin/cat", argv, envp);
  1291. }
  1292. """)
  1293. path = self.get_testfn()
  1294. create_exe(path, c_code=code)
  1295. sproc = self.spawn_testproc(
  1296. [path], stdin=subprocess.PIPE, stderr=subprocess.PIPE)
  1297. p = psutil.Process(sproc.pid)
  1298. wait_for_pid(p.pid)
  1299. assert p.is_running()
  1300. # Wait for process to exec or exit.
  1301. self.assertEqual(sproc.stderr.read(), b"")
  1302. self.assertEqual(p.environ(), {"A": "1", "C": "3"})
  1303. sproc.communicate()
  1304. self.assertEqual(sproc.returncode, 0)
  1305. # ===================================================================
  1306. # --- Limited user tests
  1307. # ===================================================================
  1308. if POSIX and os.getuid() == 0:
  1309. class LimitedUserTestCase(TestProcess):
  1310. """Repeat the previous tests by using a limited user.
  1311. Executed only on UNIX and only if the user who run the test script
  1312. is root.
  1313. """
  1314. # the uid/gid the test suite runs under
  1315. if hasattr(os, 'getuid'):
  1316. PROCESS_UID = os.getuid()
  1317. PROCESS_GID = os.getgid()
  1318. def __init__(self, *args, **kwargs):
  1319. super().__init__(*args, **kwargs)
  1320. # re-define all existent test methods in order to
  1321. # ignore AccessDenied exceptions
  1322. for attr in [x for x in dir(self) if x.startswith('test')]:
  1323. meth = getattr(self, attr)
  1324. def test_(self):
  1325. try:
  1326. meth()
  1327. except psutil.AccessDenied:
  1328. pass
  1329. setattr(self, attr, types.MethodType(test_, self))
  1330. def setUp(self):
  1331. super().setUp()
  1332. os.setegid(1000)
  1333. os.seteuid(1000)
  1334. def tearDown(self):
  1335. os.setegid(self.PROCESS_UID)
  1336. os.seteuid(self.PROCESS_GID)
  1337. super().tearDown()
  1338. def test_nice(self):
  1339. try:
  1340. psutil.Process().nice(-1)
  1341. except psutil.AccessDenied:
  1342. pass
  1343. else:
  1344. self.fail("exception not raised")
  1345. @unittest.skipIf(1, "causes problem as root")
  1346. def test_zombie_process(self):
  1347. pass
  1348. # ===================================================================
  1349. # --- psutil.Popen tests
  1350. # ===================================================================
  1351. class TestPopen(PsutilTestCase):
  1352. """Tests for psutil.Popen class."""
  1353. @classmethod
  1354. def tearDownClass(cls):
  1355. reap_children()
  1356. def test_misc(self):
  1357. # XXX this test causes a ResourceWarning on Python 3 because
  1358. # psutil.__subproc instance doesn't get propertly freed.
  1359. # Not sure what to do though.
  1360. cmd = [PYTHON_EXE, "-c", "import time; time.sleep(60);"]
  1361. with psutil.Popen(cmd, stdout=subprocess.PIPE,
  1362. stderr=subprocess.PIPE) as proc:
  1363. proc.name()
  1364. proc.cpu_times()
  1365. proc.stdin
  1366. self.assertTrue(dir(proc))
  1367. self.assertRaises(AttributeError, getattr, proc, 'foo')
  1368. proc.terminate()
  1369. if POSIX:
  1370. self.assertEqual(proc.wait(5), -signal.SIGTERM)
  1371. else:
  1372. self.assertEqual(proc.wait(5), signal.SIGTERM)
  1373. def test_ctx_manager(self):
  1374. with psutil.Popen([PYTHON_EXE, "-V"],
  1375. stdout=subprocess.PIPE,
  1376. stderr=subprocess.PIPE,
  1377. stdin=subprocess.PIPE) as proc:
  1378. proc.communicate()
  1379. assert proc.stdout.closed
  1380. assert proc.stderr.closed
  1381. assert proc.stdin.closed
  1382. self.assertEqual(proc.returncode, 0)
  1383. def test_kill_terminate(self):
  1384. # subprocess.Popen()'s terminate(), kill() and send_signal() do
  1385. # not raise exception after the process is gone. psutil.Popen
  1386. # diverges from that.
  1387. cmd = [PYTHON_EXE, "-c", "import time; time.sleep(60);"]
  1388. with psutil.Popen(cmd, stdout=subprocess.PIPE,
  1389. stderr=subprocess.PIPE) as proc:
  1390. proc.terminate()
  1391. proc.wait()
  1392. self.assertRaises(psutil.NoSuchProcess, proc.terminate)
  1393. self.assertRaises(psutil.NoSuchProcess, proc.kill)
  1394. self.assertRaises(psutil.NoSuchProcess, proc.send_signal,
  1395. signal.SIGTERM)
  1396. if WINDOWS and sys.version_info >= (2, 7):
  1397. self.assertRaises(psutil.NoSuchProcess, proc.send_signal,
  1398. signal.CTRL_C_EVENT)
  1399. self.assertRaises(psutil.NoSuchProcess, proc.send_signal,
  1400. signal.CTRL_BREAK_EVENT)
  1401. if __name__ == '__main__':
  1402. from psutil.tests.runner import run_from_name
  1403. run_from_name(__file__)