Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / tools / android / mempressure.py
blobfa3daba3be6badd9a33ae7df108f6b9c9a4b6df0
1 #!/usr/bin/env python
2 # Copyright 2013 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 import collections
7 import logging
8 import optparse
9 import os
10 import sys
12 BUILD_ANDROID_DIR = os.path.join(os.path.dirname(__file__),
13 os.pardir,
14 os.pardir,
15 'build',
16 'android')
17 sys.path.append(BUILD_ANDROID_DIR)
18 from pylib import android_commands
19 from pylib import constants
20 from pylib import flag_changer
21 from pylib.device import device_errors
22 from pylib.device import device_utils
24 # Browser Constants
25 DEFAULT_BROWSER = 'chrome'
27 # Action Constants
28 ACTION_PACKAGE = 'org.chromium.base'
29 ACTION_TRIM = {
30 'moderate' : ACTION_PACKAGE + '.ACTION_TRIM_MEMORY_MODERATE',
31 'critical' : ACTION_PACKAGE + '.ACTION_TRIM_MEMORY_RUNNING_CRITICAL',
32 'complete' : ACTION_PACKAGE + '.ACTION_TRIM_MEMORY'
34 ACTION_LOW = ACTION_PACKAGE + '.ACTION_LOW_MEMORY'
36 # Command Line Constants
37 ENABLE_TEST_INTENTS_FLAG = '--enable-test-intents'
39 def main(argv):
40 option_parser = optparse.OptionParser()
41 option_parser.add_option('-l',
42 '--low',
43 help='Simulate Activity#onLowMemory()',
44 action='store_true')
45 option_parser.add_option('-t',
46 '--trim',
47 help=('Simulate Activity#onTrimMemory(...) with ' +
48 ', '.join(ACTION_TRIM.keys())),
49 type='string')
50 option_parser.add_option('-b',
51 '--browser',
52 default=DEFAULT_BROWSER,
53 help=('Which browser to use. One of ' +
54 ', '.join(constants.PACKAGE_INFO.keys()) +
55 ' [default: %default]'),
56 type='string')
58 (options, args) = option_parser.parse_args(argv)
60 if len(args) > 1:
61 print 'Unknown argument: ', args[1:]
62 option_parser.print_help()
63 sys.exit(1)
65 if options.low and options.trim:
66 option_parser.error('options --low and --trim are mutually exclusive')
68 if not options.low and not options.trim:
69 option_parser.print_help()
70 sys.exit(1)
72 action = None
73 if options.low:
74 action = ACTION_LOW
75 elif options.trim in ACTION_TRIM.keys():
76 action = ACTION_TRIM[options.trim]
78 if action is None:
79 option_parser.print_help()
80 sys.exit(1)
82 if not options.browser in constants.PACKAGE_INFO.keys():
83 option_parser.error('Unknown browser option ' + options.browser)
85 package_info = constants.PACKAGE_INFO[options.browser]
87 package = package_info.package
88 activity = package_info.activity
90 devices = android_commands.GetAttachedDevices()
91 if not devices:
92 raise device_errors.NoDevicesError()
93 elif len(devices) > 1:
94 logging.warning('Multiple devices attached. Using %s.' % devices[0])
95 device = device_utils.DeviceUtils(devices[0])
97 try:
98 device.EnableRoot()
99 except device_errors.CommandFailedError as e:
100 # Try to change the flags and start the activity anyway.
101 # TODO(jbudorick) Handle this exception appropriately after interface
102 # conversions are finished.
103 logging.error(str(e))
104 flags = flag_changer.FlagChanger(device, package_info.cmdline_file)
105 if ENABLE_TEST_INTENTS_FLAG not in flags.Get():
106 flags.AddFlags([ENABLE_TEST_INTENTS_FLAG])
108 device.StartActivity(intent.Intent(package=package, activity=activity,
109 action=action))
111 if __name__ == '__main__':
112 sys.exit(main(sys.argv))