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