Skip to content

Commit

Permalink
Lint warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
misterion committed Feb 9, 2016
1 parent 478f874 commit c1f9f00
Show file tree
Hide file tree
Showing 17 changed files with 124 additions and 104 deletions.
4 changes: 2 additions & 2 deletions rma/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,11 @@ def do_scanner(self, r, res):
}
total = r.total_keys()
for key, data in res.items():
type = type_id_to_redis_type(key)
r_type = type_id_to_redis_type(key)
aggregate_patterns = self.get_pattern_aggregated_data(data, key)

for k, v in aggregate_patterns.items():
key_stat['data'].append([k, len(v), type, floored_percentage(len(v) / total, 2)])
key_stat['data'].append([k, len(v), r_type, floored_percentage(len(v) / total, 2)])
key_stat['data'].sort(key=lambda x: x[1], reverse=True)
return ["Keys by types", key_stat]

Expand Down
2 changes: 1 addition & 1 deletion rma/helpers/formating.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ def make_total_row(source, agg):
:param agg:
:return:
"""
return [agg[index](value) if callable(agg[index]) else agg[index] for index, value in enumerate(zip(*source))]
return [agg[index](value) if callable(agg[index]) else agg[index] for index, value in enumerate(zip(*source))]
18 changes: 12 additions & 6 deletions rma/helpers/numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@ def is_num(value):
try:
int(value)
return True
except:
except ValueError:
return False


# http://code.activestate.com/recipes/415233-getting-minmax-in-a-sequence-greaterless-than-some/
def min_ge(seq, val):
"""
Same as min_gt() except items equal to val are accepted as well.
>>> min_ge([1, 3, 6, 7], 6)
6
>>> min_ge([2, 3, 4, 8], 8)
8
:param seq:
:param val:
:return:
Examples:
min_ge([1, 3, 6, 7], 6)
>>>6
min_ge([2, 3, 4, 8], 8)
>>>8
"""

for v in seq:
Expand All @@ -26,7 +32,7 @@ def next_power_of_2(n):
"""
Return next power of 2 greater than or equal to n
"""
return 2**(n-1).bit_length()
return 2**(n - 1).bit_length()


def is_power2(num):
Expand Down
18 changes: 11 additions & 7 deletions rma/jemalloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@

class Jemalloc:
"""
Small: All 2^n-aligned allocations of size 2^n will incur no additional overhead, due to how small allocations are aligned and packed.
Small: All 2^n-aligned allocations of size 2^n will incur no additional overhead, due to how small allocations are
aligned and packed.
Small: [8], [16, 32, 48, ..., 128], [192, 256, 320, ..., 512], [768, 1024, 1280, ..., 3840]
Large: The worst case size is half the chunk size, in which case only one allocation per chunk can be allocated.
If the remaining (nearly) half of the chunk isn't otherwise useful for smaller allocations, the overhead will essentially be 50%.
However, assuming you use a diverse mixture of size classes, the actual overhead shouldn't be a significant issue in practice.
If the remaining (nearly) half of the chunk isn't otherwise useful for smaller allocations, the overhead will
essentially be 50%. However, assuming you use a diverse mixture of size classes, the actual overhead shouldn't be a
significant issue in practice.
Large: [4 KiB, 8 KiB, 12 KiB, ..., 4072 KiB]
Huge: Extra virtual memory is mapped, then the excess is trimmed and unmapped. This can leave virtual memory holes,
but it incurs no physical memory overhead. Earlier versions of jemalloc heuristically attempted to optimistically map
chunks without excess that would need to be trimmed, but it didn't save much system call overhead in practice, and I
Huge: [4 MiB, 8 MiB, 12 MiB, ...
ripped the code out during a simplification pass.
but it incurs no physical memory overhead. Earlier versions of jemalloc heuristically attempted to optimistically
map chunks without excess that would need to be trimmed, but it didn't save much system call overhead in practice.
Huge: [4 MiB, 8 MiB, 12 MiB, ..., 512 MiB]
"""
@staticmethod
def align(size):
Expand Down
60 changes: 0 additions & 60 deletions rma/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ def get_redis_object_overhead():
void *ptr;
} robj;
:param length:
:return:
"""
return 4 + 4 + 8 + 8 + size_of_pointer_fn()
Expand Down Expand Up @@ -126,7 +125,6 @@ def size_of_aligned_string(value, encoding=""):
return size_of_aligned_string_by_size(str_len, encoding)



def size_of_aligned_string_by_size(sdslen, encoding):
r_obj = get_redis_object_overhead()
if encoding == REDIS_ENCODING_INT:
Expand Down Expand Up @@ -202,64 +200,6 @@ def intset_aligned(value):
return Jemalloc.align(8 + overhead)


class StringEntry:
def __init__(self, value=""):
self.encoding = get_string_encoding(value)
self.useful_bytes = len(value)
self.free_bytes = 0
self.aligned = size_of_aligned_string(value, encoding=self.encoding)

def __enter__(self):
return self

def __exit__(self, *exc):
return False


class RealStringEntry:
def get_int_encoded_bytes(self, redis, key_name):
try:
num_value = int(redis.get(key_name))
if num_value < REDIS_SHARED_INTEGERS:
return 0
else:
return size_of_pointer_fn()
except ValueError:
pass

return size_of_pointer_fn()

def __init__(self, key_name, redis):
"""
:param key_name:
:param RmaRedis redis:
:return:
"""

self.logger = logging.getLogger(__name__)
try:
self.encoding = redis.object("ENCODING", key_name).decode('utf8')
except AttributeError as e:
self.logger.warning("Invalid encoding from server for key `%s`" % key_name)
self.encoding = REDIS_ENCODING_EMBSTR
if self.encoding == REDIS_ENCODING_INT:
self.useful_bytes = self.get_int_encoded_bytes(redis, key_name)
self.free_bytes = 0
self.aligned = size_of_aligned_string_by_size(self.useful_bytes, encoding=self.encoding)
elif self.encoding == REDIS_ENCODING_EMBSTR or self.encoding == REDIS_ENCODING_RAW:
sdslen_response = redis.debug_sdslen(key_name)
self.useful_bytes = sdslen_response['val_sds_len']
self.free_bytes = sdslen_response['val_sds_avail']
sds_len = 8 + self.useful_bytes + self.free_bytes + 1
self.aligned = size_of_aligned_string_by_size(sds_len, encoding=self.encoding)

def __enter__(self):
return self

def __exit__(self, *exc):
return False


def parse_debug(response):
"""
Parse the result of Redis's DEBUG command into a Python dict
Expand Down
2 changes: 1 addition & 1 deletion rma/reporters/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from rma.reporters.TextReporter import TextReporter
__all__ = ['TextReporter']
__all__ = ['TextReporter']
10 changes: 6 additions & 4 deletions rma/rule/Hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __init__(self, key_name, redis):
self.system = dict_overhead(self.count)
self.fieldAlignedBytes = sum(map(size_of_aligned_string, self.keys))
self.valueAlignedBytes = sum(map(size_of_aligned_string, self.values))
elif self.encoding =='ziplist':
elif self.encoding == 'ziplist':
self.system = ziplist_overhead(self.count)
self.fieldAlignedBytes = sum(map(size_of_ziplist_aligned_string, self.keys))
self.valueAlignedBytes = sum(map(size_of_ziplist_aligned_string, self.values))
Expand Down Expand Up @@ -80,7 +80,8 @@ def __init__(self, redis):

def analyze(self, keys):
key_stat = {
'headers': ['Match', "Count", "Avg key count", "Key mem", "Real", "Ratio", "Value mem", "Real", "Ratio", "System", "Encoding", "Total mem","Total aligned" ],
'headers': ['Match', "Count", "Avg key count", "Key mem", "Real", "Ratio", "Value mem", "Real", "Ratio",
"System", "Encoding", "Total mem", "Total aligned"],
'data': []
}
# Undone Prefered encoding
Expand All @@ -96,7 +97,7 @@ def analyze(self, keys):
agg.fieldAlignedBytes / agg.fieldUsedBytes,
agg.valueUsedBytes,
agg.valueAlignedBytes,
agg.valueAlignedBytes/ (agg.valueUsedBytes if agg.valueUsedBytes > 0 else 1),
agg.valueAlignedBytes / (agg.valueUsedBytes if agg.valueUsedBytes > 0 else 1),
agg.system,
agg.encoding,
agg.fieldUsedBytes + agg.valueUsedBytes,
Expand All @@ -106,7 +107,8 @@ def analyze(self, keys):
key_stat['data'].append(stat_entry)

key_stat['data'].sort(key=lambda x: x[12], reverse=True)
key_stat['data'].append(make_total_row(key_stat['data'], ['Total:', sum, 0, sum, sum, 0, sum, sum, 0, sum, '', sum, sum]))
key_stat['data'].append(
make_total_row(key_stat['data'], ['Total:', sum, 0, sum, sum, 0, sum, sum, 0, sum, '', sum, sum]))

return [
"Hash stat",
Expand Down
15 changes: 14 additions & 1 deletion rma/rule/KeyString.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import statistics
from rma.redis import *
from rma.helpers import *
from itertools import *

import statistics

class StringEntry:
def __init__(self, value=""):
self.encoding = get_string_encoding(value)
self.useful_bytes = len(value)
self.free_bytes = 0
self.aligned = size_of_aligned_string(value, encoding=self.encoding)

def __enter__(self):
return self

def __exit__(self, *exc):
return False


class KeyString:
Expand Down
6 changes: 3 additions & 3 deletions rma/rule/List.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __init__(self, key_name, redis):
if self.encoding == 'linkedlist':
self.system = dict_overhead(self.count)
self.valueAlignedBytes = sum(map(size_of_linkedlist_aligned_string, self.values))
elif self.encoding =='ziplist' or self.encoding =='quicklist':
elif self.encoding == 'ziplist' or self.encoding == 'quicklist':
# Undone `quicklist`
self.system = ziplist_overhead(self.count)
self.valueAlignedBytes = sum(map(size_of_ziplist_aligned_string, self.values))
Expand Down Expand Up @@ -79,7 +79,7 @@ def analyze(self, keys):
agg.fieldAvgCount,
agg.valueUsedBytes,
agg.valueAlignedBytes,
agg.valueAlignedBytes/ (agg.valueUsedBytes if agg.valueUsedBytes > 0 else 1),
agg.valueAlignedBytes / (agg.valueUsedBytes if agg.valueUsedBytes > 0 else 1),
agg.system,
agg.encoding,
agg.valueAlignedBytes + agg.system
Expand All @@ -93,4 +93,4 @@ def analyze(self, keys):
return [
"List stat",
key_stat
]
]
6 changes: 3 additions & 3 deletions rma/rule/Set.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def __init__(self, key_name, redis):
self.valueUsedBytes = sum((len(x) for x in self.values))
self.valueAlignedBytes = sum(map(size_of_aligned_string, self.values))
self.total = self.valueAlignedBytes + self.system
elif self.encoding =='intset':
elif self.encoding == 'intset':
self.system = intset_overhead(self.count)
# In case if intset valueAlignedBytes already contains system overhead
self.valueAlignedBytes = sum(map(size_of_ziplist_aligned_string, self.values))
Expand Down Expand Up @@ -79,7 +79,7 @@ def analyze(self, keys):
agg.fieldAvgCount,
agg.valueUsedBytes,
agg.valueAlignedBytes,
agg.valueAlignedBytes/ (agg.valueUsedBytes if agg.valueUsedBytes > 0 else 1),
agg.valueAlignedBytes / (agg.valueUsedBytes if agg.valueUsedBytes > 0 else 1),
agg.system,
agg.encoding,
agg.total
Expand All @@ -93,4 +93,4 @@ def analyze(self, keys):
return [
"SET stat",
key_stat
]
]
62 changes: 57 additions & 5 deletions rma/rule/ValueString.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,50 @@
from rma.helpers import *


class RealStringEntry:
def get_int_encoded_bytes(self, redis, key_name):
try:
num_value = int(redis.get(key_name))
if num_value < REDIS_SHARED_INTEGERS:
return 0
else:
return size_of_pointer_fn()
except ValueError:
pass

return size_of_pointer_fn()

def __init__(self, key_name, redis):
"""
:param key_name:
:param RmaRedis redis:
:return:
"""

self.logger = logging.getLogger(__name__)
try:
self.encoding = redis.object("ENCODING", key_name).decode('utf8')
except AttributeError as e:
self.logger.warning("Invalid encoding from server for key `%s`" % key_name)
self.encoding = REDIS_ENCODING_EMBSTR
if self.encoding == REDIS_ENCODING_INT:
self.useful_bytes = self.get_int_encoded_bytes(redis, key_name)
self.free_bytes = 0
self.aligned = size_of_aligned_string_by_size(self.useful_bytes, encoding=self.encoding)
elif self.encoding == REDIS_ENCODING_EMBSTR or self.encoding == REDIS_ENCODING_RAW:
sdslen_response = redis.debug_sdslen(key_name)
self.useful_bytes = sdslen_response['val_sds_len']
self.free_bytes = sdslen_response['val_sds_avail']
sds_len = 8 + self.useful_bytes + self.free_bytes + 1
self.aligned = size_of_aligned_string_by_size(sds_len, encoding=self.encoding)

def __enter__(self):
return self

def __exit__(self, *exc):
return False


class ValueString:
def __init__(self, redis):
self.redis = redis
Expand Down Expand Up @@ -33,18 +77,26 @@ def analyze(self, keys):
prefered_encoding = pref_encoding(encodings)

min_bytes = min(used_bytes)
mean = statistics.mean(used_bytes) if total_elements > 1 else min_bytes;
mean = statistics.mean(used_bytes) if total_elements > 1 else min_bytes

stat_entry = [
pattern, total_elements, used_user, free_user, aligned, aligned / (used_user if used_user > 0 else 1), prefered_encoding,
min_bytes, max(used_bytes), mean,
pattern,
total_elements,
used_user,
free_user,
aligned,
aligned / (used_user if used_user > 0 else 1),
prefered_encoding,
min_bytes,
max(used_bytes),
mean,
]
key_stat['data'].append(stat_entry)

key_stat['data'].sort(key=lambda x: x[1], reverse=True)
key_stat['data'].sort(key=lambda e: e[1], reverse=True)
key_stat['data'].append(make_total_row(key_stat['data'], ['Total:', sum, sum, 0, sum, 0, '', 0, 0, 0]))

return [
"String value stat",
key_stat
]
]
Loading

0 comments on commit c1f9f00

Please sign in to comment.