blob: e259661d0956bceebb5f6b0f584d8c9ca620f608 [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>
31
32#include "cli.hpp"
33
Jose Fonsecaa09b4172016-03-25 09:34:06 +000034#include <brotli/enc/encode.h>
35
José Fonsecaa3285532011-11-27 12:32:00 +000036#include "trace_file.hpp"
Jose Fonsecace2ed372015-11-07 23:01:11 +000037#include "trace_ostream.hpp"
José Fonsecaa3285532011-11-27 12:32:00 +000038
39
Jose Fonsecae76ff4b2015-11-07 23:47:40 +000040static const char *synopsis = "Repack a trace file with different compression.";
José Fonsecaa3285532011-11-27 12:32:00 +000041
42static void
43usage(void)
44{
45 std::cout
Jose Fonsecae76ff4b2015-11-07 23:47:40 +000046 << "usage: apitrace repack [options] <in-trace-file> <out-trace-file>\n"
José Fonsecaa3285532011-11-27 12:32:00 +000047 << 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 Fonsecae76ff4b2015-11-07 23:47:40 +000051 << "\n"
Jose Fonsecaa09b4172016-03-25 09:34:06 +000052 << " -b,--brotli Use Brotli compression\n"
53 << " -z,--zlib Use ZLib compression\n"
José Fonsecaa3285532011-11-27 12:32:00 +000054 << "\n";
55}
56
José Fonsecab1b5a382012-04-20 22:49:20 +010057const static char *
Jose Fonsecaa09b4172016-03-25 09:34:06 +000058shortOptions = "hbz";
José Fonsecab1b5a382012-04-20 22:49:20 +010059
60const static struct option
61longOptions[] = {
62 {"help", no_argument, 0, 'h'},
Jose Fonseca796d3bc2016-03-25 22:32:25 +000063 {"brotli", optional_argument, 0, 'b'},
Jose Fonsecae76ff4b2015-11-07 23:47:40 +000064 {"zlib", no_argument, 0, 'z'},
José Fonsecab1b5a382012-04-20 22:49:20 +010065 {0, 0, 0, 0}
66};
67
Jose Fonsecae76ff4b2015-11-07 23:47:40 +000068enum Format {
69 FORMAT_SNAPPY = 0,
70 FORMAT_ZLIB,
Jose Fonsecaa09b4172016-03-25 09:34:06 +000071 FORMAT_BROTLI,
Jose Fonsecae76ff4b2015-11-07 23:47:40 +000072};
73
Jose Fonsecaa09b4172016-03-25 09:34:06 +000074
75class BrotliTraceIn : public brotli::BrotliIn
José Fonsecaa3285532011-11-27 12:32:00 +000076{
Jose Fonsecaa09b4172016-03-25 09:34:06 +000077private:
78 trace::File *stream;
79 char buf[1 << 16];
80 bool eof = false;
81
82public:
83 BrotliTraceIn(trace::File *s) :
84 stream(s)
85 {
José Fonsecaa3285532011-11-27 12:32:00 +000086 }
87
Jose Fonsecaa09b4172016-03-25 09:34:06 +000088 ~BrotliTraceIn() {
José Fonsecaa3285532011-11-27 12:32:00 +000089 }
90
Jose Fonsecaa09b4172016-03-25 09:34:06 +000091 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
109static int
110repack_generic(trace::File *inFile, trace::OutStream *outFile)
111{
112 const size_t size = 8192;
José Fonsecaa3285532011-11-27 12:32:00 +0000113 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 Fonsecaa09b4172016-03-25 09:34:06 +0000121
122 return EXIT_SUCCESS;
123}
124
125
126static int
Jose Fonseca796d3bc2016-03-25 22:32:25 +0000127repack_brotli(trace::File *inFile, const char *outFileName, int quality)
Jose Fonsecaa09b4172016-03-25 09:34:06 +0000128{
129 brotli::BrotliParams params;
130
Jose Fonsecad2de4e32016-03-29 12:12:22 +0100131 // 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 Fonseca796d3bc2016-03-25 22:32:25 +0000144 if (quality > 0) {
145 params.quality = quality;
146 }
147
Jose Fonsecaa09b4172016-03-25 09:34:06 +0000148 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
164static int
Jose Fonseca796d3bc2016-03-25 22:32:25 +0000165repack(const char *inFileName, const char *outFileName, Format format, int quality)
Jose Fonsecaa09b4172016-03-25 09:34:06 +0000166{
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 Fonseca796d3bc2016-03-25 22:32:25 +0000178 ret = repack_brotli(inFile, outFileName, quality);
Jose Fonsecaa09b4172016-03-25 09:34:06 +0000179 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é Fonsecaa3285532011-11-27 12:32:00 +0000189 delete inFile;
190
Jose Fonsecaa09b4172016-03-25 09:34:06 +0000191 return ret;
José Fonsecaa3285532011-11-27 12:32:00 +0000192}
193
194static int
195command(int argc, char *argv[])
196{
Jose Fonsecae76ff4b2015-11-07 23:47:40 +0000197 Format format = FORMAT_SNAPPY;
José Fonsecab1b5a382012-04-20 22:49:20 +0100198 int opt;
Jose Fonseca796d3bc2016-03-25 22:32:25 +0000199 int quality = -1;
José Fonsecab1b5a382012-04-20 22:49:20 +0100200 while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
201 switch (opt) {
202 case 'h':
José Fonsecaa3285532011-11-27 12:32:00 +0000203 usage();
204 return 0;
Jose Fonsecaa09b4172016-03-25 09:34:06 +0000205 case 'b':
206 format = FORMAT_BROTLI;
Jose Fonseca796d3bc2016-03-25 22:32:25 +0000207 if (optarg) {
208 quality = atoi(optarg);
209 }
Jose Fonsecaa09b4172016-03-25 09:34:06 +0000210 break;
Jose Fonsecae76ff4b2015-11-07 23:47:40 +0000211 case 'z':
212 format = FORMAT_ZLIB;
213 break;
José Fonsecab1b5a382012-04-20 22:49:20 +0100214 default:
José Fonseca4ce88b82013-10-11 17:24:47 -0700215 std::cerr << "error: unexpected option `" << (char)opt << "`\n";
José Fonsecaa3285532011-11-27 12:32:00 +0000216 usage();
217 return 1;
218 }
219 }
220
José Fonsecab1b5a382012-04-20 22:49:20 +0100221 if (argc != optind + 2) {
José Fonsecaa3285532011-11-27 12:32:00 +0000222 std::cerr << "error: insufficient number of arguments\n";
223 usage();
224 return 1;
225 }
226
Jose Fonseca796d3bc2016-03-25 22:32:25 +0000227 return repack(argv[optind], argv[optind + 1], format, quality);
José Fonsecaa3285532011-11-27 12:32:00 +0000228}
229
230const Command repack_command = {
231 "repack",
232 synopsis,
233 usage,
234 command
235};