weekly_report.py revision 776d528ff8fa7e66d6abf96a735050c70f950a2b
1#!/usr/bin/python
2#
3# Copyright Google Inc. 2014
4
5import datetime
6import optparse
7import os
8import sys
9import time
10
11from utils import constants
12from utils import command_executer
13
14WEEKDAYS = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
15
16DATA_ROOT_DIR = os.path.join(constants.CROSTC_WORKSPACE,
17                             'weekly_test_data')
18
19EXPERIMENT_FILE = os.path.join(DATA_ROOT_DIR, 'weekly_report')
20
21
22def Generate_Vanilla_Report_File(vanilla_image_paths, board, remote,
23                                 chromeos_root, cmd_executer):
24
25    experiment_header = """
26name: weekly_vanilla_report
27board: %s
28remote: %s
29""" % (board, remote)
30
31
32    experiment_tests = """
33benchmark: all_perfv2 {
34  suite: telemetry_Crosperf
35  iterations: 3
36}
37"""
38
39    filename = "%s_%s_vanilla.exp" % (EXPERIMENT_FILE, board)
40    if os.path.exists(filename):
41        cmd = "rm %s" % filename
42        cmd_executer.RunCommand(cmd)
43
44    with open(filename, "w") as f:
45        print >>f, experiment_header
46        print >>f, experiment_tests
47
48        # Add each vanilla image
49        for test_path in vanilla_image_paths:
50            pieces = test_path.split("/")
51            test_name = pieces[-1]
52            test_image = """
53%s {
54  chromeos_root: %s
55  chromeos_image: %s
56}
57""" % (test_name, chromeos_root, os.path.join (test_path, "chromiumos_test_image.bin"))
58            print >>f, test_image
59
60    return filename
61
62def Generate_Test_File(test_image_paths, vanilla_image_path, board, remote,
63                       chromeos_root, cmd_executer):
64
65    experiment_header = """
66name: weekly_report
67board: %s
68remote: %s
69""" % (board, remote)
70
71
72    experiment_tests = """
73benchmark: all_perfv2 {
74  suite: telemetry_Crosperf
75  iterations: 3
76}
77"""
78
79    filename = "%s_%s.exp" % (EXPERIMENT_FILE, board)
80    if os.path.exists(filename):
81        cmd = "rm %s" % filename
82        cmd_executer.RunCommand(cmd)
83
84    with open(filename, "w") as f:
85        print >>f, experiment_header
86        print >>f, experiment_tests
87
88        # Add vanilla image (first)
89        vanilla_image = """
90vanilla_image {
91  chromeos_root: %s
92  chromeos_image: %s
93}
94""" % (chromeos_root, os.path.join(vanilla_image_path, "chromiumos_test_image.bin"))
95
96        print >>f, vanilla_image
97
98        # Add each test image
99        for test_path in test_image_paths:
100            pieces = test_path.split("/")
101            test_name = pieces[-1]
102            test_image = """
103%s {
104  chromeos_root: %s
105  chromeos_image: %s
106}
107""" % (test_name, chromeos_root, os.path.join (test_path, "chromiumos_test_image.bin"))
108            print >>f, test_image
109
110    return filename
111
112
113
114def Main(argv):
115
116    parser = optparse.OptionParser()
117    parser.add_option('-b', '--board', dest='board',
118                      help='Target board.')
119    parser.add_option("-r", "--remote", dest="remote",
120                    help="Target device.")
121    parser.add_option("-v", "--vanilla_only", dest="vanilla_only",
122                    action="store_true",
123                    default=False,
124                    help="Generate a report comparing only the vanilla images.")
125
126    options = parser.parse_args(argv[1:])[0]
127
128    if not options.board:
129        print "Must specify a board."
130        return 1
131
132    if not options.remote:
133        print "Must specify at least one remote."
134        return 1
135
136    cmd_executer = command_executer.GetCommandExecuter(log_level="average")
137
138    # Find starting index, for cycling through days of week, generating
139    # reports starting 6 days ago from today. Generate list of indices for
140    # order in which to look at weekdays for report:
141    todays_index = datetime.datetime.today().isoweekday()
142    indices = []
143    start = todays_index + 1
144    end = start + 7
145    for i in range(start,end):
146      indices.append(i % 7)
147    # E.g. if today is Sunday, then start report with last Monday, so
148    # indices = [1, 2, 3, 4, 5, 6, 0].
149
150
151
152    # Find all the test image tar files, untar them and add them to
153    # the list. Also find and untar vanilla image tar files, and keep
154    # track of the first vanilla image.
155    report_image_paths = []
156    vanilla_image_paths = []
157    first_vanilla_image = None
158    for i in indices:
159        day = WEEKDAYS[i]
160        data_path = os.path.join(DATA_ROOT_DIR, options.board, day)
161        if os.path.exists(data_path):
162            # First, untar the test image.
163            tar_file_name = "%s_test_image.tar" % day
164            tar_file_path = os.path.join(data_path, tar_file_name)
165            image_dir = "%s_test_image" % day
166            image_path = os.path.join(data_path, image_dir)
167            if os.path.exists(tar_file_path):
168                if not os.path.exists(image_path):
169                    os.makedirs(image_path)
170                cmd = ("cd %s; tar -xvf %s -C %s --strip-components 1" %
171                       (data_path, tar_file_path, image_path))
172                ret = cmd_executer.RunCommand(cmd)
173                if not ret:
174                    report_image_paths.append(image_path)
175            # Next, untar the vanilla image.
176            vanilla_file = "%s_vanilla_image.tar" % day
177            v_file_path = os.path.join(data_path, vanilla_file)
178            image_dir = "%s_vanilla_image" % day
179            image_path = os.path.join(data_path, image_dir)
180            if os.path.exists(v_file_path):
181                if not os.path.exists(image_path):
182                    os.makedirs(image_path)
183                cmd = ("cd %s; tar -xvf %s -C %s --strip-components 1" %
184                       (data_path, v_file_path, image_path))
185                ret = cmd_executer.RunCommand(cmd)
186                if not ret:
187                    vanilla_image_paths.append(image_path)
188                if not first_vanilla_image:
189                    first_vanilla_image = image_path
190
191    # Find a chroot we can use.  Look for a directory containing both
192    # an experiment file and a chromeos directory (the experiment file will
193    # only be created if both images built successfully, i.e. the chroot is
194    # good).
195    chromeos_root = None
196    for day in WEEKDAYS:
197        startdir = os.path.join(constants.CROSTC_WORKSPACE, day)
198        num_dirs = os.listdir(startdir)
199        for d in num_dirs:
200            exp_file = os.path.join(startdir, d, "toolchain_experiment.txt")
201            chroot = os.path.join(startdir, d, "chromeos")
202            if os.path.exists(chroot) and os.path.exists(exp_file):
203                chromeos_root = chroot
204            if chromeos_root:
205                break;
206        if chromeos_root:
207            break;
208
209    if not chromeos_root:
210        print "Unable to locate a usable chroot. Exiting without report."
211        return 1
212
213
214    # Create the Crosperf experiment file for generating the weekly report.
215    if not options.vanilla_only:
216        filename = Generate_Test_File (report_image_paths, first_vanilla_image,
217                                       options.board, options.remote,
218                                       chromeos_root, cmd_executer)
219    else:
220        filename = Generate_Vanilla_Report_File (vanilla_image_paths,
221                                                 options.board, options.remote,
222                                                 chromeos_root, cmd_executer)
223
224    # Run Crosperf on the file to generate the weekly report.
225    cmd = ("%s/toolchain-utils/crosperf/crosperf "
226           "%s  --cache_only=True --email=c-compiler-chrome@google.com" %
227           (constants.CROSTC_WORKSPACE, filename))
228    retval = cmd_executer.RunCommand(cmd)
229    return retval
230
231
232
233if __name__ == '__main__':
234    retval = Main(sys.argv)
235    sys.exit(retval)
236