diff options
-rw-r--r-- | cli/cli_repack.cpp | 29 | ||||
-rw-r--r-- | common/CMakeLists.txt | 1 | ||||
-rw-r--r-- | common/trace_ostream.hpp | 3 | ||||
-rw-r--r-- | common/trace_ostream_zlib.cpp | 99 |
4 files changed, 126 insertions, 6 deletions
diff --git a/cli/cli_repack.cpp b/cli/cli_repack.cpp index 3e908f54..291f0d96 100644 --- a/cli/cli_repack.cpp +++ b/cli/cli_repack.cpp @@ -35,38 +35,51 @@ #include "trace_ostream.hpp" -static const char *synopsis = "Repack a trace file with Snappy compression."; +static const char *synopsis = "Repack a trace file with different compression."; static void usage(void) { std::cout - << "usage: apitrace repack <in-trace-file> <out-trace-file>\n" + << "usage: apitrace repack [options] <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" + << " -z,--zlib Use ZLib compression instead\n" << "\n"; } const static char * -shortOptions = "h"; +shortOptions = "hz"; const static struct option longOptions[] = { {"help", no_argument, 0, 'h'}, + {"zlib", no_argument, 0, 'z'}, {0, 0, 0, 0} }; +enum Format { + FORMAT_SNAPPY = 0, + FORMAT_ZLIB, +}; + static int -repack(const char *inFileName, const char *outFileName) +repack(const char *inFileName, const char *outFileName, Format format) { trace::File *inFile = trace::File::createForRead(inFileName); if (!inFile) { return 1; } - trace::OutStream *outFile = trace::createSnappyStream(outFileName); + trace::OutStream *outFile; + if (format == FORMAT_SNAPPY) { + outFile = trace::createSnappyStream(outFileName); + } else { + outFile = trace::createZLibStream(outFileName); + } if (!outFile) { delete inFile; return 1; @@ -90,12 +103,16 @@ repack(const char *inFileName, const char *outFileName) static int command(int argc, char *argv[]) { + Format format = FORMAT_SNAPPY; int opt; while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) { switch (opt) { case 'h': usage(); return 0; + case 'z': + format = FORMAT_ZLIB; + break; default: std::cerr << "error: unexpected option `" << (char)opt << "`\n"; usage(); @@ -109,7 +126,7 @@ command(int argc, char *argv[]) return 1; } - return repack(argv[optind], argv[optind + 1]); + return repack(argv[optind], argv[optind + 1], format); } const Command repack_command = { diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 5768d5ba..06220ed2 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -27,6 +27,7 @@ add_convenience_library (common trace_profiler.cpp trace_option.cpp trace_ostream_snappy.cpp + trace_ostream_zlib.cpp ${os} os_backtrace.cpp os_crtdbg.cpp diff --git a/common/trace_ostream.hpp b/common/trace_ostream.hpp index c7557cbd..502905b4 100644 --- a/common/trace_ostream.hpp +++ b/common/trace_ostream.hpp @@ -45,5 +45,8 @@ public: OutStream * createSnappyStream(const char *filename); +OutStream * +createZLibStream(const char *filename); + } /* namespace trace */ diff --git a/common/trace_ostream_zlib.cpp b/common/trace_ostream_zlib.cpp new file mode 100644 index 00000000..54893870 --- /dev/null +++ b/common/trace_ostream_zlib.cpp @@ -0,0 +1,99 @@ +/************************************************************************** + * + * Copyright 2015 VMware, Inc. + * Copyright 2011 Zack Rusin + * 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 "trace_ostream.hpp" + + +#include <assert.h> +#include <string.h> + +#include <zlib.h> + +#include "os.hpp" + +#include <iostream> + + +using namespace trace; + + +class ZLibOutStream : public OutStream { +public: + ZLibOutStream(gzFile file); + virtual ~ZLibOutStream(); + +protected: + virtual bool write(const void *buffer, size_t length); + virtual void close(); + virtual void flush(); +private: + gzFile m_gzFile; +}; + +ZLibOutStream::ZLibOutStream(gzFile file) + : m_gzFile(file) +{ +} + +ZLibOutStream::~ZLibOutStream() +{ + close(); +} + +bool ZLibOutStream::write(const void *buffer, size_t length) +{ + return gzwrite(m_gzFile, buffer, unsigned(length)) != -1; +} + +void ZLibOutStream::close() +{ + if (m_gzFile) { + gzclose(m_gzFile); + m_gzFile = nullptr; + } +} + +void ZLibOutStream::flush() +{ + gzflush(m_gzFile, Z_SYNC_FLUSH); +} + + +OutStream * +trace::createZLibStream(const char *filename) +{ + gzFile file = gzopen(filename, "wb"); + if (!file) { + return nullptr; + } + + // Currently we only use gzip for offline compression, so aim for maximum + // compression + gzsetparams(file, Z_BEST_COMPRESSION, Z_DEFAULT_STRATEGY); + + return new ZLibOutStream(file); +} |