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 **********************************************************************/
30 #define _FILE_OFFSET_BITS 64
36 #include "igzip_lib.h"
40 #define MIN_TEST_LOOPS 10
42 # define RUN_MEM_SIZE 500000000
45 int level_size_buf
[10] = {
46 #ifdef ISAL_DEF_LVL0_DEFAULT
47 ISAL_DEF_LVL0_DEFAULT
,
51 #ifdef ISAL_DEF_LVL1_DEFAULT
52 ISAL_DEF_LVL1_DEFAULT
,
56 #ifdef ISAL_DEF_LVL2_DEFAULT
57 ISAL_DEF_LVL2_DEFAULT
,
61 #ifdef ISAL_DEF_LVL3_DEFAULT
62 ISAL_DEF_LVL3_DEFAULT
,
66 #ifdef ISAL_DEF_LVL4_DEFAULT
67 ISAL_DEF_LVL4_DEFAULT
,
71 #ifdef ISAL_DEF_LVL5_DEFAULT
72 ISAL_DEF_LVL5_DEFAULT
,
76 #ifdef ISAL_DEF_LVL6_DEFAULT
77 ISAL_DEF_LVL6_DEFAULT
,
81 #ifdef ISAL_DEF_LVL7_DEFAULT
82 ISAL_DEF_LVL7_DEFAULT
,
86 #ifdef ISAL_DEF_LVL8_DEFAULT
87 ISAL_DEF_LVL8_DEFAULT
,
91 #ifdef ISAL_DEF_LVL9_DEFAULT
92 ISAL_DEF_LVL9_DEFAULT
,
98 struct isal_zstream stream
;
103 "Usage: igzip_stateless_file_perf [options] <infile>\n"
105 " -X use compression level X with 0 <= X <= 2\n"
106 " -i <iter> number of iterations (at least 1)\n"
107 " -o <file> output file for compresed data\n"
108 " -w <size> log base 2 size of history window, between 8 and 15\n");
112 int main(int argc
, char *argv
[])
114 FILE *in
= NULL
, *out
= NULL
;
115 unsigned char *inbuf
, *outbuf
, *level_buf
= NULL
;
116 int i
, c
, iterations
= 0;
117 uint64_t infile_size
, outbuf_size
;
118 struct isal_huff_histogram histogram
;
119 struct isal_hufftables hufftables_custom
;
120 int level
= 0, level_size
= 0;
121 char *in_file_name
= NULL
, *out_file_name
= NULL
;
122 uint32_t hist_bits
= 0;
124 while ((c
= getopt(argc
, argv
, "h0123456789i:o:w:")) != -1) {
125 if (c
>= '0' && c
<= '9') {
126 if (c
> '0' + ISAL_DEF_MAX_LEVEL
)
130 level_size
= level_size_buf
[level
];
137 out_file_name
= optarg
;
140 iterations
= atoi(optarg
);
145 hist_bits
= atoi(optarg
);
146 if (hist_bits
> 15 || hist_bits
< 8)
157 in_file_name
= argv
[optind
];
158 in
= fopen(in_file_name
, "rb");
163 fprintf(stderr
, "Can't open %s for reading\n", in_file_name
);
166 if (out_file_name
!= NULL
) {
167 out
= fopen(out_file_name
, "wb");
169 fprintf(stderr
, "Can't open %s for writing\n", out_file_name
);
172 printf("outfile=%s\n", out_file_name
);
176 printf("Window Size: %d K\n", IGZIP_HIST_SIZE
/ 1024);
178 else if (hist_bits
< 10)
179 printf("Window Size: %.2f K\n", 1.0 * (1 << hist_bits
) / 1024);
181 printf("Window Size: %d K\n", (1 << hist_bits
) / 1024);
183 printf("igzip_file_perf: \n");
185 /* Allocate space for entire input file and output
186 * (assuming some possible expansion on output size)
188 infile_size
= get_filesize(in
);
190 outbuf_size
= infile_size
* 1.07 + BUF_SIZE
;
192 if (iterations
== 0) {
193 iterations
= infile_size
? RUN_MEM_SIZE
/ infile_size
: MIN_TEST_LOOPS
;
194 if (iterations
< MIN_TEST_LOOPS
)
195 iterations
= MIN_TEST_LOOPS
;
198 inbuf
= malloc(infile_size
);
200 fprintf(stderr
, "Can't allocate input buffer memory\n");
203 outbuf
= malloc(outbuf_size
);
204 if (outbuf
== NULL
) {
205 fprintf(stderr
, "Can't allocate output buffer memory\n");
209 if (level_size
!= 0) {
210 level_buf
= malloc(level_size
);
211 if (level_buf
== NULL
) {
212 fprintf(stderr
, "Can't allocate level buffer memory\n");
217 printf("igzip_file_perf: %s %d iterations\n", in_file_name
, iterations
);
218 /* Read complete input file into buffer */
219 stream
.avail_in
= (uint32_t) fread(inbuf
, 1, infile_size
, in
);
220 if (stream
.avail_in
!= infile_size
) {
221 fprintf(stderr
, "Couldn't fit all of input file into buffer\n");
225 struct perf start
, stop
;
228 for (i
= 0; i
< iterations
; i
++) {
229 isal_deflate_init(&stream
);
230 stream
.end_of_stream
= 1; /* Do the entire file at once */
231 stream
.flush
= NO_FLUSH
;
232 stream
.next_in
= inbuf
;
233 stream
.avail_in
= infile_size
;
234 stream
.next_out
= outbuf
;
235 stream
.avail_out
= outbuf_size
;
236 stream
.level
= level
;
237 stream
.level_buf
= level_buf
;
238 stream
.level_buf_size
= level_size
;
239 stream
.hist_bits
= hist_bits
;
240 isal_deflate_stateless(&stream
);
241 if (stream
.avail_in
!= 0)
246 if (stream
.avail_in
!= 0) {
247 fprintf(stderr
, "Could not compress all of inbuf\n");
251 printf(" file %s - in_size=%lu out_size=%d iter=%d ratio=%3.1f%%", in_file_name
,
252 infile_size
, stream
.total_out
, i
, 100.0 * stream
.total_out
/ infile_size
);
255 memset(&histogram
, 0, sizeof(histogram
));
257 isal_update_histogram(inbuf
, infile_size
, &histogram
);
258 isal_create_hufftables(&hufftables_custom
, &histogram
);
260 isal_deflate_init(&stream
);
261 stream
.end_of_stream
= 1; /* Do the entire file at once */
262 stream
.flush
= NO_FLUSH
;
263 stream
.next_in
= inbuf
;
264 stream
.avail_in
= infile_size
;
265 stream
.next_out
= outbuf
;
266 stream
.avail_out
= outbuf_size
;
267 stream
.level
= level
;
268 stream
.level_buf
= level_buf
;
269 stream
.level_buf_size
= level_size
;
270 stream
.hufftables
= &hufftables_custom
;
271 stream
.hist_bits
= hist_bits
;
272 isal_deflate_stateless(&stream
);
274 printf(" ratio_custom=%3.1f%%", 100.0 * stream
.total_out
/ infile_size
);
278 printf("igzip_file: ");
279 perf_print(stop
, start
, (long long)infile_size
* i
);
281 if (argc
> 2 && out
) {
282 printf("writing %s\n", out_file_name
);
283 fwrite(outbuf
, 1, stream
.total_out
, out
);
288 printf("End of igzip_file_perf\n\n");