[PATCH] trim: Add a --frames option for selecting frames to trim.

Carl Worth cworth at cworth.org
Tue Aug 14 22:53:40 PDT 2012


This accepts a "frame set" which has syntax identical to the "call
set" of the existing --calls option. So, one could trim to just the
drawing operations of the first 10 frames of a trace with
--frames=0-9/draw.
---

 I've been meaning to add an option to allow for trimming by frame
 number in addition to the existing call-number-based selection since
 about forever. I finally got around to it now since I'm writing so
 "apitrace trim" stress-testing code that will use this.[*]

 I'm glad I sat down to do this, because it was much easier than I
 expected. See the patch below which consists primarily of
 documentation updates. I reused all of the call-set parsing which
 ends up being quite convenient, one can do things like
 --frames=0-10/draw,0-10/frame to select all the drawing operations
 and frame-termination calls in frames 0 through 10.

 This patch is on top of my recently-mentioned trim code. It could be
 adapted easily to master, (I'm guessing that the only real conflicts
 are in the documentation, since I split usage() from help() earlier
 in the series). But this can wait until the whole trim work gets
 accepted too. I certainly won't lose this patch.

 I'll also update things like "apitrace dump", "apitrace dump-images",
 and "apitrace diff-images", (basically anything that now accepts
 --calls), to also accept --frames. I just thought I would share this
 now that I have it.

 [*] My trim stress-tester will accept a trace and first generate all
 frame snapshots with "apitrace dump-images". Then it will trim out
 each frame, one at a time, and generate a snapshot from each. Finally
 it will compare all of those. If the trim code can survive that with
 some real-world test cases I think we're golden.

 I plan to add this stress-tester to apitrace-tests along with a few
 toy traces. Then, individual developers can simply copy in their own
 favorite traces (that would be too big or proprietary to commit to
 apitrace-tests) to that directory and we can all get some good
 testing.

 cli/cli_trim.cpp |   56 ++++++++++++++++++++++++++++++++++++++----------------
 1 file changed, 40 insertions(+), 16 deletions(-)

diff --git a/cli/cli_trim.cpp b/cli/cli_trim.cpp
index 5da9954..4f72f95 100644
--- a/cli/cli_trim.cpp
+++ b/cli/cli_trim.cpp
@@ -55,11 +55,12 @@ usage(void)
         "\n"
         "    -h, --help               Show detailed help for trim options and exit\n"
         "        --calls=CALLSET      Include specified calls in the trimmed output.\n"
+        "        --frames=FRAMESET    Include specified frames in the trimmed output.\n"
         "        --deps               Include additional calls to satisfy dependencies\n"
         "        --no-deps            Do not include calls from dependency analysis\n"
         "        --prune              Omit uninteresting calls from the trace output\n"
         "        --no-prune           Do not prune uninteresting calls from the trace.\n"
-        "    -x, --exact              Include exactly the calls specified in --calls\n"
+        "    -x, --exact              Trim exactly to calls specified in --calls/--frames\n"
         "                             Equivalent to both --no-deps and --no-prune\n"
         "        --thread=THREAD_ID   Only retain calls from specified thread\n"
         "    -o, --output=TRACE_FILE  Output trace file\n"
@@ -76,6 +77,7 @@ help()
         "    -h, --help               Show this help message and exit\n"
         "\n"
         "        --calls=CALLSET      Include specified calls in the trimmed output.\n"
+        "        --frames=FRAMESET    Include specified frames in the trimmed output.\n"
         "                             Note that due to dependency analysis and pruning\n"
         "                             of uninteresting calls the resulting trace may\n"
         "                             include more and less calls than specified.\n"
@@ -84,24 +86,25 @@ help()
         "\n"
         "        --deps               Perform dependency analysis and include dependent\n"
         "                             calls as needed, (even if those calls were not\n"
-        "                             explcitly requested with --calls). This is the\n"
-        "                             default behavior. See --no-deps and --exact.\n"
+        "                             explcitly requested with --calls or --frames).\n"
+        "                             This is the default behavior. See --no-deps and\n"
+        "                             --exact to change the behavior.\n"
         "\n"
         "        --no-deps            Do not perform dependency analysis. In this mode\n"
         "                             the trimmed trace will never include calls from\n"
-        "                             outside the range specified in --calls.\n"
+        "                             outside what is specified in --calls or --frames.\n"
         "\n"
-        "        --prune              Omit calls that have no side effects, even if the\n"
-        "                             call is within the range specified by --calls.\n"
-        "                             This is the default behavior. See --no-prune\n"
+        "        --prune              Omit calls with no side effects, even if the call\n"
+        "                             is within the range specified by --calls/--frames.\n"
+        "                             This is the default behavior. See --no-prune.\n"
         "\n"
         "        --no-prune           Do not prune uninteresting calls from the trace.\n"
         "                             In this mode the trimmed trace will never omit\n"
