blob: d5770d4635755771a1c52571d254d6baccc58196 [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,
Tiancong Wang3edb0f52019-01-07 11:43:07 -080031 results_directory, locks_directory, cwp_dso, enable_aslr):
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080032 self.name = name
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080033 self.working_directory = working_directory
34 self.remote = remote
35 self.chromeos_root = chromeos_root
36 self.cache_conditions = cache_conditions
37 self.experiment_file = experiment_file
Ahmad Shariff395c262012-10-09 17:48:09 -070038 self.email_to = email_to
Yunlian Jiang00cc30e2013-03-28 13:23:57 -070039 if not results_directory:
40 self.results_directory = os.path.join(self.working_directory,
Luis Lozanof2a3ef42015-12-15 13:49:30 -080041 self.name + '_results')
Yunlian Jiang00cc30e2013-03-28 13:23:57 -070042 else:
43 self.results_directory = misc.CanonicalizePath(results_directory)
Luis Lozanof81680c2013-03-15 14:44:13 -070044 self.log_dir = log_dir
cmtice13909242014-03-11 13:38:07 -070045 self.log_level = log_level
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080046 self.labels = labels
47 self.benchmarks = benchmarks
48 self.num_complete = 0
Ahmad Sharif4467f002012-12-20 12:09:49 -080049 self.num_run_complete = 0
cmtice1a224362014-10-16 15:49:56 -070050 self.share_cache = share_cache
Caroline Ticee1a28bd2016-08-02 16:49:57 -070051 self.active_threads = []
cmtice517dc982015-06-12 12:22:32 -070052 # If locks_directory (self.lock_dir) not blank, we will use the file
53 # locking mechanism; if it is blank then we will use the AFE server
54 # locking mechanism.
55 self.locks_dir = locks_directory
cmticef3eb8032015-07-27 13:55:52 -070056 self.locked_machines = []
Zhizhou Yang1a199b12018-11-09 11:44:10 -080057 self.cwp_dso = cwp_dso
Tiancong Wang3edb0f52019-01-07 11:43:07 -080058 self.enable_aslr = enable_aslr
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080059
Luis Lozanodd417612015-12-08 12:08:44 -080060 if not remote:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080061 raise RuntimeError('No remote hosts specified')
Luis Lozanodd417612015-12-08 12:08:44 -080062 if not self.benchmarks:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080063 raise RuntimeError('No benchmarks specified')
Luis Lozanodd417612015-12-08 12:08:44 -080064 if not self.labels:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080065 raise RuntimeError('No labels specified')
Luis Lozanodd417612015-12-08 12:08:44 -080066
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080067 # We need one chromeos_root to run the benchmarks in, but it doesn't
68 # matter where it is, unless the ABIs are different.
69 if not chromeos_root:
70 for label in self.labels:
71 if label.chromeos_root:
72 chromeos_root = label.chromeos_root
Luis Lozanodd417612015-12-08 12:08:44 -080073 break
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080074 if not chromeos_root:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080075 raise RuntimeError('No chromeos_root given and could not determine '
76 'one from the image path.')
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080077
David Sharpa9368342016-01-22 17:52:01 -080078 machine_manager_fn = MachineManager
Ahmad Sharif4467f002012-12-20 12:09:49 -080079 if test_flag.GetTestMode():
David Sharpa9368342016-01-22 17:52:01 -080080 machine_manager_fn = MockMachineManager
81 self.machine_manager = machine_manager_fn(chromeos_root, acquire_timeout,
82 log_level, locks_directory)
Luis Lozanof81680c2013-03-15 14:44:13 -070083 self.l = logger.GetLogger(log_dir)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080084
Han Shenf9b50352015-09-17 11:26:22 -070085 for machine in self.remote:
86 # machine_manager.AddMachine only adds reachable machines.
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080087 self.machine_manager.AddMachine(machine)
Han Shenf9b50352015-09-17 11:26:22 -070088 # Now machine_manager._all_machines contains a list of reachable
89 # machines. This is a subset of self.remote. We make both lists the same.
Caroline Ticee1a28bd2016-08-02 16:49:57 -070090 self.remote = [m.name for m in self.machine_manager.GetAllMachines()]
Caroline Tice51d7a9b2015-12-09 08:01:54 -080091 if not self.remote:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080092 raise RuntimeError('No machine available for running experiment.')
Han Shenf9b50352015-09-17 11:26:22 -070093
Ahmad Sharif4467f002012-12-20 12:09:49 -080094 for label in labels:
Han Shenf9b50352015-09-17 11:26:22 -070095 # We filter out label remotes that are not reachable (not in
96 # self.remote). So each label.remote is a sublist of experiment.remote.
Caroline Ticee1a28bd2016-08-02 16:49:57 -070097 label.remote = [r for r in label.remote if r in self.remote]
Han Shen738e6de2015-12-07 13:22:25 -080098 try:
99 self.machine_manager.ComputeCommonCheckSum(label)
100 except BadChecksum:
101 # Force same image on all machines, then we do checksum again. No
102 # bailout if checksums still do not match.
103 self.machine_manager.ForceSameImageToAllMachines(label)
104 self.machine_manager.ComputeCommonCheckSum(label)
105
Ahmad Sharif4467f002012-12-20 12:09:49 -0800106 self.machine_manager.ComputeCommonCheckSumString(label)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800107
108 self.start_time = None
109 self.benchmark_runs = self._GenerateBenchmarkRuns()
110
Han Shenba649282015-08-05 17:19:55 -0700111 self._schedv2 = None
112 self._internal_counter_lock = Lock()
113
114 def set_schedv2(self, schedv2):
Caroline Ticeddde5052015-09-23 09:43:35 -0700115 self._schedv2 = schedv2
Han Shenba649282015-08-05 17:19:55 -0700116
117 def schedv2(self):
Caroline Ticeddde5052015-09-23 09:43:35 -0700118 return self._schedv2
Han Shenba649282015-08-05 17:19:55 -0700119
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800120 def _GenerateBenchmarkRuns(self):
121 """Generate benchmark runs from labels and benchmark defintions."""
122 benchmark_runs = []
123 for label in self.labels:
124 for benchmark in self.benchmarks:
George Burgess IVe56ceb42016-08-08 16:14:24 -0700125 for iteration in xrange(1, benchmark.iterations + 1):
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800126
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800127 benchmark_run_name = '%s: %s (%s)' % (label.name, benchmark.name,
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800128 iteration)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800129 full_name = '%s_%s_%s' % (label.name, benchmark.name, iteration)
130 logger_to_use = logger.Logger(self.log_dir, 'run.%s' % (full_name),
cmtice77892942014-03-18 13:47:17 -0700131 True)
Caroline Ticef6ef4392017-04-06 17:16:05 -0700132 benchmark_runs.append(
Tiancong Wang3edb0f52019-01-07 11:43:07 -0800133 benchmark_run.BenchmarkRun(
134 benchmark_run_name, benchmark, label, iteration,
135 self.cache_conditions, self.machine_manager, logger_to_use,
136 self.log_level, self.share_cache, self.enable_aslr))
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800137
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800138 return benchmark_runs
139
140 def Build(self):
141 pass
142
143 def Terminate(self):
Han Shenba649282015-08-05 17:19:55 -0700144 if self._schedv2 is not None:
145 self._schedv2.terminate()
146 else:
147 for t in self.benchmark_runs:
148 if t.isAlive():
149 self.l.LogError("Terminating run: '%s'." % t.name)
150 t.Terminate()
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800151
152 def IsComplete(self):
Han Shenba649282015-08-05 17:19:55 -0700153 if self._schedv2:
154 return self._schedv2.is_complete()
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800155 if self.active_threads:
156 for t in self.active_threads:
157 if t.isAlive():
158 t.join(0)
159 if not t.isAlive():
160 self.num_complete += 1
Ahmad Sharif4467f002012-12-20 12:09:49 -0800161 if not t.cache_hit:
162 self.num_run_complete += 1
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800163 self.active_threads.remove(t)
164 return False
165 return True
166
Han Shenba649282015-08-05 17:19:55 -0700167 def BenchmarkRunFinished(self, br):
Yunlian Jiang742ed2c2015-12-10 10:05:59 -0800168 """Update internal counters after br finishes.
Han Shenba649282015-08-05 17:19:55 -0700169
Yunlian Jiang742ed2c2015-12-10 10:05:59 -0800170 Note this is only used by schedv2 and is called by multiple threads.
171 Never throw any exception here.
172 """
Han Shenba649282015-08-05 17:19:55 -0700173
Yunlian Jiang742ed2c2015-12-10 10:05:59 -0800174 assert self._schedv2 is not None
175 with self._internal_counter_lock:
176 self.num_complete += 1
177 if not br.cache_hit:
178 self.num_run_complete += 1
Han Shenba649282015-08-05 17:19:55 -0700179
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800180 def Run(self):
181 self.start_time = time.time()
Han Shenba649282015-08-05 17:19:55 -0700182 if self._schedv2 is not None:
183 self._schedv2.run_sched()
184 else:
185 self.active_threads = []
Caroline Ticee1a28bd2016-08-02 16:49:57 -0700186 for run in self.benchmark_runs:
Han Shenba649282015-08-05 17:19:55 -0700187 # Set threads to daemon so program exits when ctrl-c is pressed.
Caroline Ticee1a28bd2016-08-02 16:49:57 -0700188 run.daemon = True
189 run.start()
190 self.active_threads.append(run)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800191
192 def SetCacheConditions(self, cache_conditions):
Caroline Ticee1a28bd2016-08-02 16:49:57 -0700193 for run in self.benchmark_runs:
194 run.SetCacheConditions(cache_conditions)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800195
196 def Cleanup(self):
cmticee5bc63b2015-05-27 16:59:37 -0700197 """Make sure all machines are unlocked."""
cmtice517dc982015-06-12 12:22:32 -0700198 if self.locks_dir:
199 # We are using the file locks mechanism, so call machine_manager.Cleanup
200 # to unlock everything.
201 self.machine_manager.Cleanup()
202 else:
Caroline Tice7057cf62015-12-10 12:09:40 -0800203 if test_flag.GetTestMode():
204 return
205
cmticef3eb8032015-07-27 13:55:52 -0700206 all_machines = self.locked_machines
207 if not all_machines:
208 return
209
210 # If we locked any machines earlier, make sure we unlock them now.
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800211 lock_mgr = afe_lock_machine.AFELockManager(
212 all_machines, '', self.labels[0].chromeos_root, None)
213 machine_states = lock_mgr.GetMachineStates('unlock')
cmtice517dc982015-06-12 12:22:32 -0700214 for k, state in machine_states.iteritems():
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800215 if state['locked']:
cmtice517dc982015-06-12 12:22:32 -0700216 lock_mgr.UpdateLockInAFE(False, k)