Skip to content

Commit

Permalink
Merge pull request iovisor#861 from brendangregg/master
Browse files Browse the repository at this point in the history
Improve linear histogram limit, and improve error message.
  • Loading branch information
4ast authored Dec 14, 2016
2 parents 698e4f7 + e7427d9 commit 7151673
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions src/python/bcc/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
BPF_MAP_TYPE_LRU_PERCPU_HASH = 10

stars_max = 40
log2_index_max = 65
linear_index_max = 1025

# helper functions, consider moving these to a utils module
def _stars(val, val_max, width):
Expand Down Expand Up @@ -287,6 +289,8 @@ def print_log2_hist(self, val_type="value", section_header="Bucket ptr",
If section_print_fn is not None, it will be passed the bucket value
to format into a string as it sees fit. If bucket_fn is not None,
it will be used to produce a bucket value for the histogram keys.
The maximum index allowed is log2_index_max (65), which will
accomodate any 64-bit integer in the histogram.
"""
if isinstance(self.Key(), ct.Structure):
tmp = {}
Expand All @@ -296,7 +300,7 @@ def print_log2_hist(self, val_type="value", section_header="Bucket ptr",
bucket = getattr(k, f1)
if bucket_fn:
bucket = bucket_fn(bucket)
vals = tmp[bucket] = tmp.get(bucket, [0] * 65)
vals = tmp[bucket] = tmp.get(bucket, [0] * log2_index_max)
slot = getattr(k, f2)
vals[slot] = v.value
for bucket, vals in tmp.items():
Expand All @@ -307,7 +311,7 @@ def print_log2_hist(self, val_type="value", section_header="Bucket ptr",
print("\n%s = %r" % (section_header, bucket))
_print_log2_hist(vals, val_type)
else:
vals = [0] * 65
vals = [0] * log2_index_max
for k, v in self.items():
vals[k.value] = v.value
_print_log2_hist(vals, val_type)
Expand All @@ -318,12 +322,14 @@ def print_linear_hist(self, val_type="value", section_header="Bucket ptr",
section_print_fn=None, bucket_fn=None)
Prints a table as a linear histogram. This is intended to span integer
ranges, eg, from 0 to 100. The val_type argument is optional, and is a
column header. If the histogram has a secondary key, multiple tables
will print and section_header can be used as a header description for
each. If section_print_fn is not None, it will be passed the bucket
value to format into a string as it sees fit. If bucket_fn is not None,
ranges, eg, from 0 to 100. The val_type argument is optional, and is a
column header. If the histogram has a secondary key, multiple tables
will print and section_header can be used as a header description for
each. If section_print_fn is not None, it will be passed the bucket
value to format into a string as it sees fit. If bucket_fn is not None,
it will be used to produce a bucket value for the histogram keys.
The maximum index allowed is linear_index_max (1025), which is hoped
to be sufficient for integer ranges spanned.
"""
if isinstance(self.Key(), ct.Structure):
tmp = {}
Expand All @@ -333,7 +339,7 @@ def print_linear_hist(self, val_type="value", section_header="Bucket ptr",
bucket = getattr(k, f1)
if bucket_fn:
bucket = bucket_fn(bucket)
vals = tmp[bucket] = tmp.get(bucket, [0] * 65)
vals = tmp[bucket] = tmp.get(bucket, [0] * linear_index_max)
slot = getattr(k, f2)
vals[slot] = v.value
for bucket, vals in tmp.items():
Expand All @@ -344,9 +350,15 @@ def print_linear_hist(self, val_type="value", section_header="Bucket ptr",
print("\n%s = %r" % (section_header, bucket))
_print_linear_hist(vals, val_type)
else:
vals = [0] * 65
vals = [0] * linear_index_max
for k, v in self.items():
vals[k.value] = v.value
try:
vals[k.value] = v.value
except IndexError:
# Improve error text. If the limit proves a nusiance, this
# function be rewritten to avoid having one.
raise IndexError(("Index in print_linear_hist() of %d " +
"exceeds max of %d.") % (k.value, linear_index_max))
_print_linear_hist(vals, val_type)


Expand Down

0 comments on commit 7151673

Please sign in to comment.