]> git.cworth.org Git - apitrace/blob - cli/cli_trim.cpp
trim: Add support for trimming out unused shaders.
[apitrace] / cli / cli_trim.cpp
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
27 #include <sstream>
28 #include <string.h>
29 #include <limits.h> // for CHAR_MAX
30 #include <getopt.h>
31
32 #include <GL/gl.h>
33 #include <GL/glext.h>
34
35 #include <set>
36
37 #include "cli.hpp"
38
39 #include "os_string.hpp"
40
41 #include "trace_callset.hpp"
42 #include "trace_parser.hpp"
43 #include "trace_writer.hpp"
44
45 #define STRNCMP_LITERAL(var, literal) strncmp((var), (literal), sizeof (literal) -1)
46
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
53         << "usage: apitrace trim [OPTIONS] TRACE_FILE...\n"
54         << synopsis << "\n"
55         "\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"
66     ;
67 }
68
69 static void
70 help()
71 {
72     std::cout
73         << "usage: apitrace trim [OPTIONS] TRACE_FILE...\n"
74         << synopsis << "\n"
75         "\n"
76         "    -h, --help               Show this help message and exit\n"
77         "\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"
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"
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"
106         "        --thread=THREAD_ID   Only retain calls from specified thread\n"
107         "\n"
108         "    -o, --output=TRACE_FILE  Output trace file\n"
109         "\n"
110     ;
111 }
112
113 enum {
114     CALLS_OPT = CHAR_MAX + 1,
115     DEPS_OPT,
116     NO_DEPS_OPT,
117     PRUNE_OPT,
118     NO_PRUNE_OPT,
119     THREAD_OPT,
120 };
121
122 const static char *
123 shortOptions = "ho:x";
124
125 const static struct option
126 longOptions[] = {
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'},
136     {0, 0, 0, 0}
137 };
138
139 struct stringCompare {
140     bool operator() (const char *a, const char *b) const {
141         return strcmp(a, b) < 0;
142     }
143 };
144
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;
149
150     /* Maps for tracking OpenGL state. */
151     std::map<GLenum, unsigned> texture_map;
152     std::map<GLint, GLuint> location_program_map;
153
154     /* The final set of calls required. This consists of calls added
155      * explicitly with the require() method as well as all calls
156      * implicitly required by those through resource dependencies. */
157     std::set<unsigned> required;
158
159     bool transformFeedbackActive;
160     bool framebufferObjectActive;
161     bool insideBeginEnd;
162
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;
168     }
169
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);
174     }
175
176     /* 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;
205
206         std::set<unsigned> *calls;
207         std::set<unsigned>::iterator call;
208
209         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' */
223         if (resources.count(resource)) {
224             calls = &resources[resource];
225             for (call = calls->begin(); call != calls->end(); call++) {
226                 result.insert(*call);
227             }
228         }
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);
248         }
249     }
250
251     void stateTrackPreCall(trace::Call *call) {
252
253         const char *name = call->name();
254
255         if (strcmp(name, "glBegin") == 0) {
256             insideBeginEnd = true;
257             return;
258         }
259
260         if (strcmp(name, "glBeginTransformFeedback") == 0) {
261             transformFeedbackActive = true;
262             return;
263         }
264
265         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
281         if (strcmp(name, "glBindFramebuffer") == 0) {
282             GLenum target;
283             GLuint framebuffer;
284
285             target = static_cast<GLenum>(call->arg(0).toSInt());
286             framebuffer = call->arg(1).toUInt();
287
288             if (target == GL_FRAMEBUFFER || target == GL_DRAW_FRAMEBUFFER) {
289                 if (framebuffer == 0) {
290                     framebufferObjectActive = false;
291                 } else {
292                     framebufferObjectActive = true;
293                 }
294             }
295             return;
296         }
297     }
298
299     void stateTrackPostCall(trace::Call *call) {
300
301         const char *name = call->name();
302
303         if (strcmp(name, "glEnd") == 0) {
304             insideBeginEnd = false;
305             return;
306         }
307
308         if (strcmp(name, "glEndTransformFeedback") == 0) {
309             transformFeedbackActive = false;
310             return;
311         }
312
313         /* If this swapbuffers was included in the trace then it will
314          * have already consumed all framebuffer dependencies. If not,
315          * then clear them now so that they don't carry over into the
316          * next frame. */
317         if (call->flags & trace::CALL_FLAG_SWAP_RENDERTARGET &&
318             call->flags & trace::CALL_FLAG_END_FRAME) {
319             dependencies.erase("framebuffer");
320             resources.erase("framebuffer");
321             return;
322         }
323     }
324
325     void recordSideEffects(trace::Call *call) {
326
327         const char *name = call->name();
328
329         /* If call is flagged as no side effects, then we are done here. */
330         if (call->flags & trace::CALL_FLAG_NO_SIDE_EFFECTS) {
331             return;
332         }
333
334         /* Similarly, swap-buffers calls don't have interesting side effects. */
335         if (call->flags & trace::CALL_FLAG_SWAP_RENDERTARGET &&
336             call->flags & trace::CALL_FLAG_END_FRAME) {
337             return;
338         }
339
340         if (strcmp(name, "glGenTextures") == 0) {
341             const trace::Array *textures = dynamic_cast<const trace::Array *>(&call->arg(1));
342             size_t i;
343             GLuint texture;
344
345             if (textures) {
346                 for (i = 0; i < textures->size(); i++) {
347                     std::stringstream ss;
348
349                     texture = textures->values[i]->toUInt();
350                     ss << "texture-" << texture;
351
352                     provide(ss.str(), call->no);
353                 }
354             }
355             return;
356         }
357
358         if (strcmp(name, "glBindTexture") == 0) {
359             GLenum target;
360             GLuint texture;
361
362             std::stringstream ss_target, ss_texture;
363
364             target = static_cast<GLenum>(call->arg(0).toSInt());
365             texture = call->arg(1).toUInt();
366
367             ss_target << "texture-target-" << target;
368             ss_texture << "texture-" << texture;
369
370             resources.erase(ss_target.str());
371             provide(ss_target.str(), call->no);
372
373             unlinkAll(ss_target.str());
374             link(ss_target.str(), ss_texture.str());
375
376             return;
377         }
378
379         /* FIXME: Need to handle glMultTexImage and friends. */
380         if (STRNCMP_LITERAL(name, "glTexImage") == 0 ||
381             STRNCMP_LITERAL(name, "glTexSubImage") == 0 ||
382             STRNCMP_LITERAL(name, "glCopyTexImage") == 0 ||
383             STRNCMP_LITERAL(name, "glCopyTexSubImage") == 0 ||
384             STRNCMP_LITERAL(name, "glCompressedTexImage") == 0 ||
385             STRNCMP_LITERAL(name, "glCompressedTexSubImage") == 0 ||
386             strcmp(name, "glInvalidateTexImage") == 0 ||
387             strcmp(name, "glInvalidateTexSubImage") == 0) {
388
389             std::set<unsigned> *calls;
390             std::set<unsigned>::iterator c;
391             std::stringstream ss_target, ss_texture;
392
393             GLenum target = static_cast<GLenum>(call->arg(0).toSInt());
394
395             ss_target << "texture-target-" << target;
396             ss_texture << "texture-" << texture_map[target];
397
398             /* The texture resource depends on this call and any calls
399              * providing the given texture target. */
400             provide(ss_texture.str(), call->no);
401
402             if (resources.count(ss_target.str())) {
403                 calls = &resources[ss_target.str()];
404                 for (c = calls->begin(); c != calls->end(); c++) {
405                     provide(ss_texture.str(), *c);
406                 }
407             }
408
409             return;
410         }
411
412         if (strcmp(name, "glEnable") == 0) {
413             GLenum cap;
414
415             cap = static_cast<GLenum>(call->arg(0).toSInt());
416
417             if (cap == GL_TEXTURE_1D ||
418                 cap == GL_TEXTURE_2D ||
419                 cap == GL_TEXTURE_3D ||
420                 cap == GL_TEXTURE_CUBE_MAP)
421             {
422                 std::stringstream ss;
423
424                 ss << "texture-target-" << cap;
425
426                 link("render-state", ss.str());
427             }
428
429             provide("state", call->no);
430
431             return;
432         }
433
434         if (strcmp(name, "glDisable") == 0) {
435             GLenum cap;
436
437             cap = static_cast<GLenum>(call->arg(0).toSInt());
438
439             if (cap == GL_TEXTURE_1D ||
440                 cap == GL_TEXTURE_2D ||
441                 cap == GL_TEXTURE_3D ||
442                 cap == GL_TEXTURE_CUBE_MAP)
443             {
444                 std::stringstream ss;
445
446                 ss << "texture-target-" << cap;
447
448                 unlink("render-state", ss.str());
449             }
450
451             provide("state", call->no);
452
453             return;
454         }
455
456         if (strcmp(name, "glCreateShader") == 0 ||
457             strcmp(name, "glCreateShaderObjectARB") == 0) {
458
459             GLuint shader;
460             std::stringstream ss;
461
462             shader = call->ret->toUInt();
463
464             ss << "shader-" << shader;
465
466             provide(ss.str(), call->no);
467
468             return;
469         }
470
471         if (strcmp(name, "glShaderSource") == 0 ||
472             strcmp(name, "glShaderSourceARB") == 0 ||
473             strcmp(name, "glCompileShader") == 0 ||
474             strcmp(name, "glCompileShaderARB") == 0 ||
475             strcmp(name, "glGetShaderiv") == 0 ||
476             strcmp(name, "glGetShaderInfoLog") == 0) {
477
478             GLuint shader;
479             std::stringstream ss;
480
481             shader = call->arg(0).toUInt();
482
483             ss << "shader-" << shader;
484
485             provide(ss.str(), call->no);
486
487             return;
488         }
489
490         if (strcmp(name, "glCreateProgram") == 0 ||
491             strcmp(name, "glCreateProgramObjectARB") == 0) {
492
493             GLuint program;
494             std::stringstream ss;
495
496             program = call->ret->toUInt();
497
498             ss << "program-" << program;
499
500             provide(ss.str(), call->no);
501
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
575             GLuint program;
576             GLint location;
577             std::stringstream ss;
578
579             program = call->arg(0).toUInt();
580             location = call->ret->toSInt();
581
582             location_program_map[location] = program;
583
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
597             GLuint program;
598             GLint location;
599             std::stringstream ss;
600
601             location = call->arg(0).toSInt();
602
603             program = location_program_map[location];
604
605             ss << "program-" << program;
606
607             provide(ss.str(), call->no);
608
609             return;
610         }
611
612         /* FIXME: We cut a huge swath by assuming that any unhandled
613          * call that has a first argument named "program" should not
614          * be included in the trimmed output unless the program of
615          * that number is also included.
616          *
617          * This heuristic is correct for many cases, but we should
618          * actually carefully verify if this includes some calls
619          * inappropriately, or if it misses some.
620          */
621         if (strcmp(name, "glLinkProgram") == 0 ||
622             strcmp(name, "glLinkProgramARB") == 0 ||
623             (call->sig->num_args > 0 &&
624              (strcmp(call->sig->arg_names[0], "program") == 0 ||
625               strcmp(call->sig->arg_names[0], "programObj") == 0))) {
626
627             GLuint program;
628             std::stringstream ss;
629
630             program = call->arg(0).toUInt();
631
632             ss << "program-" << program;
633
634             provide(ss.str(), call->no);
635
636             return;
637         }
638
639         /* Handle all rendering operations, (even though only glEnd is
640          * flagged as a rendering operation we treat everything from
641          * glBegin through glEnd as a rendering operation). */
642         if (call->flags & trace::CALL_FLAG_RENDER ||
643             insideBeginEnd) {
644
645             std::set<unsigned> calls;
646             std::set<unsigned>::iterator c;
647
648             provide("framebuffer", call->no);
649
650             calls = resolve("render-state");
651
652             for (c = calls.begin(); c != calls.end(); c++) {
653                 provide("framebuffer", *c);
654             }
655
656             /* In some cases, rendering has side effects beyond the
657              * framebuffer update. */
658             if (renderingHasSideEffect()) {
659                 provide("state", call->no);
660                 for (c = calls.begin(); c != calls.end(); c++) {
661                     provide("state", *c);
662                 }
663             }
664
665             return;
666         }
667
668         /* By default, assume this call affects the state somehow. */
669         resources["state"].insert(call->no);
670     }
671
672     void requireDependencies(trace::Call *call) {
673
674         /* Swap-buffers calls depend on framebuffer state. */
675         if (call->flags & trace::CALL_FLAG_SWAP_RENDERTARGET &&
676             call->flags & trace::CALL_FLAG_END_FRAME) {
677             consume("framebuffer");
678         }
679
680         /* By default, just assume this call depends on generic state. */
681         consume("state");
682     }
683
684
685 public:
686     TraceAnalyzer(): transformFeedbackActive(false),
687                      framebufferObjectActive(false),
688                      insideBeginEnd(false)
689     {}
690
691     ~TraceAnalyzer() {}
692
693     /* Analyze this call by tracking state and recording all the
694      * resources provided by this call as side effects.. */
695     void analyze(trace::Call *call) {
696
697         stateTrackPreCall(call);
698
699         recordSideEffects(call);
700
701         stateTrackPostCall(call);
702     }
703
704     /* Require this call and all of its dependencies to be included in
705      * the final trace. */
706     void require(trace::Call *call) {
707
708         /* First, find and insert all calls that this call depends on. */
709         requireDependencies(call);
710
711         /* Then insert this call itself. */
712         required.insert(call->no);
713     }
714
715     /* Return a set of all the required calls, (both those calls added
716      * explicitly with require() and those implicitly depended
717      * upon. */
718     std::set<unsigned> *get_required(void) {
719         return &required;
720     }
721 };
722
723 struct trim_options {
724     /* Calls to be included in trace. */
725     trace::CallSet calls;
726
727     /* Whether dependency analysis should be performed. */
728     bool dependency_analysis;
729
730     /* Whether uninteresting calls should be pruned.. */
731     bool prune_uninteresting;
732
733     /* Output filename */
734     std::string output;
735
736     /* Emit only calls from this thread (-1 == all threads) */
737     int thread;
738 };
739
740 static int
741 trim_trace(const char *filename, struct trim_options *options)
742 {
743     trace::ParseBookmark beginning;
744     trace::Parser p;
745     TraceAnalyzer analyzer;
746     std::set<unsigned> *required;
747
748     if (!p.open(filename)) {
749         std::cerr << "error: failed to open " << filename << "\n";
750         return 1;
751     }
752
753     /* Mark the beginning so we can return here for pass 2. */
754     p.getBookmark(beginning);
755
756     /* In pass 1, analyze which calls are needed. */
757     trace::Call *call;
758     while ((call = p.parse_call())) {
759
760         /* There's no use doing any work past the last call requested
761          * by the user. */
762         if (call->no > options->calls.getLast()) {
763             delete call;
764             break;
765         }
766
767         /* If requested, ignore all calls not belonging to the specified thread. */
768         if (options->thread != -1 && call->thread_id != options->thread) {
769             delete call;
770             continue;
771         }
772
773         /* Also, prune if uninteresting (unless the user asked for no pruning. */
774         if (options->prune_uninteresting && call->flags & trace::CALL_FLAG_UNINTERESTING) {
775             delete call;
776             continue;
777         }
778
779         /* If this call is included in the user-specified call set,
780          * then require it (and all dependencies) in the trimmed
781          * output. */
782         if (options->calls.contains(*call)) {
783             analyzer.require(call);
784         }
785
786         /* Regardless of whether we include this call or not, we do
787          * some dependency tracking (unless disabled by the user). We
788          * do this even for calls we have included in the output so
789          * that any state updates get performed. */
790         if (options->dependency_analysis) {
791             analyzer.analyze(call);
792         }
793
794         delete call;
795     }
796
797     /* Prepare output file and writer for output. */
798     if (options->output.empty()) {
799         os::String base(filename);
800         base.trimExtension();
801
802         options->output = std::string(base.str()) + std::string("-trim.trace");
803     }
804
805     trace::Writer writer;
806     if (!writer.open(options->output.c_str())) {
807         std::cerr << "error: failed to create " << filename << "\n";
808         return 1;
809     }
810
811     /* Reset bookmark for pass 2. */
812     p.setBookmark(beginning);
813
814     /* In pass 2, emit the calls that are required. */
815     required = analyzer.get_required();
816
817     while ((call = p.parse_call())) {
818
819         /* There's no use doing any work past the last call requested
820          * by the user. */
821         if (call->no > options->calls.getLast())
822             break;
823
824         if (required->find(call->no) != required->end()) {
825             writer.writeCall(call);
826         }
827         delete call;
828     }
829
830     std::cout << "Trimmed trace is available as " << options->output << "\n";
831
832     return 0;
833 }
834
835 static int
836 command(int argc, char *argv[])
837 {
838     struct trim_options options;
839
840     options.calls = trace::CallSet(trace::FREQUENCY_ALL);
841     options.dependency_analysis = true;
842     options.prune_uninteresting = true;
843     options.output = "";
844     options.thread = -1;
845
846     int opt;
847     while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
848         switch (opt) {
849         case 'h':
850             help();
851             return 0;
852         case CALLS_OPT:
853             options.calls = trace::CallSet(optarg);
854             break;
855         case DEPS_OPT:
856             options.dependency_analysis = true;
857             break;
858         case NO_DEPS_OPT:
859             options.dependency_analysis = false;
860             break;
861         case PRUNE_OPT:
862             options.prune_uninteresting = true;
863             break;
864         case NO_PRUNE_OPT:
865             options.prune_uninteresting = false;
866             break;
867         case 'x':
868             options.dependency_analysis = false;
869             options.prune_uninteresting = false;
870             break;
871         case THREAD_OPT:
872             options.thread = atoi(optarg);
873             break;
874         case 'o':
875             options.output = optarg;
876             break;
877         default:
878             std::cerr << "error: unexpected option `" << opt << "`\n";
879             usage();
880             return 1;
881         }
882     }
883
884     if (optind >= argc) {
885         std::cerr << "error: apitrace trim requires a trace file as an argument.\n";
886         usage();
887         return 1;
888     }
889
890     if (argc > optind + 1) {
891         std::cerr << "error: extraneous arguments:";
892         for (int i = optind + 1; i < argc; i++) {
893             std::cerr << " " << argv[i];
894         }
895         std::cerr << "\n";
896         usage();
897         return 1;
898     }
899
900     return trim_trace(argv[optind], &options);
901 }
902
903 const Command trim_command = {
904     "trim",
905     synopsis,
906     help,
907     command
908 };