aboutsummaryrefslogtreecommitdiff
path: root/deprecated/build_benchmarks.py
blob: c10c74d1d19afef9ed97eee2f24ae85e43471012 (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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/python2
#
# Copyright 2010 Google Inc. All Rights Reserved.
"""Script to build ChromeOS benchmarks

Inputs:
    chromeos_root
    toolchain_root
    board
    [chromeos/cpu/<benchname> |
     chromeos/browser/[pagecycler|sunspider] |
     chromeos/startup]

    This script assumes toolchain has already been built in toolchain_root.

    chromeos/cpu/<benchname>
       - Execute bench.py script within chroot to build benchmark
       - Copy build results to perflab-bin

    chromeos/startup
       - Call build_chromeos to build image.
       - Copy image to perflab-bin

    chromeos/browser/*
       - Call build_chromebrowser to build image with new browser
       - Copy image to perflab-bin

"""

from __future__ import print_function

__author__ = 'bjanakiraman@google.com (Bhaskar Janakiraman)'

import argparse
import os
import sys
import re

import build_chromeos
from cros_utils import command_executer
from cros_utils import logger

KNOWN_BENCHMARKS = [
    'chromeos/startup', 'chromeos/browser/pagecycler',
    'chromeos/browser/sunspider', 'chromeos/browser/v8bench',
    'chromeos/cpu/bikjmp'
]

# Commands to build CPU benchmarks.

CPU_BUILDCMD_CLEAN = ('cd /usr/local/toolchain_root/third_party/android_bench/'
                      'v2_0/CLOSED_SOURCE/%s; python ../../scripts/bench.py '
                      '--toolchain=/usr/bin --action=clean;')

CPU_BUILDCMD_BUILD = ('cd /usr/local/toolchain_root/third_party/android_bench/'
                      'v2_0/CLOSED_SOURCE/%s; python ../../scripts/bench.py '
                      '--toolchain=/usr/bin --add_cflags=%s --add_ldflags=%s '
                      '--makeopts=%s --action=build')

# Common initializations
cmd_executer = command_executer.GetCommandExecuter()


def Usage(parser, message):
  print('ERROR: %s' % message)
  parser.print_help()
  sys.exit(0)


def CreateRunsh(destdir, benchmark):
  """Create run.sh script to run benchmark.

  Perflab needs a run.sh that runs the benchmark.
  """
  run_cmd = os.path.dirname(os.path.abspath(__file__)) + '/run_benchmarks.py'
  contents = '#!/bin/sh\n%s $@ %s\n' % (run_cmd, benchmark)
  runshfile = destdir + '/run.sh'
  f = open(runshfile, 'w')
  f.write(contents)
  f.close()
  retval = cmd_executer.RunCommand('chmod +x %s' % runshfile)
  return retval


def CreateBinaryCopy(sourcedir, destdir, copy=None):
  """Create links in perflab-bin/destdir/* to sourcedir/*, instead of copies

  Args:
    sourcedir: directory from which to copy.
    destdir: directory to which to copy.
    copy: when none, make soft links to everything under sourcedir, otherwise
          copy all to destdir.
          TODO: remove this parameter if it's determined that CopyFiles can use
                rsync -L.
  """
  retval = 0
  # check if sourcedir exists
  if not os.path.exists(sourcedir):
    logger.GetLogger().LogError('benchmark results %s does not exist.' %
                                sourcedir)
    return 1

  # Deal with old copies - save off old ones for now.
  # Note - if its a link, it doesn't save anything.
  if os.path.exists(destdir):
    command = 'rm -rf %s.old' % destdir
    retval = cmd_executer.RunCommand(command)
    if retval != 0:
      return retval
    command = 'mv %s %s.old' % (destdir, destdir)
    retval = cmd_executer.RunCommand(command)
    if retval != 0:
      return retval
  os.makedirs(destdir)
  sourcedir = os.path.abspath(sourcedir)
  if copy is None:
    command = 'ln -s %s/* %s' % (sourcedir, destdir)
  else:
    command = 'cp -fr %s/* %s' % (sourcedir, destdir)
  retval = cmd_executer.RunCommand(command)
  return retval


def Main(argv):
  """Build ChromeOS."""
  # Common initializations

  parser = argparse.ArgumentParser()
  parser.add_argument('-c',
                      '--chromeos_root',
                      dest='chromeos_root',
                      help='Target directory for ChromeOS installation.')
  parser.add_argument('-t',
                      '--toolchain_root',
                      dest='toolchain_root',
                      help='This is obsolete. Do not use.')
  parser.add_argument('-r',
                      '--third_party',
                      dest='third_party',
                      help='The third_party dir containing android '
                           'benchmarks.')
  parser.add_argument('-C',
                      '--clean',
                      dest='clean',
                      action='store_true',
                      default=False,
                      help='Clean up build.')
  parser.add_argument('-B',
                      '--build',
                      dest='build',
                      action='store_true',
                      default=False,
                      help='Build benchmark.')
  parser.add_argument('-O',
                      '--only_copy',
                      dest='only_copy',
                      action='store_true',
                      default=False,
                      help='Only copy to perflab-bin - no builds.')
  parser.add_argument('--workdir',
                      dest='workdir',
                      default='.',
                      help='Work directory for perflab outputs.')
  parser.add_argument('--clobber_chroot',
                      dest='clobber_chroot',
                      action='store_true',
                      help='Delete the chroot and start fresh',
                      default=False)
  parser.add_argument('--clobber_board',
                      dest='clobber_board',
                      action='store_true',
                      help='Delete the board and start fresh',
                      default=False)
  parser.add_argument('--cflags',
                      dest='cflags',
                      default='',
                      help='CFLAGS for the ChromeOS packages')
  parser.add_argument('--cxxflags',
                      dest='cxxflags',
                      default='',
                      help='CXXFLAGS for the ChromeOS packages')
  parser.add_argument('--ldflags',
                      dest='ldflags',
                      default='',
                      help='LDFLAGS for the ChromeOS packages')
  parser.add_argument('--makeopts',
                      dest='makeopts',
                      default='',
                      help='Make options for the ChromeOS packages')
  parser.add_argument('--board',
                      dest='board',
                      help='ChromeOS target board, e.g. x86-generic')
  # Leftover positional arguments
  parser.add_argument('args', nargs='+', help='benchmarks')

  options = parser.parse_args(argv[1:])

  # validate args
  for arg in options.args:
    if arg not in KNOWN_BENCHMARKS:
      logger.GetLogger().LogFatal('Bad benchmark %s specified' % arg)

  if options.chromeos_root is None:
    Usage(parser, '--chromeos_root must be set')

  if options.board is None:
    Usage(parser, '--board must be set')

  if options.toolchain_root:
    logger.GetLogger().LogWarning('--toolchain_root should not be set')

  options.chromeos_root = os.path.expanduser(options.chromeos_root)
  options.workdir = os.path.expanduser(options.workdir)

  retval = 0
  if options.third_party:
    third_party = options.third_party
  else:
    third_party = '%s/../../../third_party' % os.path.dirname(__file__)
    third_party = os.path.realpath(third_party)
  for arg in options.args:
    # CPU benchmarks
    if re.match('chromeos/cpu', arg):
      comps = re.split('/', arg)
      benchname = comps[2]

      tec_options = []
      if third_party:
        tec_options.append('--third_party=%s' % third_party)
      if options.clean:
        retval = cmd_executer.ChrootRunCommand(options.chromeos_root,
                                               CPU_BUILDCMD_CLEAN % benchname,
                                               tec_options=tec_options)
        logger.GetLogger().LogErrorIf(retval,
                                      'clean of benchmark %s failed.' % arg)
      if options.build:
        retval = cmd_executer.ChrootRunCommand(
            options.chromeos_root,
            CPU_BUILDCMD_BUILD % (benchname, options.cflags, options.ldflags,
                                  options.makeopts),
            tec_options=tec_options)
        logger.GetLogger().LogErrorIf(retval,
                                      'Build of benchmark %s failed.' % arg)
      if retval == 0 and (options.build or options.only_copy):
        benchdir = ('%s/android_bench/v2_0/CLOSED_SOURCE/%s' %
                    (third_party, benchname))
        linkdir = '%s/perflab-bin/%s' % (options.workdir, arg)

        # For cpu/*, we need to copy (not symlinks) of all the contents,
        # because they are part of the test fixutre.
        retval = CreateBinaryCopy(benchdir, linkdir, True)
        if retval != 0:
          return retval
        retval = CreateRunsh(linkdir, arg)
        if retval != 0:
          return retval
    elif re.match('chromeos/startup', arg):
      if options.build:
        # Clean for chromeos/browser and chromeos/startup is a Nop
        # since builds are always from scratch.
        build_args = [
            os.path.dirname(os.path.abspath(__file__)) + '/build_chromeos.py',
            '--chromeos_root=' + options.chromeos_root,
            '--board=' + options.board, '--cflags=' + options.cflags,
            '--cxxflags=' + options.cxxflags, '--ldflags=' + options.ldflags,
            '--clobber_board'
        ]
        retval = build_chromeos.Main(build_args)
        logger.GetLogger().LogErrorIf(retval, 'Build of ChromeOS failed.')
      if retval == 0 and (options.build or options.only_copy):
        benchdir = '%s/src/build/images/%s/latest' % (options.chromeos_root,
                                                      options.board)
        linkdir = '%s/perflab-bin/%s' % (options.workdir, arg)
        retval = CreateBinaryCopy(benchdir, linkdir)
        if retval != 0:
          return retval
        CreateRunsh(linkdir, arg)
        if retval != 0:
          return retval
    elif re.match('chromeos/browser', arg):
      if options.build:
        # For now, re-build os. TBD: Change to call build_browser
        build_args = [os.path.dirname(os.path.abspath(__file__)) +
                      '/build_chrome_browser.py',
                      '--chromeos_root=' + options.chromeos_root,
                      '--board=' + options.board, '--cflags=' + options.cflags,
                      '--cxxflags=' + options.cxxflags,
                      '--ldflags=' + options.ldflags]
        retval = build_chromeos.Main(build_args)
        logger.GetLogger().LogErrorIf(retval, 'Build of ChromeOS failed.')
      if retval == 0 and (options.build or options.only_copy):
        benchdir = '%s/src/build/images/%s/latest' % (options.chromeos_root,
                                                      options.board)
        linkdir = '%s/perflab-bin/%s' % (options.workdir, arg)
        retval = CreateBinaryCopy(benchdir, linkdir)
        if retval != 0:
          return retval
        retval = CreateRunsh(linkdir, arg)
        if retval != 0:
          return retval

  return 0


if __name__ == '__main__':
  sys.exit(Main(sys.argv))