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_stateless_file_perf [options] <infile>\n"
51 " -X use compression level X with 0 <= X <= 1\n"
52 " -i <iter> number of iterations (at least 1)\n"
53 " -o <file> output file for compresed data\n");
57 int get_filesize(FILE * f
)
61 curr
= ftell(f
); /* Save current position */
62 fseek(f
, 0L, SEEK_END
);
64 fseek(f
, curr
, SEEK_SET
); /* Restore position */
68 int main(int argc
, char *argv
[])
70 FILE *in
, *out
= NULL
;
71 unsigned char *inbuf
, *outbuf
, *level_buf
= NULL
;
72 int i
, c
, infile_size
, iterations
= 0, outbuf_size
;
73 struct isal_huff_histogram histogram
;
74 struct isal_hufftables hufftables_custom
;
75 int level
= 0, level_size
= 0;
76 char *in_file_name
= NULL
, *out_file_name
= NULL
;
78 while ((c
= getopt(argc
, argv
, "h01i:o:")) != -1) {
81 out_file_name
= optarg
;
84 iterations
= atoi(optarg
);
90 level_size
= ISAL_DEF_LVL1_LARGE
;
102 in_file_name
= argv
[optind
];
103 in
= fopen(in_file_name
, "rb");
108 fprintf(stderr
, "Can't open %s for reading\n", in_file_name
);
111 if (out_file_name
!= NULL
) {
112 out
= fopen(out_file_name
, "wb");
114 fprintf(stderr
, "Can't open %s for writing\n", out_file_name
);
117 printf("outfile=%s\n", out_file_name
);
120 printf("Window Size: %d K\n", IGZIP_HIST_SIZE
/ 1024);
121 printf("igzip_file_perf: \n");
123 /* Allocate space for entire input file and output
124 * (assuming some possible expansion on output size)
126 infile_size
= get_filesize(in
);
128 outbuf_size
= infile_size
* 1.07 + BUF_SIZE
;
130 if (iterations
== 0) {
131 iterations
= infile_size
? RUN_MEM_SIZE
/ infile_size
: MIN_TEST_LOOPS
;
132 if (iterations
< MIN_TEST_LOOPS
)
133 iterations
= MIN_TEST_LOOPS
;
136 inbuf
= malloc(infile_size
);
138 fprintf(stderr
, "Can't allocate input buffer memory\n");
141 outbuf
= malloc(outbuf_size
);
142 if (outbuf
== NULL
) {
143 fprintf(stderr
, "Can't allocate output buffer memory\n");
147 if (level_size
!= 0) {
148 level_buf
= malloc(level_size
);
149 if (level_buf
== NULL
) {
150 fprintf(stderr
, "Can't allocate level buffer memory\n");
155 printf("igzip_file_perf: %s %d iterations\n", in_file_name
, iterations
);
156 /* Read complete input file into buffer */
157 stream
.avail_in
= (uint32_t) fread(inbuf
, 1, infile_size
, in
);
158 if (stream
.avail_in
!= infile_size
) {
159 fprintf(stderr
, "Couldn't fit all of input file into buffer\n");
163 struct perf start
, stop
;
166 for (i
= 0; i
< iterations
; i
++) {
167 isal_deflate_init(&stream
);
168 stream
.end_of_stream
= 1; /* Do the entire file at once */
169 stream
.flush
= NO_FLUSH
;
170 stream
.next_in
= inbuf
;
171 stream
.avail_in
= infile_size
;
172 stream
.next_out
= outbuf
;
173 stream
.avail_out
= outbuf_size
;
174 stream
.level
= level
;
175 stream
.level_buf
= level_buf
;
176 stream
.level_buf_size
= level_size
;
177 isal_deflate_stateless(&stream
);
178 if (stream
.avail_in
!= 0)
183 if (stream
.avail_in
!= 0) {
184 fprintf(stderr
, "Could not compress all of inbuf\n");
188 printf(" file %s - in_size=%d out_size=%d iter=%d ratio=%3.1f%%", in_file_name
,
189 infile_size
, stream
.total_out
, i
, 100.0 * stream
.total_out
/ infile_size
);
192 memset(&histogram
, 0, sizeof(histogram
));
194 isal_update_histogram(inbuf
, infile_size
, &histogram
);
195 isal_create_hufftables(&hufftables_custom
, &histogram
);
197 isal_deflate_init(&stream
);
198 stream
.end_of_stream
= 1; /* Do the entire file at once */
199 stream
.flush
= NO_FLUSH
;
200 stream
.next_in
= inbuf
;
201 stream
.avail_in
= infile_size
;
202 stream
.next_out
= outbuf
;
203 stream
.avail_out
= outbuf_size
;
204 stream
.level
= level
;
205 stream
.level_buf
= level_buf
;
206 stream
.level_buf_size
= level_size
;
207 stream
.hufftables
= &hufftables_custom
;
208 isal_deflate_stateless(&stream
);
210 printf(" ratio_custom=%3.1f%%", 100.0 * stream
.total_out
/ infile_size
);
214 printf("igzip_file: ");
215 perf_print(stop
, start
, (long long)infile_size
* i
);
217 if (argc
> 2 && out
) {
218 printf("writing %s\n", out_file_name
);
219 fwrite(outbuf
, 1, stream
.total_out
, out
);
224 printf("End of igzip_file_perf\n\n");