blob: 22cdcd13367d3c0760bc88db5cc189d2f733a912 [file] [log] [blame]
Carl Worth4c5f6fa2011-11-14 14:50:07 -08001/**************************************************************************
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 Worth33b92632012-08-14 14:11:19 -070027#include <sstream>
Carl Worth4c5f6fa2011-11-14 14:50:07 -080028#include <string.h>
José Fonsecab682b672012-02-15 07:13:31 +000029#include <limits.h> // for CHAR_MAX
30#include <getopt.h>
Carl Worth4c5f6fa2011-11-14 14:50:07 -080031
Carl Worthcc6e51c2012-08-13 14:35:43 -070032#include <GL/gl.h>
33#include <GL/glext.h>
34
Carl Worth163b22c2012-08-10 10:15:30 -070035#include <set>
36
Carl Worth4c5f6fa2011-11-14 14:50:07 -080037#include "cli.hpp"
38
39#include "os_string.hpp"
40
José Fonsecad3c00132012-01-27 22:43:53 +000041#include "trace_callset.hpp"
Carl Worth4c5f6fa2011-11-14 14:50:07 -080042#include "trace_parser.hpp"
José Fonseca630471a2012-01-27 22:06:51 +000043#include "trace_writer.hpp"
Carl Worth4c5f6fa2011-11-14 14:50:07 -080044
Carl Worth33b92632012-08-14 14:11:19 -070045#define STRNCMP_LITERAL(var, literal) strncmp((var), (literal), sizeof (literal) -1)
46
Carl Worth4c5f6fa2011-11-14 14:50:07 -080047static const char *synopsis = "Create a new trace by trimming an existing trace.";
48
49static void
50usage(void)
51{
52 std::cout
José Fonsecab682b672012-02-15 07:13:31 +000053 << "usage: apitrace trim [OPTIONS] TRACE_FILE...\n"
José Fonsecad3c00132012-01-27 22:43:53 +000054 << synopsis << "\n"
55 "\n"
Carl Worth86464432012-08-11 11:46:54 -070056 " -h, --help Show detailed help for trim options and exit\n"
57 " --calls=CALLSET Include specified calls in the trimmed output.\n"
58 " --deps Include additional calls to satisfy dependencies\n"
59 " --no-deps Do not include calls from dependency analysis\n"
60 " --prune Omit uninteresting calls from the trace output\n"
61 " --no-prune Do not prune uninteresting calls from the trace.\n"
62 " -x, --exact Include exactly the calls specified in --calls\n"
63 " Equivalent to both --no-deps and --no-prune\n"
64 " --thread=THREAD_ID Only retain calls from specified thread\n"
65 " -o, --output=TRACE_FILE Output trace file\n"
66 ;
67}
68
69static void
70help()
71{
72 std::cout
73 << "usage: apitrace trim [OPTIONS] TRACE_FILE...\n"
74 << synopsis << "\n"
75 "\n"
Carl Worth163b22c2012-08-10 10:15:30 -070076 " -h, --help Show this help message and exit\n"
Carl Worth16b18db2012-08-11 11:33:12 -070077 "\n"
78 " --calls=CALLSET Include specified calls in the trimmed output.\n"
79 " Note that due to dependency analysis and pruning\n"
80 " of uninteresting calls the resulting trace may\n"
81 " include more and less calls than specified.\n"
82 " See --no-deps, --no-prune, and --exact to change\n"
83 " this behavior.\n"
84 "\n"
Carl Worth163b22c2012-08-10 10:15:30 -070085 " --deps Perform dependency analysis and include dependent\n"
Carl Worth16b18db2012-08-11 11:33:12 -070086 " calls as needed, (even if those calls were not\n"
87 " explicitly requested with --calls). This is the\n"
88 " default behavior. See --no-deps and --exact.\n"
89 "\n"
90 " --no-deps Do not perform dependency analysis. In this mode\n"
91 " the trimmed trace will never include calls from\n"
92 " outside the range specified in --calls.\n"
93 "\n"
94 " --prune Omit calls that have no side effects, even if the\n"
95 " call is within the range specified by --calls.\n"
96 " This is the default behavior. See --no-prune\n"
97 "\n"
98 " --no-prune Do not prune uninteresting calls from the trace.\n"
99 " In this mode the trimmed trace will never omit\n"
100 " any calls within the range specified in --calls.\n"
101 "\n"
102 " -x, --exact Trim the trace to exactly the calls specified in\n"
103 " --calls. This option is equivalent to passing\n"
104 " both --no-deps and --no-prune.\n"
105 "\n"
Carl Worth163b22c2012-08-10 10:15:30 -0700106 " --thread=THREAD_ID Only retain calls from specified thread\n"
Carl Worth16b18db2012-08-11 11:33:12 -0700107 "\n"
Carl Worth163b22c2012-08-10 10:15:30 -0700108 " -o, --output=TRACE_FILE Output trace file\n"
José Fonsecad3c00132012-01-27 22:43:53 +0000109 "\n"
110 ;
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800111}
112
José Fonsecab682b672012-02-15 07:13:31 +0000113enum {
Imre Deak6f07a842012-05-08 15:20:43 +0300114 CALLS_OPT = CHAR_MAX + 1,
Carl Worth163b22c2012-08-10 10:15:30 -0700115 DEPS_OPT,
116 NO_DEPS_OPT,
Carl Worth16b18db2012-08-11 11:33:12 -0700117 PRUNE_OPT,
118 NO_PRUNE_OPT,
Imre Deak6f07a842012-05-08 15:20:43 +0300119 THREAD_OPT,
José Fonsecab682b672012-02-15 07:13:31 +0000120};
121
122const static char *
Carl Worth16b18db2012-08-11 11:33:12 -0700123shortOptions = "ho:x";
José Fonsecab682b672012-02-15 07:13:31 +0000124
125const static struct option
126longOptions[] = {
127 {"help", no_argument, 0, 'h'},
128 {"calls", required_argument, 0, CALLS_OPT},
Carl Worth163b22c2012-08-10 10:15:30 -0700129 {"deps", no_argument, 0, DEPS_OPT},
130 {"no-deps", no_argument, 0, NO_DEPS_OPT},
Carl Worth16b18db2012-08-11 11:33:12 -0700131 {"prune", no_argument, 0, PRUNE_OPT},
132 {"no-prune", no_argument, 0, NO_PRUNE_OPT},
133 {"exact", no_argument, 0, 'x'},
Imre Deak6f07a842012-05-08 15:20:43 +0300134 {"thread", required_argument, 0, THREAD_OPT},
135 {"output", required_argument, 0, 'o'},
José Fonsecab682b672012-02-15 07:13:31 +0000136 {0, 0, 0, 0}
137};
138
Carl Worth163b22c2012-08-10 10:15:30 -0700139struct stringCompare {
140 bool operator() (const char *a, const char *b) const {
141 return strcmp(a, b) < 0;
142 }
143};
144
145class TraceAnalyzer {
Carl Worth33b92632012-08-14 14:11:19 -0700146 /* Maps for tracking resource dependencies between calls. */
147 std::map<std::string, std::set<unsigned> > resources;
148 std::map<std::string, std::set<std::string> > dependencies;
149
150 /* Maps for tracking OpenGL state. */
151 std::map<GLenum, unsigned> texture_map;
Carl Worth163b22c2012-08-10 10:15:30 -0700152
153 /* The final set of calls required. This consists of calls added
154 * explicitly with the require() method as well as all calls
155 * implicitly required by those through resource dependencies. */
156 std::set<unsigned> required;
157
Carl Worthcc6e51c2012-08-13 14:35:43 -0700158 bool transformFeedbackActive;
159 bool framebufferObjectActive;
160 bool insideBeginEnd;
Carl Worth5ff38572012-09-04 11:49:13 -0700161 GLuint activeProgram;
Carl Worth163b22c2012-08-10 10:15:30 -0700162
Carl Worthcc6e51c2012-08-13 14:35:43 -0700163 /* Rendering often has no side effects, but it can in some cases,
164 * (such as when transform feedback is active, or when rendering
165 * targets a framebuffer object). */
166 bool renderingHasSideEffect() {
167 return transformFeedbackActive || framebufferObjectActive;
168 }
169
170 /* Provide: Record that the given call affects the given resource
171 * as a side effect. */
Carl Worth33b92632012-08-14 14:11:19 -0700172 void provide(std::string resource, trace::CallNo call_no) {
Carl Worthcc6e51c2012-08-13 14:35:43 -0700173 resources[resource].insert(call_no);
174 }
175
Carl Worth33b92632012-08-14 14:11:19 -0700176 /* Link: Establish a dependency between resource 'resource' and
177 * resource 'dependency'. This dependency is captured by name so
178 * that if the list of calls that provide 'dependency' grows
179 * before 'resource' is consumed, those calls will still be
180 * captured. */
181 void link(std::string resource, std::string dependency) {
182 dependencies[resource].insert(dependency);
183 }
184
185 /* Unlink: Remove dependency from 'resource' on 'dependency'. */
186 void unlink(std::string resource, std::string dependency) {
187 dependencies[resource].erase(dependency);
188 if (dependencies[resource].size() == 0) {
189 dependencies.erase(resource);
190 }
191 }
192
193 /* Unlink all: Remove dependencies from 'resource' to all other
194 * resources. */
195 void unlinkAll(std::string resource) {
196 dependencies.erase(resource);
197 }
198
199 /* Resolve: Recursively compute all calls providing 'resource',
200 * (including linked dependencies of 'resource' on other
201 * resources). */
202 std::set<unsigned> resolve(std::string resource) {
203 std::set<std::string> *deps;
204 std::set<std::string>::iterator dep;
Carl Worthcc6e51c2012-08-13 14:35:43 -0700205
206 std::set<unsigned> *calls;
207 std::set<unsigned>::iterator call;
208
Carl Worth33b92632012-08-14 14:11:19 -0700209 std::set<unsigned> result, deps_set;
210
211 /* Recursively chase dependencies. */
212 if (dependencies.count(resource)) {
213 deps = &dependencies[resource];
214 for (dep = deps->begin(); dep != deps->end(); dep++) {
215 deps_set = resolve(*dep);
216 for (call = deps_set.begin(); call != deps_set.end(); call++) {
217 result.insert(*call);
218 }
219 }
220 }
221
222 /* Also look for calls that directly provide 'resource' */
Carl Worthcc6e51c2012-08-13 14:35:43 -0700223 if (resources.count(resource)) {
224 calls = &resources[resource];
225 for (call = calls->begin(); call != calls->end(); call++) {
Carl Worth33b92632012-08-14 14:11:19 -0700226 result.insert(*call);
Carl Worthcc6e51c2012-08-13 14:35:43 -0700227 }
Carl Worth33b92632012-08-14 14:11:19 -0700228 }
229
230 return result;
231 }
232
233 /* Consume: Resolve all calls that provide the given resource, and
234 * add them to the required list. Then clear the call list for
235 * 'resource' along with any dependencies. */
236 void consume(std::string resource) {
237
238 std::set<unsigned> calls;
239 std::set<unsigned>::iterator call;
240
241 calls = resolve(resource);
242
243 dependencies.erase(resource);
244 resources.erase(resource);
245
246 for (call = calls.begin(); call != calls.end(); call++) {
247 required.insert(*call);
Carl Worthcc6e51c2012-08-13 14:35:43 -0700248 }
249 }
250
251 void stateTrackPreCall(trace::Call *call) {
252
Carl Worth33b92632012-08-14 14:11:19 -0700253 const char *name = call->name();
254
255 if (strcmp(name, "glBegin") == 0) {
Carl Worthcc6e51c2012-08-13 14:35:43 -0700256 insideBeginEnd = true;
257 return;
258 }
259
Carl Worth33b92632012-08-14 14:11:19 -0700260 if (strcmp(name, "glBeginTransformFeedback") == 0) {
Carl Worthcc6e51c2012-08-13 14:35:43 -0700261 transformFeedbackActive = true;
262 return;
263 }
264
Carl Worth33b92632012-08-14 14:11:19 -0700265 if (strcmp(name, "glBindTexture") == 0) {
266 GLenum target;
267 GLuint texture;
268
269 target = static_cast<GLenum>(call->arg(0).toSInt());
270 texture = call->arg(1).toUInt();
271
272 if (texture == 0) {
273 texture_map.erase(target);
274 } else {
275 texture_map[target] = texture;
276 }
277
278 return;
279 }
280
Carl Worth5ff38572012-09-04 11:49:13 -0700281 if (strcmp(name, "glUseProgram") == 0) {
282 activeProgram = call->arg(0).toUInt();
283 }
284
Carl Worth33b92632012-08-14 14:11:19 -0700285 if (strcmp(name, "glBindFramebuffer") == 0) {
Carl Worthcc6e51c2012-08-13 14:35:43 -0700286 GLenum target;
287 GLuint framebuffer;
288
289 target = static_cast<GLenum>(call->arg(0).toSInt());
290 framebuffer = call->arg(1).toUInt();
291
292 if (target == GL_FRAMEBUFFER || target == GL_DRAW_FRAMEBUFFER) {
293 if (framebuffer == 0) {
294 framebufferObjectActive = false;
295 } else {
296 framebufferObjectActive = true;
297 }
298 }
299 return;
300 }
301 }
302
303 void stateTrackPostCall(trace::Call *call) {
304
Carl Worth33b92632012-08-14 14:11:19 -0700305 const char *name = call->name();
306
307 if (strcmp(name, "glEnd") == 0) {
Carl Worthcc6e51c2012-08-13 14:35:43 -0700308 insideBeginEnd = false;
309 return;
310 }
311
Carl Worth33b92632012-08-14 14:11:19 -0700312 if (strcmp(name, "glEndTransformFeedback") == 0) {
Carl Worthcc6e51c2012-08-13 14:35:43 -0700313 transformFeedbackActive = false;
314 return;
315 }
316
Carl Worth33b92632012-08-14 14:11:19 -0700317 /* If this swapbuffers was included in the trace then it will
318 * have already consumed all framebuffer dependencies. If not,
319 * then clear them now so that they don't carry over into the
320 * next frame. */
Carl Worthcc6e51c2012-08-13 14:35:43 -0700321 if (call->flags & trace::CALL_FLAG_SWAP_RENDERTARGET &&
322 call->flags & trace::CALL_FLAG_END_FRAME) {
Carl Worth33b92632012-08-14 14:11:19 -0700323 dependencies.erase("framebuffer");
Carl Worthcc6e51c2012-08-13 14:35:43 -0700324 resources.erase("framebuffer");
325 return;
326 }
327 }
328
329 void recordSideEffects(trace::Call *call) {
Carl Worth33b92632012-08-14 14:11:19 -0700330
331 const char *name = call->name();
332
Carl Worthcc6e51c2012-08-13 14:35:43 -0700333 /* If call is flagged as no side effects, then we are done here. */
Carl Worth5b827e12012-08-12 20:41:50 -0700334 if (call->flags & trace::CALL_FLAG_NO_SIDE_EFFECTS) {
335 return;
336 }
337
Carl Worthcc6e51c2012-08-13 14:35:43 -0700338 /* Similarly, swap-buffers calls don't have interesting side effects. */
Carl Worth5b827e12012-08-12 20:41:50 -0700339 if (call->flags & trace::CALL_FLAG_SWAP_RENDERTARGET &&
340 call->flags & trace::CALL_FLAG_END_FRAME) {
341 return;
342 }
343
Carl Worth33b92632012-08-14 14:11:19 -0700344 if (strcmp(name, "glGenTextures") == 0) {
345 const trace::Array *textures = dynamic_cast<const trace::Array *>(&call->arg(1));
346 size_t i;
347 GLuint texture;
348
349 if (textures) {
350 for (i = 0; i < textures->size(); i++) {
351 std::stringstream ss;
352
353 texture = textures->values[i]->toUInt();
354 ss << "texture-" << texture;
355
356 provide(ss.str(), call->no);
357 }
358 }
359 return;
360 }
361
362 if (strcmp(name, "glBindTexture") == 0) {
363 GLenum target;
364 GLuint texture;
365
366 std::stringstream ss_target, ss_texture;
367
368 target = static_cast<GLenum>(call->arg(0).toSInt());
369 texture = call->arg(1).toUInt();
370
371 ss_target << "texture-target-" << target;
372 ss_texture << "texture-" << texture;
373
374 resources.erase(ss_target.str());
375 provide(ss_target.str(), call->no);
376
377 unlinkAll(ss_target.str());
378 link(ss_target.str(), ss_texture.str());
379
380 return;
381 }
382
383 /* FIXME: Need to handle glMultTexImage and friends. */
384 if (STRNCMP_LITERAL(name, "glTexImage") == 0 ||
385 STRNCMP_LITERAL(name, "glTexSubImage") == 0 ||
386 STRNCMP_LITERAL(name, "glCopyTexImage") == 0 ||
387 STRNCMP_LITERAL(name, "glCopyTexSubImage") == 0 ||
388 STRNCMP_LITERAL(name, "glCompressedTexImage") == 0 ||
389 STRNCMP_LITERAL(name, "glCompressedTexSubImage") == 0 ||
390 strcmp(name, "glInvalidateTexImage") == 0 ||
391 strcmp(name, "glInvalidateTexSubImage") == 0) {
392
393 std::set<unsigned> *calls;
394 std::set<unsigned>::iterator c;
395 std::stringstream ss_target, ss_texture;
396
397 GLenum target = static_cast<GLenum>(call->arg(0).toSInt());
398
399 ss_target << "texture-target-" << target;
400 ss_texture << "texture-" << texture_map[target];
401
402 /* The texture resource depends on this call and any calls
403 * providing the given texture target. */
404 provide(ss_texture.str(), call->no);
405
406 if (resources.count(ss_target.str())) {
407 calls = &resources[ss_target.str()];
408 for (c = calls->begin(); c != calls->end(); c++) {
409 provide(ss_texture.str(), *c);
410 }
411 }
412
413 return;
414 }
415
416 if (strcmp(name, "glEnable") == 0) {
417 GLenum cap;
418
419 cap = static_cast<GLenum>(call->arg(0).toSInt());
420
421 if (cap == GL_TEXTURE_1D ||
422 cap == GL_TEXTURE_2D ||
423 cap == GL_TEXTURE_3D ||
424 cap == GL_TEXTURE_CUBE_MAP)
425 {
426 std::stringstream ss;
427
428 ss << "texture-target-" << cap;
429
430 link("render-state", ss.str());
431 }
432
433 provide("state", call->no);
434
435 return;
436 }
437
438 if (strcmp(name, "glDisable") == 0) {
439 GLenum cap;
440
441 cap = static_cast<GLenum>(call->arg(0).toSInt());
442
443 if (cap == GL_TEXTURE_1D ||
444 cap == GL_TEXTURE_2D ||
445 cap == GL_TEXTURE_3D ||
446 cap == GL_TEXTURE_CUBE_MAP)
447 {
448 std::stringstream ss;
449
450 ss << "texture-target-" << cap;
451
452 unlink("render-state", ss.str());
453 }
454
455 provide("state", call->no);
456
457 return;
458 }
459
Carl Worthbbbbe702012-08-14 15:20:56 -0700460 if (strcmp(name, "glCreateShader") == 0 ||
461 strcmp(name, "glCreateShaderObjectARB") == 0) {
462
463 GLuint shader;
464 std::stringstream ss;
465
466 shader = call->ret->toUInt();
467
468 ss << "shader-" << shader;
469
470 provide(ss.str(), call->no);
471
472 return;
473 }
474
475 if (strcmp(name, "glShaderSource") == 0 ||
476 strcmp(name, "glShaderSourceARB") == 0 ||
477 strcmp(name, "glCompileShader") == 0 ||
478 strcmp(name, "glCompileShaderARB") == 0 ||
479 strcmp(name, "glGetShaderiv") == 0 ||
480 strcmp(name, "glGetShaderInfoLog") == 0) {
481
482 GLuint shader;
483 std::stringstream ss;
484
485 shader = call->arg(0).toUInt();
486
487 ss << "shader-" << shader;
488
489 provide(ss.str(), call->no);
490
491 return;
492 }
493
494 if (strcmp(name, "glCreateProgram") == 0 ||
495 strcmp(name, "glCreateProgramObjectARB") == 0) {
496
497 GLuint program;
498 std::stringstream ss;
499
500 program = call->ret->toUInt();
501
502 ss << "program-" << program;
503
504 provide(ss.str(), call->no);
505
506 return;
507 }
508
509 if (strcmp(name, "glAttachShader") == 0 ||
510 strcmp(name, "glAttachObjectARB") == 0) {
511
512 GLuint program, shader;
513 std::stringstream ss_program, ss_shader;
514
515 program = call->arg(0).toUInt();
516 shader = call->arg(1).toUInt();
517
518 ss_program << "program-" << program;
519 ss_shader << "shader-" << shader;
520
521 link(ss_program.str(), ss_shader.str());
522 provide(ss_program.str(), call->no);
523
524 return;
525 }
526
527 if (strcmp(name, "glDetachShader") == 0 ||
528 strcmp(name, "glDetachObjectARB") == 0) {
529
530 GLuint program, shader;
531 std::stringstream ss_program, ss_shader;
532
533 program = call->arg(0).toUInt();
534 shader = call->arg(1).toUInt();
535
536 ss_program << "program-" << program;
537 ss_shader << "shader-" << shader;
538
539 unlink(ss_program.str(), ss_shader.str());
540
541 return;
542 }
543
544 if (strcmp(name, "glUseProgram") == 0 ||
545 strcmp(name, "glUseProgramObjectARB") == 0) {
546
547 GLuint program;
548
549 program = call->arg(0).toUInt();
550
551 unlinkAll("render-program-state");
552
553 if (program == 0) {
554 unlink("render-state", "render-program-state");
555 provide("state", call->no);
556 } else {
557 std::stringstream ss;
558
559 ss << "program-" << program;
560
561 link("render-state", "render-program-state");
562 link("render-program-state", ss.str());
563
564 provide(ss.str(), call->no);
565 }
566
567 return;
568 }
569
570 if (strcmp(name, "glGetUniformLocation") == 0 ||
571 strcmp(name, "glGetUniformLocationARB") == 0 ||
572 strcmp(name, "glGetFragDataLocation") == 0 ||
573 strcmp(name, "glGetFragDataLocationEXT") == 0 ||
574 strcmp(name, "glGetSubroutineUniformLocation") == 0 ||
575 strcmp(name, "glGetProgramResourceLocation") == 0 ||
576 strcmp(name, "glGetProgramResourceLocationIndex") == 0 ||
577 strcmp(name, "glGetVaryingLocationNV") == 0) {
578
579 GLuint program;
Carl Worthbbbbe702012-08-14 15:20:56 -0700580 std::stringstream ss;
581
582 program = call->arg(0).toUInt();
Carl Worthbbbbe702012-08-14 15:20:56 -0700583
584 ss << "program-" << program;
585
586 provide(ss.str(), call->no);
587
588 return;
589 }
590
591 /* For any call that accepts 'location' as its first argument,
592 * perform a lookup in our location->program map and add a
593 * dependence on the program we find there. */
594 if (call->sig->num_args > 0 &&
595 strcmp(call->sig->arg_names[0], "location") == 0) {
596
Carl Worthbbbbe702012-08-14 15:20:56 -0700597 std::stringstream ss;
598
Carl Worth5ff38572012-09-04 11:49:13 -0700599 ss << "program-" << activeProgram;
Carl Worthbbbbe702012-08-14 15:20:56 -0700600
601 provide(ss.str(), call->no);
602
603 return;
604 }
605
606 /* FIXME: We cut a huge swath by assuming that any unhandled
607 * call that has a first argument named "program" should not
608 * be included in the trimmed output unless the program of
609 * that number is also included.
610 *
611 * This heuristic is correct for many cases, but we should
612 * actually carefully verify if this includes some calls
613 * inappropriately, or if it misses some.
614 */
615 if (strcmp(name, "glLinkProgram") == 0 ||
616 strcmp(name, "glLinkProgramARB") == 0 ||
617 (call->sig->num_args > 0 &&
618 (strcmp(call->sig->arg_names[0], "program") == 0 ||
619 strcmp(call->sig->arg_names[0], "programObj") == 0))) {
620
621 GLuint program;
622 std::stringstream ss;
623
624 program = call->arg(0).toUInt();
625
626 ss << "program-" << program;
627
628 provide(ss.str(), call->no);
629
630 return;
631 }
632
Carl Worthcc6e51c2012-08-13 14:35:43 -0700633 /* Handle all rendering operations, (even though only glEnd is
634 * flagged as a rendering operation we treat everything from
635 * glBegin through glEnd as a rendering operation). */
636 if (call->flags & trace::CALL_FLAG_RENDER ||
637 insideBeginEnd) {
638
Carl Worth33b92632012-08-14 14:11:19 -0700639 std::set<unsigned> calls;
640 std::set<unsigned>::iterator c;
641
Carl Worthcc6e51c2012-08-13 14:35:43 -0700642 provide("framebuffer", call->no);
643
Carl Worth33b92632012-08-14 14:11:19 -0700644 calls = resolve("render-state");
645
646 for (c = calls.begin(); c != calls.end(); c++) {
647 provide("framebuffer", *c);
648 }
649
Carl Worthcc6e51c2012-08-13 14:35:43 -0700650 /* In some cases, rendering has side effects beyond the
651 * framebuffer update. */
652 if (renderingHasSideEffect()) {
653 provide("state", call->no);
Carl Worth33b92632012-08-14 14:11:19 -0700654 for (c = calls.begin(); c != calls.end(); c++) {
655 provide("state", *c);
656 }
Carl Worthcc6e51c2012-08-13 14:35:43 -0700657 }
658
659 return;
660 }
661
Carl Worth5b827e12012-08-12 20:41:50 -0700662 /* By default, assume this call affects the state somehow. */
Carl Worth163b22c2012-08-10 10:15:30 -0700663 resources["state"].insert(call->no);
664 }
665
Carl Worthcc6e51c2012-08-13 14:35:43 -0700666 void requireDependencies(trace::Call *call) {
667
668 /* Swap-buffers calls depend on framebuffer state. */
669 if (call->flags & trace::CALL_FLAG_SWAP_RENDERTARGET &&
670 call->flags & trace::CALL_FLAG_END_FRAME) {
671 consume("framebuffer");
672 }
673
674 /* By default, just assume this call depends on generic state. */
675 consume("state");
676 }
677
678
679public:
680 TraceAnalyzer(): transformFeedbackActive(false),
681 framebufferObjectActive(false),
682 insideBeginEnd(false)
683 {}
684
685 ~TraceAnalyzer() {}
686
687 /* Analyze this call by tracking state and recording all the
688 * resources provided by this call as side effects.. */
689 void analyze(trace::Call *call) {
690
691 stateTrackPreCall(call);
692
693 recordSideEffects(call);
694
695 stateTrackPostCall(call);
696 }
697
Carl Worth163b22c2012-08-10 10:15:30 -0700698 /* Require this call and all of its dependencies to be included in
699 * the final trace. */
700 void require(trace::Call *call) {
Carl Worth163b22c2012-08-10 10:15:30 -0700701
702 /* First, find and insert all calls that this call depends on. */
Carl Worthcc6e51c2012-08-13 14:35:43 -0700703 requireDependencies(call);
Carl Worth163b22c2012-08-10 10:15:30 -0700704
705 /* Then insert this call itself. */
706 required.insert(call->no);
707 }
708
709 /* Return a set of all the required calls, (both those calls added
710 * explicitly with require() and those implicitly depended
711 * upon. */
712 std::set<unsigned> *get_required(void) {
713 return &required;
714 }
715};
716
717struct trim_options {
718 /* Calls to be included in trace. */
719 trace::CallSet calls;
720
721 /* Whether dependency analysis should be performed. */
722 bool dependency_analysis;
723
Carl Worth16b18db2012-08-11 11:33:12 -0700724 /* Whether uninteresting calls should be pruned.. */
725 bool prune_uninteresting;
726
Carl Worth163b22c2012-08-10 10:15:30 -0700727 /* Output filename */
728 std::string output;
729
730 /* Emit only calls from this thread (-1 == all threads) */
731 int thread;
732};
733
734static int
735trim_trace(const char *filename, struct trim_options *options)
736{
737 trace::ParseBookmark beginning;
738 trace::Parser p;
739 TraceAnalyzer analyzer;
740 std::set<unsigned> *required;
741
742 if (!p.open(filename)) {
743 std::cerr << "error: failed to open " << filename << "\n";
744 return 1;
745 }
746
747 /* Mark the beginning so we can return here for pass 2. */
748 p.getBookmark(beginning);
749
750 /* In pass 1, analyze which calls are needed. */
751 trace::Call *call;
752 while ((call = p.parse_call())) {
Carl Worth5b827e12012-08-12 20:41:50 -0700753
754 /* There's no use doing any work past the last call requested
755 * by the user. */
Carl Worth42249012012-08-14 10:26:11 -0700756 if (call->no > options->calls.getLast()) {
757 delete call;
Carl Worth5b827e12012-08-12 20:41:50 -0700758 break;
Carl Worth42249012012-08-14 10:26:11 -0700759 }
Carl Worth5b827e12012-08-12 20:41:50 -0700760
Carl Worth163b22c2012-08-10 10:15:30 -0700761 /* If requested, ignore all calls not belonging to the specified thread. */
Carl Worth42249012012-08-14 10:26:11 -0700762 if (options->thread != -1 && call->thread_id != options->thread) {
763 delete call;
Carl Worth163b22c2012-08-10 10:15:30 -0700764 continue;
Carl Worth42249012012-08-14 10:26:11 -0700765 }
Carl Worth163b22c2012-08-10 10:15:30 -0700766
Carl Worth16b18db2012-08-11 11:33:12 -0700767 /* Also, prune if uninteresting (unless the user asked for no pruning. */
768 if (options->prune_uninteresting && call->flags & trace::CALL_FLAG_UNINTERESTING) {
Carl Worth42249012012-08-14 10:26:11 -0700769 delete call;
Carl Worth16b18db2012-08-11 11:33:12 -0700770 continue;
771 }
772
Carl Worthcc6e51c2012-08-13 14:35:43 -0700773 /* If this call is included in the user-specified call set,
774 * then require it (and all dependencies) in the trimmed
775 * output. */
Carl Worth163b22c2012-08-10 10:15:30 -0700776 if (options->calls.contains(*call)) {
777 analyzer.require(call);
Carl Worthcc6e51c2012-08-13 14:35:43 -0700778 }
779
780 /* Regardless of whether we include this call or not, we do
781 * some dependency tracking (unless disabled by the user). We
782 * do this even for calls we have included in the output so
783 * that any state updates get performed. */
784 if (options->dependency_analysis) {
785 analyzer.analyze(call);
Carl Worth163b22c2012-08-10 10:15:30 -0700786 }
Carl Worth42249012012-08-14 10:26:11 -0700787
788 delete call;
Carl Worth163b22c2012-08-10 10:15:30 -0700789 }
790
791 /* Prepare output file and writer for output. */
792 if (options->output.empty()) {
793 os::String base(filename);
794 base.trimExtension();
795
796 options->output = std::string(base.str()) + std::string("-trim.trace");
797 }
798
799 trace::Writer writer;
800 if (!writer.open(options->output.c_str())) {
801 std::cerr << "error: failed to create " << filename << "\n";
802 return 1;
803 }
804
805 /* Reset bookmark for pass 2. */
806 p.setBookmark(beginning);
807
808 /* In pass 2, emit the calls that are required. */
809 required = analyzer.get_required();
810
811 while ((call = p.parse_call())) {
Carl Worth5b827e12012-08-12 20:41:50 -0700812
813 /* There's no use doing any work past the last call requested
814 * by the user. */
815 if (call->no > options->calls.getLast())
816 break;
817
Carl Worth163b22c2012-08-10 10:15:30 -0700818 if (required->find(call->no) != required->end()) {
819 writer.writeCall(call);
820 }
821 delete call;
822 }
823
824 std::cout << "Trimmed trace is available as " << options->output << "\n";
825
826 return 0;
827}
828
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800829static int
830command(int argc, char *argv[])
831{
Carl Worth163b22c2012-08-10 10:15:30 -0700832 struct trim_options options;
833
834 options.calls = trace::CallSet(trace::FREQUENCY_ALL);
835 options.dependency_analysis = true;
Carl Worth16b18db2012-08-11 11:33:12 -0700836 options.prune_uninteresting = true;
Carl Worth163b22c2012-08-10 10:15:30 -0700837 options.output = "";
838 options.thread = -1;
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800839
José Fonsecab682b672012-02-15 07:13:31 +0000840 int opt;
841 while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
842 switch (opt) {
843 case 'h':
Carl Worth86464432012-08-11 11:46:54 -0700844 help();
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800845 return 0;
José Fonsecab682b672012-02-15 07:13:31 +0000846 case CALLS_OPT:
Carl Worth163b22c2012-08-10 10:15:30 -0700847 options.calls = trace::CallSet(optarg);
848 break;
849 case DEPS_OPT:
850 options.dependency_analysis = true;
851 break;
852 case NO_DEPS_OPT:
853 options.dependency_analysis = false;
José Fonsecab682b672012-02-15 07:13:31 +0000854 break;
Carl Worth16b18db2012-08-11 11:33:12 -0700855 case PRUNE_OPT:
856 options.prune_uninteresting = true;
857 break;
858 case NO_PRUNE_OPT:
859 options.prune_uninteresting = false;
860 break;
861 case 'x':
862 options.dependency_analysis = false;
863 options.prune_uninteresting = false;
864 break;
Imre Deak6f07a842012-05-08 15:20:43 +0300865 case THREAD_OPT:
Carl Worth163b22c2012-08-10 10:15:30 -0700866 options.thread = atoi(optarg);
Imre Deak6f07a842012-05-08 15:20:43 +0300867 break;
José Fonsecab682b672012-02-15 07:13:31 +0000868 case 'o':
Carl Worth163b22c2012-08-10 10:15:30 -0700869 options.output = optarg;
José Fonsecab682b672012-02-15 07:13:31 +0000870 break;
871 default:
872 std::cerr << "error: unexpected option `" << opt << "`\n";
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800873 usage();
874 return 1;
875 }
876 }
877
José Fonsecab682b672012-02-15 07:13:31 +0000878 if (optind >= argc) {
879 std::cerr << "error: apitrace trim requires a trace file as an argument.\n";
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800880 usage();
881 return 1;
882 }
883
Carl Worthf630d9d2012-09-04 16:48:00 -0700884 if (argc > optind + 1) {
885 std::cerr << "error: extraneous arguments:";
886 for (int i = optind + 1; i < argc; i++) {
887 std::cerr << " " << argv[i];
888 }
889 std::cerr << "\n";
890 usage();
891 return 1;
892 }
893
Carl Worth163b22c2012-08-10 10:15:30 -0700894 return trim_trace(argv[optind], &options);
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800895}
896
897const Command trim_command = {
898 "trim",
899 synopsis,
Carl Worth86464432012-08-11 11:46:54 -0700900 help,
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800901 command
902};