tp: remove all increase in memory use caused by switching to vectors

This reverses all the increases in resting memory caused by
aosp/2129479 by calling shrink to fit once tables have been parsed.
This doesn't affect tables while they're being parsed: maybe we need
to look at this but needs more analysis.

Bug: 235104800
Doc: go/perfetto-tp-memory-use
Change-Id: I7b36397059f4578fd23fad131b845e08e430eaba
diff --git a/tools/measure_tp_performance.py b/tools/measure_tp_performance.py
index 88a372e..8014c39 100755
--- a/tools/measure_tp_performance.py
+++ b/tools/measure_tp_performance.py
@@ -35,7 +35,7 @@
   tp = subprocess.Popen(
       tp_args,
       stdin=subprocess.PIPE,
-      stdout=subprocess.DEVNULL,
+      stdout=None if args.verbose else subprocess.DEVNULL,
       stderr=subprocess.PIPE,
       universal_newlines=True,
       env=env)
@@ -43,6 +43,8 @@
   lines = []
   while True:
     line = tp.stderr.readline()
+    if args.verbose:
+      sys.stderr.write(line)
     lines.append(line)
 
     match = REGEX.match(line)
@@ -81,8 +83,8 @@
   profile = subprocess.Popen(
       perfetto_args,
       stdin=subprocess.PIPE,
-      stdout=subprocess.DEVNULL,
-      stderr=subprocess.DEVNULL)
+      stdout=None if args.verbose else subprocess.DEVNULL,
+      stderr=None if args.verbose else subprocess.DEVNULL)
   profile.stdin.write(config)
   profile.stdin.close()
 
@@ -149,8 +151,11 @@
   parser.add_argument(
       '--kill-existing',
       action='store_true',
-      help='Kill traced, perfetto_cmd '
-      'and trace processor shell if running')
+      help='Kill traced, perfetto_cmd and trace processor shell if running')
+  parser.add_argument(
+      '--verbose',
+      action='store_true',
+      help='Logs all stderr and stdout from subprocesses')
   parser.add_argument('trace_file', type=str, help='Path to trace')
   args = parser.parse_args()
 
@@ -166,8 +171,8 @@
                    stderr=subprocess.DEVNULL)
 
   traced = subprocess.Popen([os.path.join(args.out, 'traced')],
-                            stdout=subprocess.DEVNULL,
-                            stderr=subprocess.DEVNULL)
+                            stdout=None if args.verbose else subprocess.DEVNULL,
+                            stderr=None if args.verbose else subprocess.DEVNULL)
   print('Heap profile dump at max')
   heap_profile_run(args, dump_at_max=True)
   print('Heap profile dump at resting')