José Fonseca | 2cce192 | 2012-12-11 19:51:26 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | ########################################################################## |
| 3 | # |
| 4 | # Copyright 2012 VMware Inc. |
| 5 | # All Rights Reserved. |
| 6 | # |
| 7 | # Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | # of this software and associated documentation files (the 'Software'), to deal |
| 9 | # in the Software without restriction, including without limitation the rights |
| 10 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | # copies of the Software, and to permit persons to whom the Software is |
| 12 | # furnished to do so, subject to the following conditions: |
| 13 | # |
| 14 | # The above copyright notice and this permission notice shall be included in |
| 15 | # all copies or substantial portions of the Software. |
| 16 | # |
| 17 | # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | # THE SOFTWARE. |
| 24 | # |
| 25 | ##########################################################################/ |
| 26 | |
| 27 | '''Convert traces to/from PIX. |
| 28 | ''' |
| 29 | |
| 30 | |
| 31 | import optparse |
| 32 | import os.path |
| 33 | import subprocess |
| 34 | import platform |
| 35 | import sys |
| 36 | |
| 37 | |
José Fonseca | 8b9f78b | 2013-04-25 15:22:53 +0100 | [diff] [blame] | 38 | def getPixExe(): |
José Fonseca | 2cce192 | 2012-12-11 19:51:26 +0000 | [diff] [blame] | 39 | try: |
José Fonseca | 87f97d2 | 2012-12-11 20:37:39 +0000 | [diff] [blame] | 40 | programFiles = os.environ['ProgramFiles(x86)'] |
| 41 | except KeyError: |
| 42 | programFiles = os.environ['ProgramFiles'] |
| 43 | try: |
José Fonseca | 2cce192 | 2012-12-11 19:51:26 +0000 | [diff] [blame] | 44 | dxsdkDir = os.environ['DXSDK_DIR'] |
| 45 | except KeyError: |
| 46 | dxsdkDir = os.path.join(programFiles, "Microsoft DirectX SDL (June 2010)") |
José Fonseca | 8b9f78b | 2013-04-25 15:22:53 +0100 | [diff] [blame] | 47 | pixExe = os.path.join(dxsdkDir, "Utilities", "bin", 'x86', 'PIXwin.exe') |
| 48 | return pixExe |
| 49 | |
| 50 | |
| 51 | def callProcess(cmd): |
| 52 | if options.verbose: |
| 53 | sys.stderr.write(' '.join(cmd) + '\n') |
| 54 | ret = subprocess.call(cmd) |
| 55 | if ret: |
| 56 | exeName = os.path.basename(cmd[0]) |
| 57 | sys.stderr.write('error: %s failed with exit code %u\n' % (exeName, ret)) |
| 58 | sys.exit(ret) |
| 59 | return ret |
| 60 | |
| 61 | |
| 62 | def convertToPix(inTrace, outPixrun): |
| 63 | pix = getPixPath() |
José Fonseca | 2cce192 | 2012-12-11 19:51:26 +0000 | [diff] [blame] | 64 | |
| 65 | pixExp = os.path.join(os.path.dirname(__file__), 'apitrace.PIXExp') |
| 66 | |
| 67 | # http://social.msdn.microsoft.com/Forums/sv/devdocs/thread/15addc0c-036d-413a-854a-35637ccbb834 |
| 68 | # http://src.chromium.org/svn/trunk/o3d/tests/test_driver.py |
| 69 | cmd = [ |
José Fonseca | 8b9f78b | 2013-04-25 15:22:53 +0100 | [diff] [blame] | 70 | getPixExe(), |
José Fonseca | 2cce192 | 2012-12-11 19:51:26 +0000 | [diff] [blame] | 71 | pixExp, |
| 72 | '-start', |
| 73 | '-runfile', os.path.abspath(outPixrun), |
| 74 | '-targetpath', os.path.abspath(options.retrace), |
| 75 | #'-targetstartfolder', ..., |
| 76 | '-targetargs', os.path.abspath(inTrace), |
| 77 | ] |
| 78 | |
José Fonseca | 8b9f78b | 2013-04-25 15:22:53 +0100 | [diff] [blame] | 79 | callProcess(cmd) |
José Fonseca | 2cce192 | 2012-12-11 19:51:26 +0000 | [diff] [blame] | 80 | if os.path.exists(outPixrun): |
| 81 | sys.stderr.write('info: %s written\n' % outPixrun) |
José Fonseca | 8b9f78b | 2013-04-25 15:22:53 +0100 | [diff] [blame] | 82 | if options.verify: |
José Fonseca | 2cce192 | 2012-12-11 19:51:26 +0000 | [diff] [blame] | 83 | subprocess.call([pix, os.path.abspath(outPixrun)]) |
| 84 | else: |
| 85 | sys.stderr.write('error: %s not written\n' % outPixrun) |
| 86 | sys.exit(1) |
| 87 | |
| 88 | |
José Fonseca | 8b9f78b | 2013-04-25 15:22:53 +0100 | [diff] [blame] | 89 | def convertFromPix(inPix, outTrace): |
| 90 | pixExe = getPixExe() |
| 91 | |
| 92 | if False: |
| 93 | cmd = [ |
| 94 | pixExe, |
| 95 | inPix, |
| 96 | '-exporttocsv', 'c:\test.csv', |
| 97 | ] |
| 98 | callProcess(cmd) |
| 99 | |
| 100 | cmd = [ |
| 101 | options.apitrace, |
| 102 | 'trace', |
| 103 | '-a', options.api, |
| 104 | '-o', outTrace, |
| 105 | pixExe, |
| 106 | inPix, |
| 107 | '-playstandalone', |
| 108 | ] |
| 109 | |
| 110 | callProcess(cmd) |
| 111 | if os.path.exists(outTrace): |
| 112 | sys.stderr.write('info: %s written\n' % outTrace) |
| 113 | if options.verify: |
| 114 | subprocess.call([options.retrace, os.path.abspath(outTrace)]) |
| 115 | else: |
| 116 | sys.stderr.write('error: %s not written\n' % outTrace) |
| 117 | sys.exit(1) |
| 118 | |
| 119 | |
José Fonseca | 2cce192 | 2012-12-11 19:51:26 +0000 | [diff] [blame] | 120 | def main(): |
| 121 | global options |
| 122 | |
| 123 | # Parse command line options |
| 124 | optparser = optparse.OptionParser( |
| 125 | usage='\n\t%prog [options] <trace> ...', |
| 126 | version='%%prog') |
| 127 | optparser.add_option( |
José Fonseca | 8b9f78b | 2013-04-25 15:22:53 +0100 | [diff] [blame] | 128 | '--apitrace', metavar='PROGRAM', |
| 129 | type='string', dest='apitrace', default='apitrace.exe', |
| 130 | help='retrace command [default: %default]') |
| 131 | optparser.add_option( |
| 132 | '-a', '--api', metavar='API', |
| 133 | type='string', dest='api', default='d3d9', |
| 134 | help='api [default: %default]') |
| 135 | optparser.add_option( |
José Fonseca | 2cce192 | 2012-12-11 19:51:26 +0000 | [diff] [blame] | 136 | '-r', '--retrace', metavar='PROGRAM', |
José Fonseca | 8b9f78b | 2013-04-25 15:22:53 +0100 | [diff] [blame] | 137 | type='string', dest='retrace', default='d3dretrace.exe', |
José Fonseca | 2cce192 | 2012-12-11 19:51:26 +0000 | [diff] [blame] | 138 | help='retrace command [default: %default]') |
| 139 | optparser.add_option( |
| 140 | '-v', '--verbose', |
| 141 | action='store_true', dest='verbose', default=False, |
| 142 | help='verbose output') |
| 143 | optparser.add_option( |
| 144 | '-o', '--output', metavar='FILE', |
| 145 | type="string", dest="output", |
| 146 | help="output file [default: stdout]") |
José Fonseca | 8b9f78b | 2013-04-25 15:22:53 +0100 | [diff] [blame] | 147 | optparser.add_option( |
| 148 | '--verify', |
| 149 | action='store_true', dest='verify', default=False, |
| 150 | help='verify output by replaying it') |
José Fonseca | 2cce192 | 2012-12-11 19:51:26 +0000 | [diff] [blame] | 151 | |
| 152 | (options, args) = optparser.parse_args(sys.argv[1:]) |
| 153 | if not args: |
| 154 | optparser.error("incorrect number of arguments") |
| 155 | |
José Fonseca | 8b9f78b | 2013-04-25 15:22:53 +0100 | [diff] [blame] | 156 | for inFile in args: |
| 157 | name, inExt = os.path.splitext(os.path.basename(inFile)) |
| 158 | inExt = inExt |
| 159 | if inExt.lower() == '.trace': |
| 160 | convert = convertToPix |
| 161 | outExt = '.PIXRun' |
| 162 | elif inExt.lower() == '.pixrun': |
| 163 | convert = convertFromPix |
| 164 | outExt = '.trace' |
José Fonseca | 2cce192 | 2012-12-11 19:51:26 +0000 | [diff] [blame] | 165 | else: |
José Fonseca | 8b9f78b | 2013-04-25 15:22:53 +0100 | [diff] [blame] | 166 | optparser.error("unexpected file extensions `%s`" % inExt) |
| 167 | if options.output: |
| 168 | outFile = options.output |
| 169 | else: |
| 170 | outFile = name + outExt |
| 171 | convert(inFile, outFile) |
José Fonseca | 2cce192 | 2012-12-11 19:51:26 +0000 | [diff] [blame] | 172 | |
| 173 | |
| 174 | if __name__ == '__main__': |
| 175 | main() |