Flush profiles for all matching processes in startup script

Change-Id: Ie303e929ea99ef9c1f15c07fb142d3e9eb547764
diff --git a/tools/startup/adb_utils.py b/tools/startup/adb_utils.py
index a376018..3900ffc 100755
--- a/tools/startup/adb_utils.py
+++ b/tools/startup/adb_utils.py
@@ -85,10 +85,23 @@
   return cmd
 
 def capture_app_profile_data(app_id, device_id=None):
-  cmd = create_adb_cmd(
-      'shell killall -s SIGUSR1 %s' % app_id, device_id)
-  subprocess.check_call(cmd, stdout=DEVNULL, stderr=DEVNULL)
-  time.sleep(5)
+  ps_cmd = create_adb_cmd('shell ps -o NAME', device_id)
+  stdout = subprocess.check_output(ps_cmd).decode('utf-8').strip()
+  killed_any = False
+  for process_name in stdout.splitlines():
+    if process_name.startswith(app_id):
+      print('Flushing profile for process %s' % process_name)
+      killall_cmd = create_adb_cmd(
+          'shell killall -s SIGUSR1 %s' % process_name, device_id)
+      killall_result = subprocess.run(killall_cmd, capture_output=True)
+      stdout = killall_result.stdout.decode('utf-8')
+      stderr = killall_result.stderr.decode('utf-8')
+      if killall_result.returncode == 0:
+        killed_any = True
+      else:
+        print('Error: stdout: %s, stderr: %s' % (stdout, stderr))
+      time.sleep(5)
+  assert killed_any, 'Expected to find at least one process'
 
 def check_app_has_profile_data(app_id, device_id=None):
   profile_path = get_profile_path(app_id)