blob: 6b00cf7f33a25262bf5da1f3eb6c0ae040526921 [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
Rahul Chaudhry4f07bbf2016-01-29 15:10:02 -08009import argparse
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080010import 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
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080017from settings_factory import GlobalSettings
18from utils import logger
19
Ahmad Sharif4467f002012-12-20 12:09:49 -080020import test_flag
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080021
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080022
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080023def SetupParserOptions(parser):
24 """Add all options to the parser."""
Rahul Chaudhry4f07bbf2016-01-29 15:10:02 -080025 parser.add_argument('--dry_run',
26 dest='dry_run',
27 help=('Parse the experiment file and '
28 'show what will be done'),
29 action='store_true',
30 default=False)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080031 # Allow each of the global fields to be overridden by passing in
32 # options. Add each global field as an option.
Luis Lozanof2a3ef42015-12-15 13:49:30 -080033 option_settings = GlobalSettings('')
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080034 for field_name in option_settings.fields:
35 field = option_settings.fields[field_name]
Rahul Chaudhry4f07bbf2016-01-29 15:10:02 -080036 parser.add_argument('--%s' % field.name,
37 dest=field.name,
38 help=field.description,
39 action='store')
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080040
41
42def ConvertOptionsToSettings(options):
43 """Convert options passed in into global settings."""
Luis Lozanof2a3ef42015-12-15 13:49:30 -080044 option_settings = GlobalSettings('option_settings')
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080045 for option_name in options.__dict__:
46 if (options.__dict__[option_name] is not None and
47 option_name in option_settings.fields):
48 option_settings.SetField(option_name, options.__dict__[option_name])
49 return option_settings
50
51
52def Cleanup(experiment):
53 """Handler function which is registered to the atexit handler."""
54 experiment.Cleanup()
55
56
Luis Lozano45b53c52015-09-30 11:36:27 -070057def CallExitHandler(signum, _):
58 """Signal handler that transforms a signal into a call to exit.
59
60 This is useful because functionality registered by "atexit" will
61 be called. It also means you can "catch" the signal by catching
62 the SystemExit exception.
63 """
64 sys.exit(128 + signum)
65
66
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080067def Main(argv):
Rahul Chaudhry4f07bbf2016-01-29 15:10:02 -080068 parser = argparse.ArgumentParser()
Luis Lozanof81680c2013-03-15 14:44:13 -070069
Rahul Chaudhry4f07bbf2016-01-29 15:10:02 -080070 parser.add_argument('--noschedv2',
71 dest='noschedv2',
72 default=False,
73 action='store_true',
74 help=('Do not use new scheduler. '
75 'Use original scheduler instead.'))
76 parser.add_argument('-l',
77 '--log_dir',
78 dest='log_dir',
79 default='',
80 help='The log_dir, default is under <crosperf_logs>/logs')
Luis Lozanof81680c2013-03-15 14:44:13 -070081
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080082 SetupParserOptions(parser)
Rahul Chaudhry4f07bbf2016-01-29 15:10:02 -080083 options, args = parser.parse_known_args(argv)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080084
85 # Convert the relevant options that are passed in into a settings
86 # object which will override settings in the experiment file.
87 option_settings = ConvertOptionsToSettings(options)
Luis Lozanof81680c2013-03-15 14:44:13 -070088 log_dir = os.path.abspath(os.path.expanduser(options.log_dir))
89 logger.GetLogger(log_dir)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080090
91 if len(args) == 2:
92 experiment_filename = args[1]
93 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080094 parser.error('Invalid number arguments.')
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080095
96 working_directory = os.getcwd()
Ahmad Sharif4467f002012-12-20 12:09:49 -080097 if options.dry_run:
98 test_flag.SetTestMode(True)
99
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800100 experiment_file = ExperimentFile(
101 open(experiment_filename, 'rb'), option_settings)
102 if not experiment_file.GetGlobalSettings().GetField('name'):
Ahmad Sharif822c55d2012-02-08 20:55:47 -0800103 experiment_name = os.path.basename(experiment_filename)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800104 experiment_file.GetGlobalSettings().SetField('name', experiment_name)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800105 experiment = ExperimentFactory().GetExperiment(experiment_file,
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800106 working_directory, log_dir)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800107
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800108 json_report = experiment_file.GetGlobalSettings().GetField('json_report')
Caroline Ticeef4ca8a2015-08-25 12:53:38 -0700109
Luis Lozano45b53c52015-09-30 11:36:27 -0700110 signal.signal(signal.SIGTERM, CallExitHandler)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800111 atexit.register(Cleanup, experiment)
112
113 if options.dry_run:
Caroline Tice6e8726d2015-12-09 12:42:13 -0800114 runner = MockExperimentRunner(experiment, json_report)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800115 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800116 runner = ExperimentRunner(experiment,
117 json_report,
Han Shene205d902015-09-15 11:04:05 -0700118 using_schedv2=(not options.noschedv2))
Han Shenba649282015-08-05 17:19:55 -0700119
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800120 runner.Run()
121
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800122
123if __name__ == '__main__':
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800124 Main(sys.argv)