blob: 63e6a1040a5c98d94dbb44ec121fa7a4a18441af [file] [log] [blame]
Tiancong Wang3edb0f52019-01-07 11:43:07 -08001# -*- coding: utf-8 -*-
Yunlian Jiang00cc30e2013-03-28 13:23:57 -07002# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
Ahmad Sharif4467f002012-12-20 12:09:49 -08005"""The experiment setting module."""
6
Yunlian Jiang742ed2c2015-12-10 10:05:59 -08007from __future__ import print_function
8
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -08009import os
10import time
Ahmad Sharif4467f002012-12-20 12:09:49 -080011
cmticee5bc63b2015-05-27 16:59:37 -070012import afe_lock_machine
Han Shenba649282015-08-05 17:19:55 -070013from threading import Lock
cmticee5bc63b2015-05-27 16:59:37 -070014
Yunlian Jiang0d1a9f32015-12-09 10:47:11 -080015from cros_utils import logger
16from cros_utils import misc
Ahmad Sharif4467f002012-12-20 12:09:49 -080017
Han Shene0662972015-09-18 16:53:34 -070018import benchmark_run
Han Shen738e6de2015-12-07 13:22:25 -080019from machine_manager import BadChecksum
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080020from machine_manager import MachineManager
Ahmad Sharif4467f002012-12-20 12:09:49 -080021from machine_manager import MockMachineManager
Ahmad Sharif4467f002012-12-20 12:09:49 -080022import test_flag
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080023
Luis Lozanof2a3ef42015-12-15 13:49:30 -080024
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080025class Experiment(object):
26 """Class representing an Experiment to be run."""
27
Luis Lozanof2a3ef42015-12-15 13:49:30 -080028 def __init__(self, name, remote, working_directory, chromeos_root,
29 cache_conditions, labels, benchmarks, experiment_file, email_to,
30 acquire_timeout, log_dir, log_level, share_cache,
Zhizhou Yang1a5a3162019-03-14 13:25:06 -070031 results_directory, locks_directory, cwp_dso, enable_aslr,
32 ignore_min_max):
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080033 self.name = name
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080034 self.working_directory = working_directory
35 self.remote = remote
36 self.chromeos_root = chromeos_root
37 self.cache_conditions = cache_conditions
38 self.experiment_file = experiment_file
Ahmad Shariff395c262012-10-09 17:48:09 -070039 self.email_to = email_to
Yunlian Jiang00cc30e2013-03-28 13:23:57 -070040 if not results_directory:
41 self.results_directory = os.path.join(self.working_directory,
Luis Lozanof2a3ef42015-12-15 13:49:30 -080042 self.name + '_results')
Yunlian Jiang00cc30e2013-03-28 13:23:57 -070043 else:
44 self.results_directory = misc.CanonicalizePath(results_directory)
Luis Lozanof81680c2013-03-15 14:44:13 -070045 self.log_dir = log_dir
cmtice13909242014-03-11 13:38:07 -070046 self.log_level = log_level
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080047 self.labels = labels
48 self.benchmarks = benchmarks
49 self.num_complete = 0
Ahmad Sharif4467f002012-12-20 12:09:49 -080050 self.num_run_complete = 0
cmtice1a224362014-10-16 15:49:56 -070051 self.share_cache = share_cache
Caroline Ticee1a28bd2016-08-02 16:49:57 -070052 self.active_threads = []
cmtice517dc982015-06-12 12:22:32 -070053 # If locks_directory (self.lock_dir) not blank, we will use the file
54 # locking mechanism; if it is blank then we will use the AFE server
55 # locking mechanism.
56 self.locks_dir = locks_directory
cmticef3eb8032015-07-27 13:55:52 -070057 self.locked_machines = []
Zhizhou Yang1a199b12018-11-09 11:44:10 -080058 self.cwp_dso = cwp_dso
Tiancong Wang3edb0f52019-01-07 11:43:07 -080059 self.enable_aslr = enable_aslr
Zhizhou Yang1a5a3162019-03-14 13:25:06 -070060 self.ignore_min_max = ignore_min_max
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080061
Luis Lozanodd417612015-12-08 12:08:44 -080062 if not remote:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080063 raise RuntimeError('No remote hosts specified')
Luis Lozanodd417612015-12-08 12:08:44 -080064 if not self.benchmarks:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080065 raise RuntimeError('No benchmarks specified')
Luis Lozanodd417612015-12-08 12:08:44 -080066 if not self.labels:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080067 raise RuntimeError('No labels specified')
Luis Lozanodd417612015-12-08 12:08:44 -080068
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080069 # We need one chromeos_root to run the benchmarks in, but it doesn't
70 # matter where it is, unless the ABIs are different.
71 if not chromeos_root:
72 for label in self.labels:
73 if label.chromeos_root:
74 chromeos_root = label.chromeos_root
Luis Lozanodd417612015-12-08 12:08:44 -080075 break
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080076 if not chromeos_root:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080077 raise RuntimeError('No chromeos_root given and could not determine '
78 'one from the image path.')
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080079
David Sharpa9368342016-01-22 17:52:01 -080080 machine_manager_fn = MachineManager
Ahmad Sharif4467f002012-12-20 12:09:49 -080081 if test_flag.GetTestMode():
David Sharpa9368342016-01-22 17:52:01 -080082 machine_manager_fn = MockMachineManager
83 self.machine_manager = machine_manager_fn(chromeos_root, acquire_timeout,
84 log_level, locks_directory)
Luis Lozanof81680c2013-03-15 14:44:13 -070085 self.l = logger.GetLogger(log_dir)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080086
Han Shenf9b50352015-09-17 11:26:22 -070087 for machine in self.remote:
88 # machine_manager.AddMachine only adds reachable machines.
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080089 self.machine_manager.AddMachine(machine)
Han Shenf9b50352015-09-17 11:26:22 -070090 # Now machine_manager._all_machines contains a list of reachable
91 # machines. This is a subset of self.remote. We make both lists the same.
Caroline Ticee1a28bd2016-08-02 16:49:57 -070092 self.remote = [m.name for m in self.machine_manager.GetAllMachines()]
Caroline Tice51d7a9b2015-12-09 08:01:54 -080093 if not self.remote:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080094 raise RuntimeError('No machine available for running experiment.')
Han Shenf9b50352015-09-17 11:26:22 -070095
Ahmad Sharif4467f002012-12-20 12:09:49 -080096 for label in labels:
Han Shenf9b50352015-09-17 11:26:22 -070097 # We filter out label remotes that are not reachable (not in
98 # self.remote). So each label.remote is a sublist of experiment.remote.
Caroline Ticee1a28bd2016-08-02 16:49:57 -070099 label.remote = [r for r in label.remote if r in self.remote]
Han Shen738e6de2015-12-07 13:22:25 -0800100 try:
101 self.machine_manager.ComputeCommonCheckSum(label)
102 except BadChecksum:
103 # Force same image on all machines, then we do checksum again. No
104 # bailout if checksums still do not match.
105 self.machine_manager.ForceSameImageToAllMachines(label)
106 self.machine_manager.ComputeCommonCheckSum(label)
107
Ahmad Sharif4467f002012-12-20 12:09:49 -0800108 self.machine_manager.ComputeCommonCheckSumString(label)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800109
110 self.start_time = None
111 self.benchmark_runs = self._GenerateBenchmarkRuns()
112
Han Shenba649282015-08-05 17:19:55 -0700113 self._schedv2 = None
114 self._internal_counter_lock = Lock()
115
116 def set_schedv2(self, schedv2):
Caroline Ticeddde5052015-09-23 09:43:35 -0700117 self._schedv2 = schedv2
Han Shenba649282015-08-05 17:19:55 -0700118
119 def schedv2(self):
Caroline Ticeddde5052015-09-23 09:43:35 -0700120 return self._schedv2
Han Shenba649282015-08-05 17:19:55 -0700121
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800122 def _GenerateBenchmarkRuns(self):
123 """Generate benchmark runs from labels and benchmark defintions."""
124 benchmark_runs = []
125 for label in self.labels:
126 for benchmark in self.benchmarks:
George Burgess IVe56ceb42016-08-08 16:14:24 -0700127 for iteration in xrange(1, benchmark.iterations + 1):
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800128
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800129 benchmark_run_name = '%s: %s (%s)' % (label.name, benchmark.name,
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800130 iteration)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800131 full_name = '%s_%s_%s' % (label.name, benchmark.name, iteration)
132 logger_to_use = logger.Logger(self.log_dir, 'run.%s' % (full_name),
cmtice77892942014-03-18 13:47:17 -0700133 True)
Caroline Ticef6ef4392017-04-06 17:16:05 -0700134 benchmark_runs.append(
Tiancong Wang3edb0f52019-01-07 11:43:07 -0800135 benchmark_run.BenchmarkRun(
136 benchmark_run_name, benchmark, label, iteration,
137 self.cache_conditions, self.machine_manager, logger_to_use,
138 self.log_level, self.share_cache, self.enable_aslr))
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800139
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800140 return benchmark_runs
141
142 def Build(self):
143 pass
144
145 def Terminate(self):
Han Shenba649282015-08-05 17:19:55 -0700146 if self._schedv2 is not None:
147 self._schedv2.terminate()
148 else:
149 for t in self.benchmark_runs:
150 if t.isAlive():
151 self.l.LogError("Terminating run: '%s'." % t.name)
152 t.Terminate()
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800153
154 def IsComplete(self):
Han Shenba649282015-08-05 17:19:55 -0700155 if self._schedv2:
156 return self._schedv2.is_complete()
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800157 if self.active_threads:
158 for t in self.active_threads:
159 if t.isAlive():
160 t.join(0)
161 if not t.isAlive():
162 self.num_complete += 1
Ahmad Sharif4467f002012-12-20 12:09:49 -0800163 if not t.cache_hit:
164 self.num_run_complete += 1
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800165 self.active_threads.remove(t)
166 return False
167 return True
168
Han Shenba649282015-08-05 17:19:55 -0700169 def BenchmarkRunFinished(self, br):
Yunlian Jiang742ed2c2015-12-10 10:05:59 -0800170 """Update internal counters after br finishes.
Han Shenba649282015-08-05 17:19:55 -0700171
Yunlian Jiang742ed2c2015-12-10 10:05:59 -0800172 Note this is only used by schedv2 and is called by multiple threads.
173 Never throw any exception here.
174 """
Han Shenba649282015-08-05 17:19:55 -0700175
Yunlian Jiang742ed2c2015-12-10 10:05:59 -0800176 assert self._schedv2 is not None
177 with self._internal_counter_lock:
178 self.num_complete += 1
179 if not br.cache_hit:
180 self.num_run_complete += 1
Han Shenba649282015-08-05 17:19:55 -0700181
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800182 def Run(self):
183 self.start_time = time.time()
Han Shenba649282015-08-05 17:19:55 -0700184 if self._schedv2 is not None:
185 self._schedv2.run_sched()
186 else:
187 self.active_threads = []
Caroline Ticee1a28bd2016-08-02 16:49:57 -0700188 for run in self.benchmark_runs:
Han Shenba649282015-08-05 17:19:55 -0700189 # Set threads to daemon so program exits when ctrl-c is pressed.
Caroline Ticee1a28bd2016-08-02 16:49:57 -0700190 run.daemon = True
191 run.start()
192 self.active_threads.append(run)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800193
194 def SetCacheConditions(self, cache_conditions):
Caroline Ticee1a28bd2016-08-02 16:49:57 -0700195 for run in self.benchmark_runs:
196 run.SetCacheConditions(cache_conditions)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800197
198 def Cleanup(self):
cmticee5bc63b2015-05-27 16:59:37 -0700199 """Make sure all machines are unlocked."""
cmtice517dc982015-06-12 12:22:32 -0700200 if self.locks_dir:
201 # We are using the file locks mechanism, so call machine_manager.Cleanup
202 # to unlock everything.
203 self.machine_manager.Cleanup()
204 else:
Caroline Tice7057cf62015-12-10 12:09:40 -0800205 if test_flag.GetTestMode():
206 return
207
cmticef3eb8032015-07-27 13:55:52 -0700208 all_machines = self.locked_machines
209 if not all_machines:
210 return
211
212 # If we locked any machines earlier, make sure we unlock them now.
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800213 lock_mgr = afe_lock_machine.AFELockManager(
214 all_machines, '', self.labels[0].chromeos_root, None)
215 machine_states = lock_mgr.GetMachineStates('unlock')
cmtice517dc982015-06-12 12:22:32 -0700216 for k, state in machine_states.iteritems():
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800217 if state['locked']:
cmtice517dc982015-06-12 12:22:32 -0700218 lock_mgr.UpdateLockInAFE(False, k)