aboutsummaryrefslogtreecommitdiff
path: root/tools/utils.py
blob: 39daa7e4d40b5daa80ad06eae88d96622d371ff2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#    Copyright 2015 Linaro Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import argparse
import fnmatch
import json
import os
import pickle
import subprocess
import sys
import time
import traceback


dir_tools = os.path.dirname(os.path.realpath(__file__))
dir_root = os.path.realpath(os.path.join(dir_tools, '..'))
dir_benchmarks = os.path.join(dir_root, 'benchmarks')
dir_out = os.path.join(dir_root, 'out')
dir_build = os.path.join(dir_out, 'build')
dir_build_java_classes = os.path.join(dir_build, 'classes')
dir_framework = os.path.join(dir_root, 'framework')


# Constant shared values that should not be modified.
si_factors = {'m' : 0.001, 'n' : 0.000000001, 'u' : 0.000001}
adb_default_target_string = '<default>'
adb_default_target_copy_path = '/data/local/tmp'
default_mode = ''
default_compiler_mode = None
default_n_iterations = 1

# Printing helpers.

redirected_output = not sys.stdout.isatty()
verbose = True

def ColourCode(colour):
  return '' if redirected_output else colour

COLOUR_GREEN = ColourCode("\x1b[0;32m")
COLOUR_ORANGE = ColourCode("\x1b[0;33m")
COLOUR_RED = ColourCode("\x1b[0;31m")
NO_COLOUR = ColourCode("\x1b[0m")

def Warning(message):
    print(COLOUR_ORANGE + 'WARNING: ' + message + NO_COLOUR,
          file=sys.stderr)
    traceback.print_stack()

def Error(message, rc=1):
    print(COLOUR_RED + 'ERROR: ' + message + NO_COLOUR,
          file=sys.stderr)
    traceback.print_stack()
    sys.exit(rc)

def VerbosePrint(message):
    if verbose:
        print(message)



def ensure_dir(path):
    if path == '':
        # This can happen when a user refers to the current working directory.
        return
    try:
        if os.path.exists(path):
            if not os.path.isdir(path):
                Error('`%s` exists but is not a directory.' % path)
        else:
            os.makedirs(path)
    except:
        Error('Failed to ensure the directory `%s` exists.' % path)

def GetTimeValue(value, si_prefix):
    return value * si_factors[si_prefix] if si_prefix else value

def PrettySIFactor(value):
    si_factor = float('inf')
    si_prefix = ''

    for i in si_factors.items():
        if i[1] < si_factor and value < i[1] * 1000:
            si_factor = i[1]
            si_prefix = i[0]

    return si_factor, si_prefix

def NameMatchesAnyFilter(name, filters):
    # Ensure we have a list of filters. This lets the function work if only one
    # filter is passed as a string.
    filters = list(filters)
    for f in filters:
        if fnmatch.fnmatch(name, f):
            return True
    return False

# Wrapper around `subprocess.Popen` returning the output of the given command.
def Command(command, command_string=None, exit_on_error=True, cwd=None):
    if not command_string:
        command_string = ' '.join(command)
    if cwd:
        command_string = 'cd ' + cwd + ' && ' + command_string
    VerbosePrint(command_string)
    p = subprocess.Popen(command,
                         cwd=cwd,
                         stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    outerr, empty_err = p.communicate()
    outerr = outerr.decode()
    rc = p.returncode
    if rc:
        message = 'Command failed:\n' + command_string + '\n' + outerr
        if exit_on_error:
            Error(message)
        else:
            Warning(message)
    return rc, outerr



class SetVerbosity(argparse.Action):
    def __init__(self, option_strings, dest, nargs, **kwargs):
        super(SetVerbosity, self).__init__(option_strings, dest, nargs, **kwargs)
    def __call__(self, parser, namespace, values, option_string):
        global verbose
        verbose = False

# Common arguments for `run` scripts.
def AddCommonRunOptions(parser):
    opts = parser.add_argument_group('options common to all `run` scripts')
    opts.add_argument('--iterations', '-i',
                      type=int,
                      default=default_n_iterations,
                      help="The number of iterations to run.")
    opts.add_argument('--target', '-t',
                      nargs='?', default=None, const=adb_default_target_string,
                      help='Run on target adb device.')
    opts.add_argument('--mode',
                      choices = ['32', '64'], default = default_mode,
                      help='''When specified, force using the 32bit or 64bit
                      architecture instead of the target primary architecture.
                      This is only valid when running on target.''')
    opts.add_argument('--target-copy-path',
                      default = adb_default_target_copy_path,
                      help = '''Path where objects should be copied on the
                      target.''')
    opts.add_argument('--noverbose', action=SetVerbosity, nargs=0,
                      help='Do not print extra information and commands run.')
    opts.add_argument('--compiler-mode', choices=['optimizing', 'quick'],
                      default=default_compiler_mode,
                      help='''The compiler to use on target. When this option is
                      not specified no additional arguments are passed so the
                      default compiler on the target is used.''')

def ValidateCommonRunOptions(args):
    options_requiring_target_mode = ['mode', 'compiler-mode']
    if not args.target:
        for opt in options_requiring_target_mode:
            if getattr(args, opt.replace('-', '_')):
                Error('The `--%s` option is only valid when `--target` is specified.' % opt)

# Returns a list of `dex2oat` options for the compiler.
def GetDex2oatOptions(compiler_mode):
    if compiler_mode is None:
        return []
    elif compiler_mode == 'optimizing':
        return ['--compiler-backend=Optimizing']
    elif compiler_mode == 'quick':
        return ['--compiler-backend=Quick']
    else:
        Error('Unsupported compiler mode: ' + compiler_mode)


default_output_formats = ['pkl', 'json']

def OutputObject(object, format, output_filename):
    if format not in default_output_formats:
        Error('Unexpected format: ' + format)
    else:
        ensure_dir(os.path.dirname(output_filename))
        if format == 'pkl':
            with open(output_filename, 'wb') as output_file:
                # Create a python2-compatible pickle dump.
                pickle.dump(object, output_file, 2)
                print('Wrote results to %s.' % output_filename)
        elif format == 'json':
            with open(output_filename, 'w') as output_file:
                print(json.dumps(object), file=output_file)
                print('Wrote results to %s.' % output_filename)

def AddOutputFormatOptions(parser, formats=default_output_formats):
    opts = parser.add_argument_group('output formats')
    format_output_filename = os.path.relpath(
        os.path.join(dir_out,
                     '{type}',
                     time.strftime("%Y.%m.%d-%H:%M:%S") + '.{type}'))
    format_help_message = 'Dump results to the given file in {type} format.'
    for f in formats:
        const_output_filename = format_output_filename.format(type=f)
        opts.add_argument('--output-%s' % f,
                          nargs='?', default=const_output_filename,
                          metavar='FILE',
                          help=format_help_message.format(type=f))


# Common arguments for `compare` scripts.
def AddCommonCompareOptions(parser):
    parser.add_argument('res_1', metavar = 'res_1.pkl')
    parser.add_argument('res_2', metavar = 'res_2.pkl')

def CheckDependencies(dependencies):
    for d in dependencies:
        rc, err = Command(['which', d], exit_on_error=False)

        if rc:
            Error("Couldn't find `" + d + "`.")