blob: 1c32bce0a6b6c41bc084cd204b6ada97e56bbcb4 [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 Worthcc6e51c2012-08-13 14:35:43 -070032#include <GL/gl.h>
33#include <GL/glext.h>
34
Carl Worth163b22c2012-08-10 10:15:30 -070035#include <set>
36
Carl Worth4c5f6fa2011-11-14 14:50:07 -080037#include "cli.hpp"
38
39#include "os_string.hpp"
40
José Fonsecad3c00132012-01-27 22:43:53 +000041#include "trace_callset.hpp"
Carl Worth4c5f6fa2011-11-14 14:50:07 -080042#include "trace_parser.hpp"
José Fonseca630471a2012-01-27 22:06:51 +000043#include "trace_writer.hpp"
Carl Worth4c5f6fa2011-11-14 14:50:07 -080044
Carl Worth33b92632012-08-14 14:11:19 -070045#define STRNCMP_LITERAL(var, literal) strncmp((var), (literal), sizeof (literal) -1)
46
Carl Worth4c5f6fa2011-11-14 14:50:07 -080047static const char *synopsis = "Create a new trace by trimming an existing trace.";
48
49static void
50usage(void)
51{
52 std::cout
José Fonsecab682b672012-02-15 07:13:31 +000053 << "usage: apitrace trim [OPTIONS] TRACE_FILE...\n"
José Fonsecad3c00132012-01-27 22:43:53 +000054 << synopsis << "\n"
55 "\n"
Carl Worth86464432012-08-11 11:46:54 -070056 " -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
69static void
70help()
71{
72 std::cout
73 << "usage: apitrace trim [OPTIONS] TRACE_FILE...\n"
74 << synopsis << "\n"
75 "\n"
Carl Worth163b22c2012-08-10 10:15:30 -070076 " -h, --help Show this help message and exit\n"
Carl Worth16b18db2012-08-11 11:33:12 -070077 "\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"
Carl Worth163b22c2012-08-10 10:15:30 -070085 " --deps Perform dependency analysis and include dependent\n"
Carl Worth16b18db2012-08-11 11:33:12 -070086 " 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"
Carl Worth163b22c2012-08-10 10:15:30 -0700106 " --thread=THREAD_ID Only retain calls from specified thread\n"
Carl Worth16b18db2012-08-11 11:33:12 -0700107 "\n"
Carl Worth163b22c2012-08-10 10:15:30 -0700108 " -o, --output=TRACE_FILE Output trace file\n"
José Fonsecad3c00132012-01-27 22:43:53 +0000109 "\n"
110 ;
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800111}
112
José Fonsecab682b672012-02-15 07:13:31 +0000113enum {
Imre Deak6f07a842012-05-08 15:20:43 +0300114 CALLS_OPT = CHAR_MAX + 1,
Carl Worth163b22c2012-08-10 10:15:30 -0700115 DEPS_OPT,
116 NO_DEPS_OPT,
Carl Worth16b18db2012-08-11 11:33:12 -0700117 PRUNE_OPT,
118 NO_PRUNE_OPT,
Imre Deak6f07a842012-05-08 15:20:43 +0300119 THREAD_OPT,
José Fonsecab682b672012-02-15 07:13:31 +0000120};
121
122const static char *
Carl Worth16b18db2012-08-11 11:33:12 -0700123shortOptions = "ho:x";
José Fonsecab682b672012-02-15 07:13:31 +0000124
125const static struct option
126longOptions[] = {
127 {"help", no_argument, 0, 'h'},
128 {"calls", required_argument, 0, CALLS_OPT},
Carl Worth163b22c2012-08-10 10:15:30 -0700129 {"deps", no_argument, 0, DEPS_OPT},
130 {"no-deps", no_argument, 0, NO_DEPS_OPT},
Carl Worth16b18db2012-08-11 11:33:12 -0700131 {"prune", no_argument, 0, PRUNE_OPT},
132 {"no-prune", no_argument, 0, NO_PRUNE_OPT},
133 {"exact", no_argument, 0, 'x'},
Imre Deak6f07a842012-05-08 15:20:43 +0300134 {"thread", required_argument, 0, THREAD_OPT},
135 {"output", required_argument, 0, 'o'},
José Fonsecab682b672012-02-15 07:13:31 +0000136 {0, 0, 0, 0}
137};
138
Carl Worth163b22c2012-08-10 10:15:30 -0700139struct stringCompare {
140 bool operator() (const char *a, const char *b) const {
141 return strcmp(a, b) < 0;
142 }
143};
144
145class TraceAnalyzer {
Carl Worth33b92632012-08-14 14:11:19 -0700146 /* 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;
Carl Worth163b22c2012-08-10 10:15:30 -0700152
153 /* The final set of calls required. This consists of calls added
154 * explicitly with the require() method as well as all calls
155 * implicitly required by those through resource dependencies. */
156 std::set<unsigned> required;
157
Carl Worthcc6e51c2012-08-13 14:35:43 -0700158 bool transformFeedbackActive;
159 bool framebufferObjectActive;
160 bool insideBeginEnd;
Carl Worth163b22c2012-08-10 10:15:30 -0700161
Carl Worthcc6e51c2012-08-13 14:35:43 -0700162 /* Rendering often has no side effects, but it can in some cases,
163 * (such as when transform feedback is active, or when rendering
164 * targets a framebuffer object). */
165 bool renderingHasSideEffect() {
166 return transformFeedbackActive || framebufferObjectActive;
167 }
168
169 /* Provide: Record that the given call affects the given resource
170 * as a side effect. */
Carl Worth33b92632012-08-14 14:11:19 -0700171 void provide(std::string resource, trace::CallNo call_no) {
Carl Worthcc6e51c2012-08-13 14:35:43 -0700172 resources[resource].insert(call_no);
173 }
174
Carl Worth33b92632012-08-14 14:11:19 -0700175 /* Link: Establish a dependency between resource 'resource' and
176 * resource 'dependency'. This dependency is captured by name so
177 * that if the list of calls that provide 'dependency' grows
178 * before 'resource' is consumed, those calls will still be
179 * captured. */
180 void link(std::string resource, std::string dependency) {
181 dependencies[resource].insert(dependency);
182 }
183
184 /* Unlink: Remove dependency from 'resource' on 'dependency'. */
185 void unlink(std::string resource, std::string dependency) {
186 dependencies[resource].erase(dependency);
187 if (dependencies[resource].size() == 0) {
188 dependencies.erase(resource);
189 }
190 }
191
192 /* Unlink all: Remove dependencies from 'resource' to all other
193 * resources. */
194 void unlinkAll(std::string resource) {
195 dependencies.erase(resource);
196 }
197
198 /* Resolve: Recursively compute all calls providing 'resource',
199 * (including linked dependencies of 'resource' on other
200 * resources). */
201 std::set<unsigned> resolve(std::string resource) {
202 std::set<std::string> *deps;
203 std::set<std::string>::iterator dep;
Carl Worthcc6e51c2012-08-13 14:35:43 -0700204
205 std::set<unsigned> *calls;
206 std::set<unsigned>::iterator call;
207
Carl Worth33b92632012-08-14 14:11:19 -0700208 std::set<unsigned> result, deps_set;
209
210 /* Recursively chase dependencies. */
211 if (dependencies.count(resource)) {
212 deps = &dependencies[resource];
213 for (dep = deps->begin(); dep != deps->end(); dep++) {
214 deps_set = resolve(*dep);
215 for (call = deps_set.begin(); call != deps_set.end(); call++) {
216 result.insert(*call);
217 }
218 }
219 }
220
221 /* Also look for calls that directly provide 'resource' */
Carl Worthcc6e51c2012-08-13 14:35:43 -0700222 if (resources.count(resource)) {
223 calls = &resources[resource];
224 for (call = calls->begin(); call != calls->end(); call++) {
Carl Worth33b92632012-08-14 14:11:19 -0700225 result.insert(*call);
Carl Worthcc6e51c2012-08-13 14:35:43 -0700226 }
Carl Worth33b92632012-08-14 14:11:19 -0700227 }
228
229 return result;
230 }
231
232 /* Consume: Resolve all calls that provide the given resource, and
233 * add them to the required list. Then clear the call list for
234 * 'resource' along with any dependencies. */
235 void consume(std::string resource) {
236
237 std::set<unsigned> calls;
238 std::set<unsigned>::iterator call;
239
240 calls = resolve(resource);
241
242 dependencies.erase(resource);
243 resources.erase(resource);
244
245 for (call = calls.begin(); call != calls.end(); call++) {
246 required.insert(*call);
Carl Worthcc6e51c2012-08-13 14:35:43 -0700247 }
248 }
249
250 void stateTrackPreCall(trace::Call *call) {
251
Carl Worth33b92632012-08-14 14:11:19 -0700252 const char *name = call->name();
253
254 if (strcmp(name, "glBegin") == 0) {
Carl Worthcc6e51c2012-08-13 14:35:43 -0700255 insideBeginEnd = true;
256 return;
257 }
258
Carl Worth33b92632012-08-14 14:11:19 -0700259 if (strcmp(name, "glBeginTransformFeedback") == 0) {
Carl Worthcc6e51c2012-08-13 14:35:43 -0700260 transformFeedbackActive = true;
261 return;
262 }
263
Carl Worth33b92632012-08-14 14:11:19 -0700264 if (strcmp(name, "glBindTexture") == 0) {
265 GLenum target;
266 GLuint texture;
267
268 target = static_cast<GLenum>(call->arg(0).toSInt());
269 texture = call->arg(1).toUInt();
270
271 if (texture == 0) {
272 texture_map.erase(target);
273 } else {
274 texture_map[target] = texture;
275 }
276
277 return;
278 }
279
280 if (strcmp(name, "glBindFramebuffer") == 0) {
Carl Worthcc6e51c2012-08-13 14:35:43 -0700281 GLenum target;
282 GLuint framebuffer;
283
284 target = static_cast<GLenum>(call->arg(0).toSInt());
285 framebuffer = call->arg(1).toUInt();
286
287 if (target == GL_FRAMEBUFFER || target == GL_DRAW_FRAMEBUFFER) {
288 if (framebuffer == 0) {
289 framebufferObjectActive = false;
290 } else {
291 framebufferObjectActive = true;
292 }
293 }
294 return;
295 }
296 }
297
298 void stateTrackPostCall(trace::Call *call) {
299
Carl Worth33b92632012-08-14 14:11:19 -0700300 const char *name = call->name();
301
302 if (strcmp(name, "glEnd") == 0) {
Carl Worthcc6e51c2012-08-13 14:35:43 -0700303 insideBeginEnd = false;
304 return;
305 }
306
Carl Worth33b92632012-08-14 14:11:19 -0700307 if (strcmp(name, "glEndTransformFeedback") == 0) {
Carl Worthcc6e51c2012-08-13 14:35:43 -0700308 transformFeedbackActive = false;
309 return;
310 }
311
Carl Worth33b92632012-08-14 14:11:19 -0700312 /* If this swapbuffers was included in the trace then it will
313 * have already consumed all framebuffer dependencies. If not,
314 * then clear them now so that they don't carry over into the
315 * next frame. */
Carl Worthcc6e51c2012-08-13 14:35:43 -0700316 if (call->flags & trace::CALL_FLAG_SWAP_RENDERTARGET &&
317 call->flags & trace::CALL_FLAG_END_FRAME) {
Carl Worth33b92632012-08-14 14:11:19 -0700318 dependencies.erase("framebuffer");
Carl Worthcc6e51c2012-08-13 14:35:43 -0700319 resources.erase("framebuffer");
320 return;
321 }
322 }
323
324 void recordSideEffects(trace::Call *call) {
Carl Worth33b92632012-08-14 14:11:19 -0700325
326 const char *name = call->name();
327
Carl Worthcc6e51c2012-08-13 14:35:43 -0700328 /* If call is flagged as no side effects, then we are done here. */
Carl Worth5b827e12012-08-12 20:41:50 -0700329 if (call->flags & trace::CALL_FLAG_NO_SIDE_EFFECTS) {
330 return;
331 }
332
Carl Worthcc6e51c2012-08-13 14:35:43 -0700333 /* Similarly, swap-buffers calls don't have interesting side effects. */
Carl Worth5b827e12012-08-12 20:41:50 -0700334 if (call->flags & trace::CALL_FLAG_SWAP_RENDERTARGET &&
335 call->flags & trace::CALL_FLAG_END_FRAME) {
336 return;
337 }
338
Carl Worth33b92632012-08-14 14:11:19 -0700339 if (strcmp(name, "glGenTextures") == 0) {
340 const trace::Array *textures = dynamic_cast<const trace::Array *>(&call->arg(1));
341 size_t i;
342 GLuint texture;
343
344 if (textures) {
345 for (i = 0; i < textures->size(); i++) {
346 std::stringstream ss;
347
348 texture = textures->values[i]->toUInt();
349 ss << "texture-" << texture;
350
351 provide(ss.str(), call->no);
352 }
353 }
354 return;
355 }
356
357 if (strcmp(name, "glBindTexture") == 0) {
358 GLenum target;
359 GLuint texture;
360
361 std::stringstream ss_target, ss_texture;
362
363 target = static_cast<GLenum>(call->arg(0).toSInt());
364 texture = call->arg(1).toUInt();
365
366 ss_target << "texture-target-" << target;
367 ss_texture << "texture-" << texture;
368
369 resources.erase(ss_target.str());
370 provide(ss_target.str(), call->no);
371
372 unlinkAll(ss_target.str());
373 link(ss_target.str(), ss_texture.str());
374
375 return;
376 }
377
378 /* FIXME: Need to handle glMultTexImage and friends. */
379 if (STRNCMP_LITERAL(name, "glTexImage") == 0 ||
380 STRNCMP_LITERAL(name, "glTexSubImage") == 0 ||
381 STRNCMP_LITERAL(name, "glCopyTexImage") == 0 ||
382 STRNCMP_LITERAL(name, "glCopyTexSubImage") == 0 ||
383 STRNCMP_LITERAL(name, "glCompressedTexImage") == 0 ||
384 STRNCMP_LITERAL(name, "glCompressedTexSubImage") == 0 ||
385 strcmp(name, "glInvalidateTexImage") == 0 ||
386 strcmp(name, "glInvalidateTexSubImage") == 0) {
387
388 std::set<unsigned> *calls;
389 std::set<unsigned>::iterator c;
390 std::stringstream ss_target, ss_texture;
391
392 GLenum target = static_cast<GLenum>(call->arg(0).toSInt());
393
394 ss_target << "texture-target-" << target;
395 ss_texture << "texture-" << texture_map[target];
396
397 /* The texture resource depends on this call and any calls
398 * providing the given texture target. */
399 provide(ss_texture.str(), call->no);
400
401 if (resources.count(ss_target.str())) {
402 calls = &resources[ss_target.str()];
403 for (c = calls->begin(); c != calls->end(); c++) {
404 provide(ss_texture.str(), *c);
405 }
406 }
407
408 return;
409 }
410
411 if (strcmp(name, "glEnable") == 0) {
412 GLenum cap;
413
414 cap = static_cast<GLenum>(call->arg(0).toSInt());
415
416 if (cap == GL_TEXTURE_1D ||
417 cap == GL_TEXTURE_2D ||
418 cap == GL_TEXTURE_3D ||
419 cap == GL_TEXTURE_CUBE_MAP)
420 {
421 std::stringstream ss;
422
423 ss << "texture-target-" << cap;
424
425 link("render-state", ss.str());
426 }
427
428 provide("state", call->no);
429
430 return;
431 }
432
433 if (strcmp(name, "glDisable") == 0) {
434 GLenum cap;
435
436 cap = static_cast<GLenum>(call->arg(0).toSInt());
437
438 if (cap == GL_TEXTURE_1D ||
439 cap == GL_TEXTURE_2D ||
440 cap == GL_TEXTURE_3D ||
441 cap == GL_TEXTURE_CUBE_MAP)
442 {
443 std::stringstream ss;
444
445 ss << "texture-target-" << cap;
446
447 unlink("render-state", ss.str());
448 }
449
450 provide("state", call->no);
451
452 return;
453 }
454
Carl Worthcc6e51c2012-08-13 14:35:43 -0700455 /* Handle all rendering operations, (even though only glEnd is
456 * flagged as a rendering operation we treat everything from
457 * glBegin through glEnd as a rendering operation). */
458 if (call->flags & trace::CALL_FLAG_RENDER ||
459 insideBeginEnd) {
460
Carl Worth33b92632012-08-14 14:11:19 -0700461 std::set<unsigned> calls;
462 std::set<unsigned>::iterator c;
463
Carl Worthcc6e51c2012-08-13 14:35:43 -0700464 provide("framebuffer", call->no);
465
Carl Worth33b92632012-08-14 14:11:19 -0700466 calls = resolve("render-state");
467
468 for (c = calls.begin(); c != calls.end(); c++) {
469 provide("framebuffer", *c);
470 }
471
Carl Worthcc6e51c2012-08-13 14:35:43 -0700472 /* In some cases, rendering has side effects beyond the
473 * framebuffer update. */
474 if (renderingHasSideEffect()) {
475 provide("state", call->no);
Carl Worth33b92632012-08-14 14:11:19 -0700476 for (c = calls.begin(); c != calls.end(); c++) {
477 provide("state", *c);
478 }
Carl Worthcc6e51c2012-08-13 14:35:43 -0700479 }
480
481 return;
482 }
483
Carl Worth5b827e12012-08-12 20:41:50 -0700484 /* By default, assume this call affects the state somehow. */
Carl Worth163b22c2012-08-10 10:15:30 -0700485 resources["state"].insert(call->no);
486 }
487
Carl Worthcc6e51c2012-08-13 14:35:43 -0700488 void requireDependencies(trace::Call *call) {
489
490 /* Swap-buffers calls depend on framebuffer state. */
491 if (call->flags & trace::CALL_FLAG_SWAP_RENDERTARGET &&
492 call->flags & trace::CALL_FLAG_END_FRAME) {
493 consume("framebuffer");
494 }
495
496 /* By default, just assume this call depends on generic state. */
497 consume("state");
498 }
499
500
501public:
502 TraceAnalyzer(): transformFeedbackActive(false),
503 framebufferObjectActive(false),
504 insideBeginEnd(false)
505 {}
506
507 ~TraceAnalyzer() {}
508
509 /* Analyze this call by tracking state and recording all the
510 * resources provided by this call as side effects.. */
511 void analyze(trace::Call *call) {
512
513 stateTrackPreCall(call);
514
515 recordSideEffects(call);
516
517 stateTrackPostCall(call);
518 }
519
Carl Worth163b22c2012-08-10 10:15:30 -0700520 /* Require this call and all of its dependencies to be included in
521 * the final trace. */
522 void require(trace::Call *call) {
Carl Worth163b22c2012-08-10 10:15:30 -0700523
524 /* First, find and insert all calls that this call depends on. */
Carl Worthcc6e51c2012-08-13 14:35:43 -0700525 requireDependencies(call);
Carl Worth163b22c2012-08-10 10:15:30 -0700526
527 /* Then insert this call itself. */
528 required.insert(call->no);
529 }
530
531 /* Return a set of all the required calls, (both those calls added
532 * explicitly with require() and those implicitly depended
533 * upon. */
534 std::set<unsigned> *get_required(void) {
535 return &required;
536 }
537};
538
539struct trim_options {
540 /* Calls to be included in trace. */
541 trace::CallSet calls;
542
543 /* Whether dependency analysis should be performed. */
544 bool dependency_analysis;
545
Carl Worth16b18db2012-08-11 11:33:12 -0700546 /* Whether uninteresting calls should be pruned.. */
547 bool prune_uninteresting;
548
Carl Worth163b22c2012-08-10 10:15:30 -0700549 /* Output filename */
550 std::string output;
551
552 /* Emit only calls from this thread (-1 == all threads) */
553 int thread;
554};
555
556static int
557trim_trace(const char *filename, struct trim_options *options)
558{
559 trace::ParseBookmark beginning;
560 trace::Parser p;
561 TraceAnalyzer analyzer;
562 std::set<unsigned> *required;
563
564 if (!p.open(filename)) {
565 std::cerr << "error: failed to open " << filename << "\n";
566 return 1;
567 }
568
569 /* Mark the beginning so we can return here for pass 2. */
570 p.getBookmark(beginning);
571
572 /* In pass 1, analyze which calls are needed. */
573 trace::Call *call;
574 while ((call = p.parse_call())) {
Carl Worth5b827e12012-08-12 20:41:50 -0700575
576 /* There's no use doing any work past the last call requested
577 * by the user. */
Carl Worth42249012012-08-14 10:26:11 -0700578 if (call->no > options->calls.getLast()) {
579 delete call;
Carl Worth5b827e12012-08-12 20:41:50 -0700580 break;
Carl Worth42249012012-08-14 10:26:11 -0700581 }
Carl Worth5b827e12012-08-12 20:41:50 -0700582
Carl Worth163b22c2012-08-10 10:15:30 -0700583 /* If requested, ignore all calls not belonging to the specified thread. */
Carl Worth42249012012-08-14 10:26:11 -0700584 if (options->thread != -1 && call->thread_id != options->thread) {
585 delete call;
Carl Worth163b22c2012-08-10 10:15:30 -0700586 continue;
Carl Worth42249012012-08-14 10:26:11 -0700587 }
Carl Worth163b22c2012-08-10 10:15:30 -0700588
Carl Worth16b18db2012-08-11 11:33:12 -0700589 /* Also, prune if uninteresting (unless the user asked for no pruning. */
590 if (options->prune_uninteresting && call->flags & trace::CALL_FLAG_UNINTERESTING) {
Carl Worth42249012012-08-14 10:26:11 -0700591 delete call;
Carl Worth16b18db2012-08-11 11:33:12 -0700592 continue;
593 }
594
Carl Worthcc6e51c2012-08-13 14:35:43 -0700595 /* If this call is included in the user-specified call set,
596 * then require it (and all dependencies) in the trimmed
597 * output. */
Carl Worth163b22c2012-08-10 10:15:30 -0700598 if (options->calls.contains(*call)) {
599 analyzer.require(call);
Carl Worthcc6e51c2012-08-13 14:35:43 -0700600 }
601
602 /* Regardless of whether we include this call or not, we do
603 * some dependency tracking (unless disabled by the user). We
604 * do this even for calls we have included in the output so
605 * that any state updates get performed. */
606 if (options->dependency_analysis) {
607 analyzer.analyze(call);
Carl Worth163b22c2012-08-10 10:15:30 -0700608 }
Carl Worth42249012012-08-14 10:26:11 -0700609
610 delete call;
Carl Worth163b22c2012-08-10 10:15:30 -0700611 }
612
613 /* Prepare output file and writer for output. */
614 if (options->output.empty()) {
615 os::String base(filename);
616 base.trimExtension();
617
618 options->output = std::string(base.str()) + std::string("-trim.trace");
619 }
620
621 trace::Writer writer;
622 if (!writer.open(options->output.c_str())) {
623 std::cerr << "error: failed to create " << filename << "\n";
624 return 1;
625 }
626
627 /* Reset bookmark for pass 2. */
628 p.setBookmark(beginning);
629
630 /* In pass 2, emit the calls that are required. */
631 required = analyzer.get_required();
632
633 while ((call = p.parse_call())) {
Carl Worth5b827e12012-08-12 20:41:50 -0700634
635 /* There's no use doing any work past the last call requested
636 * by the user. */
637 if (call->no > options->calls.getLast())
638 break;
639
Carl Worth163b22c2012-08-10 10:15:30 -0700640 if (required->find(call->no) != required->end()) {
641 writer.writeCall(call);
642 }
643 delete call;
644 }
645
646 std::cout << "Trimmed trace is available as " << options->output << "\n";
647
648 return 0;
649}
650
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800651static int
652command(int argc, char *argv[])
653{
Carl Worth163b22c2012-08-10 10:15:30 -0700654 struct trim_options options;
655
656 options.calls = trace::CallSet(trace::FREQUENCY_ALL);
657 options.dependency_analysis = true;
Carl Worth16b18db2012-08-11 11:33:12 -0700658 options.prune_uninteresting = true;
Carl Worth163b22c2012-08-10 10:15:30 -0700659 options.output = "";
660 options.thread = -1;
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800661
José Fonsecab682b672012-02-15 07:13:31 +0000662 int opt;
663 while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
664 switch (opt) {
665 case 'h':
Carl Worth86464432012-08-11 11:46:54 -0700666 help();
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800667 return 0;
José Fonsecab682b672012-02-15 07:13:31 +0000668 case CALLS_OPT:
Carl Worth163b22c2012-08-10 10:15:30 -0700669 options.calls = trace::CallSet(optarg);
670 break;
671 case DEPS_OPT:
672 options.dependency_analysis = true;
673 break;
674 case NO_DEPS_OPT:
675 options.dependency_analysis = false;
José Fonsecab682b672012-02-15 07:13:31 +0000676 break;
Carl Worth16b18db2012-08-11 11:33:12 -0700677 case PRUNE_OPT:
678 options.prune_uninteresting = true;
679 break;
680 case NO_PRUNE_OPT:
681 options.prune_uninteresting = false;
682 break;
683 case 'x':
684 options.dependency_analysis = false;
685 options.prune_uninteresting = false;
686 break;
Imre Deak6f07a842012-05-08 15:20:43 +0300687 case THREAD_OPT:
Carl Worth163b22c2012-08-10 10:15:30 -0700688 options.thread = atoi(optarg);
Imre Deak6f07a842012-05-08 15:20:43 +0300689 break;
José Fonsecab682b672012-02-15 07:13:31 +0000690 case 'o':
Carl Worth163b22c2012-08-10 10:15:30 -0700691 options.output = optarg;
José Fonsecab682b672012-02-15 07:13:31 +0000692 break;
693 default:
694 std::cerr << "error: unexpected option `" << opt << "`\n";
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800695 usage();
696 return 1;
697 }
698 }
699
José Fonsecab682b672012-02-15 07:13:31 +0000700 if (optind >= argc) {
701 std::cerr << "error: apitrace trim requires a trace file as an argument.\n";
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800702 usage();
703 return 1;
704 }
705
Carl Worthf630d9d2012-09-04 16:48:00 -0700706 if (argc > optind + 1) {
707 std::cerr << "error: extraneous arguments:";
708 for (int i = optind + 1; i < argc; i++) {
709 std::cerr << " " << argv[i];
710 }
711 std::cerr << "\n";
712 usage();
713 return 1;
714 }
715
Carl Worth163b22c2012-08-10 10:15:30 -0700716 return trim_trace(argv[optind], &options);
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800717}
718
719const Command trim_command = {
720 "trim",
721 synopsis,
Carl Worth86464432012-08-11 11:46:54 -0700722 help,
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800723 command
724};