test_bsd.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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. # TODO: (FreeBSD) add test for comparing connections with 'sockstat' cmd.
  6. """Tests specific to all BSD platforms."""
  7. import datetime
  8. import os
  9. import re
  10. import shutil
  11. import time
  12. import psutil
  13. from psutil import BSD
  14. from psutil import FREEBSD
  15. from psutil import NETBSD
  16. from psutil import OPENBSD
  17. from psutil.tests import HAS_BATTERY
  18. from psutil.tests import TOLERANCE_SYS_MEM
  19. from psutil.tests import PsutilTestCase
  20. from psutil.tests import pytest
  21. from psutil.tests import retry_on_failure
  22. from psutil.tests import sh
  23. from psutil.tests import spawn_testproc
  24. from psutil.tests import terminate
  25. if BSD:
  26. from psutil._psutil_posix import getpagesize
  27. PAGESIZE = getpagesize()
  28. # muse requires root privileges
  29. MUSE_AVAILABLE = os.getuid() == 0 and shutil.which("muse")
  30. else:
  31. PAGESIZE = None
  32. MUSE_AVAILABLE = False
  33. def sysctl(cmdline):
  34. """Expects a sysctl command with an argument and parse the result
  35. returning only the value of interest.
  36. """
  37. result = sh("sysctl " + cmdline)
  38. if FREEBSD:
  39. result = result[result.find(": ") + 2 :]
  40. elif OPENBSD or NETBSD:
  41. result = result[result.find("=") + 1 :]
  42. try:
  43. return int(result)
  44. except ValueError:
  45. return result
  46. def muse(field):
  47. """Thin wrapper around 'muse' cmdline utility."""
  48. out = sh('muse')
  49. for line in out.split('\n'):
  50. if line.startswith(field):
  51. break
  52. else:
  53. raise ValueError("line not found")
  54. return int(line.split()[1])
  55. # =====================================================================
  56. # --- All BSD*
  57. # =====================================================================
  58. @pytest.mark.skipif(not BSD, reason="BSD only")
  59. class BSDTestCase(PsutilTestCase):
  60. """Generic tests common to all BSD variants."""
  61. @classmethod
  62. def setUpClass(cls):
  63. cls.pid = spawn_testproc().pid
  64. @classmethod
  65. def tearDownClass(cls):
  66. terminate(cls.pid)
  67. @pytest.mark.skipif(NETBSD, reason="-o lstart doesn't work on NETBSD")
  68. def test_process_create_time(self):
  69. output = sh(f"ps -o lstart -p {self.pid}")
  70. start_ps = output.replace('STARTED', '').strip()
  71. start_psutil = psutil.Process(self.pid).create_time()
  72. start_psutil = time.strftime(
  73. "%a %b %e %H:%M:%S %Y", time.localtime(start_psutil)
  74. )
  75. assert start_ps == start_psutil
  76. def test_disks(self):
  77. # test psutil.disk_usage() and psutil.disk_partitions()
  78. # against "df -a"
  79. def df(path):
  80. out = sh(f'df -k "{path}"').strip()
  81. lines = out.split('\n')
  82. lines.pop(0)
  83. line = lines.pop(0)
  84. dev, total, used, free = line.split()[:4]
  85. if dev == 'none':
  86. dev = ''
  87. total = int(total) * 1024
  88. used = int(used) * 1024
  89. free = int(free) * 1024
  90. return dev, total, used, free
  91. for part in psutil.disk_partitions(all=False):
  92. usage = psutil.disk_usage(part.mountpoint)
  93. dev, total, used, free = df(part.mountpoint)
  94. assert part.device == dev
  95. assert usage.total == total
  96. # 10 MB tolerance
  97. if abs(usage.free - free) > 10 * 1024 * 1024:
  98. raise self.fail(f"psutil={usage.free}, df={free}")
  99. if abs(usage.used - used) > 10 * 1024 * 1024:
  100. raise self.fail(f"psutil={usage.used}, df={used}")
  101. @pytest.mark.skipif(
  102. not shutil.which("sysctl"), reason="sysctl cmd not available"
  103. )
  104. def test_cpu_count_logical(self):
  105. syst = sysctl("hw.ncpu")
  106. assert psutil.cpu_count(logical=True) == syst
  107. @pytest.mark.skipif(
  108. not shutil.which("sysctl"), reason="sysctl cmd not available"
  109. )
  110. @pytest.mark.skipif(
  111. NETBSD, reason="skipped on NETBSD" # we check /proc/meminfo
  112. )
  113. def test_virtual_memory_total(self):
  114. num = sysctl('hw.physmem')
  115. assert num == psutil.virtual_memory().total
  116. @pytest.mark.skipif(
  117. not shutil.which("ifconfig"), reason="ifconfig cmd not available"
  118. )
  119. def test_net_if_stats(self):
  120. for name, stats in psutil.net_if_stats().items():
  121. try:
  122. out = sh(f"ifconfig {name}")
  123. except RuntimeError:
  124. pass
  125. else:
  126. assert stats.isup == ('RUNNING' in out)
  127. if "mtu" in out:
  128. assert stats.mtu == int(re.findall(r'mtu (\d+)', out)[0])
  129. # =====================================================================
  130. # --- FreeBSD
  131. # =====================================================================
  132. @pytest.mark.skipif(not FREEBSD, reason="FREEBSD only")
  133. class FreeBSDPsutilTestCase(PsutilTestCase):
  134. @classmethod
  135. def setUpClass(cls):
  136. cls.pid = spawn_testproc().pid
  137. @classmethod
  138. def tearDownClass(cls):
  139. terminate(cls.pid)
  140. @retry_on_failure()
  141. def test_memory_maps(self):
  142. out = sh(f"procstat -v {self.pid}")
  143. maps = psutil.Process(self.pid).memory_maps(grouped=False)
  144. lines = out.split('\n')[1:]
  145. while lines:
  146. line = lines.pop()
  147. fields = line.split()
  148. _, start, stop, _perms, res = fields[:5]
  149. map = maps.pop()
  150. assert f"{start}-{stop}" == map.addr
  151. assert int(res) == map.rss
  152. if not map.path.startswith('['):
  153. assert fields[10] == map.path
  154. def test_exe(self):
  155. out = sh(f"procstat -b {self.pid}")
  156. assert psutil.Process(self.pid).exe() == out.split('\n')[1].split()[-1]
  157. def test_cmdline(self):
  158. out = sh(f"procstat -c {self.pid}")
  159. assert ' '.join(psutil.Process(self.pid).cmdline()) == ' '.join(
  160. out.split('\n')[1].split()[2:]
  161. )
  162. def test_uids_gids(self):
  163. out = sh(f"procstat -s {self.pid}")
  164. euid, ruid, suid, egid, rgid, sgid = out.split('\n')[1].split()[2:8]
  165. p = psutil.Process(self.pid)
  166. uids = p.uids()
  167. gids = p.gids()
  168. assert uids.real == int(ruid)
  169. assert uids.effective == int(euid)
  170. assert uids.saved == int(suid)
  171. assert gids.real == int(rgid)
  172. assert gids.effective == int(egid)
  173. assert gids.saved == int(sgid)
  174. @retry_on_failure()
  175. def test_ctx_switches(self):
  176. tested = []
  177. out = sh(f"procstat -r {self.pid}")
  178. p = psutil.Process(self.pid)
  179. for line in out.split('\n'):
  180. line = line.lower().strip()
  181. if ' voluntary context' in line:
  182. pstat_value = int(line.split()[-1])
  183. psutil_value = p.num_ctx_switches().voluntary
  184. assert pstat_value == psutil_value
  185. tested.append(None)
  186. elif ' involuntary context' in line:
  187. pstat_value = int(line.split()[-1])
  188. psutil_value = p.num_ctx_switches().involuntary
  189. assert pstat_value == psutil_value
  190. tested.append(None)
  191. if len(tested) != 2:
  192. raise RuntimeError("couldn't find lines match in procstat out")
  193. @retry_on_failure()
  194. def test_cpu_times(self):
  195. tested = []
  196. out = sh(f"procstat -r {self.pid}")
  197. p = psutil.Process(self.pid)
  198. for line in out.split('\n'):
  199. line = line.lower().strip()
  200. if 'user time' in line:
  201. pstat_value = float('0.' + line.split()[-1].split('.')[-1])
  202. psutil_value = p.cpu_times().user
  203. assert pstat_value == psutil_value
  204. tested.append(None)
  205. elif 'system time' in line:
  206. pstat_value = float('0.' + line.split()[-1].split('.')[-1])
  207. psutil_value = p.cpu_times().system
  208. assert pstat_value == psutil_value
  209. tested.append(None)
  210. if len(tested) != 2:
  211. raise RuntimeError("couldn't find lines match in procstat out")
  212. @pytest.mark.skipif(not FREEBSD, reason="FREEBSD only")
  213. class FreeBSDSystemTestCase(PsutilTestCase):
  214. @staticmethod
  215. def parse_swapinfo():
  216. # the last line is always the total
  217. output = sh("swapinfo -k").splitlines()[-1]
  218. parts = re.split(r'\s+', output)
  219. if not parts:
  220. raise ValueError(f"Can't parse swapinfo: {output}")
  221. # the size is in 1k units, so multiply by 1024
  222. total, used, free = (int(p) * 1024 for p in parts[1:4])
  223. return total, used, free
  224. def test_cpu_frequency_against_sysctl(self):
  225. # Currently only cpu 0 is frequency is supported in FreeBSD
  226. # All other cores use the same frequency.
  227. sensor = "dev.cpu.0.freq"
  228. try:
  229. sysctl_result = int(sysctl(sensor))
  230. except RuntimeError:
  231. raise pytest.skip("frequencies not supported by kernel")
  232. assert psutil.cpu_freq().current == sysctl_result
  233. sensor = "dev.cpu.0.freq_levels"
  234. sysctl_result = sysctl(sensor)
  235. # sysctl returns a string of the format:
  236. # <freq_level_1>/<voltage_level_1> <freq_level_2>/<voltage_level_2>...
  237. # Ordered highest available to lowest available.
  238. max_freq = int(sysctl_result.split()[0].split("/")[0])
  239. min_freq = int(sysctl_result.split()[-1].split("/")[0])
  240. assert psutil.cpu_freq().max == max_freq
  241. assert psutil.cpu_freq().min == min_freq
  242. # --- virtual_memory(); tests against sysctl
  243. @retry_on_failure()
  244. def test_vmem_active(self):
  245. syst = sysctl("vm.stats.vm.v_active_count") * PAGESIZE
  246. assert abs(psutil.virtual_memory().active - syst) < TOLERANCE_SYS_MEM
  247. @retry_on_failure()
  248. def test_vmem_inactive(self):
  249. syst = sysctl("vm.stats.vm.v_inactive_count") * PAGESIZE
  250. assert abs(psutil.virtual_memory().inactive - syst) < TOLERANCE_SYS_MEM
  251. @retry_on_failure()
  252. def test_vmem_wired(self):
  253. syst = sysctl("vm.stats.vm.v_wire_count") * PAGESIZE
  254. assert abs(psutil.virtual_memory().wired - syst) < TOLERANCE_SYS_MEM
  255. @retry_on_failure()
  256. def test_vmem_cached(self):
  257. syst = sysctl("vm.stats.vm.v_cache_count") * PAGESIZE
  258. assert abs(psutil.virtual_memory().cached - syst) < TOLERANCE_SYS_MEM
  259. @retry_on_failure()
  260. def test_vmem_free(self):
  261. syst = sysctl("vm.stats.vm.v_free_count") * PAGESIZE
  262. assert abs(psutil.virtual_memory().free - syst) < TOLERANCE_SYS_MEM
  263. @retry_on_failure()
  264. def test_vmem_buffers(self):
  265. syst = sysctl("vfs.bufspace")
  266. assert abs(psutil.virtual_memory().buffers - syst) < TOLERANCE_SYS_MEM
  267. # --- virtual_memory(); tests against muse
  268. @pytest.mark.skipif(not MUSE_AVAILABLE, reason="muse not installed")
  269. def test_muse_vmem_total(self):
  270. num = muse('Total')
  271. assert psutil.virtual_memory().total == num
  272. @pytest.mark.skipif(not MUSE_AVAILABLE, reason="muse not installed")
  273. @retry_on_failure()
  274. def test_muse_vmem_active(self):
  275. num = muse('Active')
  276. assert abs(psutil.virtual_memory().active - num) < TOLERANCE_SYS_MEM
  277. @pytest.mark.skipif(not MUSE_AVAILABLE, reason="muse not installed")
  278. @retry_on_failure()
  279. def test_muse_vmem_inactive(self):
  280. num = muse('Inactive')
  281. assert abs(psutil.virtual_memory().inactive - num) < TOLERANCE_SYS_MEM
  282. @pytest.mark.skipif(not MUSE_AVAILABLE, reason="muse not installed")
  283. @retry_on_failure()
  284. def test_muse_vmem_wired(self):
  285. num = muse('Wired')
  286. assert abs(psutil.virtual_memory().wired - num) < TOLERANCE_SYS_MEM
  287. @pytest.mark.skipif(not MUSE_AVAILABLE, reason="muse not installed")
  288. @retry_on_failure()
  289. def test_muse_vmem_cached(self):
  290. num = muse('Cache')
  291. assert abs(psutil.virtual_memory().cached - num) < TOLERANCE_SYS_MEM
  292. @pytest.mark.skipif(not MUSE_AVAILABLE, reason="muse not installed")
  293. @retry_on_failure()
  294. def test_muse_vmem_free(self):
  295. num = muse('Free')
  296. assert abs(psutil.virtual_memory().free - num) < TOLERANCE_SYS_MEM
  297. @pytest.mark.skipif(not MUSE_AVAILABLE, reason="muse not installed")
  298. @retry_on_failure()
  299. def test_muse_vmem_buffers(self):
  300. num = muse('Buffer')
  301. assert abs(psutil.virtual_memory().buffers - num) < TOLERANCE_SYS_MEM
  302. def test_cpu_stats_ctx_switches(self):
  303. assert (
  304. abs(
  305. psutil.cpu_stats().ctx_switches
  306. - sysctl('vm.stats.sys.v_swtch')
  307. )
  308. < 1000
  309. )
  310. def test_cpu_stats_interrupts(self):
  311. assert (
  312. abs(psutil.cpu_stats().interrupts - sysctl('vm.stats.sys.v_intr'))
  313. < 1000
  314. )
  315. def test_cpu_stats_soft_interrupts(self):
  316. assert (
  317. abs(
  318. psutil.cpu_stats().soft_interrupts
  319. - sysctl('vm.stats.sys.v_soft')
  320. )
  321. < 1000
  322. )
  323. @retry_on_failure()
  324. def test_cpu_stats_syscalls(self):
  325. # pretty high tolerance but it looks like it's OK.
  326. assert (
  327. abs(psutil.cpu_stats().syscalls - sysctl('vm.stats.sys.v_syscall'))
  328. < 200000
  329. )
  330. # def test_cpu_stats_traps(self):
  331. # self.assertAlmostEqual(psutil.cpu_stats().traps,
  332. # sysctl('vm.stats.sys.v_trap'), delta=1000)
  333. # --- swap memory
  334. def test_swapmem_free(self):
  335. _total, _used, free = self.parse_swapinfo()
  336. assert abs(psutil.swap_memory().free - free) < TOLERANCE_SYS_MEM
  337. def test_swapmem_used(self):
  338. _total, used, _free = self.parse_swapinfo()
  339. assert abs(psutil.swap_memory().used - used) < TOLERANCE_SYS_MEM
  340. def test_swapmem_total(self):
  341. total, _used, _free = self.parse_swapinfo()
  342. assert abs(psutil.swap_memory().total - total) < TOLERANCE_SYS_MEM
  343. # --- others
  344. def test_boot_time(self):
  345. s = sysctl('sysctl kern.boottime')
  346. s = s[s.find(" sec = ") + 7 :]
  347. s = s[: s.find(',')]
  348. btime = int(s)
  349. assert btime == psutil.boot_time()
  350. # --- sensors_battery
  351. @pytest.mark.skipif(not HAS_BATTERY, reason="no battery")
  352. def test_sensors_battery(self):
  353. def secs2hours(secs):
  354. m, _s = divmod(secs, 60)
  355. h, m = divmod(m, 60)
  356. return f"{int(h)}:{int(m):02}"
  357. out = sh("acpiconf -i 0")
  358. fields = {x.split('\t')[0]: x.split('\t')[-1] for x in out.split("\n")}
  359. metrics = psutil.sensors_battery()
  360. percent = int(fields['Remaining capacity:'].replace('%', ''))
  361. remaining_time = fields['Remaining time:']
  362. assert metrics.percent == percent
  363. if remaining_time == 'unknown':
  364. assert metrics.secsleft == psutil.POWER_TIME_UNLIMITED
  365. else:
  366. assert secs2hours(metrics.secsleft) == remaining_time
  367. @pytest.mark.skipif(not HAS_BATTERY, reason="no battery")
  368. def test_sensors_battery_against_sysctl(self):
  369. assert psutil.sensors_battery().percent == sysctl(
  370. "hw.acpi.battery.life"
  371. )
  372. assert psutil.sensors_battery().power_plugged == (
  373. sysctl("hw.acpi.acline") == 1
  374. )
  375. secsleft = psutil.sensors_battery().secsleft
  376. if secsleft < 0:
  377. assert sysctl("hw.acpi.battery.time") == -1
  378. else:
  379. assert secsleft == sysctl("hw.acpi.battery.time") * 60
  380. @pytest.mark.skipif(HAS_BATTERY, reason="has battery")
  381. def test_sensors_battery_no_battery(self):
  382. # If no battery is present one of these calls is supposed
  383. # to fail, see:
  384. # https://github.com/giampaolo/psutil/issues/1074
  385. with pytest.raises(RuntimeError):
  386. sysctl("hw.acpi.battery.life")
  387. sysctl("hw.acpi.battery.time")
  388. sysctl("hw.acpi.acline")
  389. assert psutil.sensors_battery() is None
  390. # --- sensors_temperatures
  391. def test_sensors_temperatures_against_sysctl(self):
  392. num_cpus = psutil.cpu_count(True)
  393. for cpu in range(num_cpus):
  394. sensor = f"dev.cpu.{cpu}.temperature"
  395. # sysctl returns a string in the format 46.0C
  396. try:
  397. sysctl_result = int(float(sysctl(sensor)[:-1]))
  398. except RuntimeError:
  399. raise pytest.skip("temperatures not supported by kernel")
  400. assert (
  401. abs(
  402. psutil.sensors_temperatures()["coretemp"][cpu].current
  403. - sysctl_result
  404. )
  405. < 10
  406. )
  407. sensor = f"dev.cpu.{cpu}.coretemp.tjmax"
  408. sysctl_result = int(float(sysctl(sensor)[:-1]))
  409. assert (
  410. psutil.sensors_temperatures()["coretemp"][cpu].high
  411. == sysctl_result
  412. )
  413. # =====================================================================
  414. # --- OpenBSD
  415. # =====================================================================
  416. @pytest.mark.skipif(not OPENBSD, reason="OPENBSD only")
  417. class OpenBSDTestCase(PsutilTestCase):
  418. def test_boot_time(self):
  419. s = sysctl('kern.boottime')
  420. sys_bt = datetime.datetime.strptime(s, "%a %b %d %H:%M:%S %Y")
  421. psutil_bt = datetime.datetime.fromtimestamp(psutil.boot_time())
  422. assert sys_bt == psutil_bt
  423. # =====================================================================
  424. # --- NetBSD
  425. # =====================================================================
  426. @pytest.mark.skipif(not NETBSD, reason="NETBSD only")
  427. class NetBSDTestCase(PsutilTestCase):
  428. @staticmethod
  429. def parse_meminfo(look_for):
  430. with open('/proc/meminfo') as f:
  431. for line in f:
  432. if line.startswith(look_for):
  433. return int(line.split()[1]) * 1024
  434. raise ValueError(f"can't find {look_for}")
  435. # --- virtual mem
  436. def test_vmem_total(self):
  437. assert psutil.virtual_memory().total == self.parse_meminfo("MemTotal:")
  438. def test_vmem_free(self):
  439. assert (
  440. abs(psutil.virtual_memory().free - self.parse_meminfo("MemFree:"))
  441. < TOLERANCE_SYS_MEM
  442. )
  443. def test_vmem_buffers(self):
  444. assert (
  445. abs(
  446. psutil.virtual_memory().buffers
  447. - self.parse_meminfo("Buffers:")
  448. )
  449. < TOLERANCE_SYS_MEM
  450. )
  451. def test_vmem_shared(self):
  452. assert (
  453. abs(
  454. psutil.virtual_memory().shared
  455. - self.parse_meminfo("MemShared:")
  456. )
  457. < TOLERANCE_SYS_MEM
  458. )
  459. def test_vmem_cached(self):
  460. assert (
  461. abs(psutil.virtual_memory().cached - self.parse_meminfo("Cached:"))
  462. < TOLERANCE_SYS_MEM
  463. )
  464. # --- swap mem
  465. def test_swapmem_total(self):
  466. assert (
  467. abs(psutil.swap_memory().total - self.parse_meminfo("SwapTotal:"))
  468. < TOLERANCE_SYS_MEM
  469. )
  470. def test_swapmem_free(self):
  471. assert (
  472. abs(psutil.swap_memory().free - self.parse_meminfo("SwapFree:"))
  473. < TOLERANCE_SYS_MEM
  474. )
  475. def test_swapmem_used(self):
  476. smem = psutil.swap_memory()
  477. assert smem.used == smem.total - smem.free
  478. # --- others
  479. def test_cpu_stats_interrupts(self):
  480. with open('/proc/stat', 'rb') as f:
  481. for line in f:
  482. if line.startswith(b'intr'):
  483. interrupts = int(line.split()[1])
  484. break
  485. else:
  486. raise ValueError("couldn't find line")
  487. assert abs(psutil.cpu_stats().interrupts - interrupts) < 1000
  488. def test_cpu_stats_ctx_switches(self):
  489. with open('/proc/stat', 'rb') as f:
  490. for line in f:
  491. if line.startswith(b'ctxt'):
  492. ctx_switches = int(line.split()[1])
  493. break
  494. else:
  495. raise ValueError("couldn't find line")
  496. assert abs(psutil.cpu_stats().ctx_switches - ctx_switches) < 1000