Script for generating list of startup descriptors

Change-Id: I09d4061db507df5346519be4b1aff2957bbb6ec0
diff --git a/tools/startup/adb_utils.py b/tools/startup/adb_utils.py
index cbcd724..41717cf 100644
--- a/tools/startup/adb_utils.py
+++ b/tools/startup/adb_utils.py
@@ -36,6 +36,24 @@
   cmd.extend(arguments if isinstance(arguments, list) else arguments.split(' '))
   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_output(cmd)
+  time.sleep(5)
+
+def check_app_has_profile_data(app_id, device_id=None):
+  profile_path = get_profile_path(app_id)
+  cmd = create_adb_cmd(
+      'shell du /data/misc/profiles/cur/0/%s/primary.prof' % app_id,
+      device_id)
+  stdout = subprocess.check_output(cmd).decode('utf-8').strip()
+  size_str = stdout[:stdout.index('\t')]
+  assert size_str.isdigit()
+  size = int(size_str)
+  if size == 4:
+    raise ValueError('Expected size of profile at %s to be > 4K' % profile_path)
+
 def clear_profile_data(app_id, device_id=None):
   cmd = create_adb_cmd(
       'shell cmd package compile --reset %s' % app_id, device_id)
@@ -56,6 +74,21 @@
       'shell cmd package compile -m speed-profile -f %s' % app_id, device_id)
   subprocess.check_call(cmd, stdout=DEVNULL, stderr=DEVNULL)
 
+def get_apk_path(app_id, device_id=None):
+  cmd = create_adb_cmd('shell pm path %s' % app_id, device_id)
+  stdout = subprocess.check_output(cmd).decode('utf-8').strip()
+  if not stdout.startswith('package:'):
+    raise ValueError(
+        'Expected stdout to start with "package:", was: %s' % stdout)
+  apk_path = stdout[len('package:'):]
+  if not apk_path.endswith('.apk'):
+    raise ValueError(
+        'Expected stdout to end with ".apk", was: %s' % stdout)
+  return apk_path
+
+def get_profile_path(app_id):
+  return '/data/misc/profiles/cur/0/%s/primary.prof' % app_id
+
 def get_minor_major_page_faults(app_id, device_id=None):
   pid = get_pid(app_id, device_id)
   cmd = create_adb_cmd('shell ps -p %i -o MINFL,MAJFL' % pid)
@@ -92,6 +125,42 @@
             % screen_state_value)
   return ScreenState[screen_state_value]
 
+def get_classes_and_methods_from_app_profile(app_id, device_id=None):
+  apk_path = get_apk_path(app_id, device_id)
+  profile_path = get_profile_path(app_id)
+
+  # Generates a list of class and method descriptors, prefixed with one or more
+  # flags 'H' (hot), 'S' (startup), 'P' (post startup).
+  #
+  # Example:
+  #
+  # HSPLandroidx/compose/runtime/ComposerImpl;->updateValue(Ljava/lang/Object;)V
+  # HSPLandroidx/compose/runtime/ComposerImpl;->updatedNodeCount(I)I
+  # HLandroidx/compose/runtime/ComposerImpl;->validateNodeExpected()V
+  # PLandroidx/compose/runtime/CompositionImpl;->applyChanges()V
+  # HLandroidx/compose/runtime/ComposerKt;->findLocation(Ljava/util/List;I)I
+  # Landroidx/compose/runtime/ComposerImpl;
+  #
+  # See also https://developer.android.com/studio/profile/baselineprofiles.
+  cmd = create_adb_cmd(
+    'shell profman --dump-classes-and-methods'
+    ' --profile-file=%s --apk=%s --dex-location=%s'
+        % (profile_path, apk_path, apk_path))
+  stdout = subprocess.check_output(cmd).decode('utf-8').strip()
+  lines = stdout.splitlines()
+  classes_and_methods = []
+  flags_to_name = { 'H': 'hot', 'S': 'startup', 'P': 'post_startup' }
+  for line in lines:
+    flags = { 'hot': False, 'startup': False, 'post_startup': False }
+    while line[0] in flags_to_name:
+      flag_abbreviation = line[0]
+      flag_name = flags_to_name.get(flag_abbreviation)
+      flags[flag_name] = True
+      line = line[1:]
+    assert line.startswith('L')
+    classes_and_methods.append({ 'descriptor': line, 'flags': flags })
+  return classes_and_methods
+
 def get_screen_off_timeout(device_id=None):
   cmd = create_adb_cmd(
       'shell settings get system screen_off_timeout', device_id)
