blob: 9f8edc0ee28c76a5c1934fe34f864fa33d4798b6 [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):
Rahul Chaudhrye7c6fbf2016-01-15 15:16:47 -080025 """Help formatter."""
Luis Lozanof2a3ef42015-12-15 13:49:30 -080026
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080027 def format_description(self, description):
28 return description
29
30
31def SetupParserOptions(parser):
32 """Add all options to the parser."""
Luis Lozanof2a3ef42015-12-15 13:49:30 -080033 parser.add_option('--dry_run',
34 dest='dry_run',
35 help=('Parse the experiment file and '
36 'show what will be done'),
37 action='store_true',
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080038 default=False)
39 # Allow each of the global fields to be overridden by passing in
40 # options. Add each global field as an option.
Luis Lozanof2a3ef42015-12-15 13:49:30 -080041 option_settings = GlobalSettings('')
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080042 for field_name in option_settings.fields:
43 field = option_settings.fields[field_name]
Luis Lozanof2a3ef42015-12-15 13:49:30 -080044 parser.add_option('--%s' % field.name,
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080045 dest=field.name,
46 help=field.description,
Luis Lozanof2a3ef42015-12-15 13:49:30 -080047 action='store')
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080048
49
50def ConvertOptionsToSettings(options):
51 """Convert options passed in into global settings."""
Luis Lozanof2a3ef42015-12-15 13:49:30 -080052 option_settings = GlobalSettings('option_settings')
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080053 for option_name in options.__dict__:
54 if (options.__dict__[option_name] is not None and
55 option_name in option_settings.fields):
56 option_settings.SetField(option_name, options.__dict__[option_name])
57 return option_settings
58
59
60def Cleanup(experiment):
61 """Handler function which is registered to the atexit handler."""
62 experiment.Cleanup()
63
64
Luis Lozano45b53c52015-09-30 11:36:27 -070065def CallExitHandler(signum, _):
66 """Signal handler that transforms a signal into a call to exit.
67
68 This is useful because functionality registered by "atexit" will
69 be called. It also means you can "catch" the signal by catching
70 the SystemExit exception.
71 """
72 sys.exit(128 + signum)
73
74
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080075def Main(argv):
76 parser = optparse.OptionParser(usage=Help().GetUsage(),
77 description=Help().GetHelp(),
78 formatter=MyIndentedHelpFormatter(),
Luis Lozanof2a3ef42015-12-15 13:49:30 -080079 version='%prog 3.0')
Luis Lozanof81680c2013-03-15 14:44:13 -070080
Luis Lozanof2a3ef42015-12-15 13:49:30 -080081 parser.add_option('--noschedv2',
82 dest='noschedv2',
Han Shen43494292015-09-14 10:26:40 -070083 default=False,
Luis Lozanof2a3ef42015-12-15 13:49:30 -080084 action='store_true',
85 help=('Do not use new scheduler. '
86 'Use original scheduler instead.'))
87 parser.add_option('-l',
88 '--log_dir',
89 dest='log_dir',
90 default='',
91 help='The log_dir, default is under <crosperf_logs>/logs')
Luis Lozanof81680c2013-03-15 14:44:13 -070092
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080093 SetupParserOptions(parser)
94 options, args = parser.parse_args(argv)
95
96 # Convert the relevant options that are passed in into a settings
97 # object which will override settings in the experiment file.
98 option_settings = ConvertOptionsToSettings(options)
Luis Lozanof81680c2013-03-15 14:44:13 -070099 log_dir = os.path.abspath(os.path.expanduser(options.log_dir))
100 logger.GetLogger(log_dir)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800101
102 if len(args) == 2:
103 experiment_filename = args[1]
104 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800105 parser.error('Invalid number arguments.')
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800106
107 working_directory = os.getcwd()
Ahmad Sharif4467f002012-12-20 12:09:49 -0800108 if options.dry_run:
109 test_flag.SetTestMode(True)
110
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800111 experiment_file = ExperimentFile(
112 open(experiment_filename, 'rb'), option_settings)
113 if not experiment_file.GetGlobalSettings().GetField('name'):
Ahmad Sharif822c55d2012-02-08 20:55:47 -0800114 experiment_name = os.path.basename(experiment_filename)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800115 experiment_file.GetGlobalSettings().SetField('name', experiment_name)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800116 experiment = ExperimentFactory().GetExperiment(experiment_file,
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800117 working_directory, log_dir)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800118
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800119 json_report = experiment_file.GetGlobalSettings().GetField('json_report')
Caroline Ticeef4ca8a2015-08-25 12:53:38 -0700120
Luis Lozano45b53c52015-09-30 11:36:27 -0700121 signal.signal(signal.SIGTERM, CallExitHandler)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800122 atexit.register(Cleanup, experiment)
123
124 if options.dry_run:
Caroline Tice6e8726d2015-12-09 12:42:13 -0800125 runner = MockExperimentRunner(experiment, json_report)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800126 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800127 runner = ExperimentRunner(experiment,
128 json_report,
Han Shene205d902015-09-15 11:04:05 -0700129 using_schedv2=(not options.noschedv2))
Han Shenba649282015-08-05 17:19:55 -0700130
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800131 runner.Run()
132
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800133
134if __name__ == '__main__':
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800135 Main(sys.argv)