]> git.cworth.org Git - apitrace/blob - cli/cli_trim.cpp
trim: Fix early-bailout bug when accepting both --calls and --frames
[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 <set>
33
34 #include "cli.hpp"
35
36 #include "os_string.hpp"
37
38 #include "trace_analyzer.hpp"
39 #include "trace_callset.hpp"
40 #include "trace_parser.hpp"
41 #include "trace_writer.hpp"
42
43 static const char *synopsis = "Create a new trace by trimming an existing trace.";
44
45 static void
46 usage(void)
47 {
48     std::cout
49         << "usage: apitrace trim [OPTIONS] TRACE_FILE...\n"
50         << synopsis << "\n"
51         "\n"
52         "    -h, --help               Show detailed help for trim options and exit\n"
53         "        --calls=CALLSET      Include specified calls in the trimmed output.\n"
54         "        --frames=FRAMESET    Include specified frames in the trimmed output.\n"
55         "        --deps               Include additional calls to satisfy dependencies\n"
56         "        --prune              Omit uninteresting calls from the trace output\n"
57         "    -a, --auto               Trim automatically to calls specified in --calls/--frames\n"
58         "                             Equivalent to both --deps and --prune\n"
59         "        --print-callset      Print the final set of calls included in output\n"
60         "        --trim-spec=SPEC     Only performing trimming as described in SPEC\n"
61         "        --thread=THREAD_ID   Only retain calls from specified thread\n"
62         "    -o, --output=TRACE_FILE  Output trace file\n"
63     ;
64 }
65
66 static void
67 help()
68 {
69     std::cout
70         << "usage: apitrace trim [OPTIONS] TRACE_FILE...\n"
71         << synopsis << "\n"
72         "\n"
73         "    -h, --help               Show this help message and exit\n"
74         "\n"
75         "        --calls=CALLSET      Include specified calls in the trimmed output.\n"
76         "        --frames=FRAMESET    Include specified frames in the trimmed output.\n"
77         "\n"
78         "        --deps               Perform dependency analysis and include dependent\n"
79         "                             calls as needed, (even if those calls were not\n"
80         "                             explicitly requested with --calls or --frames).\n"
81         "\n"
82         "        --prune              Omit calls with no side effects, even if the call\n"
83         "                             is within the range specified by --calls/--frames.\n"
84         "\n"
85         "    -a, --auto               Use dependency analysis and pruning\n"
86         "                             of uninteresting calls the resulting trace may\n"
87         "                             include more and less calls than specified.\n"
88         "                             This option is equivalent\n"
89         "                             to passing both --deps and --prune.\n"
90         "\n"
91         "        --print-callset      Print to stdout the final set of calls included\n"
92         "                             in the trim output. This can be useful for\n"
93         "                             tweaking the trimmed callset from --auto on the\n"
94         "                             command-line.\n"
95         "                             Use --calls=@FILE to read callset from a file.\n"
96         "        --trim-spec=SPEC     Specifies which classes of calls will be trimmed.\n"
97         "                             This option only has an effect if dependency\n"
98         "                             analysis is enabled. The argument is a comma-\n"
99         "                             separated list of names from the following:\n"
100         "\n"
101         "                               no-side-effects  Calls with no side effects\n"
102         "                               textures         Calls to setup unused textures\n"
103         "                               shaders          Calls to setup unused shaders\n"
104         "                               drawing          Calls that draw\n"
105         "\n"
106         "                             The default trim specification includes all of\n"
107         "                             the above, (as much as possible will be trimmed).\n"
108         "\n"
109         "        --thread=THREAD_ID   Only retain calls from specified thread\n"
110         "\n"
111         "    -o, --output=TRACE_FILE  Output trace file\n"
112         "\n"
113     ;
114 }
115
116 enum {
117     CALLS_OPT = CHAR_MAX + 1,
118     FRAMES_OPT,
119     DEPS_OPT,
120     PRUNE_OPT,
121     THREAD_OPT,
122     PRINT_CALLSET_OPT,
123     TRIM_SPEC_OPT
124 };
125
126 const static char *
127 shortOptions = "aho:x";
128
129 const static struct option
130 longOptions[] = {
131     {"help", no_argument, 0, 'h'},
132     {"calls", required_argument, 0, CALLS_OPT},
133     {"frames", required_argument, 0, FRAMES_OPT},
134     {"deps", no_argument, 0, DEPS_OPT},
135     {"prune", no_argument, 0, PRUNE_OPT},
136     {"auto", no_argument, 0, 'a'},
137     {"thread", required_argument, 0, THREAD_OPT},
138     {"output", required_argument, 0, 'o'},
139     {"print-callset", no_argument, 0, PRINT_CALLSET_OPT},
140     {"trim-spec", required_argument, 0, TRIM_SPEC_OPT},
141     {0, 0, 0, 0}
142 };
143
144 struct stringCompare {
145     bool operator() (const char *a, const char *b) const {
146         return strcmp(a, b) < 0;
147     }
148 };
149
150 struct trim_options {
151     /* Calls to be included in trace. */
152     trace::CallSet calls;
153
154     /* Frames to be included in trace. */
155     trace::CallSet frames;
156
157     /* Whether dependency analysis should be performed. */
158     bool dependency_analysis;
159
160     /* Whether uninteresting calls should be pruned.. */
161     bool prune_uninteresting;
162
163     /* Output filename */
164     std::string output;
165
166     /* Emit only calls from this thread (-1 == all threads) */
167     int thread;
168
169     /* Print resulting callset */
170     int print_callset;
171
172     /* What kind of trimming to perform. */
173     TrimFlags trim_flags;
174 };
175
176 static int
177 trim_trace(const char *filename, struct trim_options *options)
178 {
179     trace::ParseBookmark beginning;
180     trace::Parser p;
181     TraceAnalyzer analyzer(options->trim_flags);
182     std::set<unsigned> *required;
183     unsigned frame;
184     int call_range_first, call_range_last;
185
186     if (!p.open(filename)) {
187         std::cerr << "error: failed to open " << filename << "\n";
188         return 1;
189     }
190
191     /* Mark the beginning so we can return here for pass 2. */
192     p.getBookmark(beginning);
193
194     /* In pass 1, analyze which calls are needed. */
195     frame = 0;
196     trace::Call *call;
197     while ((call = p.parse_call())) {
198
199         /* There's no use doing any work past the last call and frame
200          * requested by the user. */
201         if ((options->calls.empty() || call->no > options->calls.getLast()) &&
202             (options->frames.empty() || frame > options->frames.getLast())) {
203
204             delete call;
205             break;
206         }
207
208         /* If requested, ignore all calls not belonging to the specified thread. */
209         if (options->thread != -1 && call->thread_id != options->thread) {
210             goto NEXT;
211         }
212
213         /* Also, prune if no side effects (unless the user asked for no pruning. */
214         if (options->prune_uninteresting && call->flags & trace::CALL_FLAG_NO_SIDE_EFFECTS) {
215             goto NEXT;
216         }
217
218         /* If this call is included in the user-specified call set,
219          * then require it (and all dependencies) in the trimmed
220          * output. */
221         if (options->calls.contains(*call) ||
222             options->frames.contains(frame, call->flags)) {
223
224             analyzer.require(call);
225         }
226
227         /* Regardless of whether we include this call or not, we do
228          * some dependency tracking (unless disabled by the user). We
229          * do this even for calls we have included in the output so
230          * that any state updates get performed. */
231         if (options->dependency_analysis) {
232             analyzer.analyze(call);
233         }
234
235     NEXT:
236         if (call->flags & trace::CALL_FLAG_END_FRAME)
237             frame++;
238
239         delete call;
240     }
241
242     /* Prepare output file and writer for output. */
243     if (options->output.empty()) {
244         os::String base(filename);
245         base.trimExtension();
246
247         options->output = std::string(base.str()) + std::string("-trim.trace");
248     }
249
250     trace::Writer writer;
251     if (!writer.open(options->output.c_str())) {
252         std::cerr << "error: failed to create " << filename << "\n";
253         return 1;
254     }
255
256     /* Reset bookmark for pass 2. */
257     p.setBookmark(beginning);
258
259     /* In pass 2, emit the calls that are required. */
260     required = analyzer.get_required();
261
262     frame = 0;
263     call_range_first = -1;
264     call_range_last = -1;
265     while ((call = p.parse_call())) {
266
267         /* There's no use doing any work past the last call and frame
268          * requested by the user. */
269         if ((options->calls.empty() || call->no > options->calls.getLast()) &&
270             (options->frames.empty() || frame > options->frames.getLast())) {
271
272             break;
273         }
274
275         if (required->find(call->no) != required->end()) {
276             writer.writeCall(call);
277
278             if (options->print_callset) {
279                 if (call_range_first < 0) {
280                     call_range_first = call->no;
281                     printf ("%d", call_range_first);
282                 } else if (call->no != call_range_last + 1) {
283                     if (call_range_last != call_range_first)
284                         printf ("-%d", call_range_last);
285                     call_range_first = call->no;
286                     printf (",%d", call_range_first);
287                 }
288                 call_range_last = call->no;
289             }
290         }
291
292         if (call->flags & trace::CALL_FLAG_END_FRAME) {
293             frame++;
294         }
295
296         delete call;
297     }
298
299     if (options->print_callset) {
300         if (call_range_last != call_range_first)
301             printf ("-%d\n", call_range_last);
302     }
303
304     std::cerr << "Trimmed trace is available as " << options->output << "\n";
305
306     return 0;
307 }
308
309 static int
310 parse_trim_spec(const char *trim_spec, TrimFlags *flags)
311 {
312     std::string spec(trim_spec), word;
313     size_t start = 0, comma = 0;
314     *flags = 0;
315
316     while (start < spec.size()) {
317         comma = spec.find(',', start);
318
319         if (comma == std::string::npos)
320             word = std::string(spec, start);
321         else
322             word = std::string(spec, start, comma - start);
323
324         if (strcmp(word.c_str(), "no-side-effects") == 0)
325             *flags |= TRIM_FLAG_NO_SIDE_EFFECTS;
326         else if (strcmp(word.c_str(), "textures") == 0)
327             *flags |= TRIM_FLAG_TEXTURES;
328         else if (strcmp(word.c_str(), "shaders") == 0)
329             *flags |= TRIM_FLAG_SHADERS;
330         else if (strcmp(word.c_str(), "drawing") == 0)
331             *flags |= TRIM_FLAG_DRAWING;
332         else {
333             return 1;
334         }
335
336         if (comma == std::string::npos)
337             break;
338
339         start = comma + 1;
340     }
341
342     return 0;
343 }
344
345 static int
346 command(int argc, char *argv[])
347 {
348     struct trim_options options;
349
350     options.calls = trace::CallSet(trace::FREQUENCY_NONE);
351     options.frames = trace::CallSet(trace::FREQUENCY_NONE);
352     options.dependency_analysis = false;
353     options.prune_uninteresting = false;
354     options.output = "";
355     options.thread = -1;
356     options.print_callset = 0;
357     options.trim_flags = -1;
358
359     int opt;
360     while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
361         switch (opt) {
362         case 'h':
363             help();
364             return 0;
365         case CALLS_OPT:
366             options.calls = trace::CallSet(optarg);
367             break;
368         case FRAMES_OPT:
369             options.frames = trace::CallSet(optarg);
370             break;
371         case DEPS_OPT:
372             options.dependency_analysis = true;
373             break;
374         case PRUNE_OPT:
375             options.prune_uninteresting = true;
376             break;
377         case 'a':
378             options.dependency_analysis = true;
379             options.prune_uninteresting = true;
380             break;
381         case THREAD_OPT:
382             options.thread = atoi(optarg);
383             break;
384         case 'o':
385             options.output = optarg;
386             break;
387         case PRINT_CALLSET_OPT:
388             options.print_callset = 1;
389             break;
390         case TRIM_SPEC_OPT:
391             if (parse_trim_spec(optarg, &options.trim_flags)) {
392                 std::cerr << "error: illegal value for trim-spec: " << optarg << "\n";
393                 std::cerr << "See \"apitrace help trim\" for help.\n";
394                 return 1;
395             }
396             break;
397         default:
398             std::cerr << "error: unexpected option `" << opt << "`\n";
399             usage();
400             return 1;
401         }
402     }
403
404     /* If neither of --calls nor --frames was set, default to the
405      * entire set of calls. */
406     if (options.calls.empty() && options.frames.empty()) {
407         options.calls = trace::CallSet(trace::FREQUENCY_ALL);
408     }
409
410     if (optind >= argc) {
411         std::cerr << "error: apitrace trim requires a trace file as an argument.\n";
412         usage();
413         return 1;
414     }
415
416     if (argc > optind + 1) {
417         std::cerr << "error: extraneous arguments:";
418         for (int i = optind + 1; i < argc; i++) {
419             std::cerr << " " << argv[i];
420         }
421         std::cerr << "\n";
422         usage();
423         return 1;
424     }
425
426     if (options.dependency_analysis) {
427         std::cerr <<
428             "Note: The dependency analysis in \"apitrace trim\" is still experimental.\n"
429             "      We hope that it will be useful, but it may lead to incorrect results.\n"
430             "      If you find a trace that misbehaves while trimming, please share that\n"
431             "      by sending email to apitrace@lists.freedesktop.org, cworth@cworth.org\n";
432     }
433
434     return trim_trace(argv[optind], &options);
435 }
436
437 const Command trim_command = {
438     "trim",
439     synopsis,
440     help,
441     command
442 };