José Fonseca | a328553 | 2011-11-27 12:32:00 +0000 | [diff] [blame] | 1 | /************************************************************************** |
| 2 | * |
| 3 | * Copyright 2011 Jose Fonseca |
| 4 | * All Rights Reserved. |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | * |
| 24 | **************************************************************************/ |
| 25 | |
| 26 | |
| 27 | #include <string.h> |
José Fonseca | b1b5a38 | 2012-04-20 22:49:20 +0100 | [diff] [blame] | 28 | #include <getopt.h> |
| 29 | |
José Fonseca | a328553 | 2011-11-27 12:32:00 +0000 | [diff] [blame] | 30 | #include <iostream> |
| 31 | |
| 32 | #include "cli.hpp" |
| 33 | |
Jose Fonseca | a09b417 | 2016-03-25 09:34:06 +0000 | [diff] [blame] | 34 | #include <brotli/enc/encode.h> |
| 35 | |
José Fonseca | a328553 | 2011-11-27 12:32:00 +0000 | [diff] [blame] | 36 | #include "trace_file.hpp" |
Jose Fonseca | ce2ed37 | 2015-11-07 23:01:11 +0000 | [diff] [blame] | 37 | #include "trace_ostream.hpp" |
José Fonseca | a328553 | 2011-11-27 12:32:00 +0000 | [diff] [blame] | 38 | |
| 39 | |
Jose Fonseca | e76ff4b | 2015-11-07 23:47:40 +0000 | [diff] [blame] | 40 | static const char *synopsis = "Repack a trace file with different compression."; |
José Fonseca | a328553 | 2011-11-27 12:32:00 +0000 | [diff] [blame] | 41 | |
| 42 | static void |
| 43 | usage(void) |
| 44 | { |
| 45 | std::cout |
Jose Fonseca | e76ff4b | 2015-11-07 23:47:40 +0000 | [diff] [blame] | 46 | << "usage: apitrace repack [options] <in-trace-file> <out-trace-file>\n" |
José Fonseca | a328553 | 2011-11-27 12:32:00 +0000 | [diff] [blame] | 47 | << synopsis << "\n" |
| 48 | << "\n" |
| 49 | << "Snappy compression allows for faster replay and smaller memory footprint,\n" |
| 50 | << "at the expense of a slightly smaller compression ratio than zlib\n" |
Jose Fonseca | e76ff4b | 2015-11-07 23:47:40 +0000 | [diff] [blame] | 51 | << "\n" |
Jose Fonseca | a09b417 | 2016-03-25 09:34:06 +0000 | [diff] [blame] | 52 | << " -b,--brotli Use Brotli compression\n" |
| 53 | << " -z,--zlib Use ZLib compression\n" |
José Fonseca | a328553 | 2011-11-27 12:32:00 +0000 | [diff] [blame] | 54 | << "\n"; |
| 55 | } |
| 56 | |
José Fonseca | b1b5a38 | 2012-04-20 22:49:20 +0100 | [diff] [blame] | 57 | const static char * |
Jose Fonseca | a09b417 | 2016-03-25 09:34:06 +0000 | [diff] [blame] | 58 | shortOptions = "hbz"; |
José Fonseca | b1b5a38 | 2012-04-20 22:49:20 +0100 | [diff] [blame] | 59 | |
| 60 | const static struct option |
| 61 | longOptions[] = { |
| 62 | {"help", no_argument, 0, 'h'}, |
Jose Fonseca | 796d3bc | 2016-03-25 22:32:25 +0000 | [diff] [blame] | 63 | {"brotli", optional_argument, 0, 'b'}, |
Jose Fonseca | e76ff4b | 2015-11-07 23:47:40 +0000 | [diff] [blame] | 64 | {"zlib", no_argument, 0, 'z'}, |
José Fonseca | b1b5a38 | 2012-04-20 22:49:20 +0100 | [diff] [blame] | 65 | {0, 0, 0, 0} |
| 66 | }; |
| 67 | |
Jose Fonseca | e76ff4b | 2015-11-07 23:47:40 +0000 | [diff] [blame] | 68 | enum Format { |
| 69 | FORMAT_SNAPPY = 0, |
| 70 | FORMAT_ZLIB, |
Jose Fonseca | a09b417 | 2016-03-25 09:34:06 +0000 | [diff] [blame] | 71 | FORMAT_BROTLI, |
Jose Fonseca | e76ff4b | 2015-11-07 23:47:40 +0000 | [diff] [blame] | 72 | }; |
| 73 | |
Jose Fonseca | a09b417 | 2016-03-25 09:34:06 +0000 | [diff] [blame] | 74 | |
| 75 | class BrotliTraceIn : public brotli::BrotliIn |
José Fonseca | a328553 | 2011-11-27 12:32:00 +0000 | [diff] [blame] | 76 | { |
Jose Fonseca | a09b417 | 2016-03-25 09:34:06 +0000 | [diff] [blame] | 77 | private: |
| 78 | trace::File *stream; |
| 79 | char buf[1 << 16]; |
| 80 | bool eof = false; |
| 81 | |
| 82 | public: |
| 83 | BrotliTraceIn(trace::File *s) : |
| 84 | stream(s) |
| 85 | { |
José Fonseca | a328553 | 2011-11-27 12:32:00 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Jose Fonseca | a09b417 | 2016-03-25 09:34:06 +0000 | [diff] [blame] | 88 | ~BrotliTraceIn() { |
José Fonseca | a328553 | 2011-11-27 12:32:00 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Jose Fonseca | a09b417 | 2016-03-25 09:34:06 +0000 | [diff] [blame] | 91 | const void * |
| 92 | Read(size_t n, size_t* bytes_read) override |
| 93 | { |
| 94 | if (n > sizeof buf) { |
| 95 | n = sizeof buf; |
| 96 | } else if (n == 0) { |
| 97 | return eof ? nullptr : buf; |
| 98 | } |
| 99 | *bytes_read = stream->read(buf, n); |
| 100 | if (*bytes_read == 0) { |
| 101 | eof = true; |
| 102 | return nullptr; |
| 103 | } |
| 104 | return buf; |
| 105 | } |
| 106 | }; |
| 107 | |
| 108 | |
| 109 | static int |
| 110 | repack_generic(trace::File *inFile, trace::OutStream *outFile) |
| 111 | { |
| 112 | const size_t size = 8192; |
José Fonseca | a328553 | 2011-11-27 12:32:00 +0000 | [diff] [blame] | 113 | char *buf = new char[size]; |
| 114 | size_t read; |
| 115 | |
| 116 | while ((read = inFile->read(buf, size)) != 0) { |
| 117 | outFile->write(buf, read); |
| 118 | } |
| 119 | |
| 120 | delete [] buf; |
Jose Fonseca | a09b417 | 2016-03-25 09:34:06 +0000 | [diff] [blame] | 121 | |
| 122 | return EXIT_SUCCESS; |
| 123 | } |
| 124 | |
| 125 | |
| 126 | static int |
Jose Fonseca | 796d3bc | 2016-03-25 22:32:25 +0000 | [diff] [blame] | 127 | repack_brotli(trace::File *inFile, const char *outFileName, int quality) |
Jose Fonseca | a09b417 | 2016-03-25 09:34:06 +0000 | [diff] [blame] | 128 | { |
| 129 | brotli::BrotliParams params; |
| 130 | |
Jose Fonseca | d2de4e3 | 2016-03-29 12:12:22 +0100 | [diff] [blame^] | 131 | // Brotli default quality is 11, but there are problems using quality |
| 132 | // higher than 9: |
| 133 | // |
| 134 | // - Some traces cause compression to be extremely slow. Possibly the same |
| 135 | // issue as https://github.com/google/brotli/issues/330 |
| 136 | // - Some traces get lower compression ratio with 11 than 9. Possibly the |
| 137 | // same issue as https://github.com/google/brotli/issues/222 |
| 138 | params.quality = 9; |
| 139 | |
| 140 | // The larger the window, the higher the compression ratio and |
| 141 | // decompression speeds, so choose the maximum. |
| 142 | params.lgwin = 24; |
| 143 | |
Jose Fonseca | 796d3bc | 2016-03-25 22:32:25 +0000 | [diff] [blame] | 144 | if (quality > 0) { |
| 145 | params.quality = quality; |
| 146 | } |
| 147 | |
Jose Fonseca | a09b417 | 2016-03-25 09:34:06 +0000 | [diff] [blame] | 148 | BrotliTraceIn in(inFile); |
| 149 | FILE *fout = fopen(outFileName, "wb"); |
| 150 | if (!fout) { |
| 151 | return EXIT_FAILURE; |
| 152 | } |
| 153 | assert(fout); |
| 154 | brotli::BrotliFileOut out(fout); |
| 155 | if (!BrotliCompress(params, &in, &out)) { |
| 156 | std::cerr << "error: brotli compression failed\n"; |
| 157 | return EXIT_FAILURE; |
| 158 | } |
| 159 | fclose(fout); |
| 160 | |
| 161 | return EXIT_SUCCESS; |
| 162 | } |
| 163 | |
| 164 | static int |
Jose Fonseca | 796d3bc | 2016-03-25 22:32:25 +0000 | [diff] [blame] | 165 | repack(const char *inFileName, const char *outFileName, Format format, int quality) |
Jose Fonseca | a09b417 | 2016-03-25 09:34:06 +0000 | [diff] [blame] | 166 | { |
| 167 | int ret = EXIT_FAILURE; |
| 168 | |
| 169 | trace::File *inFile = trace::File::createForRead(inFileName); |
| 170 | if (!inFile) { |
| 171 | return 1; |
| 172 | } |
| 173 | |
| 174 | trace::OutStream *outFile = nullptr; |
| 175 | if (format == FORMAT_SNAPPY) { |
| 176 | outFile = trace::createSnappyStream(outFileName); |
| 177 | } else if (format == FORMAT_BROTLI) { |
Jose Fonseca | 796d3bc | 2016-03-25 22:32:25 +0000 | [diff] [blame] | 178 | ret = repack_brotli(inFile, outFileName, quality); |
Jose Fonseca | a09b417 | 2016-03-25 09:34:06 +0000 | [diff] [blame] | 179 | delete inFile; |
| 180 | return ret; |
| 181 | } else if (format == FORMAT_ZLIB) { |
| 182 | outFile = trace::createZLibStream(outFileName); |
| 183 | } |
| 184 | if (outFile) { |
| 185 | ret = repack_generic(inFile, outFile); |
| 186 | delete outFile; |
| 187 | } |
| 188 | |
José Fonseca | a328553 | 2011-11-27 12:32:00 +0000 | [diff] [blame] | 189 | delete inFile; |
| 190 | |
Jose Fonseca | a09b417 | 2016-03-25 09:34:06 +0000 | [diff] [blame] | 191 | return ret; |
José Fonseca | a328553 | 2011-11-27 12:32:00 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | static int |
| 195 | command(int argc, char *argv[]) |
| 196 | { |
Jose Fonseca | e76ff4b | 2015-11-07 23:47:40 +0000 | [diff] [blame] | 197 | Format format = FORMAT_SNAPPY; |
José Fonseca | b1b5a38 | 2012-04-20 22:49:20 +0100 | [diff] [blame] | 198 | int opt; |
Jose Fonseca | 796d3bc | 2016-03-25 22:32:25 +0000 | [diff] [blame] | 199 | int quality = -1; |
José Fonseca | b1b5a38 | 2012-04-20 22:49:20 +0100 | [diff] [blame] | 200 | while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) { |
| 201 | switch (opt) { |
| 202 | case 'h': |
José Fonseca | a328553 | 2011-11-27 12:32:00 +0000 | [diff] [blame] | 203 | usage(); |
| 204 | return 0; |
Jose Fonseca | a09b417 | 2016-03-25 09:34:06 +0000 | [diff] [blame] | 205 | case 'b': |
| 206 | format = FORMAT_BROTLI; |
Jose Fonseca | 796d3bc | 2016-03-25 22:32:25 +0000 | [diff] [blame] | 207 | if (optarg) { |
| 208 | quality = atoi(optarg); |
| 209 | } |
Jose Fonseca | a09b417 | 2016-03-25 09:34:06 +0000 | [diff] [blame] | 210 | break; |
Jose Fonseca | e76ff4b | 2015-11-07 23:47:40 +0000 | [diff] [blame] | 211 | case 'z': |
| 212 | format = FORMAT_ZLIB; |
| 213 | break; |
José Fonseca | b1b5a38 | 2012-04-20 22:49:20 +0100 | [diff] [blame] | 214 | default: |
José Fonseca | 4ce88b8 | 2013-10-11 17:24:47 -0700 | [diff] [blame] | 215 | std::cerr << "error: unexpected option `" << (char)opt << "`\n"; |
José Fonseca | a328553 | 2011-11-27 12:32:00 +0000 | [diff] [blame] | 216 | usage(); |
| 217 | return 1; |
| 218 | } |
| 219 | } |
| 220 | |
José Fonseca | b1b5a38 | 2012-04-20 22:49:20 +0100 | [diff] [blame] | 221 | if (argc != optind + 2) { |
José Fonseca | a328553 | 2011-11-27 12:32:00 +0000 | [diff] [blame] | 222 | std::cerr << "error: insufficient number of arguments\n"; |
| 223 | usage(); |
| 224 | return 1; |
| 225 | } |
| 226 | |
Jose Fonseca | 796d3bc | 2016-03-25 22:32:25 +0000 | [diff] [blame] | 227 | return repack(argv[optind], argv[optind + 1], format, quality); |
José Fonseca | a328553 | 2011-11-27 12:32:00 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | const Command repack_command = { |
| 231 | "repack", |
| 232 | synopsis, |
| 233 | usage, |
| 234 | command |
| 235 | }; |