blob: a7a4a7a038b8b567063707772d2672d9afa7796c [file] [log] [blame]
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -08001#!/usr/bin/python
2
Yunlian Jiang00cc30e2013-03-28 13:23:57 -07003# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -08006
Ahmad Sharif4467f002012-12-20 12:09:49 -08007"""The experiment setting module."""
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
13
Ahmad Sharif4467f002012-12-20 12:09:49 -080014from utils import logger
Yunlian Jiang00cc30e2013-03-28 13:23:57 -070015from utils import misc
Ahmad Sharif4467f002012-12-20 12:09:49 -080016
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080017from benchmark_run import BenchmarkRun
18from machine_manager import MachineManager
Ahmad Sharif4467f002012-12-20 12:09:49 -080019from machine_manager import MockMachineManager
Ahmad Sharif4467f002012-12-20 12:09:49 -080020import test_flag
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080021
22
23class Experiment(object):
24 """Class representing an Experiment to be run."""
25
Luis Lozanof81680c2013-03-15 14:44:13 -070026 def __init__(self, name, remote, working_directory,
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080027 chromeos_root, cache_conditions, labels, benchmarks,
Luis Lozanof81680c2013-03-15 14:44:13 -070028 experiment_file, email_to, acquire_timeout, log_dir,
cmtice5c09fc22015-04-22 09:25:53 -070029 log_level, share_cache, results_directory, locks_directory):
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080030 self.name = name
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080031 self.working_directory = working_directory
32 self.remote = remote
33 self.chromeos_root = chromeos_root
34 self.cache_conditions = cache_conditions
35 self.experiment_file = experiment_file
Ahmad Shariff395c262012-10-09 17:48:09 -070036 self.email_to = email_to
Yunlian Jiang00cc30e2013-03-28 13:23:57 -070037 if not results_directory:
38 self.results_directory = os.path.join(self.working_directory,
39 self.name + "_results")
40 else:
41 self.results_directory = misc.CanonicalizePath(results_directory)
Luis Lozanof81680c2013-03-15 14:44:13 -070042 self.log_dir = log_dir
cmtice13909242014-03-11 13:38:07 -070043 self.log_level = log_level
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080044 self.labels = labels
45 self.benchmarks = benchmarks
46 self.num_complete = 0
Ahmad Sharif4467f002012-12-20 12:09:49 -080047 self.num_run_complete = 0
cmtice1a224362014-10-16 15:49:56 -070048 self.share_cache = share_cache
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080049
50 # We need one chromeos_root to run the benchmarks in, but it doesn't
51 # matter where it is, unless the ABIs are different.
52 if not chromeos_root:
53 for label in self.labels:
54 if label.chromeos_root:
55 chromeos_root = label.chromeos_root
56 if not chromeos_root:
57 raise Exception("No chromeos_root given and could not determine one from "
58 "the image path.")
59
cmticee5bc63b2015-05-27 16:59:37 -070060 # This is a local directory, where the machine manager will keep track of
61 # which machines are available for which benchmark run. The assumption is
62 # that all of the machines have been globally locked for this experiment,
63 # to keep other people/experiments from accessing them, but we still need the
64 # local locks directory to keep two or more benchmark runs within the same
65 # experiment from trying to use the same machine at the same time.
66 local_locks_directory = os.path.join(self.working_directory,
67 "local_locks")
Ahmad Sharif4467f002012-12-20 12:09:49 -080068 if test_flag.GetTestMode():
cmtice13909242014-03-11 13:38:07 -070069 self.machine_manager = MockMachineManager(chromeos_root, acquire_timeout,
cmticed96e4572015-05-19 16:19:25 -070070 log_level, locks_directory)
Ahmad Sharif4467f002012-12-20 12:09:49 -080071 else:
cmtice13909242014-03-11 13:38:07 -070072 self.machine_manager = MachineManager(chromeos_root, acquire_timeout,
cmticee5bc63b2015-05-27 16:59:37 -070073 log_level, local_locks_directory)
Luis Lozanof81680c2013-03-15 14:44:13 -070074 self.l = logger.GetLogger(log_dir)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080075
76 for machine in remote:
77 self.machine_manager.AddMachine(machine)
Ahmad Sharif4467f002012-12-20 12:09:49 -080078 for label in labels:
79 self.machine_manager.ComputeCommonCheckSum(label)
80 self.machine_manager.ComputeCommonCheckSumString(label)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080081
82 self.start_time = None
83 self.benchmark_runs = self._GenerateBenchmarkRuns()
84
85 def _GenerateBenchmarkRuns(self):
86 """Generate benchmark runs from labels and benchmark defintions."""
87 benchmark_runs = []
88 for label in self.labels:
89 for benchmark in self.benchmarks:
90 for iteration in range(1, benchmark.iterations + 1):
91
92 benchmark_run_name = "%s: %s (%s)" % (label.name, benchmark.name,
93 iteration)
94 full_name = "%s_%s_%s" % (label.name, benchmark.name, iteration)
Luis Lozanof81680c2013-03-15 14:44:13 -070095 logger_to_use = logger.Logger(self.log_dir,
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080096 "run.%s" % (full_name),
cmtice77892942014-03-18 13:47:17 -070097 True)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080098 benchmark_run = BenchmarkRun(benchmark_run_name,
Ahmad Sharif4467f002012-12-20 12:09:49 -080099 benchmark,
100 label,
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800101 iteration,
102 self.cache_conditions,
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800103 self.machine_manager,
Luis Lozanof81680c2013-03-15 14:44:13 -0700104 logger_to_use,
cmtice13909242014-03-11 13:38:07 -0700105 self.log_level,
cmtice1a224362014-10-16 15:49:56 -0700106 self.share_cache)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800107
108 benchmark_runs.append(benchmark_run)
109 return benchmark_runs
110
111 def Build(self):
112 pass
113
114 def Terminate(self):
115 for t in self.benchmark_runs:
116 if t.isAlive():
117 self.l.LogError("Terminating run: '%s'." % t.name)
118 t.Terminate()
119
120 def IsComplete(self):
121 if self.active_threads:
122 for t in self.active_threads:
123 if t.isAlive():
124 t.join(0)
125 if not t.isAlive():
126 self.num_complete += 1
Ahmad Sharif4467f002012-12-20 12:09:49 -0800127 if not t.cache_hit:
128 self.num_run_complete += 1
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800129 self.active_threads.remove(t)
130 return False
131 return True
132
133 def Run(self):
134 self.start_time = time.time()
135 self.active_threads = []
136 for benchmark_run in self.benchmark_runs:
137 # Set threads to daemon so program exits when ctrl-c is pressed.
138 benchmark_run.daemon = True
139 benchmark_run.start()
140 self.active_threads.append(benchmark_run)
141
142 def SetCacheConditions(self, cache_conditions):
143 for benchmark_run in self.benchmark_runs:
144 benchmark_run.SetCacheConditions(cache_conditions)
145
146 def Cleanup(self):
cmticee5bc63b2015-05-27 16:59:37 -0700147 """Make sure all machines are unlocked."""
148 all_machines = self.remote
149 for l in self.labels:
150 all_machines += l.remote
151 lock_mgr = afe_lock_machine.AFELockManager(all_machines, "",
152 self.labels[0].chromeos_root, None)
153 machine_states = lock_mgr.GetMachineStates("unlock")
154 for k, state in machine_states.iteritems():
155 if state["locked"]:
156 lock_mgr.UpdateLockInAFE(False, k)