blob: aa53f1d686648a50d73549cf85f47429345c9e55 [file] [log] [blame]
cmtice4859f5f2014-05-30 13:15:37 -07001#!/usr/bin/python
2#
3# Copyright Google Inc. 2014
4
cmtice4536ef62014-07-08 11:17:21 -07005import datetime
cmtice4859f5f2014-05-30 13:15:37 -07006import optparse
7import os
cmtice4536ef62014-07-08 11:17:21 -07008import sys
9import time
cmtice4859f5f2014-05-30 13:15:37 -070010
11from utils import constants
12from utils import command_executer
13
cmtice4536ef62014-07-08 11:17:21 -070014WEEKDAYS = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
Luis Lozanof2a3ef42015-12-15 13:49:30 -080015DATA_ROOT_DIR = os.path.join(constants.CROSTC_WORKSPACE, 'weekly_test_data')
cmtice4859f5f2014-05-30 13:15:37 -070016EXPERIMENT_FILE = os.path.join(DATA_ROOT_DIR, 'weekly_report')
Luis Lozanof2a3ef42015-12-15 13:49:30 -080017MAIL_PROGRAM = '~/var/bin/mail-sheriff'
cmtice4859f5f2014-05-30 13:15:37 -070018
19
20def Generate_Vanilla_Report_File(vanilla_image_paths, board, remote,
21 chromeos_root, cmd_executer):
22
Luis Lozanof2a3ef42015-12-15 13:49:30 -080023 experiment_header = """
cmtice4859f5f2014-05-30 13:15:37 -070024name: weekly_vanilla_report
Luis Lozano7141db22015-04-08 15:07:35 -070025cache_only: True
26same_specs: False
cmtice4859f5f2014-05-30 13:15:37 -070027board: %s
28remote: %s
29""" % (board, remote)
30
Luis Lozanof2a3ef42015-12-15 13:49:30 -080031 experiment_tests = """
Luis Lozano95cd4492015-07-21 17:05:07 -070032benchmark: all_toolchain_perf {
cmtice4859f5f2014-05-30 13:15:37 -070033 suite: telemetry_Crosperf
34 iterations: 3
35}
36"""
37
Luis Lozanof2a3ef42015-12-15 13:49:30 -080038 filename = '%s_%s_vanilla.exp' % (EXPERIMENT_FILE, board)
39 if os.path.exists(filename):
40 cmd = 'rm %s' % filename
41 cmd_executer.RunCommand(cmd)
cmtice4859f5f2014-05-30 13:15:37 -070042
Luis Lozanof2a3ef42015-12-15 13:49:30 -080043 with open(filename, 'w') as f:
44 print >> f, experiment_header
45 print >> f, experiment_tests
cmtice4859f5f2014-05-30 13:15:37 -070046
Luis Lozanof2a3ef42015-12-15 13:49:30 -080047 # Add each vanilla image
48 for test_path in vanilla_image_paths:
49 pieces = test_path.split('/')
50 test_name = pieces[-1]
51 test_image = """
cmtice4859f5f2014-05-30 13:15:37 -070052%s {
53 chromeos_root: %s
54 chromeos_image: %s
55}
Luis Lozanof2a3ef42015-12-15 13:49:30 -080056""" % (test_name, chromeos_root, os.path.join(test_path,
57 'chromiumos_test_image.bin'))
58 print >> f, test_image
cmtice4859f5f2014-05-30 13:15:37 -070059
Luis Lozanof2a3ef42015-12-15 13:49:30 -080060 return filename
61
cmtice4859f5f2014-05-30 13:15:37 -070062
63def Generate_Test_File(test_image_paths, vanilla_image_path, board, remote,
64 chromeos_root, cmd_executer):
65
Luis Lozanof2a3ef42015-12-15 13:49:30 -080066 experiment_header = """
cmtice4859f5f2014-05-30 13:15:37 -070067name: weekly_report
Luis Lozano7141db22015-04-08 15:07:35 -070068cache_only: True
69same_specs: False
cmtice4859f5f2014-05-30 13:15:37 -070070board: %s
71remote: %s
72""" % (board, remote)
73
Luis Lozanof2a3ef42015-12-15 13:49:30 -080074 experiment_tests = """
Luis Lozano95cd4492015-07-21 17:05:07 -070075benchmark: all_toolchain_perf {
cmtice4859f5f2014-05-30 13:15:37 -070076 suite: telemetry_Crosperf
77 iterations: 3
78}
79"""
80
Luis Lozanof2a3ef42015-12-15 13:49:30 -080081 filename = '%s_%s.exp' % (EXPERIMENT_FILE, board)
82 if os.path.exists(filename):
83 cmd = 'rm %s' % filename
84 cmd_executer.RunCommand(cmd)
cmtice4859f5f2014-05-30 13:15:37 -070085
Luis Lozanof2a3ef42015-12-15 13:49:30 -080086 with open(filename, 'w') as f:
87 print >> f, experiment_header
88 print >> f, experiment_tests
cmtice4859f5f2014-05-30 13:15:37 -070089
Luis Lozanof2a3ef42015-12-15 13:49:30 -080090 # Add vanilla image (first)
91 vanilla_image = """
Luis Lozano8a68b2d2015-04-23 14:37:09 -070092%s {
cmtice4859f5f2014-05-30 13:15:37 -070093 chromeos_root: %s
94 chromeos_image: %s
95}
Luis Lozanof2a3ef42015-12-15 13:49:30 -080096""" % (vanilla_image_path.split('/')[-1], chromeos_root,
97 os.path.join(vanilla_image_path, 'chromiumos_test_image.bin'))
cmtice4859f5f2014-05-30 13:15:37 -070098
Luis Lozanof2a3ef42015-12-15 13:49:30 -080099 print >> f, vanilla_image
cmtice4859f5f2014-05-30 13:15:37 -0700100
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800101 # Add each test image
102 for test_path in test_image_paths:
103 pieces = test_path.split('/')
104 test_name = pieces[-1]
105 test_image = """
cmtice4859f5f2014-05-30 13:15:37 -0700106%s {
107 chromeos_root: %s
108 chromeos_image: %s
109}
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800110""" % (test_name, chromeos_root, os.path.join(test_path,
111 'chromiumos_test_image.bin'))
112 print >> f, test_image
cmtice4859f5f2014-05-30 13:15:37 -0700113
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800114 return filename
cmtice4859f5f2014-05-30 13:15:37 -0700115
116
117def Main(argv):
118
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800119 parser = optparse.OptionParser()
120 parser.add_option('-b', '--board', dest='board', help='Target board.')
121 parser.add_option('-r', '--remote', dest='remote', help='Target device.')
122 parser.add_option('-v',
123 '--vanilla_only',
124 dest='vanilla_only',
125 action='store_true',
cmtice4859f5f2014-05-30 13:15:37 -0700126 default=False,
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800127 help='Generate a report comparing only the vanilla images.')
cmtice4859f5f2014-05-30 13:15:37 -0700128
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800129 options = parser.parse_args(argv[1:])[0]
cmtice4859f5f2014-05-30 13:15:37 -0700130
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800131 if not options.board:
132 print 'Must specify a board.'
133 return 1
cmtice4859f5f2014-05-30 13:15:37 -0700134
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800135 if not options.remote:
136 print 'Must specify at least one remote.'
137 return 1
cmtice4859f5f2014-05-30 13:15:37 -0700138
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800139 cmd_executer = command_executer.GetCommandExecuter(log_level='average')
cmtice4859f5f2014-05-30 13:15:37 -0700140
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800141 # Find starting index, for cycling through days of week, generating
142 # reports starting 6 days ago from today. Generate list of indices for
143 # order in which to look at weekdays for report:
144 todays_index = datetime.datetime.today().isoweekday()
145 indices = []
146 start = todays_index + 1
147 end = start + 7
148 for i in range(start, end):
149 indices.append(i % 7)
150 # E.g. if today is Sunday, then start report with last Monday, so
151 # indices = [1, 2, 3, 4, 5, 6, 0].
cmtice4536ef62014-07-08 11:17:21 -0700152
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800153 # Find all the test image tar files, untar them and add them to
154 # the list. Also find and untar vanilla image tar files, and keep
155 # track of the first vanilla image.
156 report_image_paths = []
157 vanilla_image_paths = []
158 first_vanilla_image = None
159 for i in indices:
160 day = WEEKDAYS[i]
161 data_path = os.path.join(DATA_ROOT_DIR, options.board, day)
162 if os.path.exists(data_path):
163 # First, untar the test image.
164 tar_file_name = '%s_test_image.tar' % day
165 tar_file_path = os.path.join(data_path, tar_file_name)
166 image_dir = '%s_test_image' % day
167 image_path = os.path.join(data_path, image_dir)
168 if os.path.exists(tar_file_path):
169 if not os.path.exists(image_path):
170 os.makedirs(image_path)
171 cmd = ('cd %s; tar -xvf %s -C %s --strip-components 1' %
172 (data_path, tar_file_path, image_path))
173 ret = cmd_executer.RunCommand(cmd)
174 if not ret:
175 report_image_paths.append(image_path)
176 # Next, untar the vanilla image.
177 vanilla_file = '%s_vanilla_image.tar' % day
178 v_file_path = os.path.join(data_path, vanilla_file)
179 image_dir = '%s_vanilla_image' % day
180 image_path = os.path.join(data_path, image_dir)
181 if os.path.exists(v_file_path):
182 if not os.path.exists(image_path):
183 os.makedirs(image_path)
184 cmd = ('cd %s; tar -xvf %s -C %s --strip-components 1' %
185 (data_path, v_file_path, image_path))
186 ret = cmd_executer.RunCommand(cmd)
187 if not ret:
188 vanilla_image_paths.append(image_path)
189 if not first_vanilla_image:
190 first_vanilla_image = image_path
cmtice4536ef62014-07-08 11:17:21 -0700191
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800192 # Find a chroot we can use. Look for a directory containing both
193 # an experiment file and a chromeos directory (the experiment file will
194 # only be created if both images built successfully, i.e. the chroot is
195 # good).
196 chromeos_root = None
197 timestamp = datetime.datetime.strftime(datetime.datetime.now(),
198 '%Y-%m-%d_%H:%M:%S')
199 results_dir = os.path.join(
200 os.path.expanduser('~/nightly_test_reports'), '%s.%s' % (
201 timestamp, options.board), 'weekly_tests')
cmtice4536ef62014-07-08 11:17:21 -0700202
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800203 for day in WEEKDAYS:
204 startdir = os.path.join(constants.CROSTC_WORKSPACE, day)
205 num_dirs = os.listdir(startdir)
206 for d in num_dirs:
207 exp_file = os.path.join(startdir, d, 'toolchain_experiment.txt')
208 chroot = os.path.join(startdir, d, 'chromeos')
209 if os.path.exists(chroot) and os.path.exists(exp_file):
210 chromeos_root = chroot
211 if chromeos_root:
212 break
213 if chromeos_root:
214 break
cmtice4859f5f2014-05-30 13:15:37 -0700215
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800216 if not chromeos_root:
217 print 'Unable to locate a usable chroot. Exiting without report.'
218 return 1
cmtice3717ea82015-06-24 16:37:40 -0700219
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800220 # Create the Crosperf experiment file for generating the weekly report.
221 if not options.vanilla_only:
222 filename = Generate_Test_File(report_image_paths, first_vanilla_image,
223 options.board, options.remote, chromeos_root,
224 cmd_executer)
225 else:
226 filename = Generate_Vanilla_Report_File(vanilla_image_paths, options.board,
227 options.remote, chromeos_root,
228 cmd_executer)
cmtice4859f5f2014-05-30 13:15:37 -0700229
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800230 # Run Crosperf on the file to generate the weekly report.
231 cmd = ('%s/toolchain-utils/crosperf/crosperf '
232 '%s --no_email=True --results_dir=%s' %
233 (constants.CROSTC_WORKSPACE, filename, results_dir))
234 retval = cmd_executer.RunCommand(cmd)
235 if retval == 0:
236 # Send the email, if the crosperf command worked.
237 filename = os.path.join(results_dir, 'msg_body.html')
238 if (os.path.exists(filename) and
239 os.path.exists(os.path.expanduser(MAIL_PROGRAM))):
240 vanilla_string = ' '
241 if options.vanilla_only:
242 vanilla_string = ' Vanilla '
243 command = ('cat %s | %s -s "Weekly%sReport results, %s" -team -html' %
244 (filename, MAIL_PROGRAM, vanilla_string, options.board))
245 retval = cmd_executer.RunCommand(command)
cmtice4859f5f2014-05-30 13:15:37 -0700246
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800247 return retval
cmtice4859f5f2014-05-30 13:15:37 -0700248
249
250if __name__ == '__main__':
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800251 retval = Main(sys.argv)
252 sys.exit(retval)