cmtice | 4859f5f | 2014-05-30 13:15:37 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # Copyright Google Inc. 2014 |
| 4 | |
cmtice | 4536ef6 | 2014-07-08 11:17:21 -0700 | [diff] [blame] | 5 | import datetime |
cmtice | 4859f5f | 2014-05-30 13:15:37 -0700 | [diff] [blame] | 6 | import optparse |
| 7 | import os |
cmtice | 4536ef6 | 2014-07-08 11:17:21 -0700 | [diff] [blame] | 8 | import sys |
| 9 | import time |
cmtice | 4859f5f | 2014-05-30 13:15:37 -0700 | [diff] [blame] | 10 | |
| 11 | from utils import constants |
| 12 | from utils import command_executer |
| 13 | |
cmtice | 4536ef6 | 2014-07-08 11:17:21 -0700 | [diff] [blame] | 14 | WEEKDAYS = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'] |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 15 | DATA_ROOT_DIR = os.path.join(constants.CROSTC_WORKSPACE, 'weekly_test_data') |
cmtice | 4859f5f | 2014-05-30 13:15:37 -0700 | [diff] [blame] | 16 | EXPERIMENT_FILE = os.path.join(DATA_ROOT_DIR, 'weekly_report') |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 17 | MAIL_PROGRAM = '~/var/bin/mail-sheriff' |
cmtice | 4859f5f | 2014-05-30 13:15:37 -0700 | [diff] [blame] | 18 | |
| 19 | |
| 20 | def Generate_Vanilla_Report_File(vanilla_image_paths, board, remote, |
| 21 | chromeos_root, cmd_executer): |
| 22 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 23 | experiment_header = """ |
cmtice | 4859f5f | 2014-05-30 13:15:37 -0700 | [diff] [blame] | 24 | name: weekly_vanilla_report |
Luis Lozano | 7141db2 | 2015-04-08 15:07:35 -0700 | [diff] [blame] | 25 | cache_only: True |
| 26 | same_specs: False |
cmtice | 4859f5f | 2014-05-30 13:15:37 -0700 | [diff] [blame] | 27 | board: %s |
| 28 | remote: %s |
| 29 | """ % (board, remote) |
| 30 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 31 | experiment_tests = """ |
Luis Lozano | 95cd449 | 2015-07-21 17:05:07 -0700 | [diff] [blame] | 32 | benchmark: all_toolchain_perf { |
cmtice | 4859f5f | 2014-05-30 13:15:37 -0700 | [diff] [blame] | 33 | suite: telemetry_Crosperf |
| 34 | iterations: 3 |
| 35 | } |
| 36 | """ |
| 37 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 38 | filename = '%s_%s_vanilla.exp' % (EXPERIMENT_FILE, board) |
| 39 | if os.path.exists(filename): |
| 40 | cmd = 'rm %s' % filename |
| 41 | cmd_executer.RunCommand(cmd) |
cmtice | 4859f5f | 2014-05-30 13:15:37 -0700 | [diff] [blame] | 42 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 43 | with open(filename, 'w') as f: |
| 44 | print >> f, experiment_header |
| 45 | print >> f, experiment_tests |
cmtice | 4859f5f | 2014-05-30 13:15:37 -0700 | [diff] [blame] | 46 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 47 | # 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 = """ |
cmtice | 4859f5f | 2014-05-30 13:15:37 -0700 | [diff] [blame] | 52 | %s { |
| 53 | chromeos_root: %s |
| 54 | chromeos_image: %s |
| 55 | } |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 56 | """ % (test_name, chromeos_root, os.path.join(test_path, |
| 57 | 'chromiumos_test_image.bin')) |
| 58 | print >> f, test_image |
cmtice | 4859f5f | 2014-05-30 13:15:37 -0700 | [diff] [blame] | 59 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 60 | return filename |
| 61 | |
cmtice | 4859f5f | 2014-05-30 13:15:37 -0700 | [diff] [blame] | 62 | |
| 63 | def Generate_Test_File(test_image_paths, vanilla_image_path, board, remote, |
| 64 | chromeos_root, cmd_executer): |
| 65 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 66 | experiment_header = """ |
cmtice | 4859f5f | 2014-05-30 13:15:37 -0700 | [diff] [blame] | 67 | name: weekly_report |
Luis Lozano | 7141db2 | 2015-04-08 15:07:35 -0700 | [diff] [blame] | 68 | cache_only: True |
| 69 | same_specs: False |
cmtice | 4859f5f | 2014-05-30 13:15:37 -0700 | [diff] [blame] | 70 | board: %s |
| 71 | remote: %s |
| 72 | """ % (board, remote) |
| 73 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 74 | experiment_tests = """ |
Luis Lozano | 95cd449 | 2015-07-21 17:05:07 -0700 | [diff] [blame] | 75 | benchmark: all_toolchain_perf { |
cmtice | 4859f5f | 2014-05-30 13:15:37 -0700 | [diff] [blame] | 76 | suite: telemetry_Crosperf |
| 77 | iterations: 3 |
| 78 | } |
| 79 | """ |
| 80 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 81 | filename = '%s_%s.exp' % (EXPERIMENT_FILE, board) |
| 82 | if os.path.exists(filename): |
| 83 | cmd = 'rm %s' % filename |
| 84 | cmd_executer.RunCommand(cmd) |
cmtice | 4859f5f | 2014-05-30 13:15:37 -0700 | [diff] [blame] | 85 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 86 | with open(filename, 'w') as f: |
| 87 | print >> f, experiment_header |
| 88 | print >> f, experiment_tests |
cmtice | 4859f5f | 2014-05-30 13:15:37 -0700 | [diff] [blame] | 89 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 90 | # Add vanilla image (first) |
| 91 | vanilla_image = """ |
Luis Lozano | 8a68b2d | 2015-04-23 14:37:09 -0700 | [diff] [blame] | 92 | %s { |
cmtice | 4859f5f | 2014-05-30 13:15:37 -0700 | [diff] [blame] | 93 | chromeos_root: %s |
| 94 | chromeos_image: %s |
| 95 | } |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 96 | """ % (vanilla_image_path.split('/')[-1], chromeos_root, |
| 97 | os.path.join(vanilla_image_path, 'chromiumos_test_image.bin')) |
cmtice | 4859f5f | 2014-05-30 13:15:37 -0700 | [diff] [blame] | 98 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 99 | print >> f, vanilla_image |
cmtice | 4859f5f | 2014-05-30 13:15:37 -0700 | [diff] [blame] | 100 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 101 | # 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 = """ |
cmtice | 4859f5f | 2014-05-30 13:15:37 -0700 | [diff] [blame] | 106 | %s { |
| 107 | chromeos_root: %s |
| 108 | chromeos_image: %s |
| 109 | } |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 110 | """ % (test_name, chromeos_root, os.path.join(test_path, |
| 111 | 'chromiumos_test_image.bin')) |
| 112 | print >> f, test_image |
cmtice | 4859f5f | 2014-05-30 13:15:37 -0700 | [diff] [blame] | 113 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 114 | return filename |
cmtice | 4859f5f | 2014-05-30 13:15:37 -0700 | [diff] [blame] | 115 | |
| 116 | |
| 117 | def Main(argv): |
| 118 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 119 | 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', |
cmtice | 4859f5f | 2014-05-30 13:15:37 -0700 | [diff] [blame] | 126 | default=False, |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 127 | help='Generate a report comparing only the vanilla images.') |
cmtice | 4859f5f | 2014-05-30 13:15:37 -0700 | [diff] [blame] | 128 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 129 | options = parser.parse_args(argv[1:])[0] |
cmtice | 4859f5f | 2014-05-30 13:15:37 -0700 | [diff] [blame] | 130 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 131 | if not options.board: |
| 132 | print 'Must specify a board.' |
| 133 | return 1 |
cmtice | 4859f5f | 2014-05-30 13:15:37 -0700 | [diff] [blame] | 134 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 135 | if not options.remote: |
| 136 | print 'Must specify at least one remote.' |
| 137 | return 1 |
cmtice | 4859f5f | 2014-05-30 13:15:37 -0700 | [diff] [blame] | 138 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 139 | cmd_executer = command_executer.GetCommandExecuter(log_level='average') |
cmtice | 4859f5f | 2014-05-30 13:15:37 -0700 | [diff] [blame] | 140 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 141 | # 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]. |
cmtice | 4536ef6 | 2014-07-08 11:17:21 -0700 | [diff] [blame] | 152 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 153 | # 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 |
cmtice | 4536ef6 | 2014-07-08 11:17:21 -0700 | [diff] [blame] | 191 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 192 | # 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') |
cmtice | 4536ef6 | 2014-07-08 11:17:21 -0700 | [diff] [blame] | 202 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 203 | 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 |
cmtice | 4859f5f | 2014-05-30 13:15:37 -0700 | [diff] [blame] | 215 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 216 | if not chromeos_root: |
| 217 | print 'Unable to locate a usable chroot. Exiting without report.' |
| 218 | return 1 |
cmtice | 3717ea8 | 2015-06-24 16:37:40 -0700 | [diff] [blame] | 219 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 220 | # 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) |
cmtice | 4859f5f | 2014-05-30 13:15:37 -0700 | [diff] [blame] | 229 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 230 | # 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) |
cmtice | 4859f5f | 2014-05-30 13:15:37 -0700 | [diff] [blame] | 246 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 247 | return retval |
cmtice | 4859f5f | 2014-05-30 13:15:37 -0700 | [diff] [blame] | 248 | |
| 249 | |
| 250 | if __name__ == '__main__': |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 251 | retval = Main(sys.argv) |
| 252 | sys.exit(retval) |