blob: 28e78f5bca21182d4ace7a07c70ab94b56046dfc [file] [log] [blame]
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -08001#!/usr/bin/python
2
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
6import atexit
7import optparse
8import os
Luis Lozano45b53c52015-09-30 11:36:27 -07009import signal
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080010import sys
11from experiment_runner import ExperimentRunner
12from experiment_runner import MockExperimentRunner
13from experiment_factory import ExperimentFactory
14from experiment_file import ExperimentFile
15from help import Help
16from settings_factory import GlobalSettings
17from utils import logger
18
Ahmad Sharif4467f002012-12-20 12:09:49 -080019import test_flag
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080020
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080021
22class MyIndentedHelpFormatter(optparse.IndentedHelpFormatter):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080023
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080024 def format_description(self, description):
25 return description
26
27
28def SetupParserOptions(parser):
29 """Add all options to the parser."""
Luis Lozanof2a3ef42015-12-15 13:49:30 -080030 parser.add_option('--dry_run',
31 dest='dry_run',
32 help=('Parse the experiment file and '
33 'show what will be done'),
34 action='store_true',
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080035 default=False)
36 # Allow each of the global fields to be overridden by passing in
37 # options. Add each global field as an option.
Luis Lozanof2a3ef42015-12-15 13:49:30 -080038 option_settings = GlobalSettings('')
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080039 for field_name in option_settings.fields:
40 field = option_settings.fields[field_name]
Luis Lozanof2a3ef42015-12-15 13:49:30 -080041 parser.add_option('--%s' % field.name,
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080042 dest=field.name,
43 help=field.description,
Luis Lozanof2a3ef42015-12-15 13:49:30 -080044 action='store')
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080045
46
47def ConvertOptionsToSettings(options):
48 """Convert options passed in into global settings."""
Luis Lozanof2a3ef42015-12-15 13:49:30 -080049 option_settings = GlobalSettings('option_settings')
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080050 for option_name in options.__dict__:
51 if (options.__dict__[option_name] is not None and
52 option_name in option_settings.fields):
53 option_settings.SetField(option_name, options.__dict__[option_name])
54 return option_settings
55
56
57def Cleanup(experiment):
58 """Handler function which is registered to the atexit handler."""
59 experiment.Cleanup()
60
61
Luis Lozano45b53c52015-09-30 11:36:27 -070062def CallExitHandler(signum, _):
63 """Signal handler that transforms a signal into a call to exit.
64
65 This is useful because functionality registered by "atexit" will
66 be called. It also means you can "catch" the signal by catching
67 the SystemExit exception.
68 """
69 sys.exit(128 + signum)
70
71
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080072def Main(argv):
73 parser = optparse.OptionParser(usage=Help().GetUsage(),
74 description=Help().GetHelp(),
75 formatter=MyIndentedHelpFormatter(),
Luis Lozanof2a3ef42015-12-15 13:49:30 -080076 version='%prog 3.0')
Luis Lozanof81680c2013-03-15 14:44:13 -070077
Luis Lozanof2a3ef42015-12-15 13:49:30 -080078 parser.add_option('--noschedv2',
79 dest='noschedv2',
Han Shen43494292015-09-14 10:26:40 -070080 default=False,
Luis Lozanof2a3ef42015-12-15 13:49:30 -080081 action='store_true',
82 help=('Do not use new scheduler. '
83 'Use original scheduler instead.'))
84 parser.add_option('-l',
85 '--log_dir',
86 dest='log_dir',
87 default='',
88 help='The log_dir, default is under <crosperf_logs>/logs')
Luis Lozanof81680c2013-03-15 14:44:13 -070089
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080090 SetupParserOptions(parser)
91 options, args = parser.parse_args(argv)
92
93 # Convert the relevant options that are passed in into a settings
94 # object which will override settings in the experiment file.
95 option_settings = ConvertOptionsToSettings(options)
Luis Lozanof81680c2013-03-15 14:44:13 -070096 log_dir = os.path.abspath(os.path.expanduser(options.log_dir))
97 logger.GetLogger(log_dir)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080098
99 if len(args) == 2:
100 experiment_filename = args[1]
101 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800102 parser.error('Invalid number arguments.')
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800103
104 working_directory = os.getcwd()
Ahmad Sharif4467f002012-12-20 12:09:49 -0800105 if options.dry_run:
106 test_flag.SetTestMode(True)
107
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800108 experiment_file = ExperimentFile(
109 open(experiment_filename, 'rb'), option_settings)
110 if not experiment_file.GetGlobalSettings().GetField('name'):
Ahmad Sharif822c55d2012-02-08 20:55:47 -0800111 experiment_name = os.path.basename(experiment_filename)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800112 experiment_file.GetGlobalSettings().SetField('name', experiment_name)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800113 experiment = ExperimentFactory().GetExperiment(experiment_file,
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800114 working_directory, log_dir)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800115
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800116 json_report = experiment_file.GetGlobalSettings().GetField('json_report')
Caroline Ticeef4ca8a2015-08-25 12:53:38 -0700117
Luis Lozano45b53c52015-09-30 11:36:27 -0700118 signal.signal(signal.SIGTERM, CallExitHandler)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800119 atexit.register(Cleanup, experiment)
120
121 if options.dry_run:
Caroline Tice6e8726d2015-12-09 12:42:13 -0800122 runner = MockExperimentRunner(experiment, json_report)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800123 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800124 runner = ExperimentRunner(experiment,
125 json_report,
Han Shene205d902015-09-15 11:04:05 -0700126 using_schedv2=(not options.noschedv2))
Han Shenba649282015-08-05 17:19:55 -0700127
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800128 runner.Run()
129
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800130
131if __name__ == '__main__':
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800132 Main(sys.argv)