@@ -119,6 +188,20 @@
       'Starting: Intent { cmp=%s/.%s }' % (app_id, activity[len(app_id)+1:]))
   assert stdout == expected_stdout, 'was %s' % stdout
 
+def prepare_for_interaction_with_device(device_id=None, device_pin=None):
+  # Increase screen off timeout to avoid device screen turns off.
+  twenty_four_hours_in_millis = 24 * 60 * 60 * 1000
+  previous_screen_off_timeout = get_screen_off_timeout(device_id)
+  set_screen_off_timeout(twenty_four_hours_in_millis, device_id)
+
+  # Unlock device.
+  unlock(device_id, device_pin)
+
+  tear_down_options = {
+    'previous_screen_off_timeout': previous_screen_off_timeout
+  }
+  return tear_down_options
+
 def root(device_id=None):
   cmd = create_adb_cmd('root', device_id)
   subprocess.check_call(cmd, stdout=DEVNULL, stderr=DEVNULL)
@@ -132,9 +215,15 @@
   assert len(stdout) == 0
 
 def stop_app(app_id, device_id=None):
-  cmd = create_adb_cmd('shell am force-stop %s' % apk, device_id)
+  cmd = create_adb_cmd('shell am force-stop %s' % app_id, device_id)
   subprocess.check_call(cmd, stdout=DEVNULL, stderr=DEVNULL)
 
+def tear_down_after_interaction_with_device(tear_down_options, device_id=None):
+  # Reset screen off timeout.
+  set_screen_off_timeout(
+      tear_down_options['previous_screen_off_timeout'],
+      device_id)
+
 def uninstall(app_id, device_id=None):
   cmd = create_adb_cmd('uninstall %s' % app_id, device_id)
   process_result = subprocess.run(cmd, capture_output=True)
