blob: de172cb193574bc2f0d96415479d9781ebfb3ebf [file] [log] [blame]
Yunlian Jiang00cc30e2013-03-28 13:23:57 -07001# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
Ahmad Sharif4467f002012-12-20 12:09:49 -08004"""The experiment setting module."""
5
Yunlian Jiang742ed2c2015-12-10 10:05:59 -08006from __future__ import print_function
7
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -08008import os
9import time
Ahmad Sharif4467f002012-12-20 12:09:49 -080010
cmticee5bc63b2015-05-27 16:59:37 -070011import afe_lock_machine
Han Shenba649282015-08-05 17:19:55 -070012from threading import Lock
cmticee5bc63b2015-05-27 16:59:37 -070013
Yunlian Jiang0d1a9f32015-12-09 10:47:11 -080014from cros_utils import logger
15from cros_utils import misc
Ahmad Sharif4467f002012-12-20 12:09:49 -080016
Han Shene0662972015-09-18 16:53:34 -070017import benchmark_run
Han Shen738e6de2015-12-07 13:22:25 -080018from machine_manager import BadChecksum
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080019from machine_manager import MachineManager
Ahmad Sharif4467f002012-12-20 12:09:49 -080020from machine_manager import MockMachineManager
Ahmad Sharif4467f002012-12-20 12:09:49 -080021import test_flag
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080022
Luis Lozanof2a3ef42015-12-15 13:49:30 -080023
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080024class Experiment(object):
25 """Class representing an Experiment to be run."""
26
Luis Lozanof2a3ef42015-12-15 13:49:30 -080027 def __init__(self, name, remote, working_directory, chromeos_root,
28 cache_conditions, labels, benchmarks, experiment_file, email_to,
29 acquire_timeout, log_dir, log_level, share_cache,
30 results_directory, locks_directory):
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080031 self.name = name
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080032 self.working_directory = working_directory
33 self.remote = remote
34 self.chromeos_root = chromeos_root
35 self.cache_conditions = cache_conditions
36 self.experiment_file = experiment_file
Ahmad Shariff395c262012-10-09 17:48:09 -070037 self.email_to = email_to
Yunlian Jiang00cc30e2013-03-28 13:23:57 -070038 if not results_directory:
39 self.results_directory = os.path.join(self.working_directory,
Luis Lozanof2a3ef42015-12-15 13:49:30 -080040 self.name + '_results')
Yunlian Jiang00cc30e2013-03-28 13:23:57 -070041 else:
42 self.results_directory = misc.CanonicalizePath(results_directory)
Luis Lozanof81680c2013-03-15 14:44:13 -070043 self.log_dir = log_dir
cmtice13909242014-03-11 13:38:07 -070044 self.log_level = log_level
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080045 self.labels = labels
46 self.benchmarks = benchmarks
47 self.num_complete = 0
Ahmad Sharif4467f002012-12-20 12:09:49 -080048 self.num_run_complete = 0
cmtice1a224362014-10-16 15:49:56 -070049 self.share_cache = share_cache
cmtice517dc982015-06-12 12:22:32 -070050 # If locks_directory (self.lock_dir) not blank, we will use the file
51 # locking mechanism; if it is blank then we will use the AFE server
52 # locking mechanism.
53 self.locks_dir = locks_directory
cmticef3eb8032015-07-27 13:55:52 -070054 self.locked_machines = []
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080055
Luis Lozanodd417612015-12-08 12:08:44 -080056 if not remote:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080057 raise RuntimeError('No remote hosts specified')
Luis Lozanodd417612015-12-08 12:08:44 -080058 if not self.benchmarks:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080059 raise RuntimeError('No benchmarks specified')
Luis Lozanodd417612015-12-08 12:08:44 -080060 if not self.labels:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080061 raise RuntimeError('No labels specified')
Luis Lozanodd417612015-12-08 12:08:44 -080062
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080063 # We need one chromeos_root to run the benchmarks in, but it doesn't
64 # matter where it is, unless the ABIs are different.
65 if not chromeos_root:
66 for label in self.labels:
67 if label.chromeos_root:
68 chromeos_root = label.chromeos_root
Luis Lozanodd417612015-12-08 12:08:44 -080069 break
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080070 if not chromeos_root:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080071 raise RuntimeError('No chromeos_root given and could not determine '
72 'one from the image path.')
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080073
Ahmad Sharif4467f002012-12-20 12:09:49 -080074 if test_flag.GetTestMode():
cmtice13909242014-03-11 13:38:07 -070075 self.machine_manager = MockMachineManager(chromeos_root, acquire_timeout,
Caroline Ticee627fd62015-12-11 12:07:59 -080076 log_level)
Ahmad Sharif4467f002012-12-20 12:09:49 -080077 else:
cmtice13909242014-03-11 13:38:07 -070078 self.machine_manager = MachineManager(chromeos_root, acquire_timeout,
cmtice517dc982015-06-12 12:22:32 -070079 log_level, locks_directory)
Luis Lozanof81680c2013-03-15 14:44:13 -070080 self.l = logger.GetLogger(log_dir)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080081
Han Shenf9b50352015-09-17 11:26:22 -070082 for machine in self.remote:
83 # machine_manager.AddMachine only adds reachable machines.
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080084 self.machine_manager.AddMachine(machine)
Han Shenf9b50352015-09-17 11:26:22 -070085 # Now machine_manager._all_machines contains a list of reachable
86 # machines. This is a subset of self.remote. We make both lists the same.
87 self.remote = [m.name for m in self.machine_manager._all_machines]
Caroline Tice51d7a9b2015-12-09 08:01:54 -080088 if not self.remote:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080089 raise RuntimeError('No machine available for running experiment.')
Han Shenf9b50352015-09-17 11:26:22 -070090
Ahmad Sharif4467f002012-12-20 12:09:49 -080091 for label in labels:
Han Shenf9b50352015-09-17 11:26:22 -070092 # We filter out label remotes that are not reachable (not in
93 # self.remote). So each label.remote is a sublist of experiment.remote.
94 label.remote = filter(lambda x: x in self.remote, label.remote)
Han Shen738e6de2015-12-07 13:22:25 -080095 try:
96 self.machine_manager.ComputeCommonCheckSum(label)
97 except BadChecksum:
98 # Force same image on all machines, then we do checksum again. No
99 # bailout if checksums still do not match.
100 self.machine_manager.ForceSameImageToAllMachines(label)
101 self.machine_manager.ComputeCommonCheckSum(label)
102
Ahmad Sharif4467f002012-12-20 12:09:49 -0800103 self.machine_manager.ComputeCommonCheckSumString(label)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800104
105 self.start_time = None
106 self.benchmark_runs = self._GenerateBenchmarkRuns()
107
Han Shenba649282015-08-05 17:19:55 -0700108 self._schedv2 = None
109 self._internal_counter_lock = Lock()
110
111 def set_schedv2(self, schedv2):
Caroline Ticeddde5052015-09-23 09:43:35 -0700112 self._schedv2 = schedv2
Han Shenba649282015-08-05 17:19:55 -0700113
114 def schedv2(self):
Caroline Ticeddde5052015-09-23 09:43:35 -0700115 return self._schedv2
Han Shenba649282015-08-05 17:19:55 -0700116
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800117 def _GenerateBenchmarkRuns(self):
118 """Generate benchmark runs from labels and benchmark defintions."""
119 benchmark_runs = []
120 for label in self.labels:
121 for benchmark in self.benchmarks:
122 for iteration in range(1, benchmark.iterations + 1):
123
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800124 benchmark_run_name = '%s: %s (%s)' % (label.name, benchmark.name,
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800125 iteration)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800126 full_name = '%s_%s_%s' % (label.name, benchmark.name, iteration)
127 logger_to_use = logger.Logger(self.log_dir, 'run.%s' % (full_name),
cmtice77892942014-03-18 13:47:17 -0700128 True)
Han Shene0662972015-09-18 16:53:34 -0700129 benchmark_runs.append(benchmark_run.BenchmarkRun(
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800130 benchmark_run_name, benchmark, label, iteration,
131 self.cache_conditions, self.machine_manager, logger_to_use,
132 self.log_level, self.share_cache))
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800133
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800134 return benchmark_runs
135
136 def Build(self):
137 pass
138
139 def Terminate(self):
Han Shenba649282015-08-05 17:19:55 -0700140 if self._schedv2 is not None:
141 self._schedv2.terminate()
142 else:
143 for t in self.benchmark_runs:
144 if t.isAlive():
145 self.l.LogError("Terminating run: '%s'." % t.name)
146 t.Terminate()
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800147
148 def IsComplete(self):
Han Shenba649282015-08-05 17:19:55 -0700149 if self._schedv2:
150 return self._schedv2.is_complete()
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800151 if self.active_threads:
152 for t in self.active_threads:
153 if t.isAlive():
154 t.join(0)
155 if not t.isAlive():
156 self.num_complete += 1
Ahmad Sharif4467f002012-12-20 12:09:49 -0800157 if not t.cache_hit:
158 self.num_run_complete += 1
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800159 self.active_threads.remove(t)
160 return False
161 return True
162
Han Shenba649282015-08-05 17:19:55 -0700163 def BenchmarkRunFinished(self, br):
Yunlian Jiang742ed2c2015-12-10 10:05:59 -0800164 """Update internal counters after br finishes.
Han Shenba649282015-08-05 17:19:55 -0700165
Yunlian Jiang742ed2c2015-12-10 10:05:59 -0800166 Note this is only used by schedv2 and is called by multiple threads.
167 Never throw any exception here.
168 """
Han Shenba649282015-08-05 17:19:55 -0700169
Yunlian Jiang742ed2c2015-12-10 10:05:59 -0800170 assert self._schedv2 is not None
171 with self._internal_counter_lock:
172 self.num_complete += 1
173 if not br.cache_hit:
174 self.num_run_complete += 1
Han Shenba649282015-08-05 17:19:55 -0700175
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800176 def Run(self):
177 self.start_time = time.time()
Han Shenba649282015-08-05 17:19:55 -0700178 if self._schedv2 is not None:
179 self._schedv2.run_sched()
180 else:
181 self.active_threads = []
182 for benchmark_run in self.benchmark_runs:
183 # Set threads to daemon so program exits when ctrl-c is pressed.
184 benchmark_run.daemon = True
185 benchmark_run.start()
186 self.active_threads.append(benchmark_run)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800187
188 def SetCacheConditions(self, cache_conditions):
189 for benchmark_run in self.benchmark_runs:
190 benchmark_run.SetCacheConditions(cache_conditions)
191
192 def Cleanup(self):
cmticee5bc63b2015-05-27 16:59:37 -0700193 """Make sure all machines are unlocked."""
cmtice517dc982015-06-12 12:22:32 -0700194 if self.locks_dir:
195 # We are using the file locks mechanism, so call machine_manager.Cleanup
196 # to unlock everything.
197 self.machine_manager.Cleanup()
198 else:
Caroline Tice7057cf62015-12-10 12:09:40 -0800199 if test_flag.GetTestMode():
200 return
201
cmticef3eb8032015-07-27 13:55:52 -0700202 all_machines = self.locked_machines
203 if not all_machines:
204 return
205
206 # If we locked any machines earlier, make sure we unlock them now.
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800207 lock_mgr = afe_lock_machine.AFELockManager(
208 all_machines, '', self.labels[0].chromeos_root, None)
209 machine_states = lock_mgr.GetMachineStates('unlock')
cmtice517dc982015-06-12 12:22:32 -0700210 for k, state in machine_states.iteritems():
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800211 if state['locked']:
cmtice517dc982015-06-12 12:22:32 -0700212 lock_mgr.UpdateLockInAFE(False, k)