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
35 #include "os_string.hpp"
37 #include "trace_callset.hpp"
38 #include "trace_parser.hpp"
39 #include "trace_writer.hpp"
41 static const char *synopsis = "Create a new trace by trimming an existing trace.";
47 << "usage: apitrace trim [OPTIONS] TRACE_FILE...\n"
50 " -h, --help Show this help message and exit\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"
59 " --deps Perform dependency analysis and include dependent\n"
60 " 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"
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"
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"
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"
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"
80 " --thread=THREAD_ID Only retain calls from specified thread\n"
82 " -o, --output=TRACE_FILE Output trace file\n"
88 CALLS_OPT = CHAR_MAX + 1,
97 shortOptions = "ho:x";
99 const static struct option
101 {"help", no_argument, 0, 'h'},
102 {"calls", required_argument, 0, CALLS_OPT},
103 {"deps", no_argument, 0, DEPS_OPT},
104 {"no-deps", no_argument, 0, NO_DEPS_OPT},
105 {"prune", no_argument, 0, PRUNE_OPT},
106 {"no-prune", no_argument, 0, NO_PRUNE_OPT},
107 {"exact", no_argument, 0, 'x'},
108 {"thread", required_argument, 0, THREAD_OPT},
109 {"output", required_argument, 0, 'o'},
113 struct stringCompare {
114 bool operator() (const char *a, const char *b) const {
115 return strcmp(a, b) < 0;
119 class TraceAnalyzer {
120 /* Map for tracking resource dependencies between calls. */
121 std::map<const char *, std::set<unsigned>, stringCompare > resources;
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;
132 /* Compute and record all the resources provided by this call. */
133 void analyze(trace::Call *call) {
134 resources["state"].insert(call->no);
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;
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++) {
148 resources["state"].clear();
150 /* Then insert this call itself. */
151 required.insert(call->no);
154 /* Return a set of all the required calls, (both those calls added
155 * explicitly with require() and those implicitly depended
157 std::set<unsigned> *get_required(void) {
162 struct trim_options {
163 /* Calls to be included in trace. */
164 trace::CallSet calls;
166 /* Whether dependency analysis should be performed. */
167 bool dependency_analysis;
169 /* Whether uninteresting calls should be pruned.. */
170 bool prune_uninteresting;
172 /* Output filename */
175 /* Emit only calls from this thread (-1 == all threads) */
180 trim_trace(const char *filename, struct trim_options *options)
182 trace::ParseBookmark beginning;
184 TraceAnalyzer analyzer;
185 std::set<unsigned> *required;
187 if (!p.open(filename)) {
188 std::cerr << "error: failed to open " << filename << "\n";
192 /* Mark the beginning so we can return here for pass 2. */
193 p.getBookmark(beginning);
195 /* In pass 1, analyze which calls are needed. */
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)
202 /* Also, prune if uninteresting (unless the user asked for no pruning. */
203 if (options->prune_uninteresting && call->flags & trace::CALL_FLAG_UNINTERESTING) {
207 /* 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);
213 if (options->dependency_analysis)
214 analyzer.analyze(call);
218 /* Prepare output file and writer for output. */
219 if (options->output.empty()) {
220 os::String base(filename);
221 base.trimExtension();
223 options->output = std::string(base.str()) + std::string("-trim.trace");
226 trace::Writer writer;
227 if (!writer.open(options->output.c_str())) {
228 std::cerr << "error: failed to create " << filename << "\n";
232 /* Reset bookmark for pass 2. */
233 p.setBookmark(beginning);
235 /* In pass 2, emit the calls that are required. */
236 required = analyzer.get_required();
238 while ((call = p.parse_call())) {
239 if (required->find(call->no) != required->end()) {
240 writer.writeCall(call);
245 std::cout << "Trimmed trace is available as " << options->output << "\n";
251 command(int argc, char *argv[])
253 struct trim_options options;
255 options.calls = trace::CallSet(trace::FREQUENCY_ALL);
256 options.dependency_analysis = true;
257 options.prune_uninteresting = true;
262 while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
268 options.calls = trace::CallSet(optarg);
271 options.dependency_analysis = true;
274 options.dependency_analysis = false;
277 options.prune_uninteresting = true;
280 options.prune_uninteresting = false;
283 options.dependency_analysis = false;
284 options.prune_uninteresting = false;
287 options.thread = atoi(optarg);
290 options.output = optarg;
293 std::cerr << "error: unexpected option `" << opt << "`\n";
299 if (optind >= argc) {
300 std::cerr << "error: apitrace trim requires a trace file as an argument.\n";
305 if (argc > optind + 1) {
306 std::cerr << "error: extraneous arguments:";
307 for (int i = optind + 1; i < argc; i++) {
308 std::cerr << " " << argv[i];
315 return trim_trace(argv[optind], &options);
318 const Command trim_command = {