Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 1 | /************************************************************************** |
| 2 | * |
| 3 | * Copyright 2010 VMware, Inc. |
| 4 | * Copyright 2011 Intel corporation |
| 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 | |
Carl Worth | 33b9263 | 2012-08-14 14:11:19 -0700 | [diff] [blame] | 27 | #include <sstream> |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 28 | #include <string.h> |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 29 | #include <limits.h> // for CHAR_MAX |
| 30 | #include <getopt.h> |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 31 | |
Carl Worth | cc6e51c | 2012-08-13 14:35:43 -0700 | [diff] [blame] | 32 | #include <GL/gl.h> |
| 33 | #include <GL/glext.h> |
| 34 | |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 35 | #include <set> |
| 36 | |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 37 | #include "cli.hpp" |
| 38 | |
| 39 | #include "os_string.hpp" |
| 40 | |
José Fonseca | d3c0013 | 2012-01-27 22:43:53 +0000 | [diff] [blame] | 41 | #include "trace_callset.hpp" |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 42 | #include "trace_parser.hpp" |
José Fonseca | 630471a | 2012-01-27 22:06:51 +0000 | [diff] [blame] | 43 | #include "trace_writer.hpp" |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 44 | |
Carl Worth | 33b9263 | 2012-08-14 14:11:19 -0700 | [diff] [blame] | 45 | #define STRNCMP_LITERAL(var, literal) strncmp((var), (literal), sizeof (literal) -1) |
| 46 | |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 47 | static const char *synopsis = "Create a new trace by trimming an existing trace."; |
| 48 | |
| 49 | static void |
| 50 | usage(void) |
| 51 | { |
| 52 | std::cout |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 53 | << "usage: apitrace trim [OPTIONS] TRACE_FILE...\n" |
José Fonseca | d3c0013 | 2012-01-27 22:43:53 +0000 | [diff] [blame] | 54 | << synopsis << "\n" |
| 55 | "\n" |
Carl Worth | 8646443 | 2012-08-11 11:46:54 -0700 | [diff] [blame] | 56 | " -h, --help Show detailed help for trim options and exit\n" |
| 57 | " --calls=CALLSET Include specified calls in the trimmed output.\n" |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame^] | 58 | " --frames=FRAMESET Include specified frames in the trimmed output.\n" |
Carl Worth | 8646443 | 2012-08-11 11:46:54 -0700 | [diff] [blame] | 59 | " --deps Include additional calls to satisfy dependencies\n" |
| 60 | " --no-deps Do not include calls from dependency analysis\n" |
| 61 | " --prune Omit uninteresting calls from the trace output\n" |
| 62 | " --no-prune Do not prune uninteresting calls from the trace.\n" |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame^] | 63 | " -x, --exact Trim exactly to calls specified in --calls/--frames\n" |
Carl Worth | 8646443 | 2012-08-11 11:46:54 -0700 | [diff] [blame] | 64 | " Equivalent to both --no-deps and --no-prune\n" |
| 65 | " --thread=THREAD_ID Only retain calls from specified thread\n" |
| 66 | " -o, --output=TRACE_FILE Output trace file\n" |
| 67 | ; |
| 68 | } |
| 69 | |
| 70 | static void |
| 71 | help() |
| 72 | { |
| 73 | std::cout |
| 74 | << "usage: apitrace trim [OPTIONS] TRACE_FILE...\n" |
| 75 | << synopsis << "\n" |
| 76 | "\n" |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 77 | " -h, --help Show this help message and exit\n" |
Carl Worth | 16b18db | 2012-08-11 11:33:12 -0700 | [diff] [blame] | 78 | "\n" |
| 79 | " --calls=CALLSET Include specified calls in the trimmed output.\n" |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame^] | 80 | " --frames=FRAMESET Include specified frames in the trimmed output.\n" |
Carl Worth | 16b18db | 2012-08-11 11:33:12 -0700 | [diff] [blame] | 81 | " Note that due to dependency analysis and pruning\n" |
| 82 | " of uninteresting calls the resulting trace may\n" |
| 83 | " include more and less calls than specified.\n" |
| 84 | " See --no-deps, --no-prune, and --exact to change\n" |
| 85 | " this behavior.\n" |
| 86 | "\n" |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 87 | " --deps Perform dependency analysis and include dependent\n" |
Carl Worth | 16b18db | 2012-08-11 11:33:12 -0700 | [diff] [blame] | 88 | " calls as needed, (even if those calls were not\n" |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame^] | 89 | " explicitly requested with --calls or --frames).\n" |
| 90 | " This is the default behavior. See --no-deps and\n" |
| 91 | " --exact to change the behavior.\n" |
Carl Worth | 16b18db | 2012-08-11 11:33:12 -0700 | [diff] [blame] | 92 | "\n" |
| 93 | " --no-deps Do not perform dependency analysis. In this mode\n" |
| 94 | " the trimmed trace will never include calls from\n" |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame^] | 95 | " outside what is specified in --calls or --frames.\n" |
Carl Worth | 16b18db | 2012-08-11 11:33:12 -0700 | [diff] [blame] | 96 | "\n" |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame^] | 97 | " --prune Omit calls with no side effects, even if the call\n" |
| 98 | " is within the range specified by --calls/--frames.\n" |
| 99 | " This is the default behavior. See --no-prune.\n" |
Carl Worth | 16b18db | 2012-08-11 11:33:12 -0700 | [diff] [blame] | 100 | "\n" |
| 101 | " --no-prune Do not prune uninteresting calls from the trace.\n" |
| 102 | " In this mode the trimmed trace will never omit\n" |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame^] | 103 | " any calls within the user-specified range.\n" |
Carl Worth | 16b18db | 2012-08-11 11:33:12 -0700 | [diff] [blame] | 104 | "\n" |
| 105 | " -x, --exact Trim the trace to exactly the calls specified in\n" |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame^] | 106 | " --calls and --frames. This option is equivalent\n" |
| 107 | " to passing both --no-deps and --no-prune.\n" |
Carl Worth | 16b18db | 2012-08-11 11:33:12 -0700 | [diff] [blame] | 108 | "\n" |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 109 | " --thread=THREAD_ID Only retain calls from specified thread\n" |
Carl Worth | 16b18db | 2012-08-11 11:33:12 -0700 | [diff] [blame] | 110 | "\n" |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 111 | " -o, --output=TRACE_FILE Output trace file\n" |
José Fonseca | d3c0013 | 2012-01-27 22:43:53 +0000 | [diff] [blame] | 112 | "\n" |
| 113 | ; |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 114 | } |
| 115 | |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 116 | enum { |
Imre Deak | 6f07a84 | 2012-05-08 15:20:43 +0300 | [diff] [blame] | 117 | CALLS_OPT = CHAR_MAX + 1, |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame^] | 118 | FRAMES_OPT, |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 119 | DEPS_OPT, |
| 120 | NO_DEPS_OPT, |
Carl Worth | 16b18db | 2012-08-11 11:33:12 -0700 | [diff] [blame] | 121 | PRUNE_OPT, |
| 122 | NO_PRUNE_OPT, |
Imre Deak | 6f07a84 | 2012-05-08 15:20:43 +0300 | [diff] [blame] | 123 | THREAD_OPT, |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | const static char * |
Carl Worth | 16b18db | 2012-08-11 11:33:12 -0700 | [diff] [blame] | 127 | shortOptions = "ho:x"; |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 128 | |
| 129 | const static struct option |
| 130 | longOptions[] = { |
| 131 | {"help", no_argument, 0, 'h'}, |
| 132 | {"calls", required_argument, 0, CALLS_OPT}, |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame^] | 133 | {"frames", required_argument, 0, FRAMES_OPT}, |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 134 | {"deps", no_argument, 0, DEPS_OPT}, |
| 135 | {"no-deps", no_argument, 0, NO_DEPS_OPT}, |
Carl Worth | 16b18db | 2012-08-11 11:33:12 -0700 | [diff] [blame] | 136 | {"prune", no_argument, 0, PRUNE_OPT}, |
| 137 | {"no-prune", no_argument, 0, NO_PRUNE_OPT}, |
| 138 | {"exact", no_argument, 0, 'x'}, |
Imre Deak | 6f07a84 | 2012-05-08 15:20:43 +0300 | [diff] [blame] | 139 | {"thread", required_argument, 0, THREAD_OPT}, |
| 140 | {"output", required_argument, 0, 'o'}, |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 141 | {0, 0, 0, 0} |
| 142 | }; |
| 143 | |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 144 | struct stringCompare { |
| 145 | bool operator() (const char *a, const char *b) const { |
| 146 | return strcmp(a, b) < 0; |
| 147 | } |
| 148 | }; |
| 149 | |
| 150 | class TraceAnalyzer { |
Carl Worth | 33b9263 | 2012-08-14 14:11:19 -0700 | [diff] [blame] | 151 | /* Maps for tracking resource dependencies between calls. */ |
| 152 | std::map<std::string, std::set<unsigned> > resources; |
| 153 | std::map<std::string, std::set<std::string> > dependencies; |
| 154 | |
| 155 | /* Maps for tracking OpenGL state. */ |
| 156 | std::map<GLenum, unsigned> texture_map; |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 157 | |
| 158 | /* The final set of calls required. This consists of calls added |
| 159 | * explicitly with the require() method as well as all calls |
| 160 | * implicitly required by those through resource dependencies. */ |
| 161 | std::set<unsigned> required; |
| 162 | |
Carl Worth | cc6e51c | 2012-08-13 14:35:43 -0700 | [diff] [blame] | 163 | bool transformFeedbackActive; |
| 164 | bool framebufferObjectActive; |
| 165 | bool insideBeginEnd; |
Carl Worth | 5ff3857 | 2012-09-04 11:49:13 -0700 | [diff] [blame] | 166 | GLuint activeProgram; |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 167 | |
Carl Worth | cc6e51c | 2012-08-13 14:35:43 -0700 | [diff] [blame] | 168 | /* Rendering often has no side effects, but it can in some cases, |
| 169 | * (such as when transform feedback is active, or when rendering |
| 170 | * targets a framebuffer object). */ |
| 171 | bool renderingHasSideEffect() { |
| 172 | return transformFeedbackActive || framebufferObjectActive; |
| 173 | } |
| 174 | |
| 175 | /* Provide: Record that the given call affects the given resource |
| 176 | * as a side effect. */ |
Carl Worth | 33b9263 | 2012-08-14 14:11:19 -0700 | [diff] [blame] | 177 | void provide(std::string resource, trace::CallNo call_no) { |
Carl Worth | cc6e51c | 2012-08-13 14:35:43 -0700 | [diff] [blame] | 178 | resources[resource].insert(call_no); |
| 179 | } |
| 180 | |
Carl Worth | 619a0be | 2012-08-14 17:13:30 -0700 | [diff] [blame] | 181 | /* Like provide, but with a simply-formatted string, (appending an |
| 182 | * integer to the given string). */ |
| 183 | void providef(std::string resource, int resource_no, trace::CallNo call_no) { |
| 184 | std::stringstream ss; |
| 185 | ss << resource << resource_no; |
| 186 | provide(ss.str(), call_no); |
| 187 | } |
| 188 | |
Carl Worth | 33b9263 | 2012-08-14 14:11:19 -0700 | [diff] [blame] | 189 | /* Link: Establish a dependency between resource 'resource' and |
| 190 | * resource 'dependency'. This dependency is captured by name so |
| 191 | * that if the list of calls that provide 'dependency' grows |
| 192 | * before 'resource' is consumed, those calls will still be |
| 193 | * captured. */ |
| 194 | void link(std::string resource, std::string dependency) { |
| 195 | dependencies[resource].insert(dependency); |
| 196 | } |
| 197 | |
Carl Worth | 619a0be | 2012-08-14 17:13:30 -0700 | [diff] [blame] | 198 | /* Like link, but with a simply-formatted string, (appending an |
| 199 | * integer to the given string). */ |
| 200 | void linkf(std::string resource, std::string dependency, int dep_no) { |
| 201 | |
| 202 | std::stringstream ss; |
| 203 | ss << dependency << dep_no; |
| 204 | link(resource, ss.str()); |
| 205 | } |
| 206 | |
Carl Worth | 33b9263 | 2012-08-14 14:11:19 -0700 | [diff] [blame] | 207 | /* Unlink: Remove dependency from 'resource' on 'dependency'. */ |
| 208 | void unlink(std::string resource, std::string dependency) { |
| 209 | dependencies[resource].erase(dependency); |
| 210 | if (dependencies[resource].size() == 0) { |
| 211 | dependencies.erase(resource); |
| 212 | } |
| 213 | } |
| 214 | |
Carl Worth | 619a0be | 2012-08-14 17:13:30 -0700 | [diff] [blame] | 215 | /* Like unlink, but with a simply-formated string, (appending an |
| 216 | * integer to the given string). */ |
| 217 | void unlinkf(std::string resource, std::string dependency, int dep_no) { |
| 218 | |
| 219 | std::stringstream ss; |
| 220 | ss << dependency << dep_no; |
| 221 | unlink(resource, ss.str()); |
| 222 | } |
| 223 | |
Carl Worth | 33b9263 | 2012-08-14 14:11:19 -0700 | [diff] [blame] | 224 | /* Unlink all: Remove dependencies from 'resource' to all other |
| 225 | * resources. */ |
| 226 | void unlinkAll(std::string resource) { |
| 227 | dependencies.erase(resource); |
| 228 | } |
| 229 | |
| 230 | /* Resolve: Recursively compute all calls providing 'resource', |
| 231 | * (including linked dependencies of 'resource' on other |
| 232 | * resources). */ |
| 233 | std::set<unsigned> resolve(std::string resource) { |
| 234 | std::set<std::string> *deps; |
| 235 | std::set<std::string>::iterator dep; |
Carl Worth | cc6e51c | 2012-08-13 14:35:43 -0700 | [diff] [blame] | 236 | |
| 237 | std::set<unsigned> *calls; |
| 238 | std::set<unsigned>::iterator call; |
| 239 | |
Carl Worth | 33b9263 | 2012-08-14 14:11:19 -0700 | [diff] [blame] | 240 | std::set<unsigned> result, deps_set; |
| 241 | |
| 242 | /* Recursively chase dependencies. */ |
| 243 | if (dependencies.count(resource)) { |
| 244 | deps = &dependencies[resource]; |
| 245 | for (dep = deps->begin(); dep != deps->end(); dep++) { |
| 246 | deps_set = resolve(*dep); |
| 247 | for (call = deps_set.begin(); call != deps_set.end(); call++) { |
| 248 | result.insert(*call); |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | /* Also look for calls that directly provide 'resource' */ |
Carl Worth | cc6e51c | 2012-08-13 14:35:43 -0700 | [diff] [blame] | 254 | if (resources.count(resource)) { |
| 255 | calls = &resources[resource]; |
| 256 | for (call = calls->begin(); call != calls->end(); call++) { |
Carl Worth | 33b9263 | 2012-08-14 14:11:19 -0700 | [diff] [blame] | 257 | result.insert(*call); |
Carl Worth | cc6e51c | 2012-08-13 14:35:43 -0700 | [diff] [blame] | 258 | } |
Carl Worth | 33b9263 | 2012-08-14 14:11:19 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | return result; |
| 262 | } |
| 263 | |
| 264 | /* Consume: Resolve all calls that provide the given resource, and |
| 265 | * add them to the required list. Then clear the call list for |
| 266 | * 'resource' along with any dependencies. */ |
| 267 | void consume(std::string resource) { |
| 268 | |
| 269 | std::set<unsigned> calls; |
| 270 | std::set<unsigned>::iterator call; |
| 271 | |
| 272 | calls = resolve(resource); |
| 273 | |
| 274 | dependencies.erase(resource); |
| 275 | resources.erase(resource); |
| 276 | |
| 277 | for (call = calls.begin(); call != calls.end(); call++) { |
| 278 | required.insert(*call); |
Carl Worth | cc6e51c | 2012-08-13 14:35:43 -0700 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
| 282 | void stateTrackPreCall(trace::Call *call) { |
| 283 | |
Carl Worth | 33b9263 | 2012-08-14 14:11:19 -0700 | [diff] [blame] | 284 | const char *name = call->name(); |
| 285 | |
| 286 | if (strcmp(name, "glBegin") == 0) { |
Carl Worth | cc6e51c | 2012-08-13 14:35:43 -0700 | [diff] [blame] | 287 | insideBeginEnd = true; |
| 288 | return; |
| 289 | } |
| 290 | |
Carl Worth | 33b9263 | 2012-08-14 14:11:19 -0700 | [diff] [blame] | 291 | if (strcmp(name, "glBeginTransformFeedback") == 0) { |
Carl Worth | cc6e51c | 2012-08-13 14:35:43 -0700 | [diff] [blame] | 292 | transformFeedbackActive = true; |
| 293 | return; |
| 294 | } |
| 295 | |
Carl Worth | 33b9263 | 2012-08-14 14:11:19 -0700 | [diff] [blame] | 296 | if (strcmp(name, "glBindTexture") == 0) { |
| 297 | GLenum target; |
| 298 | GLuint texture; |
| 299 | |
| 300 | target = static_cast<GLenum>(call->arg(0).toSInt()); |
| 301 | texture = call->arg(1).toUInt(); |
| 302 | |
| 303 | if (texture == 0) { |
| 304 | texture_map.erase(target); |
| 305 | } else { |
| 306 | texture_map[target] = texture; |
| 307 | } |
| 308 | |
| 309 | return; |
| 310 | } |
| 311 | |
Carl Worth | 5ff3857 | 2012-09-04 11:49:13 -0700 | [diff] [blame] | 312 | if (strcmp(name, "glUseProgram") == 0) { |
| 313 | activeProgram = call->arg(0).toUInt(); |
| 314 | } |
| 315 | |
Carl Worth | 33b9263 | 2012-08-14 14:11:19 -0700 | [diff] [blame] | 316 | if (strcmp(name, "glBindFramebuffer") == 0) { |
Carl Worth | cc6e51c | 2012-08-13 14:35:43 -0700 | [diff] [blame] | 317 | GLenum target; |
| 318 | GLuint framebuffer; |
| 319 | |
| 320 | target = static_cast<GLenum>(call->arg(0).toSInt()); |
| 321 | framebuffer = call->arg(1).toUInt(); |
| 322 | |
| 323 | if (target == GL_FRAMEBUFFER || target == GL_DRAW_FRAMEBUFFER) { |
| 324 | if (framebuffer == 0) { |
| 325 | framebufferObjectActive = false; |
| 326 | } else { |
| 327 | framebufferObjectActive = true; |
| 328 | } |
| 329 | } |
| 330 | return; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | void stateTrackPostCall(trace::Call *call) { |
| 335 | |
Carl Worth | 33b9263 | 2012-08-14 14:11:19 -0700 | [diff] [blame] | 336 | const char *name = call->name(); |
| 337 | |
| 338 | if (strcmp(name, "glEnd") == 0) { |
Carl Worth | cc6e51c | 2012-08-13 14:35:43 -0700 | [diff] [blame] | 339 | insideBeginEnd = false; |
| 340 | return; |
| 341 | } |
| 342 | |
Carl Worth | 33b9263 | 2012-08-14 14:11:19 -0700 | [diff] [blame] | 343 | if (strcmp(name, "glEndTransformFeedback") == 0) { |
Carl Worth | cc6e51c | 2012-08-13 14:35:43 -0700 | [diff] [blame] | 344 | transformFeedbackActive = false; |
| 345 | return; |
| 346 | } |
| 347 | |
Carl Worth | 33b9263 | 2012-08-14 14:11:19 -0700 | [diff] [blame] | 348 | /* If this swapbuffers was included in the trace then it will |
| 349 | * have already consumed all framebuffer dependencies. If not, |
| 350 | * then clear them now so that they don't carry over into the |
| 351 | * next frame. */ |
Carl Worth | cc6e51c | 2012-08-13 14:35:43 -0700 | [diff] [blame] | 352 | if (call->flags & trace::CALL_FLAG_SWAP_RENDERTARGET && |
| 353 | call->flags & trace::CALL_FLAG_END_FRAME) { |
Carl Worth | 33b9263 | 2012-08-14 14:11:19 -0700 | [diff] [blame] | 354 | dependencies.erase("framebuffer"); |
Carl Worth | cc6e51c | 2012-08-13 14:35:43 -0700 | [diff] [blame] | 355 | resources.erase("framebuffer"); |
| 356 | return; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | void recordSideEffects(trace::Call *call) { |
Carl Worth | 33b9263 | 2012-08-14 14:11:19 -0700 | [diff] [blame] | 361 | |
| 362 | const char *name = call->name(); |
| 363 | |
Carl Worth | cc6e51c | 2012-08-13 14:35:43 -0700 | [diff] [blame] | 364 | /* If call is flagged as no side effects, then we are done here. */ |
Carl Worth | 5b827e1 | 2012-08-12 20:41:50 -0700 | [diff] [blame] | 365 | if (call->flags & trace::CALL_FLAG_NO_SIDE_EFFECTS) { |
| 366 | return; |
| 367 | } |
| 368 | |
Carl Worth | cc6e51c | 2012-08-13 14:35:43 -0700 | [diff] [blame] | 369 | /* Similarly, swap-buffers calls don't have interesting side effects. */ |
Carl Worth | 5b827e1 | 2012-08-12 20:41:50 -0700 | [diff] [blame] | 370 | if (call->flags & trace::CALL_FLAG_SWAP_RENDERTARGET && |
| 371 | call->flags & trace::CALL_FLAG_END_FRAME) { |
| 372 | return; |
| 373 | } |
| 374 | |
Carl Worth | 33b9263 | 2012-08-14 14:11:19 -0700 | [diff] [blame] | 375 | if (strcmp(name, "glGenTextures") == 0) { |
| 376 | const trace::Array *textures = dynamic_cast<const trace::Array *>(&call->arg(1)); |
| 377 | size_t i; |
| 378 | GLuint texture; |
| 379 | |
| 380 | if (textures) { |
| 381 | for (i = 0; i < textures->size(); i++) { |
Carl Worth | 33b9263 | 2012-08-14 14:11:19 -0700 | [diff] [blame] | 382 | texture = textures->values[i]->toUInt(); |
Carl Worth | 619a0be | 2012-08-14 17:13:30 -0700 | [diff] [blame] | 383 | providef("texture-", texture, call->no); |
Carl Worth | 33b9263 | 2012-08-14 14:11:19 -0700 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | return; |
| 387 | } |
| 388 | |
| 389 | if (strcmp(name, "glBindTexture") == 0) { |
| 390 | GLenum target; |
| 391 | GLuint texture; |
| 392 | |
| 393 | std::stringstream ss_target, ss_texture; |
| 394 | |
| 395 | target = static_cast<GLenum>(call->arg(0).toSInt()); |
| 396 | texture = call->arg(1).toUInt(); |
| 397 | |
| 398 | ss_target << "texture-target-" << target; |
| 399 | ss_texture << "texture-" << texture; |
| 400 | |
| 401 | resources.erase(ss_target.str()); |
| 402 | provide(ss_target.str(), call->no); |
| 403 | |
| 404 | unlinkAll(ss_target.str()); |
| 405 | link(ss_target.str(), ss_texture.str()); |
| 406 | |
| 407 | return; |
| 408 | } |
| 409 | |
| 410 | /* FIXME: Need to handle glMultTexImage and friends. */ |
| 411 | if (STRNCMP_LITERAL(name, "glTexImage") == 0 || |
| 412 | STRNCMP_LITERAL(name, "glTexSubImage") == 0 || |
| 413 | STRNCMP_LITERAL(name, "glCopyTexImage") == 0 || |
| 414 | STRNCMP_LITERAL(name, "glCopyTexSubImage") == 0 || |
| 415 | STRNCMP_LITERAL(name, "glCompressedTexImage") == 0 || |
| 416 | STRNCMP_LITERAL(name, "glCompressedTexSubImage") == 0 || |
| 417 | strcmp(name, "glInvalidateTexImage") == 0 || |
| 418 | strcmp(name, "glInvalidateTexSubImage") == 0) { |
| 419 | |
| 420 | std::set<unsigned> *calls; |
| 421 | std::set<unsigned>::iterator c; |
| 422 | std::stringstream ss_target, ss_texture; |
| 423 | |
| 424 | GLenum target = static_cast<GLenum>(call->arg(0).toSInt()); |
| 425 | |
| 426 | ss_target << "texture-target-" << target; |
| 427 | ss_texture << "texture-" << texture_map[target]; |
| 428 | |
| 429 | /* The texture resource depends on this call and any calls |
| 430 | * providing the given texture target. */ |
| 431 | provide(ss_texture.str(), call->no); |
| 432 | |
| 433 | if (resources.count(ss_target.str())) { |
| 434 | calls = &resources[ss_target.str()]; |
| 435 | for (c = calls->begin(); c != calls->end(); c++) { |
| 436 | provide(ss_texture.str(), *c); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | return; |
| 441 | } |
| 442 | |
| 443 | if (strcmp(name, "glEnable") == 0) { |
| 444 | GLenum cap; |
| 445 | |
| 446 | cap = static_cast<GLenum>(call->arg(0).toSInt()); |
| 447 | |
| 448 | if (cap == GL_TEXTURE_1D || |
| 449 | cap == GL_TEXTURE_2D || |
| 450 | cap == GL_TEXTURE_3D || |
| 451 | cap == GL_TEXTURE_CUBE_MAP) |
| 452 | { |
Carl Worth | 619a0be | 2012-08-14 17:13:30 -0700 | [diff] [blame] | 453 | linkf("render-state", "texture-target-", cap); |
Carl Worth | 33b9263 | 2012-08-14 14:11:19 -0700 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | provide("state", call->no); |
Carl Worth | 33b9263 | 2012-08-14 14:11:19 -0700 | [diff] [blame] | 457 | return; |
| 458 | } |
| 459 | |
| 460 | if (strcmp(name, "glDisable") == 0) { |
| 461 | GLenum cap; |
| 462 | |
| 463 | cap = static_cast<GLenum>(call->arg(0).toSInt()); |
| 464 | |
| 465 | if (cap == GL_TEXTURE_1D || |
| 466 | cap == GL_TEXTURE_2D || |
| 467 | cap == GL_TEXTURE_3D || |
| 468 | cap == GL_TEXTURE_CUBE_MAP) |
| 469 | { |
Carl Worth | 619a0be | 2012-08-14 17:13:30 -0700 | [diff] [blame] | 470 | unlinkf("render-state", "texture-target-", cap); |
Carl Worth | 33b9263 | 2012-08-14 14:11:19 -0700 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | provide("state", call->no); |
Carl Worth | 33b9263 | 2012-08-14 14:11:19 -0700 | [diff] [blame] | 474 | return; |
| 475 | } |
| 476 | |
Carl Worth | bbbbe70 | 2012-08-14 15:20:56 -0700 | [diff] [blame] | 477 | if (strcmp(name, "glCreateShader") == 0 || |
| 478 | strcmp(name, "glCreateShaderObjectARB") == 0) { |
| 479 | |
Carl Worth | 619a0be | 2012-08-14 17:13:30 -0700 | [diff] [blame] | 480 | GLuint shader = call->ret->toUInt(); |
| 481 | providef("shader-", shader, call->no); |
Carl Worth | bbbbe70 | 2012-08-14 15:20:56 -0700 | [diff] [blame] | 482 | return; |
| 483 | } |
| 484 | |
| 485 | if (strcmp(name, "glShaderSource") == 0 || |
| 486 | strcmp(name, "glShaderSourceARB") == 0 || |
| 487 | strcmp(name, "glCompileShader") == 0 || |
| 488 | strcmp(name, "glCompileShaderARB") == 0 || |
| 489 | strcmp(name, "glGetShaderiv") == 0 || |
| 490 | strcmp(name, "glGetShaderInfoLog") == 0) { |
| 491 | |
Carl Worth | 619a0be | 2012-08-14 17:13:30 -0700 | [diff] [blame] | 492 | GLuint shader = call->arg(0).toUInt(); |
| 493 | providef("shader-", shader, call->no); |
Carl Worth | bbbbe70 | 2012-08-14 15:20:56 -0700 | [diff] [blame] | 494 | return; |
| 495 | } |
| 496 | |
| 497 | if (strcmp(name, "glCreateProgram") == 0 || |
| 498 | strcmp(name, "glCreateProgramObjectARB") == 0) { |
| 499 | |
Carl Worth | 619a0be | 2012-08-14 17:13:30 -0700 | [diff] [blame] | 500 | GLuint program = call->ret->toUInt(); |
| 501 | providef("program-", program, call->no); |
Carl Worth | bbbbe70 | 2012-08-14 15:20:56 -0700 | [diff] [blame] | 502 | return; |
| 503 | } |
| 504 | |
| 505 | if (strcmp(name, "glAttachShader") == 0 || |
| 506 | strcmp(name, "glAttachObjectARB") == 0) { |
| 507 | |
| 508 | GLuint program, shader; |
| 509 | std::stringstream ss_program, ss_shader; |
| 510 | |
| 511 | program = call->arg(0).toUInt(); |
| 512 | shader = call->arg(1).toUInt(); |
| 513 | |
| 514 | ss_program << "program-" << program; |
| 515 | ss_shader << "shader-" << shader; |
| 516 | |
| 517 | link(ss_program.str(), ss_shader.str()); |
| 518 | provide(ss_program.str(), call->no); |
| 519 | |
| 520 | return; |
| 521 | } |
| 522 | |
| 523 | if (strcmp(name, "glDetachShader") == 0 || |
| 524 | strcmp(name, "glDetachObjectARB") == 0) { |
| 525 | |
| 526 | GLuint program, shader; |
| 527 | std::stringstream ss_program, ss_shader; |
| 528 | |
| 529 | program = call->arg(0).toUInt(); |
| 530 | shader = call->arg(1).toUInt(); |
| 531 | |
| 532 | ss_program << "program-" << program; |
| 533 | ss_shader << "shader-" << shader; |
| 534 | |
| 535 | unlink(ss_program.str(), ss_shader.str()); |
| 536 | |
| 537 | return; |
| 538 | } |
| 539 | |
| 540 | if (strcmp(name, "glUseProgram") == 0 || |
| 541 | strcmp(name, "glUseProgramObjectARB") == 0) { |
| 542 | |
| 543 | GLuint program; |
| 544 | |
| 545 | program = call->arg(0).toUInt(); |
| 546 | |
| 547 | unlinkAll("render-program-state"); |
| 548 | |
| 549 | if (program == 0) { |
| 550 | unlink("render-state", "render-program-state"); |
| 551 | provide("state", call->no); |
| 552 | } else { |
| 553 | std::stringstream ss; |
| 554 | |
| 555 | ss << "program-" << program; |
| 556 | |
| 557 | link("render-state", "render-program-state"); |
| 558 | link("render-program-state", ss.str()); |
| 559 | |
| 560 | provide(ss.str(), call->no); |
| 561 | } |
| 562 | |
| 563 | return; |
| 564 | } |
| 565 | |
| 566 | if (strcmp(name, "glGetUniformLocation") == 0 || |
| 567 | strcmp(name, "glGetUniformLocationARB") == 0 || |
| 568 | strcmp(name, "glGetFragDataLocation") == 0 || |
| 569 | strcmp(name, "glGetFragDataLocationEXT") == 0 || |
| 570 | strcmp(name, "glGetSubroutineUniformLocation") == 0 || |
| 571 | strcmp(name, "glGetProgramResourceLocation") == 0 || |
| 572 | strcmp(name, "glGetProgramResourceLocationIndex") == 0 || |
| 573 | strcmp(name, "glGetVaryingLocationNV") == 0) { |
| 574 | |
Carl Worth | 619a0be | 2012-08-14 17:13:30 -0700 | [diff] [blame] | 575 | GLuint program = call->arg(0).toUInt(); |
Carl Worth | bbbbe70 | 2012-08-14 15:20:56 -0700 | [diff] [blame] | 576 | |
Carl Worth | 619a0be | 2012-08-14 17:13:30 -0700 | [diff] [blame] | 577 | providef("program-", program, call->no); |
Carl Worth | bbbbe70 | 2012-08-14 15:20:56 -0700 | [diff] [blame] | 578 | |
| 579 | return; |
| 580 | } |
| 581 | |
| 582 | /* For any call that accepts 'location' as its first argument, |
| 583 | * perform a lookup in our location->program map and add a |
| 584 | * dependence on the program we find there. */ |
| 585 | if (call->sig->num_args > 0 && |
| 586 | strcmp(call->sig->arg_names[0], "location") == 0) { |
| 587 | |
Carl Worth | 619a0be | 2012-08-14 17:13:30 -0700 | [diff] [blame] | 588 | providef("program-", activeProgram, call->no); |
Carl Worth | bbbbe70 | 2012-08-14 15:20:56 -0700 | [diff] [blame] | 589 | return; |
| 590 | } |
| 591 | |
| 592 | /* FIXME: We cut a huge swath by assuming that any unhandled |
| 593 | * call that has a first argument named "program" should not |
| 594 | * be included in the trimmed output unless the program of |
| 595 | * that number is also included. |
| 596 | * |
| 597 | * This heuristic is correct for many cases, but we should |
| 598 | * actually carefully verify if this includes some calls |
| 599 | * inappropriately, or if it misses some. |
| 600 | */ |
| 601 | if (strcmp(name, "glLinkProgram") == 0 || |
| 602 | strcmp(name, "glLinkProgramARB") == 0 || |
| 603 | (call->sig->num_args > 0 && |
| 604 | (strcmp(call->sig->arg_names[0], "program") == 0 || |
| 605 | strcmp(call->sig->arg_names[0], "programObj") == 0))) { |
| 606 | |
Carl Worth | 619a0be | 2012-08-14 17:13:30 -0700 | [diff] [blame] | 607 | GLuint program = call->arg(0).toUInt(); |
| 608 | providef("program-", program, call->no); |
Carl Worth | bbbbe70 | 2012-08-14 15:20:56 -0700 | [diff] [blame] | 609 | return; |
| 610 | } |
| 611 | |
Carl Worth | cc6e51c | 2012-08-13 14:35:43 -0700 | [diff] [blame] | 612 | /* Handle all rendering operations, (even though only glEnd is |
| 613 | * flagged as a rendering operation we treat everything from |
| 614 | * glBegin through glEnd as a rendering operation). */ |
| 615 | if (call->flags & trace::CALL_FLAG_RENDER || |
| 616 | insideBeginEnd) { |
| 617 | |
Carl Worth | 33b9263 | 2012-08-14 14:11:19 -0700 | [diff] [blame] | 618 | std::set<unsigned> calls; |
| 619 | std::set<unsigned>::iterator c; |
| 620 | |
Carl Worth | cc6e51c | 2012-08-13 14:35:43 -0700 | [diff] [blame] | 621 | provide("framebuffer", call->no); |
| 622 | |
Carl Worth | 33b9263 | 2012-08-14 14:11:19 -0700 | [diff] [blame] | 623 | calls = resolve("render-state"); |
| 624 | |
| 625 | for (c = calls.begin(); c != calls.end(); c++) { |
| 626 | provide("framebuffer", *c); |
| 627 | } |
| 628 | |
Carl Worth | cc6e51c | 2012-08-13 14:35:43 -0700 | [diff] [blame] | 629 | /* In some cases, rendering has side effects beyond the |
| 630 | * framebuffer update. */ |
| 631 | if (renderingHasSideEffect()) { |
| 632 | provide("state", call->no); |
Carl Worth | 33b9263 | 2012-08-14 14:11:19 -0700 | [diff] [blame] | 633 | for (c = calls.begin(); c != calls.end(); c++) { |
| 634 | provide("state", *c); |
| 635 | } |
Carl Worth | cc6e51c | 2012-08-13 14:35:43 -0700 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | return; |
| 639 | } |
| 640 | |
Carl Worth | 5b827e1 | 2012-08-12 20:41:50 -0700 | [diff] [blame] | 641 | /* By default, assume this call affects the state somehow. */ |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 642 | resources["state"].insert(call->no); |
| 643 | } |
| 644 | |
Carl Worth | cc6e51c | 2012-08-13 14:35:43 -0700 | [diff] [blame] | 645 | void requireDependencies(trace::Call *call) { |
| 646 | |
| 647 | /* Swap-buffers calls depend on framebuffer state. */ |
| 648 | if (call->flags & trace::CALL_FLAG_SWAP_RENDERTARGET && |
| 649 | call->flags & trace::CALL_FLAG_END_FRAME) { |
| 650 | consume("framebuffer"); |
| 651 | } |
| 652 | |
| 653 | /* By default, just assume this call depends on generic state. */ |
| 654 | consume("state"); |
| 655 | } |
| 656 | |
| 657 | |
| 658 | public: |
| 659 | TraceAnalyzer(): transformFeedbackActive(false), |
| 660 | framebufferObjectActive(false), |
| 661 | insideBeginEnd(false) |
| 662 | {} |
| 663 | |
| 664 | ~TraceAnalyzer() {} |
| 665 | |
| 666 | /* Analyze this call by tracking state and recording all the |
| 667 | * resources provided by this call as side effects.. */ |
| 668 | void analyze(trace::Call *call) { |
| 669 | |
| 670 | stateTrackPreCall(call); |
| 671 | |
| 672 | recordSideEffects(call); |
| 673 | |
| 674 | stateTrackPostCall(call); |
| 675 | } |
| 676 | |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 677 | /* Require this call and all of its dependencies to be included in |
| 678 | * the final trace. */ |
| 679 | void require(trace::Call *call) { |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 680 | |
| 681 | /* First, find and insert all calls that this call depends on. */ |
Carl Worth | cc6e51c | 2012-08-13 14:35:43 -0700 | [diff] [blame] | 682 | requireDependencies(call); |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 683 | |
| 684 | /* Then insert this call itself. */ |
| 685 | required.insert(call->no); |
| 686 | } |
| 687 | |
| 688 | /* Return a set of all the required calls, (both those calls added |
| 689 | * explicitly with require() and those implicitly depended |
| 690 | * upon. */ |
| 691 | std::set<unsigned> *get_required(void) { |
| 692 | return &required; |
| 693 | } |
| 694 | }; |
| 695 | |
| 696 | struct trim_options { |
| 697 | /* Calls to be included in trace. */ |
| 698 | trace::CallSet calls; |
| 699 | |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame^] | 700 | /* Frames to be included in trace. */ |
| 701 | trace::CallSet frames; |
| 702 | |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 703 | /* Whether dependency analysis should be performed. */ |
| 704 | bool dependency_analysis; |
| 705 | |
Carl Worth | 16b18db | 2012-08-11 11:33:12 -0700 | [diff] [blame] | 706 | /* Whether uninteresting calls should be pruned.. */ |
| 707 | bool prune_uninteresting; |
| 708 | |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 709 | /* Output filename */ |
| 710 | std::string output; |
| 711 | |
| 712 | /* Emit only calls from this thread (-1 == all threads) */ |
| 713 | int thread; |
| 714 | }; |
| 715 | |
| 716 | static int |
| 717 | trim_trace(const char *filename, struct trim_options *options) |
| 718 | { |
| 719 | trace::ParseBookmark beginning; |
| 720 | trace::Parser p; |
| 721 | TraceAnalyzer analyzer; |
| 722 | std::set<unsigned> *required; |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame^] | 723 | unsigned frame; |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 724 | |
| 725 | if (!p.open(filename)) { |
| 726 | std::cerr << "error: failed to open " << filename << "\n"; |
| 727 | return 1; |
| 728 | } |
| 729 | |
| 730 | /* Mark the beginning so we can return here for pass 2. */ |
| 731 | p.getBookmark(beginning); |
| 732 | |
| 733 | /* In pass 1, analyze which calls are needed. */ |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame^] | 734 | frame = 0; |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 735 | trace::Call *call; |
| 736 | while ((call = p.parse_call())) { |
Carl Worth | 5b827e1 | 2012-08-12 20:41:50 -0700 | [diff] [blame] | 737 | |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame^] | 738 | /* There's no use doing any work past the last call or frame |
| 739 | * requested by the user. */ |
| 740 | if (call->no > options->calls.getLast() || |
| 741 | frame > options->frames.getLast()) { |
| 742 | |
Carl Worth | 4224901 | 2012-08-14 10:26:11 -0700 | [diff] [blame] | 743 | delete call; |
Carl Worth | 5b827e1 | 2012-08-12 20:41:50 -0700 | [diff] [blame] | 744 | break; |
Carl Worth | 4224901 | 2012-08-14 10:26:11 -0700 | [diff] [blame] | 745 | } |
Carl Worth | 5b827e1 | 2012-08-12 20:41:50 -0700 | [diff] [blame] | 746 | |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 747 | /* If requested, ignore all calls not belonging to the specified thread. */ |
Carl Worth | 4224901 | 2012-08-14 10:26:11 -0700 | [diff] [blame] | 748 | if (options->thread != -1 && call->thread_id != options->thread) { |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame^] | 749 | goto NEXT; |
Carl Worth | 4224901 | 2012-08-14 10:26:11 -0700 | [diff] [blame] | 750 | } |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 751 | |
Carl Worth | 16b18db | 2012-08-11 11:33:12 -0700 | [diff] [blame] | 752 | /* Also, prune if uninteresting (unless the user asked for no pruning. */ |
| 753 | if (options->prune_uninteresting && call->flags & trace::CALL_FLAG_UNINTERESTING) { |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame^] | 754 | goto NEXT; |
Carl Worth | 16b18db | 2012-08-11 11:33:12 -0700 | [diff] [blame] | 755 | } |
| 756 | |
Carl Worth | cc6e51c | 2012-08-13 14:35:43 -0700 | [diff] [blame] | 757 | /* If this call is included in the user-specified call set, |
| 758 | * then require it (and all dependencies) in the trimmed |
| 759 | * output. */ |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame^] | 760 | if (options->calls.contains(*call) || |
| 761 | options->frames.contains(frame, call->flags)) { |
| 762 | |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 763 | analyzer.require(call); |
Carl Worth | cc6e51c | 2012-08-13 14:35:43 -0700 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | /* Regardless of whether we include this call or not, we do |
| 767 | * some dependency tracking (unless disabled by the user). We |
| 768 | * do this even for calls we have included in the output so |
| 769 | * that any state updates get performed. */ |
| 770 | if (options->dependency_analysis) { |
| 771 | analyzer.analyze(call); |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 772 | } |
Carl Worth | 4224901 | 2012-08-14 10:26:11 -0700 | [diff] [blame] | 773 | |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame^] | 774 | NEXT: |
| 775 | if (call->flags & trace::CALL_FLAG_END_FRAME) |
| 776 | frame++; |
| 777 | |
Carl Worth | 4224901 | 2012-08-14 10:26:11 -0700 | [diff] [blame] | 778 | delete call; |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | /* Prepare output file and writer for output. */ |
| 782 | if (options->output.empty()) { |
| 783 | os::String base(filename); |
| 784 | base.trimExtension(); |
| 785 | |
| 786 | options->output = std::string(base.str()) + std::string("-trim.trace"); |
| 787 | } |
| 788 | |
| 789 | trace::Writer writer; |
| 790 | if (!writer.open(options->output.c_str())) { |
| 791 | std::cerr << "error: failed to create " << filename << "\n"; |
| 792 | return 1; |
| 793 | } |
| 794 | |
| 795 | /* Reset bookmark for pass 2. */ |
| 796 | p.setBookmark(beginning); |
| 797 | |
| 798 | /* In pass 2, emit the calls that are required. */ |
| 799 | required = analyzer.get_required(); |
| 800 | |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame^] | 801 | frame = 0; |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 802 | while ((call = p.parse_call())) { |
Carl Worth | 5b827e1 | 2012-08-12 20:41:50 -0700 | [diff] [blame] | 803 | |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame^] | 804 | /* There's no use doing any work past the last call or frame |
| 805 | * requested by the user. */ |
| 806 | if (call->no > options->calls.getLast() || |
| 807 | frame > options->frames.getLast()) { |
| 808 | |
Carl Worth | 5b827e1 | 2012-08-12 20:41:50 -0700 | [diff] [blame] | 809 | break; |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame^] | 810 | } |
Carl Worth | 5b827e1 | 2012-08-12 20:41:50 -0700 | [diff] [blame] | 811 | |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 812 | if (required->find(call->no) != required->end()) { |
| 813 | writer.writeCall(call); |
| 814 | } |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame^] | 815 | |
| 816 | if (call->flags & trace::CALL_FLAG_END_FRAME) { |
| 817 | frame++; |
| 818 | } |
| 819 | |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 820 | delete call; |
| 821 | } |
| 822 | |
| 823 | std::cout << "Trimmed trace is available as " << options->output << "\n"; |
| 824 | |
| 825 | return 0; |
| 826 | } |
| 827 | |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 828 | static int |
| 829 | command(int argc, char *argv[]) |
| 830 | { |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 831 | struct trim_options options; |
| 832 | |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame^] | 833 | options.calls = trace::CallSet(trace::FREQUENCY_NONE); |
| 834 | options.frames = trace::CallSet(trace::FREQUENCY_NONE); |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 835 | options.dependency_analysis = true; |
Carl Worth | 16b18db | 2012-08-11 11:33:12 -0700 | [diff] [blame] | 836 | options.prune_uninteresting = true; |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 837 | options.output = ""; |
| 838 | options.thread = -1; |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 839 | |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 840 | int opt; |
| 841 | while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) { |
| 842 | switch (opt) { |
| 843 | case 'h': |
Carl Worth | 8646443 | 2012-08-11 11:46:54 -0700 | [diff] [blame] | 844 | help(); |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 845 | return 0; |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 846 | case CALLS_OPT: |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 847 | options.calls = trace::CallSet(optarg); |
| 848 | break; |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame^] | 849 | case FRAMES_OPT: |
| 850 | options.frames = trace::CallSet(optarg); |
| 851 | break; |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 852 | case DEPS_OPT: |
| 853 | options.dependency_analysis = true; |
| 854 | break; |
| 855 | case NO_DEPS_OPT: |
| 856 | options.dependency_analysis = false; |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 857 | break; |
Carl Worth | 16b18db | 2012-08-11 11:33:12 -0700 | [diff] [blame] | 858 | case PRUNE_OPT: |
| 859 | options.prune_uninteresting = true; |
| 860 | break; |
| 861 | case NO_PRUNE_OPT: |
| 862 | options.prune_uninteresting = false; |
| 863 | break; |
| 864 | case 'x': |
| 865 | options.dependency_analysis = false; |
| 866 | options.prune_uninteresting = false; |
| 867 | break; |
Imre Deak | 6f07a84 | 2012-05-08 15:20:43 +0300 | [diff] [blame] | 868 | case THREAD_OPT: |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 869 | options.thread = atoi(optarg); |
Imre Deak | 6f07a84 | 2012-05-08 15:20:43 +0300 | [diff] [blame] | 870 | break; |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 871 | case 'o': |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 872 | options.output = optarg; |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 873 | break; |
| 874 | default: |
| 875 | std::cerr << "error: unexpected option `" << opt << "`\n"; |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 876 | usage(); |
| 877 | return 1; |
| 878 | } |
| 879 | } |
| 880 | |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame^] | 881 | /* If neither of --calls nor --frames was set, default to the |
| 882 | * entire set of calls. */ |
| 883 | if (options.calls.empty() && options.frames.empty()) { |
| 884 | options.calls = trace::CallSet(trace::FREQUENCY_ALL); |
| 885 | } |
| 886 | |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 887 | if (optind >= argc) { |
| 888 | std::cerr << "error: apitrace trim requires a trace file as an argument.\n"; |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 889 | usage(); |
| 890 | return 1; |
| 891 | } |
| 892 | |
Carl Worth | f630d9d | 2012-09-04 16:48:00 -0700 | [diff] [blame] | 893 | if (argc > optind + 1) { |
| 894 | std::cerr << "error: extraneous arguments:"; |
| 895 | for (int i = optind + 1; i < argc; i++) { |
| 896 | std::cerr << " " << argv[i]; |
| 897 | } |
| 898 | std::cerr << "\n"; |
| 899 | usage(); |
| 900 | return 1; |
| 901 | } |
| 902 | |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 903 | return trim_trace(argv[optind], &options); |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | const Command trim_command = { |
| 907 | "trim", |
| 908 | synopsis, |
Carl Worth | 8646443 | 2012-08-11 11:46:54 -0700 | [diff] [blame] | 909 | help, |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 910 | command |
| 911 | }; |