Commit 6df2b0ee authored by Brenden Blanco's avatar Brenden Blanco

Add custom formatter function to print_log2_hist

Fixes: #251
Signed-off-by: default avatarBrenden Blanco <bblanco@plumgrid.com>
parent ebc8e7e7
...@@ -238,13 +238,17 @@ class BPF(object): ...@@ -238,13 +238,17 @@ class BPF(object):
text = text[:-1] + "+" text = text[:-1] + "+"
return text return text
def print_log2_hist(self, val_type="value", bucket_type="ptr"): def print_log2_hist(self, val_type="value", section_header="Bucket ptr",
"""print_log2_hist(val_type="value", bucket_type="ptr") section_print_fn=None):
"""print_log2_hist(val_type="value", section_header="Bucket ptr",
section_print_fn=None)
Prints a table as a log2 histogram. The table must be stored as Prints a table as a log2 histogram. The table must be stored as
log2. The val_type argument is optional, and is a column header. log2. The val_type argument is optional, and is a column header.
If the histogram has a secondary key, multiple tables will print If the histogram has a secondary key, multiple tables will print
and bucket_type can be used as a header description for each. 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 isinstance(self.Key(), ct.Structure): if isinstance(self.Key(), ct.Structure):
tmp = {} tmp = {}
...@@ -256,7 +260,11 @@ class BPF(object): ...@@ -256,7 +260,11 @@ class BPF(object):
slot = getattr(k, f2) slot = getattr(k, f2)
vals[slot] = v.value vals[slot] = v.value
for bucket, vals in tmp.items(): for bucket, vals in tmp.items():
print("\nBucket %s = %r" % (bucket_type, bucket)) if section_print_fn:
print("\n%s = %s" % (section_header,
section_print_fn(bucket)))
else:
print("\n%s = %r" % (section_header, bucket))
self._print_log2_hist(vals, val_type, 0) self._print_log2_hist(vals, val_type, 0)
else: else:
vals = [0] * 65 vals = [0] * 65
......
...@@ -161,20 +161,6 @@ if matched == 0: ...@@ -161,20 +161,6 @@ if matched == 0:
print("Tracing %d functions for \"%s\"... Hit Ctrl-C to end." % print("Tracing %d functions for \"%s\"... Hit Ctrl-C to end." %
(matched / 2, args.pattern)) (matched / 2, args.pattern))
# custom output (from __init__.py)
def print_log2_hist_byfunc(self, val_type="value"):
tmp = {}
f1 = self.Key._fields_[0][0]
f2 = self.Key._fields_[1][0]
for k, v in self.items():
bucket = getattr(k, f1)
vals = tmp[bucket] = tmp.get(bucket, [0] * 65)
slot = getattr(k, f2)
vals[slot] = v.value
for bucket, vals in tmp.items():
print("\nFunction = %s" % BPF.ksym(bucket))
self._print_log2_hist(vals, val_type, 0)
# output # output
exiting = 0 if args.interval else 1 exiting = 0 if args.interval else 1
dist = b.get_table("dist") dist = b.get_table("dist")
...@@ -191,7 +177,7 @@ while (1): ...@@ -191,7 +177,7 @@ while (1):
print("%-8s\n" % strftime("%H:%M:%S"), end="") print("%-8s\n" % strftime("%H:%M:%S"), end="")
if args.function: if args.function:
print_log2_hist_byfunc(dist, label) dist.print_log2_hist(label, "Function", BPF.ksym)
else: else:
dist.print_log2_hist(label) dist.print_log2_hist(label)
dist.clear() dist.clear()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment