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
36 #include "os_string.hpp"
38 #include "trace_analyzer.hpp"
39 #include "trace_callset.hpp"
40 #include "trace_parser.hpp"
41 #include "trace_writer.hpp"
43 static const char *synopsis = "Create a new trace by trimming an existing trace.";
49 << "usage: apitrace trim [OPTIONS] TRACE_FILE...\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 " --no-deps Do not include any more calls than requestd\n"
57 " --prune Omit calls without side effects from the output\n"
58 " --no-prune Do not omit any requested calls\n"
59 " -a, --auto Trim automatically to calls specified in --calls/--frames\n"
60 " Equivalent to both --deps and --prune\n"
61 " --exact Trim to exactly the calls specified in --calls/--frames\n"
62 " Equivalent to both --no-deps and --no-prune\n"
63 " --print-callset Print the final set of calls included in output\n"
64 " --trim-spec=SPEC Only performing trimming as described in SPEC\n"
65 " --thread=THREAD_ID Only retain calls from specified thread\n"
66 " -o, --output=TRACE_FILE Output trace file\n"
74 << "usage: apitrace trim [OPTIONS] TRACE_FILE...\n"
77 " -h, --help Show this help message and exit\n"
79 " --calls=CALLSET Include specified calls in the trimmed output.\n"
80 " --frames=FRAMESET Include specified frames in the trimmed output.\n"
82 " --deps Perform dependency analysis and include dependent\n"
83 " calls as needed, (even if those calls were not\n"
84 " explicitly requested with --calls or --frames).\n"
85 " --no-deps Do not perform dependency analysis. Output will\n"
86 " not include any additional calls beyond those\n"
87 " explicitly requested with --calls or --frames).\n"
89 " --prune Omit calls with no side effects, even if the call\n"
90 " is within the range specified by --calls/--frames.\n"
92 " --no-prune Never omit any calls from the range specified\n"
93 " --calls/--frames.\n"
95 " -a, --auto Use dependency analysis and pruning\n"
96 " of uninteresting calls the resulting trace may\n"
97 " include more and less calls than specified.\n"
98 " This option is equivalent\n"
99 " to passing both --deps and --prune.\n"
101 " --exact Trim output to exact the calls or frames\n"
102 " specified with --calls or --frames.\n"
103 " This option is equivalent\n"
104 " to passing both --no-deps and --no-prune.\n"
106 " --print-callset Print to stdout the final set of calls included\n"
107 " in the trim output. This can be useful for\n"
108 " tweaking the trimmed callset from --auto on the\n"
110 " Use --calls=@FILE to read callset from a file.\n"
111 " --trim-spec=SPEC Specifies which classes of calls will be trimmed.\n"
112 " This option only has an effect if dependency\n"
113 " analysis is enabled. The argument is a comma-\n"
114 " separated list of names from the following:\n"
116 " no-side-effects Calls with no side effects\n"
117 " textures Calls to setup unused textures\n"
118 " shaders Calls to setup unused shaders\n"
119 " drawing Calls that draw\n"
121 " The default trim specification includes all of\n"
122 " the above, (as much as possible will be trimmed).\n"
124 " --thread=THREAD_ID Only retain calls from specified thread\n"
126 " -o, --output=TRACE_FILE Output trace file\n"
132 CALLS_OPT = CHAR_MAX + 1,
145 shortOptions = "aho:x";
147 const static struct option
149 {"help", no_argument, 0, 'h'},
150 {"calls", required_argument, 0, CALLS_OPT},
151 {"frames", required_argument, 0, FRAMES_OPT},
152 {"deps", no_argument, 0, DEPS_OPT},
153 {"no-deps", no_argument, 0, NO_DEPS_OPT},
154 {"prune", no_argument, 0, PRUNE_OPT},
155 {"no-prune", no_argument, 0, NO_PRUNE_OPT},
156 {"auto", no_argument, 0, 'a'},
157 {"exact", no_argument, 0, EXACT_OPT},
158 {"thread", required_argument, 0, THREAD_OPT},
159 {"output", required_argument, 0, 'o'},
160 {"print-callset", no_argument, 0, PRINT_CALLSET_OPT},
161 {"trim-spec", required_argument, 0, TRIM_SPEC_OPT},
165 struct stringCompare {
166 bool operator() (const char *a, const char *b) const {
167 return strcmp(a, b) < 0;
171 struct trim_options {
172 /* Calls to be included in trace. */
173 trace::CallSet calls;
175 /* Frames to be included in trace. */
176 trace::CallSet frames;
178 /* Whether dependency analysis should be performed. */
179 bool dependency_analysis;
181 /* Whether uninteresting calls should be pruned.. */
182 bool prune_uninteresting;
184 /* Output filename */
187 /* Emit only calls from this thread (-1 == all threads) */
190 /* Print resulting callset */
193 /* What kind of trimming to perform. */
194 TrimFlags trim_flags;
198 trim_trace(const char *filename, struct trim_options *options)
200 trace::ParseBookmark beginning;
202 TraceAnalyzer analyzer(options->trim_flags);
203 std::set<unsigned> *required;
205 int call_range_first, call_range_last;
207 if (!p.open(filename)) {
208 std::cerr << "error: failed to open " << filename << "\n";
212 /* Mark the beginning so we can return here for pass 2. */
213 p.getBookmark(beginning);
215 /* In pass 1, analyze which calls are needed. */
218 while ((call = p.parse_call())) {
220 /* There's no use doing any work past the last call and frame
221 * requested by the user. */
222 if ((options->calls.empty() || call->no > options->calls.getLast()) &&
223 (options->frames.empty() || frame > options->frames.getLast())) {
229 /* If requested, ignore all calls not belonging to the specified thread. */
230 if (options->thread != -1 && call->thread_id != options->thread) {
234 /* Also, prune if no side effects (unless the user asked for no pruning. */
235 if (options->prune_uninteresting && call->flags & trace::CALL_FLAG_NO_SIDE_EFFECTS) {
239 /* If this call is included in the user-specified call set,
240 * then require it (and all dependencies) in the trimmed
242 if (options->calls.contains(*call) ||
243 options->frames.contains(frame, call->flags)) {
245 analyzer.require(call);
248 /* Regardless of whether we include this call or not, we do
249 * some dependency tracking (unless disabled by the user). We
250 * do this even for calls we have included in the output so
251 * that any state updates get performed. */
252 if (options->dependency_analysis) {
253 analyzer.analyze(call);
257 if (call->flags & trace::CALL_FLAG_END_FRAME)
263 /* Prepare output file and writer for output. */
264 if (options->output.empty()) {
265 os::String base(filename);
266 base.trimExtension();
268 options->output = std::string(base.str()) + std::string("-trim.trace");
271 trace::Writer writer;
272 if (!writer.open(options->output.c_str())) {
273 std::cerr << "error: failed to create " << filename << "\n";
277 /* Reset bookmark for pass 2. */
278 p.setBookmark(beginning);
280 /* In pass 2, emit the calls that are required. */
281 required = analyzer.get_required();
284 call_range_first = -1;
285 call_range_last = -1;
286 while ((call = p.parse_call())) {
288 /* There's no use doing any work past the last call and frame
289 * requested by the user. */
290 if ((options->calls.empty() || call->no > options->calls.getLast()) &&
291 (options->frames.empty() || frame > options->frames.getLast())) {
296 if (required->find(call->no) != required->end()) {
297 writer.writeCall(call);
299 if (options->print_callset) {
300 if (call_range_first < 0) {
301 call_range_first = call->no;
302 printf ("%d", call_range_first);
303 } else if (call->no != call_range_last + 1) {
304 if (call_range_last != call_range_first)
305 printf ("-%d", call_range_last);
306 call_range_first = call->no;
307 printf (",%d", call_range_first);
309 call_range_last = call->no;
313 if (call->flags & trace::CALL_FLAG_END_FRAME) {
320 if (options->print_callset) {
321 if (call_range_last != call_range_first)
322 printf ("-%d\n", call_range_last);
325 std::cerr << "Trimmed trace is available as " << options->output << "\n";
331 parse_trim_spec(const char *trim_spec, TrimFlags *flags)
333 std::string spec(trim_spec), word;
334 size_t start = 0, comma = 0;
337 while (start < spec.size()) {
338 comma = spec.find(',', start);
340 if (comma == std::string::npos)
341 word = std::string(spec, start);
343 word = std::string(spec, start, comma - start);
345 if (strcmp(word.c_str(), "no-side-effects") == 0)
346 *flags |= TRIM_FLAG_NO_SIDE_EFFECTS;
347 else if (strcmp(word.c_str(), "textures") == 0)
348 *flags |= TRIM_FLAG_TEXTURES;
349 else if (strcmp(word.c_str(), "shaders") == 0)
350 *flags |= TRIM_FLAG_SHADERS;
351 else if (strcmp(word.c_str(), "drawing") == 0)
352 *flags |= TRIM_FLAG_DRAWING;
357 if (comma == std::string::npos)
367 command(int argc, char *argv[])
369 struct trim_options options;
371 options.calls = trace::CallSet(trace::FREQUENCY_NONE);
372 options.frames = trace::CallSet(trace::FREQUENCY_NONE);
373 options.dependency_analysis = false;
374 options.prune_uninteresting = false;
377 options.print_callset = 0;
378 options.trim_flags = -1;
381 while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
387 options.calls = trace::CallSet(optarg);
390 options.frames = trace::CallSet(optarg);
393 options.dependency_analysis = true;
396 options.dependency_analysis = false;
399 options.prune_uninteresting = true;
402 options.prune_uninteresting = false;
405 options.dependency_analysis = true;
406 options.prune_uninteresting = true;
409 options.dependency_analysis = false;
410 options.prune_uninteresting = false;
413 options.thread = atoi(optarg);
416 options.output = optarg;
418 case PRINT_CALLSET_OPT:
419 options.print_callset = 1;
422 if (parse_trim_spec(optarg, &options.trim_flags)) {
423 std::cerr << "error: illegal value for trim-spec: " << optarg << "\n";
424 std::cerr << "See \"apitrace help trim\" for help.\n";
429 std::cerr << "error: unexpected option `" << opt << "`\n";
435 /* If neither of --calls nor --frames was set, default to the
436 * entire set of calls. */
437 if (options.calls.empty() && options.frames.empty()) {
438 options.calls = trace::CallSet(trace::FREQUENCY_ALL);
441 if (optind >= argc) {
442 std::cerr << "error: apitrace trim requires a trace file as an argument.\n";
447 if (argc > optind + 1) {
448 std::cerr << "error: extraneous arguments:";
449 for (int i = optind + 1; i < argc; i++) {
450 std::cerr << " " << argv[i];
457 if (options.dependency_analysis) {
459 "Note: The dependency analysis in \"apitrace trim\" is still experimental.\n"
460 " We hope that it will be useful, but it may lead to incorrect results.\n"
461 " If you find a trace that misbehaves while trimming, please share that\n"
462 " by sending email to apitrace@lists.freedesktop.org, cworth@cworth.org\n";
465 return trim_trace(argv[optind], &options);
468 const Command trim_command = {