blob: 8f0996028ced0e97e8d0a0da9d39a26ef72e22e6 [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"
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"
Carl Worth46abfd12012-08-14 22:32:29 -070059 " -x, --exact Trim exactly to calls specified in --calls/--frames\n"
Carl Worth86464432012-08-11 11:46:54 -070060 " Equivalent to both --no-deps and --no-prune\n"
Carl Worthab82b3b2012-08-15 16:35:38 -070061 " --print-callset Print the final set of calls included in output\n"
Carl Worth86464432012-08-11 11:46:54 -070062 " --thread=THREAD_ID Only retain calls from specified thread\n"
63 " -o, --output=TRACE_FILE Output trace file\n"
64 ;
65}
66
67static void
68help()
69{
70 std::cout
71 << "usage: apitrace trim [OPTIONS] TRACE_FILE...\n"
72 << synopsis << "\n"
73 "\n"
Carl Worth163b22c2012-08-10 10:15:30 -070074 " -h, --help Show this help message and exit\n"
Carl Worth16b18db2012-08-11 11:33:12 -070075 "\n"
76 " --calls=CALLSET Include specified calls in the trimmed output.\n"
Carl Worth46abfd12012-08-14 22:32:29 -070077 " --frames=FRAMESET Include specified frames in the trimmed output.\n"
Carl Worth16b18db2012-08-11 11:33:12 -070078 " Note that due to dependency analysis and pruning\n"
79 " of uninteresting calls the resulting trace may\n"
80 " include more and less calls than specified.\n"
81 " See --no-deps, --no-prune, and --exact to change\n"
82 " this behavior.\n"
83 "\n"
Carl Worth163b22c2012-08-10 10:15:30 -070084 " --deps Perform dependency analysis and include dependent\n"
Carl Worth16b18db2012-08-11 11:33:12 -070085 " calls as needed, (even if those calls were not\n"
Carl Worth46abfd12012-08-14 22:32:29 -070086 " explicitly requested with --calls or --frames).\n"
87 " This is the default behavior. See --no-deps and\n"
88 " --exact to change the behavior.\n"
Carl Worth16b18db2012-08-11 11:33:12 -070089 "\n"
90 " --no-deps Do not perform dependency analysis. In this mode\n"
91 " the trimmed trace will never include calls from\n"
Carl Worth46abfd12012-08-14 22:32:29 -070092 " outside what is specified in --calls or --frames.\n"
Carl Worth16b18db2012-08-11 11:33:12 -070093 "\n"
Carl Worth46abfd12012-08-14 22:32:29 -070094 " --prune Omit calls with no side effects, even if the call\n"
95 " is within the range specified by --calls/--frames.\n"
96 " This is the default behavior. See --no-prune.\n"
Carl Worth16b18db2012-08-11 11:33:12 -070097 "\n"
98 " --no-prune Do not prune uninteresting calls from the trace.\n"
99 " In this mode the trimmed trace will never omit\n"
Carl Worth46abfd12012-08-14 22:32:29 -0700100 " any calls within the user-specified range.\n"
Carl Worth16b18db2012-08-11 11:33:12 -0700101 "\n"
102 " -x, --exact Trim the trace to exactly the calls specified in\n"
Carl Worth46abfd12012-08-14 22:32:29 -0700103 " --calls and --frames. This option is equivalent\n"
104 " to passing both --no-deps and --no-prune.\n"
Carl Worth16b18db2012-08-11 11:33:12 -0700105 "\n"
Carl Worthab82b3b2012-08-15 16:35:38 -0700106 " --print-callset Print to stdout the final set of calls included\n"
107 " in the trim output. This can be useful for\n"
108 " debugging trim operations by using a modified\n"
109 " callset on the command-line along with --exact.\n"
110 " Use --calls=@<file> to read callset from a file.\n"
111 "\n"
Carl Worth163b22c2012-08-10 10:15:30 -0700112 " --thread=THREAD_ID Only retain calls from specified thread\n"
Carl Worth16b18db2012-08-11 11:33:12 -0700113 "\n"
Carl Worth163b22c2012-08-10 10:15:30 -0700114 " -o, --output=TRACE_FILE Output trace file\n"
José Fonsecad3c00132012-01-27 22:43:53 +0000115 "\n"
116 ;
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800117}
118
José Fonsecab682b672012-02-15 07:13:31 +0000119enum {
Imre Deak6f07a842012-05-08 15:20:43 +0300120 CALLS_OPT = CHAR_MAX + 1,
Carl Worth46abfd12012-08-14 22:32:29 -0700121 FRAMES_OPT,
Carl Worth163b22c2012-08-10 10:15:30 -0700122 DEPS_OPT,
123 NO_DEPS_OPT,
Carl Worth16b18db2012-08-11 11:33:12 -0700124 PRUNE_OPT,
125 NO_PRUNE_OPT,
Imre Deak6f07a842012-05-08 15:20:43 +0300126 THREAD_OPT,
Carl Worthab82b3b2012-08-15 16:35:38 -0700127 PRINT_CALLSET_OPT,
José Fonsecab682b672012-02-15 07:13:31 +0000128};
129
130const static char *
Carl Worth16b18db2012-08-11 11:33:12 -0700131shortOptions = "ho:x";
José Fonsecab682b672012-02-15 07:13:31 +0000132
133const static struct option
134longOptions[] = {
135 {"help", no_argument, 0, 'h'},
136 {"calls", required_argument, 0, CALLS_OPT},
Carl Worth46abfd12012-08-14 22:32:29 -0700137 {"frames", required_argument, 0, FRAMES_OPT},
Carl Worth163b22c2012-08-10 10:15:30 -0700138 {"deps", no_argument, 0, DEPS_OPT},
139 {"no-deps", no_argument, 0, NO_DEPS_OPT},
Carl Worth16b18db2012-08-11 11:33:12 -0700140 {"prune", no_argument, 0, PRUNE_OPT},
141 {"no-prune", no_argument, 0, NO_PRUNE_OPT},
142 {"exact", no_argument, 0, 'x'},
Imre Deak6f07a842012-05-08 15:20:43 +0300143 {"thread", required_argument, 0, THREAD_OPT},
144 {"output", required_argument, 0, 'o'},
Carl Worthab82b3b2012-08-15 16:35:38 -0700145 {"print-callset", no_argument, 0, PRINT_CALLSET_OPT},
José Fonsecab682b672012-02-15 07:13:31 +0000146 {0, 0, 0, 0}
147};
148
Carl Worth163b22c2012-08-10 10:15:30 -0700149struct stringCompare {
150 bool operator() (const char *a, const char *b) const {
151 return strcmp(a, b) < 0;
152 }
153};
154
Carl Worth163b22c2012-08-10 10:15:30 -0700155struct trim_options {
156 /* Calls to be included in trace. */
157 trace::CallSet calls;
158
Carl Worth46abfd12012-08-14 22:32:29 -0700159 /* Frames to be included in trace. */
160 trace::CallSet frames;
161
Carl Worth163b22c2012-08-10 10:15:30 -0700162 /* Whether dependency analysis should be performed. */
163 bool dependency_analysis;
164
Carl Worth16b18db2012-08-11 11:33:12 -0700165 /* Whether uninteresting calls should be pruned.. */
166 bool prune_uninteresting;
167
Carl Worth163b22c2012-08-10 10:15:30 -0700168 /* Output filename */
169 std::string output;
170
171 /* Emit only calls from this thread (-1 == all threads) */
172 int thread;
Carl Worthab82b3b2012-08-15 16:35:38 -0700173
174 /* Print resulting callset */
175 int print_callset;
Carl Worth163b22c2012-08-10 10:15:30 -0700176};
177
178static int
179trim_trace(const char *filename, struct trim_options *options)
180{
181 trace::ParseBookmark beginning;
182 trace::Parser p;
183 TraceAnalyzer analyzer;
184 std::set<unsigned> *required;
Carl Worth46abfd12012-08-14 22:32:29 -0700185 unsigned frame;
Carl Worthab82b3b2012-08-15 16:35:38 -0700186 int call_range_first, call_range_last;
Carl Worth163b22c2012-08-10 10:15:30 -0700187
188 if (!p.open(filename)) {
189 std::cerr << "error: failed to open " << filename << "\n";
190 return 1;
191 }
192
193 /* Mark the beginning so we can return here for pass 2. */
194 p.getBookmark(beginning);
195
196 /* In pass 1, analyze which calls are needed. */
Carl Worth46abfd12012-08-14 22:32:29 -0700197 frame = 0;
Carl Worth163b22c2012-08-10 10:15:30 -0700198 trace::Call *call;
199 while ((call = p.parse_call())) {
Carl Worth5b827e12012-08-12 20:41:50 -0700200
Carl Worth46abfd12012-08-14 22:32:29 -0700201 /* There's no use doing any work past the last call or frame
202 * requested by the user. */
203 if (call->no > options->calls.getLast() ||
204 frame > options->frames.getLast()) {
205
Carl Worth42249012012-08-14 10:26:11 -0700206 delete call;
Carl Worth5b827e12012-08-12 20:41:50 -0700207 break;
Carl Worth42249012012-08-14 10:26:11 -0700208 }
Carl Worth5b827e12012-08-12 20:41:50 -0700209
Carl Worth163b22c2012-08-10 10:15:30 -0700210 /* If requested, ignore all calls not belonging to the specified thread. */
Carl Worth42249012012-08-14 10:26:11 -0700211 if (options->thread != -1 && call->thread_id != options->thread) {
Carl Worth46abfd12012-08-14 22:32:29 -0700212 goto NEXT;
Carl Worth42249012012-08-14 10:26:11 -0700213 }
Carl Worth163b22c2012-08-10 10:15:30 -0700214
Carl Worth16b18db2012-08-11 11:33:12 -0700215 /* Also, prune if uninteresting (unless the user asked for no pruning. */
216 if (options->prune_uninteresting && call->flags & trace::CALL_FLAG_UNINTERESTING) {
Carl Worth46abfd12012-08-14 22:32:29 -0700217 goto NEXT;
Carl Worth16b18db2012-08-11 11:33:12 -0700218 }
219
Carl Worthcc6e51c2012-08-13 14:35:43 -0700220 /* If this call is included in the user-specified call set,
221 * then require it (and all dependencies) in the trimmed
222 * output. */
Carl Worth46abfd12012-08-14 22:32:29 -0700223 if (options->calls.contains(*call) ||
224 options->frames.contains(frame, call->flags)) {
225
Carl Worth163b22c2012-08-10 10:15:30 -0700226 analyzer.require(call);
Carl Worthcc6e51c2012-08-13 14:35:43 -0700227 }
228
229 /* Regardless of whether we include this call or not, we do
230 * some dependency tracking (unless disabled by the user). We
231 * do this even for calls we have included in the output so
232 * that any state updates get performed. */
233 if (options->dependency_analysis) {
234 analyzer.analyze(call);
Carl Worth163b22c2012-08-10 10:15:30 -0700235 }
Carl Worth42249012012-08-14 10:26:11 -0700236
Carl Worth46abfd12012-08-14 22:32:29 -0700237 NEXT:
238 if (call->flags & trace::CALL_FLAG_END_FRAME)
239 frame++;
240
Carl Worth42249012012-08-14 10:26:11 -0700241 delete call;
Carl Worth163b22c2012-08-10 10:15:30 -0700242 }
243
244 /* Prepare output file and writer for output. */
245 if (options->output.empty()) {
246 os::String base(filename);
247 base.trimExtension();
248
249 options->output = std::string(base.str()) + std::string("-trim.trace");
250 }
251
252 trace::Writer writer;
253 if (!writer.open(options->output.c_str())) {
254 std::cerr << "error: failed to create " << filename << "\n";
255 return 1;
256 }
257
258 /* Reset bookmark for pass 2. */
259 p.setBookmark(beginning);
260
261 /* In pass 2, emit the calls that are required. */
262 required = analyzer.get_required();
263
Carl Worth46abfd12012-08-14 22:32:29 -0700264 frame = 0;
Carl Worthab82b3b2012-08-15 16:35:38 -0700265 call_range_first = -1;
266 call_range_last = -1;
Carl Worth163b22c2012-08-10 10:15:30 -0700267 while ((call = p.parse_call())) {
Carl Worth5b827e12012-08-12 20:41:50 -0700268
Carl Worth46abfd12012-08-14 22:32:29 -0700269 /* There's no use doing any work past the last call or frame
270 * requested by the user. */
271 if (call->no > options->calls.getLast() ||
272 frame > options->frames.getLast()) {
273
Carl Worth5b827e12012-08-12 20:41:50 -0700274 break;
Carl Worth46abfd12012-08-14 22:32:29 -0700275 }
Carl Worth5b827e12012-08-12 20:41:50 -0700276
Carl Worth163b22c2012-08-10 10:15:30 -0700277 if (required->find(call->no) != required->end()) {
278 writer.writeCall(call);
Carl Worthab82b3b2012-08-15 16:35:38 -0700279
280 if (options->print_callset) {
281 if (call_range_first < 0) {
282 call_range_first = call->no;
283 printf ("%d", call_range_first);
284 } else if (call->no != call_range_last + 1) {
285 if (call_range_last != call_range_first)
286 printf ("-%d", call_range_last);
287 call_range_first = call->no;
288 printf (",%d", call_range_first);
289 }
290 call_range_last = call->no;
291 }
Carl Worth163b22c2012-08-10 10:15:30 -0700292 }
Carl Worth46abfd12012-08-14 22:32:29 -0700293
294 if (call->flags & trace::CALL_FLAG_END_FRAME) {
295 frame++;
296 }
297
Carl Worth163b22c2012-08-10 10:15:30 -0700298 delete call;
299 }
300
Carl Worthab82b3b2012-08-15 16:35:38 -0700301 if (options->print_callset) {
302 if (call_range_last != call_range_first)
303 printf ("-%d\n", call_range_last);
304 }
305
Carl Worth1a800a12012-08-17 14:23:13 -0700306 std::cerr << "Trimmed trace is available as " << options->output << "\n";
Carl Worth163b22c2012-08-10 10:15:30 -0700307
308 return 0;
309}
310
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800311static int
312command(int argc, char *argv[])
313{
Carl Worth163b22c2012-08-10 10:15:30 -0700314 struct trim_options options;
315
Carl Worth46abfd12012-08-14 22:32:29 -0700316 options.calls = trace::CallSet(trace::FREQUENCY_NONE);
317 options.frames = trace::CallSet(trace::FREQUENCY_NONE);
Carl Worth163b22c2012-08-10 10:15:30 -0700318 options.dependency_analysis = true;
Carl Worth16b18db2012-08-11 11:33:12 -0700319 options.prune_uninteresting = true;
Carl Worth163b22c2012-08-10 10:15:30 -0700320 options.output = "";
321 options.thread = -1;
Carl Worthab82b3b2012-08-15 16:35:38 -0700322 options.print_callset = 0;
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800323
José Fonsecab682b672012-02-15 07:13:31 +0000324 int opt;
325 while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
326 switch (opt) {
327 case 'h':
Carl Worth86464432012-08-11 11:46:54 -0700328 help();
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800329 return 0;
José Fonsecab682b672012-02-15 07:13:31 +0000330 case CALLS_OPT:
Carl Worth163b22c2012-08-10 10:15:30 -0700331 options.calls = trace::CallSet(optarg);
332 break;
Carl Worth46abfd12012-08-14 22:32:29 -0700333 case FRAMES_OPT:
334 options.frames = trace::CallSet(optarg);
335 break;
Carl Worth163b22c2012-08-10 10:15:30 -0700336 case DEPS_OPT:
337 options.dependency_analysis = true;
338 break;
339 case NO_DEPS_OPT:
340 options.dependency_analysis = false;
José Fonsecab682b672012-02-15 07:13:31 +0000341 break;
Carl Worth16b18db2012-08-11 11:33:12 -0700342 case PRUNE_OPT:
343 options.prune_uninteresting = true;
344 break;
345 case NO_PRUNE_OPT:
346 options.prune_uninteresting = false;
347 break;
348 case 'x':
349 options.dependency_analysis = false;
350 options.prune_uninteresting = false;
351 break;
Imre Deak6f07a842012-05-08 15:20:43 +0300352 case THREAD_OPT:
Carl Worth163b22c2012-08-10 10:15:30 -0700353 options.thread = atoi(optarg);
Imre Deak6f07a842012-05-08 15:20:43 +0300354 break;
José Fonsecab682b672012-02-15 07:13:31 +0000355 case 'o':
Carl Worth163b22c2012-08-10 10:15:30 -0700356 options.output = optarg;
José Fonsecab682b672012-02-15 07:13:31 +0000357 break;
Carl Worthab82b3b2012-08-15 16:35:38 -0700358 case PRINT_CALLSET_OPT:
359 options.print_callset = 1;
360 break;
José Fonsecab682b672012-02-15 07:13:31 +0000361 default:
362 std::cerr << "error: unexpected option `" << opt << "`\n";
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800363 usage();
364 return 1;
365 }
366 }
367
Carl Worth46abfd12012-08-14 22:32:29 -0700368 /* If neither of --calls nor --frames was set, default to the
369 * entire set of calls. */
370 if (options.calls.empty() && options.frames.empty()) {
371 options.calls = trace::CallSet(trace::FREQUENCY_ALL);
372 }
373
José Fonsecab682b672012-02-15 07:13:31 +0000374 if (optind >= argc) {
375 std::cerr << "error: apitrace trim requires a trace file as an argument.\n";
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800376 usage();
377 return 1;
378 }
379
Carl Worthf630d9d2012-09-04 16:48:00 -0700380 if (argc > optind + 1) {
381 std::cerr << "error: extraneous arguments:";
382 for (int i = optind + 1; i < argc; i++) {
383 std::cerr << " " << argv[i];
384 }
385 std::cerr << "\n";
386 usage();
387 return 1;
388 }
389
Carl Worth83ec3f62012-08-17 14:22:30 -0700390 if (options.dependency_analysis) {
391 std::cerr <<
392 "Note: The dependency analysis in \"apitrace trim\" is still experimental.\n"
393 " We hope that it will be useful, but it may lead to incorrect results.\n"
394 " If you find a trace that misbehaves while trimming, please share that\n"
395 " by sending email to apitrace@lists.freedesktop.org, cworth@cworth.org\n";
396 }
397
Carl Worth163b22c2012-08-10 10:15:30 -0700398 return trim_trace(argv[optind], &options);
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800399}
400
401const Command trim_command = {
402 "trim",
403 synopsis,
Carl Worth86464432012-08-11 11:46:54 -0700404 help,
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800405 command
406};