From 24177025811f8b15f1f8571812373a78c905b40f Mon Sep 17 00:00:00 2001 From: Jake Omann Date: Wed, 8 Jun 2016 15:45:43 -0500 Subject: [PATCH 01/14] Fix for windows net_io_counters wrapping after 4.3GB due to MIB_IFROW using DWORD. Updated to use MIB_IF_ROW2 which gives ULONG values instead. This causes more breaking changes for Windows XP and all Windows versions less than Vista / Server 2008 meaning that it should have no problems working on Vista / Server 2008 and beyond. --- psutil/_psutil_windows.c | 59 ++++++++++------------------------------ 1 file changed, 15 insertions(+), 44 deletions(-) diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index 8e3f9593f..555f362d2 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -18,9 +18,9 @@ #include #include #include +#include #include #include -#include #include // Link with Iphlpapi.lib @@ -84,24 +84,6 @@ typedef struct _DISK_PERFORMANCE_WIN_2008 { } DISK_PERFORMANCE_WIN_2008; // --- network connections mingw32 support -#ifndef _IPRTRMIB_H -typedef struct _MIB_TCP6ROW_OWNER_PID { - UCHAR ucLocalAddr[16]; - DWORD dwLocalScopeId; - DWORD dwLocalPort; - UCHAR ucRemoteAddr[16]; - DWORD dwRemoteScopeId; - DWORD dwRemotePort; - DWORD dwState; - DWORD dwOwningPid; -} MIB_TCP6ROW_OWNER_PID, *PMIB_TCP6ROW_OWNER_PID; - -typedef struct _MIB_TCP6TABLE_OWNER_PID { - DWORD dwNumEntries; - MIB_TCP6ROW_OWNER_PID table[ANY_SIZE]; -} MIB_TCP6TABLE_OWNER_PID, *PMIB_TCP6TABLE_OWNER_PID; -#endif - #ifndef __IPHLPAPI_H__ typedef struct in6_addr { union { @@ -128,18 +110,6 @@ typedef struct _MIB_UDPTABLE_OWNER_PID { } MIB_UDPTABLE_OWNER_PID, *PMIB_UDPTABLE_OWNER_PID; #endif -typedef struct _MIB_UDP6ROW_OWNER_PID { - UCHAR ucLocalAddr[16]; - DWORD dwLocalScopeId; - DWORD dwLocalPort; - DWORD dwOwningPid; -} MIB_UDP6ROW_OWNER_PID, *PMIB_UDP6ROW_OWNER_PID; - -typedef struct _MIB_UDP6TABLE_OWNER_PID { - DWORD dwNumEntries; - MIB_UDP6ROW_OWNER_PID table[ANY_SIZE]; -} MIB_UDP6TABLE_OWNER_PID, *PMIB_UDP6TABLE_OWNER_PID; - PIP_ADAPTER_ADDRESSES psutil_get_nic_addresses() { // allocate a 15 KB buffer to start with @@ -2168,7 +2138,7 @@ psutil_disk_usage(PyObject *self, PyObject *args) { static PyObject * psutil_net_io_counters(PyObject *self, PyObject *args) { DWORD dwRetVal = 0; - MIB_IFROW *pIfRow = NULL; + MIB_IF_ROW2 *pIfRow = NULL; PIP_ADAPTER_ADDRESSES pAddresses = NULL; PIP_ADAPTER_ADDRESSES pCurrAddresses = NULL; PyObject *py_retdict = PyDict_New(); @@ -2185,29 +2155,30 @@ psutil_net_io_counters(PyObject *self, PyObject *args) { while (pCurrAddresses) { py_nic_name = NULL; py_nic_info = NULL; - pIfRow = (MIB_IFROW *) malloc(sizeof(MIB_IFROW)); + pIfRow = (MIB_IF_ROW2 *) malloc(sizeof(MIB_IF_ROW2)); if (pIfRow == NULL) { PyErr_NoMemory(); goto error; } + SecureZeroMemory((PVOID)pIfRow, sizeof(MIB_IF_ROW2)); - pIfRow->dwIndex = pCurrAddresses->IfIndex; - dwRetVal = GetIfEntry(pIfRow); + pIfRow->InterfaceIndex = pCurrAddresses->IfIndex; + dwRetVal = GetIfEntry2(pIfRow); if (dwRetVal != NO_ERROR) { PyErr_SetString(PyExc_RuntimeError, "GetIfEntry() failed."); goto error; } - py_nic_info = Py_BuildValue("(kkkkkkkk)", - pIfRow->dwOutOctets, - pIfRow->dwInOctets, - pIfRow->dwOutUcastPkts, - pIfRow->dwInUcastPkts, - pIfRow->dwInErrors, - pIfRow->dwOutErrors, - pIfRow->dwInDiscards, - pIfRow->dwOutDiscards); + py_nic_info = Py_BuildValue("(KKKKKKKK)", + pIfRow->OutOctets, + pIfRow->InOctets, + pIfRow->OutUcastPkts, + pIfRow->InUcastPkts, + pIfRow->InErrors, + pIfRow->OutErrors, + pIfRow->InDiscards, + pIfRow->OutDiscards); if (!py_nic_info) goto error; From bd251f2cbd7ecbc0b60dd601fab1baddca461011 Mon Sep 17 00:00:00 2001 From: Jake Omann Date: Wed, 8 Jun 2016 20:11:25 -0500 Subject: [PATCH 02/14] Update HISTORY.rst with #816 issue bug fix --- HISTORY.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/HISTORY.rst b/HISTORY.rst index 1adcc669b..5afe9ca05 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -9,6 +9,7 @@ Bug tracker at https://github.com/giampaolo/psutil/issues - #823: [NetBSD] virtual_memory() raises TypeError on Python 3. - #829: [UNIX] psutil.disk_usage() percent field takes root reserved space into account. +- #816: [Windows] fixed net_io_counter() wrapping after 4.3GB in any field 4.2.0 - 2016-05-14 From e8cce4ac18655efa12eda646ef49b4a6571357e2 Mon Sep 17 00:00:00 2001 From: Jake Omann Date: Fri, 10 Jun 2016 12:19:32 -0500 Subject: [PATCH 03/14] Fixes for compiling on Win XP/Win 200 --- psutil/_psutil_windows.c | 69 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 67 insertions(+), 2 deletions(-) diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index 555f362d2..59c49c47c 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -18,7 +18,9 @@ #include #include #include +#if (_WIN32_WINNT >= 0x0600) #include +#endif #include #include #include @@ -84,6 +86,24 @@ typedef struct _DISK_PERFORMANCE_WIN_2008 { } DISK_PERFORMANCE_WIN_2008; // --- network connections mingw32 support +#ifndef _IPRTRMIB_H +typedef struct _MIB_TCP6ROW_OWNER_PID { + UCHAR ucLocalAddr[16]; + DWORD dwLocalScopeId; + DWORD dwLocalPort; + UCHAR ucRemoteAddr[16]; + DWORD dwRemoteScopeId; + DWORD dwRemotePort; + DWORD dwState; + DWORD dwOwningPid; +} MIB_TCP6ROW_OWNER_PID, *PMIB_TCP6ROW_OWNER_PID; + +typedef struct _MIB_TCP6TABLE_OWNER_PID { + DWORD dwNumEntries; + MIB_TCP6ROW_OWNER_PID table[ANY_SIZE]; +} MIB_TCP6TABLE_OWNER_PID, *PMIB_TCP6TABLE_OWNER_PID; +#endif + #ifndef __IPHLPAPI_H__ typedef struct in6_addr { union { @@ -110,6 +130,20 @@ typedef struct _MIB_UDPTABLE_OWNER_PID { } MIB_UDPTABLE_OWNER_PID, *PMIB_UDPTABLE_OWNER_PID; #endif +#if (_WIN32_WINNT < 0x0600) +typedef struct _MIB_UDP6ROW_OWNER_PID { + UCHAR ucLocalAddr[16]; + DWORD dwLocalScopeId; + DWORD dwLocalPort; + DWORD dwOwningPid; +} MIB_UDP6ROW_OWNER_PID, *PMIB_UDP6ROW_OWNER_PID; + +typedef struct _MIB_UDP6TABLE_OWNER_PID { + DWORD dwNumEntries; + MIB_UDP6ROW_OWNER_PID table[ANY_SIZE]; +} MIB_UDP6TABLE_OWNER_PID, *PMIB_UDP6TABLE_OWNER_PID; +#endif + PIP_ADAPTER_ADDRESSES psutil_get_nic_addresses() { // allocate a 15 KB buffer to start with @@ -1606,7 +1640,6 @@ psutil_net_connections(PyObject *self, PyObject *args) { } // TCP IPv6 - if ((PySequence_Contains(py_af_filter, _AF_INET6) == 1) && (PySequence_Contains(py_type_filter, _SOCK_STREAM) == 1)) { @@ -2138,7 +2171,12 @@ psutil_disk_usage(PyObject *self, PyObject *args) { static PyObject * psutil_net_io_counters(PyObject *self, PyObject *args) { DWORD dwRetVal = 0; +#if (_WIN32_WINNT >= 0x0600) MIB_IF_ROW2 *pIfRow = NULL; +#endif +#if (_WIN32_WINNT < 0x0600) + MIB_IFROW *pIfRow = NULL; +#endif PIP_ADAPTER_ADDRESSES pAddresses = NULL; PIP_ADAPTER_ADDRESSES pCurrAddresses = NULL; PyObject *py_retdict = PyDict_New(); @@ -2155,21 +2193,35 @@ psutil_net_io_counters(PyObject *self, PyObject *args) { while (pCurrAddresses) { py_nic_name = NULL; py_nic_info = NULL; +#if (_WIN32_WINNT >= 0x0600) pIfRow = (MIB_IF_ROW2 *) malloc(sizeof(MIB_IF_ROW2)); +#endif +#if (_WIN32_WINNT < 0x0600) + pIfRow = (MIB_IFROW *) malloc(sizeof(MIB_IFROW)); +#endif if (pIfRow == NULL) { PyErr_NoMemory(); goto error; } +#if (_WIN32_WINNT >= 0x0600) SecureZeroMemory((PVOID)pIfRow, sizeof(MIB_IF_ROW2)); +#endif +#if (_WIN32_WINNT >= 0x0600) pIfRow->InterfaceIndex = pCurrAddresses->IfIndex; dwRetVal = GetIfEntry2(pIfRow); +#endif +#if (_WIN32_WINNT < 0x0600) + pIfRow->dwIndex = pCurrAddresses->IfIndex; + dwRetVal = GetIfEntry(pIfRow); +#endif if (dwRetVal != NO_ERROR) { - PyErr_SetString(PyExc_RuntimeError, "GetIfEntry() failed."); + PyErr_SetString(PyExc_RuntimeError, "GetIfEntry() or GetIfEntry2() failed."); goto error; } +#if (_WIN32_WINNT >= 0x0600) py_nic_info = Py_BuildValue("(KKKKKKKK)", pIfRow->OutOctets, pIfRow->InOctets, @@ -2179,6 +2231,19 @@ psutil_net_io_counters(PyObject *self, PyObject *args) { pIfRow->OutErrors, pIfRow->InDiscards, pIfRow->OutDiscards); +#endif +#if (_WIN32_WINNT < 0x0600) + py_nic_info = Py_BuildValue("(kkkkkkkk)", + pIfRow->dwOutOctets, + pIfRow->dwInOctets, + pIfRow->dwOutUcastPkts, + pIfRow->dwInUcastPkts, + pIfRow->dwInErrors, + pIfRow->dwOutErrors, + pIfRow->dwInDiscards, + pIfRow->dwOutDiscards); +#endif + if (!py_nic_info) goto error; From 474b13cb3895c24abd7486400317945ff9a98b5c Mon Sep 17 00:00:00 2001 From: Jake Omann Date: Fri, 10 Jun 2016 12:21:17 -0500 Subject: [PATCH 04/14] Add back in ws2tcpip.h in the proper place in Win XP / Win 2000 --- psutil/_psutil_windows.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index 59c49c47c..bd836e04f 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -23,6 +23,9 @@ #endif #include #include + #if (_WIN32_WINNT < 0x0600) +#include +#endif #include // Link with Iphlpapi.lib From 97a853cc13d2d25bc7c12258f14840cc885e615c Mon Sep 17 00:00:00 2001 From: Jake Omann Date: Fri, 10 Jun 2016 12:22:30 -0500 Subject: [PATCH 05/14] Whoops, whitespace issue --- psutil/_psutil_windows.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index bd836e04f..029756aff 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -23,7 +23,7 @@ #endif #include #include - #if (_WIN32_WINNT < 0x0600) +#if (_WIN32_WINNT < 0x0600) #include #endif #include From 259488001ad393faf3308b731d440def6a632223 Mon Sep 17 00:00:00 2001 From: Jake Omann Date: Fri, 10 Jun 2016 12:30:07 -0500 Subject: [PATCH 06/14] Tried to keep the mingw32 support but win 7 sdk is causing issues --- psutil/_psutil_windows.c | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index 029756aff..43d969973 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -89,24 +89,6 @@ typedef struct _DISK_PERFORMANCE_WIN_2008 { } DISK_PERFORMANCE_WIN_2008; // --- network connections mingw32 support -#ifndef _IPRTRMIB_H -typedef struct _MIB_TCP6ROW_OWNER_PID { - UCHAR ucLocalAddr[16]; - DWORD dwLocalScopeId; - DWORD dwLocalPort; - UCHAR ucRemoteAddr[16]; - DWORD dwRemoteScopeId; - DWORD dwRemotePort; - DWORD dwState; - DWORD dwOwningPid; -} MIB_TCP6ROW_OWNER_PID, *PMIB_TCP6ROW_OWNER_PID; - -typedef struct _MIB_TCP6TABLE_OWNER_PID { - DWORD dwNumEntries; - MIB_TCP6ROW_OWNER_PID table[ANY_SIZE]; -} MIB_TCP6TABLE_OWNER_PID, *PMIB_TCP6TABLE_OWNER_PID; -#endif - #ifndef __IPHLPAPI_H__ typedef struct in6_addr { union { From efb026a5d88d74081e6edae9a216cf6c7f9b85bc Mon Sep 17 00:00:00 2001 From: Jake Omann Date: Fri, 10 Jun 2016 12:32:07 -0500 Subject: [PATCH 07/14] Actually does need it in XP/2000 unfortunately --- psutil/_psutil_windows.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index 43d969973..ba2b905a2 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -89,6 +89,26 @@ typedef struct _DISK_PERFORMANCE_WIN_2008 { } DISK_PERFORMANCE_WIN_2008; // --- network connections mingw32 support +#ifndef _IPRTRMIB_H +#if (_WIN32_WINNT < 0x0600) +typedef struct _MIB_TCP6ROW_OWNER_PID { + UCHAR ucLocalAddr[16]; + DWORD dwLocalScopeId; + DWORD dwLocalPort; + UCHAR ucRemoteAddr[16]; + DWORD dwRemoteScopeId; + DWORD dwRemotePort; + DWORD dwState; + DWORD dwOwningPid; +} MIB_TCP6ROW_OWNER_PID, *PMIB_TCP6ROW_OWNER_PID; + +typedef struct _MIB_TCP6TABLE_OWNER_PID { + DWORD dwNumEntries; + MIB_TCP6ROW_OWNER_PID table[ANY_SIZE]; +} MIB_TCP6TABLE_OWNER_PID, *PMIB_TCP6TABLE_OWNER_PID; +#endif +#endif + #ifndef __IPHLPAPI_H__ typedef struct in6_addr { union { From cd3003f6f17d07ad9b0557a82063a7595d98010b Mon Sep 17 00:00:00 2001 From: Jake Omann Date: Fri, 10 Jun 2016 12:39:49 -0500 Subject: [PATCH 08/14] Add comment lines to ifs for win versions --- psutil/_psutil_windows.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index ba2b905a2..022155237 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -18,12 +18,12 @@ #include #include #include -#if (_WIN32_WINNT >= 0x0600) +#if (_WIN32_WINNT >= 0x0600) // Windows Vista, 7, 8, 8.1, 10 #include #endif #include #include -#if (_WIN32_WINNT < 0x0600) +#if (_WIN32_WINNT < 0x0600) // Windows XP / 2000 #include #endif #include @@ -90,7 +90,7 @@ typedef struct _DISK_PERFORMANCE_WIN_2008 { // --- network connections mingw32 support #ifndef _IPRTRMIB_H -#if (_WIN32_WINNT < 0x0600) +#if (_WIN32_WINNT < 0x0600) // Windows XP / 2000 typedef struct _MIB_TCP6ROW_OWNER_PID { UCHAR ucLocalAddr[16]; DWORD dwLocalScopeId; @@ -135,7 +135,7 @@ typedef struct _MIB_UDPTABLE_OWNER_PID { } MIB_UDPTABLE_OWNER_PID, *PMIB_UDPTABLE_OWNER_PID; #endif -#if (_WIN32_WINNT < 0x0600) +#if (_WIN32_WINNT < 0x0600) // Windows XP / 2000 typedef struct _MIB_UDP6ROW_OWNER_PID { UCHAR ucLocalAddr[16]; DWORD dwLocalScopeId; @@ -2176,10 +2176,10 @@ psutil_disk_usage(PyObject *self, PyObject *args) { static PyObject * psutil_net_io_counters(PyObject *self, PyObject *args) { DWORD dwRetVal = 0; -#if (_WIN32_WINNT >= 0x0600) +#if (_WIN32_WINNT >= 0x0600) // Windows Vista, 7, 8, 8.1, 10 MIB_IF_ROW2 *pIfRow = NULL; #endif -#if (_WIN32_WINNT < 0x0600) +#if (_WIN32_WINNT < 0x0600) // Windows 2000 / XP MIB_IFROW *pIfRow = NULL; #endif PIP_ADAPTER_ADDRESSES pAddresses = NULL; @@ -2198,10 +2198,10 @@ psutil_net_io_counters(PyObject *self, PyObject *args) { while (pCurrAddresses) { py_nic_name = NULL; py_nic_info = NULL; -#if (_WIN32_WINNT >= 0x0600) +#if (_WIN32_WINNT >= 0x0600) // Windows Vista, 7, 8, 8.1, 10 pIfRow = (MIB_IF_ROW2 *) malloc(sizeof(MIB_IF_ROW2)); #endif -#if (_WIN32_WINNT < 0x0600) +#if (_WIN32_WINNT < 0x0600) // Windows 2000 / XP pIfRow = (MIB_IFROW *) malloc(sizeof(MIB_IFROW)); #endif @@ -2209,15 +2209,12 @@ psutil_net_io_counters(PyObject *self, PyObject *args) { PyErr_NoMemory(); goto error; } -#if (_WIN32_WINNT >= 0x0600) +#if (_WIN32_WINNT >= 0x0600) // Windows Vista, 7, 8, 8.1, 10 SecureZeroMemory((PVOID)pIfRow, sizeof(MIB_IF_ROW2)); -#endif - -#if (_WIN32_WINNT >= 0x0600) pIfRow->InterfaceIndex = pCurrAddresses->IfIndex; dwRetVal = GetIfEntry2(pIfRow); #endif -#if (_WIN32_WINNT < 0x0600) +#if (_WIN32_WINNT < 0x0600) // Windows 2000 / XP pIfRow->dwIndex = pCurrAddresses->IfIndex; dwRetVal = GetIfEntry(pIfRow); #endif @@ -2226,7 +2223,7 @@ psutil_net_io_counters(PyObject *self, PyObject *args) { goto error; } -#if (_WIN32_WINNT >= 0x0600) +#if (_WIN32_WINNT >= 0x0600) // Windows Vista, 7, 8, 8.1, 10 py_nic_info = Py_BuildValue("(KKKKKKKK)", pIfRow->OutOctets, pIfRow->InOctets, @@ -2237,7 +2234,7 @@ psutil_net_io_counters(PyObject *self, PyObject *args) { pIfRow->InDiscards, pIfRow->OutDiscards); #endif -#if (_WIN32_WINNT < 0x0600) +#if (_WIN32_WINNT < 0x0600) // Windows 2000 / XP py_nic_info = Py_BuildValue("(kkkkkkkk)", pIfRow->dwOutOctets, pIfRow->dwInOctets, From 2815aa35a8ce2eb8a0abedcade72c228796af2a4 Mon Sep 17 00:00:00 2001 From: Jake Omann Date: Fri, 10 Jun 2016 18:50:56 -0500 Subject: [PATCH 09/14] Fix disk_usage test to use 1 kB block size due to issues with OS X --- psutil/tests/test_posix.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/psutil/tests/test_posix.py b/psutil/tests/test_posix.py index 395268db3..0579f1381 100644 --- a/psutil/tests/test_posix.py +++ b/psutil/tests/test_posix.py @@ -314,12 +314,13 @@ def test_os_waitpid_bad_ret_status(self): def test_disk_usage(self): def df(device): - out = sh("df -B 1 %s" % device).strip() + # Use 1 kB block sizes since OS X doesn't have -B flag + out = sh("df -k %s" % device).strip() line = out.split('\n')[1] fields = line.split() - total = int(fields[1]) - used = int(fields[2]) - free = int(fields[3]) + total = int(fields[1]) * 1024 + used = int(fields[2]) * 1024 + free = int(fields[3]) * 1024 percent = float(fields[4].replace('%', '')) return (total, used, free, percent) @@ -332,7 +333,7 @@ def df(device): self.assertAlmostEqual(usage.free, free, delta=tolerance) # XXX - fails as per: # https://github.com/giampaolo/psutil/issues/829 - # self.assertAlmostEqual(usage.percent, percent) + # self.assertAlmostEqual(usage.percent, percent)s if __name__ == '__main__': From fb6a0fca2d739e48efcbfe38424d838adf86911c Mon Sep 17 00:00:00 2001 From: Jake Omann Date: Fri, 10 Jun 2016 19:01:02 -0500 Subject: [PATCH 10/14] Continue on RuntimeError when running df on partitions it can't run on --- psutil/tests/test_posix.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/psutil/tests/test_posix.py b/psutil/tests/test_posix.py index 0579f1381..9dfef0b72 100644 --- a/psutil/tests/test_posix.py +++ b/psutil/tests/test_posix.py @@ -327,13 +327,19 @@ def df(device): tolerance = 4 * 1024 * 1024 # 4MB for part in psutil.disk_partitions(all=False): usage = psutil.disk_usage(part.mountpoint) - total, used, free, percent = df(part.device) + try: + total, used, free, percent = df(part.device) + except RuntimeError: + # Issue with OS X not being able to read certain partitions + # Issue with Linux systems not able to read Docker mapped locations + continue + except self.assertAlmostEqual(usage.total, total, delta=tolerance) self.assertAlmostEqual(usage.used, used, delta=tolerance) self.assertAlmostEqual(usage.free, free, delta=tolerance) # XXX - fails as per: # https://github.com/giampaolo/psutil/issues/829 - # self.assertAlmostEqual(usage.percent, percent)s + # self.assertAlmostEqual(usage.percent, percent) if __name__ == '__main__': From 76056c2954c928556fdfb4c48b327c5b8efcaf2c Mon Sep 17 00:00:00 2001 From: Jake Omann Date: Fri, 10 Jun 2016 19:01:58 -0500 Subject: [PATCH 11/14] Type fix --- psutil/tests/test_posix.py | 1 - 1 file changed, 1 deletion(-) diff --git a/psutil/tests/test_posix.py b/psutil/tests/test_posix.py index 9dfef0b72..e48cf68ba 100644 --- a/psutil/tests/test_posix.py +++ b/psutil/tests/test_posix.py @@ -333,7 +333,6 @@ def df(device): # Issue with OS X not being able to read certain partitions # Issue with Linux systems not able to read Docker mapped locations continue - except self.assertAlmostEqual(usage.total, total, delta=tolerance) self.assertAlmostEqual(usage.used, used, delta=tolerance) self.assertAlmostEqual(usage.free, free, delta=tolerance) From 7a8c26852397415d2dec7e67e21ee7284339a8a0 Mon Sep 17 00:00:00 2001 From: Jake Omann Date: Fri, 10 Jun 2016 19:05:17 -0500 Subject: [PATCH 12/14] PEP 8 compliance and history update (Vista+ only for fix) --- HISTORY.rst | 3 ++- psutil/tests/test_posix.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 5afe9ca05..97e54fe9a 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -9,7 +9,8 @@ Bug tracker at https://github.com/giampaolo/psutil/issues - #823: [NetBSD] virtual_memory() raises TypeError on Python 3. - #829: [UNIX] psutil.disk_usage() percent field takes root reserved space into account. -- #816: [Windows] fixed net_io_counter() wrapping after 4.3GB in any field +- #816: [Windows] fixed net_io_counter() wrapping after 4.3GB in any fields + on Windows Vista and above. 4.2.0 - 2016-05-14 diff --git a/psutil/tests/test_posix.py b/psutil/tests/test_posix.py index e48cf68ba..c7d0f7ee5 100644 --- a/psutil/tests/test_posix.py +++ b/psutil/tests/test_posix.py @@ -331,7 +331,7 @@ def df(device): total, used, free, percent = df(part.device) except RuntimeError: # Issue with OS X not being able to read certain partitions - # Issue with Linux systems not able to read Docker mapped locations + # Issue with Linux systems not able to read Docker mappings continue self.assertAlmostEqual(usage.total, total, delta=tolerance) self.assertAlmostEqual(usage.used, used, delta=tolerance) From c105688ec65acf1ec4a0d2b0aa9c8d2e11db90b2 Mon Sep 17 00:00:00 2001 From: Jake Omann Date: Fri, 10 Jun 2016 21:24:27 -0500 Subject: [PATCH 13/14] Styling fixes (spaces instead of tabs) --- psutil/_psutil_windows.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index 022155237..1eebb5ef8 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -2202,7 +2202,7 @@ psutil_net_io_counters(PyObject *self, PyObject *args) { pIfRow = (MIB_IF_ROW2 *) malloc(sizeof(MIB_IF_ROW2)); #endif #if (_WIN32_WINNT < 0x0600) // Windows 2000 / XP - pIfRow = (MIB_IFROW *) malloc(sizeof(MIB_IFROW)); + pIfRow = (MIB_IFROW *) malloc(sizeof(MIB_IFROW)); #endif if (pIfRow == NULL) { @@ -2216,7 +2216,7 @@ psutil_net_io_counters(PyObject *self, PyObject *args) { #endif #if (_WIN32_WINNT < 0x0600) // Windows 2000 / XP pIfRow->dwIndex = pCurrAddresses->IfIndex; - dwRetVal = GetIfEntry(pIfRow); + dwRetVal = GetIfEntry(pIfRow); #endif if (dwRetVal != NO_ERROR) { PyErr_SetString(PyExc_RuntimeError, "GetIfEntry() or GetIfEntry2() failed."); @@ -2235,7 +2235,7 @@ psutil_net_io_counters(PyObject *self, PyObject *args) { pIfRow->OutDiscards); #endif #if (_WIN32_WINNT < 0x0600) // Windows 2000 / XP - py_nic_info = Py_BuildValue("(kkkkkkkk)", + py_nic_info = Py_BuildValue("(kkkkkkkk)", pIfRow->dwOutOctets, pIfRow->dwInOctets, pIfRow->dwOutUcastPkts, From 7769dbc96e05c8e59d975379dd46fcf005917791 Mon Sep 17 00:00:00 2001 From: Jake Omann Date: Sat, 11 Jun 2016 20:56:57 -0500 Subject: [PATCH 14/14] Updated to use better if/else/endif values (my bad) Updated HISTORY to explain better that Win XP still uses 32bit values Reverted test code, will add in a different PR --- CREDITS | 4 ++++ HISTORY.rst | 4 ++-- psutil/_psutil_windows.c | 37 ++++++++++++++++++------------------- psutil/tests/test_posix.py | 16 +++++----------- 4 files changed, 29 insertions(+), 32 deletions(-) diff --git a/CREDITS b/CREDITS index b8fbf5bf4..6a6c8c8a9 100644 --- a/CREDITS +++ b/CREDITS @@ -386,3 +386,7 @@ I: 776 N: Farhan Khan E: khanzf@gmail.com I: 823 + +N: Jake Omann +E: https://github.com/jhomann +I: 816 diff --git a/HISTORY.rst b/HISTORY.rst index 97e54fe9a..a912e8302 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -9,8 +9,8 @@ Bug tracker at https://github.com/giampaolo/psutil/issues - #823: [NetBSD] virtual_memory() raises TypeError on Python 3. - #829: [UNIX] psutil.disk_usage() percent field takes root reserved space into account. -- #816: [Windows] fixed net_io_counter() wrapping after 4.3GB in any fields - on Windows Vista and above. +- #816: [Windows] fixed net_io_counter() values wrapping after 4.3GB in + Windows Vista (NT 6.0) and above using 64bit values from newer win APIs. 4.2.0 - 2016-05-14 diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index 1eebb5ef8..ebc4368ea 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -18,14 +18,11 @@ #include #include #include -#if (_WIN32_WINNT >= 0x0600) // Windows Vista, 7, 8, 8.1, 10 +#if (_WIN32_WINNT >= 0x0600) // Windows Vista and above #include #endif #include #include -#if (_WIN32_WINNT < 0x0600) // Windows XP / 2000 -#include -#endif #include // Link with Iphlpapi.lib @@ -90,7 +87,7 @@ typedef struct _DISK_PERFORMANCE_WIN_2008 { // --- network connections mingw32 support #ifndef _IPRTRMIB_H -#if (_WIN32_WINNT < 0x0600) // Windows XP / 2000 +#if (_WIN32_WINNT < 0x0600) // Windows XP typedef struct _MIB_TCP6ROW_OWNER_PID { UCHAR ucLocalAddr[16]; DWORD dwLocalScopeId; @@ -135,7 +132,7 @@ typedef struct _MIB_UDPTABLE_OWNER_PID { } MIB_UDPTABLE_OWNER_PID, *PMIB_UDPTABLE_OWNER_PID; #endif -#if (_WIN32_WINNT < 0x0600) // Windows XP / 2000 +#if (_WIN32_WINNT < 0x0600) // Windows XP typedef struct _MIB_UDP6ROW_OWNER_PID { UCHAR ucLocalAddr[16]; DWORD dwLocalScopeId; @@ -2176,12 +2173,13 @@ psutil_disk_usage(PyObject *self, PyObject *args) { static PyObject * psutil_net_io_counters(PyObject *self, PyObject *args) { DWORD dwRetVal = 0; -#if (_WIN32_WINNT >= 0x0600) // Windows Vista, 7, 8, 8.1, 10 + +#if (_WIN32_WINNT >= 0x0600) // Windows Vista and above MIB_IF_ROW2 *pIfRow = NULL; -#endif -#if (_WIN32_WINNT < 0x0600) // Windows 2000 / XP +#else // Windows XP MIB_IFROW *pIfRow = NULL; #endif + PIP_ADAPTER_ADDRESSES pAddresses = NULL; PIP_ADAPTER_ADDRESSES pCurrAddresses = NULL; PyObject *py_retdict = PyDict_New(); @@ -2198,10 +2196,10 @@ psutil_net_io_counters(PyObject *self, PyObject *args) { while (pCurrAddresses) { py_nic_name = NULL; py_nic_info = NULL; -#if (_WIN32_WINNT >= 0x0600) // Windows Vista, 7, 8, 8.1, 10 + +#if (_WIN32_WINNT >= 0x0600) // Windows Vista and above pIfRow = (MIB_IF_ROW2 *) malloc(sizeof(MIB_IF_ROW2)); -#endif -#if (_WIN32_WINNT < 0x0600) // Windows 2000 / XP +#else // Windows XP pIfRow = (MIB_IFROW *) malloc(sizeof(MIB_IFROW)); #endif @@ -2209,21 +2207,23 @@ psutil_net_io_counters(PyObject *self, PyObject *args) { PyErr_NoMemory(); goto error; } -#if (_WIN32_WINNT >= 0x0600) // Windows Vista, 7, 8, 8.1, 10 + +#if (_WIN32_WINNT >= 0x0600) // Windows Vista and above SecureZeroMemory((PVOID)pIfRow, sizeof(MIB_IF_ROW2)); pIfRow->InterfaceIndex = pCurrAddresses->IfIndex; dwRetVal = GetIfEntry2(pIfRow); -#endif -#if (_WIN32_WINNT < 0x0600) // Windows 2000 / XP +#else // Windows XP pIfRow->dwIndex = pCurrAddresses->IfIndex; dwRetVal = GetIfEntry(pIfRow); #endif + if (dwRetVal != NO_ERROR) { - PyErr_SetString(PyExc_RuntimeError, "GetIfEntry() or GetIfEntry2() failed."); + PyErr_SetString(PyExc_RuntimeError, + "GetIfEntry() or GetIfEntry2() failed."); goto error; } -#if (_WIN32_WINNT >= 0x0600) // Windows Vista, 7, 8, 8.1, 10 +#if (_WIN32_WINNT >= 0x0600) // Windows Vista and above py_nic_info = Py_BuildValue("(KKKKKKKK)", pIfRow->OutOctets, pIfRow->InOctets, @@ -2233,8 +2233,7 @@ psutil_net_io_counters(PyObject *self, PyObject *args) { pIfRow->OutErrors, pIfRow->InDiscards, pIfRow->OutDiscards); -#endif -#if (_WIN32_WINNT < 0x0600) // Windows 2000 / XP +#else // Windows XP py_nic_info = Py_BuildValue("(kkkkkkkk)", pIfRow->dwOutOctets, pIfRow->dwInOctets, diff --git a/psutil/tests/test_posix.py b/psutil/tests/test_posix.py index c7d0f7ee5..395268db3 100644 --- a/psutil/tests/test_posix.py +++ b/psutil/tests/test_posix.py @@ -314,25 +314,19 @@ def test_os_waitpid_bad_ret_status(self): def test_disk_usage(self): def df(device): - # Use 1 kB block sizes since OS X doesn't have -B flag - out = sh("df -k %s" % device).strip() + out = sh("df -B 1 %s" % device).strip() line = out.split('\n')[1] fields = line.split() - total = int(fields[1]) * 1024 - used = int(fields[2]) * 1024 - free = int(fields[3]) * 1024 + total = int(fields[1]) + used = int(fields[2]) + free = int(fields[3]) percent = float(fields[4].replace('%', '')) return (total, used, free, percent) tolerance = 4 * 1024 * 1024 # 4MB for part in psutil.disk_partitions(all=False): usage = psutil.disk_usage(part.mountpoint) - try: - total, used, free, percent = df(part.device) - except RuntimeError: - # Issue with OS X not being able to read certain partitions - # Issue with Linux systems not able to read Docker mappings - continue + total, used, free, percent = df(part.device) self.assertAlmostEqual(usage.total, total, delta=tolerance) self.assertAlmostEqual(usage.used, used, delta=tolerance) self.assertAlmostEqual(usage.free, free, delta=tolerance)