blob: cc2c9010cba05c18c2d88e9d612638e220845e1a [file] [log] [blame]
Rahul Chaudhryd7444b72015-12-22 16:12:07 -08001#!/usr/bin/python2
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -08002
3# Copyright 2011 Google Inc. All Rights Reserved.
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -08004"""The driver script for running performance benchmarks on ChromeOS."""
5
Rahul Chaudhrycbc5a262015-12-30 17:05:14 -08006from __future__ import print_function
7
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -08008import atexit
9import optparse
10import os
Luis Lozano45b53c52015-09-30 11:36:27 -070011import signal
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080012import sys
13from experiment_runner import ExperimentRunner
14from experiment_runner import MockExperimentRunner
15from experiment_factory import ExperimentFactory
16from experiment_file import ExperimentFile
17from help import Help
18from settings_factory import GlobalSettings
19from utils import logger
20
Ahmad Sharif4467f002012-12-20 12:09:49 -080021import test_flag
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080022
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080023
24class MyIndentedHelpFormatter(optparse.IndentedHelpFormatter):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080025
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080026 def format_description(self, description):
27 return description
28
29
30def SetupParserOptions(parser):
31 """Add all options to the parser."""
Luis Lozanof2a3ef42015-12-15 13:49:30 -080032 parser.add_option('--dry_run',
33 dest='dry_run',
34 help=('Parse the experiment file and '
35 'show what will be done'),
36 action='store_true',
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080037 default=False)
38 # Allow each of the global fields to be overridden by passing in
39 # options. Add each global field as an option.
Luis Lozanof2a3ef42015-12-15 13:49:30 -080040 option_settings = GlobalSettings('')
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080041 for field_name in option_settings.fields:
42 field = option_settings.fields[field_name]
Luis Lozanof2a3ef42015-12-15 13:49:30 -080043 parser.add_option('--%s' % field.name,
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080044 dest=field.name,
45 help=field.description,
Luis Lozanof2a3ef42015-12-15 13:49:30 -080046 action='store')
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080047
48
49def ConvertOptionsToSettings(options):
50 """Convert options passed in into global settings."""
Luis Lozanof2a3ef42015-12-15 13:49:30 -080051 option_settings = GlobalSettings('option_settings')
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080052 for option_name in options.__dict__:
53 if (options.__dict__[option_name] is not None and
54 option_name in option_settings.fields):
55 option_settings.SetField(option_name, options.__dict__[option_name])
56 return option_settings
57
58
59def Cleanup(experiment):
60 """Handler function which is registered to the atexit handler."""
61 experiment.Cleanup()
62
63
Luis Lozano45b53c52015-09-30 11:36:27 -070064def CallExitHandler(signum, _):
65 """Signal handler that transforms a signal into a call to exit.
66
67 This is useful because functionality registered by "atexit" will
68 be called. It also means you can "catch" the signal by catching
69 the SystemExit exception.
70 """
71 sys.exit(128 + signum)
72
73
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080074def Main(argv):
75 parser = optparse.OptionParser(usage=Help().GetUsage(),
76 description=Help().GetHelp(),
77 formatter=MyIndentedHelpFormatter(),
Luis Lozanof2a3ef42015-12-15 13:49:30 -080078 version='%prog 3.0')
Luis Lozanof81680c2013-03-15 14:44:13 -070079
Luis Lozanof2a3ef42015-12-15 13:49:30 -080080 parser.add_option('--noschedv2',
81 dest='noschedv2',
Han Shen43494292015-09-14 10:26:40 -070082 default=False,
Luis Lozanof2a3ef42015-12-15 13:49:30 -080083 action='store_true',
84 help=('Do not use new scheduler. '
85 'Use original scheduler instead.'))
86 parser.add_option('-l',
87 '--log_dir',
88 dest='log_dir',
89 default='',
90 help='The log_dir, default is under <crosperf_logs>/logs')
Luis Lozanof81680c2013-03-15 14:44:13 -070091
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080092 SetupParserOptions(parser)
93 options, args = parser.parse_args(argv)
94
95 # Convert the relevant options that are passed in into a settings
96 # object which will override settings in the experiment file.
97 option_settings = ConvertOptionsToSettings(options)
Luis Lozanof81680c2013-03-15 14:44:13 -070098 log_dir = os.path.abspath(os.path.expanduser(options.log_dir))
99 logger.GetLogger(log_dir)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800100
101 if len(args) == 2:
102 experiment_filename = args[1]
103 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800104 parser.error('Invalid number arguments.')
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800105
106 working_directory = os.getcwd()
Ahmad Sharif4467f002012-12-20 12:09:49 -0800107 if options.dry_run:
108 test_flag.SetTestMode(True)
109
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800110 experiment_file = ExperimentFile(
111 open(experiment_filename, 'rb'), option_settings)
112 if not experiment_file.GetGlobalSettings().GetField('name'):
Ahmad Sharif822c55d2012-02-08 20:55:47 -0800113 experiment_name = os.path.basename(experiment_filename)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800114 experiment_file.GetGlobalSettings().SetField('name', experiment_name)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800115 experiment = ExperimentFactory().GetExperiment(experiment_file,
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800116 working_directory, log_dir)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800117
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800118 json_report = experiment_file.GetGlobalSettings().GetField('json_report')
Caroline Ticeef4ca8a2015-08-25 12:53:38 -0700119
Luis Lozano45b53c52015-09-30 11:36:27 -0700120 signal.signal(signal.SIGTERM, CallExitHandler)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800121 atexit.register(Cleanup, experiment)
122
123 if options.dry_run:
Caroline Tice6e8726d2015-12-09 12:42:13 -0800124 runner = MockExperimentRunner(experiment, json_report)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800125 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800126 runner = ExperimentRunner(experiment,
127 json_report,
Han Shene205d902015-09-15 11:04:05 -0700128 using_schedv2=(not options.noschedv2))
Han Shenba649282015-08-05 17:19:55 -0700129
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800130 runner.Run()
131
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800132
133if __name__ == '__main__':
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800134 Main(sys.argv)