Skip to content

Commit

Permalink
Add an option to strip leading zeros from linear histograms
Browse files Browse the repository at this point in the history
Sometimes histogram gives us too much zero info that we don't really care.

For example:
      usec         : count     distribution
        0          : 0        |                                        |
        1          : 0        |                                        |
        2          : 0        |                                        |
        3          : 0        |                                        |
        4          : 0        |                                        |
        5          : 0        |                                        |
        6          : 0        |                                        |
        7          : 0        |                                        |
        8          : 0        |                                        |
        9          : 0        |                                        |
        10         : 0        |                                        |
        11         : 0        |                                        |
        12         : 0        |                                        |
        13         : 0        |                                        |
        14         : 0        |                                        |
        15         : 0        |                                        |
        16         : 0        |                                        |
        17         : 0        |                                        |
        18         : 0        |                                        |
        19         : 0        |                                        |
        20         : 0        |                                        |
        21         : 0        |                                        |
        22         : 0        |                                        |
        23         : 0        |                                        |
        24         : 0        |                                        |
        25         : 0        |                                        |
        26         : 0        |                                        |
        27         : 0        |                                        |
        28         : 0        |                                        |
        29         : 0        |                                        |
        30         : 0        |                                        |
        31         : 0        |                                        |
        32         : 0        |                                        |
        33         : 0        |                                        |
        34         : 0        |                                        |
        35         : 0        |                                        |
        36         : 0        |                                        |
        37         : 0        |                                        |
        38         : 0        |                                        |
        39         : 0        |                                        |
        40         : 0        |                                        |
        41         : 7        |****************************************|
        42         : 2        |***********                             |

Such much info is hard to analyze by FIRST glance, especially console view

After supporting strip leading zeros
print_linear_hist("usec", "name", name_print, strip_leading_zero=True)

      usec         : count     distribution
        41         : 7        |****************************************|
        42         : 2        |*************                           |

This is what we really care, and it's clear.

Signed-off-by: Edward Wu <[email protected]>
  • Loading branch information
netedwardwu authored and yonghong-song committed Jun 4, 2021
1 parent 09404eb commit 6699757
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/python/bcc/table.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def _print_log2_hist(vals, val_type, strip_leading_zero):
print(body % (low, high, val, stars,
_stars(val, val_max, stars)))

def _print_linear_hist(vals, val_type):
def _print_linear_hist(vals, val_type, strip_leading_zero):
global stars_max
log2_dist_max = 64
idx_max = -1
Expand All @@ -186,8 +186,15 @@ def _print_linear_hist(vals, val_type):
print(header % val_type);
for i in range(0, idx_max + 1):
val = vals[i]
print(body % (i, val, stars,
_stars(val, val_max, stars)))

if strip_leading_zero:
if val:
print(body % (i, val, stars,
_stars(val, val_max, stars)))
strip_leading_zero = False
else:
print(body % (i, val, stars,
_stars(val, val_max, stars)))


def get_table_type_name(ttype):
Expand Down Expand Up @@ -650,10 +657,11 @@ def print_log2_hist(self, val_type="value", section_header="Bucket ptr",
_print_log2_hist(vals, val_type, strip_leading_zero)

def print_linear_hist(self, val_type="value", section_header="Bucket ptr",
section_print_fn=None, bucket_fn=None, bucket_sort_fn=None):
section_print_fn=None, bucket_fn=None, strip_leading_zero=None,
bucket_sort_fn=None):
"""print_linear_hist(val_type="value", section_header="Bucket ptr",
section_print_fn=None, bucket_fn=None,
bucket_sort_fn=None)
strip_leading_zero=None, bucket_sort_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
Expand All @@ -662,6 +670,8 @@ def print_linear_hist(self, val_type="value", section_header="Bucket ptr",
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.
If the value of strip_leading_zero is not False, prints a histogram
that is omitted leading zeros from the beginning.
If bucket_sort_fn is not None, it will be used to sort the buckets
before iterating them, and it is useful when there are multiple fields
in the secondary key.
Expand All @@ -680,7 +690,7 @@ def print_linear_hist(self, val_type="value", section_header="Bucket ptr",
section_print_fn(bucket)))
else:
print("\n%s = %r" % (section_header, bucket))
_print_linear_hist(vals, val_type)
_print_linear_hist(vals, val_type, strip_leading_zero)
else:
vals = [0] * linear_index_max
for k, v in self.items():
Expand All @@ -691,7 +701,7 @@ def print_linear_hist(self, val_type="value", section_header="Bucket ptr",
# 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)
_print_linear_hist(vals, val_type, strip_leading_zero)


class HashTable(TableBase):
Expand Down

0 comments on commit 6699757

Please sign in to comment.