test_process.py 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571
  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 unittest
  20. import psutil
  21. from psutil import AIX
  22. from psutil import BSD
  23. from psutil import LINUX
  24. from psutil import MACOS
  25. from psutil import NETBSD
  26. from psutil import OPENBSD
  27. from psutil import OSX
  28. from psutil import POSIX
  29. from psutil import SUNOS
  30. from psutil import WINDOWS
  31. from psutil._common import open_text
  32. from psutil._compat import PY3
  33. from psutil._compat import FileNotFoundError
  34. from psutil._compat import long
  35. from psutil._compat import super
  36. from psutil.tests import APPVEYOR
  37. from psutil.tests import CI_TESTING
  38. from psutil.tests import GITHUB_ACTIONS
  39. from psutil.tests import GLOBAL_TIMEOUT
  40. from psutil.tests import HAS_CPU_AFFINITY
  41. from psutil.tests import HAS_ENVIRON
  42. from psutil.tests import HAS_IONICE
  43. from psutil.tests import HAS_MEMORY_MAPS
  44. from psutil.tests import HAS_PROC_CPU_NUM
  45. from psutil.tests import HAS_PROC_IO_COUNTERS
  46. from psutil.tests import HAS_RLIMIT
  47. from psutil.tests import HAS_THREADS
  48. from psutil.tests import MACOS_11PLUS
  49. from psutil.tests import PYPY
  50. from psutil.tests import PYTHON_EXE
  51. from psutil.tests import PYTHON_EXE_ENV
  52. from psutil.tests import PsutilTestCase
  53. from psutil.tests import ThreadTask
  54. from psutil.tests import call_until
  55. from psutil.tests import copyload_shared_lib
  56. from psutil.tests import create_exe
  57. from psutil.tests import mock
  58. from psutil.tests import process_namespace
  59. from psutil.tests import reap_children
  60. from psutil.tests import retry_on_failure
  61. from psutil.tests import sh
  62. from psutil.tests import skip_on_access_denied
  63. from psutil.tests import skip_on_not_implemented
  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. @unittest.skipIf(NETBSD, "fails on NETBSD")
  145. def test_wait_stopped(self):
  146. p = self.spawn_psproc()
  147. if POSIX:
  148. # Test waitpid() + WIFSTOPPED and WIFCONTINUED.
  149. # Note: if a process is stopped it ignores SIGTERM.
  150. p.send_signal(signal.SIGSTOP)
  151. self.assertRaises(psutil.TimeoutExpired, p.wait, timeout=0.001)
  152. p.send_signal(signal.SIGCONT)
  153. self.assertRaises(psutil.TimeoutExpired, p.wait, timeout=0.001)
  154. p.send_signal(signal.SIGTERM)
  155. self.assertEqual(p.wait(), -signal.SIGTERM)
  156. self.assertEqual(p.wait(), -signal.SIGTERM)
  157. else:
  158. p.suspend()
  159. self.assertRaises(psutil.TimeoutExpired, p.wait, timeout=0.001)
  160. p.resume()
  161. self.assertRaises(psutil.TimeoutExpired, p.wait, timeout=0.001)
  162. p.terminate()
  163. self.assertEqual(p.wait(), signal.SIGTERM)
  164. self.assertEqual(p.wait(), signal.SIGTERM)
  165. def test_wait_non_children(self):
  166. # Test wait() against a process which is not our direct
  167. # child.
  168. child, grandchild = self.spawn_children_pair()
  169. self.assertRaises(psutil.TimeoutExpired, child.wait, 0.01)
  170. self.assertRaises(psutil.TimeoutExpired, grandchild.wait, 0.01)
  171. # We also terminate the direct child otherwise the
  172. # grandchild will hang until the parent is gone.
  173. child.terminate()
  174. grandchild.terminate()
  175. child_ret = child.wait()
  176. grandchild_ret = grandchild.wait()
  177. if POSIX:
  178. self.assertEqual(child_ret, -signal.SIGTERM)
  179. # For processes which are not our children we're supposed
  180. # to get None.
  181. self.assertEqual(grandchild_ret, None)
  182. else:
  183. self.assertEqual(child_ret, signal.SIGTERM)
  184. self.assertEqual(child_ret, signal.SIGTERM)
  185. def test_wait_timeout(self):
  186. p = self.spawn_psproc()
  187. p.name()
  188. self.assertRaises(psutil.TimeoutExpired, p.wait, 0.01)
  189. self.assertRaises(psutil.TimeoutExpired, p.wait, 0)
  190. self.assertRaises(ValueError, p.wait, -1)
  191. def test_wait_timeout_nonblocking(self):
  192. p = self.spawn_psproc()
  193. self.assertRaises(psutil.TimeoutExpired, p.wait, 0)
  194. p.kill()
  195. stop_at = time.time() + GLOBAL_TIMEOUT
  196. while time.time() < stop_at:
  197. try:
  198. code = p.wait(0)
  199. break
  200. except psutil.TimeoutExpired:
  201. pass
  202. else:
  203. raise self.fail('timeout')
  204. if POSIX:
  205. self.assertEqual(code, -signal.SIGKILL)
  206. else:
  207. self.assertEqual(code, signal.SIGTERM)
  208. self.assertProcessGone(p)
  209. def test_cpu_percent(self):
  210. p = psutil.Process()
  211. p.cpu_percent(interval=0.001)
  212. p.cpu_percent(interval=0.001)
  213. for _ in range(100):
  214. percent = p.cpu_percent(interval=None)
  215. self.assertIsInstance(percent, float)
  216. self.assertGreaterEqual(percent, 0.0)
  217. with self.assertRaises(ValueError):
  218. p.cpu_percent(interval=-1)
  219. def test_cpu_percent_numcpus_none(self):
  220. # See: https://github.com/giampaolo/psutil/issues/1087
  221. with mock.patch('psutil.cpu_count', return_value=None) as m:
  222. psutil.Process().cpu_percent()
  223. assert m.called
  224. def test_cpu_times(self):
  225. times = psutil.Process().cpu_times()
  226. assert (times.user > 0.0) or (times.system > 0.0), times
  227. assert (times.children_user >= 0.0), times
  228. assert (times.children_system >= 0.0), times
  229. if LINUX:
  230. assert times.iowait >= 0.0, times
  231. # make sure returned values can be pretty printed with strftime
  232. for name in times._fields:
  233. time.strftime("%H:%M:%S", time.localtime(getattr(times, name)))
  234. def test_cpu_times_2(self):
  235. user_time, kernel_time = psutil.Process().cpu_times()[:2]
  236. utime, ktime = os.times()[:2]
  237. # Use os.times()[:2] as base values to compare our results
  238. # using a tolerance of +/- 0.1 seconds.
  239. # It will fail if the difference between the values is > 0.1s.
  240. if (max([user_time, utime]) - min([user_time, utime])) > 0.1:
  241. raise self.fail("expected: %s, found: %s" % (utime, user_time))
  242. if (max([kernel_time, ktime]) - min([kernel_time, ktime])) > 0.1:
  243. raise self.fail("expected: %s, found: %s" % (ktime, kernel_time))
  244. @unittest.skipIf(not HAS_PROC_CPU_NUM, "not supported")
  245. def test_cpu_num(self):
  246. p = psutil.Process()
  247. num = p.cpu_num()
  248. self.assertGreaterEqual(num, 0)
  249. if psutil.cpu_count() == 1:
  250. self.assertEqual(num, 0)
  251. self.assertIn(p.cpu_num(), range(psutil.cpu_count()))
  252. def test_create_time(self):
  253. p = self.spawn_psproc()
  254. now = time.time()
  255. create_time = p.create_time()
  256. # Use time.time() as base value to compare our result using a
  257. # tolerance of +/- 1 second.
  258. # It will fail if the difference between the values is > 2s.
  259. difference = abs(create_time - now)
  260. if difference > 2:
  261. raise self.fail("expected: %s, found: %s, difference: %s"
  262. % (now, create_time, difference))
  263. # make sure returned value can be pretty printed with strftime
  264. time.strftime("%Y %m %d %H:%M:%S", time.localtime(p.create_time()))
  265. @unittest.skipIf(not POSIX, 'POSIX only')
  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. self.assertEqual(len(maps), len(set(maps)))
  559. ext_maps = p.memory_maps(grouped=False)
  560. for nt in maps:
  561. if not nt.path.startswith('['):
  562. assert os.path.isabs(nt.path), nt.path
  563. if POSIX:
  564. try:
  565. assert os.path.exists(nt.path) or \
  566. os.path.islink(nt.path), nt.path
  567. except AssertionError:
  568. if not LINUX:
  569. raise
  570. else:
  571. # https://github.com/giampaolo/psutil/issues/759
  572. with open_text('/proc/self/smaps') as f:
  573. data = f.read()
  574. if "%s (deleted)" % nt.path not in data:
  575. raise
  576. else:
  577. # XXX - On Windows we have this strange behavior with
  578. # 64 bit dlls: they are visible via explorer but cannot
  579. # be accessed via os.stat() (wtf?).
  580. if '64' not in os.path.basename(nt.path):
  581. try:
  582. st = os.stat(nt.path)
  583. except FileNotFoundError:
  584. pass
  585. else:
  586. assert stat.S_ISREG(st.st_mode), nt.path
  587. for nt in ext_maps:
  588. for fname in nt._fields:
  589. value = getattr(nt, fname)
  590. if fname == 'path':
  591. continue
  592. elif fname in ('addr', 'perms'):
  593. assert value, value
  594. else:
  595. self.assertIsInstance(value, (int, long))
  596. assert value >= 0, value
  597. @unittest.skipIf(not HAS_MEMORY_MAPS, "not supported")
  598. def test_memory_maps_lists_lib(self):
  599. # Make sure a newly loaded shared lib is listed.
  600. p = psutil.Process()
  601. with copyload_shared_lib() as path:
  602. def normpath(p):
  603. return os.path.realpath(os.path.normcase(p))
  604. libpaths = [normpath(x.path)
  605. for x in p.memory_maps()]
  606. self.assertIn(normpath(path), libpaths)
  607. def test_memory_percent(self):
  608. p = psutil.Process()
  609. p.memory_percent()
  610. self.assertRaises(ValueError, p.memory_percent, memtype="?!?")
  611. if LINUX or MACOS or WINDOWS:
  612. p.memory_percent(memtype='uss')
  613. def test_is_running(self):
  614. p = self.spawn_psproc()
  615. assert p.is_running()
  616. assert p.is_running()
  617. p.kill()
  618. p.wait()
  619. assert not p.is_running()
  620. assert not p.is_running()
  621. def test_exe(self):
  622. p = self.spawn_psproc()
  623. exe = p.exe()
  624. try:
  625. self.assertEqual(exe, PYTHON_EXE)
  626. except AssertionError:
  627. if WINDOWS and len(exe) == len(PYTHON_EXE):
  628. # on Windows we don't care about case sensitivity
  629. normcase = os.path.normcase
  630. self.assertEqual(normcase(exe), normcase(PYTHON_EXE))
  631. else:
  632. # certain platforms such as BSD are more accurate returning:
  633. # "/usr/local/bin/python2.7"
  634. # ...instead of:
  635. # "/usr/local/bin/python"
  636. # We do not want to consider this difference in accuracy
  637. # an error.
  638. ver = "%s.%s" % (sys.version_info[0], sys.version_info[1])
  639. try:
  640. self.assertEqual(exe.replace(ver, ''),
  641. PYTHON_EXE.replace(ver, ''))
  642. except AssertionError:
  643. # Typically MACOS. Really not sure what to do here.
  644. pass
  645. out = sh([exe, "-c", "import os; print('hey')"])
  646. self.assertEqual(out, 'hey')
  647. def test_cmdline(self):
  648. cmdline = [PYTHON_EXE, "-c", "import time; time.sleep(60)"]
  649. p = self.spawn_psproc(cmdline)
  650. # XXX - most of the times the underlying sysctl() call on Net
  651. # and Open BSD returns a truncated string.
  652. # Also /proc/pid/cmdline behaves the same so it looks
  653. # like this is a kernel bug.
  654. # XXX - AIX truncates long arguments in /proc/pid/cmdline
  655. if NETBSD or OPENBSD or AIX:
  656. self.assertEqual(p.cmdline()[0], PYTHON_EXE)
  657. else:
  658. if MACOS and CI_TESTING:
  659. pyexe = p.cmdline()[0]
  660. if pyexe != PYTHON_EXE:
  661. self.assertEqual(' '.join(p.cmdline()[1:]),
  662. ' '.join(cmdline[1:]))
  663. return
  664. self.assertEqual(' '.join(p.cmdline()), ' '.join(cmdline))
  665. @unittest.skipIf(PYPY, "broken on PYPY")
  666. def test_long_cmdline(self):
  667. testfn = self.get_testfn()
  668. create_exe(testfn)
  669. cmdline = [testfn] + (["0123456789"] * 20)
  670. p = self.spawn_psproc(cmdline)
  671. if OPENBSD:
  672. # XXX: for some reason the test process may turn into a
  673. # zombie (don't know why).
  674. try:
  675. self.assertEqual(p.cmdline(), cmdline)
  676. except psutil.ZombieProcess:
  677. raise self.skipTest("OPENBSD: process turned into zombie")
  678. else:
  679. self.assertEqual(p.cmdline(), cmdline)
  680. def test_name(self):
  681. p = self.spawn_psproc(PYTHON_EXE)
  682. name = p.name().lower()
  683. pyexe = os.path.basename(os.path.realpath(sys.executable)).lower()
  684. assert pyexe.startswith(name), (pyexe, name)
  685. @unittest.skipIf(PYPY, "unreliable on PYPY")
  686. def test_long_name(self):
  687. testfn = self.get_testfn(suffix="0123456789" * 2)
  688. create_exe(testfn)
  689. p = self.spawn_psproc(testfn)
  690. if OPENBSD:
  691. # XXX: for some reason the test process may turn into a
  692. # zombie (don't know why). Because the name() is long, all
  693. # UNIX kernels truncate it to 15 chars, so internally psutil
  694. # tries to guess the full name() from the cmdline(). But the
  695. # cmdline() of a zombie on OpenBSD fails (internally), so we
  696. # just compare the first 15 chars. Full explanation:
  697. # https://github.com/giampaolo/psutil/issues/2239
  698. try:
  699. self.assertEqual(p.name(), os.path.basename(testfn))
  700. except AssertionError:
  701. if p.status() == psutil.STATUS_ZOMBIE:
  702. assert os.path.basename(testfn).startswith(p.name())
  703. else:
  704. raise
  705. else:
  706. self.assertEqual(p.name(), os.path.basename(testfn))
  707. # XXX
  708. @unittest.skipIf(SUNOS, "broken on SUNOS")
  709. @unittest.skipIf(AIX, "broken on AIX")
  710. @unittest.skipIf(PYPY, "broken on PYPY")
  711. def test_prog_w_funky_name(self):
  712. # Test that name(), exe() and cmdline() correctly handle programs
  713. # with funky chars such as spaces and ")", see:
  714. # https://github.com/giampaolo/psutil/issues/628
  715. funky_path = self.get_testfn(suffix='foo bar )')
  716. create_exe(funky_path)
  717. cmdline = [funky_path, "-c",
  718. "import time; [time.sleep(0.01) for x in range(3000)];"
  719. "arg1", "arg2", "", "arg3", ""]
  720. p = self.spawn_psproc(cmdline)
  721. self.assertEqual(p.cmdline(), cmdline)
  722. self.assertEqual(p.name(), os.path.basename(funky_path))
  723. self.assertEqual(os.path.normcase(p.exe()),
  724. os.path.normcase(funky_path))
  725. @unittest.skipIf(not POSIX, 'POSIX only')
  726. def test_uids(self):
  727. p = psutil.Process()
  728. real, effective, saved = p.uids()
  729. # os.getuid() refers to "real" uid
  730. self.assertEqual(real, os.getuid())
  731. # os.geteuid() refers to "effective" uid
  732. self.assertEqual(effective, os.geteuid())
  733. # No such thing as os.getsuid() ("saved" uid), but starting
  734. # from python 2.7 we have os.getresuid() which returns all
  735. # of them.
  736. if hasattr(os, "getresuid"):
  737. self.assertEqual(os.getresuid(), p.uids())
  738. @unittest.skipIf(not POSIX, 'POSIX only')
  739. def test_gids(self):
  740. p = psutil.Process()
  741. real, effective, saved = p.gids()
  742. # os.getuid() refers to "real" uid
  743. self.assertEqual(real, os.getgid())
  744. # os.geteuid() refers to "effective" uid
  745. self.assertEqual(effective, os.getegid())
  746. # No such thing as os.getsgid() ("saved" gid), but starting
  747. # from python 2.7 we have os.getresgid() which returns all
  748. # of them.
  749. if hasattr(os, "getresuid"):
  750. self.assertEqual(os.getresgid(), p.gids())
  751. def test_nice(self):
  752. p = psutil.Process()
  753. self.assertRaises(TypeError, p.nice, "str")
  754. init = p.nice()
  755. try:
  756. if WINDOWS:
  757. # A CI runner may limit our maximum priority, which will break
  758. # this test. Instead, we test in order of increasing priority,
  759. # and match either the expected value or the highest so far.
  760. highest_prio = None
  761. for prio in [psutil.IDLE_PRIORITY_CLASS,
  762. psutil.BELOW_NORMAL_PRIORITY_CLASS,
  763. psutil.NORMAL_PRIORITY_CLASS,
  764. psutil.ABOVE_NORMAL_PRIORITY_CLASS,
  765. psutil.HIGH_PRIORITY_CLASS,
  766. psutil.REALTIME_PRIORITY_CLASS]:
  767. with self.subTest(prio=prio):
  768. try:
  769. p.nice(prio)
  770. except psutil.AccessDenied:
  771. pass
  772. else:
  773. new_prio = p.nice()
  774. if CI_TESTING:
  775. if new_prio == prio or highest_prio is None:
  776. highest_prio = prio
  777. self.assertEqual(new_prio, highest_prio)
  778. else:
  779. self.assertEqual(new_prio, prio)
  780. else:
  781. try:
  782. if hasattr(os, "getpriority"):
  783. self.assertEqual(
  784. os.getpriority(os.PRIO_PROCESS, os.getpid()),
  785. p.nice())
  786. p.nice(1)
  787. self.assertEqual(p.nice(), 1)
  788. if hasattr(os, "getpriority"):
  789. self.assertEqual(
  790. os.getpriority(os.PRIO_PROCESS, os.getpid()),
  791. p.nice())
  792. # XXX - going back to previous nice value raises
  793. # AccessDenied on MACOS
  794. if not MACOS:
  795. p.nice(0)
  796. self.assertEqual(p.nice(), 0)
  797. except psutil.AccessDenied:
  798. pass
  799. finally:
  800. try:
  801. p.nice(init)
  802. except psutil.AccessDenied:
  803. pass
  804. def test_status(self):
  805. p = psutil.Process()
  806. self.assertEqual(p.status(), psutil.STATUS_RUNNING)
  807. def test_username(self):
  808. p = self.spawn_psproc()
  809. username = p.username()
  810. if WINDOWS:
  811. domain, username = username.split('\\')
  812. getpass_user = getpass.getuser()
  813. if getpass_user.endswith('$'):
  814. # When running as a service account (most likely to be
  815. # NetworkService), these user name calculations don't produce
  816. # the same result, causing the test to fail.
  817. raise unittest.SkipTest('running as service account')
  818. self.assertEqual(username, getpass_user)
  819. if 'USERDOMAIN' in os.environ:
  820. self.assertEqual(domain, os.environ['USERDOMAIN'])
  821. else:
  822. self.assertEqual(username, getpass.getuser())
  823. def test_cwd(self):
  824. p = self.spawn_psproc()
  825. self.assertEqual(p.cwd(), os.getcwd())
  826. def test_cwd_2(self):
  827. cmd = [PYTHON_EXE, "-c",
  828. "import os, time; os.chdir('..'); time.sleep(60)"]
  829. p = self.spawn_psproc(cmd)
  830. call_until(p.cwd, "ret == os.path.dirname(os.getcwd())")
  831. @unittest.skipIf(not HAS_CPU_AFFINITY, 'not supported')
  832. def test_cpu_affinity(self):
  833. p = psutil.Process()
  834. initial = p.cpu_affinity()
  835. assert initial, initial
  836. self.addCleanup(p.cpu_affinity, initial)
  837. if hasattr(os, "sched_getaffinity"):
  838. self.assertEqual(initial, list(os.sched_getaffinity(p.pid)))
  839. self.assertEqual(len(initial), len(set(initial)))
  840. all_cpus = list(range(len(psutil.cpu_percent(percpu=True))))
  841. for n in all_cpus:
  842. p.cpu_affinity([n])
  843. self.assertEqual(p.cpu_affinity(), [n])
  844. if hasattr(os, "sched_getaffinity"):
  845. self.assertEqual(p.cpu_affinity(),
  846. list(os.sched_getaffinity(p.pid)))
  847. # also test num_cpu()
  848. if hasattr(p, "num_cpu"):
  849. self.assertEqual(p.cpu_affinity()[0], p.num_cpu())
  850. # [] is an alias for "all eligible CPUs"; on Linux this may
  851. # not be equal to all available CPUs, see:
  852. # https://github.com/giampaolo/psutil/issues/956
  853. p.cpu_affinity([])
  854. if LINUX:
  855. self.assertEqual(p.cpu_affinity(), p._proc._get_eligible_cpus())
  856. else:
  857. self.assertEqual(p.cpu_affinity(), all_cpus)
  858. if hasattr(os, "sched_getaffinity"):
  859. self.assertEqual(p.cpu_affinity(),
  860. list(os.sched_getaffinity(p.pid)))
  861. #
  862. self.assertRaises(TypeError, p.cpu_affinity, 1)
  863. p.cpu_affinity(initial)
  864. # it should work with all iterables, not only lists
  865. p.cpu_affinity(set(all_cpus))
  866. p.cpu_affinity(tuple(all_cpus))
  867. @unittest.skipIf(not HAS_CPU_AFFINITY, 'not supported')
  868. def test_cpu_affinity_errs(self):
  869. p = self.spawn_psproc()
  870. invalid_cpu = [len(psutil.cpu_times(percpu=True)) + 10]
  871. self.assertRaises(ValueError, p.cpu_affinity, invalid_cpu)
  872. self.assertRaises(ValueError, p.cpu_affinity, range(10000, 11000))
  873. self.assertRaises(TypeError, p.cpu_affinity, [0, "1"])
  874. self.assertRaises(ValueError, p.cpu_affinity, [0, -1])
  875. @unittest.skipIf(not HAS_CPU_AFFINITY, 'not supported')
  876. def test_cpu_affinity_all_combinations(self):
  877. p = psutil.Process()
  878. initial = p.cpu_affinity()
  879. assert initial, initial
  880. self.addCleanup(p.cpu_affinity, initial)
  881. # All possible CPU set combinations.
  882. if len(initial) > 12:
  883. initial = initial[:12] # ...otherwise it will take forever
  884. combos = []
  885. for i in range(len(initial) + 1):
  886. for subset in itertools.combinations(initial, i):
  887. if subset:
  888. combos.append(list(subset))
  889. for combo in combos:
  890. p.cpu_affinity(combo)
  891. self.assertEqual(sorted(p.cpu_affinity()), sorted(combo))
  892. # TODO: #595
  893. @unittest.skipIf(BSD, "broken on BSD")
  894. # can't find any process file on Appveyor
  895. @unittest.skipIf(APPVEYOR, "unreliable on APPVEYOR")
  896. def test_open_files(self):
  897. p = psutil.Process()
  898. testfn = self.get_testfn()
  899. files = p.open_files()
  900. self.assertNotIn(testfn, files)
  901. with open(testfn, 'wb') as f:
  902. f.write(b'x' * 1024)
  903. f.flush()
  904. # give the kernel some time to see the new file
  905. files = call_until(p.open_files, "len(ret) != %i" % len(files))
  906. filenames = [os.path.normcase(x.path) for x in files]
  907. self.assertIn(os.path.normcase(testfn), filenames)
  908. if LINUX:
  909. for file in files:
  910. if file.path == testfn:
  911. self.assertEqual(file.position, 1024)
  912. for file in files:
  913. assert os.path.isfile(file.path), file
  914. # another process
  915. cmdline = "import time; f = open(r'%s', 'r'); time.sleep(60);" % testfn
  916. p = self.spawn_psproc([PYTHON_EXE, "-c", cmdline])
  917. for x in range(100):
  918. filenames = [os.path.normcase(x.path) for x in p.open_files()]
  919. if testfn in filenames:
  920. break
  921. time.sleep(.01)
  922. else:
  923. self.assertIn(os.path.normcase(testfn), filenames)
  924. for file in filenames:
  925. assert os.path.isfile(file), file
  926. # TODO: #595
  927. @unittest.skipIf(BSD, "broken on BSD")
  928. # can't find any process file on Appveyor
  929. @unittest.skipIf(APPVEYOR, "unreliable on APPVEYOR")
  930. def test_open_files_2(self):
  931. # test fd and path fields
  932. p = psutil.Process()
  933. normcase = os.path.normcase
  934. testfn = self.get_testfn()
  935. with open(testfn, 'w') as fileobj:
  936. for file in p.open_files():
  937. if normcase(file.path) == normcase(fileobj.name) or \
  938. file.fd == fileobj.fileno():
  939. break
  940. else:
  941. raise self.fail("no file found; files=%s" % (
  942. repr(p.open_files())))
  943. self.assertEqual(normcase(file.path), normcase(fileobj.name))
  944. if WINDOWS:
  945. self.assertEqual(file.fd, -1)
  946. else:
  947. self.assertEqual(file.fd, fileobj.fileno())
  948. # test positions
  949. ntuple = p.open_files()[0]
  950. self.assertEqual(ntuple[0], ntuple.path)
  951. self.assertEqual(ntuple[1], ntuple.fd)
  952. # test file is gone
  953. self.assertNotIn(fileobj.name, p.open_files())
  954. @unittest.skipIf(not POSIX, 'POSIX only')
  955. def test_num_fds(self):
  956. p = psutil.Process()
  957. testfn = self.get_testfn()
  958. start = p.num_fds()
  959. file = open(testfn, 'w')
  960. self.addCleanup(file.close)
  961. self.assertEqual(p.num_fds(), start + 1)
  962. sock = socket.socket()
  963. self.addCleanup(sock.close)
  964. self.assertEqual(p.num_fds(), start + 2)
  965. file.close()
  966. sock.close()
  967. self.assertEqual(p.num_fds(), start)
  968. @skip_on_not_implemented(only_if=LINUX)
  969. @unittest.skipIf(OPENBSD or NETBSD, "not reliable on OPENBSD & NETBSD")
  970. def test_num_ctx_switches(self):
  971. p = psutil.Process()
  972. before = sum(p.num_ctx_switches())
  973. for _ in range(2):
  974. time.sleep(0.05) # this shall ensure a context switch happens
  975. after = sum(p.num_ctx_switches())
  976. if after > before:
  977. return
  978. raise self.fail("num ctx switches still the same after 2 iterations")
  979. def test_ppid(self):
  980. p = psutil.Process()
  981. if hasattr(os, 'getppid'):
  982. self.assertEqual(p.ppid(), os.getppid())
  983. p = self.spawn_psproc()
  984. self.assertEqual(p.ppid(), os.getpid())
  985. def test_parent(self):
  986. p = self.spawn_psproc()
  987. self.assertEqual(p.parent().pid, os.getpid())
  988. lowest_pid = psutil.pids()[0]
  989. self.assertIsNone(psutil.Process(lowest_pid).parent())
  990. def test_parent_multi(self):
  991. parent = psutil.Process()
  992. child, grandchild = self.spawn_children_pair()
  993. self.assertEqual(grandchild.parent(), child)
  994. self.assertEqual(child.parent(), parent)
  995. @retry_on_failure()
  996. def test_parents(self):
  997. parent = psutil.Process()
  998. assert parent.parents()
  999. child, grandchild = self.spawn_children_pair()
  1000. self.assertEqual(child.parents()[0], parent)
  1001. self.assertEqual(grandchild.parents()[0], child)
  1002. self.assertEqual(grandchild.parents()[1], parent)
  1003. def test_children(self):
  1004. parent = psutil.Process()
  1005. self.assertEqual(parent.children(), [])
  1006. self.assertEqual(parent.children(recursive=True), [])
  1007. # On Windows we set the flag to 0 in order to cancel out the
  1008. # CREATE_NO_WINDOW flag (enabled by default) which creates
  1009. # an extra "conhost.exe" child.
  1010. child = self.spawn_psproc(creationflags=0)
  1011. children1 = parent.children()
  1012. children2 = parent.children(recursive=True)
  1013. for children in (children1, children2):
  1014. self.assertEqual(len(children), 1)
  1015. self.assertEqual(children[0].pid, child.pid)
  1016. self.assertEqual(children[0].ppid(), parent.pid)
  1017. def test_children_recursive(self):
  1018. # Test children() against two sub processes, p1 and p2, where
  1019. # p1 (our child) spawned p2 (our grandchild).
  1020. parent = psutil.Process()
  1021. child, grandchild = self.spawn_children_pair()
  1022. self.assertEqual(parent.children(), [child])
  1023. self.assertEqual(parent.children(recursive=True), [child, grandchild])
  1024. # If the intermediate process is gone there's no way for
  1025. # children() to recursively find it.
  1026. child.terminate()
  1027. child.wait()
  1028. self.assertEqual(parent.children(recursive=True), [])
  1029. def test_children_duplicates(self):
  1030. # find the process which has the highest number of children
  1031. table = collections.defaultdict(int)
  1032. for p in psutil.process_iter():
  1033. try:
  1034. table[p.ppid()] += 1
  1035. except psutil.Error:
  1036. pass
  1037. # this is the one, now let's make sure there are no duplicates
  1038. pid = sorted(table.items(), key=lambda x: x[1])[-1][0]
  1039. if LINUX and pid == 0:
  1040. raise self.skipTest("PID 0")
  1041. p = psutil.Process(pid)
  1042. try:
  1043. c = p.children(recursive=True)
  1044. except psutil.AccessDenied: # windows
  1045. pass
  1046. else:
  1047. self.assertEqual(len(c), len(set(c)))
  1048. def test_parents_and_children(self):
  1049. parent = psutil.Process()
  1050. child, grandchild = self.spawn_children_pair()
  1051. # forward
  1052. children = parent.children(recursive=True)
  1053. self.assertEqual(len(children), 2)
  1054. self.assertEqual(children[0], child)
  1055. self.assertEqual(children[1], grandchild)
  1056. # backward
  1057. parents = grandchild.parents()
  1058. self.assertEqual(parents[0], child)
  1059. self.assertEqual(parents[1], parent)
  1060. def test_suspend_resume(self):
  1061. p = self.spawn_psproc()
  1062. p.suspend()
  1063. for _ in range(100):
  1064. if p.status() == psutil.STATUS_STOPPED:
  1065. break
  1066. time.sleep(0.01)
  1067. p.resume()
  1068. self.assertNotEqual(p.status(), psutil.STATUS_STOPPED)
  1069. def test_invalid_pid(self):
  1070. self.assertRaises(TypeError, psutil.Process, "1")
  1071. self.assertRaises(ValueError, psutil.Process, -1)
  1072. def test_as_dict(self):
  1073. p = psutil.Process()
  1074. d = p.as_dict(attrs=['exe', 'name'])
  1075. self.assertEqual(sorted(d.keys()), ['exe', 'name'])
  1076. p = psutil.Process(min(psutil.pids()))
  1077. d = p.as_dict(attrs=['connections'], ad_value='foo')
  1078. if not isinstance(d['connections'], list):
  1079. self.assertEqual(d['connections'], 'foo')
  1080. # Test ad_value is set on AccessDenied.
  1081. with mock.patch('psutil.Process.nice', create=True,
  1082. side_effect=psutil.AccessDenied):
  1083. self.assertEqual(
  1084. p.as_dict(attrs=["nice"], ad_value=1), {"nice": 1})
  1085. # Test that NoSuchProcess bubbles up.
  1086. with mock.patch('psutil.Process.nice', create=True,
  1087. side_effect=psutil.NoSuchProcess(p.pid, "name")):
  1088. self.assertRaises(
  1089. psutil.NoSuchProcess, p.as_dict, attrs=["nice"])
  1090. # Test that ZombieProcess is swallowed.
  1091. with mock.patch('psutil.Process.nice', create=True,
  1092. side_effect=psutil.ZombieProcess(p.pid, "name")):
  1093. self.assertEqual(
  1094. p.as_dict(attrs=["nice"], ad_value="foo"), {"nice": "foo"})
  1095. # By default APIs raising NotImplementedError are
  1096. # supposed to be skipped.
  1097. with mock.patch('psutil.Process.nice', create=True,
  1098. side_effect=NotImplementedError):
  1099. d = p.as_dict()
  1100. self.assertNotIn('nice', list(d.keys()))
  1101. # ...unless the user explicitly asked for some attr.
  1102. with self.assertRaises(NotImplementedError):
  1103. p.as_dict(attrs=["nice"])
  1104. # errors
  1105. with self.assertRaises(TypeError):
  1106. p.as_dict('name')
  1107. with self.assertRaises(ValueError):
  1108. p.as_dict(['foo'])
  1109. with self.assertRaises(ValueError):
  1110. p.as_dict(['foo', 'bar'])
  1111. def test_oneshot(self):
  1112. p = psutil.Process()
  1113. with mock.patch("psutil._psplatform.Process.cpu_times") as m:
  1114. with p.oneshot():
  1115. p.cpu_times()
  1116. p.cpu_times()
  1117. self.assertEqual(m.call_count, 1)
  1118. with mock.patch("psutil._psplatform.Process.cpu_times") as m:
  1119. p.cpu_times()
  1120. p.cpu_times()
  1121. self.assertEqual(m.call_count, 2)
  1122. def test_oneshot_twice(self):
  1123. # Test the case where the ctx manager is __enter__ed twice.
  1124. # The second __enter__ is supposed to resut in a NOOP.
  1125. p = psutil.Process()
  1126. with mock.patch("psutil._psplatform.Process.cpu_times") as m1:
  1127. with mock.patch("psutil._psplatform.Process.oneshot_enter") as m2:
  1128. with p.oneshot():
  1129. p.cpu_times()
  1130. p.cpu_times()
  1131. with p.oneshot():
  1132. p.cpu_times()
  1133. p.cpu_times()
  1134. self.assertEqual(m1.call_count, 1)
  1135. self.assertEqual(m2.call_count, 1)
  1136. with mock.patch("psutil._psplatform.Process.cpu_times") as m:
  1137. p.cpu_times()
  1138. p.cpu_times()
  1139. self.assertEqual(m.call_count, 2)
  1140. def test_oneshot_cache(self):
  1141. # Make sure oneshot() cache is nonglobal. Instead it's
  1142. # supposed to be bound to the Process instance, see:
  1143. # https://github.com/giampaolo/psutil/issues/1373
  1144. p1, p2 = self.spawn_children_pair()
  1145. p1_ppid = p1.ppid()
  1146. p2_ppid = p2.ppid()
  1147. self.assertNotEqual(p1_ppid, p2_ppid)
  1148. with p1.oneshot():
  1149. self.assertEqual(p1.ppid(), p1_ppid)
  1150. self.assertEqual(p2.ppid(), p2_ppid)
  1151. with p2.oneshot():
  1152. self.assertEqual(p1.ppid(), p1_ppid)
  1153. self.assertEqual(p2.ppid(), p2_ppid)
  1154. def test_halfway_terminated_process(self):
  1155. # Test that NoSuchProcess exception gets raised in case the
  1156. # process dies after we create the Process object.
  1157. # Example:
  1158. # >>> proc = Process(1234)
  1159. # >>> time.sleep(2) # time-consuming task, process dies in meantime
  1160. # >>> proc.name()
  1161. # Refers to Issue #15
  1162. def assert_raises_nsp(fun, fun_name):
  1163. try:
  1164. ret = fun()
  1165. except psutil.ZombieProcess: # differentiate from NSP
  1166. raise
  1167. except psutil.NoSuchProcess:
  1168. pass
  1169. except psutil.AccessDenied:
  1170. if OPENBSD and fun_name in ('threads', 'num_threads'):
  1171. return
  1172. raise
  1173. else:
  1174. # NtQuerySystemInformation succeeds even if process is gone.
  1175. if WINDOWS and fun_name in ('exe', 'name'):
  1176. return
  1177. raise self.fail("%r didn't raise NSP and returned %r "
  1178. "instead" % (fun, ret))
  1179. p = self.spawn_psproc()
  1180. p.terminate()
  1181. p.wait()
  1182. if WINDOWS: # XXX
  1183. call_until(psutil.pids, "%s not in ret" % p.pid)
  1184. self.assertProcessGone(p)
  1185. ns = process_namespace(p)
  1186. for fun, name in ns.iter(ns.all):
  1187. assert_raises_nsp(fun, name)
  1188. @unittest.skipIf(not POSIX, 'POSIX only')
  1189. def test_zombie_process(self):
  1190. parent, zombie = self.spawn_zombie()
  1191. self.assertProcessZombie(zombie)
  1192. @unittest.skipIf(not POSIX, 'POSIX only')
  1193. def test_zombie_process_is_running_w_exc(self):
  1194. # Emulate a case where internally is_running() raises
  1195. # ZombieProcess.
  1196. p = psutil.Process()
  1197. with mock.patch("psutil.Process",
  1198. side_effect=psutil.ZombieProcess(0)) as m:
  1199. assert p.is_running()
  1200. assert m.called
  1201. @unittest.skipIf(not POSIX, 'POSIX only')
  1202. def test_zombie_process_status_w_exc(self):
  1203. # Emulate a case where internally status() raises
  1204. # ZombieProcess.
  1205. p = psutil.Process()
  1206. with mock.patch("psutil._psplatform.Process.status",
  1207. side_effect=psutil.ZombieProcess(0)) as m:
  1208. self.assertEqual(p.status(), psutil.STATUS_ZOMBIE)
  1209. assert m.called
  1210. def test_reused_pid(self):
  1211. # Emulate a case where PID has been reused by another process.
  1212. subp = self.spawn_testproc()
  1213. p = psutil.Process(subp.pid)
  1214. p._ident = (p.pid, p.create_time() + 100)
  1215. assert not p.is_running()
  1216. assert p != psutil.Process(subp.pid)
  1217. msg = "process no longer exists and its PID has been reused"
  1218. ns = process_namespace(p)
  1219. for fun, name in ns.iter(ns.setters + ns.killers, clear_cache=False):
  1220. with self.subTest(name=name):
  1221. self.assertRaisesRegex(psutil.NoSuchProcess, msg, fun)
  1222. self.assertRaisesRegex(psutil.NoSuchProcess, msg, p.ppid)
  1223. self.assertRaisesRegex(psutil.NoSuchProcess, msg, p.parent)
  1224. self.assertRaisesRegex(psutil.NoSuchProcess, msg, p.parents)
  1225. self.assertRaisesRegex(psutil.NoSuchProcess, msg, p.children)
  1226. def test_pid_0(self):
  1227. # Process(0) is supposed to work on all platforms except Linux
  1228. if 0 not in psutil.pids():
  1229. self.assertRaises(psutil.NoSuchProcess, psutil.Process, 0)
  1230. # These 2 are a contradiction, but "ps" says PID 1's parent
  1231. # is PID 0.
  1232. assert not psutil.pid_exists(0)
  1233. self.assertEqual(psutil.Process(1).ppid(), 0)
  1234. return
  1235. p = psutil.Process(0)
  1236. exc = psutil.AccessDenied if WINDOWS else ValueError
  1237. self.assertRaises(exc, p.wait)
  1238. self.assertRaises(exc, p.terminate)
  1239. self.assertRaises(exc, p.suspend)
  1240. self.assertRaises(exc, p.resume)
  1241. self.assertRaises(exc, p.kill)
  1242. self.assertRaises(exc, p.send_signal, signal.SIGTERM)
  1243. # test all methods
  1244. ns = process_namespace(p)
  1245. for fun, name in ns.iter(ns.getters + ns.setters):
  1246. try:
  1247. ret = fun()
  1248. except psutil.AccessDenied:
  1249. pass
  1250. else:
  1251. if name in ("uids", "gids"):
  1252. self.assertEqual(ret.real, 0)
  1253. elif name == "username":
  1254. user = 'NT AUTHORITY\\SYSTEM' if WINDOWS else 'root'
  1255. self.assertEqual(p.username(), user)
  1256. elif name == "name":
  1257. assert name, name
  1258. if not OPENBSD:
  1259. self.assertIn(0, psutil.pids())
  1260. assert psutil.pid_exists(0)
  1261. @unittest.skipIf(not HAS_ENVIRON, "not supported")
  1262. def test_environ(self):
  1263. def clean_dict(d):
  1264. # Most of these are problematic on Travis.
  1265. d.pop("PLAT", None)
  1266. d.pop("HOME", None)
  1267. if MACOS:
  1268. d.pop("__CF_USER_TEXT_ENCODING", None)
  1269. d.pop("VERSIONER_PYTHON_PREFER_32_BIT", None)
  1270. d.pop("VERSIONER_PYTHON_VERSION", None)
  1271. return dict(
  1272. [(k.replace("\r", "").replace("\n", ""),
  1273. v.replace("\r", "").replace("\n", ""))
  1274. for k, v in d.items()])
  1275. self.maxDiff = None
  1276. p = psutil.Process()
  1277. d1 = clean_dict(p.environ())
  1278. d2 = clean_dict(os.environ.copy())
  1279. if not OSX and GITHUB_ACTIONS:
  1280. self.assertEqual(d1, d2)
  1281. @unittest.skipIf(not HAS_ENVIRON, "not supported")
  1282. @unittest.skipIf(not POSIX, "POSIX only")
  1283. @unittest.skipIf(
  1284. MACOS_11PLUS,
  1285. "macOS 11+ can't get another process environment, issue #2084"
  1286. )
  1287. def test_weird_environ(self):
  1288. # environment variables can contain values without an equals sign
  1289. code = textwrap.dedent("""
  1290. #include <unistd.h>
  1291. #include <fcntl.h>
  1292. char * const argv[] = {"cat", 0};
  1293. char * const envp[] = {"A=1", "X", "C=3", 0};
  1294. int main(void) {
  1295. // Close stderr on exec so parent can wait for the
  1296. // execve to finish.
  1297. if (fcntl(2, F_SETFD, FD_CLOEXEC) != 0)
  1298. return 0;
  1299. return execve("/bin/cat", argv, envp);
  1300. }
  1301. """)
  1302. path = self.get_testfn()
  1303. create_exe(path, c_code=code)
  1304. sproc = self.spawn_testproc(
  1305. [path], stdin=subprocess.PIPE, stderr=subprocess.PIPE)
  1306. p = psutil.Process(sproc.pid)
  1307. wait_for_pid(p.pid)
  1308. assert p.is_running()
  1309. # Wait for process to exec or exit.
  1310. self.assertEqual(sproc.stderr.read(), b"")
  1311. if MACOS and CI_TESTING:
  1312. try:
  1313. env = p.environ()
  1314. except psutil.AccessDenied:
  1315. # XXX: fails sometimes with:
  1316. # PermissionError from 'sysctl(KERN_PROCARGS2) -> EIO'
  1317. return
  1318. else:
  1319. env = p.environ()
  1320. self.assertEqual(env, {"A": "1", "C": "3"})
  1321. sproc.communicate()
  1322. self.assertEqual(sproc.returncode, 0)
  1323. # ===================================================================
  1324. # --- Limited user tests
  1325. # ===================================================================
  1326. if POSIX and os.getuid() == 0:
  1327. class LimitedUserTestCase(TestProcess):
  1328. """Repeat the previous tests by using a limited user.
  1329. Executed only on UNIX and only if the user who run the test script
  1330. is root.
  1331. """
  1332. # the uid/gid the test suite runs under
  1333. if hasattr(os, 'getuid'):
  1334. PROCESS_UID = os.getuid()
  1335. PROCESS_GID = os.getgid()
  1336. def __init__(self, *args, **kwargs):
  1337. super().__init__(*args, **kwargs)
  1338. # re-define all existent test methods in order to
  1339. # ignore AccessDenied exceptions
  1340. for attr in [x for x in dir(self) if x.startswith('test')]:
  1341. meth = getattr(self, attr)
  1342. def test_(self):
  1343. try:
  1344. meth() # noqa
  1345. except psutil.AccessDenied:
  1346. pass
  1347. setattr(self, attr, types.MethodType(test_, self))
  1348. def setUp(self):
  1349. super().setUp()
  1350. os.setegid(1000)
  1351. os.seteuid(1000)
  1352. def tearDown(self):
  1353. os.setegid(self.PROCESS_UID)
  1354. os.seteuid(self.PROCESS_GID)
  1355. super().tearDown()
  1356. def test_nice(self):
  1357. try:
  1358. psutil.Process().nice(-1)
  1359. except psutil.AccessDenied:
  1360. pass
  1361. else:
  1362. raise self.fail("exception not raised")
  1363. @unittest.skipIf(1, "causes problem as root")
  1364. def test_zombie_process(self):
  1365. pass
  1366. # ===================================================================
  1367. # --- psutil.Popen tests
  1368. # ===================================================================
  1369. class TestPopen(PsutilTestCase):
  1370. """Tests for psutil.Popen class."""
  1371. @classmethod
  1372. def tearDownClass(cls):
  1373. reap_children()
  1374. def test_misc(self):
  1375. # XXX this test causes a ResourceWarning on Python 3 because
  1376. # psutil.__subproc instance doesn't get properly freed.
  1377. # Not sure what to do though.
  1378. cmd = [PYTHON_EXE, "-c", "import time; time.sleep(60);"]
  1379. with psutil.Popen(cmd, stdout=subprocess.PIPE,
  1380. stderr=subprocess.PIPE, env=PYTHON_EXE_ENV) as proc:
  1381. proc.name()
  1382. proc.cpu_times()
  1383. proc.stdin # noqa
  1384. self.assertTrue(dir(proc))
  1385. self.assertRaises(AttributeError, getattr, proc, 'foo')
  1386. proc.terminate()
  1387. if POSIX:
  1388. self.assertEqual(proc.wait(5), -signal.SIGTERM)
  1389. else:
  1390. self.assertEqual(proc.wait(5), signal.SIGTERM)
  1391. def test_ctx_manager(self):
  1392. with psutil.Popen([PYTHON_EXE, "-V"],
  1393. stdout=subprocess.PIPE,
  1394. stderr=subprocess.PIPE,
  1395. stdin=subprocess.PIPE, env=PYTHON_EXE_ENV) as proc:
  1396. proc.communicate()
  1397. assert proc.stdout.closed
  1398. assert proc.stderr.closed
  1399. assert proc.stdin.closed
  1400. self.assertEqual(proc.returncode, 0)
  1401. def test_kill_terminate(self):
  1402. # subprocess.Popen()'s terminate(), kill() and send_signal() do
  1403. # not raise exception after the process is gone. psutil.Popen
  1404. # diverges from that.
  1405. cmd = [PYTHON_EXE, "-c", "import time; time.sleep(60);"]
  1406. with psutil.Popen(cmd, stdout=subprocess.PIPE,
  1407. stderr=subprocess.PIPE, env=PYTHON_EXE_ENV) as proc:
  1408. proc.terminate()
  1409. proc.wait()
  1410. self.assertRaises(psutil.NoSuchProcess, proc.terminate)
  1411. self.assertRaises(psutil.NoSuchProcess, proc.kill)
  1412. self.assertRaises(psutil.NoSuchProcess, proc.send_signal,
  1413. signal.SIGTERM)
  1414. if WINDOWS:
  1415. self.assertRaises(psutil.NoSuchProcess, proc.send_signal,
  1416. signal.CTRL_C_EVENT)
  1417. self.assertRaises(psutil.NoSuchProcess, proc.send_signal,
  1418. signal.CTRL_BREAK_EVENT)
  1419. if __name__ == '__main__':
  1420. from psutil.tests.runner import run_from_name
  1421. run_from_name(__file__)