Zhizhou Yang | 5534af8 | 2020-01-15 16:25:04 -0800 | [diff] [blame^] | 1 | #!/usr/bin/env python3 |
Tiancong Wang | eb729be | 2019-08-07 15:18:51 -0700 | [diff] [blame] | 2 | # -*- coding: utf-8 -*- |
| 3 | # Copyright 2011 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 6 | |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 7 | """The driver script for running performance benchmarks on ChromeOS.""" |
| 8 | |
Rahul Chaudhry | cbc5a26 | 2015-12-30 17:05:14 -0800 | [diff] [blame] | 9 | from __future__ import print_function |
| 10 | |
Rahul Chaudhry | 4f07bbf | 2016-01-29 15:10:02 -0800 | [diff] [blame] | 11 | import argparse |
Denis Nikitin | 144f699 | 2019-07-20 20:25:16 -0700 | [diff] [blame] | 12 | import atexit |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 13 | import os |
Luis Lozano | 45b53c5 | 2015-09-30 11:36:27 -0700 | [diff] [blame] | 14 | import signal |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 15 | import sys |
Tiancong Wang | eb729be | 2019-08-07 15:18:51 -0700 | [diff] [blame] | 16 | |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 17 | from experiment_runner import ExperimentRunner |
| 18 | from experiment_runner import MockExperimentRunner |
| 19 | from experiment_factory import ExperimentFactory |
| 20 | from experiment_file import ExperimentFile |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 21 | from settings_factory import GlobalSettings |
Rahul Chaudhry | eff2fc1 | 2016-02-24 10:12:43 -0800 | [diff] [blame] | 22 | |
Caroline Tice | a8af9a7 | 2016-07-20 12:52:59 -0700 | [diff] [blame] | 23 | # This import causes pylint to warn about "No name 'logger' in module |
| 24 | # 'cros_utils'". I do not understand why. The import works fine in python. |
Rahul Chaudhry | eff2fc1 | 2016-02-24 10:12:43 -0800 | [diff] [blame] | 25 | # pylint: disable=no-name-in-module |
Caroline Tice | a8af9a7 | 2016-07-20 12:52:59 -0700 | [diff] [blame] | 26 | from cros_utils import logger |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 27 | |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 28 | import test_flag |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 29 | |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 30 | |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 31 | def SetupParserOptions(parser): |
| 32 | """Add all options to the parser.""" |
Manoj Gupta | aee96b7 | 2016-10-24 13:43:28 -0700 | [diff] [blame] | 33 | parser.add_argument( |
| 34 | '--dry_run', |
| 35 | dest='dry_run', |
| 36 | help=('Parse the experiment file and ' |
| 37 | 'show what will be done'), |
| 38 | action='store_true', |
| 39 | default=False) |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 40 | # Allow each of the global fields to be overridden by passing in |
| 41 | # options. Add each global field as an option. |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 42 | option_settings = GlobalSettings('') |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 43 | for field_name in option_settings.fields: |
| 44 | field = option_settings.fields[field_name] |
Manoj Gupta | aee96b7 | 2016-10-24 13:43:28 -0700 | [diff] [blame] | 45 | parser.add_argument( |
| 46 | '--%s' % field.name, |
| 47 | dest=field.name, |
| 48 | help=field.description, |
| 49 | action='store') |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 50 | |
| 51 | |
| 52 | def ConvertOptionsToSettings(options): |
| 53 | """Convert options passed in into global settings.""" |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 54 | option_settings = GlobalSettings('option_settings') |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 55 | for option_name in options.__dict__: |
| 56 | if (options.__dict__[option_name] is not None and |
| 57 | option_name in option_settings.fields): |
| 58 | option_settings.SetField(option_name, options.__dict__[option_name]) |
| 59 | return option_settings |
| 60 | |
| 61 | |
| 62 | def Cleanup(experiment): |
| 63 | """Handler function which is registered to the atexit handler.""" |
| 64 | experiment.Cleanup() |
| 65 | |
| 66 | |
Luis Lozano | 45b53c5 | 2015-09-30 11:36:27 -0700 | [diff] [blame] | 67 | def CallExitHandler(signum, _): |
| 68 | """Signal handler that transforms a signal into a call to exit. |
| 69 | |
| 70 | This is useful because functionality registered by "atexit" will |
| 71 | be called. It also means you can "catch" the signal by catching |
| 72 | the SystemExit exception. |
| 73 | """ |
| 74 | sys.exit(128 + signum) |
| 75 | |
| 76 | |
Manoj Gupta | aee96b7 | 2016-10-24 13:43:28 -0700 | [diff] [blame] | 77 | def RunCrosperf(argv): |
Rahul Chaudhry | 4f07bbf | 2016-01-29 15:10:02 -0800 | [diff] [blame] | 78 | parser = argparse.ArgumentParser() |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 79 | |
Manoj Gupta | aee96b7 | 2016-10-24 13:43:28 -0700 | [diff] [blame] | 80 | parser.add_argument( |
| 81 | '--noschedv2', |
| 82 | dest='noschedv2', |
| 83 | default=False, |
| 84 | action='store_true', |
| 85 | help=('Do not use new scheduler. ' |
| 86 | 'Use original scheduler instead.')) |
| 87 | parser.add_argument( |
| 88 | '-l', |
| 89 | '--log_dir', |
| 90 | dest='log_dir', |
| 91 | default='', |
| 92 | help='The log_dir, default is under <crosperf_logs>/logs') |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 93 | |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 94 | SetupParserOptions(parser) |
Rahul Chaudhry | 4f07bbf | 2016-01-29 15:10:02 -0800 | [diff] [blame] | 95 | options, args = parser.parse_known_args(argv) |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 96 | |
| 97 | # Convert the relevant options that are passed in into a settings |
| 98 | # object which will override settings in the experiment file. |
| 99 | option_settings = ConvertOptionsToSettings(options) |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 100 | log_dir = os.path.abspath(os.path.expanduser(options.log_dir)) |
| 101 | logger.GetLogger(log_dir) |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 102 | |
| 103 | if len(args) == 2: |
| 104 | experiment_filename = args[1] |
| 105 | else: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 106 | parser.error('Invalid number arguments.') |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 107 | |
| 108 | working_directory = os.getcwd() |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 109 | if options.dry_run: |
| 110 | test_flag.SetTestMode(True) |
| 111 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 112 | experiment_file = ExperimentFile( |
Zhizhou Yang | 5534af8 | 2020-01-15 16:25:04 -0800 | [diff] [blame^] | 113 | open(experiment_filename, encoding='utf-8'), option_settings) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 114 | if not experiment_file.GetGlobalSettings().GetField('name'): |
Ahmad Sharif | 822c55d | 2012-02-08 20:55:47 -0800 | [diff] [blame] | 115 | experiment_name = os.path.basename(experiment_filename) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 116 | experiment_file.GetGlobalSettings().SetField('name', experiment_name) |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 117 | experiment = ExperimentFactory().GetExperiment(experiment_file, |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 118 | working_directory, log_dir) |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 119 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 120 | json_report = experiment_file.GetGlobalSettings().GetField('json_report') |
Caroline Tice | ef4ca8a | 2015-08-25 12:53:38 -0700 | [diff] [blame] | 121 | |
Luis Lozano | 45b53c5 | 2015-09-30 11:36:27 -0700 | [diff] [blame] | 122 | signal.signal(signal.SIGTERM, CallExitHandler) |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 123 | atexit.register(Cleanup, experiment) |
| 124 | |
| 125 | if options.dry_run: |
Caroline Tice | 6e8726d | 2015-12-09 12:42:13 -0800 | [diff] [blame] | 126 | runner = MockExperimentRunner(experiment, json_report) |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 127 | else: |
Manoj Gupta | aee96b7 | 2016-10-24 13:43:28 -0700 | [diff] [blame] | 128 | runner = ExperimentRunner( |
| 129 | experiment, json_report, using_schedv2=(not options.noschedv2)) |
Han Shen | ba64928 | 2015-08-05 17:19:55 -0700 | [diff] [blame] | 130 | |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 131 | runner.Run() |
| 132 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 133 | |
Manoj Gupta | aee96b7 | 2016-10-24 13:43:28 -0700 | [diff] [blame] | 134 | def Main(argv): |
| 135 | try: |
| 136 | RunCrosperf(argv) |
Tiancong Wang | eb729be | 2019-08-07 15:18:51 -0700 | [diff] [blame] | 137 | except Exception: |
Manoj Gupta | aee96b7 | 2016-10-24 13:43:28 -0700 | [diff] [blame] | 138 | # Flush buffers before exiting to avoid out of order printing |
| 139 | sys.stdout.flush() |
Tiancong Wang | eb729be | 2019-08-07 15:18:51 -0700 | [diff] [blame] | 140 | # Raise exception prints out traceback |
| 141 | raise |
Manoj Gupta | aee96b7 | 2016-10-24 13:43:28 -0700 | [diff] [blame] | 142 | |
| 143 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 144 | if __name__ == '__main__': |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 145 | Main(sys.argv) |