]> git.cworth.org Git - apitrace/commitdiff
Add "apitrace repack" command.
authorJosé Fonseca <jose.r.fonseca@gmail.com>
Sun, 27 Nov 2011 12:32:00 +0000 (12:32 +0000)
committerJosé Fonseca <jose.r.fonseca@gmail.com>
Sun, 27 Nov 2011 12:45:22 +0000 (12:45 +0000)
CMakeLists.txt
cli/CMakeLists.txt
cli/cli.hpp
cli/cli_main.cpp
cli/cli_repack.cpp [new file with mode: 0644]
common/trace_file.hpp
common/trace_file_read.cpp [new file with mode: 0644]
common/trace_file_write.cpp [new file with mode: 0644]
common/trace_parser.cpp
scripts/tracerepack.py [deleted file]

index c3e8a846caf95d61ac5ea382e07851715e8a77ac..9c0f2f5c180f0e9d30e90b0492cdfdbd75d72658 100755 (executable)
@@ -264,6 +264,8 @@ endif ()
 
 add_library (common STATIC
     common/trace_file.cpp
+    common/trace_file_read.cpp
+    common/trace_file_write.cpp
     common/trace_file_zlib.cpp
     common/trace_file_snappy.cpp
     common/trace_model.cpp
index 5196467f2494f06f00a31c358cf41bf205b0b592..e9419f9916bc4c3486ad3277306068fd036d68ff 100644 (file)
@@ -4,6 +4,7 @@ add_executable (apitrace
     cli_diff_state.cpp
     cli_diff_images.cpp
     cli_dump.cpp
+    cli_repack.cpp
     cli_trace.cpp
 )
 
index 1461b4dce61d6f7e6bd211b04d527e2c0eeab1c8..ba2148bfa00d3adb4f5cdf73398dadceca013028 100644 (file)
@@ -44,6 +44,7 @@ extern const Command diff_command;
 extern const Command diff_state_command;
 extern const Command diff_images_command;
 extern const Command dump_command;
+extern const Command repack_command;
 extern const Command trace_command;
 
 #endif /* _APITRACE_CLI_HPP_ */
index fb4f88f845da40603b0428b958a577f290a7085c..8f9392e49909c86478d3f6bbbaa0c34bed832e22 100644 (file)
@@ -70,6 +70,7 @@ static const Command * commands[] = {
     &diff_state_command,
     &diff_images_command,
     &dump_command,
+    &repack_command,
     &trace_command,
     &help_command
 };
@@ -184,5 +185,5 @@ main(int argc, char **argv)
     std::cerr << "Error: unknown command " << command_name
               << " (see \"apitrace help\").\n";
 
-    return 1;
+   return 1;
 }
diff --git a/cli/cli_repack.cpp b/cli/cli_repack.cpp
new file mode 100644 (file)
index 0000000..918f54a
--- /dev/null
@@ -0,0 +1,116 @@
+/**************************************************************************
+ *
+ * Copyright 2011 Jose Fonseca
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ **************************************************************************/
+
+
+#include <string.h>
+#include <iostream>
+
+#include "cli.hpp"
+
+#include "trace_file.hpp"
+
+
+static const char *synopsis = "Repack a trace file with Snappy compression.";
+
+static void
+usage(void)
+{
+    std::cout
+        << "usage: apitrace repack <in-trace-file> <out-trace-file>\n"
+        << synopsis << "\n"
+        << "\n"
+        << "Snappy compression allows for faster replay and smaller memory footprint,\n"
+        << "at the expense of a slightly smaller compression ratio than zlib\n"
+        << "\n";
+}
+
+static int
+repack(const char *inFileName, const char *outFileName)
+{
+    trace::File *inFile = trace::File::createForRead(inFileName);
+    if (!inFile) {
+        return 1;
+    }
+
+    trace::File *outFile = trace::File::createForWrite(outFileName);
+    if (!outFile) {
+        delete inFile;
+        return 1;
+    }
+
+    size_t size = 8192;
+    char *buf = new char[size];
+    size_t read;
+
+    while ((read = inFile->read(buf, size)) != 0) {
+        outFile->write(buf, read);
+    }
+
+    delete [] buf;
+    delete outFile;
+    delete inFile;
+
+    return 0;
+}
+
+static int
+command(int argc, char *argv[])
+{
+    int i;
+
+    for (i = 0; i < argc; ++i) {
+        const char *arg = argv[i];
+
+        if (arg[0] != '-') {
+            break;
+        }
+
+        if (!strcmp(arg, "--")) {
+            break;
+        } else if (strcmp(arg, "--help") == 0) {
+            usage();
+            return 0;
+        } else {
+            std::cerr << "error: unknown option " << arg << "\n";
+            usage();
+            return 1;
+        }
+    }
+
+    if (argc != i + 2) {
+        std::cerr << "error: insufficient number of arguments\n";
+        usage();
+        return 1;
+    }
+
+    return repack(argv[i], argv[i + 1]);
+}
+
+const Command repack_command = {
+    "repack",
+    synopsis,
+    usage,
+    command
+};
index 68f50b4a0744ec3abe4d3a839abbf2c4c83fea03..411eccb644aff7c9d7a8209dfcdc696d137f8f3d 100644 (file)
@@ -53,6 +53,8 @@ public:
     static bool isSnappyCompressed(const std::string &filename);
     static File *createZLib(void);
     static File *createSnappy(void);
