Skip to content

Commit

Permalink
#1255 adjust style + give CREDITS to @href
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Mar 29, 2018
1 parent a4c07e9 commit 904252e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 23 deletions.
4 changes: 4 additions & 0 deletions CREDITS
Original file line number Diff line number Diff line change
Expand Up @@ -531,3 +531,7 @@ I: 1193, 1194
N: Maxime Mouial
W: https://github.com/hush-hush
I: 1239

N: Denis Krienbühl
W: https://github.com/href
I: 1260
2 changes: 2 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ XXXX-XX-XX
- 1240_: [Windows] cpu_times() float loses accuracy in a long running system.
(patch by stswandering)
- 1245_: [Linux] sensors_temperatures() may fail with IOError "no such file".
- 1255_: [FreeBSD] swap_memory() stats were erroneously represented in KB.
(patch by Denis Krienbühl)

5.4.3
=====
Expand Down
15 changes: 9 additions & 6 deletions psutil/arch/freebsd/specific.c
Original file line number Diff line number Diff line change
Expand Up @@ -518,12 +518,15 @@ psutil_swap_mem(PyObject *self, PyObject *args) {

int pagesize = getpagesize();

return Py_BuildValue("(KKKII)",
(unsigned long long) kvmsw[0].ksw_total * pagesize, // total
(unsigned long long) kvmsw[0].ksw_used * pagesize, // used
(unsigned long long) kvmsw[0].ksw_total * pagesize - kvmsw[0].ksw_used * pagesize, // free
swapin + swapout, // swap in
nodein + nodeout); // swap out
return Py_BuildValue(
"(KKKII)",
(unsigned long long)kvmsw[0].ksw_total * pagesize, // total
(unsigned long long)kvmsw[0].ksw_used * pagesize, // used
(unsigned long long)kvmsw[0].ksw_total * pagesize - // free
kvmsw[0].ksw_used * pagesize,
swapin + swapout, // swap in
nodein + nodeout // swap out
);

error:
return PyErr_SetFromErrno(PyExc_OSError);
Expand Down
30 changes: 13 additions & 17 deletions psutil/tests/test_bsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,19 @@ def setUpClass(cls):
def tearDownClass(cls):
reap_children()

@staticmethod
def parse_swapinfo():
# the last line is always the total
output = sh("swapinfo -k").splitlines()[-1]
parts = re.split(r'\s+', output)

if not parts:
raise ValueError("Can't parse swapinfo: %s" % output)

# the size is in 1k units, so multiply by 1024
total, used, free = (int(p) * 1024 for p in parts[1:4])
return total, used, free

@retry_before_failing()
def test_proc_memory_maps(self):
out = sh('procstat -v %s' % self.pid)
Expand Down Expand Up @@ -345,36 +358,19 @@ def test_cpu_stats_syscalls(self):
# sysctl('vm.stats.sys.v_trap'), delta=1000)

# --- swap memory
@staticmethod
def parse_swapinfo():

# the last line is always the total
output = sh("swapinfo -k").splitlines()[-1]
parts = re.split(r'\s+', output)

if not parts:
raise ValueError("Can't parse swapinfo: %s" % output)

# the size is in 1k units, so multiply by 1024
total, used, free = (int(p) * 1024 for p in parts[1:4])

return total, used, free

def test_swapmem_free(self):
total, used, free = self.parse_swapinfo()

self.assertAlmostEqual(
psutil.swap_memory().free, free, delta=MEMORY_TOLERANCE)

def test_swapmem_used(self):
total, used, free = self.parse_swapinfo()

self.assertAlmostEqual(
psutil.swap_memory().used, used, delta=MEMORY_TOLERANCE)

def test_swapmem_total(self):
total, used, free = self.parse_swapinfo()

self.assertAlmostEqual(
psutil.swap_memory().total, total, delta=MEMORY_TOLERANCE)

Expand Down

0 comments on commit 904252e

Please sign in to comment.