blob: 01db867d2562b6aa98e9740d1fb16aba75a01501 [file] [log] [blame]
Caroline Ticef6ef4392017-04-06 17:16:05 -07001#!/usr/bin/env python2
cmtice4859f5f2014-05-30 13:15:37 -07002#
3# Copyright Google Inc. 2014
Caroline Tice88272d42016-01-13 09:48:29 -08004"""Module to generate the 7-day crosperf reports."""
cmtice4859f5f2014-05-30 13:15:37 -07005
Caroline Tice88272d42016-01-13 09:48:29 -08006from __future__ import print_function
7
8import argparse
cmtice4536ef62014-07-08 11:17:21 -07009import datetime
cmtice4859f5f2014-05-30 13:15:37 -070010import os
cmtice4536ef62014-07-08 11:17:21 -070011import sys
cmtice4859f5f2014-05-30 13:15:37 -070012
Caroline Tice88272d42016-01-13 09:48:29 -080013from cros_utils import constants
14from cros_utils import command_executer
cmtice4859f5f2014-05-30 13:15:37 -070015
cmtice4536ef62014-07-08 11:17:21 -070016WEEKDAYS = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
Luis Lozanof2a3ef42015-12-15 13:49:30 -080017DATA_ROOT_DIR = os.path.join(constants.CROSTC_WORKSPACE, 'weekly_test_data')
cmtice4859f5f2014-05-30 13:15:37 -070018EXPERIMENT_FILE = os.path.join(DATA_ROOT_DIR, 'weekly_report')
Luis Lozanof2a3ef42015-12-15 13:49:30 -080019MAIL_PROGRAM = '~/var/bin/mail-sheriff'
cmtice4859f5f2014-05-30 13:15:37 -070020
21
22def Generate_Vanilla_Report_File(vanilla_image_paths, board, remote,
23 chromeos_root, cmd_executer):
24
Luis Lozanof2a3ef42015-12-15 13:49:30 -080025 experiment_header = """
cmtice4859f5f2014-05-30 13:15:37 -070026name: weekly_vanilla_report
Luis Lozano7141db22015-04-08 15:07:35 -070027cache_only: True
28same_specs: False
cmtice4859f5f2014-05-30 13:15:37 -070029board: %s
30remote: %s
31""" % (board, remote)
32
Luis Lozanof2a3ef42015-12-15 13:49:30 -080033 experiment_tests = """
Luis Lozano95cd4492015-07-21 17:05:07 -070034benchmark: all_toolchain_perf {
cmtice4859f5f2014-05-30 13:15:37 -070035 suite: telemetry_Crosperf
36 iterations: 3
37}
38"""
39
Luis Lozanof2a3ef42015-12-15 13:49:30 -080040 filename = '%s_%s_vanilla.exp' % (EXPERIMENT_FILE, board)
41 if os.path.exists(filename):
42 cmd = 'rm %s' % filename
43 cmd_executer.RunCommand(cmd)
cmtice4859f5f2014-05-30 13:15:37 -070044
Luis Lozanof2a3ef42015-12-15 13:49:30 -080045 with open(filename, 'w') as f:
Caroline Tice88272d42016-01-13 09:48:29 -080046 f.write(experiment_header)
47 f.write(experiment_tests)
cmtice4859f5f2014-05-30 13:15:37 -070048
Luis Lozanof2a3ef42015-12-15 13:49:30 -080049 # Add each vanilla image
50 for test_path in vanilla_image_paths:
51 pieces = test_path.split('/')
52 test_name = pieces[-1]
53 test_image = """
cmtice4859f5f2014-05-30 13:15:37 -070054%s {
55 chromeos_root: %s
56 chromeos_image: %s
57}
Caroline Ticef6ef4392017-04-06 17:16:05 -070058""" % (test_name, chromeos_root,
59 os.path.join(test_path, 'chromiumos_test_image.bin'))
Caroline Tice88272d42016-01-13 09:48:29 -080060 f.write(test_image)
cmtice4859f5f2014-05-30 13:15:37 -070061
Luis Lozanof2a3ef42015-12-15 13:49:30 -080062 return filename
63
cmtice4859f5f2014-05-30 13:15:37 -070064
65def Generate_Test_File(test_image_paths, vanilla_image_path, board, remote,
66 chromeos_root, cmd_executer):
67
Luis Lozanof2a3ef42015-12-15 13:49:30 -080068 experiment_header = """
cmtice4859f5f2014-05-30 13:15:37 -070069name: weekly_report
Luis Lozano7141db22015-04-08 15:07:35 -070070cache_only: True
71same_specs: False
cmtice4859f5f2014-05-30 13:15:37 -070072board: %s
73remote: %s
74""" % (board, remote)
75
Luis Lozanof2a3ef42015-12-15 13:49:30 -080076 experiment_tests = """
Luis Lozano95cd4492015-07-21 17:05:07 -070077benchmark: all_toolchain_perf {
cmtice4859f5f2014-05-30 13:15:37 -070078 suite: telemetry_Crosperf
79 iterations: 3
80}
81"""
82
Luis Lozanof2a3ef42015-12-15 13:49:30 -080083 filename = '%s_%s.exp' % (EXPERIMENT_FILE, board)
84 if os.path.exists(filename):
85 cmd = 'rm %s' % filename
86 cmd_executer.RunCommand(cmd)
cmtice4859f5f2014-05-30 13:15:37 -070087
Luis Lozanof2a3ef42015-12-15 13:49:30 -080088 with open(filename, 'w') as f:
Caroline Tice88272d42016-01-13 09:48:29 -080089 f.write(experiment_header)
90 f.write(experiment_tests)
cmtice4859f5f2014-05-30 13:15:37 -070091
Luis Lozanof2a3ef42015-12-15 13:49:30 -080092 # Add vanilla image (first)
93 vanilla_image = """
Luis Lozano8a68b2d2015-04-23 14:37:09 -070094%s {
cmtice4859f5f2014-05-30 13:15:37 -070095 chromeos_root: %s
96 chromeos_image: %s
97}
Luis Lozanof2a3ef42015-12-15 13:49:30 -080098""" % (vanilla_image_path.split('/')[-1], chromeos_root,
99 os.path.join(vanilla_image_path, 'chromiumos_test_image.bin'))
cmtice4859f5f2014-05-30 13:15:37 -0700100
Caroline Tice88272d42016-01-13 09:48:29 -0800101 f.write(vanilla_image)
cmtice4859f5f2014-05-30 13:15:37 -0700102
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800103 # Add each test image
104 for test_path in test_image_paths:
105 pieces = test_path.split('/')
106 test_name = pieces[-1]
107 test_image = """
cmtice4859f5f2014-05-30 13:15:37 -0700108%s {
109 chromeos_root: %s
110 chromeos_image: %s
111}
Caroline Ticef6ef4392017-04-06 17:16:05 -0700112""" % (test_name, chromeos_root,
113 os.path.join(test_path, 'chromiumos_test_image.bin'))
Caroline Tice88272d42016-01-13 09:48:29 -0800114 f.write(test_image)
cmtice4859f5f2014-05-30 13:15:37 -0700115
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800116 return filename
cmtice4859f5f2014-05-30 13:15:37 -0700117
118
119def Main(argv):
120
Caroline Tice88272d42016-01-13 09:48:29 -0800121 parser = argparse.ArgumentParser()
122 parser.add_argument('-b', '--board', dest='board', help='Target board.')
123 parser.add_argument('-r', '--remote', dest='remote', help='Target device.')
Caroline Ticef6ef4392017-04-06 17:16:05 -0700124 parser.add_argument(
125 '-v',
126 '--vanilla_only',
127 dest='vanilla_only',
128 action='store_true',
129 default=False,
130 help='Generate a report comparing only the vanilla '
131 'images.')
cmtice4859f5f2014-05-30 13:15:37 -0700132
Caroline Tice88272d42016-01-13 09:48:29 -0800133 options = parser.parse_args(argv[1:])
cmtice4859f5f2014-05-30 13:15:37 -0700134
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800135 if not options.board:
Caroline Tice88272d42016-01-13 09:48:29 -0800136 print('Must specify a board.')
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800137 return 1
cmtice4859f5f2014-05-30 13:15:37 -0700138
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800139 if not options.remote:
Caroline Tice88272d42016-01-13 09:48:29 -0800140 print('Must specify at least one remote.')
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800141 return 1
cmtice4859f5f2014-05-30 13:15:37 -0700142
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800143 cmd_executer = command_executer.GetCommandExecuter(log_level='average')
cmtice4859f5f2014-05-30 13:15:37 -0700144
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800145 # Find starting index, for cycling through days of week, generating
146 # reports starting 6 days ago from today. Generate list of indices for
147 # order in which to look at weekdays for report:
148 todays_index = datetime.datetime.today().isoweekday()
149 indices = []
150 start = todays_index + 1
151 end = start + 7
152 for i in range(start, end):
153 indices.append(i % 7)
154 # E.g. if today is Sunday, then start report with last Monday, so
155 # indices = [1, 2, 3, 4, 5, 6, 0].
cmtice4536ef62014-07-08 11:17:21 -0700156
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800157 # Find all the test image tar files, untar them and add them to
158 # the list. Also find and untar vanilla image tar files, and keep
159 # track of the first vanilla image.
160 report_image_paths = []
161 vanilla_image_paths = []
162 first_vanilla_image = None
163 for i in indices:
164 day = WEEKDAYS[i]
165 data_path = os.path.join(DATA_ROOT_DIR, options.board, day)
166 if os.path.exists(data_path):
167 # First, untar the test image.
168 tar_file_name = '%s_test_image.tar' % day
169 tar_file_path = os.path.join(data_path, tar_file_name)
170 image_dir = '%s_test_image' % day
171 image_path = os.path.join(data_path, image_dir)
172 if os.path.exists(tar_file_path):
173 if not os.path.exists(image_path):
174 os.makedirs(image_path)
175 cmd = ('cd %s; tar -xvf %s -C %s --strip-components 1' %
176 (data_path, tar_file_path, image_path))
177 ret = cmd_executer.RunCommand(cmd)
178 if not ret:
179 report_image_paths.append(image_path)
180 # Next, untar the vanilla image.
181 vanilla_file = '%s_vanilla_image.tar' % day
182 v_file_path = os.path.join(data_path, vanilla_file)
183 image_dir = '%s_vanilla_image' % day
184 image_path = os.path.join(data_path, image_dir)
185 if os.path.exists(v_file_path):
186 if not os.path.exists(image_path):
187 os.makedirs(image_path)
188 cmd = ('cd %s; tar -xvf %s -C %s --strip-components 1' %
189 (data_path, v_file_path, image_path))
190 ret = cmd_executer.RunCommand(cmd)
191 if not ret:
192 vanilla_image_paths.append(image_path)
193 if not first_vanilla_image:
194 first_vanilla_image = image_path
cmtice4536ef62014-07-08 11:17:21 -0700195
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800196 # Find a chroot we can use. Look for a directory containing both
197 # an experiment file and a chromeos directory (the experiment file will
198 # only be created if both images built successfully, i.e. the chroot is
199 # good).
200 chromeos_root = None
201 timestamp = datetime.datetime.strftime(datetime.datetime.now(),
202 '%Y-%m-%d_%H:%M:%S')
203 results_dir = os.path.join(
Caroline Ticef6ef4392017-04-06 17:16:05 -0700204 os.path.expanduser('~/nightly_test_reports'),
205 '%s.%s' % (timestamp, options.board), 'weekly_tests')
cmtice4536ef62014-07-08 11:17:21 -0700206
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800207 for day in WEEKDAYS:
208 startdir = os.path.join(constants.CROSTC_WORKSPACE, day)
209 num_dirs = os.listdir(startdir)
210 for d in num_dirs:
211 exp_file = os.path.join(startdir, d, 'toolchain_experiment.txt')
212 chroot = os.path.join(startdir, d, 'chromeos')
213 if os.path.exists(chroot) and os.path.exists(exp_file):
214 chromeos_root = chroot
215 if chromeos_root:
216 break
217 if chromeos_root:
218 break
cmtice4859f5f2014-05-30 13:15:37 -0700219
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800220 if not chromeos_root:
Caroline Tice88272d42016-01-13 09:48:29 -0800221 print('Unable to locate a usable chroot. Exiting without report.')
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800222 return 1
cmtice3717ea82015-06-24 16:37:40 -0700223
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800224 # Create the Crosperf experiment file for generating the weekly report.
225 if not options.vanilla_only:
226 filename = Generate_Test_File(report_image_paths, first_vanilla_image,
227 options.board, options.remote, chromeos_root,
228 cmd_executer)
229 else:
230 filename = Generate_Vanilla_Report_File(vanilla_image_paths, options.board,
231 options.remote, chromeos_root,
232 cmd_executer)
cmtice4859f5f2014-05-30 13:15:37 -0700233
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800234 # Run Crosperf on the file to generate the weekly report.
235 cmd = ('%s/toolchain-utils/crosperf/crosperf '
Caroline Ticef6ef4392017-04-06 17:16:05 -0700236 '%s --no_email=True --results_dir=%s' % (constants.CROSTC_WORKSPACE,
237 filename, results_dir))
Caroline Tice88272d42016-01-13 09:48:29 -0800238 retv = cmd_executer.RunCommand(cmd)
239 if retv == 0:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800240 # Send the email, if the crosperf command worked.
241 filename = os.path.join(results_dir, 'msg_body.html')
242 if (os.path.exists(filename) and
243 os.path.exists(os.path.expanduser(MAIL_PROGRAM))):
244 vanilla_string = ' '
245 if options.vanilla_only:
246 vanilla_string = ' Vanilla '
247 command = ('cat %s | %s -s "Weekly%sReport results, %s" -team -html' %
248 (filename, MAIL_PROGRAM, vanilla_string, options.board))
Caroline Tice88272d42016-01-13 09:48:29 -0800249 retv = cmd_executer.RunCommand(command)
cmtice4859f5f2014-05-30 13:15:37 -0700250
Caroline Tice88272d42016-01-13 09:48:29 -0800251 return retv
cmtice4859f5f2014-05-30 13:15:37 -0700252
253
254if __name__ == '__main__':
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800255 retval = Main(sys.argv)
256 sys.exit(retval)