diff --git a/tools/startup/generate_startup_descriptors.py b/tools/startup/generate_startup_descriptors.py
new file mode 100755
index 0000000..a3eb72c
--- /dev/null
+++ b/tools/startup/generate_startup_descriptors.py
@@ -0,0 +1,140 @@
+#!/usr/bin/env python3
+# Copyright (c) 2022, the R8 project authors. Please see the AUTHORS file
+# for details. All rights reserved. Use of this source code is governed by a
+# BSD-style license that can be found in the LICENSE file.
+
+import adb_utils
+
+import argparse
+import sys
+import time
+
+def extend_startup_descriptors(startup_descriptors, iteration, options):
+  generate_startup_profile_on_device(options)
+  classes_and_methods = adb_utils.get_classes_and_methods_from_app_profile(
+      options.app_id, options.device_id)
+  current_startup_descriptors = \
+      transform_classes_and_methods_to_r8_startup_descriptors(
+          classes_and_methods, options)
+  number_of_new_startup_descriptors = add_r8_startup_descriptors(
+      startup_descriptors, current_startup_descriptors)
+  if options.out is not None:
+    print(
+        'Found %i new startup descriptors in iteration %i'
+            % (number_of_new_startup_descriptors, iteration + 1))
+
+def generate_startup_profile_on_device(options):
+  if not options.use_existing_profile:
+    # Clear existing profile data.
+    adb_utils.clear_profile_data(options.app_id, options.device_id)
+
+    # Unlock device.
+    tear_down_options = adb_utils.prepare_for_interaction_with_device(
+        options.device_id, options.device_pin)
+
+    # Launch activity to generate startup profile on device.
+    adb_utils.launch_activity(
+        options.app_id, options.main_activity, options.device_id)
+
+    # Wait for activity startup.
+    time.sleep(options.startup_duration)
+
+    # Capture startup profile.
+    adb_utils.capture_app_profile_data(options.app_id, options.device_id)
+
+    # Shutdown app.
+    adb_utils.stop_app(options.app_id, options.device_id)
+
+    adb_utils.tear_down_after_interaction_with_device(
+        tear_down_options, options.device_id)
+
+  # Verify presence of profile.
+  adb_utils.check_app_has_profile_data(options.app_id, options.device_id)
+
+def transform_classes_and_methods_to_r8_startup_descriptors(
+    classes_and_methods, options):
+  startup_descriptors = []
+  for class_or_method in classes_and_methods:
+    descriptor = class_or_method.get('descriptor')
+    flags = class_or_method.get('flags')
+    if flags.get('post_startup') \
+        and not flags.get('startup') \
+        and not options.include_post_startup:
+      continue
+    startup_descriptors.append(descriptor)
+  return startup_descriptors
+
+def add_r8_startup_descriptors(startup_descriptors, startup_descriptors_to_add):
+  previous_number_of_startup_descriptors = len(startup_descriptors)
+  startup_descriptors.update(startup_descriptors_to_add)
+  new_number_of_startup_descriptors = len(startup_descriptors)
+  return new_number_of_startup_descriptors \
+      - previous_number_of_startup_descriptors
+
+def parse_options(argv):
+  result = argparse.ArgumentParser(
+      description='Generate a perfetto trace file.')
+  result.add_argument('--app-id',
+                      help='The application ID of interest',
+                      required=True)
+  result.add_argument('--device-id',
+                      help='Device id (e.g., emulator-5554).')
+  result.add_argument('--device-pin',
+                      help='Device pin code (e.g., 1234)')
+  result.add_argument('--include-post-startup',
+                      help='Include post startup classes and methods in the R8 '
+                           'startup descriptors',
+                      action='store_true',
+                      default=False)
+  result.add_argument('--iterations',
+                      help='Number of profiles to generate',
+                      default=1,
+                      type=int)
+  result.add_argument('--main-activity',
+                      help='Main activity class name')
+  result.add_argument('--out',
+                      help='File where to store startup descriptors (defaults '
+                           'to stdout)')
+  result.add_argument('--startup-duration',
+                      help='Duration in seconds before shutting down app',
+                      default=15,
+                      type=int)
+  result.add_argument('--until-stable',
+                      help='Repeat profile generation until no new startup '
+                           'descriptors are found',
+                      action='store_true',
+                      default=False)
+  result.add_argument('--use-existing-profile',
+                      help='Do not launch app to generate startup profile',
+                      action='store_true',
+                      default=False)
+  options, args = result.parse_known_args(argv)
+  assert options.main_activity is not None or options.use_existing_profile, \
+      'Argument --main-activity is required except when running with ' \
+      '--use-existing-profile.'
+  return options, args
+
+def main(argv):
+  (options, args) = parse_options(argv)
+  startup_descriptors = set()
+  if options.until_stable:
+    iteration = 0
+    while True:
+      diff = extend_startup_descriptors(startup_descriptors, iteration, options)
+      if diff == 0:
+        break
+      iteration = iteration + 1
+  else:
+    for iteration in range(options.iterations):
+      extend_startup_descriptors(startup_descriptors, iteration, options)
+  if options.out is not None:
+    with open(options.out, 'w') as f:
+      for startup_descriptor in startup_descriptors:
+        f.write(startup_descriptor)
+        f.write('\n')
+  else:
+    for startup_descriptor in startup_descriptors:
+      print(startup_descriptor)
+
+if __name__ == '__main__':
+  sys.exit(main(sys.argv[1:]))
diff --git a/tools/startup/trace_generator.py b/tools/startup/trace_generator.py
index 0b36b64..b4a4089 100755
--- a/tools/startup/trace_generator.py
+++ b/tools/startup/trace_generator.py
@@ -203,9 +203,11 @@
 def main(argv):
   (options, args) = parse_options(argv)
   with utils.TempDir() as tmp_dir:
-    tear_down_options = setup(options)
+    tear_down_options = adb_utils.prepare_for_interaction_with_device(
+        options.device_id, options.device_pin)
     run_all(options, tmp_dir)
-    tear_down(options, tear_down_options)
+    adb_utils.tear_down_after_interaction_with_device(
+        tear_down_options, options.device_id)
 
 if __name__ == '__main__':
   sys.exit(main(sys.argv[1:]))
\ No newline at end of file