Add custom formatter function to print_log2_hist
authorBrenden Blanco <bblanco@plumgrid.com>
Fri, 25 Sep 2015 20:57:18 +0000 (13:57 -0700)
committerBrenden Blanco <bblanco@plumgrid.com>
Fri, 25 Sep 2015 20:57:18 +0000 (13:57 -0700)
Fixes: #251
Signed-off-by: Brenden Blanco <bblanco@plumgrid.com>
src/python/bcc/__init__.py
tools/funclatency

index 2069e44..9fba270 100644 (file)
@@ -238,13 +238,17 @@ class BPF(object):
                 text = text[:-1] + "+"
             return text
 
-        def print_log2_hist(self, val_type="value", bucket_type="ptr"):
-            """print_log2_hist(val_type="value", bucket_type="ptr")
+        def print_log2_hist(self, val_type="value", section_header="Bucket 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
             log2. The val_type argument is optional, and is a column header.
             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):
                 tmp = {}
@@ -256,7 +260,11 @@ class BPF(object):
                     slot = getattr(k, f2)
                     vals[slot] = v.value
                 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)
             else:
                 vals = [0] * 65
index 21438fb..79a9406 100755 (executable)
@@ -161,20 +161,6 @@ if matched == 0:
 print("Tracing %d functions for \"%s\"... Hit Ctrl-C to end." %
     (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
 exiting = 0 if args.interval else 1
 dist = b.get_table("dist")
@@ -191,7 +177,7 @@ while (1):
                print("%-8s\n" % strftime("%H:%M:%S"), end="")
 
        if args.function:
-               print_log2_hist_byfunc(dist, label)
+               dist.print_log2_hist(label, "Function", BPF.ksym)
        else:
                dist.print_log2_hist(label)
        dist.clear()