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 **********************************************************************/
32 #include "huff_codes.h"
33 #include "igzip_lib.h"
36 #if defined(ZLIB_9) || defined(ZLIB_1) || defined(ZLIB_COMPARE)
41 #define MIN_TEST_LOOPS 8
43 # define RUN_MEM_SIZE 1000000000
46 int get_filesize(FILE * f
)
50 curr
= ftell(f
); /* Save current position */
51 fseek(f
, 0L, SEEK_END
);
53 fseek(f
, curr
, SEEK_SET
); /* Restore position */
57 int main(int argc
, char *argv
[])
59 FILE *in
, *out
= NULL
;
60 unsigned char *inbuf
, *outbuf
, *tempbuf
;
61 int i
, infile_size
, iterations
, outbuf_size
, check
;
63 struct inflate_state state
;
65 if (argc
> 3 || argc
< 2) {
66 fprintf(stderr
, "Usage: isal_inflate_file_perf infile\n"
67 "\t - Runs multiple iterations of igzip on a file to "
68 "get more accurate time results.\n");
71 in
= fopen(argv
[1], "rb");
73 fprintf(stderr
, "Can't open %s for reading\n", argv
[1]);
77 out
= fopen(argv
[2], "wb");
79 fprintf(stderr
, "Can't open %s for writing\n", argv
[2]);
82 printf("outfile=%s\n", argv
[2]);
84 printf("isal_inflate_perf: \n");
86 /* Allocate space for entire input file and output
87 * (assuming some possible expansion on output size)
89 infile_size
= get_filesize(in
);
91 if (infile_size
!= 0) {
92 outbuf_size
= infile_size
;
93 iterations
= RUN_MEM_SIZE
/ infile_size
;
95 printf("Error: input file has 0 size\n");
98 if (iterations
< MIN_TEST_LOOPS
)
99 iterations
= MIN_TEST_LOOPS
;
101 tempbuf
= malloc(infile_size
);
102 if (tempbuf
== NULL
) {
103 fprintf(stderr
, "Can't allocate temp buffer memory\n");
106 inbuf_size
= 2 * infile_size
;
107 inbuf
= malloc(inbuf_size
);
109 fprintf(stderr
, "Can't allocate input buffer memory\n");
112 outbuf
= malloc(outbuf_size
);
113 if (outbuf
== NULL
) {
114 fprintf(stderr
, "Can't allocate output buffer memory\n");
118 fread(tempbuf
, 1, infile_size
, in
);
121 printf("Using zlib -9 compression\n");
122 i
= compress2(inbuf
, &inbuf_size
, tempbuf
, infile_size
, 9);
124 printf("Compression of input file failed\n");
130 #elif defined(ZLIB_1)
131 printf("Using zlib -1 compression\n");
132 i
= compress2(inbuf
, &inbuf_size
, tempbuf
, infile_size
, 1);
134 printf("Compression of input file failed\n");
141 printf("Using igzip compression\n");
142 struct isal_zstream stream
;
143 isal_deflate_init(&stream
);
144 stream
.end_of_stream
= 1; /* Do the entire file at once */
145 stream
.flush
= NO_FLUSH
;
146 stream
.next_in
= tempbuf
;
147 stream
.avail_in
= infile_size
;
148 stream
.next_out
= inbuf
;
149 stream
.avail_out
= inbuf_size
;
151 struct isal_huff_histogram histogram
;
152 struct isal_hufftables hufftables_custom
;
154 memset(&histogram
, 0, sizeof(histogram
));
155 isal_update_histogram(tempbuf
, infile_size
, &histogram
);
156 isal_create_hufftables(&hufftables_custom
, &histogram
);
157 stream
.hufftables
= &hufftables_custom
;
159 isal_deflate(&stream
);
160 if (stream
.avail_in
!= 0) {
161 printf("Compression of input file failed\n");
168 printf("igzip_zlib_inflate_perf: %s %d iterations\n", argv
[1], iterations
);
169 /* Read complete input file into buffer */
171 struct perf start
, stop
;
176 for (i
= 0; i
< iterations
; i
++) {
177 gstream
.next_in
= inbuf
;
178 gstream
.avail_in
= inbuf_size
;
179 gstream
.zalloc
= Z_NULL
;
180 gstream
.zfree
= Z_NULL
;
181 gstream
.opaque
= Z_NULL
;
182 if (0 != inflateInit2(&gstream
, -15)) {
183 printf("Fail zlib init\n");
186 gstream
.next_out
= outbuf
;
187 gstream
.avail_out
= outbuf_size
;
188 check
= inflate(&gstream
, Z_FINISH
);
190 printf("Error in decompression with error %d\n", check
);
195 printf(" file %s - in_size=%d out_size=%lu iter=%d\n", argv
[1],
196 infile_size
, gstream
.total_out
, i
);
198 printf("igzip_file: ");
199 perf_print(stop
, start
, (long long)infile_size
* i
);
201 printf("End of igzip_zlib_inflate_perf\n\n");
204 printf("isal_inflate_stateless_perf: %s %d iterations\n", argv
[1], iterations
);
205 /* Read complete input file into buffer */
207 struct perf start
, stop
;
210 for (i
= 0; i
< iterations
; i
++) {
211 isal_inflate_init(&state
);
212 state
.next_in
= inbuf
;
213 state
.avail_in
= inbuf_size
;
214 state
.next_out
= outbuf
;
215 state
.avail_out
= outbuf_size
;
217 check
= isal_inflate(&state
);
219 printf("Error in decompression with error %d\n", check
);
224 printf(" file %s - in_size=%d out_size=%d iter=%d\n", argv
[1],
225 infile_size
, state
.total_out
, i
);
227 printf("igzip_file: ");
228 perf_print(stop
, start
, (long long)infile_size
* i
);
230 printf("End of isal_inflate_stateless_perf\n\n");
233 #if defined(ZLIB_1) || defined(ZLIB_9)