profiling: Wait for perfetto cmd to terminate in script.

Change-Id: I01cf89fd144c5f67695b5a13b68f1d3b8b087c13
diff --git a/tools/heap_profile b/tools/heap_profile
index 5744477..ef49ffe 100755
--- a/tools/heap_profile
+++ b/tools/heap_profile
@@ -179,6 +179,12 @@
     # Not check_call because it could have existed in the meantime.
     subprocess.call(['adb', 'shell', 'kill', '-INT', perfetto_pid])
 
+  # Wait for perfetto cmd to return.
+  while exists:
+    exists = subprocess.call(
+        ['adb', 'shell', '[ -d /proc/{} ]'.format(perfetto_pid)]) == 0
+    time.sleep(1)
+
   subprocess.check_call(['adb', 'pull', '/data/misc/perfetto-traces/profile',
                          '/tmp/profile'], stdout=NULL)
   trace_to_text_output = subprocess.check_output(