1#!/usr/bin/env python3
2#
3# Copyright 2019, The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17import argparse
18import os
19import sys
20from typing import List
21
22DIR = os.path.abspath(os.path.dirname(__file__))
23sys.path.append(os.path.dirname(DIR))  # framework/base/startop/script
24import lib.print_utils as print_utils
25import iorap.lib.iorapd_utils as iorapd_utils
26from app_startup.lib.app_runner import AppRunner
27
28IORAP_COMMON_BASH_SCRIPT = os.path.join(DIR, 'common')
29
30def parse_options(argv: List[str] = None):
31  """Parses command line arguments and returns an argparse Namespace object."""
32  parser = argparse.ArgumentParser(description="Compile perfetto trace file")
33  required_named = parser.add_argument_group('required named arguments')
34
35  required_named.add_argument('-i', dest='inodes', metavar='FILE',
36                              help='Read cached inode data from a file saved '
37                                   'earlier with pagecache.py -d')
38  required_named.add_argument('-p', dest='package',
39                              help='Package of the app to be compiled')
40
41  optional_named = parser.add_argument_group('optional named arguments')
42  optional_named.add_argument('-o', dest='output',
43                              help='The compiled trace is stored into the output file')
44  optional_named.add_argument('-a', dest='activity',
45                              help='Activity of the app to be compiled')
46  optional_named.add_argument('-d', dest='debug', action='store_true'
47                              , help='Activity of the app to be compiled')
48
49  return parser.parse_args(argv)
50
51def main(inodes, package, activity, output, **kwargs) -> int:
52  """Entries of the program."""
53  if not activity:
54    activity = AppRunner.get_activity(package)
55
56  passed = iorapd_utils.compile_perfetto_trace_on_device(package, activity,
57                                                         inodes)
58  if passed and output:
59    iorapd_utils.get_iorapd_compiler_trace(package, activity, output)
60
61  return 0
62
63if __name__ == '__main__':
64  opts = parse_options()
65  if opts.debug:
66    print_utils.DEBUG = opts.debug
67  print_utils.debug_print(opts)
68  sys.exit(main(**(vars(opts))))
69