1 # Copyright 2014: Mirantis Inc.
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
16 # yardstick comment: this is a modified copy of
17 # rally/rally/benchmark/runners/base.py
19 from __future__ import absolute_import
22 import multiprocessing
28 from oslo_config import cfg
30 import yardstick.common.utils as utils
31 from yardstick.benchmark.scenarios import base as base_scenario
32 from yardstick.dispatcher.base import Base as DispatcherBase
34 log = logging.getLogger(__name__)
39 def _output_serializer_main(filename, queue, config):
40 """entrypoint for the singleton subprocess writing to outfile
41 Use of this process enables multiple instances of a scenario without
42 messing up the output file.
45 out_type = config['yardstick'].get('DEFAULT', {})['dispatcher']
47 out_type = os.environ.get('DISPATCHER', 'file')
50 'type': out_type.capitalize(),
54 dispatcher = DispatcherBase.get(conf, config)
57 # blocks until data becomes available
59 if record == '_TERMINATE_':
60 dispatcher.flush_result_data()
63 dispatcher.record_result_data(record)
66 def _execute_shell_command(command):
67 """execute shell script with error handling"""
71 output = subprocess.check_output(command, shell=True)
74 output = traceback.format_exc()
75 log.error("exec command '%s' error:\n ", command)
76 log.error(traceback.format_exc())
78 return exitcode, output
81 def _single_action(seconds, command, queue):
82 """entrypoint for the single action process"""
83 log.debug("single action, fires after %d seconds (from now)", seconds)
85 log.debug("single action: executing command: '%s'", command)
86 ret_code, data = _execute_shell_command(command)
88 log.error("single action error! command:%s", command)
89 queue.put({'single-action-data': data})
91 log.debug("single action data: \n%s", data)
92 queue.put({'single-action-data': data})
95 def _periodic_action(interval, command, queue):
96 """entrypoint for the periodic action process"""
97 log.debug("periodic action, fires every: %d seconds", interval)
101 time_spent += interval
102 log.debug("periodic action, executing command: '%s'", command)
103 ret_code, data = _execute_shell_command(command)
105 log.error("periodic action error! command:%s", command)
106 queue.put({'periodic-action-data': data})
108 log.debug("periodic action data: \n%s", data)
109 queue.put({'periodic-action-data': data})
112 class Runner(object):
118 def get_cls(runner_type):
119 """return class of specified type"""
120 for runner in utils.itersubclasses(Runner):
121 if runner_type == runner.__execution_type__:
123 raise RuntimeError("No such runner_type %s" % runner_type)
127 """return a list of known runner type (class) names"""
129 for runner in utils.itersubclasses(Runner):
134 def get(runner_cfg, config):
135 """Returns instance of a scenario runner for execution type.
137 # if there is no runner, start the output serializer subprocess
138 if not Runner.runners:
139 log.debug("Starting dump process file '%s'",
140 runner_cfg["output_filename"])
141 Runner.queue = multiprocessing.Queue()
142 Runner.dump_process = multiprocessing.Process(
143 target=_output_serializer_main,
145 args=(runner_cfg["output_filename"], Runner.queue, config))
146 Runner.dump_process.start()
148 return Runner.get_cls(runner_cfg["type"])(runner_cfg, Runner.queue)
151 def release_dump_process():
152 """Release the dumper process"""
153 log.debug("Stopping dump process")
154 if Runner.dump_process:
155 Runner.queue.put('_TERMINATE_')
156 Runner.dump_process.join()
157 Runner.dump_process = None
161 """Release the runner"""
162 if runner in Runner.runners:
163 Runner.runners.remove(runner)
165 # if this was the last runner, stop the output serializer subprocess
166 if not Runner.runners:
167 Runner.release_dump_process()
170 def terminate(runner):
171 """Terminate the runner"""
172 if runner.process and runner.process.is_alive():
173 runner.process.terminate()
177 """Terminate all runners (subprocesses)"""
178 log.debug("Terminating all runners")
180 # release dumper process as some errors before any runner is created
181 if not Runner.runners:
182 Runner.release_dump_process()
185 for runner in Runner.runners:
186 log.debug("Terminating runner: %s", runner)
188 runner.process.terminate()
189 runner.process.join()
190 if runner.periodic_action_process:
191 log.debug("Terminating periodic action process")
192 runner.periodic_action_process.terminate()
193 runner.periodic_action_process = None
194 Runner.release(runner)
196 def __init__(self, config, queue):
198 self.periodic_action_process = None
199 self.result_queue = queue
201 self.aborted = multiprocessing.Event()
202 Runner.runners.append(self)
204 def run_post_stop_action(self):
205 """run a potentially configured post-stop action"""
206 if "post-stop-action" in self.config:
207 command = self.config["post-stop-action"]["command"]
208 log.debug("post stop action: command: '%s'", command)
209 ret_code, data = _execute_shell_command(command)
211 log.error("post action error! command:%s", command)
212 self.result_queue.put({'post-stop-action-data': data})
214 log.debug("post-stop data: \n%s", data)
215 self.result_queue.put({'post-stop-action-data': data})
217 def run(self, scenario_cfg, context_cfg):
218 scenario_type = scenario_cfg["type"]
219 class_name = base_scenario.Scenario.get(scenario_type)
220 path_split = class_name.split(".")
221 module_path = ".".join(path_split[:-1])
222 module = importlib.import_module(module_path)
223 cls = getattr(module, path_split[-1])
225 self.config['object'] = class_name
228 # run a potentially configured pre-start action
229 if "pre-start-action" in self.config:
230 command = self.config["pre-start-action"]["command"]
231 log.debug("pre start action: command: '%s'", command)
232 ret_code, data = _execute_shell_command(command)
234 log.error("pre-start action error! command:%s", command)
235 self.result_queue.put({'pre-start-action-data': data})
237 log.debug("pre-start data: \n%s", data)
238 self.result_queue.put({'pre-start-action-data': data})
240 if "single-shot-action" in self.config:
241 single_action_process = multiprocessing.Process(
242 target=_single_action,
243 name="single-shot-action",
244 args=(self.config["single-shot-action"]["after"],
245 self.config["single-shot-action"]["command"],
247 single_action_process.start()
249 if "periodic-action" in self.config:
250 self.periodic_action_process = multiprocessing.Process(
251 target=_periodic_action,
252 name="periodic-action",
253 args=(self.config["periodic-action"]["interval"],
254 self.config["periodic-action"]["command"],
256 self.periodic_action_process.start()
258 self._run_benchmark(cls, "run", scenario_cfg, context_cfg)
261 """Abort the execution of a scenario"""
264 def join(self, timeout=None):
265 self.process.join(timeout)
266 if self.periodic_action_process:
267 self.periodic_action_process.terminate()
268 self.periodic_action_process = None
270 self.run_post_stop_action()
271 return self.process.exitcode