blob: 5f294283f75b06f4513df83b5c04ce447cbdfad0 [file] [log] [blame]
José Fonsecaa3285532011-11-27 12:32:00 +00001/**************************************************************************
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
Jose Fonseca038054b2017-03-23 11:52:20 +000027#include <assert.h>
José Fonsecaa3285532011-11-27 12:32:00 +000028#include <string.h>
José Fonsecab1b5a382012-04-20 22:49:20 +010029#include <getopt.h>
30
José Fonsecaa3285532011-11-27 12:32:00 +000031#include <iostream>
Jose Fonseca7c5a77c2016-03-29 12:13:23 +010032#include <memory>
José Fonsecaa3285532011-11-27 12:32:00 +000033
34#include "cli.hpp"
35
Jose Fonseca038054b2017-03-23 11:52:20 +000036#include <brotli/enc/compressor.h>
Jose Fonseca7c5a77c2016-03-29 12:13:23 +010037#include <zlib.h> // for crc32
Jose Fonsecaa09b4172016-03-25 09:34:06 +000038
José Fonsecaa3285532011-11-27 12:32:00 +000039#include "trace_file.hpp"
Jose Fonsecace2ed372015-11-07 23:01:11 +000040#include "trace_ostream.hpp"
José Fonsecaa3285532011-11-27 12:32:00 +000041
42
Jose Fonsecae76ff4b2015-11-07 23:47:40 +000043static const char *synopsis = "Repack a trace file with different compression.";
José Fonsecaa3285532011-11-27 12:32:00 +000044
45static void
46usage(void)
47{
48 std::cout
Jose Fonsecae76ff4b2015-11-07 23:47:40 +000049 << "usage: apitrace repack [options] <in-trace-file> <out-trace-file>\n"
José Fonsecaa3285532011-11-27 12:32:00 +000050 << synopsis << "\n"
51 << "\n"
52 << "Snappy compression allows for faster replay and smaller memory footprint,\n"
53 << "at the expense of a slightly smaller compression ratio than zlib\n"
Jose Fonsecae76ff4b2015-11-07 23:47:40 +000054 << "\n"
Jose Fonsecaa09b4172016-03-25 09:34:06 +000055 << " -b,--brotli Use Brotli compression\n"
56 << " -z,--zlib Use ZLib compression\n"
José Fonsecaa3285532011-11-27 12:32:00 +000057 << "\n";
58}
59
José Fonsecab1b5a382012-04-20 22:49:20 +010060const static char *
Jose Fonsecaa09b4172016-03-25 09:34:06 +000061shortOptions = "hbz";
José Fonsecab1b5a382012-04-20 22:49:20 +010062
63const static struct option
64longOptions[] = {
65 {"help", no_argument, 0, 'h'},
Jose Fonseca796d3bc2016-03-25 22:32:25 +000066 {"brotli", optional_argument, 0, 'b'},
Jose Fonsecae76ff4b2015-11-07 23:47:40 +000067 {"zlib", no_argument, 0, 'z'},
José Fonsecab1b5a382012-04-20 22:49:20 +010068 {0, 0, 0, 0}
69};
70
Jose Fonsecae76ff4b2015-11-07 23:47:40 +000071enum Format {
72 FORMAT_SNAPPY = 0,
73 FORMAT_ZLIB,
Jose Fonsecaa09b4172016-03-25 09:34:06 +000074 FORMAT_BROTLI,
Jose Fonsecae76ff4b2015-11-07 23:47:40 +000075};
76
Jose Fonsecaa09b4172016-03-25 09:34:06 +000077
78class BrotliTraceIn : public brotli::BrotliIn
José Fonsecaa3285532011-11-27 12:32:00 +000079{
Jose Fonsecaa09b4172016-03-25 09:34:06 +000080private:
81 trace::File *stream;
82 char buf[1 << 16];
83 bool eof = false;
84
85public:
Jose Fonseca7c5a77c2016-03-29 12:13:23 +010086 uLong crc;
87
Jose Fonsecaa09b4172016-03-25 09:34:06 +000088 BrotliTraceIn(trace::File *s) :
89 stream(s)
90 {
Jose Fonseca7c5a77c2016-03-29 12:13:23 +010091 crc = crc32(0L, Z_NULL, 0);
José Fonsecaa3285532011-11-27 12:32:00 +000092 }
93
Jose Fonsecaa09b4172016-03-25 09:34:06 +000094 ~BrotliTraceIn() {
José Fonsecaa3285532011-11-27 12:32:00 +000095 }
96
Jose Fonsecaa09b4172016-03-25 09:34:06 +000097 const void *
98 Read(size_t n, size_t* bytes_read) override
99 {
100 if (n > sizeof buf) {
101 n = sizeof buf;
102 } else if (n == 0) {
103 return eof ? nullptr : buf;
104 }
105 *bytes_read = stream->read(buf, n);
106 if (*bytes_read == 0) {
107 eof = true;
108 return nullptr;
109 }
Jose Fonseca7c5a77c2016-03-29 12:13:23 +0100110 crc32(crc, reinterpret_cast<const Bytef *>(buf), *bytes_read);
Jose Fonsecaa09b4172016-03-25 09:34:06 +0000111 return buf;
112 }
113};
114
115
116static int
117repack_generic(trace::File *inFile, trace::OutStream *outFile)
118{
119 const size_t size = 8192;
José Fonsecaa3285532011-11-27 12:32:00 +0000120 char *buf = new char[size];
121 size_t read;
122
123 while ((read = inFile->read(buf, size)) != 0) {
124 outFile->write(buf, read);
125 }
126
127 delete [] buf;
Jose Fonsecaa09b4172016-03-25 09:34:06 +0000128
129 return EXIT_SUCCESS;
130}
131
132
133static int
Jose Fonseca796d3bc2016-03-25 22:32:25 +0000134repack_brotli(trace::File *inFile, const char *outFileName, int quality)
Jose Fonsecaa09b4172016-03-25 09:34:06 +0000135{
136 brotli::BrotliParams params;
137
Jose Fonsecad2de4e32016-03-29 12:12:22 +0100138 // Brotli default quality is 11, but there are problems using quality
139 // higher than 9:
140 //
141 // - Some traces cause compression to be extremely slow. Possibly the same
142 // issue as https://github.com/google/brotli/issues/330
143 // - Some traces get lower compression ratio with 11 than 9. Possibly the
144 // same issue as https://github.com/google/brotli/issues/222
145 params.quality = 9;
146
147 // The larger the window, the higher the compression ratio and
148 // decompression speeds, so choose the maximum.
149 params.lgwin = 24;
150
Jose Fonseca796d3bc2016-03-25 22:32:25 +0000151 if (quality > 0) {
152 params.quality = quality;
153 }
154
Jose Fonsecaa09b4172016-03-25 09:34:06 +0000155 BrotliTraceIn in(inFile);
156 FILE *fout = fopen(outFileName, "wb");
157 if (!fout) {
158 return EXIT_FAILURE;
159 }
160 assert(fout);
161 brotli::BrotliFileOut out(fout);
162 if (!BrotliCompress(params, &in, &out)) {
163 std::cerr << "error: brotli compression failed\n";
164 return EXIT_FAILURE;
165 }
166 fclose(fout);
167
Jose Fonseca7c5a77c2016-03-29 12:13:23 +0100168 std::unique_ptr<trace::File> outFileIn(trace::File::createBrotli());
169 if (!outFileIn->open(outFileName)) {
170 std::cerr << "error: failed to open " << outFileName << " for reading\n";
171 return EXIT_FAILURE;
172 }
173 BrotliTraceIn outIn(outFileIn.get());
174 size_t bytes_read;
175 do {
176 outIn.Read(65536, &bytes_read);
177 } while (bytes_read > 0);
178
179 if (in.crc != outIn.crc) {
180 std::cerr << "error: CRC mismatch reading " << outFileName << "\n";
181 return EXIT_FAILURE;
182 }
183
Jose Fonsecaa09b4172016-03-25 09:34:06 +0000184 return EXIT_SUCCESS;
185}
186
187static int
Jose Fonseca796d3bc2016-03-25 22:32:25 +0000188repack(const char *inFileName, const char *outFileName, Format format, int quality)
Jose Fonsecaa09b4172016-03-25 09:34:06 +0000189{
190 int ret = EXIT_FAILURE;
191
192 trace::File *inFile = trace::File::createForRead(inFileName);
193 if (!inFile) {
194 return 1;
195 }
196
197 trace::OutStream *outFile = nullptr;
198 if (format == FORMAT_SNAPPY) {
199 outFile = trace::createSnappyStream(outFileName);
200 } else if (format == FORMAT_BROTLI) {
Jose Fonseca796d3bc2016-03-25 22:32:25 +0000201 ret = repack_brotli(inFile, outFileName, quality);
Jose Fonsecaa09b4172016-03-25 09:34:06 +0000202 delete inFile;
203 return ret;
204 } else if (format == FORMAT_ZLIB) {
205 outFile = trace::createZLibStream(outFileName);
206 }
207 if (outFile) {
208 ret = repack_generic(inFile, outFile);
209 delete outFile;
210 }
211
José Fonsecaa3285532011-11-27 12:32:00 +0000212 delete inFile;
213
Jose Fonsecaa09b4172016-03-25 09:34:06 +0000214 return ret;
José Fonsecaa3285532011-11-27 12:32:00 +0000215}
216
217static int
218command(int argc, char *argv[])
219{
Jose Fonsecae76ff4b2015-11-07 23:47:40 +0000220 Format format = FORMAT_SNAPPY;
José Fonsecab1b5a382012-04-20 22:49:20 +0100221 int opt;
Jose Fonseca796d3bc2016-03-25 22:32:25 +0000222 int quality = -1;
José Fonsecab1b5a382012-04-20 22:49:20 +0100223 while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
224 switch (opt) {
225 case 'h':
José Fonsecaa3285532011-11-27 12:32:00 +0000226 usage();
227 return 0;
Jose Fonsecaa09b4172016-03-25 09:34:06 +0000228 case 'b':
229 format = FORMAT_BROTLI;
Jose Fonseca796d3bc2016-03-25 22:32:25 +0000230 if (optarg) {
231 quality = atoi(optarg);
232 }
Jose Fonsecaa09b4172016-03-25 09:34:06 +0000233 break;
Jose Fonsecae76ff4b2015-11-07 23:47:40 +0000234 case 'z':
235 format = FORMAT_ZLIB;
236 break;
José Fonsecab1b5a382012-04-20 22:49:20 +0100237 default:
José Fonseca4ce88b82013-10-11 17:24:47 -0700238 std::cerr << "error: unexpected option `" << (char)opt << "`\n";
José Fonsecaa3285532011-11-27 12:32:00 +0000239 usage();
240 return 1;
241 }
242 }
243
José Fonsecab1b5a382012-04-20 22:49:20 +0100244 if (argc != optind + 2) {
José Fonsecaa3285532011-11-27 12:32:00 +0000245 std::cerr << "error: insufficient number of arguments\n";
246 usage();
247 return 1;
248 }
249
Jose Fonseca796d3bc2016-03-25 22:32:25 +0000250 return repack(argv[optind], argv[optind + 1], format, quality);
José Fonsecaa3285532011-11-27 12:32:00 +0000251}
252
253const Command repack_command = {
254 "repack",
255 synopsis,
256 usage,
257 command
258};