diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2016-09-23 14:21:47 +0200 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2016-09-23 14:21:47 +0200 |
commit | 9c269edb5132fffacdd9af493c95b199ee02f551 (patch) | |
tree | 075591bfa6f6d16cbf6e54c9ad64f3873e9ab649 | |
parent | 7713f85074c872ec1ae128f9c62a1da1ffb5f960 (diff) | |
parent | 126d3a5fdcef49410e75a9d1177d96405414dad0 (diff) | |
download | psutil-9c269edb5132fffacdd9af493c95b199ee02f551.tar.gz |
Merge branch 'master' of github.com:giampaolo/psutil
-rw-r--r-- | IDEAS | 2 | ||||
-rw-r--r-- | psutil/_psutil_windows.c | 36 | ||||
-rw-r--r-- | psutil/tests/__init__.py | 4 | ||||
-rw-r--r-- | psutil/tests/test_bsd.py | 64 | ||||
-rw-r--r-- | psutil/tests/test_process.py | 2 |
5 files changed, 93 insertions, 15 deletions
@@ -17,6 +17,8 @@ PLATFORMS FEATURES ======== +- #893: (BSD) process environ + - #809: (BSD) per-process resource limits (rlimit()). - (UNIX) process root (different from cwd) diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index 31f9d86a..4df88864 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -3004,9 +3004,18 @@ psutil_net_if_addrs(PyObject *self, PyObject *args) { unsigned int i = 0; ULONG family; PCTSTR intRet; + PCTSTR netmaskIntRet; char *ptr; char buff[100]; DWORD bufflen = 100; + char netmask_buff[100]; + DWORD netmask_bufflen = 100; + DWORD dwRetVal = 0; +#if (_WIN32_WINNT >= 0x0600) // Windows Vista and above + ULONG converted_netmask; + UINT netmask_bits; + struct in_addr in_netmask; +#endif PIP_ADAPTER_ADDRESSES pAddresses = NULL; PIP_ADAPTER_ADDRESSES pCurrAddresses = NULL; PIP_ADAPTER_UNICAST_ADDRESS pUnicast = NULL; @@ -3016,6 +3025,7 @@ psutil_net_if_addrs(PyObject *self, PyObject *args) { PyObject *py_address = NULL; PyObject *py_mac_address = NULL; PyObject *py_nic_name = NULL; + PyObject *py_netmask = NULL; if (py_retlist == NULL) return NULL; @@ -3028,6 +3038,7 @@ psutil_net_if_addrs(PyObject *self, PyObject *args) { while (pCurrAddresses) { pUnicast = pCurrAddresses->FirstUnicastAddress; + netmaskIntRet = NULL; py_nic_name = NULL; py_nic_name = PyUnicode_FromWideChar( pCurrAddresses->FriendlyName, @@ -3089,6 +3100,15 @@ psutil_net_if_addrs(PyObject *self, PyObject *args) { pUnicast->Address.lpSockaddr; intRet = inet_ntop(AF_INET, &(sa_in->sin_addr), buff, bufflen); +#if (_WIN32_WINNT >= 0x0600) // Windows Vista and above + netmask_bits = pUnicast->OnLinkPrefixLength; + dwRetVal = ConvertLengthToIpv4Mask(netmask_bits, &converted_netmask); + if (dwRetVal == NO_ERROR) { + in_netmask.s_addr = converted_netmask; + netmaskIntRet = inet_ntop(AF_INET, &in_netmask, netmask_buff, + netmask_bufflen); + } +#endif } else if (family == AF_INET6) { struct sockaddr_in6 *sa_in6 = (struct sockaddr_in6 *) @@ -3114,7 +3134,17 @@ psutil_net_if_addrs(PyObject *self, PyObject *args) { if (py_address == NULL) goto error; - Py_INCREF(Py_None); + if (netmaskIntRet != NULL) { +#if PY_MAJOR_VERSION >= 3 + py_netmask = PyUnicode_FromString(netmask_buff); +#else + py_netmask = PyString_FromString(netmask_buff); +#endif + } else { + Py_INCREF(Py_None); + py_netmask = Py_None; + } + Py_INCREF(Py_None); Py_INCREF(Py_None); py_tuple = Py_BuildValue( @@ -3122,7 +3152,7 @@ psutil_net_if_addrs(PyObject *self, PyObject *args) { py_nic_name, family, py_address, - Py_None, // netmask (not supported) + py_netmask, Py_None, // broadcast (not supported) Py_None // ptp (not supported on Windows) ); @@ -3133,6 +3163,7 @@ psutil_net_if_addrs(PyObject *self, PyObject *args) { goto error; Py_DECREF(py_tuple); Py_DECREF(py_address); + Py_DECREF(py_netmask); pUnicast = pUnicast->Next; } @@ -3151,6 +3182,7 @@ error: Py_XDECREF(py_tuple); Py_XDECREF(py_address); Py_XDECREF(py_nic_name); + Py_XDECREF(py_netmask); return NULL; } diff --git a/psutil/tests/__init__.py b/psutil/tests/__init__.py index 28f796fd..02b59dd1 100644 --- a/psutil/tests/__init__.py +++ b/psutil/tests/__init__.py @@ -9,6 +9,7 @@ Test utilities. """ +from __future__ import print_function import atexit import contextlib import errno @@ -199,7 +200,7 @@ def get_test_subprocess(cmd=None, **kwds): if cmd is None: pyline = "from time import sleep;" pyline += "open(r'%s', 'w').close();" % TESTFN - pyline += "sleep(10)" + pyline += "sleep(60)" cmd = [PYTHON, "-c", pyline] sproc = subprocess.Popen(cmd, **kwds) stop_at = time.time() + 3 @@ -466,6 +467,7 @@ def retry_before_failing(ntimes=None): return fun(*args, **kwargs) except AssertionError as _: err = _ + print("retry (%s)" % err, file=sys.stderr) if PY3: raise err else: diff --git a/psutil/tests/test_bsd.py b/psutil/tests/test_bsd.py index 53f830d3..1ac5e632 100644 --- a/psutil/tests/test_bsd.py +++ b/psutil/tests/test_bsd.py @@ -149,15 +149,8 @@ class FreeBSDSpecificTestCase(unittest.TestCase): def tearDownClass(cls): reap_children() - def test_boot_time(self): - s = sysctl('sysctl kern.boottime') - s = s[s.find(" sec = ") + 7:] - s = s[:s.find(',')] - btime = int(s) - self.assertEqual(btime, psutil.boot_time()) - @retry_before_failing() - def test_memory_maps(self): + def test_proc_memory_maps(self): out = sh('procstat -v %s' % self.pid) maps = psutil.Process(self.pid).memory_maps(grouped=False) lines = out.split('\n')[1:] @@ -171,17 +164,17 @@ class FreeBSDSpecificTestCase(unittest.TestCase): if not map.path.startswith('['): self.assertEqual(fields[10], map.path) - def test_exe(self): + def test_proc_exe(self): out = sh('procstat -b %s' % self.pid) self.assertEqual(psutil.Process(self.pid).exe(), out.split('\n')[1].split()[-1]) - def test_cmdline(self): + def test_proc_cmdline(self): out = sh('procstat -c %s' % self.pid) self.assertEqual(' '.join(psutil.Process(self.pid).cmdline()), ' '.join(out.split('\n')[1].split()[2:])) - def test_uids_gids(self): + def test_proc_uids_gids(self): out = sh('procstat -s %s' % self.pid) euid, ruid, suid, egid, rgid, sgid = out.split('\n')[1].split()[2:8] p = psutil.Process(self.pid) @@ -194,6 +187,46 @@ class FreeBSDSpecificTestCase(unittest.TestCase): self.assertEqual(gids.effective, int(egid)) self.assertEqual(gids.saved, int(sgid)) + @retry_before_failing() + def test_proc_ctx_switches(self): + tested = [] + out = sh('procstat -r %s' % self.pid) + p = psutil.Process(self.pid) + for line in out.split('\n'): + line = line.lower().strip() + if ' voluntary context' in line: + pstat_value = int(line.split()[-1]) + psutil_value = p.num_ctx_switches().voluntary + self.assertEqual(pstat_value, psutil_value) + tested.append(None) + elif ' involuntary context' in line: + pstat_value = int(line.split()[-1]) + psutil_value = p.num_ctx_switches().involuntary + self.assertEqual(pstat_value, psutil_value) + tested.append(None) + if len(tested) != 2: + raise RuntimeError("couldn't find lines match in procstat out") + + @retry_before_failing() + def test_proc_cpu_times(self): + tested = [] + out = sh('procstat -r %s' % self.pid) + p = psutil.Process(self.pid) + for line in out.split('\n'): + line = line.lower().strip() + if 'user time' in line: + pstat_value = float('0.' + line.split()[-1].split('.')[-1]) + psutil_value = p.cpu_times().user + self.assertEqual(pstat_value, psutil_value) + tested.append(None) + elif 'system time' in line: + pstat_value = float('0.' + line.split()[-1].split('.')[-1]) + psutil_value = p.cpu_times().system + self.assertEqual(pstat_value, psutil_value) + tested.append(None) + if len(tested) != 2: + raise RuntimeError("couldn't find lines match in procstat out") + # --- virtual_memory(); tests against sysctl @retry_before_failing() @@ -301,6 +334,15 @@ class FreeBSDSpecificTestCase(unittest.TestCase): # self.assertAlmostEqual(psutil.cpu_stats().traps, # sysctl('vm.stats.sys.v_trap'), delta=1000) + # --- others + + def test_boot_time(self): + s = sysctl('sysctl kern.boottime') + s = s[s.find(" sec = ") + 7:] + s = s[:s.find(',')] + btime = int(s) + self.assertEqual(btime, psutil.boot_time()) + # ===================================================================== # --- OpenBSD diff --git a/psutil/tests/test_process.py b/psutil/tests/test_process.py index f5a24da2..4d8144b3 100644 --- a/psutil/tests/test_process.py +++ b/psutil/tests/test_process.py @@ -760,7 +760,7 @@ class TestProcess(unittest.TestCase): self.assertEqual(real, os.getgid()) # os.geteuid() refers to "effective" uid self.assertEqual(effective, os.getegid()) - # no such thing as os.getsuid() ("saved" uid), but starting + # no such thing as os.getsgid() ("saved" gid), but starting # from python 2.7 we have os.getresgid()[2] if hasattr(os, "getresuid"): self.assertEqual(saved, os.getresgid()[2]) |