test_bsd.py 20 KB

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