1 /**********************************************************************
2 Copyright(c) 2011-2016 Intel Corporation All rights reserved.
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in
11 the documentation and/or other materials provided with the
13 * Neither the name of Intel Corporation nor the names of its
14 contributors may be used to endorse or promote products derived
15 from this software without specific prior written permission.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 **********************************************************************/
34 #include "igzip_lib.h"
37 #define MIN_BUF_SIZE (4 * 1024)
38 #define MIN_TEST_LOOPS 10
40 # define RUN_MEM_SIZE 500000000
43 #define DEFAULT_SEG_SIZE (512 * 1024)
44 #define DEFAULT_SAMPLE_SIZE (32 * 1024)
49 "Usage: igzip_semi_dynamic [options] <infile>\n"
51 " -v (don't) validate output by inflate and compare\n"
52 " -i <iter> iterations\n"
53 " -t <type> 1:stateless 0:(default)stateful\n"
54 " -c <size> chunk size default=%d\n"
55 " -s <size> sample size default=%d\n"
56 " -o <file> output file\n", DEFAULT_SEG_SIZE
, DEFAULT_SAMPLE_SIZE
);
65 int len
= strnlen(s
, ARG_MAX
);
66 if (len
< 2 || len
== ARG_MAX
)
83 i
*= (1024 * 1024 * 1024);
86 i
*= (1000 * 1000 * 1000);
92 int get_filesize(FILE * f
)
96 curr
= ftell(f
); /* Save current position */
97 fseek(f
, 0L, SEEK_END
);
99 fseek(f
, curr
, SEEK_SET
); /* Restore position */
103 int main(int argc
, char *argv
[])
105 FILE *in
= stdin
, *out
= NULL
;
106 unsigned char *inbuf
, *outbuf
;
107 int i
= 0, c
, infile_size
, outbuf_size
;
108 int segment_size
= DEFAULT_SEG_SIZE
;
109 int sample_size
= DEFAULT_SAMPLE_SIZE
;
110 int check_output
= 1;
111 int iterations
= 0, do_stateless
= 0, do_stateful
= 1;
113 char *out_file_name
= NULL
;
114 struct isal_zstream stream
;
115 struct isal_huff_histogram histogram
;
116 struct isal_hufftables hufftable
;
118 while ((c
= getopt(argc
, argv
, "vht:c:s:o:i:")) != -1) {
124 if (atoi(optarg
) == 1) {
130 segment_size
= str_to_i(optarg
);
133 sample_size
= str_to_i(optarg
);
136 out_file_name
= optarg
;
139 iterations
= str_to_i(optarg
);
150 if (!(in
= fopen(argv
[optind
], "rb"))) {
151 fprintf(stderr
, "Can't open %s for reading\n", argv
[optind
]);
157 // Optionally open output file
158 if (out_file_name
!= NULL
) {
159 if (!(out
= fopen(out_file_name
, "wb"))) {
160 fprintf(stderr
, "Can't open %s for writing\n", out_file_name
);
165 printf("Window Size: %d K\n", IGZIP_HIST_SIZE
/ 1024);
168 * Allocate space for entire input file and output
169 * (assuming some possible expansion on output size)
171 infile_size
= get_filesize(in
);
172 if (infile_size
== 0) {
173 printf("Input file has zero length\n");
177 if (iterations
== 0) {
178 iterations
= RUN_MEM_SIZE
/ infile_size
;
179 if (iterations
< MIN_TEST_LOOPS
)
180 iterations
= MIN_TEST_LOOPS
;
183 outbuf_size
= infile_size
* 1.30 > MIN_BUF_SIZE
? infile_size
* 1.30 : MIN_BUF_SIZE
;
185 if (NULL
== (inbuf
= malloc(infile_size
))) {
186 fprintf(stderr
, "Can't allocate input buffer memory\n");
189 if (NULL
== (outbuf
= malloc(outbuf_size
))) {
190 fprintf(stderr
, "Can't allocate output buffer memory\n");
194 int hist_size
= sample_size
> segment_size
? segment_size
: sample_size
;
196 printf("semi-dynamic sample=%d segment=%d %s\n", hist_size
, segment_size
,
197 do_stateful
? "stateful" : "stateless");
198 printf("igzip_file_perf: %s %d iterations\n", argv
[optind
], iterations
);
200 // Read complete input file into buffer
201 stream
.avail_in
= (uint32_t) fread(inbuf
, 1, infile_size
, in
);
202 if (stream
.avail_in
!= infile_size
) {
203 fprintf(stderr
, "Couldn't fit all of input file into buffer\n");
207 struct perf start
, stop
;
212 for (i
= 0; i
< iterations
; i
++) {
213 isal_deflate_init(&stream
);
214 stream
.end_of_stream
= 0;
215 stream
.flush
= SYNC_FLUSH
;
216 stream
.next_in
= inbuf
;
217 stream
.next_out
= outbuf
;
218 stream
.avail_out
= outbuf_size
;
219 int remaining
= infile_size
;
220 int chunk_size
= segment_size
;
222 while (remaining
> 0) {
223 // Generate custom hufftables on sample
224 memset(&histogram
, 0, sizeof(struct isal_huff_histogram
));
225 if (remaining
< segment_size
* 2) {
226 chunk_size
= remaining
;
227 stream
.end_of_stream
= 1;
230 (hist_size
> chunk_size
) ? chunk_size
: hist_size
;
231 isal_update_histogram(stream
.next_in
, hist_rem
, &histogram
);
233 if (hist_rem
== chunk_size
)
234 isal_create_hufftables_subset(&hufftable
, &histogram
);
236 isal_create_hufftables(&hufftable
, &histogram
);
238 // Compress with custom table
239 stream
.avail_in
= chunk_size
;
240 stream
.hufftables
= &hufftable
;
241 remaining
-= chunk_size
;
242 isal_deflate(&stream
);
243 if (stream
.internal_state
.state
!= ZSTATE_NEW_HDR
)
253 for (i
= 0; i
< iterations
; i
++) {
254 isal_deflate_stateless_init(&stream
);
255 stream
.end_of_stream
= 0;
256 stream
.flush
= FULL_FLUSH
;
257 stream
.next_in
= inbuf
;
258 stream
.next_out
= outbuf
;
259 int remaining
= infile_size
;
260 int chunk_size
= segment_size
;
262 while (remaining
> 0) {
263 // Generate custom hufftables on sample
264 memset(&histogram
, 0, sizeof(struct isal_huff_histogram
));
265 if (remaining
< segment_size
* 2) {
266 chunk_size
= remaining
;
267 stream
.end_of_stream
= 1;
270 (hist_size
> chunk_size
) ? chunk_size
: hist_size
;
271 isal_update_histogram(stream
.next_in
, hist_rem
, &histogram
);
273 if (hist_rem
== chunk_size
)
274 isal_create_hufftables_subset(&hufftable
, &histogram
);
276 isal_create_hufftables(&hufftable
, &histogram
);
278 // Compress with custom table
279 stream
.avail_in
= chunk_size
;
280 stream
.avail_out
= chunk_size
+ 8 * (1 + (chunk_size
>> 16));
281 stream
.hufftables
= &hufftable
;
282 remaining
-= chunk_size
;
283 isal_deflate_stateless(&stream
);
284 if (stream
.avail_in
!= 0)
291 if (stream
.avail_in
!= 0) {
292 printf("Could not compress all of inbuf\n");
296 printf(" file %s - in_size=%d out_size=%d iter=%d ratio=%3.1f%%\n", argv
[optind
],
297 infile_size
, stream
.total_out
, i
, 100.0 * stream
.total_out
/ infile_size
);
299 printf("igzip_file: ");
300 perf_print(stop
, start
, (long long)infile_size
* i
);
303 printf("writing %s\n", out_file_name
);
304 fwrite(outbuf
, 1, stream
.total_out
, out
);
311 unsigned char *inflate_buf
;
312 struct inflate_state istate
;
314 if (NULL
== (inflate_buf
= malloc(infile_size
))) {
315 fprintf(stderr
, "Can't allocate reconstruct buffer memory\n");
318 isal_inflate_init(&istate
);
319 istate
.next_in
= outbuf
;
320 istate
.avail_in
= stream
.total_out
;
321 istate
.next_out
= inflate_buf
;
322 istate
.avail_out
= infile_size
;
323 int check
= isal_inflate(&istate
);
325 if (memcmp(inflate_buf
, inbuf
, infile_size
)) {
326 printf("inflate check Fail\n");
327 printf(" ret %d total_inflate=%d\n", check
, istate
.total_out
);
328 for (i
= 0; i
< infile_size
; i
++) {
329 if (inbuf
[i
] != inflate_buf
[i
]) {
330 printf(" first diff at offset=%d\n", i
);
336 printf("inflate check Pass\n");
340 printf("End of igzip_semi_dyn_file_perf\n\n");