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 **************************************************************************/
28 #include <limits.h> // for CHAR_MAX
38 #include "os_string.hpp"
40 #include "trace_callset.hpp"
41 #include "trace_parser.hpp"
42 #include "trace_writer.hpp"
44 static const char *synopsis = "Create a new trace by trimming an existing trace.";
50 << "usage: apitrace trim [OPTIONS] TRACE_FILE...\n"
53 " -h, --help Show detailed help for trim options and exit\n"
54 " --calls=CALLSET Include specified calls in the trimmed output.\n"
55 " --deps Include additional calls to satisfy dependencies\n"
56 " --no-deps Do not include calls from dependency analysis\n"
57 " --prune Omit uninteresting calls from the trace output\n"
58 " --no-prune Do not prune uninteresting calls from the trace.\n"
59 " -x, --exact Include exactly the calls specified in --calls\n"
60 " Equivalent to both --no-deps and --no-prune\n"
61 " --thread=THREAD_ID Only retain calls from specified thread\n"
62 " -o, --output=TRACE_FILE Output trace file\n"
70 << "usage: apitrace trim [OPTIONS] TRACE_FILE...\n"
73 " -h, --help Show this help message and exit\n"
75 " --calls=CALLSET Include specified calls in the trimmed output.\n"
76 " Note that due to dependency analysis and pruning\n"
77 " of uninteresting calls the resulting trace may\n"
78 " include more and less calls than specified.\n"
79 " See --no-deps, --no-prune, and --exact to change\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). This is the\n"
85 " default behavior. See --no-deps and --exact.\n"
87 " --no-deps Do not perform dependency analysis. In this mode\n"
88 " the trimmed trace will never include calls from\n"
89 " outside the range specified in --calls.\n"
91 " --prune Omit calls that have no side effects, even if the\n"
92 " call is within the range specified by --calls.\n"
93 " This is the default behavior. See --no-prune\n"
95 " --no-prune Do not prune uninteresting calls from the trace.\n"
96 " In this mode the trimmed trace will never omit\n"
97 " any calls within the range specified in --calls.\n"
99 " -x, --exact Trim the trace to exactly the calls specified in\n"
100 " --calls. This option is equivalent to passing\n"
101 " both --no-deps and --no-prune.\n"
103 " --thread=THREAD_ID Only retain calls from specified thread\n"
105 " -o, --output=TRACE_FILE Output trace file\n"
111 CALLS_OPT = CHAR_MAX + 1,
120 shortOptions = "ho:x";
122 const static struct option
124 {"help", no_argument, 0, 'h'},
125 {"calls", required_argument, 0, CALLS_OPT},
126 {"deps", no_argument, 0, DEPS_OPT},
127 {"no-deps", no_argument, 0, NO_DEPS_OPT},
128 {"prune", no_argument, 0, PRUNE_OPT},
129 {"no-prune", no_argument, 0, NO_PRUNE_OPT},
130 {"exact", no_argument, 0, 'x'},
131 {"thread", required_argument, 0, THREAD_OPT},
132 {"output", required_argument, 0, 'o'},
136 struct stringCompare {
137 bool operator() (const char *a, const char *b) const {
138 return strcmp(a, b) < 0;
142 class TraceAnalyzer {
143 /* Map for tracking resource dependencies between calls. */
144 std::map<const char *, std::set<unsigned>, stringCompare > resources;
146 /* The final set of calls required. This consists of calls added
147 * explicitly with the require() method as well as all calls
148 * implicitly required by those through resource dependencies. */
149 std::set<unsigned> required;
151 bool transformFeedbackActive;
152 bool framebufferObjectActive;
155 /* Rendering often has no side effects, but it can in some cases,
156 * (such as when transform feedback is active, or when rendering
157 * targets a framebuffer object). */
158 bool renderingHasSideEffect() {
159 return transformFeedbackActive || framebufferObjectActive;
162 /* Provide: Record that the given call affects the given resource
163 * as a side effect. */
164 void provide(const char *resource, trace::CallNo call_no) {
165 resources[resource].insert(call_no);
168 /* Consume: Add all calls that provide the given resource to the
169 * required list, then clear the list for this resource. */
170 void consume(const char *resource) {
172 std::set<unsigned> *calls;
173 std::set<unsigned>::iterator call;
175 /* Insert as required all calls that provide 'resource',
176 * then clear these calls. */
177 if (resources.count(resource)) {
178 calls = &resources[resource];
179 for (call = calls->begin(); call != calls->end(); call++) {
180 required.insert(*call);
182 resources.erase(resource);
186 void stateTrackPreCall(trace::Call *call) {
188 if (strcmp(call->name(), "glBegin") == 0) {
189 insideBeginEnd = true;
193 if (strcmp(call->name(), "glBeginTransformFeedback") == 0) {
194 transformFeedbackActive = true;
198 if (strcmp(call->name(), "glBindFramebuffer") == 0) {
202 target = static_cast<GLenum>(call->arg(0).toSInt());
203 framebuffer = call->arg(1).toUInt();
205 if (target == GL_FRAMEBUFFER || target == GL_DRAW_FRAMEBUFFER) {
206 if (framebuffer == 0) {
207 framebufferObjectActive = false;
209 framebufferObjectActive = true;
216 void stateTrackPostCall(trace::Call *call) {
218 if (strcmp(call->name(), "glEnd") == 0) {
219 insideBeginEnd = false;
223 if (strcmp(call->name(), "glEndTransformFeedback") == 0) {
224 transformFeedbackActive = false;
228 if (call->flags & trace::CALL_FLAG_SWAP_RENDERTARGET &&
229 call->flags & trace::CALL_FLAG_END_FRAME) {
230 resources.erase("framebuffer");
235 void recordSideEffects(trace::Call *call) {
236 /* If call is flagged as no side effects, then we are done here. */
237 if (call->flags & trace::CALL_FLAG_NO_SIDE_EFFECTS) {
241 /* Similarly, swap-buffers calls don't have interesting side effects. */
242 if (call->flags & trace::CALL_FLAG_SWAP_RENDERTARGET &&
243 call->flags & trace::CALL_FLAG_END_FRAME) {
247 /* Handle all rendering operations, (even though only glEnd is
248 * flagged as a rendering operation we treat everything from
249 * glBegin through glEnd as a rendering operation). */
250 if (call->flags & trace::CALL_FLAG_RENDER ||
253 provide("framebuffer", call->no);
255 /* In some cases, rendering has side effects beyond the
256 * framebuffer update. */
257 if (renderingHasSideEffect()) {
258 provide("state", call->no);
264 /* By default, assume this call affects the state somehow. */
265 resources["state"].insert(call->no);
268 void requireDependencies(trace::Call *call) {
270 /* Swap-buffers calls depend on framebuffer state. */
271 if (call->flags & trace::CALL_FLAG_SWAP_RENDERTARGET &&
272 call->flags & trace::CALL_FLAG_END_FRAME) {
273 consume("framebuffer");
276 /* By default, just assume this call depends on generic state. */
282 TraceAnalyzer(): transformFeedbackActive(false),
283 framebufferObjectActive(false),
284 insideBeginEnd(false)
289 /* Analyze this call by tracking state and recording all the
290 * resources provided by this call as side effects.. */
291 void analyze(trace::Call *call) {
293 stateTrackPreCall(call);
295 recordSideEffects(call);
297 stateTrackPostCall(call);
300 /* Require this call and all of its dependencies to be included in
301 * the final trace. */
302 void require(trace::Call *call) {
304 /* First, find and insert all calls that this call depends on. */
305 requireDependencies(call);
307 /* Then insert this call itself. */
308 required.insert(call->no);
311 /* Return a set of all the required calls, (both those calls added
312 * explicitly with require() and those implicitly depended
314 std::set<unsigned> *get_required(void) {
319 struct trim_options {
320 /* Calls to be included in trace. */
321 trace::CallSet calls;
323 /* Whether dependency analysis should be performed. */
324 bool dependency_analysis;
326 /* Whether uninteresting calls should be pruned.. */
327 bool prune_uninteresting;
329 /* Output filename */
332 /* Emit only calls from this thread (-1 == all threads) */
337 trim_trace(const char *filename, struct trim_options *options)
339 trace::ParseBookmark beginning;
341 TraceAnalyzer analyzer;
342 std::set<unsigned> *required;
344 if (!p.open(filename)) {
345 std::cerr << "error: failed to open " << filename << "\n";
349 /* Mark the beginning so we can return here for pass 2. */
350 p.getBookmark(beginning);
352 /* In pass 1, analyze which calls are needed. */
354 while ((call = p.parse_call())) {
356 /* There's no use doing any work past the last call requested
358 if (call->no > options->calls.getLast()) {
363 /* If requested, ignore all calls not belonging to the specified thread. */
364 if (options->thread != -1 && call->thread_id != options->thread) {
369 /* Also, prune if uninteresting (unless the user asked for no pruning. */
370 if (options->prune_uninteresting && call->flags & trace::CALL_FLAG_UNINTERESTING) {
375 /* If this call is included in the user-specified call set,
376 * then require it (and all dependencies) in the trimmed
378 if (options->calls.contains(*call)) {
379 analyzer.require(call);
382 /* Regardless of whether we include this call or not, we do
383 * some dependency tracking (unless disabled by the user). We
384 * do this even for calls we have included in the output so
385 * that any state updates get performed. */
386 if (options->dependency_analysis) {
387 analyzer.analyze(call);
393 /* Prepare output file and writer for output. */
394 if (options->output.empty()) {
395 os::String base(filename);
396 base.trimExtension();
398 options->output = std::string(base.str()) + std::string("-trim.trace");
401 trace::Writer writer;
402 if (!writer.open(options->output.c_str())) {
403 std::cerr << "error: failed to create " << filename << "\n";
407 /* Reset bookmark for pass 2. */
408 p.setBookmark(beginning);
410 /* In pass 2, emit the calls that are required. */
411 required = analyzer.get_required();
413 while ((call = p.parse_call())) {
415 /* There's no use doing any work past the last call requested
417 if (call->no > options->calls.getLast())
420 if (required->find(call->no) != required->end()) {
421 writer.writeCall(call);
426 std::cout << "Trimmed trace is available as " << options->output << "\n";
432 command(int argc, char *argv[])
434 struct trim_options options;
436 options.calls = trace::CallSet(trace::FREQUENCY_ALL);
437 options.dependency_analysis = true;
438 options.prune_uninteresting = true;
443 while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
449 options.calls = trace::CallSet(optarg);
452 options.dependency_analysis = true;
455 options.dependency_analysis = false;
458 options.prune_uninteresting = true;
461 options.prune_uninteresting = false;
464 options.dependency_analysis = false;
465 options.prune_uninteresting = false;
468 options.thread = atoi(optarg);
471 options.output = optarg;
474 std::cerr << "error: unexpected option `" << opt << "`\n";
480 if (optind >= argc) {
481 std::cerr << "error: apitrace trim requires a trace file as an argument.\n";
486 if (argc > optind + 1) {
487 std::cerr << "error: extraneous arguments:";
488 for (int i = optind + 1; i < argc; i++) {
489 std::cerr << " " << argv[i];
496 return trim_trace(argv[optind], &options);
499 const Command trim_command = {