blob: 2e17cdebed59b83b9a02cb6416668c1d69bb73df [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
27#include <string.h>
José Fonsecab1b5a382012-04-20 22:49:20 +010028#include <getopt.h>
29
José Fonsecaa3285532011-11-27 12:32:00 +000030#include <iostream>
Jose Fonseca7c5a77c2016-03-29 12:13:23 +010031#include <memory>
José Fonsecaa3285532011-11-27 12:32:00 +000032
33#include "cli.hpp"
34
Jose Fonsecaa09b4172016-03-25 09:34:06 +000035#include <brotli/enc/encode.h>
Jose Fonseca7c5a77c2016-03-29 12:13:23 +010036#include <zlib.h> // for crc32
Jose Fonsecaa09b4172016-03-25 09:34:06 +000037
José Fonsecaa3285532011-11-27 12:32:00 +000038#include "trace_file.hpp"
Jose Fonsecace2ed372015-11-07 23:01:11 +000039#include "trace_ostream.hpp"
José Fonsecaa3285532011-11-27 12:32:00 +000040
41
Jose Fonsecae76ff4b2015-11-07 23:47:40 +000042static const char *synopsis = "Repack a trace file with different compression.";
José Fonsecaa3285532011-11-27 12:32:00 +000043
44static void
45usage(void)
46{
47 std::cout
Jose Fonsecae76ff4b2015-11-07 23:47:40 +000048 << "usage: apitrace repack [options] <in-trace-file> <out-trace-file>\n"
José Fonsecaa3285532011-11-27 12:32:00 +000049 << synopsis << "\n"
50 << "\n"
51 << "Snappy compression allows for faster replay and smaller memory footprint,\n"
52 << "at the expense of a slightly smaller compression ratio than zlib\n"
Jose Fonsecae76ff4b2015-11-07 23:47:40 +000053 << "\n"
Jose Fonsecaa09b4172016-03-25 09:34:06 +000054 << " -b,--brotli Use Brotli compression\n"
55 << " -z,--zlib Use ZLib compression\n"
José Fonsecaa3285532011-11-27 12:32:00 +000056 << "\n";
57}
58
José Fonsecab1b5a382012-04-20 22:49:20 +010059const static char *
Jose Fonsecaa09b4172016-03-25 09:34:06 +000060shortOptions = "hbz";
José Fonsecab1b5a382012-04-20 22:49:20 +010061
62const static struct option
63longOptions[] = {
64 {"help", no_argument, 0, 'h'},
Jose Fonseca796d3bc2016-03-25 22:32:25 +000065 {"brotli", optional_argument, 0, 'b'},
Jose Fonsecae76ff4b2015-11-07 23:47:40 +000066 {"zlib", no_argument, 0, 'z'},
José Fonsecab1b5a382012-04-20 22:49:20 +010067 {0, 0, 0, 0}
68};
69
Jose Fonsecae76ff4b2015-11-07 23:47:40 +000070enum Format {
71 FORMAT_SNAPPY = 0,
72 FORMAT_ZLIB,
Jose Fonsecaa09b4172016-03-25 09:34:06 +000073 FORMAT_BROTLI,
Jose Fonsecae76ff4b2015-11-07 23:47:40 +000074};
75
Jose Fonsecaa09b4172016-03-25 09:34:06 +000076
77class BrotliTraceIn : public brotli::BrotliIn
José Fonsecaa3285532011-11-27 12:32:00 +000078{
Jose Fonsecaa09b4172016-03-25 09:34:06 +000079private:
80 trace::File *stream;
81 char buf[1 << 16];
82 bool eof = false;
83
84public:
Jose Fonseca7c5a77c2016-03-29 12:13:23 +010085 uLong crc;
86
Jose Fonsecaa09b4172016-03-25 09:34:06 +000087 BrotliTraceIn(trace::File *s) :
88 stream(s)
89 {
Jose Fonseca7c5a77c2016-03-29 12:13:23 +010090 crc = crc32(0L, Z_NULL, 0);
José Fonsecaa3285532011-11-27 12:32:00 +000091 }
92
Jose Fonsecaa09b4172016-03-25 09:34:06 +000093 ~BrotliTraceIn() {
José Fonsecaa3285532011-11-27 12:32:00 +000094 }
95
Jose Fonsecaa09b4172016-03-25 09:34:06 +000096 const void *
97 Read(size_t n, size_t* bytes_read) override
98 {
99 if (n > sizeof buf) {
100 n = sizeof buf;
101 } else if (n == 0) {
102 return eof ? nullptr : buf;
103 }
104 *bytes_read = stream->read(buf, n);
105 if (*bytes_read == 0) {
106 eof = true;
107 return nullptr;
108 }
Jose Fonseca7c5a77c2016-03-29 12:13:23 +0100109 crc32(crc, reinterpret_cast<const Bytef *>(buf), *bytes_read);
Jose Fonsecaa09b4172016-03-25 09:34:06 +0000110 return buf;
111 }
112};
113
114
115static int
116repack_generic(trace::File *inFile, trace::OutStream *outFile)
117{
118 const size_t size = 8192;
José Fonsecaa3285532011-11-27 12:32:00 +0000119 char *buf = new char[size];
120 size_t read;
121
122 while ((read = inFile->read(buf, size)) != 0) {
123 outFile->write(buf, read);
124 }
125
126 delete [] buf;
Jose Fonsecaa09b4172016-03-25 09:34:06 +0000127
128 return EXIT_SUCCESS;
129}
130
131
132static int
Jose Fonseca796d3bc2016-03-25 22:32:25 +0000133repack_brotli(trace::File *inFile, const char *outFileName, int quality)
Jose Fonsecaa09b4172016-03-25 09:34:06 +0000134{
135 brotli::BrotliParams params;
136
Jose Fonsecad2de4e32016-03-29 12:12:22 +0100137 // Brotli default quality is 11, but there are problems using quality
138 // higher than 9:
139 //
140 // - Some traces cause compression to be extremely slow. Possibly the same
141 // issue as https://github.com/google/brotli/issues/330
142 // - Some traces get lower compression ratio with 11 than 9. Possibly the
143 // same issue as https://github.com/google/brotli/issues/222
144 params.quality = 9;
145
146 // The larger the window, the higher the compression ratio and
147 // decompression speeds, so choose the maximum.
148 params.lgwin = 24;
149
Jose Fonseca796d3bc2016-03-25 22:32:25 +0000150 if (quality > 0) {
151 params.quality = quality;
152 }
153
Jose Fonsecaa09b4172016-03-25 09:34:06 +0000154 BrotliTraceIn in(inFile);
155 FILE *fout = fopen(outFileName, "wb");
156 if (!fout) {
157 return EXIT_FAILURE;
158 }
159 assert(fout);
160 brotli::BrotliFileOut out(fout);
161 if (!BrotliCompress(params, &in, &out)) {
162 std::cerr << "error: brotli compression failed\n";
163 return EXIT_FAILURE;
164 }
165 fclose(fout);
166
Jose Fonseca7c5a77c2016-03-29 12:13:23 +0100167 std::unique_ptr<trace::File> outFileIn(trace::File::createBrotli());
168 if (!outFileIn->open(outFileName)) {
169 std::cerr << "error: failed to open " << outFileName << " for reading\n";
170 return EXIT_FAILURE;
171 }
172 BrotliTraceIn outIn(outFileIn.get());
173 size_t bytes_read;
174 do {
175 outIn.Read(65536, &bytes_read);
176 } while (bytes_read > 0);
177
178 if (in.crc != outIn.crc) {
179 std::cerr << "error: CRC mismatch reading " << outFileName << "\n";
180 return EXIT_FAILURE;
181 }
182
Jose Fonsecaa09b4172016-03-25 09:34:06 +0000183 return EXIT_SUCCESS;
184}
185
186static int
Jose Fonseca796d3bc2016-03-25 22:32:25 +0000187repack(const char *inFileName, const char *outFileName, Format format, int quality)
Jose Fonsecaa09b4172016-03-25 09:34:06 +0000188{
189 int ret = EXIT_FAILURE;
190
191 trace::File *inFile = trace::File::createForRead(inFileName);
192 if (!inFile) {
193 return 1;
194 }
195
196 trace::OutStream *outFile = nullptr;
197 if (format == FORMAT_SNAPPY) {
198 outFile = trace::createSnappyStream(outFileName);
199 } else if (format == FORMAT_BROTLI) {
Jose Fonseca796d3bc2016-03-25 22:32:25 +0000200 ret = repack_brotli(inFile, outFileName, quality);
Jose Fonsecaa09b4172016-03-25 09:34:06 +0000201 delete inFile;
202 return ret;
203 } else if (format == FORMAT_ZLIB) {
204 outFile = trace::createZLibStream(outFileName);
205 }
206 if (outFile) {
207 ret = repack_generic(inFile, outFile);
208 delete outFile;
209 }
210
José Fonsecaa3285532011-11-27 12:32:00 +0000211 delete inFile;
212
Jose Fonsecaa09b4172016-03-25 09:34:06 +0000213 return ret;
José Fonsecaa3285532011-11-27 12:32:00 +0000214}
215
216static int
217command(int argc, char *argv[])
218{
Jose Fonsecae76ff4b2015-11-07 23:47:40 +0000219 Format format = FORMAT_SNAPPY;
José Fonsecab1b5a382012-04-20 22:49:20 +0100220 int opt;
Jose Fonseca796d3bc2016-03-25 22:32:25 +0000221 int quality = -1;
José Fonsecab1b5a382012-04-20 22:49:20 +0100222 while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
223 switch (opt) {
224 case 'h':
José Fonsecaa3285532011-11-27 12:32:00 +0000225 usage();
226 return 0;
Jose Fonsecaa09b4172016-03-25 09:34:06 +0000227 case 'b':
228 format = FORMAT_BROTLI;
Jose Fonseca796d3bc2016-03-25 22:32:25 +0000229 if (optarg) {
230 quality = atoi(optarg);
231 }
Jose Fonsecaa09b4172016-03-25 09:34:06 +0000232 break;
Jose Fonsecae76ff4b2015-11-07 23:47:40 +0000233 case 'z':
234 format = FORMAT_ZLIB;
235 break;
José Fonsecab1b5a382012-04-20 22:49:20 +0100236 default:
José Fonseca4ce88b82013-10-11 17:24:47 -0700237 std::cerr << "error: unexpected option `" << (char)opt << "`\n";
José Fonsecaa3285532011-11-27 12:32:00 +0000238 usage();
239 return 1;
240 }
241 }
242
José Fonsecab1b5a382012-04-20 22:49:20 +0100243 if (argc != optind + 2) {
José Fonsecaa3285532011-11-27 12:32:00 +0000244 std::cerr << "error: insufficient number of arguments\n";
245 usage();
246 return 1;
247 }
248
Jose Fonseca796d3bc2016-03-25 22:32:25 +0000249 return repack(argv[optind], argv[optind + 1], format, quality);
José Fonsecaa3285532011-11-27 12:32:00 +0000250}
251
252const Command repack_command = {
253 "repack",
254 synopsis,
255 usage,
256 command
257};