blob: 80a1b640a9890409a1d11a65dd54bc37375d205e [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
27#include <string.h>
José Fonsecab682b672012-02-15 07:13:31 +000028#include <limits.h> // for CHAR_MAX
29#include <getopt.h>
Carl Worth4c5f6fa2011-11-14 14:50:07 -080030
Carl Worth163b22c2012-08-10 10:15:30 -070031#include <set>
32
Carl Worth4c5f6fa2011-11-14 14:50:07 -080033#include "cli.hpp"
34
35#include "os_string.hpp"
36
José Fonsecad3c00132012-01-27 22:43:53 +000037#include "trace_callset.hpp"
Carl Worth4c5f6fa2011-11-14 14:50:07 -080038#include "trace_parser.hpp"
José Fonseca630471a2012-01-27 22:06:51 +000039#include "trace_writer.hpp"
Carl Worth4c5f6fa2011-11-14 14:50:07 -080040
41static const char *synopsis = "Create a new trace by trimming an existing trace.";
42
43static void
44usage(void)
45{
46 std::cout
José Fonsecab682b672012-02-15 07:13:31 +000047 << "usage: apitrace trim [OPTIONS] TRACE_FILE...\n"
José Fonsecad3c00132012-01-27 22:43:53 +000048 << synopsis << "\n"
49 "\n"
Carl Worth163b22c2012-08-10 10:15:30 -070050 " -h, --help Show this help message and exit\n"
Carl Worth16b18db2012-08-11 11:33:12 -070051 "\n"
52 " --calls=CALLSET Include specified calls in the trimmed output.\n"
53 " Note that due to dependency analysis and pruning\n"
54 " of uninteresting calls the resulting trace may\n"
55 " include more and less calls than specified.\n"
56 " See --no-deps, --no-prune, and --exact to change\n"
57 " this behavior.\n"
58 "\n"
Carl Worth163b22c2012-08-10 10:15:30 -070059 " --deps Perform dependency analysis and include dependent\n"
Carl Worth16b18db2012-08-11 11:33:12 -070060 " calls as needed, (even if those calls were not\n"
61 " explicitly requested with --calls). This is the\n"
62 " default behavior. See --no-deps and --exact.\n"
63 "\n"
64 " --no-deps Do not perform dependency analysis. In this mode\n"
65 " the trimmed trace will never include calls from\n"
66 " outside the range specified in --calls.\n"
67 "\n"
68 " --prune Omit calls that have no side effects, even if the\n"
69 " call is within the range specified by --calls.\n"
70 " This is the default behavior. See --no-prune\n"
71 "\n"
72 " --no-prune Do not prune uninteresting calls from the trace.\n"
73 " In this mode the trimmed trace will never omit\n"
74 " any calls within the range specified in --calls.\n"
75 "\n"
76 " -x, --exact Trim the trace to exactly the calls specified in\n"
77 " --calls. This option is equivalent to passing\n"
78 " both --no-deps and --no-prune.\n"
79 "\n"
Carl Worth163b22c2012-08-10 10:15:30 -070080 " --thread=THREAD_ID Only retain calls from specified thread\n"
Carl Worth16b18db2012-08-11 11:33:12 -070081 "\n"
Carl Worth163b22c2012-08-10 10:15:30 -070082 " -o, --output=TRACE_FILE Output trace file\n"
José Fonsecad3c00132012-01-27 22:43:53 +000083 "\n"
84 ;
Carl Worth4c5f6fa2011-11-14 14:50:07 -080085}
86
José Fonsecab682b672012-02-15 07:13:31 +000087enum {
Imre Deak6f07a842012-05-08 15:20:43 +030088 CALLS_OPT = CHAR_MAX + 1,
Carl Worth163b22c2012-08-10 10:15:30 -070089 DEPS_OPT,
90 NO_DEPS_OPT,
Carl Worth16b18db2012-08-11 11:33:12 -070091 PRUNE_OPT,
92 NO_PRUNE_OPT,
Imre Deak6f07a842012-05-08 15:20:43 +030093 THREAD_OPT,
José Fonsecab682b672012-02-15 07:13:31 +000094};
95
96const static char *
Carl Worth16b18db2012-08-11 11:33:12 -070097shortOptions = "ho:x";
José Fonsecab682b672012-02-15 07:13:31 +000098
99const static struct option
100longOptions[] = {
101 {"help", no_argument, 0, 'h'},
102 {"calls", required_argument, 0, CALLS_OPT},
Carl Worth163b22c2012-08-10 10:15:30 -0700103 {"deps", no_argument, 0, DEPS_OPT},
104 {"no-deps", no_argument, 0, NO_DEPS_OPT},
Carl Worth16b18db2012-08-11 11:33:12 -0700105 {"prune", no_argument, 0, PRUNE_OPT},
106 {"no-prune", no_argument, 0, NO_PRUNE_OPT},
107 {"exact", no_argument, 0, 'x'},
Imre Deak6f07a842012-05-08 15:20:43 +0300108 {"thread", required_argument, 0, THREAD_OPT},
109 {"output", required_argument, 0, 'o'},
José Fonsecab682b672012-02-15 07:13:31 +0000110 {0, 0, 0, 0}
111};
112
Carl Worth163b22c2012-08-10 10:15:30 -0700113struct stringCompare {
114 bool operator() (const char *a, const char *b) const {
115 return strcmp(a, b) < 0;
116 }
117};
118
119class TraceAnalyzer {
120 /* Map for tracking resource dependencies between calls. */
121 std::map<const char *, std::set<unsigned>, stringCompare > resources;
122
123 /* The final set of calls required. This consists of calls added
124 * explicitly with the require() method as well as all calls
125 * implicitly required by those through resource dependencies. */
126 std::set<unsigned> required;
127
128public:
129 TraceAnalyzer() {}
130 ~TraceAnalyzer() {}
131
132 /* Compute and record all the resources provided by this call. */
133 void analyze(trace::Call *call) {
134 resources["state"].insert(call->no);
135 }
136
137 /* Require this call and all of its dependencies to be included in
138 * the final trace. */
139 void require(trace::Call *call) {
140 std::set<unsigned> *dependencies;
141 std::set<unsigned>::iterator i;
142
143 /* First, find and insert all calls that this call depends on. */
144 dependencies = &resources["state"];
145 for (i = dependencies->begin(); i != dependencies->end(); i++) {
146 required.insert(*i);
147 }
148 resources["state"].clear();
149
150 /* Then insert this call itself. */
151 required.insert(call->no);
152 }
153
154 /* Return a set of all the required calls, (both those calls added
155 * explicitly with require() and those implicitly depended
156 * upon. */
157 std::set<unsigned> *get_required(void) {
158 return &required;
159 }
160};
161
162struct trim_options {
163 /* Calls to be included in trace. */
164 trace::CallSet calls;
165
166 /* Whether dependency analysis should be performed. */
167 bool dependency_analysis;
168
Carl Worth16b18db2012-08-11 11:33:12 -0700169 /* Whether uninteresting calls should be pruned.. */
170 bool prune_uninteresting;
171
Carl Worth163b22c2012-08-10 10:15:30 -0700172 /* Output filename */
173 std::string output;
174
175 /* Emit only calls from this thread (-1 == all threads) */
176 int thread;
177};
178
179static int
180trim_trace(const char *filename, struct trim_options *options)
181{
182 trace::ParseBookmark beginning;
183 trace::Parser p;
184 TraceAnalyzer analyzer;
185 std::set<unsigned> *required;
186
187 if (!p.open(filename)) {
188 std::cerr << "error: failed to open " << filename << "\n";
189 return 1;
190 }
191
192 /* Mark the beginning so we can return here for pass 2. */
193 p.getBookmark(beginning);
194
195 /* In pass 1, analyze which calls are needed. */
196 trace::Call *call;
197 while ((call = p.parse_call())) {
198 /* If requested, ignore all calls not belonging to the specified thread. */
199 if (options->thread != -1 && call->thread_id != options->thread)
200 continue;
201
Carl Worth16b18db2012-08-11 11:33:12 -0700202 /* Also, prune if uninteresting (unless the user asked for no pruning. */
203 if (options->prune_uninteresting && call->flags & trace::CALL_FLAG_UNINTERESTING) {
204 continue;
205 }
206
Carl Worth163b22c2012-08-10 10:15:30 -0700207 /* If this call is included in the user-specified call
208 * set, then we don't need to perform any analysis on
209 * it. We know it must be included. */
210 if (options->calls.contains(*call)) {
211 analyzer.require(call);
212 } else {
213 if (options->dependency_analysis)
214 analyzer.analyze(call);
215 }
216 }
217
218 /* Prepare output file and writer for output. */
219 if (options->output.empty()) {
220 os::String base(filename);
221 base.trimExtension();
222
223 options->output = std::string(base.str()) + std::string("-trim.trace");
224 }
225
226 trace::Writer writer;
227 if (!writer.open(options->output.c_str())) {
228 std::cerr << "error: failed to create " << filename << "\n";
229 return 1;
230 }
231
232 /* Reset bookmark for pass 2. */
233 p.setBookmark(beginning);
234
235 /* In pass 2, emit the calls that are required. */
236 required = analyzer.get_required();
237
238 while ((call = p.parse_call())) {
239 if (required->find(call->no) != required->end()) {
240 writer.writeCall(call);
241 }
242 delete call;
243 }
244
245 std::cout << "Trimmed trace is available as " << options->output << "\n";
246
247 return 0;
248}
249
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800250static int
251command(int argc, char *argv[])
252{
Carl Worth163b22c2012-08-10 10:15:30 -0700253 struct trim_options options;
254
255 options.calls = trace::CallSet(trace::FREQUENCY_ALL);
256 options.dependency_analysis = true;
Carl Worth16b18db2012-08-11 11:33:12 -0700257 options.prune_uninteresting = true;
Carl Worth163b22c2012-08-10 10:15:30 -0700258 options.output = "";
259 options.thread = -1;
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800260
José Fonsecab682b672012-02-15 07:13:31 +0000261 int opt;
262 while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
263 switch (opt) {
264 case 'h':
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800265 usage();
266 return 0;
José Fonsecab682b672012-02-15 07:13:31 +0000267 case CALLS_OPT:
Carl Worth163b22c2012-08-10 10:15:30 -0700268 options.calls = trace::CallSet(optarg);
269 break;
270 case DEPS_OPT:
271 options.dependency_analysis = true;
272 break;
273 case NO_DEPS_OPT:
274 options.dependency_analysis = false;
José Fonsecab682b672012-02-15 07:13:31 +0000275 break;
Carl Worth16b18db2012-08-11 11:33:12 -0700276 case PRUNE_OPT:
277 options.prune_uninteresting = true;
278 break;
279 case NO_PRUNE_OPT:
280 options.prune_uninteresting = false;
281 break;
282 case 'x':
283 options.dependency_analysis = false;
284 options.prune_uninteresting = false;
285 break;
Imre Deak6f07a842012-05-08 15:20:43 +0300286 case THREAD_OPT:
Carl Worth163b22c2012-08-10 10:15:30 -0700287 options.thread = atoi(optarg);
Imre Deak6f07a842012-05-08 15:20:43 +0300288 break;
José Fonsecab682b672012-02-15 07:13:31 +0000289 case 'o':
Carl Worth163b22c2012-08-10 10:15:30 -0700290 options.output = optarg;
José Fonsecab682b672012-02-15 07:13:31 +0000291 break;
292 default:
293 std::cerr << "error: unexpected option `" << opt << "`\n";
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800294 usage();
295 return 1;
296 }
297 }
298
José Fonsecab682b672012-02-15 07:13:31 +0000299 if (optind >= argc) {
300 std::cerr << "error: apitrace trim requires a trace file as an argument.\n";
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800301 usage();
302 return 1;
303 }
304
Carl Worthf630d9d2012-09-04 16:48:00 -0700305 if (argc > optind + 1) {
306 std::cerr << "error: extraneous arguments:";
307 for (int i = optind + 1; i < argc; i++) {
308 std::cerr << " " << argv[i];
309 }
310 std::cerr << "\n";
311 usage();
312 return 1;
313 }
314
Carl Worth163b22c2012-08-10 10:15:30 -0700315 return trim_trace(argv[optind], &options);
Carl Worth4c5f6fa2011-11-14 14:50:07 -0800316}
317
318const Command trim_command = {
319 "trim",
320 synopsis,
321 usage,
322 command
323};