summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2017-05-08 19:17:30 +0200
committerGiampaolo Rodola <g.rodola@gmail.com>2017-05-08 19:17:30 +0200
commitf0d5eed1b365e020358447b4abc3f253552e57dd (patch)
treec3932bce4c0611a9a520ec1c83ac1368dbe71a42
parent6b2f97b9e4e8bb9bd095ac8efd342b8ea7b22f69 (diff)
downloadpsutil-f0d5eed1b365e020358447b4abc3f253552e57dd.tar.gz
#802: finally start to get it right (tests pass)
-rw-r--r--psutil/_common.py64
1 files changed, 23 insertions, 41 deletions
diff --git a/psutil/_common.py b/psutil/_common.py
index 1f826a0d..9e604b6a 100644
--- a/psutil/_common.py
+++ b/psutil/_common.py
@@ -18,6 +18,7 @@ import stat
import sys
import threading
import warnings
+from collections import defaultdict
from collections import namedtuple
from socket import AF_INET
from socket import SOCK_DGRAM
@@ -470,54 +471,33 @@ def deprecated_method(replacement):
_wrapn_lock = threading.Lock()
_wrapn_cache = {}
+_wrapn_reminders = defaultdict(int)
-def wrap_numbers(new_dict, name):
- def did_nums_wrap(new_nt, old_nt):
- # Return True if one of the numbers of the new ntuple is smaller
- # than the number of the old ntuple in the same position.
- for i in range(len(new_nt)):
- new_value = new_nt[i]
- old_value = old_nt[i]
- if new_value < old_value:
- return True
- return False
-
- def replace_ntuple(new_nt, old_nt):
- # Return a new ntuple with the "adjusted" numbers.
- bits = []
- for i in range(len(new_nt)):
- new_value = new_nt[i]
- old_value = old_nt[i]
- if new_value < old_value:
- # they wrapped!
- num = old_value + new_value
- else:
- num = new_value
- bits.append(num)
- return new_nt._make(bits)
-
+def wrap_numbers(input_dict, name):
with _wrapn_lock:
if name not in _wrapn_cache:
# This was the first call.
- _wrapn_cache[name] = new_dict
- return new_dict
+ _wrapn_cache[name] = input_dict
+ return input_dict
+ new_dict = {}
old_dict = _wrapn_cache[name]
- for key, new_nt in new_dict.items():
- try:
- old_nt = old_dict[key]
- except KeyError:
- # We may get here if net_io_counters() returned a new NIC
- # or disk_io_counters() returned a new disk.
- continue
- else:
- assert new_nt._fields == old_nt._fields, (new_nt, old_nt)
- if did_nums_wrap(new_nt, old_nt):
- # we wrapped!
- new_dict[key] = replace_ntuple(new_nt, old_nt)
-
- _wrapn_cache[name] = new_dict
+ for key in input_dict.keys():
+ old_nt = old_dict[key]
+ input_nt = input_dict[key]
+
+ bits = []
+ for i in range(len(input_nt)):
+ old_value = old_nt[i]
+ input_value = input_nt[i]
+ remkey = (name, key, i)
+ if input_value < old_value:
+ _wrapn_reminders[remkey] += old_value
+ bits.append(input_value + _wrapn_reminders[remkey])
+ new_dict[key] = input_nt._make(bits)
+
+ _wrapn_cache[name] = input_dict
return new_dict
@@ -525,8 +505,10 @@ def _wrapn_cache_clear(name=None):
with _wrapn_lock:
if name is None:
_wrapn_cache.clear()
+ _wrapn_reminders.clear()
else:
_wrapn_cache.pop(name)
+ # TODO
wrap_numbers.cache_clear = _wrapn_cache_clear