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
cli_diff_state.cpp
cli_diff_images.cpp
cli_dump.cpp
+ cli_repack.cpp
cli_trace.cpp
)
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_ */
&diff_state_command,
&diff_images_command,
&dump_command,
+ &repack_command,
&trace_command,
&help_command
};
std::cerr << "Error: unknown command " << command_name
<< " (see \"apitrace help\").\n";
- return 1;
+ return 1;
}
--- /dev/null
+/**************************************************************************
+ *
+ * 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
+};
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);
--- /dev/null
+/**************************************************************************
+ *
+ * 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;
+}
--- /dev/null
+/**************************************************************************
+ *
+ * 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;
+}
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;
}
+++ /dev/null
-#!/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()