blob: d0f5c133ec9769df49ca8a2213ae1b27f24646c5 [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.
Zhizhou Yang7aa250e2019-07-16 16:23:02 -07005
Ahmad Sharif4467f002012-12-20 12:09:49 -08006"""The experiment setting module."""
7
Yunlian Jiang742ed2c2015-12-10 10:05:59 -08008from __future__ import print_function
9
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080010import os
11import time
Ahmad Sharif4467f002012-12-20 12:09:49 -080012
cmticee5bc63b2015-05-27 16:59:37 -070013import afe_lock_machine
Han Shenba649282015-08-05 17:19:55 -070014from threading import Lock
cmticee5bc63b2015-05-27 16:59:37 -070015
Yunlian Jiang0d1a9f32015-12-09 10:47:11 -080016from cros_utils import logger
17from cros_utils import misc
Ahmad Sharif4467f002012-12-20 12:09:49 -080018
Han Shene0662972015-09-18 16:53:34 -070019import benchmark_run
Han Shen738e6de2015-12-07 13:22:25 -080020from machine_manager import BadChecksum
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080021from machine_manager import MachineManager
Ahmad Sharif4467f002012-12-20 12:09:49 -080022from machine_manager import MockMachineManager
Ahmad Sharif4467f002012-12-20 12:09:49 -080023import test_flag
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080024
Luis Lozanof2a3ef42015-12-15 13:49:30 -080025
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080026class Experiment(object):
27 """Class representing an Experiment to be run."""
28
Luis Lozanof2a3ef42015-12-15 13:49:30 -080029 def __init__(self, name, remote, working_directory, chromeos_root,
30 cache_conditions, labels, benchmarks, experiment_file, email_to,
31 acquire_timeout, log_dir, log_level, share_cache,
Zhizhou Yang1a5a3162019-03-14 13:25:06 -070032 results_directory, locks_directory, cwp_dso, enable_aslr,
Denis Nikitin9d114042019-08-30 09:10:39 -070033 ignore_min_max, skylab, dut_config):
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080034 self.name = name
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080035 self.working_directory = working_directory
36 self.remote = remote
37 self.chromeos_root = chromeos_root
38 self.cache_conditions = cache_conditions
39 self.experiment_file = experiment_file
Ahmad Shariff395c262012-10-09 17:48:09 -070040 self.email_to = email_to
Yunlian Jiang00cc30e2013-03-28 13:23:57 -070041 if not results_directory:
42 self.results_directory = os.path.join(self.working_directory,
Luis Lozanof2a3ef42015-12-15 13:49:30 -080043 self.name + '_results')
Yunlian Jiang00cc30e2013-03-28 13:23:57 -070044 else:
45 self.results_directory = misc.CanonicalizePath(results_directory)
Luis Lozanof81680c2013-03-15 14:44:13 -070046 self.log_dir = log_dir
cmtice13909242014-03-11 13:38:07 -070047 self.log_level = log_level
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080048 self.labels = labels
49 self.benchmarks = benchmarks
50 self.num_complete = 0
Ahmad Sharif4467f002012-12-20 12:09:49 -080051 self.num_run_complete = 0
cmtice1a224362014-10-16 15:49:56 -070052 self.share_cache = share_cache
Caroline Ticee1a28bd2016-08-02 16:49:57 -070053 self.active_threads = []
cmtice517dc982015-06-12 12:22:32 -070054 # If locks_directory (self.lock_dir) not blank, we will use the file
55 # locking mechanism; if it is blank then we will use the AFE server
56 # locking mechanism.
57 self.locks_dir = locks_directory
cmticef3eb8032015-07-27 13:55:52 -070058 self.locked_machines = []
Zhizhou Yang1a199b12018-11-09 11:44:10 -080059 self.cwp_dso = cwp_dso
Tiancong Wang3edb0f52019-01-07 11:43:07 -080060 self.enable_aslr = enable_aslr
Zhizhou Yang1a5a3162019-03-14 13:25:06 -070061 self.ignore_min_max = ignore_min_max
Zhizhou Yang7aa250e2019-07-16 16:23:02 -070062 self.skylab = skylab
63 self.l = logger.GetLogger(log_dir)
Denis Nikitin9d114042019-08-30 09:10:39 -070064 self.intel_pstate = dut_config['intel_pstate']
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080065
Luis Lozanodd417612015-12-08 12:08:44 -080066 if not self.benchmarks:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080067 raise RuntimeError('No benchmarks specified')
Luis Lozanodd417612015-12-08 12:08:44 -080068 if not self.labels:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080069 raise RuntimeError('No labels specified')
Zhizhou Yang7aa250e2019-07-16 16:23:02 -070070 if not remote and not self.skylab:
71 raise RuntimeError('No remote hosts specified')
Luis Lozanodd417612015-12-08 12:08:44 -080072
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080073 # We need one chromeos_root to run the benchmarks in, but it doesn't
74 # matter where it is, unless the ABIs are different.
75 if not chromeos_root:
76 for label in self.labels:
77 if label.chromeos_root:
78 chromeos_root = label.chromeos_root
Luis Lozanodd417612015-12-08 12:08:44 -080079 break
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080080 if not chromeos_root:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080081 raise RuntimeError('No chromeos_root given and could not determine '
82 'one from the image path.')
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080083
David Sharpa9368342016-01-22 17:52:01 -080084 machine_manager_fn = MachineManager
Ahmad Sharif4467f002012-12-20 12:09:49 -080085 if test_flag.GetTestMode():
David Sharpa9368342016-01-22 17:52:01 -080086 machine_manager_fn = MockMachineManager
87 self.machine_manager = machine_manager_fn(chromeos_root, acquire_timeout,
88 log_level, locks_directory)
Luis Lozanof81680c2013-03-15 14:44:13 -070089 self.l = logger.GetLogger(log_dir)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080090
Han Shenf9b50352015-09-17 11:26:22 -070091 for machine in self.remote:
92 # machine_manager.AddMachine only adds reachable machines.
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080093 self.machine_manager.AddMachine(machine)
Han Shenf9b50352015-09-17 11:26:22 -070094 # Now machine_manager._all_machines contains a list of reachable
95 # machines. This is a subset of self.remote. We make both lists the same.
Caroline Ticee1a28bd2016-08-02 16:49:57 -070096 self.remote = [m.name for m in self.machine_manager.GetAllMachines()]
Caroline Tice51d7a9b2015-12-09 08:01:54 -080097 if not self.remote:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080098 raise RuntimeError('No machine available for running experiment.')
Han Shenf9b50352015-09-17 11:26:22 -070099
Ahmad Sharif4467f002012-12-20 12:09:49 -0800100 for label in labels:
Han Shenf9b50352015-09-17 11:26:22 -0700101 # We filter out label remotes that are not reachable (not in
102 # self.remote). So each label.remote is a sublist of experiment.remote.
Caroline Ticee1a28bd2016-08-02 16:49:57 -0700103 label.remote = [r for r in label.remote if r in self.remote]
Han Shen738e6de2015-12-07 13:22:25 -0800104 try:
105 self.machine_manager.ComputeCommonCheckSum(label)
106 except BadChecksum:
107 # Force same image on all machines, then we do checksum again. No
108 # bailout if checksums still do not match.
Zhizhou Yang1c490cd2019-07-17 17:51:31 -0700109 # TODO: It's not a good idea to force flashing the image when we are
110 # running with skylab... Although for now it even helps us getting
111 # that machine...
Han Shen738e6de2015-12-07 13:22:25 -0800112 self.machine_manager.ForceSameImageToAllMachines(label)
113 self.machine_manager.ComputeCommonCheckSum(label)
114
Ahmad Sharif4467f002012-12-20 12:09:49 -0800115 self.machine_manager.ComputeCommonCheckSumString(label)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800116
117 self.start_time = None
Denis Nikitin9d114042019-08-30 09:10:39 -0700118 self.benchmark_runs = self._GenerateBenchmarkRuns(dut_config)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800119
Han Shenba649282015-08-05 17:19:55 -0700120 self._schedv2 = None
121 self._internal_counter_lock = Lock()
122
123 def set_schedv2(self, schedv2):
Caroline Ticeddde5052015-09-23 09:43:35 -0700124 self._schedv2 = schedv2
Han Shenba649282015-08-05 17:19:55 -0700125
126 def schedv2(self):
Caroline Ticeddde5052015-09-23 09:43:35 -0700127 return self._schedv2
Han Shenba649282015-08-05 17:19:55 -0700128
Denis Nikitin9d114042019-08-30 09:10:39 -0700129 def _GenerateBenchmarkRuns(self, dut_config):
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800130 """Generate benchmark runs from labels and benchmark defintions."""
131 benchmark_runs = []
132 for label in self.labels:
133 for benchmark in self.benchmarks:
Zhizhou Yang7aa250e2019-07-16 16:23:02 -0700134 for iteration in range(1, benchmark.iterations + 1):
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800135
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800136 benchmark_run_name = '%s: %s (%s)' % (label.name, benchmark.name,
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800137 iteration)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800138 full_name = '%s_%s_%s' % (label.name, benchmark.name, iteration)
139 logger_to_use = logger.Logger(self.log_dir, 'run.%s' % (full_name),
cmtice77892942014-03-18 13:47:17 -0700140 True)
Caroline Ticef6ef4392017-04-06 17:16:05 -0700141 benchmark_runs.append(
Denis Nikitin9d114042019-08-30 09:10:39 -0700142 benchmark_run.BenchmarkRun(benchmark_run_name, benchmark, label,
143 iteration, self.cache_conditions,
144 self.machine_manager, logger_to_use,
145 self.log_level, self.share_cache,
146 dut_config, self.enable_aslr))
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800147
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800148 return benchmark_runs
149
150 def Build(self):
151 pass
152
153 def Terminate(self):
Han Shenba649282015-08-05 17:19:55 -0700154 if self._schedv2 is not None:
155 self._schedv2.terminate()
156 else:
157 for t in self.benchmark_runs:
158 if t.isAlive():
159 self.l.LogError("Terminating run: '%s'." % t.name)
160 t.Terminate()
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800161
162 def IsComplete(self):
Han Shenba649282015-08-05 17:19:55 -0700163 if self._schedv2:
164 return self._schedv2.is_complete()
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800165 if self.active_threads:
166 for t in self.active_threads:
167 if t.isAlive():
168 t.join(0)
169 if not t.isAlive():
170 self.num_complete += 1
Ahmad Sharif4467f002012-12-20 12:09:49 -0800171 if not t.cache_hit:
172 self.num_run_complete += 1
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800173 self.active_threads.remove(t)
174 return False
175 return True
176
Han Shenba649282015-08-05 17:19:55 -0700177 def BenchmarkRunFinished(self, br):
Yunlian Jiang742ed2c2015-12-10 10:05:59 -0800178 """Update internal counters after br finishes.
Han Shenba649282015-08-05 17:19:55 -0700179
Yunlian Jiang742ed2c2015-12-10 10:05:59 -0800180 Note this is only used by schedv2 and is called by multiple threads.
181 Never throw any exception here.
182 """
Han Shenba649282015-08-05 17:19:55 -0700183
Yunlian Jiang742ed2c2015-12-10 10:05:59 -0800184 assert self._schedv2 is not None
185 with self._internal_counter_lock:
186 self.num_complete += 1
187 if not br.cache_hit:
188 self.num_run_complete += 1
Han Shenba649282015-08-05 17:19:55 -0700189
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800190 def Run(self):
191 self.start_time = time.time()
Han Shenba649282015-08-05 17:19:55 -0700192 if self._schedv2 is not None:
193 self._schedv2.run_sched()
194 else:
195 self.active_threads = []
Caroline Ticee1a28bd2016-08-02 16:49:57 -0700196 for run in self.benchmark_runs:
Han Shenba649282015-08-05 17:19:55 -0700197 # Set threads to daemon so program exits when ctrl-c is pressed.
Caroline Ticee1a28bd2016-08-02 16:49:57 -0700198 run.daemon = True
199 run.start()
200 self.active_threads.append(run)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800201
202 def SetCacheConditions(self, cache_conditions):
Caroline Ticee1a28bd2016-08-02 16:49:57 -0700203 for run in self.benchmark_runs:
204 run.SetCacheConditions(cache_conditions)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800205
206 def Cleanup(self):
cmticee5bc63b2015-05-27 16:59:37 -0700207 """Make sure all machines are unlocked."""
cmtice517dc982015-06-12 12:22:32 -0700208 if self.locks_dir:
209 # We are using the file locks mechanism, so call machine_manager.Cleanup
210 # to unlock everything.
211 self.machine_manager.Cleanup()
212 else:
Caroline Tice7057cf62015-12-10 12:09:40 -0800213 if test_flag.GetTestMode():
214 return
215
cmticef3eb8032015-07-27 13:55:52 -0700216 all_machines = self.locked_machines
217 if not all_machines:
218 return
219
220 # If we locked any machines earlier, make sure we unlock them now.
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800221 lock_mgr = afe_lock_machine.AFELockManager(
222 all_machines, '', self.labels[0].chromeos_root, None)
223 machine_states = lock_mgr.GetMachineStates('unlock')
Zhizhou Yang7aa250e2019-07-16 16:23:02 -0700224 for k, state in machine_states.items():
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800225 if state['locked']:
cmtice517dc982015-06-12 12:22:32 -0700226 lock_mgr.UpdateLockInAFE(False, k)