blob: 4c13c02b2de5d3dd3385c9c70af56e57170bce5d [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
Rahul Chaudhryeff2fc12016-02-24 10:12:43 -080018
Caroline Ticea8af9a72016-07-20 12:52:59 -070019# This import causes pylint to warn about "No name 'logger' in module
20# 'cros_utils'". I do not understand why. The import works fine in python.
Rahul Chaudhryeff2fc12016-02-24 10:12:43 -080021# pylint: disable=no-name-in-module
Caroline Ticea8af9a72016-07-20 12:52:59 -070022from cros_utils import logger
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080023
Ahmad Sharif4467f002012-12-20 12:09:49 -080024import test_flag
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080025
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080026
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080027def SetupParserOptions(parser):
28 """Add all options to the parser."""
Rahul Chaudhry4f07bbf2016-01-29 15:10:02 -080029 parser.add_argument('--dry_run',
30 dest='dry_run',
31 help=('Parse the experiment file and '
32 'show what will be done'),
33 action='store_true',
34 default=False)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080035 # Allow each of the global fields to be overridden by passing in
36 # options. Add each global field as an option.
Luis Lozanof2a3ef42015-12-15 13:49:30 -080037 option_settings = GlobalSettings('')
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080038 for field_name in option_settings.fields:
39 field = option_settings.fields[field_name]
Rahul Chaudhry4f07bbf2016-01-29 15:10:02 -080040 parser.add_argument('--%s' % field.name,
41 dest=field.name,
42 help=field.description,
43 action='store')
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080044
45
46def ConvertOptionsToSettings(options):
47 """Convert options passed in into global settings."""
Luis Lozanof2a3ef42015-12-15 13:49:30 -080048 option_settings = GlobalSettings('option_settings')
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080049 for option_name in options.__dict__:
50 if (options.__dict__[option_name] is not None and
51 option_name in option_settings.fields):
52 option_settings.SetField(option_name, options.__dict__[option_name])
53 return option_settings
54
55
56def Cleanup(experiment):
57 """Handler function which is registered to the atexit handler."""
58 experiment.Cleanup()
59
60
Luis Lozano45b53c52015-09-30 11:36:27 -070061def CallExitHandler(signum, _):
62 """Signal handler that transforms a signal into a call to exit.
63
64 This is useful because functionality registered by "atexit" will
65 be called. It also means you can "catch" the signal by catching
66 the SystemExit exception.
67 """
68 sys.exit(128 + signum)
69
70
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080071def Main(argv):
Rahul Chaudhry4f07bbf2016-01-29 15:10:02 -080072 parser = argparse.ArgumentParser()
Luis Lozanof81680c2013-03-15 14:44:13 -070073
Rahul Chaudhry4f07bbf2016-01-29 15:10:02 -080074 parser.add_argument('--noschedv2',
75 dest='noschedv2',
76 default=False,
77 action='store_true',
78 help=('Do not use new scheduler. '
79 'Use original scheduler instead.'))
80 parser.add_argument('-l',
81 '--log_dir',
82 dest='log_dir',
83 default='',
84 help='The log_dir, default is under <crosperf_logs>/logs')
Luis Lozanof81680c2013-03-15 14:44:13 -070085
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080086 SetupParserOptions(parser)
Rahul Chaudhry4f07bbf2016-01-29 15:10:02 -080087 options, args = parser.parse_known_args(argv)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080088
89 # Convert the relevant options that are passed in into a settings
90 # object which will override settings in the experiment file.
91 option_settings = ConvertOptionsToSettings(options)
Luis Lozanof81680c2013-03-15 14:44:13 -070092 log_dir = os.path.abspath(os.path.expanduser(options.log_dir))
93 logger.GetLogger(log_dir)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080094
95 if len(args) == 2:
96 experiment_filename = args[1]
97 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080098 parser.error('Invalid number arguments.')
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080099
100 working_directory = os.getcwd()
Ahmad Sharif4467f002012-12-20 12:09:49 -0800101 if options.dry_run:
102 test_flag.SetTestMode(True)
103
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800104 experiment_file = ExperimentFile(
105 open(experiment_filename, 'rb'), option_settings)
106 if not experiment_file.GetGlobalSettings().GetField('name'):
Ahmad Sharif822c55d2012-02-08 20:55:47 -0800107 experiment_name = os.path.basename(experiment_filename)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800108 experiment_file.GetGlobalSettings().SetField('name', experiment_name)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800109 experiment = ExperimentFactory().GetExperiment(experiment_file,
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800110 working_directory, log_dir)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800111
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800112 json_report = experiment_file.GetGlobalSettings().GetField('json_report')
Caroline Ticeef4ca8a2015-08-25 12:53:38 -0700113
Luis Lozano45b53c52015-09-30 11:36:27 -0700114 signal.signal(signal.SIGTERM, CallExitHandler)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800115 atexit.register(Cleanup, experiment)
116
117 if options.dry_run:
Caroline Tice6e8726d2015-12-09 12:42:13 -0800118 runner = MockExperimentRunner(experiment, json_report)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800119 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800120 runner = ExperimentRunner(experiment,
121 json_report,
Han Shene205d902015-09-15 11:04:05 -0700122 using_schedv2=(not options.noschedv2))
Han Shenba649282015-08-05 17:19:55 -0700123
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800124 runner.Run()
125
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800126
127if __name__ == '__main__':
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800128 Main(sys.argv)