-        "                             any calls within the range specified in --calls.\n"
+        "                             any calls within the user-specified range.\n"
         "\n"
         "    -x, --exact              Trim the trace to exactly the calls specified in\n"
-        "                             --calls. This option is equivalent to passing\n"
-        "                             both --no-deps and --no-prune.\n"
+        "                             --calls and --frames. This option is equivalent\n"
+        "                             to passing both --no-deps and --no-prune.\n"
         "\n"
         "        --thread=THREAD_ID   Only retain calls from specified thread\n"
         "\n"
@@ -112,6 +115,7 @@ help()
 
 enum {
     CALLS_OPT = CHAR_MAX + 1,
+    FRAMES_OPT,
     DEPS_OPT,
     NO_DEPS_OPT,
     PRUNE_OPT,
@@ -126,6 +130,7 @@ const static struct option
 longOptions[] = {
     {"help", no_argument, 0, 'h'},
     {"calls", required_argument, 0, CALLS_OPT},
+    {"frames", required_argument, 0, FRAMES_OPT},
     {"deps", no_argument, 0, DEPS_OPT},
     {"no-deps", no_argument, 0, NO_DEPS_OPT},
     {"prune", no_argument, 0, PRUNE_OPT},
@@ -663,6 +668,9 @@ struct trim_options {
     /* Calls to be included in trace. */
     trace::CallSet calls;
 
+    /* Frames to be included in trace. */
+    trace::CallSet frames;
+
     /* Whether dependency analysis should be performed. */
     bool dependency_analysis;
 
@@ -683,6 +691,7 @@ trim_trace(const char *filename, struct trim_options *options)
     trace::Parser p;
     TraceAnalyzer analyzer;
     std::set<unsigned> *required;
+    unsigned frame;
 
     if (!p.open(filename)) {
         std::cerr << "error: failed to open " << filename << "\n";
@@ -693,6 +702,7 @@ trim_trace(const char *filename, struct trim_options *options)
     p.getBookmark(beginning);
 
     /* In pass 1, analyze which calls are needed. */
+    frame = 0;
     trace::Call *call;
     while ((call = p.parse_call())) {
 
@@ -705,20 +715,20 @@ trim_trace(const char *filename, struct trim_options *options)
 
         /* If requested, ignore all calls not belonging to the specified thread. */
         if (options->thread != -1 && call->thread_id != options->thread) {
-            delete call;
-            continue;
+            goto NEXT;
         }
 
         /* Also, prune if uninteresting (unless the user asked for no pruning. */
         if (options->prune_uninteresting && call->flags & trace::CALL_FLAG_UNINTERESTING) {
-            delete call;
-            continue;
+            goto NEXT;
         }
 
         /* If this call is included in the user-specified call set,
          * then require it (and all dependencies) in the trimmed
          * output. */
-        if (options->calls.contains(*call)) {
+        if (options->calls.contains(*call) ||
+            options->frames.contains(frame, call->flags)) {
+
             analyzer.require(call);
         }
 
@@ -730,6 +740,10 @@ trim_trace(const char *filename, struct trim_options *options)
             analyzer.analyze(call);
         }
 
+    NEXT:
+        if (call->flags & trace::CALL_FLAG_END_FRAME)
+            frame++;
+
         delete call;
     }
 
@@ -776,7 +790,8 @@ command(int argc, char *argv[])
 {
     struct trim_options options;
 
-    options.calls = trace::CallSet(trace::FREQUENCY_ALL);
+    options.calls = trace::CallSet(trace::FREQUENCY_NONE);
+    options.frames = trace::CallSet(trace::FREQUENCY_NONE);
     options.dependency_analysis = true;
     options.prune_uninteresting = true;
     options.output = "";
@@ -791,6 +806,9 @@ command(int argc, char *argv[])
         case CALLS_OPT:
             options.calls = trace::CallSet(optarg);
             break;
+        case FRAMES_OPT:
+            options.frames = trace::CallSet(optarg);
+            break;
         case DEPS_OPT:
             options.dependency_analysis = true;
             break;
@@ -820,6 +838,12 @@ command(int argc, char *argv[])
         }
     }
 
+    /* If neither of --calls nor --frames was set, default to the
+     * entire set of calls. */
+    if (options.calls.empty() && options.frames.empty()) {
+        options.calls = trace::CallSet(trace::FREQUENCY_ALL);
+    }
+
     if (optind >= argc) {
         std::cerr << "error: apitrace trim requires a trace file as an argument.\n";
         usage();
-- 
1.7.10



More information about the apitrace mailing list