blob: ec0b20e9a301a586461cdbd7693712ed64c33340 [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
12from utils import logger
Yunlian Jiang00cc30e2013-03-28 13:23:57 -070013from utils import misc
Ahmad Sharif4467f002012-12-20 12:09:49 -080014
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080015from benchmark_run import BenchmarkRun
16from machine_manager import MachineManager
Ahmad Sharif4467f002012-12-20 12:09:49 -080017from machine_manager import MockMachineManager
Ahmad Sharif4467f002012-12-20 12:09:49 -080018import test_flag
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080019
20
21class Experiment(object):
22 """Class representing an Experiment to be run."""
23
Luis Lozanof81680c2013-03-15 14:44:13 -070024 def __init__(self, name, remote, working_directory,
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080025 chromeos_root, cache_conditions, labels, benchmarks,
Luis Lozanof81680c2013-03-15 14:44:13 -070026 experiment_file, email_to, acquire_timeout, log_dir,
Yunlian Jiang00cc30e2013-03-28 13:23:57 -070027 share_users, results_directory):
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080028 self.name = name
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080029 self.working_directory = working_directory
30 self.remote = remote
31 self.chromeos_root = chromeos_root
32 self.cache_conditions = cache_conditions
33 self.experiment_file = experiment_file
Ahmad Shariff395c262012-10-09 17:48:09 -070034 self.email_to = email_to
Yunlian Jiang00cc30e2013-03-28 13:23:57 -070035 if not results_directory:
36 self.results_directory = os.path.join(self.working_directory,
37 self.name + "_results")
38 else:
39 self.results_directory = misc.CanonicalizePath(results_directory)
Luis Lozanof81680c2013-03-15 14:44:13 -070040 self.log_dir = log_dir
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080041 self.labels = labels
42 self.benchmarks = benchmarks
43 self.num_complete = 0
Ahmad Sharif4467f002012-12-20 12:09:49 -080044 self.num_run_complete = 0
Luis Lozanof81680c2013-03-15 14:44:13 -070045 self.share_users = share_users
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080046
47 # We need one chromeos_root to run the benchmarks in, but it doesn't
48 # matter where it is, unless the ABIs are different.
49 if not chromeos_root:
50 for label in self.labels:
51 if label.chromeos_root:
52 chromeos_root = label.chromeos_root
53 if not chromeos_root:
54 raise Exception("No chromeos_root given and could not determine one from "
55 "the image path.")
56
Ahmad Sharif4467f002012-12-20 12:09:49 -080057 if test_flag.GetTestMode():
Luis Lozanof81680c2013-03-15 14:44:13 -070058 self.machine_manager = MockMachineManager(chromeos_root, acquire_timeout)
Ahmad Sharif4467f002012-12-20 12:09:49 -080059 else:
Luis Lozanof81680c2013-03-15 14:44:13 -070060 self.machine_manager = MachineManager(chromeos_root, acquire_timeout)
61 self.l = logger.GetLogger(log_dir)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080062
63 for machine in remote:
64 self.machine_manager.AddMachine(machine)
Ahmad Sharif4467f002012-12-20 12:09:49 -080065 for label in labels:
66 self.machine_manager.ComputeCommonCheckSum(label)
67 self.machine_manager.ComputeCommonCheckSumString(label)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080068
69 self.start_time = None
70 self.benchmark_runs = self._GenerateBenchmarkRuns()
71
72 def _GenerateBenchmarkRuns(self):
73 """Generate benchmark runs from labels and benchmark defintions."""
74 benchmark_runs = []
75 for label in self.labels:
76 for benchmark in self.benchmarks:
77 for iteration in range(1, benchmark.iterations + 1):
78
79 benchmark_run_name = "%s: %s (%s)" % (label.name, benchmark.name,
80 iteration)
81 full_name = "%s_%s_%s" % (label.name, benchmark.name, iteration)
Luis Lozanof81680c2013-03-15 14:44:13 -070082 logger_to_use = logger.Logger(self.log_dir,
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080083 "run.%s" % (full_name),
84 True)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080085 benchmark_run = BenchmarkRun(benchmark_run_name,
Ahmad Sharif4467f002012-12-20 12:09:49 -080086 benchmark,
87 label,
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080088 iteration,
89 self.cache_conditions,
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080090 self.machine_manager,
Luis Lozanof81680c2013-03-15 14:44:13 -070091 logger_to_use,
92 self.share_users)
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -080093
94 benchmark_runs.append(benchmark_run)
95 return benchmark_runs
96
97 def Build(self):
98 pass
99
100 def Terminate(self):
101 for t in self.benchmark_runs:
102 if t.isAlive():
103 self.l.LogError("Terminating run: '%s'." % t.name)
104 t.Terminate()
105
106 def IsComplete(self):
107 if self.active_threads:
108 for t in self.active_threads:
109 if t.isAlive():
110 t.join(0)
111 if not t.isAlive():
112 self.num_complete += 1
Ahmad Sharif4467f002012-12-20 12:09:49 -0800113 if not t.cache_hit:
114 self.num_run_complete += 1
Ahmad Sharif0dcbc4b2012-02-02 16:37:18 -0800115 self.active_threads.remove(t)
116 return False
117 return True
118
119 def Run(self):
120 self.start_time = time.time()
121 self.active_threads = []
122 for benchmark_run in self.benchmark_runs:
123 # Set threads to daemon so program exits when ctrl-c is pressed.
124 benchmark_run.daemon = True
125 benchmark_run.start()
126 self.active_threads.append(benchmark_run)
127
128 def SetCacheConditions(self, cache_conditions):
129 for benchmark_run in self.benchmark_runs:
130 benchmark_run.SetCacheConditions(cache_conditions)
131
132 def Cleanup(self):
133 self.machine_manager.Cleanup()