summaryrefslogtreecommitdiffstats
path: root/Tools/Scripts/webkitpy/common/checkout/baselineoptimizer.py
blob: 8ae0bb9b9a6a02cea040afab57dc94f427a6a63f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# Copyright (C) 2011, Google Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#     * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
#     * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


# Yes, it's a hypergraph.
# FIXME: Should this function live with the ports somewhere?
# Perhaps this should move onto PortFactory?
def _baseline_search_hypergraph(host):
    hypergraph = {}

    # These edges in the hypergraph aren't visible on build.webkit.org,
    # but they impose constraints on how we optimize baselines.
    hypergraph.update(_VIRTUAL_PORTS)

    # FIXME: Should we get this constant from somewhere?
    fallback_path = ['LayoutTests']

    port_factory = host.port_factory
    for port_name in port_factory.all_port_names():
        port = port_factory.get(port_name)
        webkit_base = port.webkit_base()
        search_path = port.baseline_search_path()
        if search_path:
            hypergraph[port_name] = [host.filesystem.relpath(path, webkit_base) for path in search_path] + fallback_path
    return hypergraph


_VIRTUAL_PORTS = {
    'mac-future': ['LayoutTests/platform/mac-future', 'LayoutTests/platform/mac', 'LayoutTests'],
    'qt-unknown': ['LayoutTests/platform/qt-unknown', 'LayoutTests/platform/qt', 'LayoutTests'],
}


# FIXME: Should this function be somewhere more general?
def _invert_dictionary(dictionary):
    inverted_dictionary = {}
    for key, value in dictionary.items():
        if inverted_dictionary.get(value):
            inverted_dictionary[value].append(key)
        else:
            inverted_dictionary[value] = [key]
    return inverted_dictionary


class BaselineOptimizer(object):
    def __init__(self, host):
        self._host = host
        self._filesystem = self._host.filesystem
        self._scm = self._host.scm()
        self._hypergraph = _baseline_search_hypergraph(host)
        self._directories = reduce(set.union, map(set, self._hypergraph.values()))

    def _read_results_by_directory(self, baseline_name):
        results_by_directory = {}
        for directory in self._directories:
            path = self._filesystem.join(self._scm.checkout_root, directory, baseline_name)
            if self._filesystem.exists(path):
                results_by_directory[directory] = self._filesystem.sha1(path)
        return results_by_directory

    def _results_by_port_name(self, results_by_directory):
        results_by_port_name = {}
        for port_name, search_path in self._hypergraph.items():
            for directory in search_path:
                if directory in results_by_directory:
                    results_by_port_name[port_name] = results_by_directory[directory]
                    break
        return results_by_port_name

    def _most_specific_common_directory(self, port_names):
        paths = [self._hypergraph[port_name] for port_name in port_names]
        common_directories = reduce(set.intersection, map(set, paths))

        def score(directory):
            return sum([path.index(directory) for path in paths])

        _, directory = sorted([(score(directory), directory) for directory in common_directories])[0]
        return directory

    def _filter_port_names_by_result(self, predicate, port_names_by_result):
        filtered_port_names_by_result = {}
        for result, port_names in port_names_by_result.items():
            filtered_port_names = filter(predicate, port_names)
            if filtered_port_names:
                filtered_port_names_by_result[result] = filtered_port_names
        return filtered_port_names_by_result

    def _place_results_in_most_specific_common_directory(self, port_names_by_result, results_by_directory):
        for result, port_names in port_names_by_result.items():
            directory = self._most_specific_common_directory(port_names)
            results_by_directory[directory] = result

    def _find_optimal_result_placement(self, baseline_name):
        results_by_directory = self._read_results_by_directory(baseline_name)
        results_by_port_name = self._results_by_port_name(results_by_directory)
        port_names_by_result = _invert_dictionary(results_by_port_name)

        new_results_by_directory = {}
        unsatisfied_port_names_by_result = port_names_by_result
        while unsatisfied_port_names_by_result:
            self._place_results_in_most_specific_common_directory(unsatisfied_port_names_by_result, new_results_by_directory)
            new_results_by_port_name = self._results_by_port_name(new_results_by_directory)

            def is_unsatisfied(port_name):
                return results_by_port_name[port_name] != new_results_by_port_name[port_name]

            new_unsatisfied_port_names_by_result = self._filter_port_names_by_result(is_unsatisfied, port_names_by_result)

            if len(new_unsatisfied_port_names_by_result.values()) >= len(unsatisfied_port_names_by_result.values()):
                break  # Frowns. We do not appear to be converging.
            unsatisfied_port_names_by_result = new_unsatisfied_port_names_by_result

        self._filter_virtual_ports(new_results_by_directory)
        return results_by_directory, new_results_by_directory

    def _filter_virtual_ports(self, new_results_by_directory):
        for port in _VIRTUAL_PORTS:
            virtual_directory = _VIRTUAL_PORTS[port][0]
            if virtual_directory in new_results_by_directory:
                real_directory = _VIRTUAL_PORTS[port][1]
                if real_directory not in new_results_by_directory:
                    new_results_by_directory[real_directory] = new_results_by_directory[virtual_directory]
                del new_results_by_directory[virtual_directory]

    def _move_baselines(self, baseline_name, results_by_directory, new_results_by_directory):
        data_for_result = {}
        for directory, result in results_by_directory.items():
            if not result in data_for_result:
                source = self._filesystem.join(self._scm.checkout_root, directory, baseline_name)
                data_for_result[result] = self._filesystem.read_binary_file(source)

        file_names = []
        for directory, result in results_by_directory.items():
            if new_results_by_directory.get(directory) != result:
                file_names.append(self._filesystem.join(self._scm.checkout_root, directory, baseline_name))
        if file_names:
            self._scm.delete_list(file_names)

        file_names = []
        for directory, result in new_results_by_directory.items():
            if results_by_directory.get(directory) != result:
                destination = self._filesystem.join(self._scm.checkout_root, directory, baseline_name)
                self._filesystem.maybe_make_directory(self._filesystem.split(destination)[0])
                self._filesystem.write_binary_file(destination, data_for_result[result])
                file_names.append(destination)
        if file_names:
            self._scm.add_list(file_names)

    def directories_by_result(self, baseline_name):
        results_by_directory = self._read_results_by_directory(baseline_name)
        return _invert_dictionary(results_by_directory)

    def optimize(self, baseline_name):
        results_by_directory, new_results_by_directory = self._find_optimal_result_placement(baseline_name)
        if self._results_by_port_name(results_by_directory) != self._results_by_port_name(new_results_by_directory):
            return False
        self._move_baselines(baseline_name, results_by_directory, new_results_by_directory)
        return True