summaryrefslogtreecommitdiff
path: root/scripts/tracecheck.py
blob: d204fa63dbfcbc7a7a6672b888cfea3caac35078 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env python
##########################################################################
#
# Copyright 2011 VMware, Inc.
# All Rights Reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the 'Software'), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
##########################################################################/

'''Check a trace replays successfully or not.

It is meant to be used with git bisect.  See git bisect manpage for more
details.
'''


import optparse
import os.path
import platform
import re
import subprocess
import sys
import traceback


def good():
    '''Tell git-bisect that this commit is good.'''

    sys.stdout.write('GOOD\n')
    sys.exit(0)


def bad():
    '''Tell git-bisect that this commit is bad.'''

    sys.stdout.write('BAD\n')
    sys.exit(1)


def skip():
    '''Tell git-bisect to skip this commit.'''

    sys.stdout.write('SKIP\n')
    sys.exit(125)


def abort():
    '''Tell git-bisect to abort.'''

    sys.stdout.write('ABORT\n')
    sys.exit(-1)


def which(executable):
    '''Search for the executable on the PATH.'''

    if platform.system() == 'Windows':
        exts = ['.exe']
    else:
        exts = ['']
    dirs = os.environ['PATH'].split(os.path.pathsep)
    for dir in dirs:
        path = os.path.join(dir, executable)
        for ext in exts:
            if os.path.exists(path + ext):
                return True
    return False


def main():
    '''Main program.

    It will always invoke sys.exit(), and never return normally.
    '''

    # Try to guess the build command.
    if os.path.exists('SConstruct'):
        default_build = 'scons'
    elif os.path.exists('Makefile'):
        default_build = 'make'
    else:
        default_build = None

    # Parse command line options
    optparser = optparse.OptionParser(
        usage='\n\tgit bisect run %prog [options] -- [glretrace options] <trace>',
        version='%%prog')
    optparser.add_option(
        '-b', '--build', metavar='COMMAND',
        type='string', dest='build', default=default_build,
        help='build command [default: %default]')
    optparser.add_option(
        '-r', '--retrace', metavar='PROGRAM',
        type='string', dest='retrace', default='glretrace',
        help='retrace command [default: %default]')
    optparser.add_option(
        '-c', '--compare', metavar='PREFIX',
        type='string', dest='compare_prefix', default=None,
        help='snapshot comparison prefix')
    optparser.add_option(
        '--precision-threshold', metavar='BITS',
        type='float', dest='precision_threshold', default=8.0,
        help='precision threshold in bits [default: %default]')
    optparser.add_option(
        '--gl-renderer', metavar='REGEXP',
        type='string', dest='gl_renderer_re', default='^.*$',
        help='require a matching GL_RENDERER string [default: %default]')

    (options, args) = optparser.parse_args(sys.argv[1:])
    if not args:
        optparser.error("incorrect number of arguments")

    # Build the source
    if options.build:
        sys.stdout.write(options.build + '\n')
        sys.stdout.flush()
        returncode = subprocess.call(options.build, shell=True)
        if returncode:
            skip()

    # TODO: For this to be useful on Windows we'll also need an installation
    # procedure here.

    # Do some sanity checks.  In particular we want to make sure that the GL
    # implementation is usable, and is the right one (i.e., we didn't fallback
    # to a different OpenGL implementation due to missing symbols).
    if platform.system() != 'Windows' and which('glxinfo'):
        p = subprocess.Popen(['glxinfo'], stdout=subprocess.PIPE)
        stdout, stderr = p.communicate()
        if p.returncode:
            skip()

        # Search for the GL_RENDERER string
        gl_renderer_header = 'OpenGL renderer string: '
        gl_renderer = ''
        for line in stdout.split('\n'):
            if line.startswith(gl_renderer_header):
                gl_renderer = line[len(gl_renderer_header):]
            if line.startswith('direct rendering: No'):
                sys.stderr.write('Indirect rendering.\n')
                skip()

        # and match it against the regular expression specified in the command
        # line.
        if not re.search(options.gl_renderer_re, gl_renderer):
            sys.stderr.write("GL_RENDERER mismatch: %r !~ /%s/\n"  % (gl_renderer, options.gl_renderer_re))
            skip()

    # Run glretrace
    command = [options.retrace]
    if options.compare_prefix:
        command += ['-c', options.compare_prefix]
    else:
        command += ['-b']
    command += args
    sys.stdout.write(' '.join(command) + '\n')
    sys.stdout.flush()
    p = subprocess.Popen(command, stdout=subprocess.PIPE)
    stdout, stderr = p.communicate()
    if p.returncode:
        if not options.compare_prefix:
            bad()
        else:
            skip()

    if options.compare_prefix:
        failed = False
        precision_re = re.compile('^Snapshot (\S+) average precision of (\S+) bits$')
        for line in stdout.split('\n'):
            mo = precision_re.match(line)
            if mo:
                print line
                call_no = int(mo.group(1))
                precision = float(mo.group(2))
                if precision < options.precision_threshold:
                    failed = True
        if failed:
            bad()

    # TODO: allow more criterias here, such as, performance threshold

    # Success
    good()


# Invoke main program, aborting the bisection on Ctrl+C or any uncaught Python
# exception.
if __name__ == '__main__':
    try:
        main()
    except SystemExit:
        raise
    except KeyboardInterrupt:
        abort()
    except:
        traceback.print_exc()
        abort()