1 /**************************************************************************
3 * Copyright 2010 VMware, Inc.
4 * Copyright 2011 Intel corporation
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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
25 **************************************************************************/
29 #include <limits.h> // for CHAR_MAX
39 #include "os_string.hpp"
41 #include "trace_callset.hpp"
42 #include "trace_parser.hpp"
43 #include "trace_writer.hpp"
45 #define STRNCMP_LITERAL(var, literal) strncmp((var), (literal), sizeof (literal) -1)
47 static const char *synopsis = "Create a new trace by trimming an existing trace.";
53 << "usage: apitrace trim [OPTIONS] TRACE_FILE...\n"
56 " -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"
73 << "usage: apitrace trim [OPTIONS] TRACE_FILE...\n"
76 " -h, --help Show this help message and exit\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"
85 " --deps Perform dependency analysis and include dependent\n"
86 " 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"
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"
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"
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"
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"
106 " --thread=THREAD_ID Only retain calls from specified thread\n"
108 " -o, --output=TRACE_FILE Output trace file\n"
114 CALLS_OPT = CHAR_MAX + 1,
123 shortOptions = "ho:x";
125 const static struct option
127 {"help", no_argument, 0, 'h'},
128 {"calls", required_argument, 0, CALLS_OPT},
129 {"deps", no_argument, 0, DEPS_OPT},
130 {"no-deps", no_argument, 0, NO_DEPS_OPT},
131 {"prune", no_argument, 0, PRUNE_OPT},
132 {"no-prune", no_argument, 0, NO_PRUNE_OPT},
133 {"exact", no_argument, 0, 'x'},
134 {"thread", required_argument, 0, THREAD_OPT},
135 {"output", required_argument, 0, 'o'},
139 struct stringCompare {
140 bool operator() (const char *a, const char *b) const {
141 return strcmp(a, b) < 0;
145 class TraceAnalyzer {
146 /* 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;
150 /* Maps for tracking OpenGL state. */
151 std::map<GLenum, unsigned> texture_map;
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;
158 bool transformFeedbackActive;
159 bool framebufferObjectActive;
161 GLuint activeProgram;
163 /* 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;
170 /* Provide: Record that the given call affects the given resource
171 * as a side effect. */
172 void provide(std::string resource, trace::CallNo call_no) {
173 resources[resource].insert(call_no);
176 /* Like provide, but with a simply-formatted string, (appending an
177 * integer to the given string). */
178 void providef(std::string resource, int resource_no, trace::CallNo call_no) {
179 std::stringstream ss;
180 ss << resource << resource_no;
181 provide(ss.str(), call_no);
184 /* Link: Establish a dependency between resource 'resource' and
185 * resource 'dependency'. This dependency is captured by name so
186 * that if the list of calls that provide 'dependency' grows
187 * before 'resource' is consumed, those calls will still be
189 void link(std::string resource, std::string dependency) {
190 dependencies[resource].insert(dependency);
193 /* Like link, but with a simply-formatted string, (appending an
194 * integer to the given string). */
195 void linkf(std::string resource, std::string dependency, int dep_no) {
197 std::stringstream ss;
198 ss << dependency << dep_no;
199 link(resource, ss.str());
202 /* Unlink: Remove dependency from 'resource' on 'dependency'. */
203 void unlink(std::string resource, std::string dependency) {
204 dependencies[resource].erase(dependency);
205 if (dependencies[resource].size() == 0) {
206 dependencies.erase(resource);
210 /* Like unlink, but with a simply-formated string, (appending an
211 * integer to the given string). */
212 void unlinkf(std::string resource, std::string dependency, int dep_no) {
214 std::stringstream ss;
215 ss << dependency << dep_no;
216 unlink(resource, ss.str());
219 /* Unlink all: Remove dependencies from 'resource' to all other
221 void unlinkAll(std::string resource) {
222 dependencies.erase(resource);
225 /* Resolve: Recursively compute all calls providing 'resource',
226 * (including linked dependencies of 'resource' on other
228 std::set<unsigned> resolve(std::string resource) {
229 std::set<std::string> *deps;
230 std::set<std::string>::iterator dep;
232 std::set<unsigned> *calls;
233 std::set<unsigned>::iterator call;
235 std::set<unsigned> result, deps_set;
237 /* Recursively chase dependencies. */
238 if (dependencies.count(resource)) {
239 deps = &dependencies[resource];
240 for (dep = deps->begin(); dep != deps->end(); dep++) {
241 deps_set = resolve(*dep);
242 for (call = deps_set.begin(); call != deps_set.end(); call++) {
243 result.insert(*call);
248 /* Also look for calls that directly provide 'resource' */
249 if (resources.count(resource)) {
250 calls = &resources[resource];
251 for (call = calls->begin(); call != calls->end(); call++) {
252 result.insert(*call);
259 /* Consume: Resolve all calls that provide the given resource, and
260 * add them to the required list. Then clear the call list for
261 * 'resource' along with any dependencies. */
262 void consume(std::string resource) {
264 std::set<unsigned> calls;
265 std::set<unsigned>::iterator call;
267 calls = resolve(resource);
269 dependencies.erase(resource);
270 resources.erase(resource);
272 for (call = calls.begin(); call != calls.end(); call++) {
273 required.insert(*call);
277 void stateTrackPreCall(trace::Call *call) {
279 const char *name = call->name();
281 if (strcmp(name, "glBegin") == 0) {
282 insideBeginEnd = true;
286 if (strcmp(name, "glBeginTransformFeedback") == 0) {
287 transformFeedbackActive = true;
291 if (strcmp(name, "glBindTexture") == 0) {
295 target = static_cast<GLenum>(call->arg(0).toSInt());
296 texture = call->arg(1).toUInt();
299 texture_map.erase(target);
301 texture_map[target] = texture;
307 if (strcmp(name, "glUseProgram") == 0) {
308 activeProgram = call->arg(0).toUInt();
311 if (strcmp(name, "glBindFramebuffer") == 0) {
315 target = static_cast<GLenum>(call->arg(0).toSInt());
316 framebuffer = call->arg(1).toUInt();
318 if (target == GL_FRAMEBUFFER || target == GL_DRAW_FRAMEBUFFER) {
319 if (framebuffer == 0) {
320 framebufferObjectActive = false;
322 framebufferObjectActive = true;
329 void stateTrackPostCall(trace::Call *call) {
331 const char *name = call->name();
333 if (strcmp(name, "glEnd") == 0) {
334 insideBeginEnd = false;
338 if (strcmp(name, "glEndTransformFeedback") == 0) {
339 transformFeedbackActive = false;
343 /* If this swapbuffers was included in the trace then it will
344 * have already consumed all framebuffer dependencies. If not,
345 * then clear them now so that they don't carry over into the
347 if (call->flags & trace::CALL_FLAG_SWAP_RENDERTARGET &&
348 call->flags & trace::CALL_FLAG_END_FRAME) {
349 dependencies.erase("framebuffer");
350 resources.erase("framebuffer");
355 void recordSideEffects(trace::Call *call) {
357 const char *name = call->name();
359 /* If call is flagged as no side effects, then we are done here. */
360 if (call->flags & trace::CALL_FLAG_NO_SIDE_EFFECTS) {
364 /* Similarly, swap-buffers calls don't have interesting side effects. */
365 if (call->flags & trace::CALL_FLAG_SWAP_RENDERTARGET &&
366 call->flags & trace::CALL_FLAG_END_FRAME) {
370 if (strcmp(name, "glGenTextures") == 0) {
371 const trace::Array *textures = dynamic_cast<const trace::Array *>(&call->arg(1));
376 for (i = 0; i < textures->size(); i++) {
377 texture = textures->values[i]->toUInt();
378 providef("texture-", texture, call->no);
384 if (strcmp(name, "glBindTexture") == 0) {
388 std::stringstream ss_target, ss_texture;
390 target = static_cast<GLenum>(call->arg(0).toSInt());
391 texture = call->arg(1).toUInt();
393 ss_target << "texture-target-" << target;
394 ss_texture << "texture-" << texture;
396 resources.erase(ss_target.str());
397 provide(ss_target.str(), call->no);
399 unlinkAll(ss_target.str());
400 link(ss_target.str(), ss_texture.str());
405 /* FIXME: Need to handle glMultTexImage and friends. */
406 if (STRNCMP_LITERAL(name, "glTexImage") == 0 ||
407 STRNCMP_LITERAL(name, "glTexSubImage") == 0 ||
408 STRNCMP_LITERAL(name, "glCopyTexImage") == 0 ||
409 STRNCMP_LITERAL(name, "glCopyTexSubImage") == 0 ||
410 STRNCMP_LITERAL(name, "glCompressedTexImage") == 0 ||
411 STRNCMP_LITERAL(name, "glCompressedTexSubImage") == 0 ||
412 strcmp(name, "glInvalidateTexImage") == 0 ||
413 strcmp(name, "glInvalidateTexSubImage") == 0) {
415 std::set<unsigned> *calls;
416 std::set<unsigned>::iterator c;
417 std::stringstream ss_target, ss_texture;
419 GLenum target = static_cast<GLenum>(call->arg(0).toSInt());
421 ss_target << "texture-target-" << target;
422 ss_texture << "texture-" << texture_map[target];
424 /* The texture resource depends on this call and any calls
425 * providing the given texture target. */
426 provide(ss_texture.str(), call->no);
428 if (resources.count(ss_target.str())) {
429 calls = &resources[ss_target.str()];
430 for (c = calls->begin(); c != calls->end(); c++) {
431 provide(ss_texture.str(), *c);
438 if (strcmp(name, "glEnable") == 0) {
441 cap = static_cast<GLenum>(call->arg(0).toSInt());
443 if (cap == GL_TEXTURE_1D ||
444 cap == GL_TEXTURE_2D ||
445 cap == GL_TEXTURE_3D ||
446 cap == GL_TEXTURE_CUBE_MAP)
448 linkf("render-state", "texture-target-", cap);
451 provide("state", call->no);
455 if (strcmp(name, "glDisable") == 0) {
458 cap = static_cast<GLenum>(call->arg(0).toSInt());
460 if (cap == GL_TEXTURE_1D ||
461 cap == GL_TEXTURE_2D ||
462 cap == GL_TEXTURE_3D ||
463 cap == GL_TEXTURE_CUBE_MAP)
465 unlinkf("render-state", "texture-target-", cap);
468 provide("state", call->no);
472 if (strcmp(name, "glCreateShader") == 0 ||
473 strcmp(name, "glCreateShaderObjectARB") == 0) {
475 GLuint shader = call->ret->toUInt();
476 providef("shader-", shader, call->no);
480 if (strcmp(name, "glShaderSource") == 0 ||
481 strcmp(name, "glShaderSourceARB") == 0 ||
482 strcmp(name, "glCompileShader") == 0 ||
483 strcmp(name, "glCompileShaderARB") == 0 ||
484 strcmp(name, "glGetShaderiv") == 0 ||
485 strcmp(name, "glGetShaderInfoLog") == 0) {
487 GLuint shader = call->arg(0).toUInt();
488 providef("shader-", shader, call->no);
492 if (strcmp(name, "glCreateProgram") == 0 ||
493 strcmp(name, "glCreateProgramObjectARB") == 0) {
495 GLuint program = call->ret->toUInt();
496 providef("program-", program, call->no);
500 if (strcmp(name, "glAttachShader") == 0 ||
501 strcmp(name, "glAttachObjectARB") == 0) {
503 GLuint program, shader;
504 std::stringstream ss_program, ss_shader;
506 program = call->arg(0).toUInt();
507 shader = call->arg(1).toUInt();
509 ss_program << "program-" << program;
510 ss_shader << "shader-" << shader;
512 link(ss_program.str(), ss_shader.str());
513 provide(ss_program.str(), call->no);
518 if (strcmp(name, "glDetachShader") == 0 ||
519 strcmp(name, "glDetachObjectARB") == 0) {
521 GLuint program, shader;
522 std::stringstream ss_program, ss_shader;
524 program = call->arg(0).toUInt();
525 shader = call->arg(1).toUInt();
527 ss_program << "program-" << program;
528 ss_shader << "shader-" << shader;
530 unlink(ss_program.str(), ss_shader.str());
535 if (strcmp(name, "glUseProgram") == 0 ||
536 strcmp(name, "glUseProgramObjectARB") == 0) {
540 program = call->arg(0).toUInt();
542 unlinkAll("render-program-state");
545 unlink("render-state", "render-program-state");
546 provide("state", call->no);
548 std::stringstream ss;
550 ss << "program-" << program;
552 link("render-state", "render-program-state");
553 link("render-program-state", ss.str());
555 provide(ss.str(), call->no);
561 if (strcmp(name, "glGetUniformLocation") == 0 ||
562 strcmp(name, "glGetUniformLocationARB") == 0 ||
563 strcmp(name, "glGetFragDataLocation") == 0 ||
564 strcmp(name, "glGetFragDataLocationEXT") == 0 ||
565 strcmp(name, "glGetSubroutineUniformLocation") == 0 ||
566 strcmp(name, "glGetProgramResourceLocation") == 0 ||
567 strcmp(name, "glGetProgramResourceLocationIndex") == 0 ||
568 strcmp(name, "glGetVaryingLocationNV") == 0) {
570 GLuint program = call->arg(0).toUInt();
572 providef("program-", program, call->no);
577 /* For any call that accepts 'location' as its first argument,
578 * perform a lookup in our location->program map and add a
579 * dependence on the program we find there. */
580 if (call->sig->num_args > 0 &&
581 strcmp(call->sig->arg_names[0], "location") == 0) {
583 providef("program-", activeProgram, call->no);
587 /* FIXME: We cut a huge swath by assuming that any unhandled
588 * call that has a first argument named "program" should not
589 * be included in the trimmed output unless the program of
590 * that number is also included.
592 * This heuristic is correct for many cases, but we should
593 * actually carefully verify if this includes some calls
594 * inappropriately, or if it misses some.
596 if (strcmp(name, "glLinkProgram") == 0 ||
597 strcmp(name, "glLinkProgramARB") == 0 ||
598 (call->sig->num_args > 0 &&
599 (strcmp(call->sig->arg_names[0], "program") == 0 ||
600 strcmp(call->sig->arg_names[0], "programObj") == 0))) {
602 GLuint program = call->arg(0).toUInt();
603 providef("program-", program, call->no);
607 /* Handle all rendering operations, (even though only glEnd is
608 * flagged as a rendering operation we treat everything from
609 * glBegin through glEnd as a rendering operation). */
610 if (call->flags & trace::CALL_FLAG_RENDER ||
613 std::set<unsigned> calls;
614 std::set<unsigned>::iterator c;
616 provide("framebuffer", call->no);
618 calls = resolve("render-state");
620 for (c = calls.begin(); c != calls.end(); c++) {
621 provide("framebuffer", *c);
624 /* In some cases, rendering has side effects beyond the
625 * framebuffer update. */
626 if (renderingHasSideEffect()) {
627 provide("state", call->no);
628 for (c = calls.begin(); c != calls.end(); c++) {
629 provide("state", *c);
636 /* By default, assume this call affects the state somehow. */
637 resources["state"].insert(call->no);
640 void requireDependencies(trace::Call *call) {
642 /* Swap-buffers calls depend on framebuffer state. */
643 if (call->flags & trace::CALL_FLAG_SWAP_RENDERTARGET &&
644 call->flags & trace::CALL_FLAG_END_FRAME) {
645 consume("framebuffer");
648 /* By default, just assume this call depends on generic state. */
654 TraceAnalyzer(): transformFeedbackActive(false),
655 framebufferObjectActive(false),
656 insideBeginEnd(false)
661 /* Analyze this call by tracking state and recording all the
662 * resources provided by this call as side effects.. */
663 void analyze(trace::Call *call) {
665 stateTrackPreCall(call);
667 recordSideEffects(call);
669 stateTrackPostCall(call);
672 /* Require this call and all of its dependencies to be included in
673 * the final trace. */
674 void require(trace::Call *call) {
676 /* First, find and insert all calls that this call depends on. */
677 requireDependencies(call);
679 /* Then insert this call itself. */
680 required.insert(call->no);
683 /* Return a set of all the required calls, (both those calls added
684 * explicitly with require() and those implicitly depended
686 std::set<unsigned> *get_required(void) {
691 struct trim_options {
692 /* Calls to be included in trace. */
693 trace::CallSet calls;
695 /* Whether dependency analysis should be performed. */
696 bool dependency_analysis;
698 /* Whether uninteresting calls should be pruned.. */
699 bool prune_uninteresting;
701 /* Output filename */
704 /* Emit only calls from this thread (-1 == all threads) */
709 trim_trace(const char *filename, struct trim_options *options)
711 trace::ParseBookmark beginning;
713 TraceAnalyzer analyzer;
714 std::set<unsigned> *required;
716 if (!p.open(filename)) {
717 std::cerr << "error: failed to open " << filename << "\n";
721 /* Mark the beginning so we can return here for pass 2. */
722 p.getBookmark(beginning);
724 /* In pass 1, analyze which calls are needed. */
726 while ((call = p.parse_call())) {
728 /* There's no use doing any work past the last call requested
730 if (call->no > options->calls.getLast()) {
735 /* If requested, ignore all calls not belonging to the specified thread. */
736 if (options->thread != -1 && call->thread_id != options->thread) {
741 /* Also, prune if uninteresting (unless the user asked for no pruning. */
742 if (options->prune_uninteresting && call->flags & trace::CALL_FLAG_UNINTERESTING) {
747 /* If this call is included in the user-specified call set,
748 * then require it (and all dependencies) in the trimmed
750 if (options->calls.contains(*call)) {
751 analyzer.require(call);
754 /* Regardless of whether we include this call or not, we do
755 * some dependency tracking (unless disabled by the user). We
756 * do this even for calls we have included in the output so
757 * that any state updates get performed. */
758 if (options->dependency_analysis) {
759 analyzer.analyze(call);
765 /* Prepare output file and writer for output. */
766 if (options->output.empty()) {
767 os::String base(filename);
768 base.trimExtension();
770 options->output = std::string(base.str()) + std::string("-trim.trace");
773 trace::Writer writer;
774 if (!writer.open(options->output.c_str())) {
775 std::cerr << "error: failed to create " << filename << "\n";
779 /* Reset bookmark for pass 2. */
780 p.setBookmark(beginning);
782 /* In pass 2, emit the calls that are required. */
783 required = analyzer.get_required();
785 while ((call = p.parse_call())) {
787 /* There's no use doing any work past the last call requested
789 if (call->no > options->calls.getLast())
792 if (required->find(call->no) != required->end()) {
793 writer.writeCall(call);
798 std::cout << "Trimmed trace is available as " << options->output << "\n";
804 command(int argc, char *argv[])
806 struct trim_options options;
808 options.calls = trace::CallSet(trace::FREQUENCY_ALL);
809 options.dependency_analysis = true;
810 options.prune_uninteresting = true;
815 while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
821 options.calls = trace::CallSet(optarg);
824 options.dependency_analysis = true;
827 options.dependency_analysis = false;
830 options.prune_uninteresting = true;
833 options.prune_uninteresting = false;
836 options.dependency_analysis = false;
837 options.prune_uninteresting = false;
840 options.thread = atoi(optarg);
843 options.output = optarg;
846 std::cerr << "error: unexpected option `" << opt << "`\n";
852 if (optind >= argc) {
853 std::cerr << "error: apitrace trim requires a trace file as an argument.\n";
858 if (argc > optind + 1) {
859 std::cerr << "error: extraneous arguments:";
860 for (int i = optind + 1; i < argc; i++) {
861 std::cerr << " " << argv[i];
868 return trim_trace(argv[optind], &options);
871 const Command trim_command = {