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 **********************************************************************/
35 #include "igzip_lib.h"
39 #define MIN_TEST_LOOPS 10
41 # define RUN_MEM_SIZE 500000000
44 struct isal_zstream stream
;
49 "Usage: igzip_file_perf [options] <infile>\n"
51 " -X use compression level X with 0 <= X <= 1\n"
52 " -b <size> input buffer size, 0 buffers all the input\n"
53 " -i <iter> number of iterations (at least 1)\n"
54 " -o <file> output file for compresed data\n");
58 int get_filesize(FILE * f
)
62 curr
= ftell(f
); /* Save current position */
63 fseek(f
, 0L, SEEK_END
);
65 fseek(f
, curr
, SEEK_SET
); /* Restore position */
69 int main(int argc
, char *argv
[])
71 FILE *in
, *out
= NULL
;
72 unsigned char *inbuf
, *outbuf
, *level_buf
= NULL
;
73 int i
, c
, infile_size
, iterations
= 0, outbuf_size
, inbuf_size
= 0;
74 struct isal_huff_histogram histogram
;
75 struct isal_hufftables hufftables_custom
;
76 int level
= 0, level_size
= 0, avail_in
;
77 char *in_file_name
= NULL
, *out_file_name
= NULL
;
79 while ((c
= getopt(argc
, argv
, "h01i:b:o:")) != -1) {
82 out_file_name
= optarg
;
85 iterations
= atoi(optarg
);
90 inbuf_size
= atoi(optarg
);
94 level_size
= ISAL_DEF_LVL1_LARGE
;
106 in_file_name
= argv
[optind
];
107 in
= fopen(in_file_name
, "rb");
112 fprintf(stderr
, "Can't open %s for reading\n", in_file_name
);
115 if (out_file_name
!= NULL
) {
116 out
= fopen(out_file_name
, "wb");
118 fprintf(stderr
, "Can't open %s for writing\n", out_file_name
);
121 printf("outfile=%s\n", out_file_name
);
124 printf("Window Size: %d K\n", IGZIP_HIST_SIZE
/ 1024);
125 printf("igzip_file_perf: \n");
128 /* Allocate space for entire input file and output
129 * (assuming some possible expansion on output size)
131 infile_size
= get_filesize(in
);
133 outbuf_size
= 2 * infile_size
+ BUF_SIZE
;
135 if (iterations
== 0) {
136 iterations
= infile_size
? RUN_MEM_SIZE
/ infile_size
: MIN_TEST_LOOPS
;
137 if (iterations
< MIN_TEST_LOOPS
)
138 iterations
= MIN_TEST_LOOPS
;
141 inbuf
= malloc(infile_size
);
143 fprintf(stderr
, "Can't allocate input buffer memory\n");
146 outbuf
= malloc(outbuf_size
);
147 if (outbuf
== NULL
) {
148 fprintf(stderr
, "Can't allocate output buffer memory\n");
152 if (level_size
!= 0) {
153 level_buf
= malloc(level_size
);
154 if (level_buf
== NULL
) {
155 fprintf(stderr
, "Can't allocate level buffer memory\n");
160 inbuf_size
= inbuf_size
? inbuf_size
: infile_size
;
162 printf("igzip_file_perf: %s %d iterations\n", in_file_name
, iterations
);
163 /* Read complete input file into buffer */
164 stream
.avail_in
= (uint32_t) fread(inbuf
, 1, infile_size
, in
);
165 if (stream
.avail_in
!= infile_size
) {
166 fprintf(stderr
, "Couldn't fit all of input file into buffer\n");
170 struct perf start
, stop
;
173 for (i
= 0; i
< iterations
; i
++) {
174 isal_deflate_init(&stream
);
175 stream
.end_of_stream
= 0;
176 stream
.flush
= NO_FLUSH
;
177 stream
.level
= level
;
178 stream
.level_buf
= level_buf
;
179 stream
.level_buf_size
= level_size
;
180 stream
.next_out
= outbuf
;
181 stream
.avail_out
= outbuf_size
;
182 stream
.next_in
= inbuf
;
183 avail_in
= infile_size
;
185 while (avail_in
> 0) {
186 stream
.avail_in
= avail_in
>= inbuf_size
? inbuf_size
: avail_in
;
187 avail_in
-= inbuf_size
;
190 stream
.end_of_stream
= 1;
192 isal_deflate(&stream
);
194 if (stream
.avail_in
!= 0)
201 if (stream
.avail_in
!= 0) {
202 fprintf(stderr
, "Could not compress all of inbuf\n");
206 printf(" file %s - in_size=%d out_size=%d iter=%d ratio=%3.1f%%",
207 in_file_name
, infile_size
, stream
.total_out
, i
,
208 100.0 * stream
.total_out
/ infile_size
);
211 memset(&histogram
, 0, sizeof(histogram
));
213 isal_update_histogram(inbuf
, infile_size
, &histogram
);
214 isal_create_hufftables(&hufftables_custom
, &histogram
);
216 isal_deflate_init(&stream
);
217 stream
.end_of_stream
= 0;
218 stream
.flush
= NO_FLUSH
;
219 stream
.level
= level
;
220 stream
.level_buf
= level_buf
;
221 stream
.level_buf_size
= level_size
;
222 stream
.next_out
= outbuf
;
223 stream
.avail_out
= outbuf_size
;
224 stream
.next_in
= inbuf
;
225 stream
.hufftables
= &hufftables_custom
;
226 avail_in
= infile_size
;
228 while (avail_in
> 0) {
229 stream
.avail_in
= avail_in
>= inbuf_size
? inbuf_size
: avail_in
;
230 avail_in
-= inbuf_size
;
233 stream
.end_of_stream
= 1;
235 isal_deflate(&stream
);
237 if (stream
.avail_in
!= 0)
241 printf(" ratio_custom=%3.1f%%", 100.0 * stream
.total_out
/ infile_size
);
245 if (stream
.avail_in
!= 0) {
246 fprintf(stderr
, "Could not compress all of inbuf\n");
250 printf("igzip_file: ");
251 perf_print(stop
, start
, (long long)infile_size
* i
);
253 if (argc
> 2 && out
) {
254 printf("writing %s\n", out_file_name
);
255 fwrite(outbuf
, 1, stream
.total_out
, out
);
260 printf("End of igzip_file_perf\n\n");