+    static File *createForRead(const char *filename);
+    static File *createForWrite(const char *filename);
 public:
     File(const std::string &filename = std::string(),
          File::Mode mode = File::Read);
diff --git a/common/trace_file_read.cpp b/common/trace_file_read.cpp
new file mode 100644 (file)
index 0000000..68bc74a
--- /dev/null
@@ -0,0 +1,59 @@
+/**************************************************************************
+ *
+ * Copyright 2011 Jose Fonseca
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ **************************************************************************/
+
+
+#include "os.hpp"
+#include "trace_file.hpp"
+
+
+using namespace trace;
+
+
+File *
+File::createForRead(const char *filename)
+{
+    File *file;
+
+    if (File::isSnappyCompressed(filename)) {
+        file = File::createSnappy();
+    } else if (File::isZLibCompressed(filename)) {
+        file = File::createZLib();
+    } else  {
+        os::log("error: could not determine %s compression type\n", filename);
+        return NULL;
+    }
+
+    if (!file) {
+        return NULL;
+    }
+
+    if (!file->open(filename, File::Read)) {
+        os::log("error: could not open %s for reading\n", filename);
+        delete file;
+        return NULL;
+    }
+
+    return file;
+}
diff --git a/common/trace_file_write.cpp b/common/trace_file_write.cpp
new file mode 100644 (file)
index 0000000..4cc8984
--- /dev/null
@@ -0,0 +1,50 @@
+/**************************************************************************
+ *
+ * Copyright 2011 Jose Fonseca
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ **************************************************************************/
+
+
+#include "os.hpp"
+#include "trace_file.hpp"
+
+
+using namespace trace;
+
+
+File *
+File::createForWrite(const char *filename)
+{
+    File *file;
+    file = File::createSnappy();
+    if (!file) {
+        return NULL;
+    }
+
+    if (!file->open(filename, File::Write)) {
+        os::log("error: could not open %s for writing\n", filename);
+        delete file;
+        return NULL;
+    }
+
+    return file;
+}
index 7556b14a759db76a0e0c26a5ce1260344252008e..aabf388121cb73232f85f67d242fe8e439ed56df 100644 (file)
@@ -52,13 +52,8 @@ Parser::~Parser() {
 
 bool Parser::open(const char *filename) {
     assert(!file);
-    if (File::isZLibCompressed(filename)) {
-        file = File::createZLib();
-    } else {
-        file = File::createSnappy();
-    }
-
-    if (!file->open(filename, File::Read)) {
+    file = File::createForRead(filename);
+    if (!file) {
         return false;
     }
 
diff --git a/scripts/tracerepack.py b/scripts/tracerepack.py
deleted file mode 100755 (executable)
index 028eb18..0000000
+++ /dev/null
@@ -1,75 +0,0 @@
-#!/usr/bin/env python
-##########################################################################
-#
-# Copyright 2011 Jose Fonseca
-# All Rights Reserved.
-#
-# Permission is hereby granted, free of charge, to any person obtaining a copy
-# of this software and associated documentation files (the 'Software'), to deal
-# in the Software without restriction, including without limitation the rights
-# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-# copies of the Software, and to permit persons to whom the Software is
-# furnished to do so, subject to the following conditions:
-#
-# The above copyright notice and this permission notice shall be included in
-# all copies or substantial portions of the Software.
-#
-# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-# THE SOFTWARE.
-#
-##########################################################################/
-
-'''Script to recompress a trace.
-'''
-
-
-import optparse
-import os.path
-import sys
-import gzip
-import tempfile
-import shutil
-
-
-def repack(in_name):
-    mtime = os.path.getmtime(in_name)
-    out_name = tempfile.mktemp()
-
-    in_stream = gzip.GzipFile(in_name, 'rb')
-    out_stream = gzip.GzipFile(out_name, 'wb', compresslevel=9, mtime=mtime)
-    
-    shutil.copyfileobj(in_stream, out_stream)
-    
-    in_stream.close()
-    out_stream.close()
-    
-    in_size = os.path.getsize(in_name)
-    out_size = os.path.getsize(out_name)
-
-    print '%u -> %u' % (in_size, out_size)
-    
-    if out_size < in_size:
-        shutil.move(out_name, in_name)
-    else:
-        os.unlink(out_name)
-
-
-def main():
-    optparser = optparse.OptionParser(
-        usage='\n\t%prog <trace> ...',
-        version='%%prog')
-
-    (options, args) = optparser.parse_args(sys.argv[1:])
-    if not args:
-        optparser.error("incorrect number of arguments")
-
-    map(repack, args)
-
-
-if __name__ == '__main__':
-    main()