feature: merge counters from multiple runs of tbm.py to one report

If a trace is too big for tbm to handle (i.e. takes too much time to
process), multiple instances of tbm can be run concurrently, and then
merge-counters.py (from this commit) can merge the results.

Change-Id: I863287901f311f416f7f75b61f604b9213818a55
diff --git a/tbm/merge-counters.py b/tbm/merge-counters.py
new file mode 100755
index 0000000..0de9575
--- /dev/null
+++ b/tbm/merge-counters.py
@@ -0,0 +1,52 @@
+#! /usr/bin/env python3
+
+"""Merge multiple saved counters to one report."""
+
+
+import argparse
+import pickle
+import sys
+from typing import Sequence
+
+
+from counter import Counter
+
+
+def main(argv: Sequence[str]) -> int:
+    parser = argparse.ArgumentParser(description=__doc__)
+
+    parser.add_argument("-r", "--report",
+                        help="Print report to RFILE",
+                        metavar="RFILE")
+
+    parser.add_argument("files",
+                        nargs="+",
+                        help="TBM counter files",
+                        metavar="FILE")
+
+    args = parser.parse_args(argv)
+
+    data = None
+
+    for cfile in args.files:
+        with open(cfile, "rb") as ocfile:
+            new: Counter = pickle.load(ocfile)
+
+        if data is None:
+            data = new
+        else:
+            data += new
+
+    assert data
+
+    if args.report:
+        with open(args.report, "w", encoding="ascii") as out:
+            data.print(out)
+    else:
+        data.print()
+
+    return 0
+
+
+if __name__ == '__main__':
+    sys.exit(main(sys.argv[1:]))