Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 1 | /************************************************************************** |
| 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 Worth | 33b9263 | 2012-08-14 14:11:19 -0700 | [diff] [blame] | 27 | #include <sstream> |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 28 | #include <string.h> |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 29 | #include <limits.h> // for CHAR_MAX |
| 30 | #include <getopt.h> |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 31 | |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 32 | #include <set> |
| 33 | |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 34 | #include "cli.hpp" |
| 35 | |
| 36 | #include "os_string.hpp" |
| 37 | |
Carl Worth | 8efbe01 | 2012-08-17 14:15:54 -0700 | [diff] [blame] | 38 | #include "trace_analyzer.hpp" |
José Fonseca | d3c0013 | 2012-01-27 22:43:53 +0000 | [diff] [blame] | 39 | #include "trace_callset.hpp" |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 40 | #include "trace_parser.hpp" |
José Fonseca | 630471a | 2012-01-27 22:06:51 +0000 | [diff] [blame] | 41 | #include "trace_writer.hpp" |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 42 | |
| 43 | static const char *synopsis = "Create a new trace by trimming an existing trace."; |
| 44 | |
| 45 | static void |
| 46 | usage(void) |
| 47 | { |
| 48 | std::cout |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 49 | << "usage: apitrace trim [OPTIONS] TRACE_FILE...\n" |
José Fonseca | d3c0013 | 2012-01-27 22:43:53 +0000 | [diff] [blame] | 50 | << synopsis << "\n" |
| 51 | "\n" |
Carl Worth | 8646443 | 2012-08-11 11:46:54 -0700 | [diff] [blame] | 52 | " -h, --help Show detailed help for trim options and exit\n" |
| 53 | " --calls=CALLSET Include specified calls in the trimmed output.\n" |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame] | 54 | " --frames=FRAMESET Include specified frames in the trimmed output.\n" |
Carl Worth | 8646443 | 2012-08-11 11:46:54 -0700 | [diff] [blame] | 55 | " --deps Include additional calls to satisfy dependencies\n" |
Carl Worth | 8646443 | 2012-08-11 11:46:54 -0700 | [diff] [blame] | 56 | " --prune Omit uninteresting calls from the trace output\n" |
José Fonseca | 6e4768b | 2012-11-22 08:31:47 +0000 | [diff] [blame] | 57 | " -a, --auto Trim automatically to calls specified in --calls/--frames\n" |
| 58 | " Equivalent to both --deps and --prune\n" |
Carl Worth | ab82b3b | 2012-08-15 16:35:38 -0700 | [diff] [blame] | 59 | " --print-callset Print the final set of calls included in output\n" |
Carl Worth | 25fe524 | 2012-11-14 20:55:31 -0800 | [diff] [blame] | 60 | " --trim-spec=SPEC Only performing trimming as described in SPEC\n" |
Carl Worth | 8646443 | 2012-08-11 11:46:54 -0700 | [diff] [blame] | 61 | " --thread=THREAD_ID Only retain calls from specified thread\n" |
| 62 | " -o, --output=TRACE_FILE Output trace file\n" |
| 63 | ; |
| 64 | } |
| 65 | |
| 66 | static void |
| 67 | help() |
| 68 | { |
| 69 | std::cout |
| 70 | << "usage: apitrace trim [OPTIONS] TRACE_FILE...\n" |
| 71 | << synopsis << "\n" |
| 72 | "\n" |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 73 | " -h, --help Show this help message and exit\n" |
Carl Worth | 16b18db | 2012-08-11 11:33:12 -0700 | [diff] [blame] | 74 | "\n" |
| 75 | " --calls=CALLSET Include specified calls in the trimmed output.\n" |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame] | 76 | " --frames=FRAMESET Include specified frames in the trimmed output.\n" |
Carl Worth | 16b18db | 2012-08-11 11:33:12 -0700 | [diff] [blame] | 77 | "\n" |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 78 | " --deps Perform dependency analysis and include dependent\n" |
Carl Worth | 16b18db | 2012-08-11 11:33:12 -0700 | [diff] [blame] | 79 | " calls as needed, (even if those calls were not\n" |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame] | 80 | " explicitly requested with --calls or --frames).\n" |
Carl Worth | 16b18db | 2012-08-11 11:33:12 -0700 | [diff] [blame] | 81 | "\n" |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame] | 82 | " --prune Omit calls with no side effects, even if the call\n" |
| 83 | " is within the range specified by --calls/--frames.\n" |
Carl Worth | 16b18db | 2012-08-11 11:33:12 -0700 | [diff] [blame] | 84 | "\n" |
José Fonseca | 6e4768b | 2012-11-22 08:31:47 +0000 | [diff] [blame] | 85 | " -a, --auto Use dependency analysis and pruning\n" |
| 86 | " of uninteresting calls the resulting trace may\n" |
| 87 | " include more and less calls than specified.\n" |
| 88 | " This option is equivalent\n" |
| 89 | " to passing both --deps and --prune.\n" |
Carl Worth | 16b18db | 2012-08-11 11:33:12 -0700 | [diff] [blame] | 90 | "\n" |
Carl Worth | ab82b3b | 2012-08-15 16:35:38 -0700 | [diff] [blame] | 91 | " --print-callset Print to stdout the final set of calls included\n" |
| 92 | " in the trim output. This can be useful for\n" |
Carl Worth | 25fe524 | 2012-11-14 20:55:31 -0800 | [diff] [blame] | 93 | " tweaking the trimmed callset from --auto on the\n" |
José Fonseca | 6e4768b | 2012-11-22 08:31:47 +0000 | [diff] [blame] | 94 | " command-line.\n" |
| 95 | " Use --calls=@FILE to read callset from a file.\n" |
Carl Worth | 25fe524 | 2012-11-14 20:55:31 -0800 | [diff] [blame] | 96 | " --trim-spec=SPEC Specifies which classes of calls will be trimmed.\n" |
| 97 | " This option only has an effect if dependency\n" |
| 98 | " analysis is enabled. The argument is a comma-\n" |
| 99 | " separated list of names from the following:\n" |
| 100 | "\n" |
| 101 | " no-side-effects Calls with no side effects\n" |
| 102 | " textures Calls to setup unused textures\n" |
| 103 | " shaders Calls to setup unused shaders\n" |
| 104 | " drawing Calls that draw\n" |
| 105 | "\n" |
| 106 | " The default trim specification includes all of\n" |
| 107 | " the above, (as much as possible will be trimmed).\n" |
Carl Worth | ab82b3b | 2012-08-15 16:35:38 -0700 | [diff] [blame] | 108 | "\n" |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 109 | " --thread=THREAD_ID Only retain calls from specified thread\n" |
Carl Worth | 16b18db | 2012-08-11 11:33:12 -0700 | [diff] [blame] | 110 | "\n" |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 111 | " -o, --output=TRACE_FILE Output trace file\n" |
José Fonseca | d3c0013 | 2012-01-27 22:43:53 +0000 | [diff] [blame] | 112 | "\n" |
| 113 | ; |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 114 | } |
| 115 | |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 116 | enum { |
Imre Deak | 6f07a84 | 2012-05-08 15:20:43 +0300 | [diff] [blame] | 117 | CALLS_OPT = CHAR_MAX + 1, |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame] | 118 | FRAMES_OPT, |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 119 | DEPS_OPT, |
Carl Worth | 16b18db | 2012-08-11 11:33:12 -0700 | [diff] [blame] | 120 | PRUNE_OPT, |
Imre Deak | 6f07a84 | 2012-05-08 15:20:43 +0300 | [diff] [blame] | 121 | THREAD_OPT, |
Carl Worth | ab82b3b | 2012-08-15 16:35:38 -0700 | [diff] [blame] | 122 | PRINT_CALLSET_OPT, |
Carl Worth | 25fe524 | 2012-11-14 20:55:31 -0800 | [diff] [blame] | 123 | TRIM_SPEC_OPT |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | const static char * |
Carl Worth | 54300eb | 2013-01-28 23:37:17 +1100 | [diff] [blame] | 127 | shortOptions = "aho:x"; |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 128 | |
| 129 | const static struct option |
| 130 | longOptions[] = { |
| 131 | {"help", no_argument, 0, 'h'}, |
| 132 | {"calls", required_argument, 0, CALLS_OPT}, |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame] | 133 | {"frames", required_argument, 0, FRAMES_OPT}, |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 134 | {"deps", no_argument, 0, DEPS_OPT}, |
Carl Worth | 16b18db | 2012-08-11 11:33:12 -0700 | [diff] [blame] | 135 | {"prune", no_argument, 0, PRUNE_OPT}, |
José Fonseca | 6e4768b | 2012-11-22 08:31:47 +0000 | [diff] [blame] | 136 | {"auto", no_argument, 0, 'a'}, |
Imre Deak | 6f07a84 | 2012-05-08 15:20:43 +0300 | [diff] [blame] | 137 | {"thread", required_argument, 0, THREAD_OPT}, |
| 138 | {"output", required_argument, 0, 'o'}, |
Carl Worth | ab82b3b | 2012-08-15 16:35:38 -0700 | [diff] [blame] | 139 | {"print-callset", no_argument, 0, PRINT_CALLSET_OPT}, |
Carl Worth | 25fe524 | 2012-11-14 20:55:31 -0800 | [diff] [blame] | 140 | {"trim-spec", required_argument, 0, TRIM_SPEC_OPT}, |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 141 | {0, 0, 0, 0} |
| 142 | }; |
| 143 | |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 144 | struct stringCompare { |
| 145 | bool operator() (const char *a, const char *b) const { |
| 146 | return strcmp(a, b) < 0; |
| 147 | } |
| 148 | }; |
| 149 | |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 150 | struct trim_options { |
| 151 | /* Calls to be included in trace. */ |
| 152 | trace::CallSet calls; |
| 153 | |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame] | 154 | /* Frames to be included in trace. */ |
| 155 | trace::CallSet frames; |
| 156 | |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 157 | /* Whether dependency analysis should be performed. */ |
| 158 | bool dependency_analysis; |
| 159 | |
Carl Worth | 16b18db | 2012-08-11 11:33:12 -0700 | [diff] [blame] | 160 | /* Whether uninteresting calls should be pruned.. */ |
| 161 | bool prune_uninteresting; |
| 162 | |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 163 | /* Output filename */ |
| 164 | std::string output; |
| 165 | |
| 166 | /* Emit only calls from this thread (-1 == all threads) */ |
| 167 | int thread; |
Carl Worth | ab82b3b | 2012-08-15 16:35:38 -0700 | [diff] [blame] | 168 | |
| 169 | /* Print resulting callset */ |
| 170 | int print_callset; |
Carl Worth | 25fe524 | 2012-11-14 20:55:31 -0800 | [diff] [blame] | 171 | |
| 172 | /* What kind of trimming to perform. */ |
| 173 | TrimFlags trim_flags; |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 174 | }; |
| 175 | |
| 176 | static int |
| 177 | trim_trace(const char *filename, struct trim_options *options) |
| 178 | { |
| 179 | trace::ParseBookmark beginning; |
| 180 | trace::Parser p; |
Carl Worth | 25fe524 | 2012-11-14 20:55:31 -0800 | [diff] [blame] | 181 | TraceAnalyzer analyzer(options->trim_flags); |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 182 | std::set<unsigned> *required; |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame] | 183 | unsigned frame; |
Carl Worth | ab82b3b | 2012-08-15 16:35:38 -0700 | [diff] [blame] | 184 | int call_range_first, call_range_last; |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 185 | |
| 186 | if (!p.open(filename)) { |
| 187 | std::cerr << "error: failed to open " << filename << "\n"; |
| 188 | return 1; |
| 189 | } |
| 190 | |
| 191 | /* Mark the beginning so we can return here for pass 2. */ |
| 192 | p.getBookmark(beginning); |
| 193 | |
| 194 | /* In pass 1, analyze which calls are needed. */ |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame] | 195 | frame = 0; |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 196 | trace::Call *call; |
| 197 | while ((call = p.parse_call())) { |
Carl Worth | 5b827e1 | 2012-08-12 20:41:50 -0700 | [diff] [blame] | 198 | |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame] | 199 | /* There's no use doing any work past the last call or frame |
| 200 | * requested by the user. */ |
| 201 | if (call->no > options->calls.getLast() || |
| 202 | frame > options->frames.getLast()) { |
| 203 | |
Carl Worth | 4224901 | 2012-08-14 10:26:11 -0700 | [diff] [blame] | 204 | delete call; |
Carl Worth | 5b827e1 | 2012-08-12 20:41:50 -0700 | [diff] [blame] | 205 | break; |
Carl Worth | 4224901 | 2012-08-14 10:26:11 -0700 | [diff] [blame] | 206 | } |
Carl Worth | 5b827e1 | 2012-08-12 20:41:50 -0700 | [diff] [blame] | 207 | |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 208 | /* If requested, ignore all calls not belonging to the specified thread. */ |
Carl Worth | 4224901 | 2012-08-14 10:26:11 -0700 | [diff] [blame] | 209 | if (options->thread != -1 && call->thread_id != options->thread) { |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame] | 210 | goto NEXT; |
Carl Worth | 4224901 | 2012-08-14 10:26:11 -0700 | [diff] [blame] | 211 | } |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 212 | |
Carl Worth | 6e57af8 | 2013-02-15 16:31:11 -0800 | [diff] [blame^] | 213 | /* Also, prune if no side effects (unless the user asked for no pruning. */ |
| 214 | if (options->prune_uninteresting && call->flags & trace::CALL_FLAG_NO_SIDE_EFFECTS) { |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame] | 215 | goto NEXT; |
Carl Worth | 16b18db | 2012-08-11 11:33:12 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Carl Worth | cc6e51c | 2012-08-13 14:35:43 -0700 | [diff] [blame] | 218 | /* If this call is included in the user-specified call set, |
| 219 | * then require it (and all dependencies) in the trimmed |
| 220 | * output. */ |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame] | 221 | if (options->calls.contains(*call) || |
| 222 | options->frames.contains(frame, call->flags)) { |
| 223 | |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 224 | analyzer.require(call); |
Carl Worth | cc6e51c | 2012-08-13 14:35:43 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /* Regardless of whether we include this call or not, we do |
| 228 | * some dependency tracking (unless disabled by the user). We |
| 229 | * do this even for calls we have included in the output so |
| 230 | * that any state updates get performed. */ |
| 231 | if (options->dependency_analysis) { |
| 232 | analyzer.analyze(call); |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 233 | } |
Carl Worth | 4224901 | 2012-08-14 10:26:11 -0700 | [diff] [blame] | 234 | |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame] | 235 | NEXT: |
| 236 | if (call->flags & trace::CALL_FLAG_END_FRAME) |
| 237 | frame++; |
| 238 | |
Carl Worth | 4224901 | 2012-08-14 10:26:11 -0700 | [diff] [blame] | 239 | delete call; |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /* Prepare output file and writer for output. */ |
| 243 | if (options->output.empty()) { |
| 244 | os::String base(filename); |
| 245 | base.trimExtension(); |
| 246 | |
| 247 | options->output = std::string(base.str()) + std::string("-trim.trace"); |
| 248 | } |
| 249 | |
| 250 | trace::Writer writer; |
| 251 | if (!writer.open(options->output.c_str())) { |
| 252 | std::cerr << "error: failed to create " << filename << "\n"; |
| 253 | return 1; |
| 254 | } |
| 255 | |
| 256 | /* Reset bookmark for pass 2. */ |
| 257 | p.setBookmark(beginning); |
| 258 | |
| 259 | /* In pass 2, emit the calls that are required. */ |
| 260 | required = analyzer.get_required(); |
| 261 | |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame] | 262 | frame = 0; |
Carl Worth | ab82b3b | 2012-08-15 16:35:38 -0700 | [diff] [blame] | 263 | call_range_first = -1; |
| 264 | call_range_last = -1; |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 265 | while ((call = p.parse_call())) { |
Carl Worth | 5b827e1 | 2012-08-12 20:41:50 -0700 | [diff] [blame] | 266 | |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame] | 267 | /* There's no use doing any work past the last call or frame |
| 268 | * requested by the user. */ |
| 269 | if (call->no > options->calls.getLast() || |
| 270 | frame > options->frames.getLast()) { |
| 271 | |
Carl Worth | 5b827e1 | 2012-08-12 20:41:50 -0700 | [diff] [blame] | 272 | break; |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame] | 273 | } |
Carl Worth | 5b827e1 | 2012-08-12 20:41:50 -0700 | [diff] [blame] | 274 | |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 275 | if (required->find(call->no) != required->end()) { |
| 276 | writer.writeCall(call); |
Carl Worth | ab82b3b | 2012-08-15 16:35:38 -0700 | [diff] [blame] | 277 | |
| 278 | if (options->print_callset) { |
| 279 | if (call_range_first < 0) { |
| 280 | call_range_first = call->no; |
| 281 | printf ("%d", call_range_first); |
| 282 | } else if (call->no != call_range_last + 1) { |
| 283 | if (call_range_last != call_range_first) |
| 284 | printf ("-%d", call_range_last); |
| 285 | call_range_first = call->no; |
| 286 | printf (",%d", call_range_first); |
| 287 | } |
| 288 | call_range_last = call->no; |
| 289 | } |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 290 | } |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame] | 291 | |
| 292 | if (call->flags & trace::CALL_FLAG_END_FRAME) { |
| 293 | frame++; |
| 294 | } |
| 295 | |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 296 | delete call; |
| 297 | } |
| 298 | |
Carl Worth | ab82b3b | 2012-08-15 16:35:38 -0700 | [diff] [blame] | 299 | if (options->print_callset) { |
| 300 | if (call_range_last != call_range_first) |
| 301 | printf ("-%d\n", call_range_last); |
| 302 | } |
| 303 | |
Carl Worth | 1a800a1 | 2012-08-17 14:23:13 -0700 | [diff] [blame] | 304 | std::cerr << "Trimmed trace is available as " << options->output << "\n"; |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 305 | |
| 306 | return 0; |
| 307 | } |
| 308 | |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 309 | static int |
Carl Worth | 25fe524 | 2012-11-14 20:55:31 -0800 | [diff] [blame] | 310 | parse_trim_spec(const char *trim_spec, TrimFlags *flags) |
| 311 | { |
| 312 | std::string spec(trim_spec), word; |
| 313 | size_t start = 0, comma = 0; |
| 314 | *flags = 0; |
| 315 | |
| 316 | while (start < spec.size()) { |
| 317 | comma = spec.find(',', start); |
| 318 | |
| 319 | if (comma == std::string::npos) |
| 320 | word = std::string(spec, start); |
| 321 | else |
| 322 | word = std::string(spec, start, comma - start); |
| 323 | |
| 324 | if (strcmp(word.c_str(), "no-side-effects") == 0) |
| 325 | *flags |= TRIM_FLAG_NO_SIDE_EFFECTS; |
| 326 | else if (strcmp(word.c_str(), "textures") == 0) |
| 327 | *flags |= TRIM_FLAG_TEXTURES; |
| 328 | else if (strcmp(word.c_str(), "shaders") == 0) |
| 329 | *flags |= TRIM_FLAG_SHADERS; |
| 330 | else if (strcmp(word.c_str(), "drawing") == 0) |
| 331 | *flags |= TRIM_FLAG_DRAWING; |
| 332 | else { |
| 333 | return 1; |
| 334 | } |
| 335 | |
| 336 | if (comma == std::string::npos) |
| 337 | break; |
| 338 | |
| 339 | start = comma + 1; |
| 340 | } |
| 341 | |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | static int |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 346 | command(int argc, char *argv[]) |
| 347 | { |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 348 | struct trim_options options; |
| 349 | |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame] | 350 | options.calls = trace::CallSet(trace::FREQUENCY_NONE); |
| 351 | options.frames = trace::CallSet(trace::FREQUENCY_NONE); |
José Fonseca | 6e4768b | 2012-11-22 08:31:47 +0000 | [diff] [blame] | 352 | options.dependency_analysis = false; |
| 353 | options.prune_uninteresting = false; |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 354 | options.output = ""; |
| 355 | options.thread = -1; |
Carl Worth | ab82b3b | 2012-08-15 16:35:38 -0700 | [diff] [blame] | 356 | options.print_callset = 0; |
Carl Worth | 25fe524 | 2012-11-14 20:55:31 -0800 | [diff] [blame] | 357 | options.trim_flags = -1; |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 358 | |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 359 | int opt; |
| 360 | while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) { |
| 361 | switch (opt) { |
| 362 | case 'h': |
Carl Worth | 8646443 | 2012-08-11 11:46:54 -0700 | [diff] [blame] | 363 | help(); |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 364 | return 0; |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 365 | case CALLS_OPT: |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 366 | options.calls = trace::CallSet(optarg); |
| 367 | break; |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame] | 368 | case FRAMES_OPT: |
| 369 | options.frames = trace::CallSet(optarg); |
| 370 | break; |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 371 | case DEPS_OPT: |
| 372 | options.dependency_analysis = true; |
| 373 | break; |
Carl Worth | 16b18db | 2012-08-11 11:33:12 -0700 | [diff] [blame] | 374 | case PRUNE_OPT: |
| 375 | options.prune_uninteresting = true; |
| 376 | break; |
José Fonseca | 6e4768b | 2012-11-22 08:31:47 +0000 | [diff] [blame] | 377 | case 'a': |
| 378 | options.dependency_analysis = true; |
| 379 | options.prune_uninteresting = true; |
Carl Worth | 16b18db | 2012-08-11 11:33:12 -0700 | [diff] [blame] | 380 | break; |
Imre Deak | 6f07a84 | 2012-05-08 15:20:43 +0300 | [diff] [blame] | 381 | case THREAD_OPT: |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 382 | options.thread = atoi(optarg); |
Imre Deak | 6f07a84 | 2012-05-08 15:20:43 +0300 | [diff] [blame] | 383 | break; |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 384 | case 'o': |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 385 | options.output = optarg; |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 386 | break; |
Carl Worth | ab82b3b | 2012-08-15 16:35:38 -0700 | [diff] [blame] | 387 | case PRINT_CALLSET_OPT: |
| 388 | options.print_callset = 1; |
| 389 | break; |
Carl Worth | 25fe524 | 2012-11-14 20:55:31 -0800 | [diff] [blame] | 390 | case TRIM_SPEC_OPT: |
| 391 | if (parse_trim_spec(optarg, &options.trim_flags)) { |
| 392 | std::cerr << "error: illegal value for trim-spec: " << optarg << "\n"; |
| 393 | std::cerr << "See \"apitrace help trim\" for help.\n"; |
| 394 | return 1; |
| 395 | } |
| 396 | break; |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 397 | default: |
| 398 | std::cerr << "error: unexpected option `" << opt << "`\n"; |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 399 | usage(); |
| 400 | return 1; |
| 401 | } |
| 402 | } |
| 403 | |
Carl Worth | 46abfd1 | 2012-08-14 22:32:29 -0700 | [diff] [blame] | 404 | /* If neither of --calls nor --frames was set, default to the |
| 405 | * entire set of calls. */ |
| 406 | if (options.calls.empty() && options.frames.empty()) { |
| 407 | options.calls = trace::CallSet(trace::FREQUENCY_ALL); |
| 408 | } |
| 409 | |
José Fonseca | b682b67 | 2012-02-15 07:13:31 +0000 | [diff] [blame] | 410 | if (optind >= argc) { |
| 411 | std::cerr << "error: apitrace trim requires a trace file as an argument.\n"; |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 412 | usage(); |
| 413 | return 1; |
| 414 | } |
| 415 | |
Carl Worth | f630d9d | 2012-09-04 16:48:00 -0700 | [diff] [blame] | 416 | if (argc > optind + 1) { |
| 417 | std::cerr << "error: extraneous arguments:"; |
| 418 | for (int i = optind + 1; i < argc; i++) { |
| 419 | std::cerr << " " << argv[i]; |
| 420 | } |
| 421 | std::cerr << "\n"; |
| 422 | usage(); |
| 423 | return 1; |
| 424 | } |
| 425 | |
Carl Worth | 83ec3f6 | 2012-08-17 14:22:30 -0700 | [diff] [blame] | 426 | if (options.dependency_analysis) { |
| 427 | std::cerr << |
| 428 | "Note: The dependency analysis in \"apitrace trim\" is still experimental.\n" |
| 429 | " We hope that it will be useful, but it may lead to incorrect results.\n" |
| 430 | " If you find a trace that misbehaves while trimming, please share that\n" |
| 431 | " by sending email to apitrace@lists.freedesktop.org, cworth@cworth.org\n"; |
| 432 | } |
| 433 | |
Carl Worth | 163b22c | 2012-08-10 10:15:30 -0700 | [diff] [blame] | 434 | return trim_trace(argv[optind], &options); |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | const Command trim_command = { |
| 438 | "trim", |
| 439 | synopsis, |
Carl Worth | 8646443 | 2012-08-11 11:46:54 -0700 | [diff] [blame] | 440 | help, |
Carl Worth | 4c5f6fa | 2011-11-14 14:50:07 -0800 | [diff] [blame] | 441 | command |
| 442 | }; |