blob: 33b3f87835ca505ef7d06a0f7d1c58e8d7280438 [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
cmtice517dc982015-06-12 12:22:32 -070049 # If locks_directory (self.lock_dir) not blank, we will use the file
50 # locking mechanism; if it is blank then we will use the AFE server
51 # locking mechanism.
52 self.locks_dir = locks_directory
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080053
54 # We need one chromeos_root to run the benchmarks in, but it doesn't
55 # matter where it is, unless the ABIs are different.
56 if not chromeos_root:
57 for label in self.labels:
58 if label.chromeos_root:
59 chromeos_root = label.chromeos_root
60 if not chromeos_root:
61 raise Exception("No chromeos_root given and could not determine one from "
62 "the image path.")
63
Ahmad Sharif4467f002012-12-20 12:09:49 -080064 if test_flag.GetTestMode():
cmtice13909242014-03-11 13:38:07 -070065 self.machine_manager = MockMachineManager(chromeos_root, acquire_timeout,
cmticed96e4572015-05-19 16:19:25 -070066 log_level, locks_directory)
Ahmad Sharif4467f002012-12-20 12:09:49 -080067 else:
cmtice13909242014-03-11 13:38:07 -070068 self.machine_manager = MachineManager(chromeos_root, acquire_timeout,
cmtice517dc982015-06-12 12:22:32 -070069 log_level, locks_directory)
Luis Lozanof81680c2013-03-15 14:44:13 -070070 self.l = logger.GetLogger(log_dir)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080071
72 for machine in remote:
73 self.machine_manager.AddMachine(machine)
Ahmad Sharif4467f002012-12-20 12:09:49 -080074 for label in labels:
75 self.machine_manager.ComputeCommonCheckSum(label)
76 self.machine_manager.ComputeCommonCheckSumString(label)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080077
78 self.start_time = None
79 self.benchmark_runs = self._GenerateBenchmarkRuns()
80
81 def _GenerateBenchmarkRuns(self):
82 """Generate benchmark runs from labels and benchmark defintions."""
83 benchmark_runs = []
84 for label in self.labels:
85 for benchmark in self.benchmarks:
86 for iteration in range(1, benchmark.iterations + 1):
87
88 benchmark_run_name = "%s: %s (%s)" % (label.name, benchmark.name,
89 iteration)
90 full_name = "%s_%s_%s" % (label.name, benchmark.name, iteration)
Luis Lozanof81680c2013-03-15 14:44:13 -070091 logger_to_use = logger.Logger(self.log_dir,
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080092 "run.%s" % (full_name),
cmtice77892942014-03-18 13:47:17 -070093 True)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080094 benchmark_run = BenchmarkRun(benchmark_run_name,
Ahmad Sharif4467f002012-12-20 12:09:49 -080095 benchmark,
96 label,
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080097 iteration,
98 self.cache_conditions,
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080099 self.machine_manager,
Luis Lozanof81680c2013-03-15 14:44:13 -0700100 logger_to_use,
cmtice13909242014-03-11 13:38:07 -0700101 self.log_level,
cmtice1a224362014-10-16 15:49:56 -0700102 self.share_cache)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800103
104 benchmark_runs.append(benchmark_run)
105 return benchmark_runs
106
107 def Build(self):
108 pass
109
110 def Terminate(self):
111 for t in self.benchmark_runs:
112 if t.isAlive():
113 self.l.LogError("Terminating run: '%s'." % t.name)
114 t.Terminate()
115
116 def IsComplete(self):
117 if self.active_threads:
118 for t in self.active_threads:
119 if t.isAlive():
120 t.join(0)
121 if not t.isAlive():
122 self.num_complete += 1
Ahmad Sharif4467f002012-12-20 12:09:49 -0800123 if not t.cache_hit:
124 self.num_run_complete += 1
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800125 self.active_threads.remove(t)
126 return False
127 return True
128
129 def Run(self):
130 self.start_time = time.time()
131 self.active_threads = []
132 for benchmark_run in self.benchmark_runs:
133 # Set threads to daemon so program exits when ctrl-c is pressed.
134 benchmark_run.daemon = True
135 benchmark_run.start()
136 self.active_threads.append(benchmark_run)
137
138 def SetCacheConditions(self, cache_conditions):
139 for benchmark_run in self.benchmark_runs:
140 benchmark_run.SetCacheConditions(cache_conditions)
141
142 def Cleanup(self):
cmticee5bc63b2015-05-27 16:59:37 -0700143 """Make sure all machines are unlocked."""
cmtice517dc982015-06-12 12:22:32 -0700144 if self.locks_dir:
145 # We are using the file locks mechanism, so call machine_manager.Cleanup
146 # to unlock everything.
147 self.machine_manager.Cleanup()
148 else:
149 all_machines = self.remote
150 for l in self.labels:
151 all_machines += l.remote
152 lock_mgr = afe_lock_machine.AFELockManager(all_machines, "",
153 self.labels[0].chromeos_root,
154 None)
155 machine_states = lock_mgr.GetMachineStates("unlock")
156 for k, state in machine_states.iteritems():
157 if state["locked"]:
158 lock_mgr.UpdateLockInAFE(False, k)