blob: b0cb8f167557fbe162b174a50ddf2610ce905b2b [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 Worth163b22c2012-08-10 10:15:30 -070032#include <set>
33
Carl Worth4c5f6fa2011-11-14 14:50:07 -080034#include "cli.hpp"
35
36#include "os_string.hpp"
37
Carl Worth8efbe012012-08-17 14:15:54 -070038#include "trace_analyzer.hpp"
José Fonsecad3c00132012-01-27 22:43:53 +000039#include "trace_callset.hpp"
Carl Worth4c5f6fa2011-11-14 14:50:07 -080040#include "trace_parser.hpp"
José Fonseca630471a2012-01-27 22:06:51 +000041#include "trace_writer.hpp"
Carl Worth4c5f6fa2011-11-14 14:50:07 -080042
43static const char *synopsis = "Create a new trace by trimming an existing trace.";
44
45static void
46usage(void)
47{
48 std::cout
José Fonsecab682b672012-02-15 07:13:31 +000049 << "usage: apitrace trim [OPTIONS] TRACE_FILE...\n"
José Fonsecad3c00132012-01-27 22:43:53 +000050 << synopsis << "\n"
51 "\n"
Carl Worth86464432012-08-11 11:46:54 -070052 " -h, --help Show detailed help for trim options and exit\n"
53 " --calls=CALLSET Include specified calls in the trimmed output.\n"
Carl Worth46abfd12012-08-14 22:32:29 -070054 " --frames=FRAMESET Include specified frames in the trimmed output.\n"
Carl Worth86464432012-08-11 11:46:54 -070055 " --deps Include additional calls to satisfy dependencies\n"
Carl Worth86464432012-08-11 11:46:54 -070056 " --prune Omit uninteresting calls from the trace output\n"
José Fonseca6e4768b2012-11-22 08:31:47 +000057 " -a, --auto Trim automatically to calls specified in --calls/--frames\n"
58 " Equivalent to both --deps and --prune\n"
Carl Worthab82b3b2012-08-15 16:35:38 -070059 " --print-callset Print the final set of calls included in output\n"
Carl Worth25fe5242012-11-14 20:55:31 -080060 " --trim-spec=SPEC Only performing trimming as described in SPEC\n"
Carl Worth86464432012-08-11 11:46:54 -070061 " --thread=THREAD_ID Only retain calls from specified thread\n"
62 " -o, --output=TRACE_FILE Output trace file\n"
63 ;
64}
65
66static void
67help()
68{
69 std::cout
70 << "usage: apitrace trim [OPTIONS] TRACE_FILE...\n"
71 << synopsis << "\n"
72 "\n"
Carl Worth163b22c2012-08-10 10:15:30 -070073 " -h, --help Show this help message and exit\n"
Carl Worth16b18db2012-08-11 11:33:12 -070074 "\n"
75 " --calls=CALLSET Include specified calls in the trimmed output.\n"
Carl Worth46abfd12012-08-14 22:32:29 -070076 " --frames=FRAMESET Include specified frames in the trimmed output.\n"
Carl Worth16b18db2012-08-11 11:33:12 -070077 "\n"
Carl Worth163b22c2012-08-10 10:15:30 -070078 " --deps Perform dependency analysis and include dependent\n"
Carl Worth16b18db2012-08-11 11:33:12 -070079 " calls as needed, (even if those calls were not\n"
Carl Worth46abfd12012-08-14 22:32:29 -070080 " explicitly requested with --calls or --frames).\n"
Carl Worth16b18db2012-08-11 11:33:12 -070081 "\n"
Carl Worth46abfd12012-08-14 22:32:29 -070082 " --prune Omit calls with no side effects, even if the call\n"
83 " is within the range specified by --calls/--frames.\n"
Carl Worth16b18db2012-08-11 11:33:12 -070084 "\n"
José Fonseca6e4768b2012-11-22 08:31:47 +000085 " -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"
Carl Worth16b18db2012-08-11 11:33:12 -070090 "\n"
Carl Worthab82b3b2012-08-15 16:35:38 -070091 " --print-callset Print to stdout the final set of calls included\n"
92 " in the trim output. This can be useful for\n"
Carl Worth25fe5242012-11-14 20:55:31 -080093 " tweaking the trimmed callset from --auto on the\n"
José Fonseca6e4768b2012-11-22 08:31:47 +000094 " command-line.\n"
95 " Use --calls=@FILE to read callset from a file.\n"
Carl Worth25fe5242012-11-14 20:55:31 -080096 " --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"
Carl Worthab82b3b2012-08-15 16:35:38 -0700108 "\n"
Carl Worth163b22c2012-08-10 10:15:30 -0700109 " --thread=THREAD_ID Only retain calls from specified thread\n"
Carl Worth16b18db2012-08-11 11:33:12 -0700110 "\n"
Carl Worth163b22c2012-08-10 10:15:30 -0700111 " -o, --output=TRACE_FILE Output trace file\n"
José Fonsecad3c00132012-01-27 22:43:53 +0000112 "\n"
113 ;
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800114}
115
José Fonsecab682b672012-02-15 07:13:31 +0000116enum {
Imre Deak6f07a842012-05-08 15:20:43 +0300117 CALLS_OPT = CHAR_MAX + 1,
Carl Worth46abfd12012-08-14 22:32:29 -0700118 FRAMES_OPT,
Carl Worth163b22c2012-08-10 10:15:30 -0700119 DEPS_OPT,
Carl Worth16b18db2012-08-11 11:33:12 -0700120 PRUNE_OPT,
Imre Deak6f07a842012-05-08 15:20:43 +0300121 THREAD_OPT,
Carl Worthab82b3b2012-08-15 16:35:38 -0700122 PRINT_CALLSET_OPT,
Carl Worth25fe5242012-11-14 20:55:31 -0800123 TRIM_SPEC_OPT
José Fonsecab682b672012-02-15 07:13:31 +0000124};
125
126const static char *
Carl Worth54300eb2013-01-28 23:37:17 +1100127shortOptions = "aho:x";
José Fonsecab682b672012-02-15 07:13:31 +0000128
129const static struct option
130longOptions[] = {
131 {"help", no_argument, 0, 'h'},
132 {"calls", required_argument, 0, CALLS_OPT},
Carl Worth46abfd12012-08-14 22:32:29 -0700133 {"frames", required_argument, 0, FRAMES_OPT},
Carl Worth163b22c2012-08-10 10:15:30 -0700134 {"deps", no_argument, 0, DEPS_OPT},
Carl Worth16b18db2012-08-11 11:33:12 -0700135 {"prune", no_argument, 0, PRUNE_OPT},
José Fonseca6e4768b2012-11-22 08:31:47 +0000136 {"auto", no_argument, 0, 'a'},
Imre Deak6f07a842012-05-08 15:20:43 +0300137 {"thread", required_argument, 0, THREAD_OPT},
138 {"output", required_argument, 0, 'o'},
Carl Worthab82b3b2012-08-15 16:35:38 -0700139 {"print-callset", no_argument, 0, PRINT_CALLSET_OPT},
Carl Worth25fe5242012-11-14 20:55:31 -0800140 {"trim-spec", required_argument, 0, TRIM_SPEC_OPT},
José Fonsecab682b672012-02-15 07:13:31 +0000141 {0, 0, 0, 0}
142};
143
Carl Worth163b22c2012-08-10 10:15:30 -0700144struct stringCompare {
145 bool operator() (const char *a, const char *b) const {
146 return strcmp(a, b) < 0;
147 }
148};
149
Carl Worth163b22c2012-08-10 10:15:30 -0700150struct trim_options {
151 /* Calls to be included in trace. */
152 trace::CallSet calls;
153
Carl Worth46abfd12012-08-14 22:32:29 -0700154 /* Frames to be included in trace. */
155 trace::CallSet frames;
156
Carl Worth163b22c2012-08-10 10:15:30 -0700157 /* Whether dependency analysis should be performed. */
158 bool dependency_analysis;
159
Carl Worth16b18db2012-08-11 11:33:12 -0700160 /* Whether uninteresting calls should be pruned.. */
161 bool prune_uninteresting;
162
Carl Worth163b22c2012-08-10 10:15:30 -0700163 /* Output filename */
164 std::string output;
165
166 /* Emit only calls from this thread (-1 == all threads) */
167 int thread;
Carl Worthab82b3b2012-08-15 16:35:38 -0700168
169 /* Print resulting callset */
170 int print_callset;
Carl Worth25fe5242012-11-14 20:55:31 -0800171
172 /* What kind of trimming to perform. */
173 TrimFlags trim_flags;
Carl Worth163b22c2012-08-10 10:15:30 -0700174};
175
176static int
177trim_trace(const char *filename, struct trim_options *options)
178{
179 trace::ParseBookmark beginning;
180 trace::Parser p;
Carl Worth25fe5242012-11-14 20:55:31 -0800181 TraceAnalyzer analyzer(options->trim_flags);
Carl Worth163b22c2012-08-10 10:15:30 -0700182 std::set<unsigned> *required;
Carl Worth46abfd12012-08-14 22:32:29 -0700183 unsigned frame;
Carl Worthab82b3b2012-08-15 16:35:38 -0700184 int call_range_first, call_range_last;
Carl Worth163b22c2012-08-10 10:15:30 -0700185
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. */
Carl Worth46abfd12012-08-14 22:32:29 -0700195 frame = 0;
Carl Worth163b22c2012-08-10 10:15:30 -0700196 trace::Call *call;
197 while ((call = p.parse_call())) {
Carl Worth5b827e12012-08-12 20:41:50 -0700198
Carl Worth46abfd12012-08-14 22:32:29 -0700199 /* There's no use doing any work past the last call or frame
200 * requested by the user. */
201 if (call->no > options->calls.getLast() ||
202 frame > options->frames.getLast()) {
203
Carl Worth42249012012-08-14 10:26:11 -0700204 delete call;
Carl Worth5b827e12012-08-12 20:41:50 -0700205 break;
Carl Worth42249012012-08-14 10:26:11 -0700206 }
Carl Worth5b827e12012-08-12 20:41:50 -0700207
Carl Worth163b22c2012-08-10 10:15:30 -0700208 /* If requested, ignore all calls not belonging to the specified thread. */
Carl Worth42249012012-08-14 10:26:11 -0700209 if (options->thread != -1 && call->thread_id != options->thread) {
Carl Worth46abfd12012-08-14 22:32:29 -0700210 goto NEXT;
Carl Worth42249012012-08-14 10:26:11 -0700211 }
Carl Worth163b22c2012-08-10 10:15:30 -0700212
Carl Worth6e57af82013-02-15 16:31:11 -0800213 /* 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) {
Carl Worth46abfd12012-08-14 22:32:29 -0700215 goto NEXT;
Carl Worth16b18db2012-08-11 11:33:12 -0700216 }
217
Carl Worthcc6e51c2012-08-13 14:35:43 -0700218 /* If this call is included in the user-specified call set,
219 * then require it (and all dependencies) in the trimmed
220 * output. */
Carl Worth46abfd12012-08-14 22:32:29 -0700221 if (options->calls.contains(*call) ||
222 options->frames.contains(frame, call->flags)) {
223
Carl Worth163b22c2012-08-10 10:15:30 -0700224 analyzer.require(call);
Carl Worthcc6e51c2012-08-13 14:35:43 -0700225 }
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);
Carl Worth163b22c2012-08-10 10:15:30 -0700233 }
Carl Worth42249012012-08-14 10:26:11 -0700234
Carl Worth46abfd12012-08-14 22:32:29 -0700235 NEXT:
236 if (call->flags & trace::CALL_FLAG_END_FRAME)
237 frame++;
238
Carl Worth42249012012-08-14 10:26:11 -0700239 delete call;
Carl Worth163b22c2012-08-10 10:15:30 -0700240 }
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
Carl Worth46abfd12012-08-14 22:32:29 -0700262 frame = 0;
Carl Worthab82b3b2012-08-15 16:35:38 -0700263 call_range_first = -1;
264 call_range_last = -1;
Carl Worth163b22c2012-08-10 10:15:30 -0700265 while ((call = p.parse_call())) {
Carl Worth5b827e12012-08-12 20:41:50 -0700266
Carl Worth46abfd12012-08-14 22:32:29 -0700267 /* There's no use doing any work past the last call or frame
268 * requested by the user. */
269 if (call->no > options->calls.getLast() ||
270 frame > options->frames.getLast()) {
271
Carl Worth5b827e12012-08-12 20:41:50 -0700272 break;
Carl Worth46abfd12012-08-14 22:32:29 -0700273 }
Carl Worth5b827e12012-08-12 20:41:50 -0700274
Carl Worth163b22c2012-08-10 10:15:30 -0700275 if (required->find(call->no) != required->end()) {
276 writer.writeCall(call);
Carl Worthab82b3b2012-08-15 16:35:38 -0700277
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 }
Carl Worth163b22c2012-08-10 10:15:30 -0700290 }
Carl Worth46abfd12012-08-14 22:32:29 -0700291
292 if (call->flags & trace::CALL_FLAG_END_FRAME) {
293 frame++;
294 }
295
Carl Worth163b22c2012-08-10 10:15:30 -0700296 delete call;
297 }
298
Carl Worthab82b3b2012-08-15 16:35:38 -0700299 if (options->print_callset) {
300 if (call_range_last != call_range_first)
301 printf ("-%d\n", call_range_last);
302 }
303
Carl Worth1a800a12012-08-17 14:23:13 -0700304 std::cerr << "Trimmed trace is available as " << options->output << "\n";
Carl Worth163b22c2012-08-10 10:15:30 -0700305
306 return 0;
307}
308
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800309static int
Carl Worth25fe5242012-11-14 20:55:31 -0800310parse_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
345static int
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800346command(int argc, char *argv[])
347{
Carl Worth163b22c2012-08-10 10:15:30 -0700348 struct trim_options options;
349
Carl Worth46abfd12012-08-14 22:32:29 -0700350 options.calls = trace::CallSet(trace::FREQUENCY_NONE);
351 options.frames = trace::CallSet(trace::FREQUENCY_NONE);
José Fonseca6e4768b2012-11-22 08:31:47 +0000352 options.dependency_analysis = false;
353 options.prune_uninteresting = false;
Carl Worth163b22c2012-08-10 10:15:30 -0700354 options.output = "";
355 options.thread = -1;
Carl Worthab82b3b2012-08-15 16:35:38 -0700356 options.print_callset = 0;
Carl Worth25fe5242012-11-14 20:55:31 -0800357 options.trim_flags = -1;
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800358
José Fonsecab682b672012-02-15 07:13:31 +0000359 int opt;
360 while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
361 switch (opt) {
362 case 'h':
Carl Worth86464432012-08-11 11:46:54 -0700363 help();
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800364 return 0;
José Fonsecab682b672012-02-15 07:13:31 +0000365 case CALLS_OPT:
Carl Worth163b22c2012-08-10 10:15:30 -0700366 options.calls = trace::CallSet(optarg);
367 break;
Carl Worth46abfd12012-08-14 22:32:29 -0700368 case FRAMES_OPT:
369 options.frames = trace::CallSet(optarg);
370 break;
Carl Worth163b22c2012-08-10 10:15:30 -0700371 case DEPS_OPT:
372 options.dependency_analysis = true;
373 break;
Carl Worth16b18db2012-08-11 11:33:12 -0700374 case PRUNE_OPT:
375 options.prune_uninteresting = true;
376 break;
José Fonseca6e4768b2012-11-22 08:31:47 +0000377 case 'a':
378 options.dependency_analysis = true;
379 options.prune_uninteresting = true;
Carl Worth16b18db2012-08-11 11:33:12 -0700380 break;
Imre Deak6f07a842012-05-08 15:20:43 +0300381 case THREAD_OPT:
Carl Worth163b22c2012-08-10 10:15:30 -0700382 options.thread = atoi(optarg);
Imre Deak6f07a842012-05-08 15:20:43 +0300383 break;
José Fonsecab682b672012-02-15 07:13:31 +0000384 case 'o':
Carl Worth163b22c2012-08-10 10:15:30 -0700385 options.output = optarg;
José Fonsecab682b672012-02-15 07:13:31 +0000386 break;
Carl Worthab82b3b2012-08-15 16:35:38 -0700387 case PRINT_CALLSET_OPT:
388 options.print_callset = 1;
389 break;
Carl Worth25fe5242012-11-14 20:55:31 -0800390 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;
José Fonsecab682b672012-02-15 07:13:31 +0000397 default:
398 std::cerr << "error: unexpected option `" << opt << "`\n";
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800399 usage();
400 return 1;
401 }
402 }
403
Carl Worth46abfd12012-08-14 22:32:29 -0700404 /* 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
José Fonsecab682b672012-02-15 07:13:31 +0000410 if (optind >= argc) {
411 std::cerr << "error: apitrace trim requires a trace file as an argument.\n";
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800412 usage();
413 return 1;
414 }
415
Carl Worthf630d9d2012-09-04 16:48:00 -0700416 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
Carl Worth83ec3f62012-08-17 14:22:30 -0700426 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
Carl Worth163b22c2012-08-10 10:15:30 -0700434 return trim_trace(argv[optind], &options);
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800435}
436
437const Command trim_command = {
438 "trim",
439 synopsis,
Carl Worth86464432012-08-11 11:46:54 -0700440 help,
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800441 command
442};